1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
33 #include <vlc/decoder.h>
36 #include "input_internal.h"
38 #include "stream_output.h"
39 #include "vlc_playlist.h"
40 #include "vlc_interface.h"
41 #include "vlc_interaction.h"
42 #include "vlc_meta_engine.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Run ( input_thread_t *p_input );
50 static int RunAndClean ( input_thread_t *p_input );
52 static input_thread_t * Create ( vlc_object_t *, input_item_t *, char *,
54 static int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
55 static void Error ( input_thread_t *p_input );
56 static void End ( input_thread_t *p_input );
57 static void MainLoop( input_thread_t *p_input );
59 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
60 static void ControlReduce( input_thread_t * );
61 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
63 static int UpdateFromAccess( input_thread_t * );
64 static int UpdateFromDemux( input_thread_t * );
65 static int UpdateMeta( input_thread_t *, vlc_bool_t );
67 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
69 static void DecodeUrl( char * );
70 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
72 static input_source_t *InputSourceNew( input_thread_t *);
73 static int InputSourceInit( input_thread_t *, input_source_t *,
74 char *, 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 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 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
123 /* Init Common fields */
124 p_input->b_eof = VLC_FALSE;
125 p_input->b_can_pace_control = VLC_TRUE;
126 p_input->i_start = 0;
129 p_input->i_title = 0;
130 p_input->title = NULL;
131 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
132 p_input->i_state = INIT_S;
133 p_input->i_rate = INPUT_RATE_DEFAULT;
134 p_input->i_bookmark = 0;
135 p_input->bookmark = NULL;
136 p_input->p_meta = NULL;
137 p_input->p_es_out = NULL;
138 p_input->p_sout = NULL;
139 p_input->b_out_pace_control = VLC_FALSE;
140 p_input->i_pts_delay = 0;
142 /* Init Input fields */
143 p_input->input.p_item = p_item;
144 p_input->input.p_access = NULL;
145 p_input->input.p_stream = NULL;
146 p_input->input.p_demux = NULL;
147 p_input->input.b_title_demux = VLC_FALSE;
148 p_input->input.i_title = 0;
149 p_input->input.title = NULL;
150 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
151 p_input->input.b_can_pace_control = VLC_TRUE;
152 p_input->input.b_eof = VLC_FALSE;
153 p_input->input.i_cr_average = 0;
155 if( !p_input->input.p_item->p_meta )
156 p_input->input.p_item->p_meta = vlc_meta_New();
157 stats_ReinitInputStats( p_item->p_stats );
160 p_input->i_slave = 0;
161 p_input->slave = NULL;
163 /* Init control buffer */
164 vlc_mutex_init( p_input, &p_input->lock_control );
165 p_input->i_control = 0;
167 /* Parse input options */
168 vlc_mutex_lock( &p_item->lock );
169 for( i = 0; i < p_item->i_options; i++ )
171 var_OptionParse( p_input, p_item->ppsz_options[i] );
173 vlc_mutex_unlock( &p_item->lock );
175 /* Create Object Variables for private use only */
176 input_ConfigVarInit( p_input );
178 /* Create Objects variables for public Get and Set */
179 input_ControlVarInit( p_input );
180 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
184 var_Get( p_input, "bookmarks", &val );
187 /* FIXME: have a common cfg parsing routine used by sout and others */
188 char *psz_parser, *psz_start, *psz_end;
189 psz_parser = val.psz_string;
190 while( (psz_start = strchr( psz_parser, '{' ) ) )
192 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
195 psz_end = strchr( psz_start, '}' );
196 if( !psz_end ) break;
197 psz_parser = psz_end + 1;
198 backup = *psz_parser;
201 while( (psz_end = strchr( psz_start, ',' ) ) )
204 if( !strncmp( psz_start, "name=", 5 ) )
206 p_seekpoint->psz_name = psz_start + 5;
208 else if( !strncmp( psz_start, "bytes=", 6 ) )
210 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
212 else if( !strncmp( psz_start, "time=", 5 ) )
214 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
217 psz_start = psz_end + 1;
219 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
220 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
221 p_seekpoint->i_time_offset );
222 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
223 vlc_seekpoint_Delete( p_seekpoint );
224 *psz_parser = backup;
226 free( val.psz_string );
230 /* Remove 'Now playing' info as it is probably outdated */
231 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT),
232 VLC_META_NOW_PLAYING ); /* ? Don't translate as it might has been copied ? */
238 * Initialize an input thread and run it. You will need to monitor the thread to clean
239 * up after it is done
241 * \param p_parent a vlc_object
242 * \param p_item an input item
243 * \return a pointer to the spawned input thread
245 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
246 input_item_t *p_item )
248 return __input_CreateThread2( p_parent, p_item, NULL );
253 input_thread_t *__input_CreateThread2( vlc_object_t *p_parent,
254 input_item_t *p_item,
257 input_thread_t *p_input = NULL; /* thread descriptor */
259 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
260 /* Now we can attach our new input */
261 vlc_object_attach( p_input, p_parent );
263 /* Create thread and wait for its readiness. */
264 if( vlc_thread_create( p_input, "input", Run,
265 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
267 msg_Err( p_input, "cannot create input thread" );
268 vlc_object_detach( p_input );
269 vlc_object_destroy( p_input );
278 * Initialize an input thread and run it. This thread will clean after himself,
279 * you can forget about it. It can work either in blocking or non-blocking mode
281 * \param p_parent a vlc_object
282 * \param p_item an input item
283 * \param b_block should we block until read is finished ?
284 * \return the input object id if non blocking, an error code else
286 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
289 input_thread_t *p_input = NULL; /* thread descriptor */
291 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
292 /* Now we can attach our new input */
293 vlc_object_attach( p_input, p_parent );
297 RunAndClean( p_input );
302 if( vlc_thread_create( p_input, "input", RunAndClean,
303 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
305 msg_Err( p_input, "cannot create input thread" );
306 vlc_object_detach( p_input );
307 vlc_object_destroy( p_input );
311 return p_input->i_object_id;
315 * Initialize an input and initialize it to preparse the item
316 * This function is blocking. It will only accept to parse files
318 * \param p_parent a vlc_object_t
319 * \param p_item an input item
320 * \return VLC_SUCCESS or an error
322 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
324 input_thread_t *p_input = NULL; /* thread descriptor */
326 /* Allocate descriptor */
327 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
328 p_input->i_flags |= OBJECT_FLAGS_QUIET;
329 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
331 /* Now we can attach our new input */
332 vlc_object_attach( p_input, p_parent );
334 Init( p_input, VLC_TRUE );
336 /* Clean up master */
337 InputSourceClean( p_input, &p_input->input );
339 /* Unload all modules */
340 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
342 vlc_object_detach( p_input );
343 vlc_object_destroy( p_input );
348 int __input_SecondaryPreparse( vlc_object_t *p_parent, input_item_t *p_item )
350 struct meta_engine_t *p_me;
352 /* FIXME: don't launch any module if we already have all the needed
353 * info. Easiest way to do this would be to add a dummy module.
354 * I'll do that later */
356 p_me = vlc_object_create( p_parent, VLC_OBJECT_META_ENGINE );
357 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
358 p_me->i_mandatory = VLC_META_ENGINE_TITLE
359 | VLC_META_ENGINE_AUTHOR
360 | VLC_META_ENGINE_ARTIST
361 | VLC_META_ENGINE_ART_URL;
362 p_me->i_optional = 0;
363 p_me->p_item = p_item;
364 p_me->p_module = module_Need( p_me, "meta engine", 0, VLC_FALSE );
366 if( !p_me->p_module )
368 msg_Err( p_parent, "no suitable meta engine module" );
372 module_Unneed( p_me, p_me->p_module );
375 && p_item->p_meta->psz_arturl
376 && *p_item->p_meta->psz_arturl
377 && strncmp( p_item->p_meta->psz_arturl, "file", 4 ) )
379 /* process album art */
380 #define MAX_PATH 10000
381 char *psz_artist = p_item->p_meta->psz_artist;
382 char *psz_album = p_item->p_meta->psz_album;
383 char *psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
384 char *psz_filename = (char *)malloc( MAX_PATH );
388 snprintf( psz_filename, MAX_PATH,
389 "file://%s/" CONFIG_DIR,
390 p_parent->p_libvlc->psz_homedir );
391 utf8_mkdir( psz_filename+7 );
392 snprintf( psz_filename, MAX_PATH,
393 "file://%s/" CONFIG_DIR "/art",
394 p_parent->p_libvlc->psz_homedir );
395 utf8_mkdir( psz_filename+7 );
396 snprintf( psz_filename, MAX_PATH,
397 "file://%s/" CONFIG_DIR "/art/%s",
398 p_parent->p_libvlc->psz_homedir,
400 utf8_mkdir( psz_filename+7 );
401 snprintf( psz_filename, MAX_PATH,
402 "file://%s/" CONFIG_DIR "/art/%s/%s",
403 p_parent->p_libvlc->psz_homedir,
404 psz_artist, psz_album );
405 utf8_mkdir( psz_filename+7 );
407 snprintf( psz_filename, MAX_PATH,
408 "file://%s/" CONFIG_DIR "/art/%s/%s/art%s",
409 p_parent->p_libvlc->psz_homedir,
410 psz_artist, psz_album, psz_type );
411 msg_Dbg( p_parent, "Saving album art to %s", psz_filename );
413 /* Check if file exists */
414 p_file = utf8_fopen( psz_filename+7, "r" );
417 msg_Dbg( p_parent, "Album art %s already exists", psz_filename );
422 stream_t *p_stream = stream_UrlNew( p_parent,
423 p_item->p_meta->psz_arturl );
427 void *p_buffer = malloc( 1<<16 );
429 p_file = utf8_fopen( psz_filename+7, "w" );
431 while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
433 printf("%d\n", l_read);
434 fwrite( p_buffer, l_read, 1, p_file );
440 stream_Delete( p_stream );
441 msg_Dbg( p_parent, "Album art saved to %s\n", psz_filename );
442 free( p_item->p_meta->psz_arturl );
443 p_item->p_meta->psz_arturl = strdup( psz_filename );
446 free( psz_filename );
453 * Request a running input thread to stop and die
455 * \param the input thread to stop
457 void input_StopThread( input_thread_t *p_input )
462 /* Set die for input */
463 p_input->b_die = VLC_TRUE;
465 /* We cannot touch p_input fields directly (we can from another thread),
466 * so use the vlc_object_find way, it's perfectly safe */
468 /* Set die for all access */
469 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
470 for( i = 0; i < p_list->i_count; i++ )
472 p_list->p_values[i].p_object->b_die = VLC_TRUE;
474 vlc_list_release( p_list );
476 /* Set die for all stream */
477 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
478 for( i = 0; i < p_list->i_count; i++ )
480 p_list->p_values[i].p_object->b_die = VLC_TRUE;
482 vlc_list_release( p_list );
484 /* Set die for all demux */
485 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
486 for( i = 0; i < p_list->i_count; i++ )
488 p_list->p_values[i].p_object->b_die = VLC_TRUE;
490 vlc_list_release( p_list );
492 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
496 * Clean up a dead input thread
497 * This function does not return until the thread is effectively cancelled.
499 * \param the input thread to kill
501 void input_DestroyThread( input_thread_t *p_input )
503 /* Join the thread */
504 vlc_thread_join( p_input );
506 /* Delete input lock (only after thread joined) */
507 vlc_mutex_destroy( &p_input->lock_control );
509 /* TODO: maybe input_DestroyThread should also delete p_input instead
510 * of the playlist but I'm not sure if it's possible */
513 /*****************************************************************************
514 * Run: main thread loop
515 * This is the "normal" thread that spawns the input processing chain,
516 * reads the stream, cleans up and waits
517 *****************************************************************************/
518 static int Run( input_thread_t *p_input )
520 /* Signal that the thread is launched */
521 vlc_thread_ready( p_input );
523 if( Init( p_input, VLC_FALSE ) )
525 /* If we failed, wait before we are killed, and exit */
526 p_input->b_error = VLC_TRUE;
530 /* Tell we're dead */
531 p_input->b_dead = VLC_TRUE;
538 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
540 /* We have finish to demux data but not to play them */
541 while( !p_input->b_die )
543 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
546 msg_Dbg( p_input, "waiting decoder fifos to empty" );
548 msleep( INPUT_IDLE_SLEEP );
551 /* We have finished */
552 p_input->b_eof = VLC_TRUE;
555 /* Wait until we are asked to die */
556 if( !p_input->b_die )
567 /*****************************************************************************
568 * RunAndClean: main thread loop
569 * This is the "just forget me" thread that spawns the input processing chain,
570 * reads the stream, cleans up and releases memory
571 *****************************************************************************/
572 static int RunAndClean( input_thread_t *p_input )
574 /* Signal that the thread is launched */
575 vlc_thread_ready( p_input );
577 if( Init( p_input, VLC_FALSE ) )
579 /* If we failed, just exit */
585 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
587 /* We have finished demuxing data but not playing it */
588 while( !p_input->b_die )
590 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
593 msg_Dbg( p_input, "waiting decoder fifos to empty" );
595 msleep( INPUT_IDLE_SLEEP );
598 /* We have finished */
599 p_input->b_eof = VLC_TRUE;
606 vlc_object_detach( p_input );
607 vlc_object_destroy( p_input );
613 /*****************************************************************************
614 * Main loop: Fill buffers from access, and demux
615 *****************************************************************************/
616 static void MainLoop( input_thread_t *p_input )
618 int64_t i_intf_update = 0;
619 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
621 vlc_bool_t b_force_update = VLC_FALSE;
627 if( p_input->i_state != PAUSE_S )
629 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
630 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
637 if( p_input->input.b_title_demux &&
638 p_input->input.p_demux->info.i_update )
640 i_ret = UpdateFromDemux( p_input );
641 b_force_update = VLC_TRUE;
643 else if( !p_input->input.b_title_demux &&
644 p_input->input.p_access &&
645 p_input->input.p_access->info.i_update )
647 i_ret = UpdateFromAccess( p_input );
648 b_force_update = VLC_TRUE;
652 if( i_ret == 0 ) /* EOF */
656 var_Get( p_input, "input-repeat", &repeat );
657 if( repeat.i_int == 0 )
659 /* End of file - we do not set b_die because only the
660 * playlist is allowed to do so. */
661 msg_Dbg( p_input, "EOF reached" );
662 p_input->input.b_eof = VLC_TRUE;
666 msg_Dbg( p_input, "repeating the same input (%d)",
668 if( repeat.i_int > 0 )
671 var_Set( p_input, "input-repeat", repeat );
674 /* Seek to start title/seekpoint */
675 val.i_int = p_input->input.i_title_start -
676 p_input->input.i_title_offset;
677 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
679 input_ControlPush( p_input,
680 INPUT_CONTROL_SET_TITLE, &val );
682 val.i_int = p_input->input.i_seekpoint_start -
683 p_input->input.i_seekpoint_offset;
684 if( val.i_int > 0 /* TODO: check upper boundary */ )
685 input_ControlPush( p_input,
686 INPUT_CONTROL_SET_SEEKPOINT, &val );
688 /* Seek to start position */
689 if( p_input->i_start > 0 )
691 val.i_time = p_input->i_start;
692 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
698 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
705 p_input->b_error = VLC_TRUE;
708 if( i_ret > 0 && p_input->i_slave > 0 )
710 SlaveDemux( p_input );
720 vlc_mutex_lock( &p_input->lock_control );
721 ControlReduce( p_input );
722 while( !ControlPopNoLock( p_input, &i_type, &val ) )
724 msg_Dbg( p_input, "control type=%d", i_type );
725 if( Control( p_input, i_type, val ) )
726 b_force_update = VLC_TRUE;
728 vlc_mutex_unlock( &p_input->lock_control );
730 if( b_force_update || i_intf_update < mdate() )
734 int64_t i_time, i_length;
735 /* update input status variables */
736 if( !demux2_Control( p_input->input.p_demux,
737 DEMUX_GET_POSITION, &f_pos ) )
739 val.f_float = (float)f_pos;
740 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
742 if( !demux2_Control( p_input->input.p_demux,
743 DEMUX_GET_TIME, &i_time ) )
745 p_input->i_time = i_time;
747 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
749 if( !demux2_Control( p_input->input.p_demux,
750 DEMUX_GET_LENGTH, &i_length ) )
753 var_Get( p_input, "length", &old_val );
754 val.i_time = i_length;
755 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
757 if( old_val.i_time != val.i_time )
759 UpdateItemLength( p_input, i_length, VLC_TRUE );
763 var_SetBool( p_input, "intf-change", VLC_TRUE );
764 i_intf_update = mdate() + I64C(150000);
770 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
780 /* Initialize optional stream output. (before access/demuxer)
781 * XXX: we add a special case if the uri starts by vlc.
782 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
783 * be destroyed in case of a file).
784 * (this will break playing of file starting by 'vlc:' but I don't
785 * want to add more logic, just force file by file:// or code it ;)
787 memset( &p_input->counters, 0, sizeof( p_input->counters ) );
790 /* Prepare statistics */
791 #define INIT_COUNTER( p, type, compute ) p_input->counters.p_##p = \
792 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
793 if( p_input->p_libvlc->b_stats )
795 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
796 INIT_COUNTER( read_packets, INTEGER, COUNTER );
797 INIT_COUNTER( demux_read, INTEGER, COUNTER );
798 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
799 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
800 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
801 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
802 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
803 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
804 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
805 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
806 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
807 p_input->counters.p_sout_send_bitrate = NULL;
808 p_input->counters.p_sout_sent_packets = NULL;
809 p_input->counters.p_sout_sent_bytes = NULL;
810 if( p_input->counters.p_demux_bitrate )
811 p_input->counters.p_demux_bitrate->update_interval = 1000000;
812 if( p_input->counters.p_input_bitrate )
813 p_input->counters.p_input_bitrate->update_interval = 1000000;
815 vlc_mutex_init( p_input, &p_input->counters.counters_lock );
818 psz = var_GetString( p_input, "sout" );
819 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
821 p_input->p_sout = sout_NewInstance( p_input, psz );
822 if( p_input->p_sout == NULL )
824 msg_Err( p_input, "cannot start stream output instance, " \
829 if( p_input->p_libvlc->b_stats )
831 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
832 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
833 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
834 if( p_input->counters.p_sout_send_bitrate )
835 p_input->counters.p_sout_send_bitrate->update_interval =
843 p_input->p_es_out = input_EsOutNew( p_input );
844 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
845 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
847 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
848 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
850 if( InputSourceInit( p_input, &p_input->input,
851 p_input->input.p_item->psz_uri, NULL, b_quick ) )
856 /* Create global title (from master) */
859 p_input->i_title = p_input->input.i_title;
860 p_input->title = p_input->input.title;
861 p_input->i_title_offset = p_input->input.i_title_offset;
862 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
863 if( p_input->i_title > 0 )
865 /* Setup variables */
866 input_ControlVarNavigation( p_input );
867 input_ControlVarTitle( p_input, 0 );
871 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
872 p_input->b_can_pause = p_input->input.b_can_pause;
875 if( p_input->i_pts_delay < 0 )
876 p_input->i_pts_delay = 0;
878 /* If the desynchronisation requested by the user is < 0, we need to
879 * cache more data. */
880 var_Get( p_input, "audio-desync", &val );
881 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
883 /* Update cr_average depending on the caching */
884 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
885 p_input->input.i_cr_average /= 10;
886 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
889 /* Load master infos */
891 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
892 &val.i_time ) && val.i_time > 0 )
894 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
895 UpdateItemLength( p_input, val.i_time, b_quick );
896 p_input->input.p_item->i_duration = val.i_time;
899 /* Start title/chapter */
902 val.i_int = p_input->input.i_title_start -
903 p_input->input.i_title_offset;
904 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
905 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
906 val.i_int = p_input->input.i_seekpoint_start -
907 p_input->input.i_seekpoint_offset;
908 if( val.i_int > 0 /* TODO: check upper boundary */ )
909 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
913 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
915 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
918 if( p_input->i_start > 0 )
920 if( p_input->i_start >= val.i_time )
922 msg_Warn( p_input, "invalid start-time ignored" );
928 msg_Dbg( p_input, "starting at time: %ds",
929 (int)( p_input->i_start / I64C(1000000) ) );
931 s.i_time = p_input->i_start;
932 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
935 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
937 msg_Warn( p_input, "invalid stop-time ignored" );
943 /* Get fps and set it if not already set */
944 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
947 float f_requested_fps;
949 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
950 var_SetFloat( p_input, "sub-original-fps", f_fps );
952 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
953 if( f_requested_fps != f_fps )
955 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
957 var_SetFloat( p_input, "sub-fps", f_requested_fps );
961 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
964 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
968 /* Look for and add subtitle files */
969 psz_subtitle = var_GetString( p_input, "sub-file" );
972 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
973 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
976 var_Get( p_input, "sub-autodetect-file", &val );
979 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
980 char **subs = subtitles_Detect( p_input, psz_autopath,
981 p_input->input.p_item->psz_uri );
986 /* Try to autoselect the first autodetected subtitles file
987 * if no subtitles file was specified */
988 if( *psz_subtitle == 0 && subs && subs[0] )
990 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
995 /* Then, just add the following subtitles files */
996 for( ; subs && subs[i]; i++ )
998 if( strcmp( psz_subtitle, subs[i] ) )
1000 sub = InputSourceNew( p_input );
1001 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
1004 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
1009 if( subs ) free( subs );
1010 if( psz_autopath ) free( psz_autopath );
1012 free( psz_subtitle );
1014 /* Look for slave */
1015 psz = var_GetString( p_input, "input-slave" );
1019 input_source_t *slave;
1020 while( psz && *psz )
1022 while( *psz == ' ' || *psz == '#' )
1026 if( ( psz_delim = strchr( psz, '#' ) ) )
1028 *psz_delim++ = '\0';
1035 msg_Dbg( p_input, "adding slave input '%s'", psz );
1036 slave = InputSourceNew( p_input );
1037 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
1039 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1044 if( psz ) free( psz );
1048 p_input->i_start = 0;
1049 p_input->i_start = 0;
1055 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1056 i_es_out_mode = ES_OUT_MODE_AUTO;
1058 if( p_input->p_sout )
1060 var_Get( p_input, "sout-all", &val );
1063 i_es_out_mode = ES_OUT_MODE_ALL;
1068 var_Get( p_input, "programs", &val );
1069 if ( val.p_list && val.p_list->i_count )
1071 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1072 /* Note : we should remove the "program" callback. */
1075 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1079 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1081 /* Inform the demuxer about waited group (needed only for DVB) */
1082 if( i_es_out_mode == ES_OUT_MODE_ALL )
1084 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1086 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1088 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
1093 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
1094 (int) var_GetInteger( p_input, "program" ), NULL );
1097 if( p_input->p_sout )
1099 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
1101 p_input->b_out_pace_control = VLC_FALSE;
1105 p_input->b_out_pace_control = VLC_TRUE;
1108 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
1110 /* We don't want a high input priority here or we'll
1111 * end-up sucking up all the CPU time */
1112 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1115 msg_Dbg( p_input, "starting in %s mode",
1116 p_input->b_out_pace_control ? "async" : "sync" );
1120 p_meta = p_input->input.p_item->p_meta;
1121 /* Get meta data from users */
1122 InputMetaUser( p_input );
1123 /* Get meta data from master input */
1124 demux2_Control( p_input->input.p_demux, DEMUX_GET_META, p_meta );
1126 /* Access_file does not give any meta, and there are no slave */
1129 if( p_input->input.p_access )
1130 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1133 /* Get meta data from slave input */
1134 for( i = 0; i < p_input->i_slave; i++ )
1136 demux2_Control( p_input->slave[i]->p_demux,
1137 DEMUX_GET_META, p_meta );
1138 if( p_input->slave[i]->p_access )
1140 access2_Control( p_input->slave[i]->p_access,
1141 ACCESS_GET_META, p_meta );
1146 UpdateMeta( p_input, b_quick );
1150 msg_Dbg( p_input, "`%s' successfully opened",
1151 p_input->input.p_item->psz_uri );
1155 /* initialization is complete */
1156 input_ChangeState(p_input, PLAYING_S);
1161 if( p_input->p_es_out )
1162 input_EsOutDelete( p_input->p_es_out );
1164 if( p_input->p_sout )
1165 sout_DeleteInstance( p_input->p_sout );
1167 /* Mark them deleted */
1168 p_input->input.p_demux = NULL;
1169 p_input->input.p_stream = NULL;
1170 p_input->input.p_access = NULL;
1171 p_input->p_es_out = NULL;
1172 p_input->p_sout = NULL;
1174 return VLC_EGENERIC;
1177 /*****************************************************************************
1178 * Error: RunThread() error loop
1179 *****************************************************************************
1180 * This function is called when an error occurred during thread main's loop.
1181 *****************************************************************************/
1182 static void Error( input_thread_t *p_input )
1184 while( !p_input->b_die )
1187 msleep( INPUT_IDLE_SLEEP );
1191 /*****************************************************************************
1192 * End: end the input thread
1193 *****************************************************************************/
1194 static void End( input_thread_t * p_input )
1198 msg_Dbg( p_input, "closing input" );
1200 /* We are at the end */
1201 input_ChangeState(p_input, END_S);
1203 /* Clean control variables */
1204 input_ControlVarClean( p_input );
1206 /* Clean up master */
1207 InputSourceClean( p_input, &p_input->input );
1210 for( i = 0; i < p_input->i_slave; i++ )
1212 InputSourceClean( p_input, p_input->slave[i] );
1213 free( p_input->slave[i] );
1215 if( p_input->slave ) free( p_input->slave );
1217 /* Unload all modules */
1218 if( p_input->p_es_out )
1219 input_EsOutDelete( p_input->p_es_out );
1221 #define CL_CO( c ) stats_CounterClean( p_input->counters.p_##c ); p_input->counters.p_##c = NULL;
1222 if( p_input->p_libvlc->b_stats )
1224 vlc_mutex_lock( &p_input->counters.counters_lock );
1225 CL_CO( read_bytes );
1226 CL_CO( read_packets );
1227 CL_CO( demux_read );
1228 CL_CO( input_bitrate );
1229 CL_CO( demux_bitrate );
1230 CL_CO( played_abuffers );
1231 CL_CO( lost_abuffers );
1232 CL_CO( displayed_pictures );
1233 CL_CO( lost_pictures );
1234 CL_CO( decoded_audio) ;
1235 CL_CO( decoded_video );
1236 CL_CO( decoded_sub) ;
1237 vlc_mutex_unlock( &p_input->counters.counters_lock );
1240 /* Close optional stream output instance */
1241 if( p_input->p_sout )
1245 vlc_mutex_lock( &p_input->counters.counters_lock );
1246 CL_CO( sout_sent_packets );
1247 CL_CO( sout_sent_bytes );
1248 CL_CO( sout_send_bitrate );
1249 vlc_mutex_unlock( &p_input->counters.counters_lock );
1251 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool )
1253 /* attach sout to the playlist */
1254 msg_Dbg( p_input, "keeping sout" );
1255 vlc_object_detach( p_input->p_sout );
1256 vlc_object_attach( p_input->p_sout, p_input->p_libvlc->p_playlist );
1260 msg_Dbg( p_input, "destroying sout" );
1261 sout_DeleteInstance( p_input->p_sout );
1266 /* Tell we're dead */
1267 p_input->b_dead = VLC_TRUE;
1270 /*****************************************************************************
1272 *****************************************************************************/
1273 static inline int ControlPopNoLock( input_thread_t *p_input,
1274 int *pi_type, vlc_value_t *p_val )
1276 if( p_input->i_control <= 0 )
1278 return VLC_EGENERIC;
1281 *pi_type = p_input->control[0].i_type;
1282 *p_val = p_input->control[0].val;
1284 p_input->i_control--;
1285 if( p_input->i_control > 0 )
1289 for( i = 0; i < p_input->i_control; i++ )
1291 p_input->control[i].i_type = p_input->control[i+1].i_type;
1292 p_input->control[i].val = p_input->control[i+1].val;
1299 static void ControlReduce( input_thread_t *p_input )
1302 for( i = 1; i < p_input->i_control; i++ )
1304 const int i_lt = p_input->control[i-1].i_type;
1305 const int i_ct = p_input->control[i].i_type;
1307 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1308 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1312 ( i_ct == INPUT_CONTROL_SET_STATE ||
1313 i_ct == INPUT_CONTROL_SET_RATE ||
1314 i_ct == INPUT_CONTROL_SET_POSITION ||
1315 i_ct == INPUT_CONTROL_SET_TIME ||
1316 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1317 i_ct == INPUT_CONTROL_SET_TITLE ||
1318 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1319 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1322 // msg_Dbg( p_input, "merged at %d", i );
1323 /* Remove the i-1 */
1324 for( j = i; j < p_input->i_control; j++ )
1325 p_input->control[j-1] = p_input->control[j];
1326 p_input->i_control--;
1330 /* TODO but that's not that important
1331 - merge SET_X with SET_X_CMD
1332 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1333 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1340 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1343 vlc_bool_t b_force_update = VLC_FALSE;
1345 if( !p_input ) return b_force_update;
1349 case INPUT_CONTROL_SET_DIE:
1350 msg_Dbg( p_input, "control: stopping input" );
1351 /* Mark all submodules to die */
1352 if( p_input->input.p_access )
1353 p_input->input.p_access->b_die = VLC_TRUE;
1354 if( p_input->input.p_stream )
1355 p_input->input.p_stream->b_die = VLC_TRUE;
1356 p_input->input.p_demux->b_die = VLC_TRUE;
1358 p_input->b_die = VLC_TRUE;
1361 case INPUT_CONTROL_SET_POSITION:
1362 case INPUT_CONTROL_SET_POSITION_OFFSET:
1365 if( i_type == INPUT_CONTROL_SET_POSITION )
1367 f_pos = val.f_float;
1371 /* Should not fail */
1372 demux2_Control( p_input->input.p_demux,
1373 DEMUX_GET_POSITION, &f_pos );
1374 f_pos += val.f_float;
1376 if( f_pos < 0.0 ) f_pos = 0.0;
1377 if( f_pos > 1.0 ) f_pos = 1.0;
1378 /* Reset the decoders states and clock sync (before calling the demuxer */
1379 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1380 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1381 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1384 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1385 "%2.1f%% failed", f_pos * 100 );
1389 if( p_input->i_slave > 0 )
1390 SlaveSeek( p_input );
1392 b_force_update = VLC_TRUE;
1397 case INPUT_CONTROL_SET_TIME:
1398 case INPUT_CONTROL_SET_TIME_OFFSET:
1403 if( i_type == INPUT_CONTROL_SET_TIME )
1405 i_time = val.i_time;
1409 /* Should not fail */
1410 demux2_Control( p_input->input.p_demux,
1411 DEMUX_GET_TIME, &i_time );
1412 i_time += val.i_time;
1414 if( i_time < 0 ) i_time = 0;
1416 /* Reset the decoders states and clock sync (before calling the demuxer */
1417 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1418 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1420 i_ret = demux2_Control( p_input->input.p_demux,
1421 DEMUX_SET_TIME, i_time );
1426 /* Emulate it with a SET_POS */
1427 demux2_Control( p_input->input.p_demux,
1428 DEMUX_GET_LENGTH, &i_length );
1431 double f_pos = (double)i_time / (double)i_length;
1432 i_ret = demux2_Control( p_input->input.p_demux,
1433 DEMUX_SET_POSITION, f_pos );
1438 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1439 " failed or not possible", i_time );
1443 if( p_input->i_slave > 0 )
1444 SlaveSeek( p_input );
1446 b_force_update = VLC_TRUE;
1451 case INPUT_CONTROL_SET_STATE:
1452 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1453 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1456 if( p_input->input.p_access )
1457 i_ret = access2_Control( p_input->input.p_access,
1458 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1460 i_ret = demux2_Control( p_input->input.p_demux,
1461 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1465 /* FIXME What to do ? */
1466 msg_Warn( p_input, "cannot unset pause -> EOF" );
1467 vlc_mutex_unlock( &p_input->lock_control );
1468 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1469 vlc_mutex_lock( &p_input->lock_control );
1472 b_force_update = VLC_TRUE;
1474 /* Switch to play */
1475 p_input->i_state = PLAYING_S;
1476 val.i_int = PLAYING_S;
1477 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1480 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1481 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1483 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1484 p_input->b_can_pause )
1487 if( p_input->input.p_access )
1488 i_ret = access2_Control( p_input->input.p_access,
1489 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1491 i_ret = demux2_Control( p_input->input.p_demux,
1492 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1494 b_force_update = VLC_TRUE;
1498 msg_Warn( p_input, "cannot set pause state" );
1499 val.i_int = p_input->i_state;
1503 val.i_int = PAUSE_S;
1506 /* Switch to new state */
1507 p_input->i_state = val.i_int;
1508 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1510 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1512 b_force_update = VLC_TRUE;
1514 /* Correct "state" value */
1515 val.i_int = p_input->i_state;
1516 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1518 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1520 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1524 case INPUT_CONTROL_SET_RATE:
1525 case INPUT_CONTROL_SET_RATE_SLOWER:
1526 case INPUT_CONTROL_SET_RATE_FASTER:
1530 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1531 i_rate = p_input->i_rate * 2;
1532 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1533 i_rate = p_input->i_rate / 2;
1537 if( i_rate < INPUT_RATE_MIN )
1539 msg_Dbg( p_input, "cannot set rate faster" );
1540 i_rate = INPUT_RATE_MIN;
1542 else if( i_rate > INPUT_RATE_MAX )
1544 msg_Dbg( p_input, "cannot set rate slower" );
1545 i_rate = INPUT_RATE_MAX;
1547 if( i_rate != INPUT_RATE_DEFAULT &&
1548 ( !p_input->b_can_pace_control ||
1549 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1551 msg_Dbg( p_input, "cannot change rate" );
1552 i_rate = INPUT_RATE_DEFAULT;
1554 if( i_rate != p_input->i_rate )
1556 p_input->i_rate = i_rate;
1558 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1560 /* We haven't send data to decoder when rate != default */
1561 if( i_rate == INPUT_RATE_DEFAULT )
1562 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1565 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1567 b_force_update = VLC_TRUE;
1572 case INPUT_CONTROL_SET_PROGRAM:
1573 /* No need to force update, es_out does it if needed */
1574 es_out_Control( p_input->p_es_out,
1575 ES_OUT_SET_GROUP, val.i_int );
1577 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1581 case INPUT_CONTROL_SET_ES:
1582 /* No need to force update, es_out does it if needed */
1583 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1584 input_EsOutGetFromID( p_input->p_es_out,
1588 case INPUT_CONTROL_SET_AUDIO_DELAY:
1589 input_EsOutSetDelay( p_input->p_es_out,
1590 AUDIO_ES, val.i_time );
1591 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1594 case INPUT_CONTROL_SET_SPU_DELAY:
1595 input_EsOutSetDelay( p_input->p_es_out,
1596 SPU_ES, val.i_time );
1597 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1600 case INPUT_CONTROL_SET_TITLE:
1601 case INPUT_CONTROL_SET_TITLE_NEXT:
1602 case INPUT_CONTROL_SET_TITLE_PREV:
1603 if( p_input->input.b_title_demux &&
1604 p_input->input.i_title > 0 )
1607 /* FIXME handle demux title */
1608 demux_t *p_demux = p_input->input.p_demux;
1611 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1612 i_title = p_demux->info.i_title - 1;
1613 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1614 i_title = p_demux->info.i_title + 1;
1616 i_title = val.i_int;
1618 if( i_title >= 0 && i_title < p_input->input.i_title )
1620 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1621 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1623 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1624 input_ControlVarTitle( p_input, i_title );
1627 else if( p_input->input.i_title > 0 )
1629 access_t *p_access = p_input->input.p_access;
1632 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1633 i_title = p_access->info.i_title - 1;
1634 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1635 i_title = p_access->info.i_title + 1;
1637 i_title = val.i_int;
1639 if( i_title >= 0 && i_title < p_input->input.i_title )
1641 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1642 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1644 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1645 stream_AccessReset( p_input->input.p_stream );
1649 case INPUT_CONTROL_SET_SEEKPOINT:
1650 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1651 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1652 if( p_input->input.b_title_demux &&
1653 p_input->input.i_title > 0 )
1655 demux_t *p_demux = p_input->input.p_demux;
1657 int64_t i_input_time;
1658 int64_t i_seekpoint_time;
1660 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1662 i_seekpoint = p_demux->info.i_seekpoint;
1663 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1664 if( i_seekpoint_time >= 0 &&
1665 !demux2_Control( p_demux,
1666 DEMUX_GET_TIME, &i_input_time ) )
1668 if ( i_input_time < i_seekpoint_time + 3000000 )
1674 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1675 i_seekpoint = p_demux->info.i_seekpoint + 1;
1677 i_seekpoint = val.i_int;
1679 if( i_seekpoint >= 0 && i_seekpoint <
1680 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1682 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1683 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1685 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1688 else if( p_input->input.i_title > 0 )
1690 demux_t *p_demux = p_input->input.p_demux;
1691 access_t *p_access = p_input->input.p_access;
1693 int64_t i_input_time;
1694 int64_t i_seekpoint_time;
1696 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1698 i_seekpoint = p_access->info.i_seekpoint;
1699 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1700 if( i_seekpoint_time >= 0 &&
1701 demux2_Control( p_demux,
1702 DEMUX_GET_TIME, &i_input_time ) )
1704 if ( i_input_time < i_seekpoint_time + 3000000 )
1710 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1711 i_seekpoint = p_access->info.i_seekpoint + 1;
1713 i_seekpoint = val.i_int;
1715 if( i_seekpoint >= 0 && i_seekpoint <
1716 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1718 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1719 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1721 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1723 stream_AccessReset( p_input->input.p_stream );
1728 case INPUT_CONTROL_ADD_SLAVE:
1729 if( val.psz_string )
1731 input_source_t *slave = InputSourceNew( p_input );
1733 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1736 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1740 msg_Dbg( p_input, "adding %s as slave on the fly",
1744 if( demux2_Control( p_input->input.p_demux,
1745 DEMUX_GET_TIME, &i_time ) )
1747 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1748 InputSourceClean( p_input, slave );
1752 if( demux2_Control( slave->p_demux,
1753 DEMUX_SET_TIME, i_time ) )
1755 msg_Err( p_input, "seek failed for new slave" );
1756 InputSourceClean( p_input, slave );
1761 /* Get meta (access and demux) */
1762 access2_Control( slave->p_access, ACCESS_GET_META,
1764 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1765 UpdateMeta( p_input, VLC_FALSE );
1767 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1771 msg_Warn( p_input, "failed to add %s as slave",
1775 free( val.psz_string );
1779 case INPUT_CONTROL_SET_BOOKMARK:
1781 msg_Err( p_input, "not yet implemented" );
1785 return b_force_update;
1788 /*****************************************************************************
1790 *****************************************************************************/
1791 static int UpdateFromDemux( input_thread_t *p_input )
1793 demux_t *p_demux = p_input->input.p_demux;
1796 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1798 v.i_int = p_demux->info.i_title;
1799 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1801 input_ControlVarTitle( p_input, p_demux->info.i_title );
1803 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1805 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1807 v.i_int = p_demux->info.i_seekpoint;
1808 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1810 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1812 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1814 /* Hmmm only works with master input */
1815 if( p_input->input.p_demux == p_demux )
1817 int i_title_end = p_input->input.i_title_end -
1818 p_input->input.i_title_offset;
1819 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1820 p_input->input.i_seekpoint_offset;
1822 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1824 if( p_demux->info.i_title > i_title_end ||
1825 ( p_demux->info.i_title == i_title_end &&
1826 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1828 else if( i_seekpoint_end >=0 )
1830 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1832 else if( i_title_end >= 0 )
1834 if( p_demux->info.i_title > i_title_end ) return 0;
1841 /*****************************************************************************
1843 *****************************************************************************/
1844 static int UpdateFromAccess( input_thread_t *p_input )
1846 access_t *p_access = p_input->input.p_access;
1849 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1851 v.i_int = p_access->info.i_title;
1852 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1854 input_ControlVarTitle( p_input, p_access->info.i_title );
1856 stream_AccessUpdate( p_input->input.p_stream );
1858 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1860 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1862 v.i_int = p_access->info.i_seekpoint;
1863 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1864 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1866 if( p_access->info.i_update & INPUT_UPDATE_META )
1868 /* TODO maybe multi - access ? */
1869 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1870 access2_Control( p_input->input.p_access,ACCESS_GET_META, p_meta );
1871 UpdateMeta( p_input, VLC_FALSE );
1872 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1873 p_access->info.i_update &= ~INPUT_UPDATE_META;
1876 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1878 /* Hmmm only works with master input */
1879 if( p_input->input.p_access == p_access )
1881 int i_title_end = p_input->input.i_title_end -
1882 p_input->input.i_title_offset;
1883 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1884 p_input->input.i_seekpoint_offset;
1886 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1888 if( p_access->info.i_title > i_title_end ||
1889 ( p_access->info.i_title == i_title_end &&
1890 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1892 else if( i_seekpoint_end >=0 )
1894 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1896 else if( i_title_end >= 0 )
1898 if( p_access->info.i_title > i_title_end ) return 0;
1905 /*****************************************************************************
1907 *****************************************************************************/
1908 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1910 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1914 if( p_meta->psz_title && !p_input->input.p_item->b_fixed_name )
1915 input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1917 /** \todo handle sout meta */
1922 /*****************************************************************************
1924 *****************************************************************************/
1925 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1926 vlc_bool_t b_quick )
1928 char psz_buffer[MSTRTIME_MAX_SIZE];
1930 vlc_mutex_lock( &p_input->input.p_item->lock );
1931 p_input->input.p_item->i_duration = i_length;
1932 vlc_mutex_unlock( &p_input->input.p_item->lock );
1934 pl_Yield( p_input );
1935 var_SetInteger( pl_Get( p_input ), "item-change",
1936 p_input->input.p_item->i_id );
1937 pl_Release( p_input )
1939 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1940 msecstotimestr( psz_buffer, i_length / 1000 ) );
1943 /*****************************************************************************
1945 *****************************************************************************/
1946 static input_source_t *InputSourceNew( input_thread_t *p_input )
1948 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1952 msg_Err( p_input, "out of memory for new input source" );
1957 in->p_access = NULL;
1958 in->p_stream = NULL;
1960 in->b_title_demux = VLC_FALSE;
1963 in->b_can_pace_control = VLC_TRUE;
1964 in->b_eof = VLC_FALSE;
1965 in->i_cr_average = 0;
1970 /*****************************************************************************
1972 *****************************************************************************/
1973 static int InputSourceInit( input_thread_t *p_input,
1974 input_source_t *in, char *psz_mrl,
1975 char *psz_forced_demux, vlc_bool_t b_quick )
1977 char *psz_dup = strdup( psz_mrl );
1985 if( !in ) return VLC_EGENERIC;
1990 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1991 &psz_access, &psz_demux, &psz_path );
1993 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1994 psz_mrl, psz_access, psz_demux, psz_path );
1996 /* Hack to allow udp://@:port syntax */
1998 (strncmp( psz_access, "udp", 3 ) &&
1999 strncmp( psz_access, "rtp", 3 )) )
2001 /* Find optional titles and seekpoints */
2002 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2003 &in->i_seekpoint_start, &in->i_seekpoint_end );
2005 if( psz_forced_demux && *psz_forced_demux )
2007 psz_demux = psz_forced_demux;
2009 else if( !psz_demux || *psz_demux == '\0' )
2011 /* special hack for forcing a demuxer with --demux=module
2012 * (and do nothing with a list) */
2013 char *psz_var_demux = var_GetString( p_input, "demux" );
2015 if( *psz_var_demux != '\0' &&
2016 !strchr(psz_var_demux, ',' ) &&
2017 !strchr(psz_var_demux, ':' ) )
2019 psz_demux = psz_var_demux;
2021 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2023 else if( psz_var_demux )
2025 free( psz_var_demux );
2029 /* Try access_demux if no demux given */
2030 if( *psz_demux == '\0' )
2032 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2033 NULL, p_input->p_es_out, VLC_FALSE );
2039 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2040 psz_demux = strdup( "" );
2041 psz_access = strdup( "file" );
2046 int64_t i_pts_delay;
2048 /* Get infos from access_demux */
2049 demux2_Control( in->p_demux,
2050 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2051 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2053 in->b_title_demux = VLC_TRUE;
2054 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2055 &in->title, &in->i_title,
2056 &in->i_title_offset, &in->i_seekpoint_offset ) )
2061 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2062 &in->b_can_pace_control );
2063 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2067 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2073 int64_t i_pts_delay;
2075 input_ChangeState( p_input, OPENING_S);
2077 /* Now try a real access */
2078 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2081 /* Access failed, URL encoded ? */
2082 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2084 DecodeUrl( psz_path );
2086 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2087 psz_access, psz_demux, psz_path );
2089 in->p_access = access2_New( p_input,
2090 psz_access, psz_demux, psz_path,
2093 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2094 * are forbidden in filenames on Win32. */
2096 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2097 if( in->p_access == NULL &&
2098 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2100 if( psz_dup ) free( psz_dup );
2101 psz_dup = strdup( psz_mrl );
2103 if( psz_forced_demux && *psz_forced_demux )
2105 psz_demux = psz_forced_demux;
2107 else psz_demux = "";
2110 in->p_access = access2_New( p_input,
2111 psz_access, psz_demux, psz_path,
2116 if( in->p_access == NULL )
2118 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2119 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2120 _("Your input can't be opened"),
2121 _("VLC is unable to open the MRL '%s'."
2122 " Check the log for details."), psz_mrl );
2127 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2128 while( psz && *psz )
2130 access_t *p_access = in->p_access;
2131 char *end = strchr( psz, ':' );
2136 in->p_access = access2_FilterNew( in->p_access, psz );
2137 if( in->p_access == NULL )
2139 in->p_access = p_access;
2140 msg_Warn( p_input, "failed to insert access filter %s",
2146 if( psz_tmp ) free( psz_tmp );
2148 /* Get infos from access */
2151 access2_Control( in->p_access,
2152 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2153 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2155 in->b_title_demux = VLC_FALSE;
2156 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2157 &in->title, &in->i_title,
2158 &in->i_title_offset, &in->i_seekpoint_offset ) )
2164 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2165 &in->b_can_pace_control );
2166 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2168 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2170 var_Set( p_input, "seekable", val );
2173 input_ChangeState( p_input, BUFFERING_S);
2175 /* Create the stream_t */
2176 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2177 if( in->p_stream == NULL )
2179 msg_Warn( p_input, "cannot create a stream_t from access" );
2183 /* Open a demuxer */
2184 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2186 psz_demux = in->p_access->psz_demux;
2188 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2189 in->p_stream, p_input->p_es_out, b_quick );
2190 if( in->p_demux == NULL )
2192 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2193 psz_access, psz_demux, psz_path );
2194 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2195 _("Can't recognize the input's format"),
2196 _("The format of '%s' can't be detected. "
2197 "Have a look the log for details."), psz_mrl );
2201 /* TODO get title from demux */
2202 if( !b_quick && in->i_title <= 0 )
2204 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2205 &in->title, &in->i_title,
2206 &in->i_title_offset, &in->i_seekpoint_offset ))
2213 in->b_title_demux = VLC_TRUE;
2218 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2219 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2221 if( psz_dup ) free( psz_dup );
2226 demux2_Delete( in->p_demux );
2229 stream_Delete( in->p_stream );
2232 access2_Delete( in->p_access );
2233 if( psz_dup ) free( psz_dup );
2235 return VLC_EGENERIC;
2238 /*****************************************************************************
2240 *****************************************************************************/
2241 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2244 demux2_Delete( in->p_demux );
2247 stream_Delete( in->p_stream );
2250 access2_Delete( in->p_access );
2252 if( in->i_title > 0 )
2255 for( i = 0; i < in->i_title; i++ )
2257 vlc_input_title_Delete( in->title[i] );
2263 static void SlaveDemux( input_thread_t *p_input )
2268 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2270 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2274 for( i = 0; i < p_input->i_slave; i++ )
2276 input_source_t *in = p_input->slave[i];
2282 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2287 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2289 msg_Err( p_input, "slave[%d] doesn't like "
2290 "DEMUX_GET_TIME -> EOF", i );
2295 if( i_stime >= i_time )
2298 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2304 i_ret = in->p_demux->pf_demux( in->p_demux );
2309 msg_Dbg( p_input, "slave %d EOF", i );
2310 in->b_eof = VLC_TRUE;
2315 static void SlaveSeek( input_thread_t *p_input )
2320 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2322 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2326 for( i = 0; i < p_input->i_slave; i++ )
2328 input_source_t *in = p_input->slave[i];
2330 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2332 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2333 in->b_eof = VLC_TRUE;
2337 /*****************************************************************************
2339 *****************************************************************************/
2340 static void InputMetaUser( input_thread_t *p_input )
2342 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
2345 if( !p_meta ) return;
2347 /* Get meta information from user */
2348 #define GET_META( field, s ) \
2349 var_Get( p_input, (s), &val ); \
2350 if( *val.psz_string ) { \
2351 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2352 p_meta->psz_ ## field = strdup( val.psz_string ); \
2354 free( val.psz_string )
2356 GET_META( title, "meta-title" );
2357 GET_META( author, "meta-author" );
2358 GET_META( artist, "meta-artist" );
2359 GET_META( genre, "meta-genre" );
2360 GET_META( copyright, "meta-copyright" );
2361 GET_META( description, "meta-description" );
2362 GET_META( date, "meta-date" );
2363 GET_META( url, "meta-url" );
2367 /*****************************************************************************
2368 * DecodeUrl: decode a given encoded url
2369 *****************************************************************************/
2370 static void DecodeUrl( char *psz )
2372 char *dup = strdup( psz );
2390 *psz++ = strtol( val, NULL, 16 );
2392 else if( *p == '+' )
2402 if( psz ) *psz++ ='\0';
2403 if( dup ) free( dup );
2406 /*****************************************************************************
2407 * MRLSplit: parse the access, demux and url part of the
2408 * Media Resource Locator.
2409 *****************************************************************************/
2410 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2411 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2413 char *psz_access = NULL;
2414 char *psz_demux = NULL;
2415 char *psz_path = NULL;
2416 char *psz, *psz_check;
2418 psz = strchr( psz_dup, ':' );
2420 /* '@' not allowed in access/demux part */
2421 psz_check = strchr( psz_dup, '@' );
2422 if( psz_check && psz_check < psz ) psz = 0;
2424 #if defined( WIN32 ) || defined( UNDER_CE )
2425 if( psz - psz_dup == 1 )
2427 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2436 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2440 psz = strchr( psz_dup, '/' );
2447 psz_access = psz_dup;
2454 if( !psz_access ) *ppsz_access = "";
2455 else *ppsz_access = psz_access;
2457 if( !psz_demux ) *ppsz_demux = "";
2458 else *ppsz_demux = psz_demux;
2460 if( !psz_path ) *ppsz_path = "";
2461 else *ppsz_path = psz_path;
2464 /*****************************************************************************
2465 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2468 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2469 *****************************************************************************/
2470 static void MRLSections( input_thread_t *p_input, char *psz_source,
2471 int *pi_title_start, int *pi_title_end,
2472 int *pi_chapter_start, int *pi_chapter_end )
2474 char *psz, *psz_end, *psz_next, *psz_check;
2476 *pi_title_start = *pi_title_end = -1;
2477 *pi_chapter_start = *pi_chapter_end = -1;
2479 /* Start by parsing titles and chapters */
2480 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2482 /* Check we are really dealing with a title/chapter section */
2483 psz_check = psz + 1;
2484 if( !*psz_check ) return;
2485 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2486 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2487 if( *psz_check == ':' && ++psz_check )
2488 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2489 if( *psz_check != '-' && *psz_check ) return;
2490 if( *psz_check == '-' && ++psz_check )
2491 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2492 if( *psz_check != ':' && *psz_check ) return;
2493 if( *psz_check == ':' && ++psz_check )
2494 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2495 if( *psz_check ) return;
2497 /* Separate start and end */
2499 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2501 /* Look for the start title */
2502 *pi_title_start = strtol( psz, &psz_next, 0 );
2503 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2504 *pi_title_end = *pi_title_start;
2507 /* Look for the start chapter */
2509 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2510 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2511 *pi_chapter_end = *pi_chapter_start;
2515 /* Look for the end title */
2516 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2517 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2520 /* Look for the end chapter */
2521 if( *psz_end ) psz_end++;
2522 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2523 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2526 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2527 psz_source, *pi_title_start, *pi_chapter_start,
2528 *pi_title_end, *pi_chapter_end );
2531 /*****************************************************************************
2532 * input_AddSubtitles: add a subtitles file and enable it
2533 *****************************************************************************/
2534 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2535 vlc_bool_t b_check_extension )
2537 input_source_t *sub;
2540 char *psz_path, *psz_extension;
2542 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2547 /* if we are provided a subtitle.sub file,
2548 * see if we don't have a subtitle.idx and use it instead */
2549 psz_path = strdup( psz_subtitle );
2552 psz_extension = strrchr( psz_path, '.');
2553 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2557 strcpy( psz_extension, ".idx" );
2558 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2559 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2562 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2563 psz_path, psz_subtitle );
2564 strcpy( psz_subtitle, psz_path );
2570 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2572 sub = InputSourceNew( p_input );
2573 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle", VLC_FALSE ) )
2575 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
2578 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2580 if( count.i_int == 0 )
2582 /* if it was first one, there is disable too */
2584 if( count.i_int < list.p_list->i_count )
2586 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2587 &list.p_list->p_values[count.i_int] );
2589 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );