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 input_ControlVarInit( p_input );
201 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
203 if( !p_input->b_preparsing )
205 var_Get( p_input, "bookmarks", &val );
208 /* FIXME: have a common cfg parsing routine used by sout and others */
209 char *psz_parser, *psz_start, *psz_end;
210 psz_parser = val.psz_string;
211 while( (psz_start = strchr( psz_parser, '{' ) ) )
213 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
216 psz_end = strchr( psz_start, '}' );
217 if( !psz_end ) break;
218 psz_parser = psz_end + 1;
219 backup = *psz_parser;
222 while( (psz_end = strchr( psz_start, ',' ) ) )
225 if( !strncmp( psz_start, "name=", 5 ) )
227 p_seekpoint->psz_name = psz_start + 5;
229 else if( !strncmp( psz_start, "bytes=", 6 ) )
231 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
233 else if( !strncmp( psz_start, "time=", 5 ) )
235 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
238 psz_start = psz_end + 1;
240 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
241 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
242 p_seekpoint->i_time_offset );
243 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
244 vlc_seekpoint_Delete( p_seekpoint );
245 *psz_parser = backup;
247 free( val.psz_string );
251 /* Remove 'Now playing' info as it is probably outdated */
252 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT),
253 VLC_META_NOW_PLAYING ); /* ? Don't translate as it might has been copied ? */
259 * Initialize an input thread and run it. You will need to monitor the
260 * thread to clean up after it is done
262 * \param p_parent a vlc_object
263 * \param p_item an input item
264 * \return a pointer to the spawned input thread
266 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
267 input_item_t *p_item )
269 return __input_CreateThread2( p_parent, p_item, NULL );
273 input_thread_t *__input_CreateThread2( vlc_object_t *p_parent,
274 input_item_t *p_item,
275 const char *psz_header )
277 input_thread_t *p_input = NULL; /* thread descriptor */
279 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
283 /* Now we can attach our new input */
284 vlc_object_attach( p_input, p_parent );
286 /* Create thread and wait for its readiness. */
287 if( vlc_thread_create( p_input, "input", Run,
288 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
290 input_ChangeState( p_input, ERROR_S );
291 msg_Err( p_input, "cannot create input thread" );
292 vlc_object_detach( p_input );
294 vlc_object_destroy( p_input );
302 * Initialize an input thread and run it. This thread will clean after himself,
303 * you can forget about it. It can work either in blocking or non-blocking mode
305 * \param p_parent a vlc_object
306 * \param p_item an input item
307 * \param b_block should we block until read is finished ?
308 * \return the input object id if non blocking, an error code else
310 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
313 input_thread_t *p_input = NULL; /* thread descriptor */
315 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
319 /* Now we can attach our new input */
320 vlc_object_attach( p_input, p_parent );
324 RunAndClean( p_input );
329 if( vlc_thread_create( p_input, "input", RunAndClean,
330 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
332 input_ChangeState( p_input, ERROR_S );
333 msg_Err( p_input, "cannot create input thread" );
334 vlc_object_detach( p_input );
336 vlc_object_destroy( p_input );
340 return p_input->i_object_id;
344 * Initialize an input and initialize it to preparse the item
345 * This function is blocking. It will only accept to parse files
347 * \param p_parent a vlc_object_t
348 * \param p_item an input item
349 * \return VLC_SUCCESS or an error
351 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
353 input_thread_t *p_input = NULL; /* thread descriptor */
355 /* Allocate descriptor */
356 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
360 p_input->i_flags |= OBJECT_FLAGS_QUIET;
361 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
363 /* Now we can attach our new input */
364 vlc_object_attach( p_input, p_parent );
368 /* Clean up master */
369 InputSourceClean( p_input, &p_input->p->input );
371 /* Unload all modules */
372 if( p_input->p->p_es_out ) input_EsOutDelete( p_input->p->p_es_out );
374 vlc_object_detach( p_input );
376 vlc_object_destroy( p_input );
382 * Request a running input thread to stop and die
384 * \param the input thread to stop
386 void input_StopThread( input_thread_t *p_input )
391 /* Set die for input */
392 p_input->b_die = VLC_TRUE;
394 /* We cannot touch p_input fields directly (we come from another thread),
395 * so use the vlc_object_find way, it's perfectly safe */
397 /* Set die for all access */
398 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
399 for( i = 0; i < p_list->i_count; i++ )
401 p_list->p_values[i].p_object->b_die = VLC_TRUE;
403 vlc_list_release( p_list );
405 /* Set die for all stream */
406 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
407 for( i = 0; i < p_list->i_count; i++ )
409 p_list->p_values[i].p_object->b_die = VLC_TRUE;
411 vlc_list_release( p_list );
413 /* Set die for all demux */
414 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
415 for( i = 0; i < p_list->i_count; i++ )
417 p_list->p_values[i].p_object->b_die = VLC_TRUE;
419 vlc_list_release( p_list );
421 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
425 * Clean up a dead input thread
426 * This function does not return until the thread is effectively cancelled.
428 * \param the input thread to kill
430 void input_DestroyThread( input_thread_t *p_input )
432 /* Join the thread */
433 vlc_thread_join( p_input );
435 /* Delete input lock (only after thread joined) */
436 vlc_mutex_destroy( &p_input->p->lock_control );
438 /* TODO: maybe input_DestroyThread should also delete p_input instead
439 * of the playlist but I'm not sure if it's possible */
442 /*****************************************************************************
443 * Run: main thread loop
444 * This is the "normal" thread that spawns the input processing chain,
445 * reads the stream, cleans up and waits
446 *****************************************************************************/
447 static int Run( input_thread_t *p_input )
449 /* Signal that the thread is launched */
450 vlc_thread_ready( p_input );
452 if( Init( p_input ) )
454 /* If we failed, wait before we are killed, and exit */
455 p_input->b_error = VLC_TRUE;
456 playlist_Signal( pl_Get( p_input ) );
460 /* Tell we're dead */
461 p_input->b_dead = VLC_TRUE;
468 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
470 /* We have finish to demux data but not to play them */
471 while( !p_input->b_die )
473 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
476 msg_Dbg( p_input, "waiting decoder fifos to empty" );
478 msleep( INPUT_IDLE_SLEEP );
481 /* We have finished */
482 p_input->b_eof = VLC_TRUE;
483 playlist_Signal( pl_Get( p_input ) );
486 /* Wait until we are asked to die */
487 if( !p_input->b_die )
498 /*****************************************************************************
499 * RunAndClean: main thread loop
500 * This is the "just forget me" thread that spawns the input processing chain,
501 * reads the stream, cleans up and releases memory
502 *****************************************************************************/
503 static int RunAndClean( input_thread_t *p_input )
505 /* Signal that the thread is launched */
506 vlc_thread_ready( p_input );
508 if( Init( p_input ) )
510 /* If we failed, just exit */
516 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
518 /* We have finished demuxing data but not playing it */
519 while( !p_input->b_die )
521 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
524 msg_Dbg( p_input, "waiting decoder fifos to empty" );
526 msleep( INPUT_IDLE_SLEEP );
529 /* We have finished */
530 p_input->b_eof = VLC_TRUE;
537 vlc_object_detach( p_input );
539 vlc_object_destroy( p_input );
544 /*****************************************************************************
545 * Main loop: Fill buffers from access, and demux
546 *****************************************************************************/
547 static void MainLoop( input_thread_t *p_input )
549 int64_t i_intf_update = 0;
551 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
553 vlc_bool_t b_force_update = VLC_FALSE;
559 if( p_input->i_state != PAUSE_S )
561 if( p_input->p->i_stop <= 0 || p_input->i_time < p_input->p->i_stop )
562 i_ret=p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
569 if( p_input->p->input.b_title_demux &&
570 p_input->p->input.p_demux->info.i_update )
572 i_ret = UpdateFromDemux( p_input );
573 b_force_update = VLC_TRUE;
575 else if( !p_input->p->input.b_title_demux &&
576 p_input->p->input.p_access &&
577 p_input->p->input.p_access->info.i_update )
579 i_ret = UpdateFromAccess( p_input );
580 b_force_update = VLC_TRUE;
584 if( i_ret == 0 ) /* EOF */
588 var_Get( p_input, "input-repeat", &repeat );
589 if( repeat.i_int == 0 )
591 /* End of file - we do not set b_die because only the
592 * playlist is allowed to do so. */
593 input_ChangeState( p_input, END_S );
594 msg_Dbg( p_input, "EOF reached" );
595 p_input->p->input.b_eof = VLC_TRUE;
599 msg_Dbg( p_input, "repeating the same input (%d)",
601 if( repeat.i_int > 0 )
604 var_Set( p_input, "input-repeat", repeat );
607 /* Seek to start title/seekpoint */
608 val.i_int = p_input->p->input.i_title_start -
609 p_input->p->input.i_title_offset;
610 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
612 input_ControlPush( p_input,
613 INPUT_CONTROL_SET_TITLE, &val );
615 val.i_int = p_input->p->input.i_seekpoint_start -
616 p_input->p->input.i_seekpoint_offset;
617 if( val.i_int > 0 /* TODO: check upper boundary */ )
618 input_ControlPush( p_input,
619 INPUT_CONTROL_SET_SEEKPOINT, &val );
621 /* Seek to start position */
622 if( p_input->p->i_start > 0 )
624 val.i_time = p_input->p->i_start;
625 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
631 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
638 p_input->b_error = VLC_TRUE;
641 if( i_ret > 0 && p_input->p->i_slave > 0 )
643 SlaveDemux( p_input );
653 vlc_mutex_lock( &p_input->p->lock_control );
654 ControlReduce( p_input );
655 while( !ControlPopNoLock( p_input, &i_type, &val ) )
657 msg_Dbg( p_input, "control type=%d", i_type );
658 if( Control( p_input, i_type, val ) )
659 b_force_update = VLC_TRUE;
661 vlc_mutex_unlock( &p_input->p->lock_control );
663 if( b_force_update || i_intf_update < mdate() )
667 int64_t i_time, i_length;
668 /* update input status variables */
669 if( !demux2_Control( p_input->p->input.p_demux,
670 DEMUX_GET_POSITION, &f_pos ) )
672 val.f_float = (float)f_pos;
673 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
675 if( !demux2_Control( p_input->p->input.p_demux,
676 DEMUX_GET_TIME, &i_time ) )
678 p_input->i_time = i_time;
680 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
682 if( !demux2_Control( p_input->p->input.p_demux,
683 DEMUX_GET_LENGTH, &i_length ) )
686 var_Get( p_input, "length", &old_val );
687 val.i_time = i_length;
688 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
690 if( old_val.i_time != val.i_time )
692 UpdateItemLength( p_input, i_length );
696 var_SetBool( p_input, "intf-change", VLC_TRUE );
697 i_intf_update = mdate() + I64C(150000);
699 /* 150ms * 8 = ~ 1 second */
700 if( ++i_updates % 8 == 0 )
702 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
703 /* Are we the thread responsible for computing global stats ? */
704 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
706 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
707 p_input->p_libvlc->p_playlist->p_stats );
713 static int Init( input_thread_t * p_input )
723 /* Initialize optional stream output. (before access/demuxer)
724 * XXX: we add a special case if the uri starts by vlc.
725 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
726 * be destroyed in case of a file).
727 * (this will break playing of file starting by 'vlc:' but I don't
728 * want to add more logic, just force file by file:// or code it ;)
730 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
731 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
733 if( !p_input->b_preparsing )
735 /* Prepare statistics */
736 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
737 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
738 if( p_input->p_libvlc->b_stats )
740 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
741 INIT_COUNTER( read_packets, INTEGER, COUNTER );
742 INIT_COUNTER( demux_read, INTEGER, COUNTER );
743 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
744 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
745 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
746 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
747 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
748 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
749 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
750 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
751 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
752 p_input->p->counters.p_sout_send_bitrate = NULL;
753 p_input->p->counters.p_sout_sent_packets = NULL;
754 p_input->p->counters.p_sout_sent_bytes = NULL;
755 if( p_input->p->counters.p_demux_bitrate )
756 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
757 if( p_input->p->counters.p_input_bitrate )
758 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
762 psz = var_GetString( p_input, "sout" );
763 if( *psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
765 p_input->p->p_sout = sout_NewInstance( p_input, psz );
766 if( p_input->p->p_sout == NULL )
768 input_ChangeState( p_input, ERROR_S );
769 msg_Err( p_input, "cannot start stream output instance, " \
774 if( p_input->p_libvlc->b_stats )
776 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
777 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
778 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
779 if( p_input->p->counters.p_sout_send_bitrate )
780 p_input->p->counters.p_sout_send_bitrate->update_interval =
788 p_input->p->p_es_out = input_EsOutNew( p_input );
789 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
790 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
792 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
793 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
795 if( InputSourceInit( p_input, &p_input->p->input,
796 p_input->p->input.p_item->psz_uri, NULL ) )
801 /* Create global title (from master) */
802 if( !p_input->b_preparsing )
804 p_input->p->i_title = p_input->p->input.i_title;
805 p_input->p->title = p_input->p->input.title;
806 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
807 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
808 if( p_input->p->i_title > 0 )
810 /* Setup variables */
811 input_ControlVarNavigation( p_input );
812 input_ControlVarTitle( p_input, 0 );
816 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
817 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
820 if( p_input->i_pts_delay < 0 )
821 p_input->i_pts_delay = 0;
823 /* If the desynchronisation requested by the user is < 0, we need to
824 * cache more data. */
825 var_Get( p_input, "audio-desync", &val );
826 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
828 /* Update cr_average depending on the caching */
829 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
830 p_input->p->input.i_cr_average /= 10;
831 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
834 /* Load master infos */
836 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
837 &val.i_time ) && val.i_time > 0 )
839 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
840 UpdateItemLength( p_input, val.i_time );
841 p_input->p->input.p_item->i_duration = val.i_time;
844 /* Start title/chapter */
845 if( !p_input->b_preparsing )
847 val.i_int = p_input->p->input.i_title_start -
848 p_input->p->input.i_title_offset;
849 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
850 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
851 val.i_int = p_input->p->input.i_seekpoint_start -
852 p_input->p->input.i_seekpoint_offset;
853 if( val.i_int > 0 /* TODO: check upper boundary */ )
854 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
858 p_input->p->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
860 p_input->p->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
863 if( p_input->p->i_start > 0 )
865 if( p_input->p->i_start >= val.i_time )
867 msg_Warn( p_input, "invalid start-time ignored" );
873 msg_Dbg( p_input, "starting at time: %ds",
874 (int)( p_input->p->i_start / I64C(1000000) ) );
876 s.i_time = p_input->p->i_start;
877 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
880 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
882 msg_Warn( p_input, "invalid stop-time ignored" );
883 p_input->p->i_stop = 0;
887 /* Get fps and set it if not already set */
888 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
891 float f_requested_fps;
893 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
894 var_SetFloat( p_input, "sub-original-fps", f_fps );
896 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
897 if( f_requested_fps != f_fps )
899 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
901 var_SetFloat( p_input, "sub-fps", f_requested_fps );
905 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
908 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
911 /* Look for and add subtitle files */
912 psz_subtitle = var_GetString( p_input, "sub-file" );
915 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
916 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
919 var_Get( p_input, "sub-autodetect-file", &val );
922 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
923 char **subs = subtitles_Detect( p_input, psz_autopath,
924 p_input->p->input.p_item->psz_uri );
928 /* Try to autoselect the first autodetected subtitles file
929 * if no subtitles file was specified */
930 if( *psz_subtitle == 0 && subs && subs[0] )
932 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
937 /* Then, just add the following subtitles files */
938 for( ; subs && subs[i]; i++ )
940 if( strcmp( psz_subtitle, subs[i] ) )
942 sub = InputSourceNew( p_input );
943 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
945 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
951 if( subs ) free( subs );
952 if( psz_autopath ) free( psz_autopath );
954 free( psz_subtitle );
957 psz = var_GetString( p_input, "input-slave" );
961 input_source_t *slave;
964 while( *psz == ' ' || *psz == '#' )
968 if( ( psz_delim = strchr( psz, '#' ) ) )
977 msg_Dbg( p_input, "adding slave input '%s'", psz );
978 slave = InputSourceNew( p_input );
979 if( !InputSourceInit( p_input, slave, psz, NULL ) )
981 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
987 if( psz ) free( psz );
991 p_input->p->i_start = 0;
992 p_input->p->i_start = 0;
996 if( !p_input->b_preparsing )
998 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
999 i_es_out_mode = ES_OUT_MODE_AUTO;
1001 if( p_input->p->p_sout )
1003 var_Get( p_input, "sout-all", &val );
1006 i_es_out_mode = ES_OUT_MODE_ALL;
1011 var_Get( p_input, "programs", &val );
1012 if ( val.p_list && val.p_list->i_count )
1014 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1015 /* Note : we should remove the "program" callback. */
1018 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1022 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1024 /* Inform the demuxer about waited group (needed only for DVB) */
1025 if( i_es_out_mode == ES_OUT_MODE_ALL )
1027 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1029 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1031 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1036 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1037 (int) var_GetInteger( p_input, "program" ), NULL );
1040 if( p_input->p->p_sout )
1042 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1044 p_input->p->b_out_pace_control = VLC_FALSE;
1048 p_input->p->b_out_pace_control = VLC_TRUE;
1051 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1053 /* We don't want a high input priority here or we'll
1054 * end-up sucking up all the CPU time */
1055 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1058 msg_Dbg( p_input, "starting in %s mode",
1059 p_input->p->b_out_pace_control ? "async" : "sync" );
1063 p_meta = p_input->p->input.p_item->p_meta;
1064 /* Get meta data from users */
1065 InputMetaUser( p_input );
1066 /* Get meta data from master input */
1067 demux2_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
1069 /* Access_file does not give any meta, and there are no slave */
1070 if( !p_input->b_preparsing )
1072 if( p_input->p->input.p_access )
1073 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1076 /* Get meta data from slave input */
1077 for( i = 0; i < p_input->p->i_slave; i++ )
1079 demux2_Control( p_input->p->slave[i]->p_demux,
1080 DEMUX_GET_META, p_meta );
1081 if( p_input->p->slave[i]->p_access )
1083 access2_Control( p_input->p->slave[i]->p_access,
1084 ACCESS_GET_META, p_meta );
1089 UpdateMeta( p_input );
1091 if( !p_input->b_preparsing )
1093 msg_Dbg( p_input, "`%s' successfully opened",
1094 p_input->p->input.p_item->psz_uri );
1098 /* initialization is complete */
1099 input_ChangeState( p_input, PLAYING_S );
1104 input_ChangeState( p_input, ERROR_S );
1106 if( p_input->p->p_es_out )
1107 input_EsOutDelete( p_input->p->p_es_out );
1109 if( p_input->p->p_sout )
1110 sout_DeleteInstance( p_input->p->p_sout );
1112 /* Mark them deleted */
1113 p_input->p->input.p_demux = NULL;
1114 p_input->p->input.p_stream = NULL;
1115 p_input->p->input.p_access = NULL;
1116 p_input->p->p_es_out = NULL;
1117 p_input->p->p_sout = NULL;
1119 return VLC_EGENERIC;
1122 /*****************************************************************************
1123 * Error: RunThread() error loop
1124 *****************************************************************************
1125 * This function is called when an error occurred during thread main's loop.
1126 *****************************************************************************/
1127 static void Error( input_thread_t *p_input )
1129 while( !p_input->b_die )
1132 input_ChangeState( p_input, ERROR_S );
1133 msleep( INPUT_IDLE_SLEEP );
1137 /*****************************************************************************
1138 * End: end the input thread
1139 *****************************************************************************/
1140 static void End( input_thread_t * p_input )
1144 msg_Dbg( p_input, "closing input" );
1146 /* We are at the end */
1147 input_ChangeState( p_input, END_S );
1149 /* Clean control variables */
1150 input_ControlVarClean( p_input );
1152 /* Clean up master */
1153 InputSourceClean( p_input, &p_input->p->input );
1156 for( i = 0; i < p_input->p->i_slave; i++ )
1158 InputSourceClean( p_input, p_input->p->slave[i] );
1159 free( p_input->p->slave[i] );
1161 if( p_input->p->slave ) free( p_input->p->slave );
1163 /* Unload all modules */
1164 if( p_input->p->p_es_out )
1165 input_EsOutDelete( p_input->p->p_es_out );
1167 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1168 if( p_input->p_libvlc->b_stats )
1170 /* make sure we are up to date */
1171 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1172 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1174 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1175 p_input->p_libvlc->p_playlist->p_stats );
1176 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1178 CL_CO( read_bytes );
1179 CL_CO( read_packets );
1180 CL_CO( demux_read );
1181 CL_CO( input_bitrate );
1182 CL_CO( demux_bitrate );
1183 CL_CO( played_abuffers );
1184 CL_CO( lost_abuffers );
1185 CL_CO( displayed_pictures );
1186 CL_CO( lost_pictures );
1187 CL_CO( decoded_audio) ;
1188 CL_CO( decoded_video );
1189 CL_CO( decoded_sub) ;
1192 /* Close optional stream output instance */
1193 if( p_input->p->p_sout )
1197 CL_CO( sout_sent_packets );
1198 CL_CO( sout_sent_bytes );
1199 CL_CO( sout_send_bitrate );
1201 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool )
1203 /* attach sout to the playlist */
1204 msg_Dbg( p_input, "keeping sout" );
1205 vlc_object_detach( p_input->p->p_sout );
1206 vlc_object_attach( p_input->p->p_sout, p_input->p_libvlc->p_playlist );
1210 msg_Dbg( p_input, "destroying sout" );
1211 sout_DeleteInstance( p_input->p->p_sout );
1216 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1218 /* Tell we're dead */
1219 p_input->b_dead = VLC_TRUE;
1222 /*****************************************************************************
1224 *****************************************************************************/
1225 static inline int ControlPopNoLock( input_thread_t *p_input,
1226 int *pi_type, vlc_value_t *p_val )
1228 if( p_input->p->i_control <= 0 )
1230 return VLC_EGENERIC;
1233 *pi_type = p_input->p->control[0].i_type;
1234 *p_val = p_input->p->control[0].val;
1236 p_input->p->i_control--;
1237 if( p_input->p->i_control > 0 )
1241 for( i = 0; i < p_input->p->i_control; i++ )
1243 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1244 p_input->p->control[i].val = p_input->p->control[i+1].val;
1251 static void ControlReduce( input_thread_t *p_input )
1258 for( i = 1; i < p_input->p->i_control; i++ )
1260 const int i_lt = p_input->p->control[i-1].i_type;
1261 const int i_ct = p_input->p->control[i].i_type;
1263 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1264 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1268 ( i_ct == INPUT_CONTROL_SET_STATE ||
1269 i_ct == INPUT_CONTROL_SET_RATE ||
1270 i_ct == INPUT_CONTROL_SET_POSITION ||
1271 i_ct == INPUT_CONTROL_SET_TIME ||
1272 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1273 i_ct == INPUT_CONTROL_SET_TITLE ||
1274 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1275 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1278 // msg_Dbg( p_input, "merged at %d", i );
1279 /* Remove the i-1 */
1280 for( j = i; j < p_input->p->i_control; j++ )
1281 p_input->p->control[j-1] = p_input->p->control[j];
1282 p_input->p->i_control--;
1286 /* TODO but that's not that important
1287 - merge SET_X with SET_X_CMD
1288 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1289 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1296 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1299 vlc_bool_t b_force_update = VLC_FALSE;
1301 if( !p_input ) return b_force_update;
1305 case INPUT_CONTROL_SET_DIE:
1306 msg_Dbg( p_input, "control: stopping input" );
1307 /* Mark all submodules to die */
1308 if( p_input->p->input.p_access )
1309 p_input->p->input.p_access->b_die = VLC_TRUE;
1310 if( p_input->p->input.p_stream )
1311 p_input->p->input.p_stream->b_die = VLC_TRUE;
1312 p_input->p->input.p_demux->b_die = VLC_TRUE;
1314 p_input->b_die = VLC_TRUE;
1317 case INPUT_CONTROL_SET_POSITION:
1318 case INPUT_CONTROL_SET_POSITION_OFFSET:
1321 if( i_type == INPUT_CONTROL_SET_POSITION )
1323 f_pos = val.f_float;
1327 /* Should not fail */
1328 demux2_Control( p_input->p->input.p_demux,
1329 DEMUX_GET_POSITION, &f_pos );
1330 f_pos += val.f_float;
1332 if( f_pos < 0.0 ) f_pos = 0.0;
1333 if( f_pos > 1.0 ) f_pos = 1.0;
1334 /* Reset the decoders states and clock sync (before calling the demuxer */
1335 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1336 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1337 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1340 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1341 "%2.1f%% failed", f_pos * 100 );
1345 if( p_input->p->i_slave > 0 )
1346 SlaveSeek( p_input );
1348 b_force_update = VLC_TRUE;
1353 case INPUT_CONTROL_SET_TIME:
1354 case INPUT_CONTROL_SET_TIME_OFFSET:
1359 if( i_type == INPUT_CONTROL_SET_TIME )
1361 i_time = val.i_time;
1365 /* Should not fail */
1366 demux2_Control( p_input->p->input.p_demux,
1367 DEMUX_GET_TIME, &i_time );
1368 i_time += val.i_time;
1370 if( i_time < 0 ) i_time = 0;
1372 /* Reset the decoders states and clock sync (before calling the demuxer */
1373 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1374 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1376 i_ret = demux2_Control( p_input->p->input.p_demux,
1377 DEMUX_SET_TIME, i_time );
1382 /* Emulate it with a SET_POS */
1383 demux2_Control( p_input->p->input.p_demux,
1384 DEMUX_GET_LENGTH, &i_length );
1387 double f_pos = (double)i_time / (double)i_length;
1388 i_ret = demux2_Control( p_input->p->input.p_demux,
1389 DEMUX_SET_POSITION, f_pos );
1394 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1395 " failed or not possible", i_time );
1399 if( p_input->p->i_slave > 0 )
1400 SlaveSeek( p_input );
1402 b_force_update = VLC_TRUE;
1407 case INPUT_CONTROL_SET_STATE:
1408 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1409 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1412 if( p_input->p->input.p_access )
1413 i_ret = access2_Control( p_input->p->input.p_access,
1414 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1416 i_ret = demux2_Control( p_input->p->input.p_demux,
1417 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1421 /* FIXME What to do ? */
1422 msg_Warn( p_input, "cannot unset pause -> EOF" );
1423 vlc_mutex_unlock( &p_input->p->lock_control );
1424 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1425 vlc_mutex_lock( &p_input->p->lock_control );
1428 b_force_update = VLC_TRUE;
1430 /* Switch to play */
1431 p_input->i_state = PLAYING_S;
1432 val.i_int = PLAYING_S;
1433 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1436 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1437 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1439 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1440 p_input->p->b_can_pause )
1443 if( p_input->p->input.p_access )
1444 i_ret = access2_Control( p_input->p->input.p_access,
1445 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1447 i_ret = demux2_Control( p_input->p->input.p_demux,
1448 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1450 b_force_update = VLC_TRUE;
1454 msg_Warn( p_input, "cannot set pause state" );
1455 val.i_int = p_input->i_state;
1459 val.i_int = PAUSE_S;
1462 /* Switch to new state */
1463 p_input->i_state = val.i_int;
1464 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1466 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1468 b_force_update = VLC_TRUE;
1470 /* Correct "state" value */
1471 val.i_int = p_input->i_state;
1472 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1474 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1476 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1480 case INPUT_CONTROL_SET_RATE:
1481 case INPUT_CONTROL_SET_RATE_SLOWER:
1482 case INPUT_CONTROL_SET_RATE_FASTER:
1486 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1487 i_rate = p_input->p->i_rate * 2;
1488 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1489 i_rate = p_input->p->i_rate / 2;
1493 if( i_rate < INPUT_RATE_MIN )
1495 msg_Dbg( p_input, "cannot set rate faster" );
1496 i_rate = INPUT_RATE_MIN;
1498 else if( i_rate > INPUT_RATE_MAX )
1500 msg_Dbg( p_input, "cannot set rate slower" );
1501 i_rate = INPUT_RATE_MAX;
1503 if( i_rate != INPUT_RATE_DEFAULT &&
1504 ( !p_input->b_can_pace_control ||
1505 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1507 msg_Dbg( p_input, "cannot change rate" );
1508 i_rate = INPUT_RATE_DEFAULT;
1510 if( i_rate != p_input->p->i_rate )
1512 p_input->p->i_rate = i_rate;
1514 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1516 /* We haven't send data to decoder when rate != default */
1517 if( i_rate == INPUT_RATE_DEFAULT )
1518 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE );
1521 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1523 b_force_update = VLC_TRUE;
1528 case INPUT_CONTROL_SET_PROGRAM:
1529 /* No need to force update, es_out does it if needed */
1530 es_out_Control( p_input->p->p_es_out,
1531 ES_OUT_SET_GROUP, val.i_int );
1533 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1537 case INPUT_CONTROL_SET_ES:
1538 /* No need to force update, es_out does it if needed */
1539 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1540 input_EsOutGetFromID( p_input->p->p_es_out,
1544 case INPUT_CONTROL_SET_AUDIO_DELAY:
1545 input_EsOutSetDelay( p_input->p->p_es_out,
1546 AUDIO_ES, val.i_time );
1547 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1550 case INPUT_CONTROL_SET_SPU_DELAY:
1551 input_EsOutSetDelay( p_input->p->p_es_out,
1552 SPU_ES, val.i_time );
1553 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1556 case INPUT_CONTROL_SET_TITLE:
1557 case INPUT_CONTROL_SET_TITLE_NEXT:
1558 case INPUT_CONTROL_SET_TITLE_PREV:
1559 if( p_input->p->input.b_title_demux &&
1560 p_input->p->input.i_title > 0 )
1563 /* FIXME handle demux title */
1564 demux_t *p_demux = p_input->p->input.p_demux;
1567 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1568 i_title = p_demux->info.i_title - 1;
1569 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1570 i_title = p_demux->info.i_title + 1;
1572 i_title = val.i_int;
1574 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1576 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1577 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1579 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1580 input_ControlVarTitle( p_input, i_title );
1583 else if( p_input->p->input.i_title > 0 )
1585 access_t *p_access = p_input->p->input.p_access;
1588 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1589 i_title = p_access->info.i_title - 1;
1590 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1591 i_title = p_access->info.i_title + 1;
1593 i_title = val.i_int;
1595 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1597 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1598 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1600 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1601 stream_AccessReset( p_input->p->input.p_stream );
1605 case INPUT_CONTROL_SET_SEEKPOINT:
1606 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1607 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1608 if( p_input->p->input.b_title_demux &&
1609 p_input->p->input.i_title > 0 )
1611 demux_t *p_demux = p_input->p->input.p_demux;
1613 int64_t i_input_time;
1614 int64_t i_seekpoint_time;
1616 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1618 i_seekpoint = p_demux->info.i_seekpoint;
1619 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1620 if( i_seekpoint_time >= 0 &&
1621 !demux2_Control( p_demux,
1622 DEMUX_GET_TIME, &i_input_time ) )
1624 if ( i_input_time < i_seekpoint_time + 3000000 )
1630 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1631 i_seekpoint = p_demux->info.i_seekpoint + 1;
1633 i_seekpoint = val.i_int;
1635 if( i_seekpoint >= 0 && i_seekpoint <
1636 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1638 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1639 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1641 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1644 else if( p_input->p->input.i_title > 0 )
1646 demux_t *p_demux = p_input->p->input.p_demux;
1647 access_t *p_access = p_input->p->input.p_access;
1649 int64_t i_input_time;
1650 int64_t i_seekpoint_time;
1652 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1654 i_seekpoint = p_access->info.i_seekpoint;
1655 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1656 if( i_seekpoint_time >= 0 &&
1657 demux2_Control( p_demux,
1658 DEMUX_GET_TIME, &i_input_time ) )
1660 if ( i_input_time < i_seekpoint_time + 3000000 )
1666 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1667 i_seekpoint = p_access->info.i_seekpoint + 1;
1669 i_seekpoint = val.i_int;
1671 if( i_seekpoint >= 0 && i_seekpoint <
1672 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1674 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE );
1675 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1677 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1679 stream_AccessReset( p_input->p->input.p_stream );
1684 case INPUT_CONTROL_ADD_SLAVE:
1685 if( val.psz_string )
1687 input_source_t *slave = InputSourceNew( p_input );
1689 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1691 vlc_meta_t *p_meta = p_input->p->input.p_item->p_meta;
1695 msg_Dbg( p_input, "adding %s as slave on the fly",
1699 if( demux2_Control( p_input->p->input.p_demux,
1700 DEMUX_GET_TIME, &i_time ) )
1702 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1703 InputSourceClean( p_input, slave );
1707 if( demux2_Control( slave->p_demux,
1708 DEMUX_SET_TIME, i_time ) )
1710 msg_Err( p_input, "seek failed for new slave" );
1711 InputSourceClean( p_input, slave );
1716 /* Get meta (access and demux) */
1717 access2_Control( slave->p_access, ACCESS_GET_META,
1719 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1720 UpdateMeta( p_input );
1722 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1727 msg_Warn( p_input, "failed to add %s as slave",
1731 free( val.psz_string );
1735 case INPUT_CONTROL_SET_BOOKMARK:
1737 msg_Err( p_input, "not yet implemented" );
1741 return b_force_update;
1744 /*****************************************************************************
1746 *****************************************************************************/
1747 static int UpdateFromDemux( input_thread_t *p_input )
1749 demux_t *p_demux = p_input->p->input.p_demux;
1752 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1754 v.i_int = p_demux->info.i_title;
1755 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1757 input_ControlVarTitle( p_input, p_demux->info.i_title );
1759 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1761 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1763 v.i_int = p_demux->info.i_seekpoint;
1764 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1766 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1768 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1770 /* Hmmm only works with master input */
1771 if( p_input->p->input.p_demux == p_demux )
1773 int i_title_end = p_input->p->input.i_title_end -
1774 p_input->p->input.i_title_offset;
1775 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1776 p_input->p->input.i_seekpoint_offset;
1778 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1780 if( p_demux->info.i_title > i_title_end ||
1781 ( p_demux->info.i_title == i_title_end &&
1782 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1784 else if( i_seekpoint_end >=0 )
1786 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1788 else if( i_title_end >= 0 )
1790 if( p_demux->info.i_title > i_title_end ) return 0;
1797 /*****************************************************************************
1799 *****************************************************************************/
1800 static int UpdateFromAccess( input_thread_t *p_input )
1802 access_t *p_access = p_input->p->input.p_access;
1805 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1807 v.i_int = p_access->info.i_title;
1808 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1810 input_ControlVarTitle( p_input, p_access->info.i_title );
1812 stream_AccessUpdate( p_input->p->input.p_stream );
1814 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1816 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1818 v.i_int = p_access->info.i_seekpoint;
1819 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1820 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1822 if( p_access->info.i_update & INPUT_UPDATE_META )
1824 /* TODO maybe multi - access ? */
1825 vlc_meta_t *p_meta = p_input->p->input.p_item->p_meta;
1826 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
1827 UpdateMeta( p_input );
1828 var_SetBool( p_input, "item-change", p_input->p->input.p_item->i_id );
1829 p_access->info.i_update &= ~INPUT_UPDATE_META;
1832 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1834 /* Hmmm only works with master input */
1835 if( p_input->p->input.p_access == p_access )
1837 int i_title_end = p_input->p->input.i_title_end -
1838 p_input->p->input.i_title_offset;
1839 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1840 p_input->p->input.i_seekpoint_offset;
1842 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1844 if( p_access->info.i_title > i_title_end ||
1845 ( p_access->info.i_title == i_title_end &&
1846 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1848 else if( i_seekpoint_end >=0 )
1850 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1852 else if( i_title_end >= 0 )
1854 if( p_access->info.i_title > i_title_end ) return 0;
1861 /*****************************************************************************
1863 *****************************************************************************/
1864 static int UpdateMeta( input_thread_t *p_input )
1866 vlc_meta_t *p_meta = p_input->p->input.p_item->p_meta;
1870 if( p_meta->psz_title && !p_input->p->input.p_item->b_fixed_name )
1871 input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1873 /** \todo handle sout meta */
1878 /*****************************************************************************
1880 *****************************************************************************/
1881 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
1883 vlc_mutex_lock( &p_input->p->input.p_item->lock );
1884 p_input->p->input.p_item->i_duration = i_length;
1885 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
1887 if( !p_input->b_preparsing )
1889 pl_Yield( p_input );
1890 var_SetInteger( pl_Get( p_input ), "item-change",
1891 p_input->p->input.p_item->i_id );
1892 pl_Release( p_input )
1896 /*****************************************************************************
1898 *****************************************************************************/
1899 static input_source_t *InputSourceNew( input_thread_t *p_input )
1901 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1905 msg_Err( p_input, "out of memory for new input source" );
1910 in->p_access = NULL;
1911 in->p_stream = NULL;
1913 in->b_title_demux = VLC_FALSE;
1916 in->b_can_pace_control = VLC_TRUE;
1917 in->b_eof = VLC_FALSE;
1918 in->i_cr_average = 0;
1923 /*****************************************************************************
1925 *****************************************************************************/
1926 static int InputSourceInit( input_thread_t *p_input,
1927 input_source_t *in, const char *psz_mrl,
1928 const char *psz_forced_demux )
1930 char psz_dup[strlen (psz_mrl) + 1];
1931 const char *psz_access;
1932 const char *psz_demux;
1938 strcpy (psz_dup, psz_mrl);
1940 if( !in ) return VLC_EGENERIC;
1941 if( !p_input ) return VLC_EGENERIC;
1944 if( !p_input->b_preparsing )
1946 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1947 &psz_access, &psz_demux, &psz_path );
1949 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1950 psz_mrl, psz_access, psz_demux, psz_path );
1952 /* Hack to allow udp://@:port syntax */
1954 (strncmp( psz_access, "udp", 3 ) &&
1955 strncmp( psz_access, "rtp", 3 )) )
1957 /* Find optional titles and seekpoints */
1958 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1959 &in->i_seekpoint_start, &in->i_seekpoint_end );
1961 if( psz_forced_demux && *psz_forced_demux )
1963 psz_demux = psz_forced_demux;
1965 else if( !psz_demux || *psz_demux == '\0' )
1967 /* special hack for forcing a demuxer with --demux=module
1968 * (and do nothing with a list) */
1969 char *psz_var_demux = var_GetString( p_input, "demux" );
1971 if( *psz_var_demux != '\0' &&
1972 !strchr(psz_var_demux, ',' ) &&
1973 !strchr(psz_var_demux, ':' ) )
1975 psz_demux = psz_var_demux;
1977 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
1979 else if( psz_var_demux )
1981 free( psz_var_demux );
1985 /* Try access_demux if no demux given */
1986 if( *psz_demux == '\0' )
1988 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1989 NULL, p_input->p->p_es_out, VLC_FALSE );
1995 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
1997 psz_access = "file";
2002 int64_t i_pts_delay;
2004 /* Get infos from access_demux */
2005 demux2_Control( in->p_demux,
2006 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2007 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2009 in->b_title_demux = VLC_TRUE;
2010 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2011 &in->title, &in->i_title,
2012 &in->i_title_offset, &in->i_seekpoint_offset ) )
2017 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2018 &in->b_can_pace_control );
2019 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2023 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2029 int64_t i_pts_delay;
2031 input_ChangeState( p_input, OPENING_S );
2033 /* Now try a real access */
2034 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2035 p_input->b_preparsing );
2037 /* Access failed, URL encoded ? */
2038 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2040 decode_URI( psz_path );
2042 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2043 psz_access, psz_demux, psz_path );
2045 in->p_access = access2_New( p_input,
2046 psz_access, psz_demux, psz_path,
2047 p_input->b_preparsing );
2049 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2050 * are forbidden in filenames on Win32. */
2052 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2053 if( in->p_access == NULL &&
2054 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2056 strcpy (psz_dup, psz_mrl);
2058 if( psz_forced_demux && *psz_forced_demux )
2060 psz_demux = psz_forced_demux;
2062 else psz_demux = "";
2065 in->p_access = access2_New( p_input,
2066 psz_access, psz_demux, psz_path,
2067 p_input->b_preparsing );
2071 if( in->p_access == NULL )
2073 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2074 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2075 _("Your input can't be opened"),
2076 _("VLC is unable to open the MRL '%s'."
2077 " Check the log for details."), psz_mrl );
2082 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2083 while( psz && *psz )
2085 access_t *p_access = in->p_access;
2086 char *end = strchr( psz, ':' );
2091 in->p_access = access2_FilterNew( in->p_access, psz );
2092 if( in->p_access == NULL )
2094 in->p_access = p_access;
2095 msg_Warn( p_input, "failed to insert access filter %s",
2101 if( psz_tmp ) free( psz_tmp );
2103 /* Get infos from access */
2104 if( !p_input->b_preparsing )
2106 access2_Control( in->p_access,
2107 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2108 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2110 in->b_title_demux = VLC_FALSE;
2111 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2112 &in->title, &in->i_title,
2113 &in->i_title_offset, &in->i_seekpoint_offset ) )
2119 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2120 &in->b_can_pace_control );
2121 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2123 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2125 var_Set( p_input, "seekable", val );
2128 input_ChangeState( p_input, BUFFERING_S );
2130 /* Create the stream_t */
2131 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2132 if( in->p_stream == NULL )
2134 msg_Warn( p_input, "cannot create a stream_t from access" );
2138 /* Open a demuxer */
2139 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2141 psz_demux = in->p_access->psz_demux;
2143 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2144 in->p_stream, p_input->p->p_es_out,
2145 p_input->b_preparsing );
2146 if( in->p_demux == NULL )
2148 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2149 psz_access, psz_demux, psz_path );
2150 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2151 _("Can't recognize the input's format"),
2152 _("The format of '%s' can't be detected. "
2153 "Have a look the log for details."), psz_mrl );
2157 /* TODO get title from demux */
2158 if( !p_input->b_preparsing && in->i_title <= 0 )
2160 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2161 &in->title, &in->i_title,
2162 &in->i_title_offset, &in->i_seekpoint_offset ))
2169 in->b_title_demux = VLC_TRUE;
2174 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2175 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2180 input_ChangeState( p_input, ERROR_S );
2183 demux2_Delete( in->p_demux );
2186 stream_Delete( in->p_stream );
2189 access2_Delete( in->p_access );
2191 return VLC_EGENERIC;
2194 /*****************************************************************************
2196 *****************************************************************************/
2197 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2200 demux2_Delete( in->p_demux );
2203 stream_Delete( in->p_stream );
2206 access2_Delete( in->p_access );
2208 if( in->i_title > 0 )
2211 for( i = 0; i < in->i_title; i++ )
2213 vlc_input_title_Delete( in->title[i] );
2219 static void SlaveDemux( input_thread_t *p_input )
2224 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2226 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2230 for( i = 0; i < p_input->p->i_slave; i++ )
2232 input_source_t *in = p_input->p->slave[i];
2238 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2243 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2245 msg_Err( p_input, "slave[%d] doesn't like "
2246 "DEMUX_GET_TIME -> EOF", i );
2251 if( i_stime >= i_time )
2254 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2260 i_ret = in->p_demux->pf_demux( in->p_demux );
2265 msg_Dbg( p_input, "slave %d EOF", i );
2266 in->b_eof = VLC_TRUE;
2271 static void SlaveSeek( input_thread_t *p_input )
2276 if( !p_input ) return;
2278 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2280 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2284 for( i = 0; i < p_input->p->i_slave; i++ )
2286 input_source_t *in = p_input->p->slave[i];
2288 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2290 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2291 in->b_eof = VLC_TRUE;
2296 /*****************************************************************************
2298 *****************************************************************************/
2299 static void InputMetaUser( input_thread_t *p_input )
2301 vlc_meta_t *p_meta = p_input->p->input.p_item->p_meta;
2304 if( !p_meta ) return;
2306 /* Get meta information from user */
2307 #define GET_META( field, s ) \
2308 var_Get( p_input, (s), &val ); \
2309 if( *val.psz_string ) { \
2310 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2311 p_meta->psz_ ## field = strdup( val.psz_string ); \
2313 free( val.psz_string )
2315 GET_META( title, "meta-title" );
2316 GET_META( artist, "meta-artist" );
2317 GET_META( genre, "meta-genre" );
2318 GET_META( copyright, "meta-copyright" );
2319 GET_META( description, "meta-description" );
2320 GET_META( date, "meta-date" );
2321 GET_META( url, "meta-url" );
2325 /*****************************************************************************
2326 * MRLSplit: parse the access, demux and url part of the
2327 * Media Resource Locator.
2328 *****************************************************************************/
2329 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2330 const char **ppsz_access, const char **ppsz_demux,
2333 const char *psz_access = "";
2334 const char *psz_demux = "";
2336 char *psz, *psz_check;
2338 psz = strchr( psz_dup, ':' );
2340 /* '@' not allowed in access/demux part */
2341 psz_check = strchr( psz_dup, '@' );
2342 if( psz_check && psz_check < psz ) psz = 0;
2344 #if defined( WIN32 ) || defined( UNDER_CE )
2345 if( psz - psz_dup == 1 )
2347 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2356 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2360 psz = strchr( psz_dup, '/' );
2367 psz_access = psz_dup;
2374 *ppsz_access = psz_access;
2375 *ppsz_demux = psz_demux;
2376 *ppsz_path = psz_path;
2379 /*****************************************************************************
2380 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2383 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2384 *****************************************************************************/
2385 static void MRLSections( input_thread_t *p_input, char *psz_source,
2386 int *pi_title_start, int *pi_title_end,
2387 int *pi_chapter_start, int *pi_chapter_end )
2389 char *psz, *psz_end, *psz_next, *psz_check;
2391 *pi_title_start = *pi_title_end = -1;
2392 *pi_chapter_start = *pi_chapter_end = -1;
2394 /* Start by parsing titles and chapters */
2395 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2397 /* Check we are really dealing with a title/chapter section */
2398 psz_check = psz + 1;
2399 if( !*psz_check ) return;
2400 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2401 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2402 if( *psz_check == ':' && ++psz_check )
2403 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2404 if( *psz_check != '-' && *psz_check ) return;
2405 if( *psz_check == '-' && ++psz_check )
2406 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2407 if( *psz_check != ':' && *psz_check ) return;
2408 if( *psz_check == ':' && ++psz_check )
2409 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2410 if( *psz_check ) return;
2412 /* Separate start and end */
2414 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2416 /* Look for the start title */
2417 *pi_title_start = strtol( psz, &psz_next, 0 );
2418 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2419 *pi_title_end = *pi_title_start;
2422 /* Look for the start chapter */
2424 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2425 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2426 *pi_chapter_end = *pi_chapter_start;
2430 /* Look for the end title */
2431 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2432 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2435 /* Look for the end chapter */
2436 if( *psz_end ) psz_end++;
2437 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2438 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2441 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2442 psz_source, *pi_title_start, *pi_chapter_start,
2443 *pi_title_end, *pi_chapter_end );
2446 /*****************************************************************************
2447 * input_AddSubtitles: add a subtitles file and enable it
2448 *****************************************************************************/
2449 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2450 vlc_bool_t b_check_extension )
2452 input_source_t *sub;
2455 char *psz_path, *psz_extension;
2457 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2462 /* if we are provided a subtitle.sub file,
2463 * see if we don't have a subtitle.idx and use it instead */
2464 psz_path = strdup( psz_subtitle );
2467 psz_extension = strrchr( psz_path, '.');
2468 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2472 strcpy( psz_extension, ".idx" );
2473 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2474 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2477 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2478 psz_path, psz_subtitle );
2479 strcpy( psz_subtitle, psz_path );
2485 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2487 sub = InputSourceNew( p_input );
2488 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2490 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2493 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2495 if( count.i_int == 0 )
2497 /* if it was first one, there is disable too */
2499 if( count.i_int < list.p_list->i_count )
2501 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2502 &list.p_list->p_values[count.i_int] );
2504 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );