1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
33 #include <vlc/decoder.h>
36 #include "input_internal.h"
38 #include "stream_output.h"
40 #include "vlc_interface.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Run ( input_thread_t *p_input );
48 static int Init ( input_thread_t *p_input );
49 static void Error( input_thread_t *p_input );
50 static void End ( input_thread_t *p_input );
52 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
53 static void ControlReduce( input_thread_t * );
54 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
57 static int UpdateFromAccess( input_thread_t * );
58 static int UpdateFromDemux( input_thread_t * );
60 static void UpdateItemLength( input_thread_t *, int64_t i_length );
62 static void ParseOption( input_thread_t *p_input, const char *psz_option );
64 static void DecodeUrl ( char * );
65 static void MRLSplit( input_thread_t *, char *, char **, char **, char ** );
66 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
68 static input_source_t *InputSourceNew( input_thread_t *);
69 static int InputSourceInit( input_thread_t *, input_source_t *,
70 char *, char *psz_forced_demux );
71 static void InputSourceClean( input_thread_t *, input_source_t * );
73 static void SlaveDemux( input_thread_t *p_input );
74 static void SlaveSeek( input_thread_t *p_input );
76 static vlc_meta_t *InputMetaUser( input_thread_t *p_input );
78 /*****************************************************************************
79 * input_CreateThread: creates a new input thread
80 *****************************************************************************
81 * This function creates a new input, and returns a pointer
82 * to its description. On error, it returns NULL.
84 * Variables for _public_ use:
87 * - rate,rate-slower, rate-faster
88 * - position, position-offset
90 * - title,title-next,title-prev
91 * - chapter,chapter-next, chapter-prev
92 * - program, audio-es, video-es, spu-es
93 * - audio-delay, spu-delay
98 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
99 * * For intf callback upon changes
101 * TODO explain when Callback is called
102 * TODO complete this list (?)
103 *****************************************************************************/
104 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
105 input_item_t *p_item )
108 input_thread_t *p_input; /* thread descriptor */
112 /* Allocate descriptor */
113 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
114 if( p_input == NULL )
116 msg_Err( p_parent, "out of memory" );
120 /* Init Common fields */
121 p_input->b_eof = VLC_FALSE;
122 p_input->b_can_pace_control = VLC_TRUE;
123 p_input->i_start = 0;
126 p_input->i_title = 0;
127 p_input->title = NULL;
128 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
129 p_input->i_state = INIT_S;
130 p_input->i_rate = INPUT_RATE_DEFAULT;
131 p_input->i_bookmark = 0;
132 p_input->bookmark = NULL;
133 p_input->p_es_out = NULL;
134 p_input->p_sout = NULL;
135 p_input->b_out_pace_control = VLC_FALSE;
136 p_input->i_pts_delay = 0;
138 /* Init Input fields */
139 p_input->input.p_item = p_item;
140 p_input->input.p_access = NULL;
141 p_input->input.p_stream = NULL;
142 p_input->input.p_demux = NULL;
143 p_input->input.b_title_demux = VLC_FALSE;
144 p_input->input.i_title = 0;
145 p_input->input.title = NULL;
146 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
147 p_input->input.b_can_pace_control = VLC_TRUE;
148 p_input->input.b_eof = VLC_FALSE;
149 p_input->input.i_cr_average = 0;
152 p_input->i_slave = 0;
153 p_input->slave = NULL;
155 /* Init control buffer */
156 vlc_mutex_init( p_input, &p_input->lock_control );
157 p_input->i_control = 0;
159 /* Parse input options */
160 vlc_mutex_lock( &p_item->lock );
161 for( i = 0; i < p_item->i_options; i++ )
163 msg_Dbg( p_input, "option: %s", p_item->ppsz_options[i] );
164 ParseOption( p_input, p_item->ppsz_options[i] );
166 vlc_mutex_unlock( &p_item->lock );
168 /* Create Object Variables for private use only */
169 input_ConfigVarInit( p_input );
171 /* Create Objects variables for public Get and Set */
172 input_ControlVarInit( p_input );
173 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
176 var_Get( p_input, "bookmarks", &val );
179 /* FIXME: have a common cfg parsing routine used by sout and others */
180 char *psz_parser, *psz_start, *psz_end;
181 psz_parser = val.psz_string;
182 while( (psz_start = strchr( psz_parser, '{' ) ) )
184 seekpoint_t seekpoint;
187 psz_end = strchr( psz_start, '}' );
188 if( !psz_end ) break;
189 psz_parser = psz_end + 1;
190 backup = *psz_parser;
194 seekpoint.psz_name = 0;
195 seekpoint.i_byte_offset = 0;
196 seekpoint.i_time_offset = 0;
197 while( (psz_end = strchr( psz_start, ',' ) ) )
200 if( !strncmp( psz_start, "name=", 5 ) )
202 seekpoint.psz_name = psz_start + 5;
204 else if( !strncmp( psz_start, "bytes=", 6 ) )
206 seekpoint.i_byte_offset = atoll(psz_start + 6);
208 else if( !strncmp( psz_start, "time=", 5 ) )
210 seekpoint.i_time_offset = atoll(psz_start + 5) * 1000000;
212 psz_start = psz_end + 1;
214 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
215 seekpoint.psz_name, seekpoint.i_byte_offset,
216 seekpoint.i_time_offset );
217 input_Control( p_input, INPUT_ADD_BOOKMARK, &seekpoint );
218 *psz_parser = backup;
220 free( val.psz_string );
223 /* Now we can attach our new input */
224 vlc_object_attach( p_input, p_parent );
226 /* Create thread and wait for its readiness. */
227 if( vlc_thread_create( p_input, "input", Run,
228 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
230 msg_Err( p_input, "cannot create input thread" );
231 vlc_object_detach( p_input );
232 vlc_object_destroy( p_input );
239 /*****************************************************************************
240 * input_StopThread: mark an input thread as zombie
241 *****************************************************************************
242 * This function should not return until the thread is effectively cancelled.
243 *****************************************************************************/
244 void input_StopThread( input_thread_t *p_input )
249 /* Set die for input */
250 p_input->b_die = VLC_TRUE;
252 /* We cannot touch p_input fields directly (we can from another thread),
253 * so use the vlc_object_find way, it's perfectly safe */
255 /* Set die for all access */
256 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
257 for( i = 0; i < p_list->i_count; i++ )
259 p_list->p_values[i].p_object->b_die = VLC_TRUE;
261 vlc_list_release( p_list );
263 /* Set die for all stream */
264 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
265 for( i = 0; i < p_list->i_count; i++ )
267 p_list->p_values[i].p_object->b_die = VLC_TRUE;
269 vlc_list_release( p_list );
271 /* Set die for all demux */
272 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
273 for( i = 0; i < p_list->i_count; i++ )
275 p_list->p_values[i].p_object->b_die = VLC_TRUE;
277 vlc_list_release( p_list );
279 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
282 /*****************************************************************************
283 * input_DestroyThread: mark an input thread as zombie
284 *****************************************************************************
285 * This function should not return until the thread is effectively cancelled.
286 *****************************************************************************/
287 void input_DestroyThread( input_thread_t *p_input )
289 /* Join the thread */
290 vlc_thread_join( p_input );
292 /* Delete input lock (only after thread joined) */
293 vlc_mutex_destroy( &p_input->lock_control );
295 /* TODO: maybe input_DestroyThread should also delete p_input instead
296 * of the playlist but I'm not sure if it's possible */
299 /*****************************************************************************
300 * Run: main thread loop
301 *****************************************************************************
302 * Thread in charge of processing the network packets and demultiplexing.
305 * read subtitle support (XXX take care of spu-delay in the right way).
306 * multi-input support (XXX may be done with subs)
307 *****************************************************************************/
308 static int Run( input_thread_t *p_input )
310 int64_t i_intf_update = 0;
312 /* Signal that the thread is launched */
313 vlc_thread_ready( p_input );
315 if( Init( p_input ) )
317 /* If we failed, wait before we are killed, and exit */
318 p_input->b_error = VLC_TRUE;
322 /* Tell we're dead */
323 p_input->b_dead = VLC_TRUE;
329 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
331 vlc_bool_t b_force_update = VLC_FALSE;
337 if( p_input->i_state != PAUSE_S )
339 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
340 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
347 if( p_input->input.b_title_demux &&
348 p_input->input.p_demux->info.i_update )
350 i_ret = UpdateFromDemux( p_input );
351 b_force_update = VLC_TRUE;
353 else if( !p_input->input.b_title_demux &&
354 p_input->input.p_access &&
355 p_input->input.p_access->info.i_update )
357 i_ret = UpdateFromAccess( p_input );
358 b_force_update = VLC_TRUE;
362 if( i_ret == 0 ) /* EOF */
366 var_Get( p_input, "input-repeat", &repeat );
367 if( repeat.i_int == 0 )
369 /* End of file - we do not set b_die because only the
370 * playlist is allowed to do so. */
371 msg_Dbg( p_input, "EOF reached" );
372 p_input->input.b_eof = VLC_TRUE;
376 msg_Dbg( p_input, "repeating the same input (%d)",
378 if( repeat.i_int > 0 )
381 var_Set( p_input, "input-repeat", repeat );
384 /* Seek to start title/seekpoint */
385 val.i_int = p_input->input.i_title_start -
386 p_input->input.i_title_offset;
387 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
389 input_ControlPush( p_input,
390 INPUT_CONTROL_SET_TITLE, &val );
392 val.i_int = p_input->input.i_seekpoint_start -
393 p_input->input.i_seekpoint_offset;
394 if( val.i_int > 0 /* TODO: check upper boundary */ )
395 input_ControlPush( p_input,
396 INPUT_CONTROL_SET_SEEKPOINT, &val );
398 /* Seek to start position */
399 if( p_input->i_start > 0 )
401 val.i_time = p_input->i_start;
402 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
408 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
415 p_input->b_error = VLC_TRUE;
418 if( i_ret > 0 && p_input->i_slave > 0 )
420 SlaveDemux( p_input );
430 vlc_mutex_lock( &p_input->lock_control );
431 ControlReduce( p_input );
432 while( !ControlPopNoLock( p_input, &i_type, &val ) )
434 msg_Dbg( p_input, "control type=%d", i_type );
435 if( Control( p_input, i_type, val ) )
436 b_force_update = VLC_TRUE;
438 vlc_mutex_unlock( &p_input->lock_control );
440 if( b_force_update || i_intf_update < mdate() )
444 int64_t i_time, i_length;
445 /* update input status variables */
446 if( !demux2_Control( p_input->input.p_demux,
447 DEMUX_GET_POSITION, &f_pos ) )
449 val.f_float = (float)f_pos;
450 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
452 if( !demux2_Control( p_input->input.p_demux,
453 DEMUX_GET_TIME, &i_time ) )
455 p_input->i_time = i_time;
457 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
459 if( !demux2_Control( p_input->input.p_demux,
460 DEMUX_GET_LENGTH, &i_length ) )
463 var_Get( p_input, "length", &old_val );
464 val.i_time = i_length;
465 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
467 if( old_val.i_time != val.i_time )
469 UpdateItemLength( p_input, i_length );
473 var_SetBool( p_input, "intf-change", VLC_TRUE );
474 i_intf_update = mdate() + I64C(150000);
478 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
480 /* We have finish to demux data but not to play them */
481 while( !p_input->b_die )
483 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
486 msg_Dbg( p_input, "waiting decoder fifos to empty" );
488 msleep( INPUT_IDLE_SLEEP );
491 /* We have finished */
492 p_input->b_eof = VLC_TRUE;
495 /* Wait we are asked to die */
496 if( !p_input->b_die )
507 /*****************************************************************************
508 * Init: init the input Thread
509 *****************************************************************************/
510 static int Init( input_thread_t * p_input )
516 vlc_meta_t *p_meta, *p_meta_user;
519 /* Initialize optional stream output. (before access/demuxer) */
520 psz = var_GetString( p_input, "sout" );
523 p_input->p_sout = sout_NewInstance( p_input, psz );
524 if( p_input->p_sout == NULL )
526 msg_Err( p_input, "cannot start stream output instance, aborting" );
534 p_input->p_es_out = input_EsOutNew( p_input );
535 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
536 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
538 if( InputSourceInit( p_input, &p_input->input,
539 p_input->input.p_item->psz_uri, NULL ) )
544 /* Create global title (from master) */
545 p_input->i_title = p_input->input.i_title;
546 p_input->title = p_input->input.title;
547 p_input->i_title_offset = p_input->input.i_title_offset;
548 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
549 if( p_input->i_title > 0 )
551 /* Setup variables */
552 input_ControlVarNavigation( p_input );
553 input_ControlVarTitle( p_input, 0 );
557 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
558 p_input->b_can_pause = p_input->input.b_can_pause;
561 if( p_input->i_pts_delay <= 0 )
562 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
564 /* If the desynchronisation requested by the user is < 0, we need to
565 * cache more data. */
566 var_Get( p_input, "audio-desync", &val );
567 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
569 /* Update cr_average depending on the caching */
570 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
571 p_input->input.i_cr_average /= 10;
572 if( p_input->input.i_cr_average <= 0 ) p_input->input.i_cr_average = 1;
574 /* Load master infos */
576 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
577 &val.i_time ) && val.i_time > 0 )
579 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
581 UpdateItemLength( p_input, val.i_time );
584 /* Start title/chapter */
585 val.i_int = p_input->input.i_title_start -
586 p_input->input.i_title_offset;
587 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
588 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
589 val.i_int = p_input->input.i_seekpoint_start -
590 p_input->input.i_seekpoint_offset;
591 if( val.i_int > 0 /* TODO: check upper boundary */ )
592 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
596 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
598 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
601 if( p_input->i_start > 0 )
603 if( p_input->i_start >= val.i_time )
605 msg_Warn( p_input, "invalid start-time ignored" );
611 msg_Dbg( p_input, "start-time: %ds",
612 (int)( p_input->i_start / I64C(1000000) ) );
614 s.i_time = p_input->i_start;
615 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
618 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
620 msg_Warn( p_input, "invalid stop-time ignored" );
626 /* Get fps and set it if not already set */
627 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
632 if( var_Get( p_input, "sub-fps", &fps ) )
634 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT| VLC_VAR_DOINHERIT );
635 var_SetFloat( p_input, "sub-fps", f_fps );
639 /* Look for and add subtitle files */
640 psz_subtitle = var_GetString( p_input, "sub-file" );
647 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
649 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
652 sub = InputSourceNew( p_input );
653 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
655 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
658 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
660 if( count.i_int == 0 )
661 count.i_int++; /* if it was first one, there is disable too */
663 if( count.i_int < list.p_list->i_count )
665 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
666 &list.p_list->p_values[count.i_int] );
668 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
673 var_Get( p_input, "sub-autodetect-file", &val );
676 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
677 char **subs = subtitles_Detect( p_input, psz_autopath,
678 p_input->input.p_item->psz_uri );
681 for( i = 0; subs[i] != NULL; i++ )
683 if( strcmp( psz_subtitle, subs[i] ) )
685 sub = InputSourceNew( p_input );
686 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
688 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
694 free( psz_autopath );
696 free( psz_subtitle );
699 psz = var_GetString( p_input, "input-slave" );
702 char *psz_delim = strchr( psz, '#' );
706 input_source_t *slave;
721 msg_Dbg( p_input, "adding slave '%s'", psz );
722 slave = InputSourceNew( p_input );
723 if( !InputSourceInit( p_input, slave, psz, NULL ) )
725 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
734 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
735 val.b_bool = VLC_FALSE;
736 if( p_input->p_sout )
738 var_Get( p_input, "sout-all", &val );
740 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
741 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
743 /* Inform the demuxer about waited group (needed only for DVB) */
745 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1 );
747 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
748 (int) var_GetInteger( p_input, "program" ) );
750 if( p_input->p_sout )
752 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
754 p_input->b_out_pace_control = VLC_FALSE;
758 p_input->b_out_pace_control = VLC_TRUE;
760 msg_Dbg( p_input, "starting in %s mode",
761 p_input->b_out_pace_control ? "asynch" : "synch" );
764 /* Get meta data from users */
765 p_meta_user = InputMetaUser( p_input );
767 /* Get meta data from master input */
768 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
774 p_meta = p_meta_user;
776 else if( p_meta_user )
778 vlc_meta_Merge( p_meta, p_meta_user );
779 vlc_meta_Delete( p_meta_user );
782 /* Get meta data from slave input */
783 for( i = 0; i < p_input->i_slave; i++ )
785 vlc_meta_t *p_meta_slave;
787 if( !demux2_Control( p_input->slave[i]->p_demux, DEMUX_GET_META, &p_meta_slave ) )
791 p_meta = p_meta_slave;
793 else if( p_meta_slave )
795 vlc_meta_Merge( p_meta, p_meta_slave );
796 vlc_meta_Delete( p_meta_slave );
801 if( p_meta && p_meta->i_meta > 0 )
803 msg_Dbg( p_input, "meta information:" );
804 for( i = 0; i < p_meta->i_meta; i++ )
806 msg_Dbg( p_input, " - '%s' = '%s'",
807 _(p_meta->name[i]), p_meta->value[i] );
809 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] )
810 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
812 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
813 input_Control( p_input, INPUT_ADD_INFO, _("General"),
814 _("Author"), p_meta->value[i] );
816 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
817 _(p_meta->name[i]), "%s", p_meta->value[i] );
820 for( i = 0; i < p_meta->i_track; i++ )
822 vlc_meta_t *tk = p_meta->track[i];
827 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
829 msg_Dbg( p_input, " - track[%d]:", i );
831 sprintf( psz_cat, "%s %d", _("Stream"), i );
832 for( j = 0; j < tk->i_meta; j++ )
834 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
837 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
838 _(tk->name[j]), "%s", tk->value[j] );
843 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
845 p_input->p_sout->p_meta = p_meta;
849 vlc_meta_Delete( p_meta );
852 else if( p_meta ) vlc_meta_Delete( p_meta );
854 msg_Dbg( p_input, "`%s' sucessfully opened",
855 p_input->input.p_item->psz_uri );
857 /* initialization is complete */
858 p_input->i_state = PLAYING_S;
860 val.i_int = PLAYING_S;
861 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
866 if( p_input->p_es_out )
867 input_EsOutDelete( p_input->p_es_out );
869 if( p_input->p_sout )
870 sout_DeleteInstance( p_input->p_sout );
872 /* Mark them deleted */
873 p_input->input.p_demux = NULL;
874 p_input->input.p_stream = NULL;
875 p_input->input.p_access = NULL;
876 p_input->p_es_out = NULL;
877 p_input->p_sout = NULL;
882 /*****************************************************************************
883 * Error: RunThread() error loop
884 *****************************************************************************
885 * This function is called when an error occurred during thread main's loop.
886 *****************************************************************************/
887 static void Error( input_thread_t *p_input )
889 while( !p_input->b_die )
892 msleep( INPUT_IDLE_SLEEP );
896 /*****************************************************************************
897 * End: end the input thread
898 *****************************************************************************/
899 static void End( input_thread_t * p_input )
904 msg_Dbg( p_input, "closing input" );
906 /* We are at the end */
907 p_input->i_state = END_S;
910 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
912 /* Clean control variables */
913 input_ControlVarClean( p_input );
915 /* Clean up master */
916 InputSourceClean( p_input, &p_input->input );
919 for( i = 0; i < p_input->i_slave; i++ )
921 InputSourceClean( p_input, p_input->slave[i] );
922 free( p_input->slave[i] );
924 if( p_input->slave ) free( p_input->slave );
926 /* Unload all modules */
927 if( p_input->p_es_out )
928 input_EsOutDelete( p_input->p_es_out );
930 /* Close optional stream output instance */
931 if( p_input->p_sout )
934 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
937 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
939 /* attach sout to the playlist */
940 msg_Warn( p_input, "keeping sout" );
941 vlc_object_detach( p_input->p_sout );
942 vlc_object_attach( p_input->p_sout, p_pl );
946 msg_Warn( p_input, "destroying sout" );
947 sout_DeleteInstance( p_input->p_sout );
950 vlc_object_release( p_pl );
953 /* Tell we're dead */
954 p_input->b_dead = VLC_TRUE;
957 /*****************************************************************************
959 *****************************************************************************/
960 static inline int ControlPopNoLock( input_thread_t *p_input,
961 int *pi_type, vlc_value_t *p_val )
963 if( p_input->i_control <= 0 )
968 *pi_type = p_input->control[0].i_type;
969 *p_val = p_input->control[0].val;
971 p_input->i_control--;
972 if( p_input->i_control > 0 )
976 for( i = 0; i < p_input->i_control; i++ )
978 p_input->control[i].i_type = p_input->control[i+1].i_type;
979 p_input->control[i].val = p_input->control[i+1].val;
986 static void ControlReduce( input_thread_t *p_input )
989 for( i = 1; i < p_input->i_control; i++ )
991 const int i_lt = p_input->control[i-1].i_type;
992 const int i_ct = p_input->control[i].i_type;
994 /* XXX We can't merge INPUT_CONTROL_SET_ES */
995 msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
998 ( i_ct == INPUT_CONTROL_SET_STATE ||
999 i_ct == INPUT_CONTROL_SET_RATE ||
1000 i_ct == INPUT_CONTROL_SET_POSITION ||
1001 i_ct == INPUT_CONTROL_SET_TIME ||
1002 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1003 i_ct == INPUT_CONTROL_SET_TITLE ||
1004 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1005 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1008 msg_Dbg( p_input, "merged at %d", i );
1009 /* Remove the i-1 */
1010 for( j = i; j < p_input->i_control; j++ )
1011 p_input->control[j-1] = p_input->control[j];
1012 p_input->i_control--;
1016 /* TODO but that's not that important
1017 - merge SET_X with SET_X_CMD
1018 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1019 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1026 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1029 vlc_bool_t b_force_update = VLC_FALSE;
1033 case INPUT_CONTROL_SET_DIE:
1034 msg_Dbg( p_input, "control: INPUT_CONTROL_SET_DIE proceed" );
1035 /* Mark all submodules to die */
1036 if( p_input->input.p_access )
1037 p_input->input.p_access->b_die = VLC_TRUE;
1038 if( p_input->input.p_stream )
1039 p_input->input.p_stream->b_die = VLC_TRUE;
1040 p_input->input.p_demux->b_die = VLC_TRUE;
1042 p_input->b_die = VLC_TRUE;
1045 case INPUT_CONTROL_SET_POSITION:
1046 case INPUT_CONTROL_SET_POSITION_OFFSET:
1049 if( i_type == INPUT_CONTROL_SET_POSITION )
1051 f_pos = val.f_float;
1055 /* Should not fail */
1056 demux2_Control( p_input->input.p_demux,
1057 DEMUX_GET_POSITION, &f_pos );
1058 f_pos += val.f_float;
1060 if( f_pos < 0.0 ) f_pos = 0.0;
1061 if( f_pos > 1.0 ) f_pos = 1.0;
1062 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1065 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1066 "%2.1f%% failed", f_pos * 100 );
1070 if( p_input->i_slave > 0 )
1071 SlaveSeek( p_input );
1073 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1074 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1075 b_force_update = VLC_TRUE;
1080 case INPUT_CONTROL_SET_TIME:
1081 case INPUT_CONTROL_SET_TIME_OFFSET:
1086 if( i_type == INPUT_CONTROL_SET_TIME )
1088 i_time = val.i_time;
1092 /* Should not fail */
1093 demux2_Control( p_input->input.p_demux,
1094 DEMUX_GET_TIME, &i_time );
1095 i_time += val.i_time;
1097 if( i_time < 0 ) i_time = 0;
1098 i_ret = demux2_Control( p_input->input.p_demux,
1099 DEMUX_SET_TIME, i_time );
1103 /* Emulate it with a SET_POS */
1105 demux2_Control( p_input->input.p_demux,
1106 DEMUX_GET_LENGTH, &i_length );
1109 double f_pos = (double)i_time / (double)i_length;
1110 i_ret = demux2_Control( p_input->input.p_demux,
1111 DEMUX_SET_POSITION, f_pos );
1116 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1117 " failed", i_time );
1121 if( p_input->i_slave > 0 )
1122 SlaveSeek( p_input );
1124 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1126 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1127 b_force_update = VLC_TRUE;
1132 case INPUT_CONTROL_SET_STATE:
1133 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1134 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1137 if( p_input->input.p_access )
1138 i_ret = access2_Control( p_input->input.p_access,
1139 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1141 i_ret = demux2_Control( p_input->input.p_demux,
1142 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1146 /* FIXME What to do ? */
1147 msg_Warn( p_input, "cannot unset pause -> EOF" );
1148 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1151 b_force_update = VLC_TRUE;
1153 /* Switch to play */
1154 p_input->i_state = PLAYING_S;
1155 val.i_int = PLAYING_S;
1156 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1159 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1161 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1162 p_input->b_can_pause )
1165 if( p_input->input.p_access )
1166 i_ret = access2_Control( p_input->input.p_access,
1167 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1169 i_ret = demux2_Control( p_input->input.p_demux,
1170 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1172 b_force_update = VLC_TRUE;
1176 msg_Warn( p_input, "cannot set pause state" );
1177 val.i_int = p_input->i_state;
1181 val.i_int = PAUSE_S;
1184 /* Switch to new state */
1185 p_input->i_state = val.i_int;
1186 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1188 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1190 b_force_update = VLC_TRUE;
1192 /* Correct "state" value */
1193 val.i_int = p_input->i_state;
1194 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1196 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1198 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1202 case INPUT_CONTROL_SET_RATE:
1203 case INPUT_CONTROL_SET_RATE_SLOWER:
1204 case INPUT_CONTROL_SET_RATE_FASTER:
1208 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1209 i_rate = p_input->i_rate * 2;
1210 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1211 i_rate = p_input->i_rate / 2;
1215 if( i_rate < INPUT_RATE_MIN )
1217 msg_Dbg( p_input, "cannot set rate faster" );
1218 i_rate = INPUT_RATE_MIN;
1220 else if( i_rate > INPUT_RATE_MAX )
1222 msg_Dbg( p_input, "cannot set rate slower" );
1223 i_rate = INPUT_RATE_MAX;
1225 if( i_rate != INPUT_RATE_DEFAULT &&
1226 ( !p_input->b_can_pace_control ||
1227 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1229 msg_Dbg( p_input, "cannot change rate" );
1230 i_rate = INPUT_RATE_DEFAULT;
1232 if( i_rate != p_input->i_rate )
1234 p_input->i_rate = i_rate;
1236 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1238 /* We haven't send data to decoder when rate != default */
1239 if( i_rate == INPUT_RATE_DEFAULT )
1240 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1243 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1245 b_force_update = VLC_TRUE;
1250 case INPUT_CONTROL_SET_PROGRAM:
1251 /* No need to force update, es_out does it if needed */
1252 es_out_Control( p_input->p_es_out,
1253 ES_OUT_SET_GROUP, val.i_int );
1255 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int );
1258 case INPUT_CONTROL_SET_ES:
1259 /* No need to force update, es_out does it if needed */
1260 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1261 input_EsOutGetFromID( p_input->p_es_out,
1265 case INPUT_CONTROL_SET_AUDIO_DELAY:
1266 input_EsOutSetDelay( p_input->p_es_out,
1267 AUDIO_ES, val.i_time );
1268 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1271 case INPUT_CONTROL_SET_SPU_DELAY:
1272 input_EsOutSetDelay( p_input->p_es_out,
1273 SPU_ES, val.i_time );
1274 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1277 case INPUT_CONTROL_SET_TITLE:
1278 case INPUT_CONTROL_SET_TITLE_NEXT:
1279 case INPUT_CONTROL_SET_TITLE_PREV:
1280 if( p_input->input.b_title_demux &&
1281 p_input->input.i_title > 0 )
1284 /* FIXME handle demux title */
1285 demux_t *p_demux = p_input->input.p_demux;
1288 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1289 i_title = p_demux->info.i_title - 1;
1290 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1291 i_title = p_demux->info.i_title + 1;
1293 i_title = val.i_int;
1295 if( i_title >= 0 && i_title < p_input->input.i_title )
1297 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1299 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1300 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1302 input_ControlVarTitle( p_input, i_title );
1305 else if( p_input->input.i_title > 0 )
1307 access_t *p_access = p_input->input.p_access;
1310 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1311 i_title = p_access->info.i_title - 1;
1312 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1313 i_title = p_access->info.i_title + 1;
1315 i_title = val.i_int;
1317 if( i_title >= 0 && i_title < p_input->input.i_title )
1319 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1320 stream_AccessReset( p_input->input.p_stream );
1322 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1323 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1327 case INPUT_CONTROL_SET_SEEKPOINT:
1328 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1329 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1330 if( p_input->input.b_title_demux &&
1331 p_input->input.i_title > 0 )
1333 demux_t *p_demux = p_input->input.p_demux;
1336 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1337 i_seekpoint = p_demux->info.i_seekpoint - 1;
1338 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1339 i_seekpoint = p_demux->info.i_seekpoint + 1;
1341 i_seekpoint = val.i_int;
1343 if( i_seekpoint >= 0 && i_seekpoint <
1344 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1346 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1348 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1349 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1352 else if( p_input->input.i_title > 0 )
1354 access_t *p_access = p_input->input.p_access;
1357 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1358 i_seekpoint = p_access->info.i_seekpoint - 1;
1359 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1360 i_seekpoint = p_access->info.i_seekpoint + 1;
1362 i_seekpoint = val.i_int;
1364 if( i_seekpoint >= 0 && i_seekpoint <
1365 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1367 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1368 stream_AccessReset( p_input->input.p_stream );
1370 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1371 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1376 case INPUT_CONTROL_SET_BOOKMARK:
1378 msg_Err( p_input, "not yet implemented" );
1382 return b_force_update;
1385 /*****************************************************************************
1387 *****************************************************************************/
1388 static int UpdateFromDemux( input_thread_t *p_input )
1390 demux_t *p_demux = p_input->input.p_demux;
1393 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1395 v.i_int = p_demux->info.i_title;
1396 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1398 input_ControlVarTitle( p_input, p_demux->info.i_title );
1400 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1402 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1404 v.i_int = p_demux->info.i_seekpoint;
1405 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1407 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1409 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1411 /* Hmmm only works with master input */
1412 if( p_input->input.p_demux == p_demux )
1414 int i_title_end = p_input->input.i_title_end -
1415 p_input->input.i_title_offset;
1416 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1417 p_input->input.i_seekpoint_offset;
1419 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1421 if( p_demux->info.i_title > i_title_end ||
1422 ( p_demux->info.i_title == i_title_end &&
1423 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1425 else if( i_seekpoint_end >=0 )
1427 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1429 else if( i_title_end >= 0 )
1431 if( p_demux->info.i_title > i_title_end ) return 0;
1438 /*****************************************************************************
1440 *****************************************************************************/
1441 static int UpdateFromAccess( input_thread_t *p_input )
1443 access_t *p_access = p_input->input.p_access;
1446 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1448 v.i_int = p_access->info.i_title;
1449 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1451 input_ControlVarTitle( p_input, p_access->info.i_title );
1453 stream_AccessUpdate( p_input->input.p_stream );
1455 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1457 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1459 v.i_int = p_access->info.i_seekpoint;
1460 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1462 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1464 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1466 /* Hmmm only works with master input */
1467 if( p_input->input.p_access == p_access )
1469 int i_title_end = p_input->input.i_title_end -
1470 p_input->input.i_title_offset;
1471 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1472 p_input->input.i_seekpoint_offset;
1474 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1476 if( p_access->info.i_title > i_title_end ||
1477 ( p_access->info.i_title == i_title_end &&
1478 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1480 else if( i_seekpoint_end >=0 )
1482 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1484 else if( i_title_end >= 0 )
1486 if( p_access->info.i_title > i_title_end ) return 0;
1493 /*****************************************************************************
1495 *****************************************************************************/
1496 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
1498 char psz_buffer[MSTRTIME_MAX_SIZE];
1500 vlc_mutex_lock( &p_input->input.p_item->lock );
1501 p_input->input.p_item->i_duration = i_length;
1502 vlc_mutex_unlock( &p_input->input.p_item->lock );
1504 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1505 msecstotimestr( psz_buffer, i_length / 1000 ) );
1508 /*****************************************************************************
1510 *****************************************************************************/
1511 static input_source_t *InputSourceNew( input_thread_t *p_input )
1513 input_source_t *in = malloc( sizeof( input_source_t ) );
1516 in->p_access = NULL;
1517 in->p_stream = NULL;
1519 in->b_title_demux = VLC_FALSE;
1522 in->b_can_pace_control = VLC_TRUE;
1523 in->b_eof = VLC_FALSE;
1524 in->i_cr_average = 0;
1529 /*****************************************************************************
1531 *****************************************************************************/
1532 static int InputSourceInit( input_thread_t *p_input,
1533 input_source_t *in, char *psz_mrl,
1534 char *psz_forced_demux )
1536 char *psz_dup = strdup( psz_mrl );
1543 MRLSplit( p_input, psz_dup, &psz_access, &psz_demux, &psz_path );
1545 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1546 psz_mrl, psz_access, psz_demux, psz_path );
1548 /* Hack to allow udp://@:port syntax */
1550 (strncmp( psz_access, "udp", 3 ) && strncmp( psz_access, "rtp", 3 )) )
1552 /* Find optional titles and seekpoints */
1553 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1554 &in->i_seekpoint_start, &in->i_seekpoint_end );
1556 if( psz_forced_demux && *psz_forced_demux )
1557 psz_demux = psz_forced_demux;
1559 /* Try access_demux if no demux given */
1560 if( *psz_demux == '\0' )
1562 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1563 NULL, p_input->p_es_out );
1568 int64_t i_pts_delay;
1570 /* Get infos from access_demux */
1571 demux2_Control( in->p_demux,
1572 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1573 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1575 in->b_title_demux = VLC_TRUE;
1576 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1577 &in->title, &in->i_title,
1578 &in->i_title_offset, &in->i_seekpoint_offset ) )
1583 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1584 &in->b_can_pace_control );
1585 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1589 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1595 int64_t i_pts_delay;
1597 /* Now try a real access */
1598 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path );
1600 /* Access failed, URL encoded ? */
1601 if( in->p_access == NULL && strchr( psz_path, '%' ) )
1603 DecodeUrl( psz_path );
1605 msg_Dbg( p_input, "retying with access `%s' demux `%s' path `%s'",
1606 psz_access, psz_demux, psz_path );
1608 in->p_access = access2_New( p_input,
1609 psz_access, psz_demux, psz_path );
1611 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
1612 * are forbidden in filenames on Win32. */
1614 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1615 if( in->p_access == NULL &&
1616 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1619 psz_dup = strdup( psz_mrl );
1624 in->p_access = access2_New( p_input,
1625 psz_access, psz_demux, psz_path );
1629 if( in->p_access == NULL )
1631 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
1635 /* Get infos from access */
1636 access2_Control( in->p_access,
1637 ACCESS_GET_PTS_DELAY, &i_pts_delay );
1638 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1640 in->b_title_demux = VLC_FALSE;
1641 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
1642 &in->title, &in->i_title,
1643 &in->i_title_offset, &in->i_seekpoint_offset ) )
1649 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
1650 &in->b_can_pace_control );
1651 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
1653 access2_Control( in->p_access, ACCESS_CAN_SEEK,
1655 var_Set( p_input, "seekable", val );
1657 /* Create the stream_t */
1658 in->p_stream = stream_AccessNew( in->p_access );
1659 if( in->p_stream == NULL )
1661 msg_Warn( p_input, "cannot create a stream_t from access" );
1665 /* Open a demuxer */
1666 if( *psz_demux == '\0' && *in->p_access->psz_demux )
1668 psz_demux = in->p_access->psz_demux;
1670 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1671 in->p_stream, p_input->p_es_out );
1672 if( in->p_demux == NULL )
1674 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
1675 psz_access, psz_demux, psz_path );
1679 /* TODO get title from demux */
1680 if( in->i_title <= 0 )
1682 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1683 &in->title, &in->i_title,
1684 &in->i_title_offset, &in->i_seekpoint_offset ))
1691 in->b_title_demux = VLC_TRUE;
1700 demux2_Delete( in->p_demux );
1703 stream_AccessDelete( in->p_stream );
1706 access2_Delete( in->p_access );
1709 return VLC_EGENERIC;
1712 /*****************************************************************************
1714 *****************************************************************************/
1715 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
1718 demux2_Delete( in->p_demux );
1721 stream_AccessDelete( in->p_stream );
1724 access2_Delete( in->p_access );
1726 if( in->i_title > 0 )
1729 for( i = 0; i < in->i_title; i++ )
1731 vlc_input_title_Delete( in->title[i] );
1737 static void SlaveDemux( input_thread_t *p_input )
1741 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1743 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1747 for( i = 0; i < p_input->i_slave; i++ )
1749 input_source_t *in = p_input->slave[i];
1755 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
1760 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
1762 msg_Err( p_input, "slave[%d] doesn't like "
1763 "DEMUX_GET_TIME -> EOF", i );
1768 if( i_stime >= i_time )
1771 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
1777 i_ret = in->p_demux->pf_demux( in->p_demux );
1782 msg_Dbg( p_input, "slave %d EOF", i );
1783 in->b_eof = VLC_TRUE;
1788 static void SlaveSeek( input_thread_t *p_input )
1793 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1795 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1799 for( i = 0; i < p_input->i_slave; i++ )
1801 input_source_t *in = p_input->slave[i];
1803 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
1805 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
1806 in->b_eof = VLC_TRUE;
1810 /*****************************************************************************
1812 *****************************************************************************/
1813 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
1818 if( ( p_meta = vlc_meta_New() ) == NULL )
1821 /* Get meta information from user */
1822 #define GET_META( c, s ) \
1823 var_Get( p_input, (s), &val ); \
1824 if( *val.psz_string ) \
1825 vlc_meta_Add( p_meta, c, val.psz_string ); \
1826 free( val.psz_string )
1828 GET_META( VLC_META_TITLE, "meta-title" );
1829 GET_META( VLC_META_AUTHOR, "meta-author" );
1830 GET_META( VLC_META_ARTIST, "meta-artist" );
1831 GET_META( VLC_META_GENRE, "meta-genre" );
1832 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
1833 GET_META( VLC_META_DESCRIPTION, "meta-description" );
1834 GET_META( VLC_META_DATE, "meta-date" );
1835 GET_META( VLC_META_URL, "meta-url" );
1841 /*****************************************************************************
1842 * DecodeUrl: decode a given encoded url
1843 *****************************************************************************/
1844 static void DecodeUrl( char *psz )
1846 char *dup = strdup( psz );
1864 *psz++ = strtol( val, NULL, 16 );
1866 else if( *p == '+' )
1880 /*****************************************************************************
1881 * ParseOption: parses the options for the input
1882 *****************************************************************************
1883 * This function parses the input (config) options and creates their associated
1885 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1886 * bar is the value of the option.
1887 *****************************************************************************/
1888 static void ParseOption( input_thread_t *p_input, const char *psz_option )
1890 char *psz_name = (char *)psz_option;
1891 char *psz_value = strchr( psz_option, '=' );
1892 int i_name_len, i_type;
1893 vlc_bool_t b_isno = VLC_FALSE;
1896 if( psz_value ) i_name_len = psz_value - psz_option;
1897 else i_name_len = strlen( psz_option );
1899 /* It's too much of an hassle to remove the ':' when we parse
1900 * the cmd line :) */
1901 if( i_name_len && *psz_name == ':' )
1907 if( i_name_len == 0 ) return;
1909 psz_name = strndup( psz_name, i_name_len );
1910 if( psz_value ) psz_value++;
1912 i_type = config_GetType( p_input, psz_name );
1914 if( !i_type && !psz_value )
1916 /* check for "no-foo" or "nofoo" */
1917 if( !strncmp( psz_name, "no-", 3 ) )
1919 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1921 else if( !strncmp( psz_name, "no", 2 ) )
1923 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1925 else goto cleanup; /* Option doesn't exist */
1928 i_type = config_GetType( p_input, psz_name );
1930 if( !i_type ) goto cleanup; /* Option doesn't exist */
1932 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1934 if( ( i_type != VLC_VAR_BOOL ) &&
1935 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1937 /* Create the variable in the input object.
1938 * Children of the input object will be able to retreive this value
1939 * thanks to the inheritance property of the object variables. */
1940 var_Create( p_input, psz_name, i_type );
1945 val.b_bool = !b_isno;
1948 case VLC_VAR_INTEGER:
1949 val.i_int = atoi( psz_value );
1953 val.f_float = atof( psz_value );
1956 case VLC_VAR_STRING:
1957 case VLC_VAR_MODULE:
1959 case VLC_VAR_DIRECTORY:
1960 val.psz_string = psz_value;
1968 var_Set( p_input, psz_name, val );
1970 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1973 if( psz_name ) free( psz_name );
1977 /*****************************************************************************
1978 * MRLSplit: parse the access, demux and url part of the
1979 * Media Resource Locator.
1980 *****************************************************************************/
1981 static void MRLSplit( input_thread_t *p_input, char *psz_dup,
1982 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
1984 char *psz_access = NULL;
1985 char *psz_demux = NULL;
1986 char *psz_path = NULL;
1987 char *psz, *psz_check;
1989 psz = strchr( psz_dup, ':' );
1991 /* '@' not allowed in access/demux part */
1992 psz_check = strchr( psz_dup, '@' );
1993 if( psz_check && psz_check < psz ) psz = 0;
1995 #if defined( WIN32 ) || defined( UNDER_CE )
1996 if( psz - psz_dup == 1 )
1998 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2007 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2011 psz = strchr( psz_dup, '/' );
2018 psz_access = psz_dup;
2025 if( !psz_access ) *ppsz_access = "";
2026 else *ppsz_access = psz_access;
2028 if( !psz_demux ) *ppsz_demux = "";
2029 else *ppsz_demux = psz_demux;
2031 if( !psz_path ) *ppsz_path = "";
2032 else *ppsz_path = psz_path;
2035 /*****************************************************************************
2036 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2039 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2040 *****************************************************************************/
2041 static void MRLSections( input_thread_t *p_input, char *psz_source,
2042 int *pi_title_start, int *pi_title_end,
2043 int *pi_chapter_start, int *pi_chapter_end )
2045 char *psz, *psz_end, *psz_next, *psz_check;
2047 *pi_title_start = *pi_title_end = -1;
2048 *pi_chapter_start = *pi_chapter_end = -1;
2050 /* Start by parsing titles and chapters */
2051 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2053 /* Check we are really dealing with a title/chapter section */
2054 psz_check = psz + 1;
2055 if( !*psz_check ) return;
2056 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2057 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2058 if( *psz_check == ':' && ++psz_check )
2059 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2060 if( *psz_check != '-' && *psz_check ) return;
2061 if( *psz_check == '-' && ++psz_check )
2062 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2063 if( *psz_check != ':' && *psz_check ) return;
2064 if( *psz_check == ':' && ++psz_check )
2065 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2066 if( *psz_check ) return;
2068 /* Separate start and end */
2070 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2072 /* Look for the start title */
2073 *pi_title_start = strtol( psz, &psz_next, 0 );
2074 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2075 *pi_title_end = *pi_title_start;
2078 /* Look for the start chapter */
2080 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2081 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2082 *pi_chapter_end = *pi_chapter_start;
2086 /* Look for the end title */
2087 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2088 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2091 /* Look for the end chapter */
2092 if( *psz_end ) psz_end++;
2093 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2094 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2097 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2098 psz_source, *pi_title_start, *pi_chapter_start,
2099 *pi_title_end, *pi_chapter_end );