1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2004 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 <vlc/input.h>
33 #include <vlc/decoder.h>
36 #include "input_internal.h"
38 #include "stream_output.h"
39 #include "vlc_playlist.h"
40 #include "vlc_interface.h"
41 #include "vlc_interaction.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Run ( input_thread_t *p_input );
49 static int RunAndClean ( input_thread_t *p_input );
51 static input_thread_t * Create ( vlc_object_t *, input_item_t *, char *,
53 static int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
54 static void Error ( input_thread_t *p_input );
55 static void End ( input_thread_t *p_input );
56 static void MainLoop( input_thread_t *p_input );
58 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
59 static void ControlReduce( input_thread_t * );
60 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
62 static int UpdateFromAccess( input_thread_t * );
63 static int UpdateFromDemux( input_thread_t * );
64 static int UpdateMeta( input_thread_t *, vlc_bool_t );
66 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
68 static void DecodeUrl( char * );
69 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
71 static input_source_t *InputSourceNew( input_thread_t *);
72 static int InputSourceInit( input_thread_t *, input_source_t *,
73 char *, char *psz_forced_demux,
75 static void InputSourceClean( input_thread_t *, input_source_t * );
77 static void SlaveDemux( input_thread_t *p_input );
78 static void SlaveSeek( input_thread_t *p_input );
80 static void InputMetaUser( input_thread_t *p_input );
82 /*****************************************************************************
83 * This function creates a new input, and returns a pointer
84 * to its description. On error, it returns NULL.
86 * Variables for _public_ use:
89 * - rate,rate-slower, rate-faster
90 * - position, position-offset
92 * - title,title-next,title-prev
93 * - chapter,chapter-next, chapter-prev
94 * - program, audio-es, video-es, spu-es
95 * - audio-delay, spu-delay
100 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
101 * * For intf callback upon changes
103 * TODO explain when Callback is called
104 * TODO complete this list (?)
105 *****************************************************************************/
106 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
107 char *psz_header, vlc_bool_t b_quick )
109 input_thread_t *p_input = NULL; /* thread descriptor */
113 /* Allocate descriptor */
114 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
115 if( p_input == NULL )
117 msg_Err( p_parent, "out of memory" );
120 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
122 /* Init Common fields */
123 p_input->b_eof = VLC_FALSE;
124 p_input->b_can_pace_control = VLC_TRUE;
125 p_input->i_start = 0;
128 p_input->i_title = 0;
129 p_input->title = NULL;
130 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
131 p_input->i_state = INIT_S;
132 p_input->i_rate = INPUT_RATE_DEFAULT;
133 p_input->i_bookmark = 0;
134 p_input->bookmark = NULL;
135 p_input->p_meta = NULL;
136 p_input->p_es_out = NULL;
137 p_input->p_sout = NULL;
138 p_input->b_out_pace_control = VLC_FALSE;
139 p_input->i_pts_delay = 0;
141 /* Init Input fields */
142 p_input->input.p_item = p_item;
143 p_input->input.p_access = NULL;
144 p_input->input.p_stream = NULL;
145 p_input->input.p_demux = NULL;
146 p_input->input.b_title_demux = VLC_FALSE;
147 p_input->input.i_title = 0;
148 p_input->input.title = NULL;
149 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
150 p_input->input.b_can_pace_control = VLC_TRUE;
151 p_input->input.b_eof = VLC_FALSE;
152 p_input->input.i_cr_average = 0;
154 if( !p_input->input.p_item->p_meta )
155 p_input->input.p_item->p_meta = vlc_meta_New();
156 stats_ReinitInputStats( p_item->p_stats );
159 p_input->i_slave = 0;
160 p_input->slave = NULL;
162 /* Init control buffer */
163 vlc_mutex_init( p_input, &p_input->lock_control );
164 p_input->i_control = 0;
166 /* Parse input options */
167 vlc_mutex_lock( &p_item->lock );
168 for( i = 0; i < p_item->i_options; i++ )
170 var_OptionParse( p_input, p_item->ppsz_options[i] );
172 vlc_mutex_unlock( &p_item->lock );
174 /* Create Object Variables for private use only */
175 input_ConfigVarInit( p_input );
177 /* Create Objects variables for public Get and Set */
178 input_ControlVarInit( p_input );
179 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
183 var_Get( p_input, "bookmarks", &val );
186 /* FIXME: have a common cfg parsing routine used by sout and others */
187 char *psz_parser, *psz_start, *psz_end;
188 psz_parser = val.psz_string;
189 while( (psz_start = strchr( psz_parser, '{' ) ) )
191 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
194 psz_end = strchr( psz_start, '}' );
195 if( !psz_end ) break;
196 psz_parser = psz_end + 1;
197 backup = *psz_parser;
200 while( (psz_end = strchr( psz_start, ',' ) ) )
203 if( !strncmp( psz_start, "name=", 5 ) )
205 p_seekpoint->psz_name = psz_start + 5;
207 else if( !strncmp( psz_start, "bytes=", 6 ) )
209 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
211 else if( !strncmp( psz_start, "time=", 5 ) )
213 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
216 psz_start = psz_end + 1;
218 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
219 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
220 p_seekpoint->i_time_offset );
221 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
222 vlc_seekpoint_Delete( p_seekpoint );
223 *psz_parser = backup;
225 free( val.psz_string );
229 /* Remove 'Now playing' info as it is probably outdated */
230 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT),
231 VLC_META_NOW_PLAYING ); /* ? Don't translate as it might has been copied ? */
237 * Initialize an input thread and run it. You will need to monitor the thread to clean
238 * up after it is done
240 * \param p_parent a vlc_object
241 * \param p_item an input item
242 * \return a pointer to the spawned input thread
244 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
245 input_item_t *p_item )
247 return __input_CreateThread2( p_parent, p_item, NULL );
252 input_thread_t *__input_CreateThread2( vlc_object_t *p_parent,
253 input_item_t *p_item,
256 input_thread_t *p_input = NULL; /* thread descriptor */
258 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
259 /* Now we can attach our new input */
260 vlc_object_attach( p_input, p_parent );
262 /* Create thread and wait for its readiness. */
263 if( vlc_thread_create( p_input, "input", Run,
264 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
266 msg_Err( p_input, "cannot create input thread" );
267 vlc_object_detach( p_input );
268 vlc_object_destroy( p_input );
277 * Initialize an input thread and run it. This thread will clean after himself,
278 * you can forget about it. It can work either in blocking or non-blocking mode
280 * \param p_parent a vlc_object
281 * \param p_item an input item
282 * \param b_block should we block until read is finished ?
283 * \return the input object id if non blocking, an error code else
285 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
288 input_thread_t *p_input = NULL; /* thread descriptor */
290 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
291 /* Now we can attach our new input */
292 vlc_object_attach( p_input, p_parent );
296 RunAndClean( p_input );
301 if( vlc_thread_create( p_input, "input", RunAndClean,
302 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
304 msg_Err( p_input, "cannot create input thread" );
305 vlc_object_detach( p_input );
306 vlc_object_destroy( p_input );
310 return p_input->i_object_id;
314 * Initialize an input and initialize it to preparse the item
315 * This function is blocking. It will only accept to parse files
317 * \param p_parent a vlc_object_t
318 * \param p_item an input item
319 * \return VLC_SUCCESS or an error
321 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
323 input_thread_t *p_input = NULL; /* thread descriptor */
325 /* Allocate descriptor */
326 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
327 p_input->i_flags |= OBJECT_FLAGS_NODBG;
328 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
330 /* Now we can attach our new input */
331 vlc_object_attach( p_input, p_parent );
333 Init( p_input, VLC_TRUE );
335 /* Clean up master */
336 InputSourceClean( p_input, &p_input->input );
338 /* Unload all modules */
339 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
341 vlc_object_detach( p_input );
342 vlc_object_destroy( p_input );
348 * Request a running input thread to stop and die
350 * \param the input thread to stop
352 void input_StopThread( input_thread_t *p_input )
357 /* Set die for input */
358 p_input->b_die = VLC_TRUE;
360 /* We cannot touch p_input fields directly (we can from another thread),
361 * so use the vlc_object_find way, it's perfectly safe */
363 /* Set die for all access */
364 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
365 for( i = 0; i < p_list->i_count; i++ )
367 p_list->p_values[i].p_object->b_die = VLC_TRUE;
369 vlc_list_release( p_list );
371 /* Set die for all stream */
372 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
373 for( i = 0; i < p_list->i_count; i++ )
375 p_list->p_values[i].p_object->b_die = VLC_TRUE;
377 vlc_list_release( p_list );
379 /* Set die for all demux */
380 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
381 for( i = 0; i < p_list->i_count; i++ )
383 p_list->p_values[i].p_object->b_die = VLC_TRUE;
385 vlc_list_release( p_list );
387 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
391 * Clean up a dead input thread
392 * This function does not return until the thread is effectively cancelled.
394 * \param the input thread to kill
396 void input_DestroyThread( input_thread_t *p_input )
398 /* Join the thread */
399 vlc_thread_join( p_input );
401 /* Delete input lock (only after thread joined) */
402 vlc_mutex_destroy( &p_input->lock_control );
404 /* TODO: maybe input_DestroyThread should also delete p_input instead
405 * of the playlist but I'm not sure if it's possible */
408 /*****************************************************************************
409 * Run: main thread loop
410 * This is the "normal" thread that spawns the input processing chain,
411 * reads the stream, cleans up and waits
412 *****************************************************************************/
413 static int Run( input_thread_t *p_input )
415 /* Signal that the thread is launched */
416 vlc_thread_ready( p_input );
418 if( Init( p_input, VLC_FALSE ) )
420 /* If we failed, wait before we are killed, and exit */
421 p_input->b_error = VLC_TRUE;
425 /* Tell we're dead */
426 p_input->b_dead = VLC_TRUE;
433 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
435 /* We have finish to demux data but not to play them */
436 while( !p_input->b_die )
438 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
441 msg_Dbg( p_input, "waiting decoder fifos to empty" );
443 msleep( INPUT_IDLE_SLEEP );
446 /* We have finished */
447 p_input->b_eof = VLC_TRUE;
450 /* Wait we are asked to die */
451 if( !p_input->b_die )
462 /*****************************************************************************
463 * RunAndClean: main thread loop
464 * This is the "just forget me" thread that spawns the input processing chain,
465 * reads the stream, cleans up and releases memory
466 *****************************************************************************/
467 static int RunAndClean( input_thread_t *p_input )
469 /* Signal that the thread is launched */
470 vlc_thread_ready( p_input );
472 if( Init( p_input, VLC_FALSE ) )
474 /* If we failed, just exit */
480 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
482 /* We have finish to demux data but not to play them */
483 while( !p_input->b_die )
485 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
488 msg_Dbg( p_input, "waiting decoder fifos to empty" );
490 msleep( INPUT_IDLE_SLEEP );
492 /* We have finished */
493 p_input->b_eof = VLC_TRUE;
500 vlc_object_detach( p_input );
501 vlc_object_destroy( p_input );
507 /*****************************************************************************
508 * Main loop: Fill buffers from access, and demux
509 *****************************************************************************/
510 static void MainLoop( input_thread_t *p_input )
512 int64_t i_intf_update = 0;
513 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
515 vlc_bool_t b_force_update = VLC_FALSE;
521 if( p_input->i_state != PAUSE_S )
523 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
524 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
531 if( p_input->input.b_title_demux &&
532 p_input->input.p_demux->info.i_update )
534 i_ret = UpdateFromDemux( p_input );
535 b_force_update = VLC_TRUE;
537 else if( !p_input->input.b_title_demux &&
538 p_input->input.p_access &&
539 p_input->input.p_access->info.i_update )
541 i_ret = UpdateFromAccess( p_input );
542 b_force_update = VLC_TRUE;
546 if( i_ret == 0 ) /* EOF */
550 var_Get( p_input, "input-repeat", &repeat );
551 if( repeat.i_int == 0 )
553 /* End of file - we do not set b_die because only the
554 * playlist is allowed to do so. */
555 msg_Dbg( p_input, "EOF reached" );
556 p_input->input.b_eof = VLC_TRUE;
560 msg_Dbg( p_input, "repeating the same input (%d)",
562 if( repeat.i_int > 0 )
565 var_Set( p_input, "input-repeat", repeat );
568 /* Seek to start title/seekpoint */
569 val.i_int = p_input->input.i_title_start -
570 p_input->input.i_title_offset;
571 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
573 input_ControlPush( p_input,
574 INPUT_CONTROL_SET_TITLE, &val );
576 val.i_int = p_input->input.i_seekpoint_start -
577 p_input->input.i_seekpoint_offset;
578 if( val.i_int > 0 /* TODO: check upper boundary */ )
579 input_ControlPush( p_input,
580 INPUT_CONTROL_SET_SEEKPOINT, &val );
582 /* Seek to start position */
583 if( p_input->i_start > 0 )
585 val.i_time = p_input->i_start;
586 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
592 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
599 p_input->b_error = VLC_TRUE;
602 if( i_ret > 0 && p_input->i_slave > 0 )
604 SlaveDemux( p_input );
614 vlc_mutex_lock( &p_input->lock_control );
615 ControlReduce( p_input );
616 while( !ControlPopNoLock( p_input, &i_type, &val ) )
618 msg_Dbg( p_input, "control type=%d", i_type );
619 if( Control( p_input, i_type, val ) )
620 b_force_update = VLC_TRUE;
622 vlc_mutex_unlock( &p_input->lock_control );
624 if( b_force_update || i_intf_update < mdate() )
628 int64_t i_time, i_length;
629 /* update input status variables */
630 if( !demux2_Control( p_input->input.p_demux,
631 DEMUX_GET_POSITION, &f_pos ) )
633 val.f_float = (float)f_pos;
634 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
636 if( !demux2_Control( p_input->input.p_demux,
637 DEMUX_GET_TIME, &i_time ) )
639 p_input->i_time = i_time;
641 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
643 if( !demux2_Control( p_input->input.p_demux,
644 DEMUX_GET_LENGTH, &i_length ) )
647 var_Get( p_input, "length", &old_val );
648 val.i_time = i_length;
649 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
651 if( old_val.i_time != val.i_time )
653 UpdateItemLength( p_input, i_length, VLC_TRUE );
657 var_SetBool( p_input, "intf-change", VLC_TRUE );
658 i_intf_update = mdate() + I64C(150000);
664 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
674 /* Initialize optional stream output. (before access/demuxer)
675 * XXX: we add a special case if the uri starts by vlc.
676 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
677 * be destroyed in case of a file).
678 * (this will break playing of file starting by 'vlc:' but I don't
679 * want to add more logic, just force file by file:// or code it ;)
683 /* Prepare statistics */
684 #define INIT_COUNTER( p, type, compute ) p_input->counters.p_##p = \
685 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
687 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
688 INIT_COUNTER( read_packets, INTEGER, COUNTER );
689 INIT_COUNTER( demux_read, INTEGER, COUNTER );
690 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
691 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
692 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
693 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
694 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
695 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
696 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
697 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
698 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
699 p_input->counters.p_sout_send_bitrate = NULL;
700 p_input->counters.p_sout_sent_packets = NULL;
701 p_input->counters.p_sout_sent_bytes = NULL;
702 if( p_input->counters.p_demux_bitrate )
703 p_input->counters.p_demux_bitrate->update_interval = 1000000;
704 if( p_input->counters.p_input_bitrate )
705 p_input->counters.p_input_bitrate->update_interval = 1000000;
706 vlc_mutex_init( p_input, &p_input->counters.counters_lock );
709 psz = var_GetString( p_input, "sout" );
710 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
712 p_input->p_sout = sout_NewInstance( p_input, psz );
713 if( p_input->p_sout == NULL )
715 msg_Err( p_input, "cannot start stream output instance, " \
720 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
721 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
722 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
723 if( p_input->counters.p_sout_send_bitrate )
724 p_input->counters.p_sout_send_bitrate->update_interval = 1000000;
730 p_input->p_es_out = input_EsOutNew( p_input );
731 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
732 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
734 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
735 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
737 if( InputSourceInit( p_input, &p_input->input,
738 p_input->input.p_item->psz_uri, NULL, b_quick ) )
743 /* Create global title (from master) */
746 p_input->i_title = p_input->input.i_title;
747 p_input->title = p_input->input.title;
748 p_input->i_title_offset = p_input->input.i_title_offset;
749 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
750 if( p_input->i_title > 0 )
752 /* Setup variables */
753 input_ControlVarNavigation( p_input );
754 input_ControlVarTitle( p_input, 0 );
758 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
759 p_input->b_can_pause = p_input->input.b_can_pause;
762 if( p_input->i_pts_delay < 0 )
763 p_input->i_pts_delay = 0;
765 /* If the desynchronisation requested by the user is < 0, we need to
766 * cache more data. */
767 var_Get( p_input, "audio-desync", &val );
768 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
770 /* Update cr_average depending on the caching */
771 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
772 p_input->input.i_cr_average /= 10;
773 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
776 /* Load master infos */
778 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
779 &val.i_time ) && val.i_time > 0 )
781 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
782 UpdateItemLength( p_input, val.i_time, b_quick );
783 p_input->input.p_item->i_duration = val.i_time;
786 /* Start title/chapter */
789 val.i_int = p_input->input.i_title_start -
790 p_input->input.i_title_offset;
791 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
792 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
793 val.i_int = p_input->input.i_seekpoint_start -
794 p_input->input.i_seekpoint_offset;
795 if( val.i_int > 0 /* TODO: check upper boundary */ )
796 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
800 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
802 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
805 if( p_input->i_start > 0 )
807 if( p_input->i_start >= val.i_time )
809 msg_Warn( p_input, "invalid start-time ignored" );
815 msg_Dbg( p_input, "starting at time: %ds",
816 (int)( p_input->i_start / I64C(1000000) ) );
818 s.i_time = p_input->i_start;
819 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
822 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
824 msg_Warn( p_input, "invalid stop-time ignored" );
830 /* Get fps and set it if not already set */
831 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
834 float f_requested_fps;
836 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
837 var_SetFloat( p_input, "sub-original-fps", f_fps );
839 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
840 if( f_requested_fps != f_fps )
842 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
844 var_SetFloat( p_input, "sub-fps", f_requested_fps );
848 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
851 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
855 /* Look for and add subtitle files */
856 psz_subtitle = var_GetString( p_input, "sub-file" );
859 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
860 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
863 var_Get( p_input, "sub-autodetect-file", &val );
866 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
867 char **subs = subtitles_Detect( p_input, psz_autopath,
868 p_input->input.p_item->psz_uri );
873 /* Try to autoselect the first autodetected subtitles file
874 * if no subtitles file was specified */
875 if( *psz_subtitle == 0 && subs && subs[0] )
877 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
882 /* Then, just add the following subtitles files */
883 for( ; subs && subs[i]; i++ )
885 if( strcmp( psz_subtitle, subs[i] ) )
887 sub = InputSourceNew( p_input );
888 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
891 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
896 if( subs ) free( subs );
897 if( psz_autopath ) free( psz_autopath );
899 free( psz_subtitle );
902 psz = var_GetString( p_input, "input-slave" );
906 input_source_t *slave;
909 while( *psz == ' ' || *psz == '#' )
913 if( ( psz_delim = strchr( psz, '#' ) ) )
922 msg_Dbg( p_input, "adding slave input '%s'", psz );
923 slave = InputSourceNew( p_input );
924 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
926 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
931 if( psz ) free( psz );
935 p_input->i_start = 0;
936 p_input->i_start = 0;
942 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
943 i_es_out_mode = ES_OUT_MODE_AUTO;
945 if( p_input->p_sout )
947 var_Get( p_input, "sout-all", &val );
950 i_es_out_mode = ES_OUT_MODE_ALL;
955 var_Get( p_input, "programs", &val );
956 if ( val.p_list && val.p_list->i_count )
958 i_es_out_mode = ES_OUT_MODE_PARTIAL;
959 /* Note : we should remove the "program" callback. */
962 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
966 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
968 /* Inform the demuxer about waited group (needed only for DVB) */
969 if( i_es_out_mode == ES_OUT_MODE_ALL )
971 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
973 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
975 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
980 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
981 (int) var_GetInteger( p_input, "program" ), NULL );
984 if( p_input->p_sout )
986 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
988 p_input->b_out_pace_control = VLC_FALSE;
992 p_input->b_out_pace_control = VLC_TRUE;
995 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
997 /* We don't want a high input priority here or we'll
998 * end-up sucking up all the CPU time */
999 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1002 msg_Dbg( p_input, "starting in %s mode",
1003 p_input->b_out_pace_control ? "asynch" : "synch" );
1007 p_meta = p_input->input.p_item->p_meta;
1008 /* Get meta data from users */
1009 InputMetaUser( p_input );
1010 /* Get meta data from master input */
1011 demux2_Control( p_input->input.p_demux, DEMUX_GET_META, p_meta );
1013 /* Access_file does not give any meta, and there are no slave */
1016 if( p_input->input.p_access )
1017 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1020 /* Get meta data from slave input */
1021 for( i = 0; i < p_input->i_slave; i++ )
1023 demux2_Control( p_input->slave[i]->p_demux,
1024 DEMUX_GET_META, p_meta );
1025 if( p_input->slave[i]->p_access )
1027 access2_Control( p_input->slave[i]->p_access,
1028 ACCESS_GET_META, p_meta );
1033 UpdateMeta( p_input, b_quick );
1037 msg_Dbg( p_input, "`%s' successfully opened",
1038 p_input->input.p_item->psz_uri );
1042 /* initialization is complete */
1043 p_input->i_state = PLAYING_S;
1045 val.i_int = PLAYING_S;
1046 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1051 if( p_input->p_es_out )
1052 input_EsOutDelete( p_input->p_es_out );
1054 if( p_input->p_sout )
1055 sout_DeleteInstance( p_input->p_sout );
1057 /* Mark them deleted */
1058 p_input->input.p_demux = NULL;
1059 p_input->input.p_stream = NULL;
1060 p_input->input.p_access = NULL;
1061 p_input->p_es_out = NULL;
1062 p_input->p_sout = NULL;
1064 return VLC_EGENERIC;
1067 /*****************************************************************************
1068 * Error: RunThread() error loop
1069 *****************************************************************************
1070 * This function is called when an error occurred during thread main's loop.
1071 *****************************************************************************/
1072 static void Error( input_thread_t *p_input )
1074 while( !p_input->b_die )
1077 msleep( INPUT_IDLE_SLEEP );
1081 /*****************************************************************************
1082 * End: end the input thread
1083 *****************************************************************************/
1084 static void End( input_thread_t * p_input )
1089 msg_Dbg( p_input, "closing input" );
1091 /* We are at the end */
1092 p_input->i_state = END_S;
1095 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1097 /* Clean control variables */
1098 input_ControlVarClean( p_input );
1100 /* Clean up master */
1101 InputSourceClean( p_input, &p_input->input );
1104 for( i = 0; i < p_input->i_slave; i++ )
1106 InputSourceClean( p_input, p_input->slave[i] );
1107 free( p_input->slave[i] );
1109 if( p_input->slave ) free( p_input->slave );
1111 /* Unload all modules */
1112 if( p_input->p_es_out )
1113 input_EsOutDelete( p_input->p_es_out );
1115 #define CL_CO( c ) stats_CounterClean( p_input->counters.p_##c )
1117 CL_CO( read_bytes );
1118 CL_CO( read_packets );
1119 CL_CO( demux_read );
1120 CL_CO( input_bitrate );
1121 CL_CO( demux_bitrate );
1122 CL_CO( played_abuffers );
1123 CL_CO( lost_abuffers );
1124 CL_CO( displayed_pictures );
1125 CL_CO( lost_pictures );
1126 CL_CO( decoded_audio) ;
1127 CL_CO( decoded_video );
1128 CL_CO( decoded_sub) ;
1130 /* Close optional stream output instance */
1131 if( p_input->p_sout )
1133 vlc_object_t *p_pl =
1134 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1137 CL_CO( sout_sent_packets );
1138 CL_CO( sout_sent_bytes );
1139 CL_CO( sout_send_bitrate );
1141 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1143 /* attach sout to the playlist */
1144 msg_Dbg( p_input, "keeping sout" );
1145 vlc_object_detach( p_input->p_sout );
1146 vlc_object_attach( p_input->p_sout, p_pl );
1150 msg_Dbg( p_input, "destroying sout" );
1151 sout_DeleteInstance( p_input->p_sout );
1154 vlc_object_release( p_pl );
1158 /* Tell we're dead */
1159 p_input->b_dead = VLC_TRUE;
1162 /*****************************************************************************
1164 *****************************************************************************/
1165 static inline int ControlPopNoLock( input_thread_t *p_input,
1166 int *pi_type, vlc_value_t *p_val )
1168 if( p_input->i_control <= 0 )
1170 return VLC_EGENERIC;
1173 *pi_type = p_input->control[0].i_type;
1174 *p_val = p_input->control[0].val;
1176 p_input->i_control--;
1177 if( p_input->i_control > 0 )
1181 for( i = 0; i < p_input->i_control; i++ )
1183 p_input->control[i].i_type = p_input->control[i+1].i_type;
1184 p_input->control[i].val = p_input->control[i+1].val;
1191 static void ControlReduce( input_thread_t *p_input )
1194 for( i = 1; i < p_input->i_control; i++ )
1196 const int i_lt = p_input->control[i-1].i_type;
1197 const int i_ct = p_input->control[i].i_type;
1199 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1200 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1204 ( i_ct == INPUT_CONTROL_SET_STATE ||
1205 i_ct == INPUT_CONTROL_SET_RATE ||
1206 i_ct == INPUT_CONTROL_SET_POSITION ||
1207 i_ct == INPUT_CONTROL_SET_TIME ||
1208 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1209 i_ct == INPUT_CONTROL_SET_TITLE ||
1210 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1211 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1214 // msg_Dbg( p_input, "merged at %d", i );
1215 /* Remove the i-1 */
1216 for( j = i; j < p_input->i_control; j++ )
1217 p_input->control[j-1] = p_input->control[j];
1218 p_input->i_control--;
1222 /* TODO but that's not that important
1223 - merge SET_X with SET_X_CMD
1224 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1225 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1232 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1235 vlc_bool_t b_force_update = VLC_FALSE;
1237 if( !p_input ) return b_force_update;
1241 case INPUT_CONTROL_SET_DIE:
1242 msg_Dbg( p_input, "control: stopping input" );
1243 /* Mark all submodules to die */
1244 if( p_input->input.p_access )
1245 p_input->input.p_access->b_die = VLC_TRUE;
1246 if( p_input->input.p_stream )
1247 p_input->input.p_stream->b_die = VLC_TRUE;
1248 p_input->input.p_demux->b_die = VLC_TRUE;
1250 p_input->b_die = VLC_TRUE;
1253 case INPUT_CONTROL_SET_POSITION:
1254 case INPUT_CONTROL_SET_POSITION_OFFSET:
1257 if( i_type == INPUT_CONTROL_SET_POSITION )
1259 f_pos = val.f_float;
1263 /* Should not fail */
1264 demux2_Control( p_input->input.p_demux,
1265 DEMUX_GET_POSITION, &f_pos );
1266 f_pos += val.f_float;
1268 if( f_pos < 0.0 ) f_pos = 0.0;
1269 if( f_pos > 1.0 ) f_pos = 1.0;
1270 /* Reset the decoders states and clock synch (before calling the demuxer */
1271 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1272 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1273 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1276 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1277 "%2.1f%% failed", f_pos * 100 );
1281 if( p_input->i_slave > 0 )
1282 SlaveSeek( p_input );
1284 b_force_update = VLC_TRUE;
1289 case INPUT_CONTROL_SET_TIME:
1290 case INPUT_CONTROL_SET_TIME_OFFSET:
1295 if( i_type == INPUT_CONTROL_SET_TIME )
1297 i_time = val.i_time;
1301 /* Should not fail */
1302 demux2_Control( p_input->input.p_demux,
1303 DEMUX_GET_TIME, &i_time );
1304 i_time += val.i_time;
1306 if( i_time < 0 ) i_time = 0;
1308 /* Reset the decoders states and clock synch (before calling the demuxer */
1309 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1310 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1312 i_ret = demux2_Control( p_input->input.p_demux,
1313 DEMUX_SET_TIME, i_time );
1318 /* Emulate it with a SET_POS */
1319 demux2_Control( p_input->input.p_demux,
1320 DEMUX_GET_LENGTH, &i_length );
1323 double f_pos = (double)i_time / (double)i_length;
1324 i_ret = demux2_Control( p_input->input.p_demux,
1325 DEMUX_SET_POSITION, f_pos );
1330 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1331 " failed or not possible", i_time );
1335 if( p_input->i_slave > 0 )
1336 SlaveSeek( p_input );
1338 b_force_update = VLC_TRUE;
1343 case INPUT_CONTROL_SET_STATE:
1344 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1345 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1348 if( p_input->input.p_access )
1349 i_ret = access2_Control( p_input->input.p_access,
1350 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1352 i_ret = demux2_Control( p_input->input.p_demux,
1353 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1357 /* FIXME What to do ? */
1358 msg_Warn( p_input, "cannot unset pause -> EOF" );
1359 vlc_mutex_unlock( &p_input->lock_control );
1360 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1361 vlc_mutex_lock( &p_input->lock_control );
1364 b_force_update = VLC_TRUE;
1366 /* Switch to play */
1367 p_input->i_state = PLAYING_S;
1368 val.i_int = PLAYING_S;
1369 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1372 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1373 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1375 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1376 p_input->b_can_pause )
1379 if( p_input->input.p_access )
1380 i_ret = access2_Control( p_input->input.p_access,
1381 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1383 i_ret = demux2_Control( p_input->input.p_demux,
1384 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1386 b_force_update = VLC_TRUE;
1390 msg_Warn( p_input, "cannot set pause state" );
1391 val.i_int = p_input->i_state;
1395 val.i_int = PAUSE_S;
1398 /* Switch to new state */
1399 p_input->i_state = val.i_int;
1400 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1402 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1404 b_force_update = VLC_TRUE;
1406 /* Correct "state" value */
1407 val.i_int = p_input->i_state;
1408 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1410 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1412 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1416 case INPUT_CONTROL_SET_RATE:
1417 case INPUT_CONTROL_SET_RATE_SLOWER:
1418 case INPUT_CONTROL_SET_RATE_FASTER:
1422 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1423 i_rate = p_input->i_rate * 2;
1424 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1425 i_rate = p_input->i_rate / 2;
1429 if( i_rate < INPUT_RATE_MIN )
1431 msg_Dbg( p_input, "cannot set rate faster" );
1432 i_rate = INPUT_RATE_MIN;
1434 else if( i_rate > INPUT_RATE_MAX )
1436 msg_Dbg( p_input, "cannot set rate slower" );
1437 i_rate = INPUT_RATE_MAX;
1439 if( i_rate != INPUT_RATE_DEFAULT &&
1440 ( !p_input->b_can_pace_control ||
1441 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1443 msg_Dbg( p_input, "cannot change rate" );
1444 i_rate = INPUT_RATE_DEFAULT;
1446 if( i_rate != p_input->i_rate )
1448 p_input->i_rate = i_rate;
1450 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1452 /* We haven't send data to decoder when rate != default */
1453 if( i_rate == INPUT_RATE_DEFAULT )
1454 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1457 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1459 b_force_update = VLC_TRUE;
1464 case INPUT_CONTROL_SET_PROGRAM:
1465 /* No need to force update, es_out does it if needed */
1466 es_out_Control( p_input->p_es_out,
1467 ES_OUT_SET_GROUP, val.i_int );
1469 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1473 case INPUT_CONTROL_SET_ES:
1474 /* No need to force update, es_out does it if needed */
1475 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1476 input_EsOutGetFromID( p_input->p_es_out,
1480 case INPUT_CONTROL_SET_AUDIO_DELAY:
1481 input_EsOutSetDelay( p_input->p_es_out,
1482 AUDIO_ES, val.i_time );
1483 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1486 case INPUT_CONTROL_SET_SPU_DELAY:
1487 input_EsOutSetDelay( p_input->p_es_out,
1488 SPU_ES, val.i_time );
1489 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1492 case INPUT_CONTROL_SET_TITLE:
1493 case INPUT_CONTROL_SET_TITLE_NEXT:
1494 case INPUT_CONTROL_SET_TITLE_PREV:
1495 if( p_input->input.b_title_demux &&
1496 p_input->input.i_title > 0 )
1499 /* FIXME handle demux title */
1500 demux_t *p_demux = p_input->input.p_demux;
1503 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1504 i_title = p_demux->info.i_title - 1;
1505 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1506 i_title = p_demux->info.i_title + 1;
1508 i_title = val.i_int;
1510 if( i_title >= 0 && i_title < p_input->input.i_title )
1512 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1513 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1515 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1516 input_ControlVarTitle( p_input, i_title );
1519 else if( p_input->input.i_title > 0 )
1521 access_t *p_access = p_input->input.p_access;
1524 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1525 i_title = p_access->info.i_title - 1;
1526 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1527 i_title = p_access->info.i_title + 1;
1529 i_title = val.i_int;
1531 if( i_title >= 0 && i_title < p_input->input.i_title )
1533 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1534 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1536 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1537 stream_AccessReset( p_input->input.p_stream );
1541 case INPUT_CONTROL_SET_SEEKPOINT:
1542 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1543 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1544 if( p_input->input.b_title_demux &&
1545 p_input->input.i_title > 0 )
1547 demux_t *p_demux = p_input->input.p_demux;
1549 int64_t i_input_time;
1550 int64_t i_seekpoint_time;
1552 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1554 i_seekpoint = p_demux->info.i_seekpoint;
1555 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1556 if( i_seekpoint_time >= 0 &&
1557 !demux2_Control( p_demux,
1558 DEMUX_GET_TIME, &i_input_time ) )
1560 if ( i_input_time < i_seekpoint_time + 3000000 )
1566 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1567 i_seekpoint = p_demux->info.i_seekpoint + 1;
1569 i_seekpoint = val.i_int;
1571 if( i_seekpoint >= 0 && i_seekpoint <
1572 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1574 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1575 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1577 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1580 else if( p_input->input.i_title > 0 )
1582 demux_t *p_demux = p_input->input.p_demux;
1583 access_t *p_access = p_input->input.p_access;
1585 int64_t i_input_time;
1586 int64_t i_seekpoint_time;
1588 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1590 i_seekpoint = p_access->info.i_seekpoint;
1591 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1592 if( i_seekpoint_time >= 0 &&
1593 demux2_Control( p_demux,
1594 DEMUX_GET_TIME, &i_input_time ) )
1596 if ( i_input_time < i_seekpoint_time + 3000000 )
1602 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1603 i_seekpoint = p_access->info.i_seekpoint + 1;
1605 i_seekpoint = val.i_int;
1607 if( i_seekpoint >= 0 && i_seekpoint <
1608 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1610 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1611 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1613 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1615 stream_AccessReset( p_input->input.p_stream );
1620 case INPUT_CONTROL_ADD_SLAVE:
1621 if( val.psz_string )
1623 input_source_t *slave = InputSourceNew( p_input );
1625 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1628 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1632 msg_Dbg( p_input, "adding %s as slave on the fly",
1636 if( demux2_Control( p_input->input.p_demux,
1637 DEMUX_GET_TIME, &i_time ) )
1639 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1640 InputSourceClean( p_input, slave );
1644 if( demux2_Control( slave->p_demux,
1645 DEMUX_SET_TIME, i_time ) )
1647 msg_Err( p_input, "seek failed for new slave" );
1648 InputSourceClean( p_input, slave );
1653 /* Get meta (access and demux) */
1654 access2_Control( slave->p_access, ACCESS_GET_META,
1656 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1657 UpdateMeta( p_input, VLC_FALSE );
1659 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1663 msg_Warn( p_input, "failed to add %s as slave",
1667 free( val.psz_string );
1671 case INPUT_CONTROL_SET_BOOKMARK:
1673 msg_Err( p_input, "not yet implemented" );
1677 return b_force_update;
1680 /*****************************************************************************
1682 *****************************************************************************/
1683 static int UpdateFromDemux( input_thread_t *p_input )
1685 demux_t *p_demux = p_input->input.p_demux;
1688 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1690 v.i_int = p_demux->info.i_title;
1691 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1693 input_ControlVarTitle( p_input, p_demux->info.i_title );
1695 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1697 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1699 v.i_int = p_demux->info.i_seekpoint;
1700 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1702 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1704 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1706 /* Hmmm only works with master input */
1707 if( p_input->input.p_demux == p_demux )
1709 int i_title_end = p_input->input.i_title_end -
1710 p_input->input.i_title_offset;
1711 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1712 p_input->input.i_seekpoint_offset;
1714 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1716 if( p_demux->info.i_title > i_title_end ||
1717 ( p_demux->info.i_title == i_title_end &&
1718 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1720 else if( i_seekpoint_end >=0 )
1722 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1724 else if( i_title_end >= 0 )
1726 if( p_demux->info.i_title > i_title_end ) return 0;
1733 /*****************************************************************************
1735 *****************************************************************************/
1736 static int UpdateFromAccess( input_thread_t *p_input )
1738 access_t *p_access = p_input->input.p_access;
1741 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1743 v.i_int = p_access->info.i_title;
1744 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1746 input_ControlVarTitle( p_input, p_access->info.i_title );
1748 stream_AccessUpdate( p_input->input.p_stream );
1750 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1752 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1754 v.i_int = p_access->info.i_seekpoint;
1755 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1756 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1758 if( p_access->info.i_update & INPUT_UPDATE_META )
1760 /* TODO maybe multi - access ? */
1761 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1762 access2_Control( p_input->input.p_access,ACCESS_GET_META, p_meta );
1763 UpdateMeta( p_input, VLC_FALSE );
1764 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1765 p_access->info.i_update &= ~INPUT_UPDATE_META;
1768 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1770 /* Hmmm only works with master input */
1771 if( p_input->input.p_access == p_access )
1773 int i_title_end = p_input->input.i_title_end -
1774 p_input->input.i_title_offset;
1775 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1776 p_input->input.i_seekpoint_offset;
1778 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1780 if( p_access->info.i_title > i_title_end ||
1781 ( p_access->info.i_title == i_title_end &&
1782 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1784 else if( i_seekpoint_end >=0 )
1786 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1788 else if( i_title_end >= 0 )
1790 if( p_access->info.i_title > i_title_end ) return 0;
1797 /*****************************************************************************
1799 *****************************************************************************/
1800 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1802 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1806 if( p_meta->psz_title && !p_input->input.p_item->b_fixed_name )
1807 input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1809 /** \todo handle sout meta */
1814 /*****************************************************************************
1816 *****************************************************************************/
1817 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1818 vlc_bool_t b_quick )
1820 playlist_t *p_playlist;
1821 char psz_buffer[MSTRTIME_MAX_SIZE];
1823 vlc_mutex_lock( &p_input->input.p_item->lock );
1824 p_input->input.p_item->i_duration = i_length;
1825 vlc_mutex_unlock( &p_input->input.p_item->lock );
1827 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1831 var_SetInteger( p_playlist, "item-change",
1832 p_input->input.p_item->i_id );
1833 vlc_object_release( p_playlist );
1836 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1837 msecstotimestr( psz_buffer, i_length / 1000 ) );
1840 /*****************************************************************************
1842 *****************************************************************************/
1843 static input_source_t *InputSourceNew( input_thread_t *p_input )
1845 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1849 msg_Err( p_input, "out of memory for new input source" );
1854 in->p_access = NULL;
1855 in->p_stream = NULL;
1857 in->b_title_demux = VLC_FALSE;
1860 in->b_can_pace_control = VLC_TRUE;
1861 in->b_eof = VLC_FALSE;
1862 in->i_cr_average = 0;
1867 /*****************************************************************************
1869 *****************************************************************************/
1870 static int InputSourceInit( input_thread_t *p_input,
1871 input_source_t *in, char *psz_mrl,
1872 char *psz_forced_demux, vlc_bool_t b_quick )
1874 char *psz_dup = strdup( psz_mrl );
1882 if( !in ) return VLC_EGENERIC;
1887 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1888 &psz_access, &psz_demux, &psz_path );
1890 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1891 psz_mrl, psz_access, psz_demux, psz_path );
1893 /* Hack to allow udp://@:port syntax */
1895 (strncmp( psz_access, "udp", 3 ) &&
1896 strncmp( psz_access, "rtp", 3 )) )
1898 /* Find optional titles and seekpoints */
1899 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1900 &in->i_seekpoint_start, &in->i_seekpoint_end );
1902 if( psz_forced_demux && *psz_forced_demux )
1904 psz_demux = psz_forced_demux;
1906 else if( !psz_demux || *psz_demux == '\0' )
1908 /* special hack for forcing a demuxer with --demux=module
1909 * (and do nothing with a list) */
1910 char *psz_var_demux = var_GetString( p_input, "demux" );
1912 if( *psz_var_demux != '\0' &&
1913 !strchr(psz_var_demux, ',' ) &&
1914 !strchr(psz_var_demux, ':' ) )
1916 psz_demux = psz_var_demux;
1918 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
1920 else if( psz_var_demux )
1922 free( psz_var_demux );
1926 /* Try access_demux if no demux given */
1927 if( *psz_demux == '\0' )
1929 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1930 NULL, p_input->p_es_out, VLC_FALSE );
1936 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
1937 psz_demux = strdup( "" );
1938 psz_access = strdup( "file" );
1943 int64_t i_pts_delay;
1945 /* Get infos from access_demux */
1946 demux2_Control( in->p_demux,
1947 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1948 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1950 in->b_title_demux = VLC_TRUE;
1951 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1952 &in->title, &in->i_title,
1953 &in->i_title_offset, &in->i_seekpoint_offset ) )
1958 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1959 &in->b_can_pace_control );
1960 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1964 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1970 int64_t i_pts_delay;
1972 /* Now try a real access */
1973 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
1976 /* Access failed, URL encoded ? */
1977 if( in->p_access == NULL && strchr( psz_path, '%' ) )
1979 DecodeUrl( psz_path );
1981 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
1982 psz_access, psz_demux, psz_path );
1984 in->p_access = access2_New( p_input,
1985 psz_access, psz_demux, psz_path,
1988 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
1989 * are forbidden in filenames on Win32. */
1991 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1992 if( in->p_access == NULL &&
1993 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1995 if( psz_dup ) free( psz_dup );
1996 psz_dup = strdup( psz_mrl );
1998 if( psz_forced_demux && *psz_forced_demux )
2000 psz_demux = psz_forced_demux;
2002 else psz_demux = "";
2005 in->p_access = access2_New( p_input,
2006 psz_access, psz_demux, psz_path,
2011 if( in->p_access == NULL )
2013 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2014 intf_UserFatal( VLC_OBJECT( p_input),
2015 _("Errors"),"Unable to open '%s'", psz_mrl );
2020 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2021 while( psz && *psz )
2023 access_t *p_access = in->p_access;
2024 char *end = strchr( psz, ':' );
2029 in->p_access = access2_FilterNew( in->p_access, psz );
2030 if( in->p_access == NULL )
2032 in->p_access = p_access;
2033 msg_Warn( p_input, "failed to insert access filter %s",
2039 if( psz_tmp ) free( psz_tmp );
2041 /* Get infos from access */
2044 access2_Control( in->p_access,
2045 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2046 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2048 in->b_title_demux = VLC_FALSE;
2049 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2050 &in->title, &in->i_title,
2051 &in->i_title_offset, &in->i_seekpoint_offset ) )
2057 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2058 &in->b_can_pace_control );
2059 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2061 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2063 var_Set( p_input, "seekable", val );
2066 /* Create the stream_t */
2067 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2068 if( in->p_stream == NULL )
2070 msg_Warn( p_input, "cannot create a stream_t from access" );
2074 /* Open a demuxer */
2075 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2077 psz_demux = in->p_access->psz_demux;
2079 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2080 in->p_stream, p_input->p_es_out, b_quick );
2081 if( in->p_demux == NULL )
2083 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2084 psz_access, psz_demux, psz_path );
2085 intf_UserFatal( VLC_OBJECT( p_input), _("Errors"),
2086 "Unrecognized format for '%s'", psz_mrl );
2090 /* TODO get title from demux */
2091 if( !b_quick && in->i_title <= 0 )
2093 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2094 &in->title, &in->i_title,
2095 &in->i_title_offset, &in->i_seekpoint_offset ))
2102 in->b_title_demux = VLC_TRUE;
2107 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2108 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2110 if( psz_dup ) free( psz_dup );
2115 demux2_Delete( in->p_demux );
2118 stream_Delete( in->p_stream );
2121 access2_Delete( in->p_access );
2122 if( psz_dup ) free( psz_dup );
2124 return VLC_EGENERIC;
2127 /*****************************************************************************
2129 *****************************************************************************/
2130 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2133 demux2_Delete( in->p_demux );
2136 stream_Delete( in->p_stream );
2139 access2_Delete( in->p_access );
2141 if( in->i_title > 0 )
2144 for( i = 0; i < in->i_title; i++ )
2146 vlc_input_title_Delete( in->title[i] );
2152 static void SlaveDemux( input_thread_t *p_input )
2157 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2159 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2163 for( i = 0; i < p_input->i_slave; i++ )
2165 input_source_t *in = p_input->slave[i];
2171 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2176 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2178 msg_Err( p_input, "slave[%d] doesn't like "
2179 "DEMUX_GET_TIME -> EOF", i );
2184 if( i_stime >= i_time )
2187 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2193 i_ret = in->p_demux->pf_demux( in->p_demux );
2198 msg_Dbg( p_input, "slave %d EOF", i );
2199 in->b_eof = VLC_TRUE;
2204 static void SlaveSeek( input_thread_t *p_input )
2209 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2211 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2215 for( i = 0; i < p_input->i_slave; i++ )
2217 input_source_t *in = p_input->slave[i];
2219 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2221 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2222 in->b_eof = VLC_TRUE;
2226 /*****************************************************************************
2228 *****************************************************************************/
2229 static void InputMetaUser( input_thread_t *p_input )
2231 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
2234 if( !p_meta ) return;
2236 /* Get meta information from user */
2237 #define GET_META( field, s ) \
2238 var_Get( p_input, (s), &val ); \
2239 if( *val.psz_string ) { \
2240 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2241 p_meta->psz_ ## field = strdup( val.psz_string ); \
2243 free( val.psz_string )
2245 GET_META( title, "meta-title" );
2246 GET_META( author, "meta-author" );
2247 GET_META( artist, "meta-artist" );
2248 GET_META( genre, "meta-genre" );
2249 GET_META( copyright, "meta-copyright" );
2250 GET_META( description, "meta-description" );
2251 GET_META( date, "meta-date" );
2252 GET_META( url, "meta-url" );
2256 /*****************************************************************************
2257 * DecodeUrl: decode a given encoded url
2258 *****************************************************************************/
2259 static void DecodeUrl( char *psz )
2261 char *dup = strdup( psz );
2279 *psz++ = strtol( val, NULL, 16 );
2281 else if( *p == '+' )
2291 if( psz ) *psz++ ='\0';
2292 if( dup ) free( dup );
2295 /*****************************************************************************
2296 * MRLSplit: parse the access, demux and url part of the
2297 * Media Resource Locator.
2298 *****************************************************************************/
2299 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2300 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2302 char *psz_access = NULL;
2303 char *psz_demux = NULL;
2304 char *psz_path = NULL;
2305 char *psz, *psz_check;
2307 psz = strchr( psz_dup, ':' );
2309 /* '@' not allowed in access/demux part */
2310 psz_check = strchr( psz_dup, '@' );
2311 if( psz_check && psz_check < psz ) psz = 0;
2313 #if defined( WIN32 ) || defined( UNDER_CE )
2314 if( psz - psz_dup == 1 )
2316 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2325 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2329 psz = strchr( psz_dup, '/' );
2336 psz_access = psz_dup;
2343 if( !psz_access ) *ppsz_access = "";
2344 else *ppsz_access = psz_access;
2346 if( !psz_demux ) *ppsz_demux = "";
2347 else *ppsz_demux = psz_demux;
2349 if( !psz_path ) *ppsz_path = "";
2350 else *ppsz_path = psz_path;
2353 /*****************************************************************************
2354 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2357 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2358 *****************************************************************************/
2359 static void MRLSections( input_thread_t *p_input, char *psz_source,
2360 int *pi_title_start, int *pi_title_end,
2361 int *pi_chapter_start, int *pi_chapter_end )
2363 char *psz, *psz_end, *psz_next, *psz_check;
2365 *pi_title_start = *pi_title_end = -1;
2366 *pi_chapter_start = *pi_chapter_end = -1;
2368 /* Start by parsing titles and chapters */
2369 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2371 /* Check we are really dealing with a title/chapter section */
2372 psz_check = psz + 1;
2373 if( !*psz_check ) return;
2374 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2375 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2376 if( *psz_check == ':' && ++psz_check )
2377 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2378 if( *psz_check != '-' && *psz_check ) return;
2379 if( *psz_check == '-' && ++psz_check )
2380 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2381 if( *psz_check != ':' && *psz_check ) return;
2382 if( *psz_check == ':' && ++psz_check )
2383 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2384 if( *psz_check ) return;
2386 /* Separate start and end */
2388 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2390 /* Look for the start title */
2391 *pi_title_start = strtol( psz, &psz_next, 0 );
2392 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2393 *pi_title_end = *pi_title_start;
2396 /* Look for the start chapter */
2398 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2399 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2400 *pi_chapter_end = *pi_chapter_start;
2404 /* Look for the end title */
2405 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2406 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2409 /* Look for the end chapter */
2410 if( *psz_end ) psz_end++;
2411 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2412 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2415 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2416 psz_source, *pi_title_start, *pi_chapter_start,
2417 *pi_title_end, *pi_chapter_end );
2420 /*****************************************************************************
2421 * input_AddSubtitles: add a subtitles file and enable it
2422 *****************************************************************************/
2423 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2424 vlc_bool_t b_check_extension )
2426 input_source_t *sub;
2429 char *psz_path, *psz_extension;
2431 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2436 /* if we are provided a subtitle.sub file,
2437 * see if we don't have a subtitle.idx and use it instead */
2438 psz_path = strdup( psz_subtitle );
2441 psz_extension = strrchr( psz_path, '.');
2442 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2446 strcpy( psz_extension, ".idx" );
2447 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2448 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2451 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2452 psz_path, psz_subtitle );
2453 strcpy( psz_subtitle, psz_path );
2459 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2461 sub = InputSourceNew( p_input );
2462 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle", VLC_FALSE ) )
2464 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
2467 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2469 if( count.i_int == 0 )
2471 /* if it was first one, there is disable too */
2473 if( count.i_int < list.p_list->i_count )
2475 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2476 &list.p_list->p_values[count.i_int] );
2478 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );