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 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Run ( input_thread_t *p_input );
53 static int RunAndClean ( input_thread_t *p_input );
55 static input_thread_t * Create ( vlc_object_t *, input_item_t *, char *,
57 static int Init ( input_thread_t *p_input );
58 static void Error ( input_thread_t *p_input );
59 static void End ( input_thread_t *p_input );
60 static void MainLoop( input_thread_t *p_input );
62 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
63 static void ControlReduce( input_thread_t * );
64 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
66 static int UpdateFromAccess( input_thread_t * );
67 static int UpdateFromDemux( input_thread_t * );
68 static int UpdateMeta( input_thread_t * );
70 static void UpdateItemLength( input_thread_t *, int64_t i_length );
72 static void DecodeUrl( char * );
73 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
75 static input_source_t *InputSourceNew( input_thread_t *);
76 static int InputSourceInit( input_thread_t *, input_source_t *,
77 char *, char *psz_forced_demux );
78 static void InputSourceClean( input_thread_t *, input_source_t * );
80 static void SlaveDemux( input_thread_t *p_input );
81 static void SlaveSeek( input_thread_t *p_input );
83 static void InputMetaUser( input_thread_t *p_input );
85 /*****************************************************************************
86 * This function creates a new input, and returns a pointer
87 * to its description. On error, it returns NULL.
89 * Variables for _public_ use:
92 * - rate,rate-slower, rate-faster
93 * - position, position-offset
95 * - title,title-next,title-prev
96 * - chapter,chapter-next, chapter-prev
97 * - program, audio-es, video-es, spu-es
98 * - audio-delay, spu-delay
103 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
104 * * For intf callback upon changes
106 * TODO explain when Callback is called
107 * TODO complete this list (?)
108 *****************************************************************************/
109 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
110 char *psz_header, vlc_bool_t b_quick )
112 input_thread_t *p_input = NULL; /* thread descriptor */
116 /* Allocate descriptor */
117 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
118 if( p_input == NULL )
120 msg_Err( p_parent, "out of memory" );
124 /* One "randomly" selected input thread is responsible for computing
125 * the global stats. Check if there is already someone doing this */
126 if( p_input->p_libvlc->p_playlist->p_stats && !b_quick )
128 vlc_mutex_lock( &p_input->p_libvlc->p_playlist->p_stats->lock );
129 if( p_input->p_libvlc->p_playlist->p_stats_computer == NULL )
131 p_input->p_libvlc->p_playlist->p_stats_computer = p_input;
133 vlc_mutex_unlock( &p_input->p_libvlc->p_playlist->p_stats->lock );
136 p_input->b_preparsing = b_quick;
137 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
139 /* Init Common fields */
140 p_input->b_eof = VLC_FALSE;
141 p_input->b_can_pace_control = VLC_TRUE;
142 p_input->i_start = 0;
145 p_input->i_title = 0;
146 p_input->title = NULL;
147 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
148 p_input->i_state = INIT_S;
149 p_input->i_rate = INPUT_RATE_DEFAULT;
150 p_input->i_bookmark = 0;
151 p_input->bookmark = NULL;
152 p_input->p_meta = NULL;
153 p_input->p_es_out = NULL;
154 p_input->p_sout = NULL;
155 p_input->b_out_pace_control = VLC_FALSE;
156 p_input->i_pts_delay = 0;
158 /* Init Input fields */
159 p_input->input.p_item = p_item;
160 p_input->input.p_access = NULL;
161 p_input->input.p_stream = NULL;
162 p_input->input.p_demux = NULL;
163 p_input->input.b_title_demux = VLC_FALSE;
164 p_input->input.i_title = 0;
165 p_input->input.title = NULL;
166 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
167 p_input->input.b_can_pace_control = VLC_TRUE;
168 p_input->input.b_eof = VLC_FALSE;
169 p_input->input.i_cr_average = 0;
171 if( !p_input->input.p_item->p_meta )
172 p_input->input.p_item->p_meta = vlc_meta_New();
174 if( !p_item->p_stats )
176 p_item->p_stats = (input_stats_t*)malloc( sizeof( input_stats_t ) );
177 vlc_mutex_init( p_input, &p_item->p_stats->lock );
178 stats_ReinitInputStats( p_item->p_stats );
182 p_input->i_slave = 0;
183 p_input->slave = NULL;
185 /* Init control buffer */
186 vlc_mutex_init( p_input, &p_input->lock_control );
187 p_input->i_control = 0;
189 /* Parse input options */
190 vlc_mutex_lock( &p_item->lock );
191 for( i = 0; i < p_item->i_options; i++ )
193 var_OptionParse( p_input, p_item->ppsz_options[i] );
195 vlc_mutex_unlock( &p_item->lock );
197 /* Create Object Variables for private use only */
198 input_ConfigVarInit( p_input );
200 /* Create Objects variables for public Get and Set */
201 input_ControlVarInit( p_input );
202 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
204 if( !p_input->b_preparsing )
206 var_Get( p_input, "bookmarks", &val );
209 /* FIXME: have a common cfg parsing routine used by sout and others */
210 char *psz_parser, *psz_start, *psz_end;
211 psz_parser = val.psz_string;
212 while( (psz_start = strchr( psz_parser, '{' ) ) )
214 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
217 psz_end = strchr( psz_start, '}' );
218 if( !psz_end ) break;
219 psz_parser = psz_end + 1;
220 backup = *psz_parser;
223 while( (psz_end = strchr( psz_start, ',' ) ) )
226 if( !strncmp( psz_start, "name=", 5 ) )
228 p_seekpoint->psz_name = psz_start + 5;
230 else if( !strncmp( psz_start, "bytes=", 6 ) )
232 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
234 else if( !strncmp( psz_start, "time=", 5 ) )
236 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
239 psz_start = psz_end + 1;
241 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
242 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
243 p_seekpoint->i_time_offset );
244 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
245 vlc_seekpoint_Delete( p_seekpoint );
246 *psz_parser = backup;
248 free( val.psz_string );
252 /* Remove 'Now playing' info as it is probably outdated */
253 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT),
254 VLC_META_NOW_PLAYING ); /* ? Don't translate as it might has been copied ? */
260 * Initialize an input thread and run it. You will need to monitor the
261 * thread to clean up after it is done
263 * \param p_parent a vlc_object
264 * \param p_item an input item
265 * \return a pointer to the spawned input thread
267 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
268 input_item_t *p_item )
270 return __input_CreateThread2( p_parent, p_item, NULL );
275 input_thread_t *__input_CreateThread2( vlc_object_t *p_parent,
276 input_item_t *p_item,
279 input_thread_t *p_input = NULL; /* thread descriptor */
281 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
282 /* Now we can attach our new input */
283 vlc_object_attach( p_input, p_parent );
285 /* Create thread and wait for its readiness. */
286 if( vlc_thread_create( p_input, "input", Run,
287 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
289 msg_Err( p_input, "cannot create input thread" );
290 vlc_object_detach( p_input );
291 vlc_object_destroy( p_input );
300 * Initialize an input thread and run it. This thread will clean after himself,
301 * you can forget about it. It can work either in blocking or non-blocking mode
303 * \param p_parent a vlc_object
304 * \param p_item an input item
305 * \param b_block should we block until read is finished ?
306 * \return the input object id if non blocking, an error code else
308 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
311 input_thread_t *p_input = NULL; /* thread descriptor */
313 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
314 /* Now we can attach our new input */
315 vlc_object_attach( p_input, p_parent );
319 RunAndClean( p_input );
324 if( vlc_thread_create( p_input, "input", RunAndClean,
325 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
327 msg_Err( p_input, "cannot create input thread" );
328 vlc_object_detach( p_input );
329 vlc_object_destroy( p_input );
333 return p_input->i_object_id;
337 * Initialize an input and initialize it to preparse the item
338 * This function is blocking. It will only accept to parse files
340 * \param p_parent a vlc_object_t
341 * \param p_item an input item
342 * \return VLC_SUCCESS or an error
344 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
346 input_thread_t *p_input = NULL; /* thread descriptor */
348 /* Allocate descriptor */
349 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
350 p_input->i_flags |= OBJECT_FLAGS_QUIET;
351 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
353 /* Now we can attach our new input */
354 vlc_object_attach( p_input, p_parent );
358 /* Clean up master */
359 InputSourceClean( p_input, &p_input->input );
361 /* Unload all modules */
362 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
364 vlc_object_detach( p_input );
365 vlc_object_destroy( p_input );
371 * Request a running input thread to stop and die
373 * \param the input thread to stop
375 void input_StopThread( input_thread_t *p_input )
380 /* Set die for input */
381 p_input->b_die = VLC_TRUE;
383 /* We cannot touch p_input fields directly (we can from another thread),
384 * so use the vlc_object_find way, it's perfectly safe */
386 /* Set die for all access */
387 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
388 for( i = 0; i < p_list->i_count; i++ )
390 p_list->p_values[i].p_object->b_die = VLC_TRUE;
392 vlc_list_release( p_list );
394 /* Set die for all stream */
395 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
396 for( i = 0; i < p_list->i_count; i++ )
398 p_list->p_values[i].p_object->b_die = VLC_TRUE;
400 vlc_list_release( p_list );
402 /* Set die for all demux */
403 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
404 for( i = 0; i < p_list->i_count; i++ )
406 p_list->p_values[i].p_object->b_die = VLC_TRUE;
408 vlc_list_release( p_list );
410 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
414 * Clean up a dead input thread
415 * This function does not return until the thread is effectively cancelled.
417 * \param the input thread to kill
419 void input_DestroyThread( input_thread_t *p_input )
421 /* Join the thread */
422 vlc_thread_join( p_input );
424 /* Delete input lock (only after thread joined) */
425 vlc_mutex_destroy( &p_input->lock_control );
427 /* TODO: maybe input_DestroyThread should also delete p_input instead
428 * of the playlist but I'm not sure if it's possible */
431 /*****************************************************************************
432 * Run: main thread loop
433 * This is the "normal" thread that spawns the input processing chain,
434 * reads the stream, cleans up and waits
435 *****************************************************************************/
436 static int Run( input_thread_t *p_input )
438 /* Signal that the thread is launched */
439 vlc_thread_ready( p_input );
441 if( Init( p_input ) )
443 /* If we failed, wait before we are killed, and exit */
444 p_input->b_error = VLC_TRUE;
448 /* Tell we're dead */
449 p_input->b_dead = VLC_TRUE;
456 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
458 /* We have finish to demux data but not to play them */
459 while( !p_input->b_die )
461 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
464 msg_Dbg( p_input, "waiting decoder fifos to empty" );
466 msleep( INPUT_IDLE_SLEEP );
469 /* We have finished */
470 p_input->b_eof = VLC_TRUE;
473 /* Wait until we are asked to die */
474 if( !p_input->b_die )
485 /*****************************************************************************
486 * RunAndClean: main thread loop
487 * This is the "just forget me" thread that spawns the input processing chain,
488 * reads the stream, cleans up and releases memory
489 *****************************************************************************/
490 static int RunAndClean( input_thread_t *p_input )
492 /* Signal that the thread is launched */
493 vlc_thread_ready( p_input );
495 if( Init( p_input ) )
497 /* If we failed, just exit */
503 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
505 /* We have finished demuxing data but not playing it */
506 while( !p_input->b_die )
508 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
511 msg_Dbg( p_input, "waiting decoder fifos to empty" );
513 msleep( INPUT_IDLE_SLEEP );
516 /* We have finished */
517 p_input->b_eof = VLC_TRUE;
524 vlc_object_detach( p_input );
525 vlc_object_destroy( p_input );
531 /*****************************************************************************
532 * Main loop: Fill buffers from access, and demux
533 *****************************************************************************/
534 static void MainLoop( input_thread_t *p_input )
536 int64_t i_intf_update = 0;
538 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
540 vlc_bool_t b_force_update = VLC_FALSE;
546 if( p_input->i_state != PAUSE_S )
548 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
549 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
556 if( p_input->input.b_title_demux &&
557 p_input->input.p_demux->info.i_update )
559 i_ret = UpdateFromDemux( p_input );
560 b_force_update = VLC_TRUE;
562 else if( !p_input->input.b_title_demux &&
563 p_input->input.p_access &&
564 p_input->input.p_access->info.i_update )
566 i_ret = UpdateFromAccess( p_input );
567 b_force_update = VLC_TRUE;
571 if( i_ret == 0 ) /* EOF */
575 var_Get( p_input, "input-repeat", &repeat );
576 if( repeat.i_int == 0 )
578 /* End of file - we do not set b_die because only the
579 * playlist is allowed to do so. */
580 msg_Dbg( p_input, "EOF reached" );
581 p_input->input.b_eof = VLC_TRUE;
585 msg_Dbg( p_input, "repeating the same input (%d)",
587 if( repeat.i_int > 0 )
590 var_Set( p_input, "input-repeat", repeat );
593 /* Seek to start title/seekpoint */
594 val.i_int = p_input->input.i_title_start -
595 p_input->input.i_title_offset;
596 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
598 input_ControlPush( p_input,
599 INPUT_CONTROL_SET_TITLE, &val );
601 val.i_int = p_input->input.i_seekpoint_start -
602 p_input->input.i_seekpoint_offset;
603 if( val.i_int > 0 /* TODO: check upper boundary */ )
604 input_ControlPush( p_input,
605 INPUT_CONTROL_SET_SEEKPOINT, &val );
607 /* Seek to start position */
608 if( p_input->i_start > 0 )
610 val.i_time = p_input->i_start;
611 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
617 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
624 p_input->b_error = VLC_TRUE;
627 if( i_ret > 0 && p_input->i_slave > 0 )
629 SlaveDemux( p_input );
639 vlc_mutex_lock( &p_input->lock_control );
640 ControlReduce( p_input );
641 while( !ControlPopNoLock( p_input, &i_type, &val ) )
643 msg_Dbg( p_input, "control type=%d", i_type );
644 if( Control( p_input, i_type, val ) )
645 b_force_update = VLC_TRUE;
647 vlc_mutex_unlock( &p_input->lock_control );
649 if( b_force_update || i_intf_update < mdate() )
653 int64_t i_time, i_length;
654 /* update input status variables */
655 if( !demux2_Control( p_input->input.p_demux,
656 DEMUX_GET_POSITION, &f_pos ) )
658 val.f_float = (float)f_pos;
659 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
661 if( !demux2_Control( p_input->input.p_demux,
662 DEMUX_GET_TIME, &i_time ) )
664 p_input->i_time = i_time;
666 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
668 if( !demux2_Control( p_input->input.p_demux,
669 DEMUX_GET_LENGTH, &i_length ) )
672 var_Get( p_input, "length", &old_val );
673 val.i_time = i_length;
674 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
676 if( old_val.i_time != val.i_time )
678 UpdateItemLength( p_input, i_length );
682 var_SetBool( p_input, "intf-change", VLC_TRUE );
683 i_intf_update = mdate() + I64C(150000);
685 /* 150ms * 8 = ~ 1 second */
686 if( ++i_updates % 8 == 0 )
688 stats_ComputeInputStats( p_input, p_input->input.p_item->p_stats );
689 /* Are we the thread responsible for computing global stats ? */
690 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
692 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
693 p_input->p_libvlc->p_playlist->p_stats );
700 static int Init( input_thread_t * p_input )
710 /* Initialize optional stream output. (before access/demuxer)
711 * XXX: we add a special case if the uri starts by vlc.
712 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
713 * be destroyed in case of a file).
714 * (this will break playing of file starting by 'vlc:' but I don't
715 * want to add more logic, just force file by file:// or code it ;)
717 memset( &p_input->counters, 0, sizeof( p_input->counters ) );
718 if( !p_input->b_preparsing )
720 /* Prepare statistics */
721 #define INIT_COUNTER( p, type, compute ) p_input->counters.p_##p = \
722 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
723 if( p_input->p_libvlc->b_stats )
725 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
726 INIT_COUNTER( read_packets, INTEGER, COUNTER );
727 INIT_COUNTER( demux_read, INTEGER, COUNTER );
728 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
729 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
730 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
731 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
732 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
733 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
734 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
735 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
736 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
737 p_input->counters.p_sout_send_bitrate = NULL;
738 p_input->counters.p_sout_sent_packets = NULL;
739 p_input->counters.p_sout_sent_bytes = NULL;
740 if( p_input->counters.p_demux_bitrate )
741 p_input->counters.p_demux_bitrate->update_interval = 1000000;
742 if( p_input->counters.p_input_bitrate )
743 p_input->counters.p_input_bitrate->update_interval = 1000000;
745 vlc_mutex_init( p_input, &p_input->counters.counters_lock );
748 psz = var_GetString( p_input, "sout" );
749 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
751 p_input->p_sout = sout_NewInstance( p_input, psz );
752 if( p_input->p_sout == NULL )
754 msg_Err( p_input, "cannot start stream output instance, " \
759 if( p_input->p_libvlc->b_stats )
761 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
762 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
763 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
764 if( p_input->counters.p_sout_send_bitrate )
765 p_input->counters.p_sout_send_bitrate->update_interval =
773 p_input->p_es_out = input_EsOutNew( p_input );
774 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
775 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
777 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
778 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
780 if( InputSourceInit( p_input, &p_input->input,
781 p_input->input.p_item->psz_uri, NULL ) )
786 /* Create global title (from master) */
787 if( !p_input->b_preparsing )
789 p_input->i_title = p_input->input.i_title;
790 p_input->title = p_input->input.title;
791 p_input->i_title_offset = p_input->input.i_title_offset;
792 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
793 if( p_input->i_title > 0 )
795 /* Setup variables */
796 input_ControlVarNavigation( p_input );
797 input_ControlVarTitle( p_input, 0 );
801 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
802 p_input->b_can_pause = p_input->input.b_can_pause;
805 if( p_input->i_pts_delay < 0 )
806 p_input->i_pts_delay = 0;
808 /* If the desynchronisation requested by the user is < 0, we need to
809 * cache more data. */
810 var_Get( p_input, "audio-desync", &val );
811 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
813 /* Update cr_average depending on the caching */
814 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
815 p_input->input.i_cr_average /= 10;
816 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
819 /* Load master infos */
821 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
822 &val.i_time ) && val.i_time > 0 )
824 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
825 UpdateItemLength( p_input, val.i_time );
826 p_input->input.p_item->i_duration = val.i_time;
829 /* Start title/chapter */
830 if( !p_input->b_preparsing )
832 val.i_int = p_input->input.i_title_start -
833 p_input->input.i_title_offset;
834 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
835 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
836 val.i_int = p_input->input.i_seekpoint_start -
837 p_input->input.i_seekpoint_offset;
838 if( val.i_int > 0 /* TODO: check upper boundary */ )
839 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
843 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
845 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
848 if( p_input->i_start > 0 )
850 if( p_input->i_start >= val.i_time )
852 msg_Warn( p_input, "invalid start-time ignored" );
858 msg_Dbg( p_input, "starting at time: %ds",
859 (int)( p_input->i_start / I64C(1000000) ) );
861 s.i_time = p_input->i_start;
862 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
865 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
867 msg_Warn( p_input, "invalid stop-time ignored" );
873 /* Get fps and set it if not already set */
874 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
877 float f_requested_fps;
879 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
880 var_SetFloat( p_input, "sub-original-fps", f_fps );
882 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
883 if( f_requested_fps != f_fps )
885 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
887 var_SetFloat( p_input, "sub-fps", f_requested_fps );
891 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
894 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
898 /* Look for and add subtitle files */
899 psz_subtitle = var_GetString( p_input, "sub-file" );
902 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
903 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
906 var_Get( p_input, "sub-autodetect-file", &val );
909 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
910 char **subs = subtitles_Detect( p_input, psz_autopath,
911 p_input->input.p_item->psz_uri );
916 /* Try to autoselect the first autodetected subtitles file
917 * if no subtitles file was specified */
918 if( *psz_subtitle == 0 && subs && subs[0] )
920 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
925 /* Then, just add the following subtitles files */
926 for( ; subs && subs[i]; i++ )
928 if( strcmp( psz_subtitle, subs[i] ) )
930 sub = InputSourceNew( p_input );
931 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
933 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
939 if( subs ) free( subs );
940 if( psz_autopath ) free( psz_autopath );
942 free( psz_subtitle );
945 psz = var_GetString( p_input, "input-slave" );
949 input_source_t *slave;
952 while( *psz == ' ' || *psz == '#' )
956 if( ( psz_delim = strchr( psz, '#' ) ) )
965 msg_Dbg( p_input, "adding slave input '%s'", psz );
966 slave = InputSourceNew( p_input );
967 if( !InputSourceInit( p_input, slave, psz, NULL ) )
969 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
975 if( psz ) free( psz );
979 p_input->i_start = 0;
980 p_input->i_start = 0;
984 if( !p_input->b_preparsing )
986 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
987 i_es_out_mode = ES_OUT_MODE_AUTO;
989 if( p_input->p_sout )
991 var_Get( p_input, "sout-all", &val );
994 i_es_out_mode = ES_OUT_MODE_ALL;
999 var_Get( p_input, "programs", &val );
1000 if ( val.p_list && val.p_list->i_count )
1002 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1003 /* Note : we should remove the "program" callback. */
1006 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1010 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1012 /* Inform the demuxer about waited group (needed only for DVB) */
1013 if( i_es_out_mode == ES_OUT_MODE_ALL )
1015 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1017 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1019 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
1024 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
1025 (int) var_GetInteger( p_input, "program" ), NULL );
1028 if( p_input->p_sout )
1030 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
1032 p_input->b_out_pace_control = VLC_FALSE;
1036 p_input->b_out_pace_control = VLC_TRUE;
1039 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
1041 /* We don't want a high input priority here or we'll
1042 * end-up sucking up all the CPU time */
1043 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1046 msg_Dbg( p_input, "starting in %s mode",
1047 p_input->b_out_pace_control ? "async" : "sync" );
1051 p_meta = p_input->input.p_item->p_meta;
1052 /* Get meta data from users */
1053 InputMetaUser( p_input );
1054 /* Get meta data from master input */
1055 demux2_Control( p_input->input.p_demux, DEMUX_GET_META, p_meta );
1057 /* Access_file does not give any meta, and there are no slave */
1058 if( !p_input->b_preparsing )
1060 if( p_input->input.p_access )
1061 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1064 /* Get meta data from slave input */
1065 for( i = 0; i < p_input->i_slave; i++ )
1067 demux2_Control( p_input->slave[i]->p_demux,
1068 DEMUX_GET_META, p_meta );
1069 if( p_input->slave[i]->p_access )
1071 access2_Control( p_input->slave[i]->p_access,
1072 ACCESS_GET_META, p_meta );
1077 UpdateMeta( p_input );
1079 if( !p_input->b_preparsing )
1081 msg_Dbg( p_input, "`%s' successfully opened",
1082 p_input->input.p_item->psz_uri );
1086 /* initialization is complete */
1087 input_ChangeState(p_input, PLAYING_S);
1092 if( p_input->p_es_out )
1093 input_EsOutDelete( p_input->p_es_out );
1095 if( p_input->p_sout )
1096 sout_DeleteInstance( p_input->p_sout );
1098 /* Mark them deleted */
1099 p_input->input.p_demux = NULL;
1100 p_input->input.p_stream = NULL;
1101 p_input->input.p_access = NULL;
1102 p_input->p_es_out = NULL;
1103 p_input->p_sout = NULL;
1105 return VLC_EGENERIC;
1108 /*****************************************************************************
1109 * Error: RunThread() error loop
1110 *****************************************************************************
1111 * This function is called when an error occurred during thread main's loop.
1112 *****************************************************************************/
1113 static void Error( input_thread_t *p_input )
1115 while( !p_input->b_die )
1118 msleep( INPUT_IDLE_SLEEP );
1122 /*****************************************************************************
1123 * End: end the input thread
1124 *****************************************************************************/
1125 static void End( input_thread_t * p_input )
1129 msg_Dbg( p_input, "closing input" );
1131 /* We are at the end */
1132 input_ChangeState(p_input, END_S);
1134 /* Clean control variables */
1135 input_ControlVarClean( p_input );
1137 /* Clean up master */
1138 InputSourceClean( p_input, &p_input->input );
1141 for( i = 0; i < p_input->i_slave; i++ )
1143 InputSourceClean( p_input, p_input->slave[i] );
1144 free( p_input->slave[i] );
1146 if( p_input->slave ) free( p_input->slave );
1148 /* Unload all modules */
1149 if( p_input->p_es_out )
1150 input_EsOutDelete( p_input->p_es_out );
1152 #define CL_CO( c ) stats_CounterClean( p_input->counters.p_##c ); p_input->counters.p_##c = NULL;
1153 if( p_input->p_libvlc->b_stats )
1155 /* make sure we are up to date */
1156 stats_ComputeInputStats( p_input, p_input->input.p_item->p_stats );
1157 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1159 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1160 p_input->p_libvlc->p_playlist->p_stats );
1161 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1163 vlc_mutex_lock( &p_input->counters.counters_lock );
1164 CL_CO( read_bytes );
1165 CL_CO( read_packets );
1166 CL_CO( demux_read );
1167 CL_CO( input_bitrate );
1168 CL_CO( demux_bitrate );
1169 CL_CO( played_abuffers );
1170 CL_CO( lost_abuffers );
1171 CL_CO( displayed_pictures );
1172 CL_CO( lost_pictures );
1173 CL_CO( decoded_audio) ;
1174 CL_CO( decoded_video );
1175 CL_CO( decoded_sub) ;
1176 vlc_mutex_unlock( &p_input->counters.counters_lock );
1179 /* Close optional stream output instance */
1180 if( p_input->p_sout )
1184 vlc_mutex_lock( &p_input->counters.counters_lock );
1185 CL_CO( sout_sent_packets );
1186 CL_CO( sout_sent_bytes );
1187 CL_CO( sout_send_bitrate );
1188 vlc_mutex_unlock( &p_input->counters.counters_lock );
1190 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool )
1192 /* attach sout to the playlist */
1193 msg_Dbg( p_input, "keeping sout" );
1194 vlc_object_detach( p_input->p_sout );
1195 vlc_object_attach( p_input->p_sout, p_input->p_libvlc->p_playlist );
1199 msg_Dbg( p_input, "destroying sout" );
1200 sout_DeleteInstance( p_input->p_sout );
1205 /* Tell we're dead */
1206 p_input->b_dead = VLC_TRUE;
1209 /*****************************************************************************
1211 *****************************************************************************/
1212 static inline int ControlPopNoLock( input_thread_t *p_input,
1213 int *pi_type, vlc_value_t *p_val )
1215 if( p_input->i_control <= 0 )
1217 return VLC_EGENERIC;
1220 *pi_type = p_input->control[0].i_type;
1221 *p_val = p_input->control[0].val;
1223 p_input->i_control--;
1224 if( p_input->i_control > 0 )
1228 for( i = 0; i < p_input->i_control; i++ )
1230 p_input->control[i].i_type = p_input->control[i+1].i_type;
1231 p_input->control[i].val = p_input->control[i+1].val;
1238 static void ControlReduce( input_thread_t *p_input )
1241 for( i = 1; i < p_input->i_control; i++ )
1243 const int i_lt = p_input->control[i-1].i_type;
1244 const int i_ct = p_input->control[i].i_type;
1246 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1247 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1251 ( i_ct == INPUT_CONTROL_SET_STATE ||
1252 i_ct == INPUT_CONTROL_SET_RATE ||
1253 i_ct == INPUT_CONTROL_SET_POSITION ||
1254 i_ct == INPUT_CONTROL_SET_TIME ||
1255 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1256 i_ct == INPUT_CONTROL_SET_TITLE ||
1257 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1258 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1261 // msg_Dbg( p_input, "merged at %d", i );
1262 /* Remove the i-1 */
1263 for( j = i; j < p_input->i_control; j++ )
1264 p_input->control[j-1] = p_input->control[j];
1265 p_input->i_control--;
1269 /* TODO but that's not that important
1270 - merge SET_X with SET_X_CMD
1271 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1272 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1279 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1282 vlc_bool_t b_force_update = VLC_FALSE;
1284 if( !p_input ) return b_force_update;
1288 case INPUT_CONTROL_SET_DIE:
1289 msg_Dbg( p_input, "control: stopping input" );
1290 /* Mark all submodules to die */
1291 if( p_input->input.p_access )
1292 p_input->input.p_access->b_die = VLC_TRUE;
1293 if( p_input->input.p_stream )
1294 p_input->input.p_stream->b_die = VLC_TRUE;
1295 p_input->input.p_demux->b_die = VLC_TRUE;
1297 p_input->b_die = VLC_TRUE;
1300 case INPUT_CONTROL_SET_POSITION:
1301 case INPUT_CONTROL_SET_POSITION_OFFSET:
1304 if( i_type == INPUT_CONTROL_SET_POSITION )
1306 f_pos = val.f_float;
1310 /* Should not fail */
1311 demux2_Control( p_input->input.p_demux,
1312 DEMUX_GET_POSITION, &f_pos );
1313 f_pos += val.f_float;
1315 if( f_pos < 0.0 ) f_pos = 0.0;
1316 if( f_pos > 1.0 ) f_pos = 1.0;
1317 /* Reset the decoders states and clock sync (before calling the demuxer */
1318 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1319 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1320 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1323 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1324 "%2.1f%% failed", f_pos * 100 );
1328 if( p_input->i_slave > 0 )
1329 SlaveSeek( p_input );
1331 b_force_update = VLC_TRUE;
1336 case INPUT_CONTROL_SET_TIME:
1337 case INPUT_CONTROL_SET_TIME_OFFSET:
1342 if( i_type == INPUT_CONTROL_SET_TIME )
1344 i_time = val.i_time;
1348 /* Should not fail */
1349 demux2_Control( p_input->input.p_demux,
1350 DEMUX_GET_TIME, &i_time );
1351 i_time += val.i_time;
1353 if( i_time < 0 ) i_time = 0;
1355 /* Reset the decoders states and clock sync (before calling the demuxer */
1356 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1357 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1359 i_ret = demux2_Control( p_input->input.p_demux,
1360 DEMUX_SET_TIME, i_time );
1365 /* Emulate it with a SET_POS */
1366 demux2_Control( p_input->input.p_demux,
1367 DEMUX_GET_LENGTH, &i_length );
1370 double f_pos = (double)i_time / (double)i_length;
1371 i_ret = demux2_Control( p_input->input.p_demux,
1372 DEMUX_SET_POSITION, f_pos );
1377 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1378 " failed or not possible", i_time );
1382 if( p_input->i_slave > 0 )
1383 SlaveSeek( p_input );
1385 b_force_update = VLC_TRUE;
1390 case INPUT_CONTROL_SET_STATE:
1391 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1392 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1395 if( p_input->input.p_access )
1396 i_ret = access2_Control( p_input->input.p_access,
1397 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1399 i_ret = demux2_Control( p_input->input.p_demux,
1400 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1404 /* FIXME What to do ? */
1405 msg_Warn( p_input, "cannot unset pause -> EOF" );
1406 vlc_mutex_unlock( &p_input->lock_control );
1407 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1408 vlc_mutex_lock( &p_input->lock_control );
1411 b_force_update = VLC_TRUE;
1413 /* Switch to play */
1414 p_input->i_state = PLAYING_S;
1415 val.i_int = PLAYING_S;
1416 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1419 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1420 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1422 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1423 p_input->b_can_pause )
1426 if( p_input->input.p_access )
1427 i_ret = access2_Control( p_input->input.p_access,
1428 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1430 i_ret = demux2_Control( p_input->input.p_demux,
1431 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1433 b_force_update = VLC_TRUE;
1437 msg_Warn( p_input, "cannot set pause state" );
1438 val.i_int = p_input->i_state;
1442 val.i_int = PAUSE_S;
1445 /* Switch to new state */
1446 p_input->i_state = val.i_int;
1447 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1449 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1451 b_force_update = VLC_TRUE;
1453 /* Correct "state" value */
1454 val.i_int = p_input->i_state;
1455 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1457 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1459 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1463 case INPUT_CONTROL_SET_RATE:
1464 case INPUT_CONTROL_SET_RATE_SLOWER:
1465 case INPUT_CONTROL_SET_RATE_FASTER:
1469 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1470 i_rate = p_input->i_rate * 2;
1471 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1472 i_rate = p_input->i_rate / 2;
1476 if( i_rate < INPUT_RATE_MIN )
1478 msg_Dbg( p_input, "cannot set rate faster" );
1479 i_rate = INPUT_RATE_MIN;
1481 else if( i_rate > INPUT_RATE_MAX )
1483 msg_Dbg( p_input, "cannot set rate slower" );
1484 i_rate = INPUT_RATE_MAX;
1486 if( i_rate != INPUT_RATE_DEFAULT &&
1487 ( !p_input->b_can_pace_control ||
1488 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1490 msg_Dbg( p_input, "cannot change rate" );
1491 i_rate = INPUT_RATE_DEFAULT;
1493 if( i_rate != p_input->i_rate )
1495 p_input->i_rate = i_rate;
1497 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1499 /* We haven't send data to decoder when rate != default */
1500 if( i_rate == INPUT_RATE_DEFAULT )
1501 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1504 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1506 b_force_update = VLC_TRUE;
1511 case INPUT_CONTROL_SET_PROGRAM:
1512 /* No need to force update, es_out does it if needed */
1513 es_out_Control( p_input->p_es_out,
1514 ES_OUT_SET_GROUP, val.i_int );
1516 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1520 case INPUT_CONTROL_SET_ES:
1521 /* No need to force update, es_out does it if needed */
1522 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1523 input_EsOutGetFromID( p_input->p_es_out,
1527 case INPUT_CONTROL_SET_AUDIO_DELAY:
1528 input_EsOutSetDelay( p_input->p_es_out,
1529 AUDIO_ES, val.i_time );
1530 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1533 case INPUT_CONTROL_SET_SPU_DELAY:
1534 input_EsOutSetDelay( p_input->p_es_out,
1535 SPU_ES, val.i_time );
1536 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1539 case INPUT_CONTROL_SET_TITLE:
1540 case INPUT_CONTROL_SET_TITLE_NEXT:
1541 case INPUT_CONTROL_SET_TITLE_PREV:
1542 if( p_input->input.b_title_demux &&
1543 p_input->input.i_title > 0 )
1546 /* FIXME handle demux title */
1547 demux_t *p_demux = p_input->input.p_demux;
1550 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1551 i_title = p_demux->info.i_title - 1;
1552 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1553 i_title = p_demux->info.i_title + 1;
1555 i_title = val.i_int;
1557 if( i_title >= 0 && i_title < p_input->input.i_title )
1559 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1560 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1562 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1563 input_ControlVarTitle( p_input, i_title );
1566 else if( p_input->input.i_title > 0 )
1568 access_t *p_access = p_input->input.p_access;
1571 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1572 i_title = p_access->info.i_title - 1;
1573 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1574 i_title = p_access->info.i_title + 1;
1576 i_title = val.i_int;
1578 if( i_title >= 0 && i_title < p_input->input.i_title )
1580 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1581 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1583 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1584 stream_AccessReset( p_input->input.p_stream );
1588 case INPUT_CONTROL_SET_SEEKPOINT:
1589 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1590 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1591 if( p_input->input.b_title_demux &&
1592 p_input->input.i_title > 0 )
1594 demux_t *p_demux = p_input->input.p_demux;
1596 int64_t i_input_time;
1597 int64_t i_seekpoint_time;
1599 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1601 i_seekpoint = p_demux->info.i_seekpoint;
1602 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1603 if( i_seekpoint_time >= 0 &&
1604 !demux2_Control( p_demux,
1605 DEMUX_GET_TIME, &i_input_time ) )
1607 if ( i_input_time < i_seekpoint_time + 3000000 )
1613 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1614 i_seekpoint = p_demux->info.i_seekpoint + 1;
1616 i_seekpoint = val.i_int;
1618 if( i_seekpoint >= 0 && i_seekpoint <
1619 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1621 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1622 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1624 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1627 else if( p_input->input.i_title > 0 )
1629 demux_t *p_demux = p_input->input.p_demux;
1630 access_t *p_access = p_input->input.p_access;
1632 int64_t i_input_time;
1633 int64_t i_seekpoint_time;
1635 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1637 i_seekpoint = p_access->info.i_seekpoint;
1638 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1639 if( i_seekpoint_time >= 0 &&
1640 demux2_Control( p_demux,
1641 DEMUX_GET_TIME, &i_input_time ) )
1643 if ( i_input_time < i_seekpoint_time + 3000000 )
1649 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1650 i_seekpoint = p_access->info.i_seekpoint + 1;
1652 i_seekpoint = val.i_int;
1654 if( i_seekpoint >= 0 && i_seekpoint <
1655 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1657 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1658 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1660 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1662 stream_AccessReset( p_input->input.p_stream );
1667 case INPUT_CONTROL_ADD_SLAVE:
1668 if( val.psz_string )
1670 input_source_t *slave = InputSourceNew( p_input );
1672 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1674 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1678 msg_Dbg( p_input, "adding %s as slave on the fly",
1682 if( demux2_Control( p_input->input.p_demux,
1683 DEMUX_GET_TIME, &i_time ) )
1685 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1686 InputSourceClean( p_input, slave );
1690 if( demux2_Control( slave->p_demux,
1691 DEMUX_SET_TIME, i_time ) )
1693 msg_Err( p_input, "seek failed for new slave" );
1694 InputSourceClean( p_input, slave );
1699 /* Get meta (access and demux) */
1700 access2_Control( slave->p_access, ACCESS_GET_META,
1702 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1703 UpdateMeta( p_input );
1705 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1710 msg_Warn( p_input, "failed to add %s as slave",
1714 free( val.psz_string );
1718 case INPUT_CONTROL_SET_BOOKMARK:
1720 msg_Err( p_input, "not yet implemented" );
1724 return b_force_update;
1727 /*****************************************************************************
1729 *****************************************************************************/
1730 static int UpdateFromDemux( input_thread_t *p_input )
1732 demux_t *p_demux = p_input->input.p_demux;
1735 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1737 v.i_int = p_demux->info.i_title;
1738 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1740 input_ControlVarTitle( p_input, p_demux->info.i_title );
1742 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1744 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1746 v.i_int = p_demux->info.i_seekpoint;
1747 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1749 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1751 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1753 /* Hmmm only works with master input */
1754 if( p_input->input.p_demux == p_demux )
1756 int i_title_end = p_input->input.i_title_end -
1757 p_input->input.i_title_offset;
1758 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1759 p_input->input.i_seekpoint_offset;
1761 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1763 if( p_demux->info.i_title > i_title_end ||
1764 ( p_demux->info.i_title == i_title_end &&
1765 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1767 else if( i_seekpoint_end >=0 )
1769 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1771 else if( i_title_end >= 0 )
1773 if( p_demux->info.i_title > i_title_end ) return 0;
1780 /*****************************************************************************
1782 *****************************************************************************/
1783 static int UpdateFromAccess( input_thread_t *p_input )
1785 access_t *p_access = p_input->input.p_access;
1788 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1790 v.i_int = p_access->info.i_title;
1791 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1793 input_ControlVarTitle( p_input, p_access->info.i_title );
1795 stream_AccessUpdate( p_input->input.p_stream );
1797 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1799 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1801 v.i_int = p_access->info.i_seekpoint;
1802 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1803 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1805 if( p_access->info.i_update & INPUT_UPDATE_META )
1807 /* TODO maybe multi - access ? */
1808 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1809 access2_Control( p_input->input.p_access,ACCESS_GET_META, p_meta );
1810 UpdateMeta( p_input );
1811 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1812 p_access->info.i_update &= ~INPUT_UPDATE_META;
1815 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1817 /* Hmmm only works with master input */
1818 if( p_input->input.p_access == p_access )
1820 int i_title_end = p_input->input.i_title_end -
1821 p_input->input.i_title_offset;
1822 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1823 p_input->input.i_seekpoint_offset;
1825 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1827 if( p_access->info.i_title > i_title_end ||
1828 ( p_access->info.i_title == i_title_end &&
1829 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1831 else if( i_seekpoint_end >=0 )
1833 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1835 else if( i_title_end >= 0 )
1837 if( p_access->info.i_title > i_title_end ) return 0;
1844 /*****************************************************************************
1846 *****************************************************************************/
1847 static int UpdateMeta( input_thread_t *p_input )
1849 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1853 if( p_meta->psz_title && !p_input->input.p_item->b_fixed_name )
1854 input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1856 /** \todo handle sout meta */
1861 /*****************************************************************************
1863 *****************************************************************************/
1864 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
1866 vlc_mutex_lock( &p_input->input.p_item->lock );
1867 p_input->input.p_item->i_duration = i_length;
1868 vlc_mutex_unlock( &p_input->input.p_item->lock );
1870 if( !p_input->b_preparsing )
1872 pl_Yield( p_input );
1873 var_SetInteger( pl_Get( p_input ), "item-change",
1874 p_input->input.p_item->i_id );
1875 pl_Release( p_input )
1879 /*****************************************************************************
1881 *****************************************************************************/
1882 static input_source_t *InputSourceNew( input_thread_t *p_input )
1884 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1888 msg_Err( p_input, "out of memory for new input source" );
1893 in->p_access = NULL;
1894 in->p_stream = NULL;
1896 in->b_title_demux = VLC_FALSE;
1899 in->b_can_pace_control = VLC_TRUE;
1900 in->b_eof = VLC_FALSE;
1901 in->i_cr_average = 0;
1906 /*****************************************************************************
1908 *****************************************************************************/
1909 static int InputSourceInit( input_thread_t *p_input,
1910 input_source_t *in, char *psz_mrl,
1911 char *psz_forced_demux )
1913 char *psz_dup = strdup( psz_mrl );
1921 if( !in ) return VLC_EGENERIC;
1924 if( !p_input->b_preparsing )
1926 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1927 &psz_access, &psz_demux, &psz_path );
1929 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1930 psz_mrl, psz_access, psz_demux, psz_path );
1932 /* Hack to allow udp://@:port syntax */
1934 (strncmp( psz_access, "udp", 3 ) &&
1935 strncmp( psz_access, "rtp", 3 )) )
1937 /* Find optional titles and seekpoints */
1938 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1939 &in->i_seekpoint_start, &in->i_seekpoint_end );
1941 if( psz_forced_demux && *psz_forced_demux )
1943 psz_demux = psz_forced_demux;
1945 else if( !psz_demux || *psz_demux == '\0' )
1947 /* special hack for forcing a demuxer with --demux=module
1948 * (and do nothing with a list) */
1949 char *psz_var_demux = var_GetString( p_input, "demux" );
1951 if( *psz_var_demux != '\0' &&
1952 !strchr(psz_var_demux, ',' ) &&
1953 !strchr(psz_var_demux, ':' ) )
1955 psz_demux = psz_var_demux;
1957 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
1959 else if( psz_var_demux )
1961 free( psz_var_demux );
1965 /* Try access_demux if no demux given */
1966 if( *psz_demux == '\0' )
1968 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1969 NULL, p_input->p_es_out, VLC_FALSE );
1975 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
1977 psz_access = "file";
1982 int64_t i_pts_delay;
1984 /* Get infos from access_demux */
1985 demux2_Control( in->p_demux,
1986 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1987 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1989 in->b_title_demux = VLC_TRUE;
1990 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1991 &in->title, &in->i_title,
1992 &in->i_title_offset, &in->i_seekpoint_offset ) )
1997 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1998 &in->b_can_pace_control );
1999 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2003 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2009 int64_t i_pts_delay;
2011 input_ChangeState( p_input, OPENING_S);
2013 /* Now try a real access */
2014 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2015 p_input->b_preparsing );
2017 /* Access failed, URL encoded ? */
2018 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2020 DecodeUrl( psz_path );
2022 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2023 psz_access, psz_demux, psz_path );
2025 in->p_access = access2_New( p_input,
2026 psz_access, psz_demux, psz_path,
2027 p_input->b_preparsing );
2029 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2030 * are forbidden in filenames on Win32. */
2032 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2033 if( in->p_access == NULL &&
2034 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2036 if( psz_dup ) free( psz_dup );
2037 psz_dup = strdup( psz_mrl );
2039 if( psz_forced_demux && *psz_forced_demux )
2041 psz_demux = psz_forced_demux;
2043 else psz_demux = "";
2046 in->p_access = access2_New( p_input,
2047 psz_access, psz_demux, psz_path,
2048 p_input->b_preparsing );
2052 if( in->p_access == NULL )
2054 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2055 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2056 _("Your input can't be opened"),
2057 _("VLC is unable to open the MRL '%s'."
2058 " Check the log for details."), psz_mrl );
2063 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2064 while( psz && *psz )
2066 access_t *p_access = in->p_access;
2067 char *end = strchr( psz, ':' );
2072 in->p_access = access2_FilterNew( in->p_access, psz );
2073 if( in->p_access == NULL )
2075 in->p_access = p_access;
2076 msg_Warn( p_input, "failed to insert access filter %s",
2082 if( psz_tmp ) free( psz_tmp );
2084 /* Get infos from access */
2085 if( !p_input->b_preparsing )
2087 access2_Control( in->p_access,
2088 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2089 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2091 in->b_title_demux = VLC_FALSE;
2092 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2093 &in->title, &in->i_title,
2094 &in->i_title_offset, &in->i_seekpoint_offset ) )
2100 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2101 &in->b_can_pace_control );
2102 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2104 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2106 var_Set( p_input, "seekable", val );
2109 input_ChangeState( p_input, BUFFERING_S);
2111 /* Create the stream_t */
2112 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2113 if( in->p_stream == NULL )
2115 msg_Warn( p_input, "cannot create a stream_t from access" );
2119 /* Open a demuxer */
2120 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2122 psz_demux = in->p_access->psz_demux;
2124 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2125 in->p_stream, p_input->p_es_out,
2126 p_input->b_preparsing );
2127 if( in->p_demux == NULL )
2129 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2130 psz_access, psz_demux, psz_path );
2131 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2132 _("Can't recognize the input's format"),
2133 _("The format of '%s' can't be detected. "
2134 "Have a look the log for details."), psz_mrl );
2138 /* TODO get title from demux */
2139 if( !p_input->b_preparsing && in->i_title <= 0 )
2141 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2142 &in->title, &in->i_title,
2143 &in->i_title_offset, &in->i_seekpoint_offset ))
2150 in->b_title_demux = VLC_TRUE;
2155 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2156 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2158 if( psz_dup ) free( psz_dup );
2163 demux2_Delete( in->p_demux );
2166 stream_Delete( in->p_stream );
2169 access2_Delete( in->p_access );
2170 if( psz_dup ) free( psz_dup );
2172 return VLC_EGENERIC;
2175 /*****************************************************************************
2177 *****************************************************************************/
2178 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2181 demux2_Delete( in->p_demux );
2184 stream_Delete( in->p_stream );
2187 access2_Delete( in->p_access );
2189 if( in->i_title > 0 )
2192 for( i = 0; i < in->i_title; i++ )
2194 vlc_input_title_Delete( in->title[i] );
2200 static void SlaveDemux( input_thread_t *p_input )
2205 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2207 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2211 for( i = 0; i < p_input->i_slave; i++ )
2213 input_source_t *in = p_input->slave[i];
2219 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2224 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2226 msg_Err( p_input, "slave[%d] doesn't like "
2227 "DEMUX_GET_TIME -> EOF", i );
2232 if( i_stime >= i_time )
2235 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2241 i_ret = in->p_demux->pf_demux( in->p_demux );
2246 msg_Dbg( p_input, "slave %d EOF", i );
2247 in->b_eof = VLC_TRUE;
2252 static void SlaveSeek( input_thread_t *p_input )
2257 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2259 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2263 for( i = 0; i < p_input->i_slave; i++ )
2265 input_source_t *in = p_input->slave[i];
2267 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2269 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2270 in->b_eof = VLC_TRUE;
2274 /*****************************************************************************
2276 *****************************************************************************/
2277 static void InputMetaUser( input_thread_t *p_input )
2279 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
2282 if( !p_meta ) return;
2284 /* Get meta information from user */
2285 #define GET_META( field, s ) \
2286 var_Get( p_input, (s), &val ); \
2287 if( *val.psz_string ) { \
2288 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2289 p_meta->psz_ ## field = strdup( val.psz_string ); \
2291 free( val.psz_string )
2293 GET_META( title, "meta-title" );
2294 GET_META( artist, "meta-artist" );
2295 GET_META( genre, "meta-genre" );
2296 GET_META( copyright, "meta-copyright" );
2297 GET_META( description, "meta-description" );
2298 GET_META( date, "meta-date" );
2299 GET_META( url, "meta-url" );
2303 /*****************************************************************************
2304 * DecodeUrl: decode a given encoded url
2305 *****************************************************************************/
2306 static void DecodeUrl( char *psz )
2308 char *dup = strdup( psz );
2326 *psz++ = strtol( val, NULL, 16 );
2328 else if( *p == '+' )
2338 if( psz ) *psz++ ='\0';
2339 if( dup ) free( dup );
2342 /*****************************************************************************
2343 * MRLSplit: parse the access, demux and url part of the
2344 * Media Resource Locator.
2345 *****************************************************************************/
2346 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2347 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2349 char *psz_access = NULL;
2350 char *psz_demux = NULL;
2351 char *psz_path = NULL;
2352 char *psz, *psz_check;
2354 psz = strchr( psz_dup, ':' );
2356 /* '@' not allowed in access/demux part */
2357 psz_check = strchr( psz_dup, '@' );
2358 if( psz_check && psz_check < psz ) psz = 0;
2360 #if defined( WIN32 ) || defined( UNDER_CE )
2361 if( psz - psz_dup == 1 )
2363 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2372 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2376 psz = strchr( psz_dup, '/' );
2383 psz_access = psz_dup;
2390 if( !psz_access ) *ppsz_access = "";
2391 else *ppsz_access = psz_access;
2393 if( !psz_demux ) *ppsz_demux = "";
2394 else *ppsz_demux = psz_demux;
2396 if( !psz_path ) *ppsz_path = "";
2397 else *ppsz_path = psz_path;
2400 /*****************************************************************************
2401 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2404 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2405 *****************************************************************************/
2406 static void MRLSections( input_thread_t *p_input, char *psz_source,
2407 int *pi_title_start, int *pi_title_end,
2408 int *pi_chapter_start, int *pi_chapter_end )
2410 char *psz, *psz_end, *psz_next, *psz_check;
2412 *pi_title_start = *pi_title_end = -1;
2413 *pi_chapter_start = *pi_chapter_end = -1;
2415 /* Start by parsing titles and chapters */
2416 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2418 /* Check we are really dealing with a title/chapter section */
2419 psz_check = psz + 1;
2420 if( !*psz_check ) return;
2421 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2422 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2423 if( *psz_check == ':' && ++psz_check )
2424 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2425 if( *psz_check != '-' && *psz_check ) return;
2426 if( *psz_check == '-' && ++psz_check )
2427 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2428 if( *psz_check != ':' && *psz_check ) return;
2429 if( *psz_check == ':' && ++psz_check )
2430 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2431 if( *psz_check ) return;
2433 /* Separate start and end */
2435 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2437 /* Look for the start title */
2438 *pi_title_start = strtol( psz, &psz_next, 0 );
2439 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2440 *pi_title_end = *pi_title_start;
2443 /* Look for the start chapter */
2445 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2446 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2447 *pi_chapter_end = *pi_chapter_start;
2451 /* Look for the end title */
2452 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2453 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2456 /* Look for the end chapter */
2457 if( *psz_end ) psz_end++;
2458 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2459 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2462 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2463 psz_source, *pi_title_start, *pi_chapter_start,
2464 *pi_title_end, *pi_chapter_end );
2467 /*****************************************************************************
2468 * input_AddSubtitles: add a subtitles file and enable it
2469 *****************************************************************************/
2470 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2471 vlc_bool_t b_check_extension )
2473 input_source_t *sub;
2476 char *psz_path, *psz_extension;
2478 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2483 /* if we are provided a subtitle.sub file,
2484 * see if we don't have a subtitle.idx and use it instead */
2485 psz_path = strdup( psz_subtitle );
2488 psz_extension = strrchr( psz_path, '.');
2489 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2493 strcpy( psz_extension, ".idx" );
2494 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2495 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2498 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2499 psz_path, psz_subtitle );
2500 strcpy( psz_subtitle, psz_path );
2506 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2508 sub = InputSourceNew( p_input );
2509 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2511 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
2514 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2516 if( count.i_int == 0 )
2518 /* if it was first one, there is disable too */
2520 if( count.i_int < list.p_list->i_count )
2522 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2523 &list.p_list->p_values[count.i_int] );
2525 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );