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 *****************************************************************************/
31 #include <vlc/input.h>
32 #include <vlc/decoder.h>
35 #include "input_internal.h"
37 #include "stream_output.h"
39 #include "vlc_interface.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Run ( input_thread_t *p_input );
47 static int Init ( input_thread_t *p_input );
48 static void Error( input_thread_t *p_input );
49 static void End ( input_thread_t *p_input );
51 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
52 static void ControlReduce( input_thread_t * );
53 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
56 static int UpdateFromAccess( input_thread_t * );
57 static int UpdateFromDemux( input_thread_t * );
59 static void UpdateItemLength( input_thread_t *, int64_t i_length );
61 static void ParseOption( input_thread_t *p_input, const char *psz_option );
63 static void DecodeUrl ( char * );
64 static void MRLSplit( input_thread_t *, char *, char **, char **, char ** );
65 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
67 static input_source_t *InputSourceNew( input_thread_t *);
68 static int InputSourceInit( input_thread_t *, input_source_t *,
69 char *, char *psz_forced_demux );
70 static void InputSourceClean( input_thread_t *, input_source_t * );
72 static void SlaveDemux( input_thread_t *p_input );
73 static void SlaveSeek( input_thread_t *p_input );
75 static vlc_meta_t *InputMetaUser( input_thread_t *p_input );
77 /*****************************************************************************
78 * input_CreateThread: creates a new input thread
79 *****************************************************************************
80 * This function creates a new input, and returns a pointer
81 * to its description. On error, it returns NULL.
83 * Variables for _public_ use:
86 * - rate,rate-slower, rate-faster
87 * - position, position-offset
89 * - title,title-next,title-prev
90 * - chapter,chapter-next, chapter-prev
91 * - program, audio-es, video-es, spu-es
92 * - audio-delay, spu-delay
97 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
98 * * For intf callback upon changes
100 * TODO explain when Callback is called
101 * TODO complete this list (?)
102 *****************************************************************************/
103 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
104 input_item_t *p_item )
107 input_thread_t *p_input; /* thread descriptor */
111 /* Allocate descriptor */
112 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
113 if( p_input == NULL )
115 msg_Err( p_parent, "out of memory" );
119 /* Init Common fields */
120 p_input->b_eof = VLC_FALSE;
121 p_input->b_can_pace_control = VLC_TRUE;
122 p_input->i_start = 0;
125 p_input->i_title = 0;
126 p_input->title = NULL;
127 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
128 p_input->i_state = INIT_S;
129 p_input->i_rate = INPUT_RATE_DEFAULT;
130 p_input->i_bookmark = 0;
131 p_input->bookmark = NULL;
132 p_input->p_es_out = NULL;
133 p_input->p_sout = NULL;
134 p_input->b_out_pace_control = VLC_FALSE;
135 p_input->i_pts_delay = 0;
137 /* Init Input fields */
138 p_input->input.p_item = p_item;
139 p_input->input.p_access = NULL;
140 p_input->input.p_stream = NULL;
141 p_input->input.p_demux = NULL;
142 p_input->input.b_title_demux = VLC_FALSE;
143 p_input->input.i_title = 0;
144 p_input->input.title = NULL;
145 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
146 p_input->input.b_can_pace_control = VLC_TRUE;
147 p_input->input.b_eof = VLC_FALSE;
148 p_input->input.i_cr_average = 0;
151 p_input->i_slave = 0;
152 p_input->slave = NULL;
154 /* Init control buffer */
155 vlc_mutex_init( p_input, &p_input->lock_control );
156 p_input->i_control = 0;
158 /* Parse input options */
159 vlc_mutex_lock( &p_item->lock );
160 for( i = 0; i < p_item->i_options; i++ )
162 msg_Dbg( p_input, "option: %s", p_item->ppsz_options[i] );
163 ParseOption( p_input, p_item->ppsz_options[i] );
165 vlc_mutex_unlock( &p_item->lock );
167 /* Create Object Variables for private use only */
168 input_ConfigVarInit( p_input );
170 /* Create Objects variables for public Get and Set */
171 input_ControlVarInit( p_input );
172 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
175 var_Get( p_input, "bookmarks", &val );
178 /* FIXME: have a common cfg parsing routine used by sout and others */
179 char *psz_parser, *psz_start, *psz_end;
180 psz_parser = val.psz_string;
181 while( (psz_start = strchr( psz_parser, '{' ) ) )
183 seekpoint_t seekpoint;
186 psz_end = strchr( psz_start, '}' );
187 if( !psz_end ) break;
188 psz_parser = psz_end + 1;
189 backup = *psz_parser;
193 seekpoint.psz_name = 0;
194 seekpoint.i_byte_offset = 0;
195 seekpoint.i_time_offset = 0;
196 while( (psz_end = strchr( psz_start, ',' ) ) )
199 if( !strncmp( psz_start, "name=", 5 ) )
201 seekpoint.psz_name = psz_start + 5;
203 else if( !strncmp( psz_start, "bytes=", 6 ) )
205 seekpoint.i_byte_offset = atoll(psz_start + 6);
207 else if( !strncmp( psz_start, "time=", 5 ) )
209 seekpoint.i_time_offset = atoll(psz_start + 5) * 1000000;
211 psz_start = psz_end + 1;
213 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
214 seekpoint.psz_name, seekpoint.i_byte_offset,
215 seekpoint.i_time_offset );
216 input_Control( p_input, INPUT_ADD_BOOKMARK, &seekpoint );
217 *psz_parser = backup;
219 free( val.psz_string );
222 /* Now we can attach our new input */
223 vlc_object_attach( p_input, p_parent );
225 /* Create thread and wait for its readiness. */
226 if( vlc_thread_create( p_input, "input", Run,
227 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
229 msg_Err( p_input, "cannot create input thread" );
230 vlc_object_detach( p_input );
231 vlc_object_destroy( p_input );
238 /*****************************************************************************
239 * input_StopThread: mark an input thread as zombie
240 *****************************************************************************
241 * This function should not return until the thread is effectively cancelled.
242 *****************************************************************************/
243 void input_StopThread( input_thread_t *p_input )
248 /* Set die for input */
249 p_input->b_die = VLC_TRUE;
251 /* We cannot touch p_input fields directly (we can from another thread),
252 * so use the vlc_object_find way, it's perfectly safe */
254 /* Set die for all access */
255 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
256 for( i = 0; i < p_list->i_count; i++ )
258 p_list->p_values[i].p_object->b_die = VLC_TRUE;
260 vlc_list_release( p_list );
262 /* Set die for all stream */
263 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
264 for( i = 0; i < p_list->i_count; i++ )
266 p_list->p_values[i].p_object->b_die = VLC_TRUE;
268 vlc_list_release( p_list );
270 /* Set die for all demux */
271 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
272 for( i = 0; i < p_list->i_count; i++ )
274 p_list->p_values[i].p_object->b_die = VLC_TRUE;
276 vlc_list_release( p_list );
278 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
281 /*****************************************************************************
282 * input_DestroyThread: mark an input thread as zombie
283 *****************************************************************************
284 * This function should not return until the thread is effectively cancelled.
285 *****************************************************************************/
286 void input_DestroyThread( input_thread_t *p_input )
288 /* Join the thread */
289 vlc_thread_join( p_input );
291 /* Delete input lock (only after thread joined) */
292 vlc_mutex_destroy( &p_input->lock_control );
294 /* TODO: maybe input_DestroyThread should also delete p_input instead
295 * of the playlist but I'm not sure if it's possible */
298 /*****************************************************************************
299 * Run: main thread loop
300 *****************************************************************************
301 * Thread in charge of processing the network packets and demultiplexing.
304 * read subtitle support (XXX take care of spu-delay in the right way).
305 * multi-input support (XXX may be done with subs)
306 *****************************************************************************/
307 static int Run( input_thread_t *p_input )
309 int64_t i_intf_update = 0;
311 /* Signal that the thread is launched */
312 vlc_thread_ready( p_input );
314 if( Init( p_input ) )
316 /* If we failed, wait before we are killed, and exit */
317 p_input->b_error = VLC_TRUE;
321 /* Tell we're dead */
322 p_input->b_dead = VLC_TRUE;
328 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
330 vlc_bool_t b_force_update = VLC_FALSE;
336 if( p_input->i_state != PAUSE_S )
338 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
339 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
346 if( p_input->input.b_title_demux &&
347 p_input->input.p_demux->info.i_update )
349 i_ret = UpdateFromDemux( p_input );
350 b_force_update = VLC_TRUE;
352 else if( !p_input->input.b_title_demux &&
353 p_input->input.p_access &&
354 p_input->input.p_access->info.i_update )
356 i_ret = UpdateFromAccess( p_input );
357 b_force_update = VLC_TRUE;
361 if( i_ret == 0 ) /* EOF */
365 var_Get( p_input, "input-repeat", &repeat );
366 if( repeat.i_int == 0 )
368 /* End of file - we do not set b_die because only the
369 * playlist is allowed to do so. */
370 msg_Dbg( p_input, "EOF reached" );
371 p_input->input.b_eof = VLC_TRUE;
375 msg_Dbg( p_input, "repeating the same input (%d)",
377 if( repeat.i_int > 0 )
380 var_Set( p_input, "input-repeat", repeat );
383 /* Seek to start title/seekpoint */
384 val.i_int = p_input->input.i_title_start -
385 p_input->input.i_title_offset;
386 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
388 input_ControlPush( p_input,
389 INPUT_CONTROL_SET_TITLE, &val );
391 val.i_int = p_input->input.i_seekpoint_start -
392 p_input->input.i_seekpoint_offset;
393 if( val.i_int > 0 /* TODO: check upper boundary */ )
394 input_ControlPush( p_input,
395 INPUT_CONTROL_SET_SEEKPOINT, &val );
397 /* Seek to start position */
398 if( p_input->i_start > 0 )
400 val.i_time = p_input->i_start;
401 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
407 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
414 p_input->b_error = VLC_TRUE;
417 if( i_ret > 0 && p_input->i_slave > 0 )
419 SlaveDemux( p_input );
429 vlc_mutex_lock( &p_input->lock_control );
430 ControlReduce( p_input );
431 while( !ControlPopNoLock( p_input, &i_type, &val ) )
433 msg_Dbg( p_input, "control type=%d", i_type );
434 if( Control( p_input, i_type, val ) )
435 b_force_update = VLC_TRUE;
437 vlc_mutex_unlock( &p_input->lock_control );
439 if( b_force_update || i_intf_update < mdate() )
443 int64_t i_time, i_length;
444 /* update input status variables */
445 if( !demux2_Control( p_input->input.p_demux,
446 DEMUX_GET_POSITION, &f_pos ) )
448 val.f_float = (float)f_pos;
449 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
451 if( !demux2_Control( p_input->input.p_demux,
452 DEMUX_GET_TIME, &i_time ) )
454 p_input->i_time = i_time;
456 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
458 if( !demux2_Control( p_input->input.p_demux,
459 DEMUX_GET_LENGTH, &i_length ) )
462 var_Get( p_input, "length", &old_val );
463 val.i_time = i_length;
464 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
466 if( old_val.i_time != val.i_time )
468 UpdateItemLength( p_input, i_length );
472 var_SetBool( p_input, "intf-change", VLC_TRUE );
473 i_intf_update = mdate() + I64C(150000);
477 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
479 /* We have finish to demux data but not to play them */
480 while( !p_input->b_die )
482 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
485 msg_Dbg( p_input, "waiting decoder fifos to empty" );
487 msleep( INPUT_IDLE_SLEEP );
490 /* We have finished */
491 p_input->b_eof = VLC_TRUE;
494 /* Wait we are asked to die */
495 if( !p_input->b_die )
506 /*****************************************************************************
507 * Init: init the input Thread
508 *****************************************************************************/
509 static int Init( input_thread_t * p_input )
515 vlc_meta_t *p_meta, *p_meta_user;
518 /* Initialize optional stream output. (before access/demuxer) */
519 psz = var_GetString( p_input, "sout" );
522 p_input->p_sout = sout_NewInstance( p_input, psz );
523 if( p_input->p_sout == NULL )
525 msg_Err( p_input, "cannot start stream output instance, aborting" );
533 p_input->p_es_out = input_EsOutNew( p_input );
534 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
535 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
537 if( InputSourceInit( p_input, &p_input->input,
538 p_input->input.p_item->psz_uri, NULL ) )
543 /* Create global title (from master) */
544 p_input->i_title = p_input->input.i_title;
545 p_input->title = p_input->input.title;
546 p_input->i_title_offset = p_input->input.i_title_offset;
547 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
548 if( p_input->i_title > 0 )
550 /* Setup variables */
551 input_ControlVarNavigation( p_input );
552 input_ControlVarTitle( p_input, 0 );
556 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
557 p_input->b_can_pause = p_input->input.b_can_pause;
560 if( p_input->i_pts_delay <= 0 )
561 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
563 /* If the desynchronisation requested by the user is < 0, we need to
564 * cache more data. */
565 var_Get( p_input, "audio-desync", &val );
566 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
568 /* Load master infos */
570 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
571 &val.i_time ) && val.i_time > 0 )
573 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
575 UpdateItemLength( p_input, val.i_time );
578 /* Start title/chapter */
579 val.i_int = p_input->input.i_title_start -
580 p_input->input.i_title_offset;
581 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
582 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
583 val.i_int = p_input->input.i_seekpoint_start -
584 p_input->input.i_seekpoint_offset;
585 if( val.i_int > 0 /* TODO: check upper boundary */ )
586 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
590 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
592 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
595 if( p_input->i_start > 0 )
597 if( p_input->i_start >= val.i_time )
599 msg_Warn( p_input, "invalid start-time ignored" );
605 msg_Dbg( p_input, "start-time: %ds",
606 (int)( p_input->i_start / I64C(1000000) ) );
608 s.i_time = p_input->i_start;
609 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
612 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
614 msg_Warn( p_input, "invalid stop-time ignored" );
620 /* Get fps and set it if not already set */
621 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
626 if( var_Get( p_input, "sub-fps", &fps ) )
628 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT| VLC_VAR_DOINHERIT );
629 var_SetFloat( p_input, "sub-fps", f_fps );
633 /* Look for and add subtitle files */
634 psz_subtitle = var_GetString( p_input, "sub-file" );
641 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
643 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
646 sub = InputSourceNew( p_input );
647 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
649 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
652 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
654 if( count.i_int == 0 )
655 count.i_int++; /* if it was first one, there is disable too */
657 if( count.i_int < list.p_list->i_count )
659 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
660 &list.p_list->p_values[count.i_int] );
662 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
667 var_Get( p_input, "sub-autodetect-file", &val );
670 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
671 char **subs = subtitles_Detect( p_input, psz_autopath,
672 p_input->input.p_item->psz_uri );
675 for( i = 0; subs[i] != NULL; i++ )
677 if( strcmp( psz_subtitle, subs[i] ) )
679 sub = InputSourceNew( p_input );
680 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
682 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
688 free( psz_autopath );
690 free( psz_subtitle );
693 psz = var_GetString( p_input, "input-slave" );
696 char *psz_delim = strchr( psz, '#' );
700 input_source_t *slave;
715 msg_Dbg( p_input, "adding slave '%s'", psz );
716 slave = InputSourceNew( p_input );
717 if( !InputSourceInit( p_input, slave, psz, NULL ) )
719 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
728 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
729 val.b_bool = VLC_FALSE;
730 if( p_input->p_sout )
732 var_Get( p_input, "sout-all", &val );
734 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
735 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
737 if( p_input->p_sout )
739 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
741 p_input->b_out_pace_control = VLC_FALSE;
745 p_input->b_out_pace_control = VLC_TRUE;
747 msg_Dbg( p_input, "starting in %s mode",
748 p_input->b_out_pace_control ? "asynch" : "synch" );
751 /* Get meta data from users */
752 p_meta_user = InputMetaUser( p_input );
754 /* Get meta data from master input */
755 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
761 p_meta = p_meta_user;
763 else if( p_meta_user )
765 vlc_meta_Merge( p_meta, p_meta_user );
766 vlc_meta_Delete( p_meta_user );
769 /* Get meta data from slave input */
770 for( i = 0; i < p_input->i_slave; i++ )
772 vlc_meta_t *p_meta_slave;
774 if( !demux2_Control( p_input->slave[i]->p_demux, DEMUX_GET_META, &p_meta_slave ) )
778 p_meta = p_meta_slave;
780 else if( p_meta_slave )
782 vlc_meta_Merge( p_meta, p_meta_slave );
783 vlc_meta_Delete( p_meta_slave );
788 if( p_meta && p_meta->i_meta > 0 )
790 msg_Dbg( p_input, "meta information:" );
791 for( i = 0; i < p_meta->i_meta; i++ )
793 msg_Dbg( p_input, " - '%s' = '%s'",
794 _(p_meta->name[i]), p_meta->value[i] );
796 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] )
797 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
799 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
800 input_Control( p_input, INPUT_ADD_INFO, _("General"),
801 _("Author"), p_meta->value[i] );
803 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
804 _(p_meta->name[i]), "%s", p_meta->value[i] );
807 for( i = 0; i < p_meta->i_track; i++ )
809 vlc_meta_t *tk = p_meta->track[i];
814 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
816 msg_Dbg( p_input, " - track[%d]:", i );
818 sprintf( psz_cat, "%s %d", _("Stream"), i );
819 for( j = 0; j < tk->i_meta; j++ )
821 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
824 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
825 _(tk->name[j]), "%s", tk->value[j] );
830 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
832 p_input->p_sout->p_meta = p_meta;
836 vlc_meta_Delete( p_meta );
840 msg_Dbg( p_input, "`%s' sucessfully opened",
841 p_input->input.p_item->psz_uri );
843 /* initialization is complete */
844 p_input->i_state = PLAYING_S;
846 val.i_int = PLAYING_S;
847 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
852 if( p_input->p_es_out )
853 input_EsOutDelete( p_input->p_es_out );
855 if( p_input->p_sout )
856 sout_DeleteInstance( p_input->p_sout );
858 /* Mark them deleted */
859 p_input->input.p_demux = NULL;
860 p_input->input.p_stream = NULL;
861 p_input->input.p_access = NULL;
862 p_input->p_es_out = NULL;
863 p_input->p_sout = NULL;
868 /*****************************************************************************
869 * Error: RunThread() error loop
870 *****************************************************************************
871 * This function is called when an error occured during thread main's loop.
872 *****************************************************************************/
873 static void Error( input_thread_t *p_input )
875 while( !p_input->b_die )
878 msleep( INPUT_IDLE_SLEEP );
882 /*****************************************************************************
883 * End: end the input thread
884 *****************************************************************************/
885 static void End( input_thread_t * p_input )
890 msg_Dbg( p_input, "closing `%s'",
891 p_input->input.p_item->psz_uri );
893 /* We are at the end */
894 p_input->i_state = END_S;
897 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
899 /* Clean control variables */
900 input_ControlVarClean( p_input );
902 /* Clean up master */
903 InputSourceClean( p_input, &p_input->input );
906 for( i = 0; i < p_input->i_slave; i++ )
908 InputSourceClean( p_input, p_input->slave[i] );
909 free( p_input->slave[i] );
911 if( p_input->slave ) free( p_input->slave );
913 /* Unload all modules */
914 if( p_input->p_es_out )
915 input_EsOutDelete( p_input->p_es_out );
917 /* Close optional stream output instance */
918 if( p_input->p_sout )
921 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
924 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
926 /* attach sout to the playlist */
927 msg_Warn( p_input, "keeping sout" );
928 vlc_object_detach( p_input->p_sout );
929 vlc_object_attach( p_input->p_sout, p_pl );
933 msg_Warn( p_input, "destroying sout" );
934 sout_DeleteInstance( p_input->p_sout );
937 vlc_object_release( p_pl );
940 /* Tell we're dead */
941 p_input->b_dead = VLC_TRUE;
944 /*****************************************************************************
946 *****************************************************************************/
947 static inline int ControlPopNoLock( input_thread_t *p_input,
948 int *pi_type, vlc_value_t *p_val )
950 if( p_input->i_control <= 0 )
955 *pi_type = p_input->control[0].i_type;
956 *p_val = p_input->control[0].val;
958 p_input->i_control--;
959 if( p_input->i_control > 0 )
963 for( i = 0; i < p_input->i_control; i++ )
965 p_input->control[i].i_type = p_input->control[i+1].i_type;
966 p_input->control[i].val = p_input->control[i+1].val;
973 static void ControlReduce( input_thread_t *p_input )
976 for( i = 1; i < p_input->i_control; i++ )
978 const int i_lt = p_input->control[i-1].i_type;
979 const int i_ct = p_input->control[i].i_type;
981 /* XXX We can't merge INPUT_CONTROL_SET_ES */
982 msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
985 ( i_ct == INPUT_CONTROL_SET_STATE ||
986 i_ct == INPUT_CONTROL_SET_RATE ||
987 i_ct == INPUT_CONTROL_SET_POSITION ||
988 i_ct == INPUT_CONTROL_SET_TIME ||
989 i_ct == INPUT_CONTROL_SET_PROGRAM ||
990 i_ct == INPUT_CONTROL_SET_TITLE ||
991 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
992 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
995 msg_Dbg( p_input, "merged at %d", i );
997 for( j = i; j < p_input->i_control; j++ )
998 p_input->control[j-1] = p_input->control[j];
999 p_input->i_control--;
1003 /* TODO but that's not that important
1004 - merge SET_X with SET_X_CMD
1005 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1006 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1013 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1016 vlc_bool_t b_force_update = VLC_FALSE;
1020 case INPUT_CONTROL_SET_DIE:
1021 msg_Dbg( p_input, "control: INPUT_CONTROL_SET_DIE proceed" );
1022 /* Mark all submodules to die */
1023 if( p_input->input.p_access )
1024 p_input->input.p_access->b_die = VLC_TRUE;
1025 if( p_input->input.p_stream )
1026 p_input->input.p_stream->b_die = VLC_TRUE;
1027 p_input->input.p_demux->b_die = VLC_TRUE;
1029 p_input->b_die = VLC_TRUE;
1032 case INPUT_CONTROL_SET_POSITION:
1033 case INPUT_CONTROL_SET_POSITION_OFFSET:
1036 if( i_type == INPUT_CONTROL_SET_POSITION )
1038 f_pos = val.f_float;
1042 /* Should not fail */
1043 demux2_Control( p_input->input.p_demux,
1044 DEMUX_GET_POSITION, &f_pos );
1045 f_pos += val.f_float;
1047 if( f_pos < 0.0 ) f_pos = 0.0;
1048 if( f_pos > 1.0 ) f_pos = 1.0;
1049 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1052 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1053 "%2.1f%% failed", f_pos * 100 );
1057 if( p_input->i_slave > 0 )
1058 SlaveSeek( p_input );
1060 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1061 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1062 b_force_update = VLC_TRUE;
1067 case INPUT_CONTROL_SET_TIME:
1068 case INPUT_CONTROL_SET_TIME_OFFSET:
1073 if( i_type == INPUT_CONTROL_SET_TIME )
1075 i_time = val.i_time;
1079 /* Should not fail */
1080 demux2_Control( p_input->input.p_demux,
1081 DEMUX_GET_TIME, &i_time );
1082 i_time += val.i_time;
1084 if( i_time < 0 ) i_time = 0;
1085 i_ret = demux2_Control( p_input->input.p_demux,
1086 DEMUX_SET_TIME, i_time );
1090 /* Emulate it with a SET_POS */
1092 demux2_Control( p_input->input.p_demux,
1093 DEMUX_GET_LENGTH, &i_length );
1096 double f_pos = (double)i_time / (double)i_length;
1097 i_ret = demux2_Control( p_input->input.p_demux,
1098 DEMUX_SET_POSITION, f_pos );
1103 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1104 " failed", i_time );
1108 if( p_input->i_slave > 0 )
1109 SlaveSeek( p_input );
1111 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1113 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1114 b_force_update = VLC_TRUE;
1119 case INPUT_CONTROL_SET_STATE:
1120 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1121 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1124 if( p_input->input.p_access )
1125 i_ret = access2_Control( p_input->input.p_access,
1126 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1128 i_ret = demux2_Control( p_input->input.p_demux,
1129 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1133 /* FIXME What to do ? */
1134 msg_Warn( p_input, "cannot unset pause -> EOF" );
1135 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1138 b_force_update = VLC_TRUE;
1140 /* Switch to play */
1141 p_input->i_state = PLAYING_S;
1142 val.i_int = PLAYING_S;
1143 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1146 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1148 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1149 p_input->b_can_pause )
1152 if( p_input->input.p_access )
1153 i_ret = access2_Control( p_input->input.p_access,
1154 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1156 i_ret = demux2_Control( p_input->input.p_demux,
1157 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1159 b_force_update = VLC_TRUE;
1163 msg_Warn( p_input, "cannot set pause state" );
1164 val.i_int = p_input->i_state;
1168 val.i_int = PAUSE_S;
1171 /* Switch to new state */
1172 p_input->i_state = val.i_int;
1173 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1175 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1177 b_force_update = VLC_TRUE;
1179 /* Correct "state" value */
1180 val.i_int = p_input->i_state;
1181 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1183 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1185 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1189 case INPUT_CONTROL_SET_RATE:
1190 case INPUT_CONTROL_SET_RATE_SLOWER:
1191 case INPUT_CONTROL_SET_RATE_FASTER:
1195 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1196 i_rate = p_input->i_rate * 2;
1197 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1198 i_rate = p_input->i_rate / 2;
1202 if( i_rate < INPUT_RATE_MIN )
1204 msg_Dbg( p_input, "cannot set rate faster" );
1205 i_rate = INPUT_RATE_MIN;
1207 else if( i_rate > INPUT_RATE_MAX )
1209 msg_Dbg( p_input, "cannot set rate slower" );
1210 i_rate = INPUT_RATE_MAX;
1212 if( i_rate != INPUT_RATE_DEFAULT &&
1213 ( !p_input->b_can_pace_control ||
1214 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1216 msg_Dbg( p_input, "cannot change rate" );
1217 i_rate = INPUT_RATE_DEFAULT;
1219 if( i_rate != p_input->i_rate )
1221 p_input->i_rate = i_rate;
1223 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1225 /* We haven't send data to decoder when rate != default */
1226 if( i_rate == INPUT_RATE_DEFAULT )
1227 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1230 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1232 b_force_update = VLC_TRUE;
1237 case INPUT_CONTROL_SET_PROGRAM:
1238 /* No need to force update, es_out does it if needed */
1239 es_out_Control( p_input->p_es_out,
1240 ES_OUT_SET_GROUP, val.i_int );
1243 case INPUT_CONTROL_SET_ES:
1244 /* No need to force update, es_out does it if needed */
1245 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1246 input_EsOutGetFromID( p_input->p_es_out,
1250 case INPUT_CONTROL_SET_AUDIO_DELAY:
1251 input_EsOutSetDelay( p_input->p_es_out,
1252 AUDIO_ES, val.i_time );
1253 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1256 case INPUT_CONTROL_SET_SPU_DELAY:
1257 input_EsOutSetDelay( p_input->p_es_out,
1258 SPU_ES, val.i_time );
1259 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1262 case INPUT_CONTROL_SET_TITLE:
1263 case INPUT_CONTROL_SET_TITLE_NEXT:
1264 case INPUT_CONTROL_SET_TITLE_PREV:
1265 if( p_input->input.b_title_demux &&
1266 p_input->input.i_title > 0 )
1269 /* FIXME handle demux title */
1270 demux_t *p_demux = p_input->input.p_demux;
1273 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1274 i_title = p_demux->info.i_title - 1;
1275 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1276 i_title = p_demux->info.i_title + 1;
1278 i_title = val.i_int;
1280 if( i_title >= 0 && i_title < p_input->input.i_title )
1282 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1284 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1285 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1287 input_ControlVarTitle( p_input, i_title );
1290 else if( p_input->input.i_title > 0 )
1292 access_t *p_access = p_input->input.p_access;
1295 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1296 i_title = p_access->info.i_title - 1;
1297 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1298 i_title = p_access->info.i_title + 1;
1300 i_title = val.i_int;
1302 if( i_title >= 0 && i_title < p_input->input.i_title )
1304 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1305 stream_AccessReset( p_input->input.p_stream );
1307 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1308 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1312 case INPUT_CONTROL_SET_SEEKPOINT:
1313 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1314 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1315 if( p_input->input.b_title_demux &&
1316 p_input->input.i_title > 0 )
1318 demux_t *p_demux = p_input->input.p_demux;
1321 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1322 i_seekpoint = p_demux->info.i_seekpoint - 1;
1323 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1324 i_seekpoint = p_demux->info.i_seekpoint + 1;
1326 i_seekpoint = val.i_int;
1328 if( i_seekpoint >= 0 && i_seekpoint <
1329 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1331 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1333 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1334 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1337 else if( p_input->input.i_title > 0 )
1339 access_t *p_access = p_input->input.p_access;
1342 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1343 i_seekpoint = p_access->info.i_seekpoint - 1;
1344 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1345 i_seekpoint = p_access->info.i_seekpoint + 1;
1347 i_seekpoint = val.i_int;
1349 if( i_seekpoint >= 0 && i_seekpoint <
1350 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1352 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1353 stream_AccessReset( p_input->input.p_stream );
1355 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1356 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1361 case INPUT_CONTROL_SET_BOOKMARK:
1363 msg_Err( p_input, "not yet implemented" );
1367 return b_force_update;
1370 /*****************************************************************************
1372 *****************************************************************************/
1373 static int UpdateFromDemux( input_thread_t *p_input )
1375 demux_t *p_demux = p_input->input.p_demux;
1378 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1380 v.i_int = p_demux->info.i_title;
1381 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1383 input_ControlVarTitle( p_input, p_demux->info.i_title );
1385 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1387 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1389 v.i_int = p_demux->info.i_seekpoint;
1390 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1392 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1394 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1396 /* Hmmm only works with master input */
1397 if( p_input->input.p_demux == p_demux )
1399 int i_title_end = p_input->input.i_title_end -
1400 p_input->input.i_title_offset;
1401 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1402 p_input->input.i_seekpoint_offset;
1404 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1406 if( p_demux->info.i_title > i_title_end ||
1407 ( p_demux->info.i_title == i_title_end &&
1408 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1410 else if( i_seekpoint_end >=0 )
1412 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1414 else if( i_title_end >= 0 )
1416 if( p_demux->info.i_title > i_title_end ) return 0;
1423 /*****************************************************************************
1425 *****************************************************************************/
1426 static int UpdateFromAccess( input_thread_t *p_input )
1428 access_t *p_access = p_input->input.p_access;
1431 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1433 v.i_int = p_access->info.i_title;
1434 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1436 input_ControlVarTitle( p_input, p_access->info.i_title );
1438 stream_AccessUpdate( p_input->input.p_stream );
1440 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1442 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1444 v.i_int = p_access->info.i_seekpoint;
1445 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1447 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1449 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1451 /* Hmmm only works with master input */
1452 if( p_input->input.p_access == p_access )
1454 int i_title_end = p_input->input.i_title_end -
1455 p_input->input.i_title_offset;
1456 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1457 p_input->input.i_seekpoint_offset;
1459 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1461 if( p_access->info.i_title > i_title_end ||
1462 ( p_access->info.i_title == i_title_end &&
1463 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1465 else if( i_seekpoint_end >=0 )
1467 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1469 else if( i_title_end >= 0 )
1471 if( p_access->info.i_title > i_title_end ) return 0;
1478 /*****************************************************************************
1480 *****************************************************************************/
1481 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
1483 char psz_buffer[MSTRTIME_MAX_SIZE];
1485 vlc_mutex_lock( &p_input->input.p_item->lock );
1486 p_input->input.p_item->i_duration = i_length;
1487 vlc_mutex_unlock( &p_input->input.p_item->lock );
1489 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1490 msecstotimestr( psz_buffer, i_length / 1000 ) );
1493 /*****************************************************************************
1495 *****************************************************************************/
1496 static input_source_t *InputSourceNew( input_thread_t *p_input )
1498 input_source_t *in = malloc( sizeof( input_source_t ) );
1501 in->p_access = NULL;
1502 in->p_stream = NULL;
1504 in->b_title_demux = VLC_FALSE;
1507 in->b_can_pace_control = VLC_TRUE;
1508 in->b_eof = VLC_FALSE;
1509 in->i_cr_average = 0;
1514 /*****************************************************************************
1516 *****************************************************************************/
1517 static int InputSourceInit( input_thread_t *p_input,
1518 input_source_t *in, char *psz_mrl,
1519 char *psz_forced_demux )
1521 char *psz_dup = strdup( psz_mrl );
1528 MRLSplit( p_input, psz_dup, &psz_access, &psz_demux, &psz_path );
1530 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1531 psz_mrl, psz_access, psz_demux, psz_path );
1533 /* Find optional titles and seekpoints */
1534 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1535 &in->i_seekpoint_start, &in->i_seekpoint_end );
1537 if( psz_forced_demux && *psz_forced_demux )
1538 psz_demux = psz_forced_demux;
1540 /* Try access_demux if no demux given */
1541 if( *psz_demux == '\0' )
1543 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1544 NULL, p_input->p_es_out );
1549 int64_t i_pts_delay;
1551 /* Get infos from access_demux */
1552 demux2_Control( in->p_demux,
1553 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1554 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1556 in->b_title_demux = VLC_TRUE;
1557 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1558 &in->title, &in->i_title,
1559 &in->i_title_offset, &in->i_seekpoint_offset ) )
1564 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1565 &in->b_can_pace_control );
1566 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1570 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1576 int64_t i_pts_delay;
1578 /* Now try a real access */
1579 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path );
1581 /* Access failed, URL encoded ? */
1582 if( in->p_access == NULL && strchr( psz_path, '%' ) )
1584 DecodeUrl( psz_path );
1586 msg_Dbg( p_input, "retying with access `%s' demux `%s' path `%s'",
1587 psz_access, psz_demux, psz_path );
1589 in->p_access = access2_New( p_input,
1590 psz_access, psz_demux, psz_path );
1592 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
1593 * are forbidden in filenames on Win32. */
1595 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1596 if( in->p_access == NULL &&
1597 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1600 psz_dup = strdup( psz_mrl );
1605 in->p_access = access2_New( p_input,
1606 psz_access, psz_demux, psz_path );
1610 if( in->p_access == NULL )
1612 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
1616 /* Get infos from access */
1617 access2_Control( in->p_access,
1618 ACCESS_GET_PTS_DELAY, &i_pts_delay );
1619 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1621 in->b_title_demux = VLC_FALSE;
1622 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
1623 &in->title, &in->i_title,
1624 &in->i_title_offset, &in->i_seekpoint_offset ) )
1630 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
1631 &in->b_can_pace_control );
1632 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
1634 access2_Control( in->p_access, ACCESS_CAN_SEEK,
1636 var_Set( p_input, "seekable", val );
1638 /* Create the stream_t */
1639 in->p_stream = stream_AccessNew( in->p_access );
1640 if( in->p_stream == NULL )
1642 msg_Warn( p_input, "cannot create a stream_t from access" );
1646 /* Open a demuxer */
1647 if( *psz_demux == '\0' && *in->p_access->psz_demux )
1649 psz_demux = in->p_access->psz_demux;
1651 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1652 in->p_stream, p_input->p_es_out );
1653 if( in->p_demux == NULL )
1655 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
1656 psz_access, psz_demux, psz_path );
1660 /* TODO get title from demux */
1661 if( in->i_title <= 0 )
1663 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1664 &in->title, &in->i_title,
1665 &in->i_title_offset, &in->i_seekpoint_offset ))
1672 in->b_title_demux = VLC_TRUE;
1681 demux2_Delete( in->p_demux );
1684 stream_AccessDelete( in->p_stream );
1687 access2_Delete( in->p_access );
1690 return VLC_EGENERIC;
1693 /*****************************************************************************
1695 *****************************************************************************/
1696 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
1699 demux2_Delete( in->p_demux );
1702 stream_AccessDelete( in->p_stream );
1705 access2_Delete( in->p_access );
1707 if( in->i_title > 0 )
1710 for( i = 0; i < in->i_title; i++ )
1712 vlc_input_title_Delete( in->title[i] );
1718 static void SlaveDemux( input_thread_t *p_input )
1722 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1724 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1728 for( i = 0; i < p_input->i_slave; i++ )
1730 input_source_t *in = p_input->slave[i];
1736 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
1741 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
1743 msg_Err( p_input, "slave[%d] doesn't like "
1744 "DEMUX_GET_TIME -> EOF", i );
1749 if( i_stime >= i_time )
1752 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
1758 i_ret = in->p_demux->pf_demux( in->p_demux );
1763 msg_Dbg( p_input, "slave %d EOF", i );
1764 in->b_eof = VLC_TRUE;
1769 static void SlaveSeek( input_thread_t *p_input )
1774 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1776 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1780 for( i = 0; i < p_input->i_slave; i++ )
1782 input_source_t *in = p_input->slave[i];
1784 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
1786 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
1787 in->b_eof = VLC_TRUE;
1791 /*****************************************************************************
1793 *****************************************************************************/
1794 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
1799 if( ( p_meta = vlc_meta_New() ) == NULL )
1802 /* Get meta information from user */
1803 #define GET_META( c, s ) \
1804 var_Get( p_input, (s), &val ); \
1805 if( *val.psz_string ) \
1806 vlc_meta_Add( p_meta, c, val.psz_string ); \
1807 free( val.psz_string )
1809 GET_META( VLC_META_TITLE, "meta-title" );
1810 GET_META( VLC_META_AUTHOR, "meta-author" );
1811 GET_META( VLC_META_ARTIST, "meta-artist" );
1812 GET_META( VLC_META_GENRE, "meta-genre" );
1813 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
1814 GET_META( VLC_META_DESCRIPTION, "meta-description" );
1815 GET_META( VLC_META_DATE, "meta-date" );
1816 GET_META( VLC_META_URL, "meta-url" );
1822 /*****************************************************************************
1823 * DecodeUrl: decode a given encoded url
1824 *****************************************************************************/
1825 static void DecodeUrl( char *psz )
1827 char *dup = strdup( psz );
1845 *psz++ = strtol( val, NULL, 16 );
1847 else if( *p == '+' )
1861 /*****************************************************************************
1862 * ParseOption: parses the options for the input
1863 *****************************************************************************
1864 * This function parses the input (config) options and creates their associated
1866 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1867 * bar is the value of the option.
1868 *****************************************************************************/
1869 static void ParseOption( input_thread_t *p_input, const char *psz_option )
1871 char *psz_name = (char *)psz_option;
1872 char *psz_value = strchr( psz_option, '=' );
1873 int i_name_len, i_type;
1874 vlc_bool_t b_isno = VLC_FALSE;
1877 if( psz_value ) i_name_len = psz_value - psz_option;
1878 else i_name_len = strlen( psz_option );
1880 /* It's too much of an hassle to remove the ':' when we parse
1881 * the cmd line :) */
1882 if( i_name_len && *psz_name == ':' )
1888 if( i_name_len == 0 ) return;
1890 psz_name = strndup( psz_name, i_name_len );
1891 if( psz_value ) psz_value++;
1893 i_type = config_GetType( p_input, psz_name );
1895 if( !i_type && !psz_value )
1897 /* check for "no-foo" or "nofoo" */
1898 if( !strncmp( psz_name, "no-", 3 ) )
1900 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1902 else if( !strncmp( psz_name, "no", 2 ) )
1904 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1906 else goto cleanup; /* Option doesn't exist */
1909 i_type = config_GetType( p_input, psz_name );
1911 if( !i_type ) goto cleanup; /* Option doesn't exist */
1913 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1915 if( ( i_type != VLC_VAR_BOOL ) &&
1916 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1918 /* Create the variable in the input object.
1919 * Children of the input object will be able to retreive this value
1920 * thanks to the inheritance property of the object variables. */
1921 var_Create( p_input, psz_name, i_type );
1926 val.b_bool = !b_isno;
1929 case VLC_VAR_INTEGER:
1930 val.i_int = atoi( psz_value );
1934 val.f_float = atof( psz_value );
1937 case VLC_VAR_STRING:
1938 case VLC_VAR_MODULE:
1940 case VLC_VAR_DIRECTORY:
1941 val.psz_string = psz_value;
1949 var_Set( p_input, psz_name, val );
1951 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1954 if( psz_name ) free( psz_name );
1958 /*****************************************************************************
1959 * MRLSplit: parse the access, demux and url part of the
1960 * Media Resource Locator.
1961 *****************************************************************************/
1962 static void MRLSplit( input_thread_t *p_input, char *psz_dup,
1963 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
1965 char *psz_access = NULL;
1966 char *psz_demux = NULL;
1967 char *psz_path = NULL;
1970 psz = strchr( psz_dup, ':' );
1972 #if defined( WIN32 ) || defined( UNDER_CE )
1973 if( psz - psz_dup == 1 )
1975 msg_Warn( p_input, "drive letter %c: found in source string",
1985 if( psz[0] == '/' && psz[1] == '/' )
1990 psz = strchr( psz_dup, '/' );
1997 psz_access = psz_dup;
2004 if( psz_access == NULL )
2007 *ppsz_access = psz_access;
2009 if( psz_demux == NULL )
2012 *ppsz_demux = psz_demux;
2014 if( psz_path == NULL )
2017 *ppsz_path = psz_path;
2020 /*****************************************************************************
2021 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2024 * [url][@[title-start][,chapter-start][-[title-end][,chapter-end]]]
2025 *****************************************************************************/
2026 static void MRLSections( input_thread_t *p_input, char *psz_source,
2027 int *pi_title_start, int *pi_title_end,
2028 int *pi_chapter_start, int *pi_chapter_end )
2030 char *psz, *psz_end, *psz_next;
2032 *pi_title_start = *pi_title_end = -1;
2033 *pi_chapter_start = *pi_chapter_end = -1;
2035 /* Start by parsing titles and chapters */
2036 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2040 /* Separate start and end */
2041 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2043 /* Look for the start title */
2044 *pi_title_start = strtol( psz, &psz_next, 0 );
2045 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2046 *pi_title_end = *pi_title_start;
2049 /* Look for the start chapter */
2051 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2052 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2053 *pi_chapter_end = *pi_chapter_start;
2057 /* Look for the end title */
2058 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2059 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2062 /* Look for the end chapter */
2063 if( *psz_end ) psz_end++;
2064 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2065 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2068 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2069 psz_source, *pi_title_start, *pi_chapter_start,
2070 *pi_title_end, *pi_chapter_end );