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 *****************************************************************************/
33 #include "input_internal.h"
36 #include "../stream_output/stream_output.h"
38 #include <vlc_playlist.h>
39 #include <vlc_interface.h>
41 #include <vlc_demux.h>
42 #include <vlc_charset.h>
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Run ( input_thread_t *p_input );
52 static int RunAndClean ( input_thread_t *p_input );
54 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
55 const char *, vlc_bool_t );
56 static int Init ( input_thread_t *p_input );
57 static void Error ( input_thread_t *p_input );
58 static void End ( input_thread_t *p_input );
59 static void MainLoop( input_thread_t *p_input );
61 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
62 static void ControlReduce( input_thread_t * );
63 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
65 static int UpdateFromAccess( input_thread_t * );
66 static int UpdateFromDemux( input_thread_t * );
67 static int UpdateMeta( input_thread_t * );
69 static void UpdateItemLength( input_thread_t *, int64_t i_length );
71 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
73 static input_source_t *InputSourceNew( input_thread_t *);
74 static int InputSourceInit( input_thread_t *, input_source_t *,
75 const char *, const char *psz_forced_demux );
76 static void InputSourceClean( input_thread_t *, input_source_t * );
78 static void SlaveDemux( input_thread_t *p_input );
79 static void SlaveSeek( input_thread_t *p_input );
81 static void InputMetaUser( input_thread_t *p_input );
83 /*****************************************************************************
84 * This function creates a new input, and returns a pointer
85 * to its description. On error, it returns NULL.
87 * Variables for _public_ use:
90 * - rate,rate-slower, rate-faster
91 * - position, position-offset
93 * - title,title-next,title-prev
94 * - chapter,chapter-next, chapter-prev
95 * - program, audio-es, video-es, spu-es
96 * - audio-delay, spu-delay
101 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
102 * * For intf callback upon changes
104 * TODO explain when Callback is called
105 * TODO complete this list (?)
106 *****************************************************************************/
107 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
108 const char *psz_header, vlc_bool_t b_quick )
110 input_thread_t *p_input = NULL; /* thread descriptor */
114 /* Allocate descriptor */
115 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
116 if( p_input == NULL )
118 msg_Err( p_parent, "out of memory" );
121 MALLOC_NULL( p_input->p, input_thread_private_t );
123 /* One "randomly" selected input thread is responsible for computing
124 * the global stats. Check if there is already someone doing this */
125 if( p_input->p_libvlc->p_playlist->p_stats && !b_quick )
127 vlc_mutex_lock( &p_input->p_libvlc->p_playlist->p_stats->lock );
128 if( p_input->p_libvlc->p_playlist->p_stats_computer == NULL )
130 p_input->p_libvlc->p_playlist->p_stats_computer = p_input;
132 vlc_mutex_unlock( &p_input->p_libvlc->p_playlist->p_stats->lock );
135 p_input->b_preparsing = b_quick;
136 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
138 /* Init Common fields */
139 p_input->b_eof = VLC_FALSE;
140 p_input->b_can_pace_control = VLC_TRUE;
141 p_input->p->i_start = 0;
143 p_input->p->i_stop = 0;
144 p_input->p->i_title = 0;
145 p_input->p->title = NULL;
146 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
147 p_input->i_state = INIT_S;
148 p_input->p->i_rate = INPUT_RATE_DEFAULT;
149 p_input->p->i_bookmark = 0;
150 p_input->p->bookmark = NULL;
151 p_input->p->p_meta = NULL;
152 p_input->p->p_es_out = NULL;
153 p_input->p->p_sout = NULL;
154 p_input->p->b_out_pace_control = VLC_FALSE;
155 p_input->i_pts_delay = 0;
157 /* Init Input fields */
158 p_input->p->input.p_item = p_item;
159 p_input->p->input.p_access = NULL;
160 p_input->p->input.p_stream = NULL;
161 p_input->p->input.p_demux = NULL;
162 p_input->p->input.b_title_demux = VLC_FALSE;
163 p_input->p->input.i_title = 0;
164 p_input->p->input.title = NULL;
165 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
166 p_input->p->input.b_can_pace_control = VLC_TRUE;
167 p_input->p->input.b_eof = VLC_FALSE;
168 p_input->p->input.i_cr_average = 0;
170 if( !p_input->p->input.p_item->p_meta )
171 p_input->p->input.p_item->p_meta = vlc_meta_New();
173 if( !p_item->p_stats )
175 p_item->p_stats = (input_stats_t*)malloc( sizeof( input_stats_t ) );
176 vlc_mutex_init( p_input, &p_item->p_stats->lock );
177 stats_ReinitInputStats( p_item->p_stats );
181 p_input->p->i_slave = 0;
182 p_input->p->slave = NULL;
184 /* Init control buffer */
185 vlc_mutex_init( p_input, &p_input->p->lock_control );
186 p_input->p->i_control = 0;
188 /* Parse input options */
189 vlc_mutex_lock( &p_item->lock );
190 for( i = 0; i < p_item->i_options; i++ )
192 var_OptionParse( p_input, p_item->ppsz_options[i] );
194 vlc_mutex_unlock( &p_item->lock );
196 /* Create Object Variables for private use only */
197 input_ConfigVarInit( p_input );
199 /* Create Objects variables for public Get and Set */
200 if( !p_input->b_preparsing )
201 input_ControlVarInit( p_input );
203 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
205 if( !p_input->b_preparsing )
207 var_Get( p_input, "bookmarks", &val );
210 /* FIXME: have a common cfg parsing routine used by sout and others */
211 char *psz_parser, *psz_start, *psz_end;
212 psz_parser = val.psz_string;
213 while( (psz_start = strchr( psz_parser, '{' ) ) )
215 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
218 psz_end = strchr( psz_start, '}' );
219 if( !psz_end ) break;
220 psz_parser = psz_end + 1;
221 backup = *psz_parser;
224 while( (psz_end = strchr( psz_start, ',' ) ) )
227 if( !strncmp( psz_start, "name=", 5 ) )
229 p_seekpoint->psz_name = psz_start + 5;
231 else if( !strncmp( psz_start, "bytes=", 6 ) )
233 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
235 else if( !strncmp( psz_start, "time=", 5 ) )
237 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
240 psz_start = psz_end + 1;
242 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
243 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
244 p_seekpoint->i_time_offset );
245 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
246 vlc_seekpoint_Delete( p_seekpoint );
247 *psz_parser = backup;
249 free( val.psz_string );
253 /* Remove 'Now playing' info as it is probably outdated */
254 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT),
255 VLC_META_NOW_PLAYING ); /* ? Don't translate as it might has been copied ? */
261 * Initialize an input thread and run it. You will need to monitor the
262 * thread to clean up after it is done
264 * \param p_parent a vlc_object
265 * \param p_item an input item
266 * \return a pointer to the spawned input thread
268 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
269 input_item_t *p_item )
271 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,
277 const char *psz_header )
279 input_thread_t *p_input = NULL; /* thread descriptor */
281 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
285 /* Now we can attach our new input */
286 vlc_object_attach( p_input, p_parent );
288 /* Create thread and wait for its readiness. */
289 if( vlc_thread_create( p_input, "input", Run,
290 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
292 input_ChangeState( p_input, ERROR_S );
293 msg_Err( p_input, "cannot create input thread" );
294 vlc_object_detach( p_input );
296 vlc_object_destroy( p_input );
304 * Initialize an input thread and run it. This thread will clean after himself,
305 * you can forget about it. It can work either in blocking or non-blocking mode
307 * \param p_parent a vlc_object
308 * \param p_item an input item
309 * \param b_block should we block until read is finished ?
310 * \return the input object id if non blocking, an error code else
312 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
315 input_thread_t *p_input = NULL; /* thread descriptor */
317 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
321 /* Now we can attach our new input */
322 vlc_object_attach( p_input, p_parent );
326 RunAndClean( p_input );
331 if( vlc_thread_create( p_input, "input", RunAndClean,
332 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
334 input_ChangeState( p_input, ERROR_S );
335 msg_Err( p_input, "cannot create input thread" );
336 vlc_object_detach( p_input );
338 vlc_object_destroy( p_input );
342 return p_input->i_object_id;
346 * Initialize an input and initialize it to preparse the item
347 * This function is blocking. It will only accept to parse files
349 * \param p_parent a vlc_object_t
350 * \param p_item an input item
351 * \return VLC_SUCCESS or an error
353 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
355 input_thread_t *p_input = NULL; /* thread descriptor */
357 /* Allocate descriptor */
358 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
362 p_input->i_flags |= OBJECT_FLAGS_QUIET;
363 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
365 /* Now we can attach our new input */
366 vlc_object_attach( p_input, p_parent );
370 /* Clean up master */
371 InputSourceClean( p_input, &p_input->p->input );
373 /* Unload all modules */
374 if( p_input->p->p_es_out ) input_EsOutDelete( p_input->p->p_es_out );
376 vlc_object_detach( p_input );
378 vlc_object_destroy( p_input );
384 * Request a running input thread to stop and die
386 * \param the input thread to stop
388 void input_StopThread( input_thread_t *p_input )
393 /* Set die for input */
394 p_input->b_die = VLC_TRUE;
396 /* We cannot touch p_input fields directly (we come from another thread),
397 * so use the vlc_object_find way, it's perfectly safe */
399 /* Set die for all access */
400 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
401 for( i = 0; i < p_list->i_count; i++ )
403 p_list->p_values[i].p_object->b_die = VLC_TRUE;
405 vlc_list_release( p_list );
407 /* Set die for all stream */
408 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
409 for( i = 0; i < p_list->i_count; i++ )
411 p_list->p_values[i].p_object->b_die = VLC_TRUE;
413 vlc_list_release( p_list );
415 /* Set die for all demux */
416 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
417 for( i = 0; i < p_list->i_count; i++ )
419 p_list->p_values[i].p_object->b_die = VLC_TRUE;
421 vlc_list_release( p_list );
423 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
427 * Clean up a dead input thread
428 * This function does not return until the thread is effectively cancelled.
430 * \param the input thread to kill
432 void input_DestroyThread( input_thread_t *p_input )
434 /* Join the thread */
435 vlc_thread_join( p_input );
437 /* Delete input lock (only after thread joined) */
438 vlc_mutex_destroy( &p_input->p->lock_control );
440 /* TODO: maybe input_DestroyThread should also delete p_input instead
441 * of the playlist but I'm not sure if it's possible */
444 /*****************************************************************************
445 * Run: main thread loop
446 * This is the "normal" thread that spawns the input processing chain,
447 * reads the stream, cleans up and waits
448 *****************************************************************************/
449 static int Run( input_thread_t *p_input )
451 /* Signal that the thread is launched */
452 vlc_thread_ready( p_input );
454 if( Init( p_input ) )
456 /* If we failed, wait before we are killed, and exit */
457 p_input->b_error = VLC_TRUE;
458 playlist_Signal( pl_Get( p_input ) );
462 /* Tell we're dead */
463 p_input->b_dead = VLC_TRUE;
470 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
472 /* We have finish to demux data but not to play them */
473 while( !p_input->b_die )
475 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
478 msg_Dbg( p_input, "waiting decoder fifos to empty" );
480 msleep( INPUT_IDLE_SLEEP );
483 /* We have finished */
484 p_input->b_eof = VLC_TRUE;
485 playlist_Signal( pl_Get( p_input ) );
488 /* Wait until we are asked to die */
489 if( !p_input->b_die )
500 /*****************************************************************************
501 * RunAndClean: main thread loop
502 * This is the "just forget me" thread that spawns the input processing chain,
503 * reads the stream, cleans up and releases memory
504 *****************************************************************************/
505 static int RunAndClean( input_thread_t *p_input )
507 /* Signal that the thread is launched */
508 vlc_thread_ready( p_input );
510 if( Init( p_input ) )
512 /* If we failed, just exit */
518 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
520 /* We have finished demuxing data but not playing it */
521 while( !p_input->b_die )
523 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
526 msg_Dbg( p_input, "waiting decoder fifos to empty" );
528 msleep( INPUT_IDLE_SLEEP );
531 /* We have finished */
532 p_input->b_eof = VLC_TRUE;
539 vlc_object_detach( p_input );
541 vlc_object_destroy( p_input );
546 /*****************************************************************************
547 * Main loop: Fill buffers from access, and demux
548 *****************************************************************************/
549 static void MainLoop( input_thread_t *p_input )
551 int64_t i_intf_update = 0;
553 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
555 vlc_bool_t b_force_update = VLC_FALSE;
561 if( p_input->i_state != PAUSE_S )
563 if( p_input->p->i_stop <= 0 || p_input->i_time < p_input->p->i_stop )
564 i_ret=p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
571 if( p_input->p->input.b_title_demux &&
572 p_input->p->input.p_demux->info.i_update )
574 i_ret = UpdateFromDemux( p_input );
575 b_force_update = VLC_TRUE;
577 else if( !p_input->p->input.b_title_demux &&
578 p_input->p->input.p_access &&
579 p_input->p->input.p_access->info.i_update )
581 i_ret = UpdateFromAccess( p_input );
582 b_force_update = VLC_TRUE;
586 if( i_ret == 0 ) /* EOF */
590 var_Get( p_input, "input-repeat", &repeat );
591 if( repeat.i_int == 0 )
593 /* End of file - we do not set b_die because only the
594 * playlist is allowed to do so. */
595 input_ChangeState( p_input, END_S );
596 msg_Dbg( p_input, "EOF reached" );
597 p_input->p->input.b_eof = VLC_TRUE;
601 msg_Dbg( p_input, "repeating the same input (%d)",
603 if( repeat.i_int > 0 )
606 var_Set( p_input, "input-repeat", repeat );
609 /* Seek to start title/seekpoint */
610 val.i_int = p_input->p->input.i_title_start -
611 p_input->p->input.i_title_offset;
612 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
614 input_ControlPush( p_input,
615 INPUT_CONTROL_SET_TITLE, &val );
617 val.i_int = p_input->p->input.i_seekpoint_start -
618 p_input->p->input.i_seekpoint_offset;
619 if( val.i_int > 0 /* TODO: check upper boundary */ )
620 input_ControlPush( p_input,
621 INPUT_CONTROL_SET_SEEKPOINT, &val );
623 /* Seek to start position */
624 if( p_input->p->i_start > 0 )
626 val.i_time = p_input->p->i_start;
627 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
633 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
640 p_input->b_error = VLC_TRUE;
643 if( i_ret > 0 && p_input->p->i_slave > 0 )
645 SlaveDemux( p_input );
655 vlc_mutex_lock( &p_input->p->lock_control );
656 ControlReduce( p_input );
657 while( !ControlPopNoLock( p_input, &i_type, &val ) )
659 msg_Dbg( p_input, "control type=%d", i_type );
660 if( Control( p_input, i_type, val ) )
661 b_force_update = VLC_TRUE;
663 vlc_mutex_unlock( &p_input->p->lock_control );
665 if( b_force_update || i_intf_update < mdate() )
669 int64_t i_time, i_length;
670 /* update input status variables */
671 if( !demux2_Control( p_input->p->input.p_demux,
672 DEMUX_GET_POSITION, &f_pos ) )
674 val.f_float = (float)f_pos;
675 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
677 if( !demux2_Control( p_input->p->input.p_demux,
678 DEMUX_GET_TIME, &i_time ) )
680 p_input->i_time = i_time;
682 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
684 if( !demux2_Control( p_input->p->input.p_demux,
685 DEMUX_GET_LENGTH, &i_length ) )
688 var_Get( p_input, "length", &old_val );
689 val.i_time = i_length;
690 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
692 if( old_val.i_time != val.i_time )
694 UpdateItemLength( p_input, i_length );
698 var_SetBool( p_input, "intf-change", VLC_TRUE );
699 i_intf_update = mdate() + I64C(150000);
701 /* 150ms * 8 = ~ 1 second */
702 if( ++i_updates % 8 == 0 )
704 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
705 /* Are we the thread responsible for computing global stats ? */
706 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
708 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
709 p_input->p_libvlc->p_playlist->p_stats );
715 static int Init( input_thread_t * p_input )
725 /* Initialize optional stream output. (before access/demuxer)
726 * XXX: we add a special case if the uri starts by vlc.
727 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
728 * be destroyed in case of a file).
729 * (this will break playing of file starting by 'vlc:' but I don't
730 * want to add more logic, just force file by file:// or code it ;)
732 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
733 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
735 if( !p_input->b_preparsing )
737 /* Prepare statistics */
738 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
739 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
740 if( p_input->p_libvlc->b_stats )
742 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
743 INIT_COUNTER( read_packets, INTEGER, COUNTER );
744 INIT_COUNTER( demux_read, INTEGER, COUNTER );
745 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
746 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
747 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
748 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
749 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
750 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
751 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
752 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
753 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
754 p_input->p->counters.p_sout_send_bitrate = NULL;
755 p_input->p->counters.p_sout_sent_packets = NULL;
756 p_input->p->counters.p_sout_sent_bytes = NULL;
757 if( p_input->p->counters.p_demux_bitrate )
758 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
759 if( p_input->p->counters.p_input_bitrate )
760 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
764 psz = var_GetString( p_input, "sout" );
765 if( *psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
767 p_input->p->p_sout = sout_NewInstance( p_input, psz );
768 if( p_input->p->p_sout == NULL )
770 input_ChangeState( p_input, ERROR_S );
771 msg_Err( p_input, "cannot start stream output instance, " \
776 if( p_input->p_libvlc->b_stats )
778 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
779 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
780 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
781 if( p_input->p->counters.p_sout_send_bitrate )
782 p_input->p->counters.p_sout_send_bitrate->update_interval =
790 p_input->p->p_es_out = input_EsOutNew( p_input );
791 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
792 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
794 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
795 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
797 if( InputSourceInit( p_input, &p_input->p->input,
798 p_input->p->input.p_item->psz_uri, NULL ) )
803 /* Create global title (from master) */
804 if( !p_input->b_preparsing )
806 p_input->p->i_title = p_input->p->input.i_title;
807 p_input->p->title = p_input->p->input.title;
808 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
809 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
810 if( p_input->p->i_title > 0 )
812 /* Setup variables */
813 input_ControlVarNavigation( p_input );
814 input_ControlVarTitle( p_input, 0 );
818 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
819 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
822 if( p_input->i_pts_delay < 0 )
823 p_input->i_pts_delay = 0;
825 /* If the desynchronisation requested by the user is < 0, we need to
826 * cache more data. */
827 var_Get( p_input, "audio-desync", &val );
828 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
830 /* Update cr_average depending on the caching */
831 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
832 p_input->p->input.i_cr_average /= 10;
833 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
836 /* Load master infos */
838 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
839 &val.i_time ) && val.i_time > 0 )
841 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
842 UpdateItemLength( p_input, val.i_time );
843 p_input->p->input.p_item->i_duration = val.i_time;
846 /* Start title/chapter */
847 if( !p_input->b_preparsing )
849 val.i_int = p_input->p->input.i_title_start -
850 p_input->p->input.i_title_offset;
851 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
852 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
853 val.i_int = p_input->p->input.i_seekpoint_start -
854 p_input->p->input.i_seekpoint_offset;
855 if( val.i_int > 0 /* TODO: check upper boundary */ )
856 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
860 p_input->p->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
862 p_input->p->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
865 if( p_input->p->i_start > 0 )
867 if( p_input->p->i_start >= val.i_time )
869 msg_Warn( p_input, "invalid start-time ignored" );
875 msg_Dbg( p_input, "starting at time: %ds",
876 (int)( p_input->p->i_start / I64C(1000000) ) );
878 s.i_time = p_input->p->i_start;
879 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
882 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
884 msg_Warn( p_input, "invalid stop-time ignored" );
885 p_input->p->i_stop = 0;
889 /* Get fps and set it if not already set */
890 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
893 float f_requested_fps;
895 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
896 var_SetFloat( p_input, "sub-original-fps", f_fps );
898 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
899 if( f_requested_fps != f_fps )
901 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
903 var_SetFloat( p_input, "sub-fps", f_requested_fps );
907 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
910 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
913 /* Look for and add subtitle files */
914 psz_subtitle = var_GetString( p_input, "sub-file" );
917 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
918 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
921 var_Get( p_input, "sub-autodetect-file", &val );
924 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
925 char **subs = subtitles_Detect( p_input, psz_autopath,
926 p_input->p->input.p_item->psz_uri );
930 /* Try to autoselect the first autodetected subtitles file
931 * if no subtitles file was specified */
932 if( *psz_subtitle == 0 && subs && subs[0] )
934 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
939 /* Then, just add the following subtitles files */
940 for( ; subs && subs[i]; i++ )
942 if( strcmp( psz_subtitle, subs[i] ) )
944 sub = InputSourceNew( p_input );
945 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
947 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
953 if( subs ) free( subs );
954 if( psz_autopath ) free( psz_autopath );
956 free( psz_subtitle );
959 psz = var_GetString( p_input, "input-slave" );
963 input_source_t *slave;
966 while( *psz == ' ' || *psz == '#' )
970 if( ( psz_delim = strchr( psz, '#' ) ) )
979 msg_Dbg( p_input, "adding slave input '%s'", psz );
980 slave = InputSourceNew( p_input );
981 if( !InputSourceInit( p_input, slave, psz, NULL ) )
983 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
989 if( psz ) free( psz );
993 p_input->p->i_start = 0;
994 p_input->p->i_start = 0;
998 if( !p_input->b_preparsing )
1000 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1001 i_es_out_mode = ES_OUT_MODE_AUTO;
1003 if( p_input->p->p_sout )
1005 var_Get( p_input, "sout-all", &val );
1008 i_es_out_mode = ES_OUT_MODE_ALL;
1013 var_Get( p_input, "programs", &val );
1014 if ( val.p_list && val.p_list->i_count )
1016 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1017 /* Note : we should remove the "program" callback. */
1020 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1024 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1026 /* Inform the demuxer about waited group (needed only for DVB) */
1027 if( i_es_out_mode == ES_OUT_MODE_ALL )
1029 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1031 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1033 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1038 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1039 (int) var_GetInteger( p_input, "program" ), NULL );
1042 if( p_input->p->p_sout )
1044 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1046 p_input->p->b_out_pace_control = VLC_FALSE;
1050 p_input->p->b_out_pace_control = VLC_TRUE;
1053 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1055 /* We don't want a high input priority here or we'll
1056 * end-up sucking up all the CPU time */
1057 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1060 msg_Dbg( p_input, "starting in %s mode",
1061 p_input->p->b_out_pace_control ? "async" : "sync" );
1065 p_meta = p_input->p->input.p_item->p_meta;
1066 /* Get meta data from users */
1067 InputMetaUser( p_input );
1068 /* Get meta data from master input */
1069 demux2_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
1071 /* Access_file does not give any meta, and there are no slave */
1072 if( !p_input->b_preparsing )
1074 if( p_input->p->input.p_access )
1075 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1078 /* Get meta data from slave input */
1079 for( i = 0; i < p_input->p->i_slave; i++ )
1081 demux2_Control( p_input->p->slave[i]->p_demux,
1082 DEMUX_GET_META, p_meta );
1083 if( p_input->p->slave[i]->p_access )
1085 access2_Control( p_input->p->slave[i]->p_access,
1086 ACCESS_GET_META, p_meta );
1091 UpdateMeta( p_input );
1093 if( !p_input->b_preparsing )
1095 msg_Dbg( p_input, "`%s' successfully opened",
1096 p_input->p->input.p_item->psz_uri );
1100 /* initialization is complete */
1101 input_ChangeState( p_input, PLAYING_S );
1106 input_ChangeState( p_input, ERROR_S );
1108 if( p_input->p->p_es_out )
1109 input_EsOutDelete( p_input->p->p_es_out );
1111 if( p_input->p->p_sout )
1112 sout_DeleteInstance( p_input->p->p_sout );
1114 /* Mark them deleted */
1115 p_input->p->input.p_demux = NULL;
1116 p_input->p->input.p_stream = NULL;
1117 p_input->p->input.p_access = NULL;
1118 p_input->p->p_es_out = NULL;
1119 p_input->p->p_sout = NULL;
1121 return VLC_EGENERIC;
1124 /*****************************************************************************
1125 * Error: RunThread() error loop
1126 *****************************************************************************
1127 * This function is called when an error occurred during thread main's loop.
1128 *****************************************************************************/
1129 static void Error( input_thread_t *p_input )
1131 while( !p_input->b_die )
1134 input_ChangeState( p_input, ERROR_S );
1135 msleep( INPUT_IDLE_SLEEP );
1139 /*****************************************************************************
1140 * End: end the input thread
1141 *****************************************************************************/
1142 static void End( input_thread_t * p_input )
1146 msg_Dbg( p_input, "closing input" );
1148 /* We are at the end */
1149 input_ChangeState( p_input, END_S );
1151 /* Clean control variables */
1152 if( !p_input->b_preparsing )
1153 input_ControlVarClean( p_input );
1155 /* Clean up master */
1156 InputSourceClean( p_input, &p_input->p->input );
1159 for( i = 0; i < p_input->p->i_slave; i++ )
1161 InputSourceClean( p_input, p_input->p->slave[i] );
1162 free( p_input->p->slave[i] );
1164 if( p_input->p->slave ) free( p_input->p->slave );
1166 /* Unload all modules */
1167 if( p_input->p->p_es_out )
1168 input_EsOutDelete( p_input->p->p_es_out );
1170 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1171 if( p_input->p_libvlc->b_stats )
1173 /* make sure we are up to date */
1174 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1175 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1177 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1178 p_input->p_libvlc->p_playlist->p_stats );
1179 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1181 CL_CO( read_bytes );
1182 CL_CO( read_packets );
1183 CL_CO( demux_read );
1184 CL_CO( input_bitrate );
1185 CL_CO( demux_bitrate );
1186 CL_CO( played_abuffers );
1187 CL_CO( lost_abuffers );
1188 CL_CO( displayed_pictures );
1189 CL_CO( lost_pictures );
1190 CL_CO( decoded_audio) ;
1191 CL_CO( decoded_video );
1192 CL_CO( decoded_sub) ;
1195 /* Close optional stream output instance */
1196 if( p_input->p->p_sout )
1200 CL_CO( sout_sent_packets );
1201 CL_CO( sout_sent_bytes );
1202 CL_CO( sout_send_bitrate );
1204 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool )
1206 /* attach sout to the playlist */
1207 msg_Dbg( p_input, "keeping sout" );
1208 vlc_object_detach( p_input->p->p_sout );
1209 vlc_object_attach( p_input->p->p_sout, p_input->p_libvlc->p_playlist );
1213 msg_Dbg( p_input, "destroying sout" );
1214 sout_DeleteInstance( p_input->p->p_sout );
1219 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1221 /* Tell we're dead */
1222 p_input->b_dead = VLC_TRUE;
1225 /*****************************************************************************
1227 *****************************************************************************/
1228 static inline int ControlPopNoLock( input_thread_t *p_input,
1229 int *pi_type, vlc_value_t *p_val )
1231 if( p_input->p->i_control <= 0 )
1233 return VLC_EGENERIC;
1236 *pi_type = p_input->p->control[0].i_type;
1237 *p_val = p_input->p->control[0].val;
1239 p_input->p->i_control--;
1240 if( p_input->p->i_control > 0 )
1244 for( i = 0; i < p_input->p->i_control; i++ )
1246 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1247 p_input->p->control[i].val = p_input->p->control[i+1].val;
1254 static void ControlReduce( input_thread_t *p_input )
1261 for( i = 1; i < p_input->p->i_control; i++ )
1263 const int i_lt = p_input->p->control[i-1].i_type;
1264 const int i_ct = p_input->p->control[i].i_type;
1266 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1267 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1271 ( i_ct == INPUT_CONTROL_SET_STATE ||
1272 i_ct == INPUT_CONTROL_SET_RATE ||
1273 i_ct == INPUT_CONTROL_SET_POSITION ||
1274 i_ct == INPUT_CONTROL_SET_TIME ||
1275 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1276 i_ct == INPUT_CONTROL_SET_TITLE ||
1277 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1278 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1281 // msg_Dbg( p_input, "merged at %d", i );
1282 /* Remove the i-1 */
1283 for( j = i; j < p_input->p->i_control; j++ )
1284 p_input->p->control[j-1] = p_input->p->control[j];
1285 p_input->p->i_control--;
1289 /* TODO but that's not that important
1290 - merge SET_X with SET_X_CMD
1291 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1292 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1299 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1302 vlc_bool_t b_force_update = VLC_FALSE;
1304 if( !p_input ) return b_force_update;
1308 case INPUT_CONTROL_SET_DIE:
1309 msg_Dbg( p_input, "control: stopping input" );
1310 /* Mark all submodules to die */
1311 if( p_input->p->input.p_access )
1312 p_input->p->input.p_access->b_die = VLC_TRUE;
1313 if( p_input->p->input.p_stream )
1314 p_input->p->input.p_stream->b_die = VLC_TRUE;
1315 p_input->p->input.p_demux->b_die = VLC_TRUE;
1317 p_input->b_die = VLC_TRUE;
1320 case INPUT_CONTROL_SET_POSITION:
1321 case INPUT_CONTROL_SET_POSITION_OFFSET:
1324 if( i_type == INPUT_CONTROL_SET_POSITION )
1326 f_pos = val.f_float;
1330 /* Should not fail */
1331 demux2_Control( p_input->p->input.p_demux,
1332 DEMUX_GET_POSITION, &f_pos );
1333 f_pos += val.f_float;
1335 if( f_pos < 0.0 ) f_pos = 0.0;
1336 if( f_pos > 1.0 ) f_pos = 1.0;
1337 /* Reset the decoders states and clock sync (before calling the demuxer */
1338 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1339 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1340 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1343 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1344 "%2.1f%% failed", f_pos * 100 );
1348 if( p_input->p->i_slave > 0 )
1349 SlaveSeek( p_input );
1351 b_force_update = VLC_TRUE;
1356 case INPUT_CONTROL_SET_TIME:
1357 case INPUT_CONTROL_SET_TIME_OFFSET:
1362 if( i_type == INPUT_CONTROL_SET_TIME )
1364 i_time = val.i_time;
1368 /* Should not fail */
1369 demux2_Control( p_input->p->input.p_demux,
1370 DEMUX_GET_TIME, &i_time );
1371 i_time += val.i_time;
1373 if( i_time < 0 ) i_time = 0;
1375 /* Reset the decoders states and clock sync (before calling the demuxer */
1376 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1377 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1379 i_ret = demux2_Control( p_input->p->input.p_demux,
1380 DEMUX_SET_TIME, i_time );
1385 /* Emulate it with a SET_POS */
1386 demux2_Control( p_input->p->input.p_demux,
1387 DEMUX_GET_LENGTH, &i_length );
1390 double f_pos = (double)i_time / (double)i_length;
1391 i_ret = demux2_Control( p_input->p->input.p_demux,
1392 DEMUX_SET_POSITION, f_pos );
1397 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1398 " failed or not possible", i_time );
1402 if( p_input->p->i_slave > 0 )
1403 SlaveSeek( p_input );
1405 b_force_update = VLC_TRUE;
1410 case INPUT_CONTROL_SET_STATE:
1411 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1412 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1415 if( p_input->p->input.p_access )
1416 i_ret = access2_Control( p_input->p->input.p_access,
1417 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1419 i_ret = demux2_Control( p_input->p->input.p_demux,
1420 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1424 /* FIXME What to do ? */
1425 msg_Warn( p_input, "cannot unset pause -> EOF" );
1426 vlc_mutex_unlock( &p_input->p->lock_control );
1427 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1428 vlc_mutex_lock( &p_input->p->lock_control );
1431 b_force_update = VLC_TRUE;
1433 /* Switch to play */
1434 p_input->i_state = PLAYING_S;
1435 val.i_int = PLAYING_S;
1436 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1439 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1440 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1442 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1443 p_input->p->b_can_pause )
1446 if( p_input->p->input.p_access )
1447 i_ret = access2_Control( p_input->p->input.p_access,
1448 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1450 i_ret = demux2_Control( p_input->p->input.p_demux,
1451 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1453 b_force_update = VLC_TRUE;
1457 msg_Warn( p_input, "cannot set pause state" );
1458 val.i_int = p_input->i_state;
1462 val.i_int = PAUSE_S;
1465 /* Switch to new state */
1466 p_input->i_state = val.i_int;
1467 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1469 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1471 b_force_update = VLC_TRUE;
1473 /* Correct "state" value */
1474 val.i_int = p_input->i_state;
1475 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1477 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1479 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1483 case INPUT_CONTROL_SET_RATE:
1484 case INPUT_CONTROL_SET_RATE_SLOWER:
1485 case INPUT_CONTROL_SET_RATE_FASTER:
1489 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1490 i_rate = p_input->p->i_rate * 2;
1491 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1492 i_rate = p_input->p->i_rate / 2;
1496 if( i_rate < INPUT_RATE_MIN )
1498 msg_Dbg( p_input, "cannot set rate faster" );
1499 i_rate = INPUT_RATE_MIN;
1501 else if( i_rate > INPUT_RATE_MAX )
1503 msg_Dbg( p_input, "cannot set rate slower" );
1504 i_rate = INPUT_RATE_MAX;
1506 if( i_rate != INPUT_RATE_DEFAULT &&
1507 ( !p_input->b_can_pace_control ||
1508 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1510 msg_Dbg( p_input, "cannot change rate" );
1511 i_rate = INPUT_RATE_DEFAULT;
1513 if( i_rate != p_input->p->i_rate )
1515 p_input->p->i_rate = i_rate;
1517 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1519 /* We haven't send data to decoder when rate != default */
1520 if( i_rate == INPUT_RATE_DEFAULT )
1521 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE );
1524 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1526 b_force_update = VLC_TRUE;
1531 case INPUT_CONTROL_SET_PROGRAM:
1532 /* No need to force update, es_out does it if needed */
1533 es_out_Control( p_input->p->p_es_out,
1534 ES_OUT_SET_GROUP, val.i_int );
1536 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1540 case INPUT_CONTROL_SET_ES:
1541 /* No need to force update, es_out does it if needed */
1542 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1543 input_EsOutGetFromID( p_input->p->p_es_out,
1547 case INPUT_CONTROL_SET_AUDIO_DELAY:
1548 input_EsOutSetDelay( p_input->p->p_es_out,
1549 AUDIO_ES, val.i_time );
1550 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1553 case INPUT_CONTROL_SET_SPU_DELAY:
1554 input_EsOutSetDelay( p_input->p->p_es_out,
1555 SPU_ES, val.i_time );
1556 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1559 case INPUT_CONTROL_SET_TITLE:
1560 case INPUT_CONTROL_SET_TITLE_NEXT:
1561 case INPUT_CONTROL_SET_TITLE_PREV:
1562 if( p_input->p->input.b_title_demux &&
1563 p_input->p->input.i_title > 0 )
1566 /* FIXME handle demux title */
1567 demux_t *p_demux = p_input->p->input.p_demux;
1570 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1571 i_title = p_demux->info.i_title - 1;
1572 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1573 i_title = p_demux->info.i_title + 1;
1575 i_title = val.i_int;
1577 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1579 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1580 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1582 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1583 input_ControlVarTitle( p_input, i_title );
1586 else if( p_input->p->input.i_title > 0 )
1588 access_t *p_access = p_input->p->input.p_access;
1591 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1592 i_title = p_access->info.i_title - 1;
1593 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1594 i_title = p_access->info.i_title + 1;
1596 i_title = val.i_int;
1598 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1600 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1601 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1603 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1604 stream_AccessReset( p_input->p->input.p_stream );
1608 case INPUT_CONTROL_SET_SEEKPOINT:
1609 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1610 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1611 if( p_input->p->input.b_title_demux &&
1612 p_input->p->input.i_title > 0 )
1614 demux_t *p_demux = p_input->p->input.p_demux;
1616 int64_t i_input_time;
1617 int64_t i_seekpoint_time;
1619 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1621 i_seekpoint = p_demux->info.i_seekpoint;
1622 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1623 if( i_seekpoint_time >= 0 &&
1624 !demux2_Control( p_demux,
1625 DEMUX_GET_TIME, &i_input_time ) )
1627 if ( i_input_time < i_seekpoint_time + 3000000 )
1633 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1634 i_seekpoint = p_demux->info.i_seekpoint + 1;
1636 i_seekpoint = val.i_int;
1638 if( i_seekpoint >= 0 && i_seekpoint <
1639 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1641 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1642 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1644 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1647 else if( p_input->p->input.i_title > 0 )
1649 demux_t *p_demux = p_input->p->input.p_demux;
1650 access_t *p_access = p_input->p->input.p_access;
1652 int64_t i_input_time;
1653 int64_t i_seekpoint_time;
1655 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1657 i_seekpoint = p_access->info.i_seekpoint;
1658 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1659 if( i_seekpoint_time >= 0 &&
1660 demux2_Control( p_demux,
1661 DEMUX_GET_TIME, &i_input_time ) )
1663 if ( i_input_time < i_seekpoint_time + 3000000 )
1669 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1670 i_seekpoint = p_access->info.i_seekpoint + 1;
1672 i_seekpoint = val.i_int;
1674 if( i_seekpoint >= 0 && i_seekpoint <
1675 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1677 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1678 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1680 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1682 stream_AccessReset( p_input->p->input.p_stream );
1687 case INPUT_CONTROL_ADD_SLAVE:
1688 if( val.psz_string )
1690 input_source_t *slave = InputSourceNew( p_input );
1692 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1694 vlc_meta_t *p_meta = p_input->p->input.p_item->p_meta;
1698 msg_Dbg( p_input, "adding %s as slave on the fly",
1702 if( demux2_Control( p_input->p->input.p_demux,
1703 DEMUX_GET_TIME, &i_time ) )
1705 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1706 InputSourceClean( p_input, slave );
1710 if( demux2_Control( slave->p_demux,
1711 DEMUX_SET_TIME, i_time ) )
1713 msg_Err( p_input, "seek failed for new slave" );
1714 InputSourceClean( p_input, slave );
1719 /* Get meta (access and demux) */
1720 access2_Control( slave->p_access, ACCESS_GET_META,
1722 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1723 UpdateMeta( p_input );
1725 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1730 msg_Warn( p_input, "failed to add %s as slave",
1734 free( val.psz_string );
1738 case INPUT_CONTROL_SET_BOOKMARK:
1740 msg_Err( p_input, "not yet implemented" );
1744 return b_force_update;
1747 /*****************************************************************************
1749 *****************************************************************************/
1750 static int UpdateFromDemux( input_thread_t *p_input )
1752 demux_t *p_demux = p_input->p->input.p_demux;
1755 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1757 v.i_int = p_demux->info.i_title;
1758 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1760 input_ControlVarTitle( p_input, p_demux->info.i_title );
1762 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1764 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1766 v.i_int = p_demux->info.i_seekpoint;
1767 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1769 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1771 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1773 /* Hmmm only works with master input */
1774 if( p_input->p->input.p_demux == p_demux )
1776 int i_title_end = p_input->p->input.i_title_end -
1777 p_input->p->input.i_title_offset;
1778 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1779 p_input->p->input.i_seekpoint_offset;
1781 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1783 if( p_demux->info.i_title > i_title_end ||
1784 ( p_demux->info.i_title == i_title_end &&
1785 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1787 else if( i_seekpoint_end >=0 )
1789 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1791 else if( i_title_end >= 0 )
1793 if( p_demux->info.i_title > i_title_end ) return 0;
1800 /*****************************************************************************
1802 *****************************************************************************/
1803 static int UpdateFromAccess( input_thread_t *p_input )
1805 access_t *p_access = p_input->p->input.p_access;
1808 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1810 v.i_int = p_access->info.i_title;
1811 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1813 input_ControlVarTitle( p_input, p_access->info.i_title );
1815 stream_AccessUpdate( p_input->p->input.p_stream );
1817 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1819 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1821 v.i_int = p_access->info.i_seekpoint;
1822 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1823 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1825 if( p_access->info.i_update & INPUT_UPDATE_META )
1827 /* TODO maybe multi - access ? */
1828 vlc_meta_t *p_meta = p_input->p->input.p_item->p_meta;
1829 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
1830 UpdateMeta( p_input );
1831 var_SetBool( p_input, "item-change", p_input->p->input.p_item->i_id );
1832 p_access->info.i_update &= ~INPUT_UPDATE_META;
1835 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1837 /* Hmmm only works with master input */
1838 if( p_input->p->input.p_access == p_access )
1840 int i_title_end = p_input->p->input.i_title_end -
1841 p_input->p->input.i_title_offset;
1842 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1843 p_input->p->input.i_seekpoint_offset;
1845 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1847 if( p_access->info.i_title > i_title_end ||
1848 ( p_access->info.i_title == i_title_end &&
1849 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1851 else if( i_seekpoint_end >=0 )
1853 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1855 else if( i_title_end >= 0 )
1857 if( p_access->info.i_title > i_title_end ) return 0;
1864 /*****************************************************************************
1866 *****************************************************************************/
1867 static int UpdateMeta( input_thread_t *p_input )
1869 vlc_meta_t *p_meta = p_input->p->input.p_item->p_meta;
1873 if( p_meta->psz_title && !p_input->p->input.p_item->b_fixed_name )
1874 input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1876 /** \todo handle sout meta */
1881 /*****************************************************************************
1883 *****************************************************************************/
1884 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
1886 vlc_mutex_lock( &p_input->p->input.p_item->lock );
1887 p_input->p->input.p_item->i_duration = i_length;
1888 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1890 if( !p_input->b_preparsing )
1892 pl_Yield( p_input );
1893 var_SetInteger( pl_Get( p_input ), "item-change",
1894 p_input->p->input.p_item->i_id );
1895 pl_Release( p_input )
1899 /*****************************************************************************
1901 *****************************************************************************/
1902 static input_source_t *InputSourceNew( input_thread_t *p_input )
1904 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1908 msg_Err( p_input, "out of memory for new input source" );
1913 in->p_access = NULL;
1914 in->p_stream = NULL;
1916 in->b_title_demux = VLC_FALSE;
1919 in->b_can_pace_control = VLC_TRUE;
1920 in->b_eof = VLC_FALSE;
1921 in->i_cr_average = 0;
1926 /*****************************************************************************
1928 *****************************************************************************/
1929 static int InputSourceInit( input_thread_t *p_input,
1930 input_source_t *in, const char *psz_mrl,
1931 const char *psz_forced_demux )
1933 char psz_dup[strlen (psz_mrl) + 1];
1934 const char *psz_access;
1935 const char *psz_demux;
1941 strcpy (psz_dup, psz_mrl);
1943 if( !in ) return VLC_EGENERIC;
1944 if( !p_input ) return VLC_EGENERIC;
1947 if( !p_input->b_preparsing )
1949 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1950 &psz_access, &psz_demux, &psz_path );
1952 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1953 psz_mrl, psz_access, psz_demux, psz_path );
1955 /* Hack to allow udp://@:port syntax */
1957 (strncmp( psz_access, "udp", 3 ) &&
1958 strncmp( psz_access, "rtp", 3 )) )
1960 /* Find optional titles and seekpoints */
1961 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1962 &in->i_seekpoint_start, &in->i_seekpoint_end );
1964 if( psz_forced_demux && *psz_forced_demux )
1966 psz_demux = psz_forced_demux;
1968 else if( !psz_demux || *psz_demux == '\0' )
1970 /* special hack for forcing a demuxer with --demux=module
1971 * (and do nothing with a list) */
1972 char *psz_var_demux = var_GetString( p_input, "demux" );
1974 if( *psz_var_demux != '\0' &&
1975 !strchr(psz_var_demux, ',' ) &&
1976 !strchr(psz_var_demux, ':' ) )
1978 psz_demux = psz_var_demux;
1980 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
1982 else if( psz_var_demux )
1984 free( psz_var_demux );
1988 /* Try access_demux if no demux given */
1989 if( *psz_demux == '\0' )
1991 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1992 NULL, p_input->p->p_es_out, VLC_FALSE );
1998 if( !strncmp( psz_path, "file://", 7 ) )
2000 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2002 psz_access = "file";
2007 int64_t i_pts_delay;
2009 /* Get infos from access_demux */
2010 demux2_Control( in->p_demux,
2011 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2012 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2014 in->b_title_demux = VLC_TRUE;
2015 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2016 &in->title, &in->i_title,
2017 &in->i_title_offset, &in->i_seekpoint_offset ) )
2022 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2023 &in->b_can_pace_control );
2024 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2028 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2034 int64_t i_pts_delay;
2036 input_ChangeState( p_input, OPENING_S );
2038 /* Now try a real access */
2039 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2040 p_input->b_preparsing );
2042 /* Access failed, URL encoded ? */
2043 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2045 decode_URI( psz_path );
2047 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2048 psz_access, psz_demux, psz_path );
2050 in->p_access = access2_New( p_input,
2051 psz_access, psz_demux, psz_path,
2052 p_input->b_preparsing );
2055 if( in->p_access == NULL )
2057 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2059 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2060 _("Your input can't be opened"),
2061 _("VLC is unable to open the MRL '%s'."
2062 " Check the log for details."), psz_mrl );
2067 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2068 while( psz && *psz )
2070 access_t *p_access = in->p_access;
2071 char *end = strchr( psz, ':' );
2076 in->p_access = access2_FilterNew( in->p_access, psz );
2077 if( in->p_access == NULL )
2079 in->p_access = p_access;
2080 msg_Warn( p_input, "failed to insert access filter %s",
2086 if( psz_tmp ) free( psz_tmp );
2088 /* Get infos from access */
2089 if( !p_input->b_preparsing )
2091 access2_Control( in->p_access,
2092 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2093 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2095 in->b_title_demux = VLC_FALSE;
2096 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2097 &in->title, &in->i_title,
2098 &in->i_title_offset, &in->i_seekpoint_offset ) )
2104 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2105 &in->b_can_pace_control );
2106 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2108 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2110 var_Set( p_input, "seekable", val );
2113 input_ChangeState( p_input, BUFFERING_S );
2115 /* Create the stream_t */
2116 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2117 if( in->p_stream == NULL )
2119 msg_Warn( p_input, "cannot create a stream_t from access" );
2123 /* Open a demuxer */
2124 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2126 psz_demux = in->p_access->psz_demux;
2128 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2129 in->p_stream, p_input->p->p_es_out,
2130 p_input->b_preparsing );
2131 if( in->p_demux == NULL )
2133 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2134 psz_access, psz_demux, psz_path );
2135 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2136 _("Can't recognize the input's format"),
2137 _("The format of '%s' can't be detected. "
2138 "Have a look the log for details."), psz_mrl );
2142 /* TODO get title from demux */
2143 if( !p_input->b_preparsing && in->i_title <= 0 )
2145 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2146 &in->title, &in->i_title,
2147 &in->i_title_offset, &in->i_seekpoint_offset ))
2154 in->b_title_demux = VLC_TRUE;
2159 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2160 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2165 input_ChangeState( p_input, ERROR_S );
2168 demux2_Delete( in->p_demux );
2171 stream_Delete( in->p_stream );
2174 access2_Delete( in->p_access );
2176 return VLC_EGENERIC;
2179 /*****************************************************************************
2181 *****************************************************************************/
2182 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2185 demux2_Delete( in->p_demux );
2188 stream_Delete( in->p_stream );
2191 access2_Delete( in->p_access );
2193 if( in->i_title > 0 )
2196 for( i = 0; i < in->i_title; i++ )
2198 vlc_input_title_Delete( in->title[i] );
2204 static void SlaveDemux( input_thread_t *p_input )
2209 if( demux2_Control( p_input->p->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->p->i_slave; i++ )
2217 input_source_t *in = p_input->p->slave[i];
2223 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2228 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2230 msg_Err( p_input, "slave[%d] doesn't like "
2231 "DEMUX_GET_TIME -> EOF", i );
2236 if( i_stime >= i_time )
2239 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2245 i_ret = in->p_demux->pf_demux( in->p_demux );
2250 msg_Dbg( p_input, "slave %d EOF", i );
2251 in->b_eof = VLC_TRUE;
2256 static void SlaveSeek( input_thread_t *p_input )
2261 if( !p_input ) return;
2263 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2265 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2269 for( i = 0; i < p_input->p->i_slave; i++ )
2271 input_source_t *in = p_input->p->slave[i];
2273 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2275 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2276 in->b_eof = VLC_TRUE;
2281 /*****************************************************************************
2283 *****************************************************************************/
2284 static void InputMetaUser( input_thread_t *p_input )
2286 vlc_meta_t *p_meta = p_input->p->input.p_item->p_meta;
2289 if( !p_meta ) return;
2291 /* Get meta information from user */
2292 #define GET_META( field, s ) \
2293 var_Get( p_input, (s), &val ); \
2294 if( *val.psz_string ) { \
2295 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2296 p_meta->psz_ ## field = strdup( val.psz_string ); \
2298 free( val.psz_string )
2300 GET_META( title, "meta-title" );
2301 GET_META( artist, "meta-artist" );
2302 GET_META( genre, "meta-genre" );
2303 GET_META( copyright, "meta-copyright" );
2304 GET_META( description, "meta-description" );
2305 GET_META( date, "meta-date" );
2306 GET_META( url, "meta-url" );
2310 /*****************************************************************************
2311 * MRLSplit: parse the access, demux and url part of the
2312 * Media Resource Locator.
2313 *****************************************************************************/
2314 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2315 const char **ppsz_access, const char **ppsz_demux,
2318 const char *psz_access = "";
2319 const char *psz_demux = "";
2321 char *psz, *psz_check;
2323 psz = strchr( psz_dup, ':' );
2325 /* '@' not allowed in access/demux part */
2326 psz_check = strchr( psz_dup, '@' );
2327 if( psz_check && psz_check < psz ) psz = 0;
2329 #if defined( WIN32 ) || defined( UNDER_CE )
2330 if( psz - psz_dup == 1 )
2332 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2341 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2345 psz = strchr( psz_dup, '/' );
2352 psz_access = psz_dup;
2359 *ppsz_access = psz_access;
2360 *ppsz_demux = psz_demux;
2361 *ppsz_path = psz_path;
2364 /*****************************************************************************
2365 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2368 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2369 *****************************************************************************/
2370 static void MRLSections( input_thread_t *p_input, char *psz_source,
2371 int *pi_title_start, int *pi_title_end,
2372 int *pi_chapter_start, int *pi_chapter_end )
2374 char *psz, *psz_end, *psz_next, *psz_check;
2376 *pi_title_start = *pi_title_end = -1;
2377 *pi_chapter_start = *pi_chapter_end = -1;
2379 /* Start by parsing titles and chapters */
2380 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2382 /* Check we are really dealing with a title/chapter section */
2383 psz_check = psz + 1;
2384 if( !*psz_check ) return;
2385 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2386 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2387 if( *psz_check == ':' && ++psz_check )
2388 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2389 if( *psz_check != '-' && *psz_check ) return;
2390 if( *psz_check == '-' && ++psz_check )
2391 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2392 if( *psz_check != ':' && *psz_check ) return;
2393 if( *psz_check == ':' && ++psz_check )
2394 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2395 if( *psz_check ) return;
2397 /* Separate start and end */
2399 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2401 /* Look for the start title */
2402 *pi_title_start = strtol( psz, &psz_next, 0 );
2403 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2404 *pi_title_end = *pi_title_start;
2407 /* Look for the start chapter */
2409 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2410 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2411 *pi_chapter_end = *pi_chapter_start;
2415 /* Look for the end title */
2416 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2417 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2420 /* Look for the end chapter */
2421 if( *psz_end ) psz_end++;
2422 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2423 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2426 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2427 psz_source, *pi_title_start, *pi_chapter_start,
2428 *pi_title_end, *pi_chapter_end );
2431 /*****************************************************************************
2432 * input_AddSubtitles: add a subtitles file and enable it
2433 *****************************************************************************/
2434 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2435 vlc_bool_t b_check_extension )
2437 input_source_t *sub;
2440 char *psz_path, *psz_extension;
2442 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2447 /* if we are provided a subtitle.sub file,
2448 * see if we don't have a subtitle.idx and use it instead */
2449 psz_path = strdup( psz_subtitle );
2452 psz_extension = strrchr( psz_path, '.');
2453 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2457 strcpy( psz_extension, ".idx" );
2458 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2459 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2462 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2463 psz_path, psz_subtitle );
2464 strcpy( psz_subtitle, psz_path );
2470 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2472 sub = InputSourceNew( p_input );
2473 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2475 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2478 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2480 if( count.i_int == 0 )
2482 /* if it was first one, there is disable too */
2484 if( count.i_int < list.p_list->i_count )
2486 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2487 &list.p_list->p_values[count.i_int] );
2489 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );