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"
39 #include "vlc_playlist.h"
40 #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 * );
58 static int UpdateMeta( 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_meta = NULL;
134 p_input->p_es_out = NULL;
135 p_input->p_sout = NULL;
136 p_input->b_out_pace_control = VLC_FALSE;
137 p_input->i_pts_delay = 0;
139 /* Init Input fields */
140 p_input->input.p_item = p_item;
141 p_input->input.p_access = NULL;
142 p_input->input.p_stream = NULL;
143 p_input->input.p_demux = NULL;
144 p_input->input.b_title_demux = VLC_FALSE;
145 p_input->input.i_title = 0;
146 p_input->input.title = NULL;
147 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
148 p_input->input.b_can_pace_control = VLC_TRUE;
149 p_input->input.b_eof = VLC_FALSE;
150 p_input->input.i_cr_average = 0;
153 p_input->i_slave = 0;
154 p_input->slave = NULL;
156 /* Init control buffer */
157 vlc_mutex_init( p_input, &p_input->lock_control );
158 p_input->i_control = 0;
160 /* Parse input options */
161 vlc_mutex_lock( &p_item->lock );
162 for( i = 0; i < p_item->i_options; i++ )
164 // msg_Dbg( p_input, "option: %s", p_item->ppsz_options[i] );
165 ParseOption( p_input, p_item->ppsz_options[i] );
167 vlc_mutex_unlock( &p_item->lock );
169 /* Create Object Variables for private use only */
170 input_ConfigVarInit( p_input );
172 /* Create Objects variables for public Get and Set */
173 input_ControlVarInit( p_input );
174 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
177 var_Get( p_input, "bookmarks", &val );
180 /* FIXME: have a common cfg parsing routine used by sout and others */
181 char *psz_parser, *psz_start, *psz_end;
182 psz_parser = val.psz_string;
183 while( (psz_start = strchr( psz_parser, '{' ) ) )
185 seekpoint_t seekpoint;
188 psz_end = strchr( psz_start, '}' );
189 if( !psz_end ) break;
190 psz_parser = psz_end + 1;
191 backup = *psz_parser;
195 seekpoint.psz_name = 0;
196 seekpoint.i_byte_offset = 0;
197 seekpoint.i_time_offset = 0;
198 while( (psz_end = strchr( psz_start, ',' ) ) )
201 if( !strncmp( psz_start, "name=", 5 ) )
203 seekpoint.psz_name = psz_start + 5;
205 else if( !strncmp( psz_start, "bytes=", 6 ) )
207 seekpoint.i_byte_offset = atoll(psz_start + 6);
209 else if( !strncmp( psz_start, "time=", 5 ) )
211 seekpoint.i_time_offset = atoll(psz_start + 5) * 1000000;
213 psz_start = psz_end + 1;
215 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
216 seekpoint.psz_name, seekpoint.i_byte_offset,
217 seekpoint.i_time_offset );
218 input_Control( p_input, INPUT_ADD_BOOKMARK, &seekpoint );
219 *psz_parser = backup;
221 free( val.psz_string );
224 /* Now we can attach our new input */
225 vlc_object_attach( p_input, p_parent );
227 /* Create thread and wait for its readiness. */
228 if( vlc_thread_create( p_input, "input", Run,
229 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
231 msg_Err( p_input, "cannot create input thread" );
232 vlc_object_detach( p_input );
233 vlc_object_destroy( p_input );
240 /*****************************************************************************
241 * input_StopThread: mark an input thread as zombie
242 *****************************************************************************
243 * This function should not return until the thread is effectively cancelled.
244 *****************************************************************************/
245 void input_StopThread( input_thread_t *p_input )
250 /* Set die for input */
251 p_input->b_die = VLC_TRUE;
253 /* We cannot touch p_input fields directly (we can from another thread),
254 * so use the vlc_object_find way, it's perfectly safe */
256 /* Set die for all access */
257 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
258 for( i = 0; i < p_list->i_count; i++ )
260 p_list->p_values[i].p_object->b_die = VLC_TRUE;
262 vlc_list_release( p_list );
264 /* Set die for all stream */
265 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
266 for( i = 0; i < p_list->i_count; i++ )
268 p_list->p_values[i].p_object->b_die = VLC_TRUE;
270 vlc_list_release( p_list );
272 /* Set die for all demux */
273 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
274 for( i = 0; i < p_list->i_count; i++ )
276 p_list->p_values[i].p_object->b_die = VLC_TRUE;
278 vlc_list_release( p_list );
280 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
283 /*****************************************************************************
284 * input_DestroyThread: mark an input thread as zombie
285 *****************************************************************************
286 * This function should not return until the thread is effectively cancelled.
287 *****************************************************************************/
288 void input_DestroyThread( input_thread_t *p_input )
290 /* Join the thread */
291 vlc_thread_join( p_input );
293 /* Delete input lock (only after thread joined) */
294 vlc_mutex_destroy( &p_input->lock_control );
296 /* TODO: maybe input_DestroyThread should also delete p_input instead
297 * of the playlist but I'm not sure if it's possible */
300 /*****************************************************************************
301 * Run: main thread loop
302 *****************************************************************************
303 * Thread in charge of processing the network packets and demultiplexing.
306 * read subtitle support (XXX take care of spu-delay in the right way).
307 * multi-input support (XXX may be done with subs)
308 *****************************************************************************/
309 static int Run( input_thread_t *p_input )
311 int64_t i_intf_update = 0;
313 /* Signal that the thread is launched */
314 vlc_thread_ready( p_input );
316 if( Init( p_input ) )
318 /* If we failed, wait before we are killed, and exit */
319 p_input->b_error = VLC_TRUE;
323 /* Tell we're dead */
324 p_input->b_dead = VLC_TRUE;
330 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
332 vlc_bool_t b_force_update = VLC_FALSE;
338 if( p_input->i_state != PAUSE_S )
340 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
341 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
348 if( p_input->input.b_title_demux &&
349 p_input->input.p_demux->info.i_update )
351 i_ret = UpdateFromDemux( p_input );
352 b_force_update = VLC_TRUE;
354 else if( !p_input->input.b_title_demux &&
355 p_input->input.p_access &&
356 p_input->input.p_access->info.i_update )
358 i_ret = UpdateFromAccess( p_input );
359 b_force_update = VLC_TRUE;
363 if( i_ret == 0 ) /* EOF */
367 var_Get( p_input, "input-repeat", &repeat );
368 if( repeat.i_int == 0 )
370 /* End of file - we do not set b_die because only the
371 * playlist is allowed to do so. */
372 msg_Dbg( p_input, "EOF reached" );
373 p_input->input.b_eof = VLC_TRUE;
377 msg_Dbg( p_input, "repeating the same input (%d)",
379 if( repeat.i_int > 0 )
382 var_Set( p_input, "input-repeat", repeat );
385 /* Seek to start title/seekpoint */
386 val.i_int = p_input->input.i_title_start -
387 p_input->input.i_title_offset;
388 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
390 input_ControlPush( p_input,
391 INPUT_CONTROL_SET_TITLE, &val );
393 val.i_int = p_input->input.i_seekpoint_start -
394 p_input->input.i_seekpoint_offset;
395 if( val.i_int > 0 /* TODO: check upper boundary */ )
396 input_ControlPush( p_input,
397 INPUT_CONTROL_SET_SEEKPOINT, &val );
399 /* Seek to start position */
400 if( p_input->i_start > 0 )
402 val.i_time = p_input->i_start;
403 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
409 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
416 p_input->b_error = VLC_TRUE;
419 if( i_ret > 0 && p_input->i_slave > 0 )
421 SlaveDemux( p_input );
431 vlc_mutex_lock( &p_input->lock_control );
432 ControlReduce( p_input );
433 while( !ControlPopNoLock( p_input, &i_type, &val ) )
435 msg_Dbg( p_input, "control type=%d", i_type );
436 if( Control( p_input, i_type, val ) )
437 b_force_update = VLC_TRUE;
439 vlc_mutex_unlock( &p_input->lock_control );
441 if( b_force_update || i_intf_update < mdate() )
445 int64_t i_time, i_length;
446 /* update input status variables */
447 if( !demux2_Control( p_input->input.p_demux,
448 DEMUX_GET_POSITION, &f_pos ) )
450 val.f_float = (float)f_pos;
451 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
453 if( !demux2_Control( p_input->input.p_demux,
454 DEMUX_GET_TIME, &i_time ) )
456 p_input->i_time = i_time;
458 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
460 if( !demux2_Control( p_input->input.p_demux,
461 DEMUX_GET_LENGTH, &i_length ) )
464 var_Get( p_input, "length", &old_val );
465 val.i_time = i_length;
466 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
468 if( old_val.i_time != val.i_time )
470 UpdateItemLength( p_input, i_length );
474 var_SetBool( p_input, "intf-change", VLC_TRUE );
475 i_intf_update = mdate() + I64C(150000);
479 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
481 /* We have finish to demux data but not to play them */
482 while( !p_input->b_die )
484 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
487 msg_Dbg( p_input, "waiting decoder fifos to empty" );
489 msleep( INPUT_IDLE_SLEEP );
492 /* We have finished */
493 p_input->b_eof = VLC_TRUE;
496 /* Wait we are asked to die */
497 if( !p_input->b_die )
508 /*****************************************************************************
509 * Init: init the input Thread
510 *****************************************************************************/
511 static int Init( input_thread_t * p_input )
517 vlc_meta_t *p_meta, *p_meta_tmp;
521 /* Initialize optional stream output. (before access/demuxer) */
522 psz = var_GetString( p_input, "sout" );
525 p_input->p_sout = sout_NewInstance( p_input, psz );
526 if( p_input->p_sout == NULL )
528 msg_Err( p_input, "cannot start stream output instance, aborting" );
536 p_input->p_es_out = input_EsOutNew( p_input );
537 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
538 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
540 if( InputSourceInit( p_input, &p_input->input,
541 p_input->input.p_item->psz_uri, NULL ) )
546 /* Create global title (from master) */
547 p_input->i_title = p_input->input.i_title;
548 p_input->title = p_input->input.title;
549 p_input->i_title_offset = p_input->input.i_title_offset;
550 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
551 if( p_input->i_title > 0 )
553 /* Setup variables */
554 input_ControlVarNavigation( p_input );
555 input_ControlVarTitle( p_input, 0 );
559 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
560 p_input->b_can_pause = p_input->input.b_can_pause;
563 if( p_input->i_pts_delay <= 0 )
564 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
566 /* If the desynchronisation requested by the user is < 0, we need to
567 * cache more data. */
568 var_Get( p_input, "audio-desync", &val );
569 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
571 /* Update cr_average depending on the caching */
572 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
573 p_input->input.i_cr_average /= 10;
574 if( p_input->input.i_cr_average <= 0 ) p_input->input.i_cr_average = 1;
576 /* Load master infos */
578 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
579 &val.i_time ) && val.i_time > 0 )
581 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
583 UpdateItemLength( p_input, val.i_time );
584 p_input->input.p_item->i_duration = val.i_time;
587 /* Start title/chapter */
588 val.i_int = p_input->input.i_title_start -
589 p_input->input.i_title_offset;
590 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
591 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
592 val.i_int = p_input->input.i_seekpoint_start -
593 p_input->input.i_seekpoint_offset;
594 if( val.i_int > 0 /* TODO: check upper boundary */ )
595 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
599 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
601 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
604 if( p_input->i_start > 0 )
606 if( p_input->i_start >= val.i_time )
608 msg_Warn( p_input, "invalid start-time ignored" );
614 msg_Dbg( p_input, "start-time: %ds",
615 (int)( p_input->i_start / I64C(1000000) ) );
617 s.i_time = p_input->i_start;
618 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
621 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
623 msg_Warn( p_input, "invalid stop-time ignored" );
629 /* Get fps and set it if not already set */
630 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
634 float f_requested_fps;
636 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
637 var_SetFloat( p_input, "sub-original-fps", f_fps );
639 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
640 if( f_requested_fps != f_fps )
642 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT| VLC_VAR_DOINHERIT );
643 var_SetFloat( p_input, "sub-fps", f_requested_fps );
647 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
651 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
655 /* Look for and add subtitle files */
656 psz_subtitle = var_GetString( p_input, "sub-file" );
663 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
665 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
668 sub = InputSourceNew( p_input );
669 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
671 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
674 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
676 if( count.i_int == 0 )
677 count.i_int++; /* if it was first one, there is disable too */
679 if( count.i_int < list.p_list->i_count )
681 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
682 &list.p_list->p_values[count.i_int] );
684 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
689 var_Get( p_input, "sub-autodetect-file", &val );
692 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
693 char **subs = subtitles_Detect( p_input, psz_autopath,
694 p_input->input.p_item->psz_uri );
697 for( i = 0; subs[i] != NULL; i++ )
699 if( strcmp( psz_subtitle, subs[i] ) )
701 sub = InputSourceNew( p_input );
702 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
704 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
710 free( psz_autopath );
712 free( psz_subtitle );
715 psz = var_GetString( p_input, "input-slave" );
719 input_source_t *slave;
722 while( *psz == ' ' || *psz == '#' )
726 if( ( psz_delim = strchr( psz, '#' ) ) )
735 msg_Dbg( p_input, "adding slave '%s'", psz );
736 slave = InputSourceNew( p_input );
737 if( !InputSourceInit( p_input, slave, psz, NULL ) )
739 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
744 if( psz ) free( psz );
747 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
748 i_es_out_mode = ES_OUT_MODE_AUTO;
750 if( p_input->p_sout )
752 var_Get( p_input, "sout-all", &val );
755 i_es_out_mode = ES_OUT_MODE_ALL;
760 var_Get( p_input, "programs", &val );
761 if ( val.p_list && val.p_list->i_count )
763 i_es_out_mode = ES_OUT_MODE_PARTIAL;
764 /* Note : we should remove the "program" callback. */
767 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
770 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
772 /* Inform the demuxer about waited group (needed only for DVB) */
773 if( i_es_out_mode == ES_OUT_MODE_ALL )
775 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
777 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
779 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
784 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
785 (int) var_GetInteger( p_input, "program" ), NULL );
788 if( p_input->p_sout )
790 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
792 p_input->b_out_pace_control = VLC_FALSE;
796 p_input->b_out_pace_control = VLC_TRUE;
798 msg_Dbg( p_input, "starting in %s mode",
799 p_input->b_out_pace_control ? "asynch" : "synch" );
802 /* Get meta data from users */
803 p_meta_tmp = InputMetaUser( p_input );
805 /* Get meta data from master input */
806 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
814 else if( p_meta_tmp )
816 vlc_meta_Merge( p_meta, p_meta_tmp );
817 vlc_meta_Delete( p_meta_tmp );
820 if( !p_input->input.p_access ||
821 access2_Control( p_input->input.p_access, ACCESS_GET_META, &p_meta_tmp))
828 else if( p_meta_tmp )
830 vlc_meta_Merge( p_meta, p_meta_tmp );
831 vlc_meta_Delete( p_meta_tmp );
834 /* Get meta data from slave input */
835 for( i = 0; i < p_input->i_slave; i++ )
837 vlc_meta_t *p_meta_slave;
839 if( !demux2_Control( p_input->slave[i]->p_demux,
840 DEMUX_GET_META, &p_meta_slave ) )
844 p_meta = p_meta_slave;
846 else if( p_meta_slave )
848 vlc_meta_Merge( p_meta, p_meta_slave );
849 vlc_meta_Delete( p_meta_slave );
853 if( p_input->slave[i]->p_access &&
854 !access2_Control( p_input->slave[i]->p_access,
855 ACCESS_GET_META, &p_meta_slave ) )
859 p_meta = p_meta_slave;
861 else if( p_meta_slave )
863 vlc_meta_Merge( p_meta, p_meta_slave );
864 vlc_meta_Delete( p_meta_slave );
870 p_input->p_meta = p_meta;
871 UpdateMeta( p_input );
873 msg_Dbg( p_input, "`%s' sucessfully opened",
874 p_input->input.p_item->psz_uri );
876 /* Trigger intf update for this item */
877 /* Playlist has a callback on this variable and will forward
879 var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
881 /* initialization is complete */
882 p_input->i_state = PLAYING_S;
884 val.i_int = PLAYING_S;
885 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
890 if( p_input->p_es_out )
891 input_EsOutDelete( p_input->p_es_out );
893 if( p_input->p_sout )
894 sout_DeleteInstance( p_input->p_sout );
896 /* Mark them deleted */
897 p_input->input.p_demux = NULL;
898 p_input->input.p_stream = NULL;
899 p_input->input.p_access = NULL;
900 p_input->p_es_out = NULL;
901 p_input->p_sout = NULL;
906 /*****************************************************************************
907 * Error: RunThread() error loop
908 *****************************************************************************
909 * This function is called when an error occurred during thread main's loop.
910 *****************************************************************************/
911 static void Error( input_thread_t *p_input )
913 while( !p_input->b_die )
916 msleep( INPUT_IDLE_SLEEP );
920 /*****************************************************************************
921 * End: end the input thread
922 *****************************************************************************/
923 static void End( input_thread_t * p_input )
928 msg_Dbg( p_input, "closing input" );
930 /* We are at the end */
931 p_input->i_state = END_S;
934 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
936 /* Clean control variables */
937 input_ControlVarClean( p_input );
939 /* Clean up master */
940 InputSourceClean( p_input, &p_input->input );
943 for( i = 0; i < p_input->i_slave; i++ )
945 InputSourceClean( p_input, p_input->slave[i] );
946 free( p_input->slave[i] );
948 if( p_input->slave ) free( p_input->slave );
950 /* Unload all modules */
951 if( p_input->p_es_out )
952 input_EsOutDelete( p_input->p_es_out );
954 /* Close optional stream output instance */
955 if( p_input->p_sout )
958 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
961 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
963 /* attach sout to the playlist */
964 msg_Warn( p_input, "keeping sout" );
965 vlc_object_detach( p_input->p_sout );
966 vlc_object_attach( p_input->p_sout, p_pl );
970 msg_Warn( p_input, "destroying sout" );
971 sout_DeleteInstance( p_input->p_sout );
974 vlc_object_release( p_pl );
978 if( p_input->p_meta )
979 vlc_meta_Delete( p_input->p_meta );
981 /* Tell we're dead */
982 p_input->b_dead = VLC_TRUE;
985 /*****************************************************************************
987 *****************************************************************************/
988 static inline int ControlPopNoLock( input_thread_t *p_input,
989 int *pi_type, vlc_value_t *p_val )
991 if( p_input->i_control <= 0 )
996 *pi_type = p_input->control[0].i_type;
997 *p_val = p_input->control[0].val;
999 p_input->i_control--;
1000 if( p_input->i_control > 0 )
1004 for( i = 0; i < p_input->i_control; i++ )
1006 p_input->control[i].i_type = p_input->control[i+1].i_type;
1007 p_input->control[i].val = p_input->control[i+1].val;
1014 static void ControlReduce( input_thread_t *p_input )
1017 for( i = 1; i < p_input->i_control; i++ )
1019 const int i_lt = p_input->control[i-1].i_type;
1020 const int i_ct = p_input->control[i].i_type;
1022 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1023 msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1026 ( i_ct == INPUT_CONTROL_SET_STATE ||
1027 i_ct == INPUT_CONTROL_SET_RATE ||
1028 i_ct == INPUT_CONTROL_SET_POSITION ||
1029 i_ct == INPUT_CONTROL_SET_TIME ||
1030 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1031 i_ct == INPUT_CONTROL_SET_TITLE ||
1032 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1033 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1036 msg_Dbg( p_input, "merged at %d", i );
1037 /* Remove the i-1 */
1038 for( j = i; j < p_input->i_control; j++ )
1039 p_input->control[j-1] = p_input->control[j];
1040 p_input->i_control--;
1044 /* TODO but that's not that important
1045 - merge SET_X with SET_X_CMD
1046 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1047 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1054 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1057 vlc_bool_t b_force_update = VLC_FALSE;
1061 case INPUT_CONTROL_SET_DIE:
1062 msg_Dbg( p_input, "control: INPUT_CONTROL_SET_DIE proceed" );
1063 /* Mark all submodules to die */
1064 if( p_input->input.p_access )
1065 p_input->input.p_access->b_die = VLC_TRUE;
1066 if( p_input->input.p_stream )
1067 p_input->input.p_stream->b_die = VLC_TRUE;
1068 p_input->input.p_demux->b_die = VLC_TRUE;
1070 p_input->b_die = VLC_TRUE;
1073 case INPUT_CONTROL_SET_POSITION:
1074 case INPUT_CONTROL_SET_POSITION_OFFSET:
1077 if( i_type == INPUT_CONTROL_SET_POSITION )
1079 f_pos = val.f_float;
1083 /* Should not fail */
1084 demux2_Control( p_input->input.p_demux,
1085 DEMUX_GET_POSITION, &f_pos );
1086 f_pos += val.f_float;
1088 if( f_pos < 0.0 ) f_pos = 0.0;
1089 if( f_pos > 1.0 ) f_pos = 1.0;
1090 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1093 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1094 "%2.1f%% failed", f_pos * 100 );
1098 if( p_input->i_slave > 0 )
1099 SlaveSeek( p_input );
1101 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1102 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1103 b_force_update = VLC_TRUE;
1108 case INPUT_CONTROL_SET_TIME:
1109 case INPUT_CONTROL_SET_TIME_OFFSET:
1114 if( i_type == INPUT_CONTROL_SET_TIME )
1116 i_time = val.i_time;
1120 /* Should not fail */
1121 demux2_Control( p_input->input.p_demux,
1122 DEMUX_GET_TIME, &i_time );
1123 i_time += val.i_time;
1125 if( i_time < 0 ) i_time = 0;
1126 i_ret = demux2_Control( p_input->input.p_demux,
1127 DEMUX_SET_TIME, i_time );
1131 /* Emulate it with a SET_POS */
1133 demux2_Control( p_input->input.p_demux,
1134 DEMUX_GET_LENGTH, &i_length );
1137 double f_pos = (double)i_time / (double)i_length;
1138 i_ret = demux2_Control( p_input->input.p_demux,
1139 DEMUX_SET_POSITION, f_pos );
1144 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1145 " failed", i_time );
1149 if( p_input->i_slave > 0 )
1150 SlaveSeek( p_input );
1152 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1154 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1155 b_force_update = VLC_TRUE;
1160 case INPUT_CONTROL_SET_STATE:
1161 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1162 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1165 if( p_input->input.p_access )
1166 i_ret = access2_Control( p_input->input.p_access,
1167 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1169 i_ret = demux2_Control( p_input->input.p_demux,
1170 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1174 /* FIXME What to do ? */
1175 msg_Warn( p_input, "cannot unset pause -> EOF" );
1176 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1179 b_force_update = VLC_TRUE;
1181 /* Switch to play */
1182 p_input->i_state = PLAYING_S;
1183 val.i_int = PLAYING_S;
1184 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1187 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1189 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1190 p_input->b_can_pause )
1193 if( p_input->input.p_access )
1194 i_ret = access2_Control( p_input->input.p_access,
1195 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1197 i_ret = demux2_Control( p_input->input.p_demux,
1198 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1200 b_force_update = VLC_TRUE;
1204 msg_Warn( p_input, "cannot set pause state" );
1205 val.i_int = p_input->i_state;
1209 val.i_int = PAUSE_S;
1212 /* Switch to new state */
1213 p_input->i_state = val.i_int;
1214 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1216 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1218 b_force_update = VLC_TRUE;
1220 /* Correct "state" value */
1221 val.i_int = p_input->i_state;
1222 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1224 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1226 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1230 case INPUT_CONTROL_SET_RATE:
1231 case INPUT_CONTROL_SET_RATE_SLOWER:
1232 case INPUT_CONTROL_SET_RATE_FASTER:
1236 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1237 i_rate = p_input->i_rate * 2;
1238 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1239 i_rate = p_input->i_rate / 2;
1243 if( i_rate < INPUT_RATE_MIN )
1245 msg_Dbg( p_input, "cannot set rate faster" );
1246 i_rate = INPUT_RATE_MIN;
1248 else if( i_rate > INPUT_RATE_MAX )
1250 msg_Dbg( p_input, "cannot set rate slower" );
1251 i_rate = INPUT_RATE_MAX;
1253 if( i_rate != INPUT_RATE_DEFAULT &&
1254 ( !p_input->b_can_pace_control ||
1255 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1257 msg_Dbg( p_input, "cannot change rate" );
1258 i_rate = INPUT_RATE_DEFAULT;
1260 if( i_rate != p_input->i_rate )
1262 p_input->i_rate = i_rate;
1264 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1266 /* We haven't send data to decoder when rate != default */
1267 if( i_rate == INPUT_RATE_DEFAULT )
1268 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1271 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1273 b_force_update = VLC_TRUE;
1278 case INPUT_CONTROL_SET_PROGRAM:
1279 /* No need to force update, es_out does it if needed */
1280 es_out_Control( p_input->p_es_out,
1281 ES_OUT_SET_GROUP, val.i_int );
1283 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1287 case INPUT_CONTROL_SET_ES:
1288 /* No need to force update, es_out does it if needed */
1289 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1290 input_EsOutGetFromID( p_input->p_es_out,
1294 case INPUT_CONTROL_SET_AUDIO_DELAY:
1295 input_EsOutSetDelay( p_input->p_es_out,
1296 AUDIO_ES, val.i_time );
1297 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1300 case INPUT_CONTROL_SET_SPU_DELAY:
1301 input_EsOutSetDelay( p_input->p_es_out,
1302 SPU_ES, val.i_time );
1303 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1306 case INPUT_CONTROL_SET_TITLE:
1307 case INPUT_CONTROL_SET_TITLE_NEXT:
1308 case INPUT_CONTROL_SET_TITLE_PREV:
1309 if( p_input->input.b_title_demux &&
1310 p_input->input.i_title > 0 )
1313 /* FIXME handle demux title */
1314 demux_t *p_demux = p_input->input.p_demux;
1317 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1318 i_title = p_demux->info.i_title - 1;
1319 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1320 i_title = p_demux->info.i_title + 1;
1322 i_title = val.i_int;
1324 if( i_title >= 0 && i_title < p_input->input.i_title )
1326 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1328 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1329 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1331 input_ControlVarTitle( p_input, i_title );
1334 else if( p_input->input.i_title > 0 )
1336 access_t *p_access = p_input->input.p_access;
1339 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1340 i_title = p_access->info.i_title - 1;
1341 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1342 i_title = p_access->info.i_title + 1;
1344 i_title = val.i_int;
1346 if( i_title >= 0 && i_title < p_input->input.i_title )
1348 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1349 stream_AccessReset( p_input->input.p_stream );
1351 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1352 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1356 case INPUT_CONTROL_SET_SEEKPOINT:
1357 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1358 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1359 if( p_input->input.b_title_demux &&
1360 p_input->input.i_title > 0 )
1362 demux_t *p_demux = p_input->input.p_demux;
1365 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1366 i_seekpoint = p_demux->info.i_seekpoint - 1;
1367 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1368 i_seekpoint = p_demux->info.i_seekpoint + 1;
1370 i_seekpoint = val.i_int;
1372 if( i_seekpoint >= 0 && i_seekpoint <
1373 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1375 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1377 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1378 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1381 else if( p_input->input.i_title > 0 )
1383 access_t *p_access = p_input->input.p_access;
1386 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1387 i_seekpoint = p_access->info.i_seekpoint - 1;
1388 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1389 i_seekpoint = p_access->info.i_seekpoint + 1;
1391 i_seekpoint = val.i_int;
1393 if( i_seekpoint >= 0 && i_seekpoint <
1394 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1396 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1397 stream_AccessReset( p_input->input.p_stream );
1399 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1400 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1405 case INPUT_CONTROL_ADD_SLAVE:
1406 if( val.psz_string )
1408 input_source_t *slave = InputSourceNew( p_input );
1410 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1412 vlc_meta_t *p_meta_new = NULL;
1417 msg_Dbg( p_input, "adding %s as slave on the fly",
1421 if( demux2_Control( p_input->input.p_demux,
1422 DEMUX_GET_TIME, &i_time ) )
1424 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1425 InputSourceClean( p_input, slave );
1429 if( demux2_Control( slave->p_demux,
1430 DEMUX_SET_TIME, i_time ) )
1432 msg_Err( p_input, "seek failed for new slave" );
1433 InputSourceClean( p_input, slave );
1439 /* Get meta (access and demux) */
1440 if( access2_Control( slave->p_access,
1441 ACCESS_GET_META, &p_meta_new ) )
1443 if( !demux2_Control( slave->p_demux,
1444 DEMUX_GET_META, &p_meta ) )
1448 vlc_meta_Merge( p_meta_new, p_meta );
1449 vlc_meta_Delete( p_meta );
1453 p_meta_new = p_meta;
1459 if( p_input->p_meta )
1461 vlc_meta_Merge( p_input->p_meta, p_meta_new );
1462 vlc_meta_Delete( p_meta_new );
1466 p_input->p_meta = p_meta_new;
1468 UpdateMeta( p_input );
1471 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1475 msg_Warn( p_input, "failed to add %s as slave",
1479 free( val.psz_string );
1483 case INPUT_CONTROL_SET_BOOKMARK:
1485 msg_Err( p_input, "not yet implemented" );
1489 return b_force_update;
1492 /*****************************************************************************
1494 *****************************************************************************/
1495 static int UpdateFromDemux( input_thread_t *p_input )
1497 demux_t *p_demux = p_input->input.p_demux;
1500 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1502 v.i_int = p_demux->info.i_title;
1503 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1505 input_ControlVarTitle( p_input, p_demux->info.i_title );
1507 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1509 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1511 v.i_int = p_demux->info.i_seekpoint;
1512 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1514 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1516 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1518 /* Hmmm only works with master input */
1519 if( p_input->input.p_demux == p_demux )
1521 int i_title_end = p_input->input.i_title_end -
1522 p_input->input.i_title_offset;
1523 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1524 p_input->input.i_seekpoint_offset;
1526 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1528 if( p_demux->info.i_title > i_title_end ||
1529 ( p_demux->info.i_title == i_title_end &&
1530 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1532 else if( i_seekpoint_end >=0 )
1534 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1536 else if( i_title_end >= 0 )
1538 if( p_demux->info.i_title > i_title_end ) return 0;
1545 /*****************************************************************************
1547 *****************************************************************************/
1548 static int UpdateFromAccess( input_thread_t *p_input )
1550 access_t *p_access = p_input->input.p_access;
1553 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1555 v.i_int = p_access->info.i_title;
1556 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1558 input_ControlVarTitle( p_input, p_access->info.i_title );
1560 stream_AccessUpdate( p_input->input.p_stream );
1562 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1564 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1566 v.i_int = p_access->info.i_seekpoint;
1567 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1569 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1571 if( p_access->info.i_update & INPUT_UPDATE_META )
1573 /* TODO maybe multi - access ? */
1575 if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1577 if( p_input->p_meta )
1579 vlc_meta_Merge( p_input->p_meta, p_meta );
1580 vlc_meta_Delete( p_meta );
1584 p_input->p_meta = p_meta;
1587 UpdateMeta( p_input );
1588 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1590 p_access->info.i_update &= ~INPUT_UPDATE_META;
1593 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1595 /* Hmmm only works with master input */
1596 if( p_input->input.p_access == p_access )
1598 int i_title_end = p_input->input.i_title_end -
1599 p_input->input.i_title_offset;
1600 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1601 p_input->input.i_seekpoint_offset;
1603 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1605 if( p_access->info.i_title > i_title_end ||
1606 ( p_access->info.i_title == i_title_end &&
1607 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1609 else if( i_seekpoint_end >=0 )
1611 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1613 else if( i_title_end >= 0 )
1615 if( p_access->info.i_title > i_title_end ) return 0;
1622 /*****************************************************************************
1624 *****************************************************************************/
1625 static int UpdateMeta( input_thread_t *p_input )
1627 vlc_meta_t *p_meta = p_input->p_meta;
1630 if( !p_meta || p_meta->i_meta == 0 )
1633 msg_Dbg( p_input, "meta information:" );
1634 for( i = 0; i < p_meta->i_meta; i++ )
1636 msg_Dbg( p_input, " - '%s' = '%s'",
1637 _(p_meta->name[i]), p_meta->value[i] );
1639 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] &&
1640 !p_input->input.p_item->b_fixed_name )
1641 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1643 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1644 input_Control( p_input, INPUT_ADD_INFO, _("General"),
1645 _("Author"), p_meta->value[i] );
1647 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1648 _(p_meta->name[i]), "%s", p_meta->value[i] );
1651 for( i = 0; i < p_meta->i_track; i++ )
1653 vlc_meta_t *tk = p_meta->track[i];
1656 if( tk->i_meta > 0 )
1658 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1660 msg_Dbg( p_input, " - track[%d]:", i );
1662 sprintf( psz_cat, "%s %d", _("Stream"), i );
1663 for( j = 0; j < tk->i_meta; j++ )
1665 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1668 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1669 _(tk->name[j]), "%s", tk->value[j] );
1674 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1676 p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1682 /*****************************************************************************
1684 *****************************************************************************/
1685 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
1687 playlist_t *p_playlist;
1688 char psz_buffer[MSTRTIME_MAX_SIZE];
1690 vlc_mutex_lock( &p_input->input.p_item->lock );
1691 p_input->input.p_item->i_duration = i_length;
1692 vlc_mutex_unlock( &p_input->input.p_item->lock );
1693 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_PARENT);
1694 var_SetInteger( p_playlist, "item-change",
1695 p_input->input.p_item->i_id );
1696 vlc_object_release( p_playlist );
1699 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1700 msecstotimestr( psz_buffer, i_length / 1000 ) );
1703 /*****************************************************************************
1705 *****************************************************************************/
1706 static input_source_t *InputSourceNew( input_thread_t *p_input )
1708 input_source_t *in = malloc( sizeof( input_source_t ) );
1711 in->p_access = NULL;
1712 in->p_stream = NULL;
1714 in->b_title_demux = VLC_FALSE;
1717 in->b_can_pace_control = VLC_TRUE;
1718 in->b_eof = VLC_FALSE;
1719 in->i_cr_average = 0;
1724 /*****************************************************************************
1726 *****************************************************************************/
1727 static int InputSourceInit( input_thread_t *p_input,
1728 input_source_t *in, char *psz_mrl,
1729 char *psz_forced_demux )
1731 char *psz_dup = strdup( psz_mrl );
1738 MRLSplit( p_input, psz_dup, &psz_access, &psz_demux, &psz_path );
1740 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1741 psz_mrl, psz_access, psz_demux, psz_path );
1743 /* Hack to allow udp://@:port syntax */
1745 (strncmp( psz_access, "udp", 3 ) && strncmp( psz_access, "rtp", 3 )) )
1747 /* Find optional titles and seekpoints */
1748 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1749 &in->i_seekpoint_start, &in->i_seekpoint_end );
1751 if( psz_forced_demux && *psz_forced_demux )
1752 psz_demux = psz_forced_demux;
1754 /* Try access_demux if no demux given */
1755 if( *psz_demux == '\0' )
1757 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1758 NULL, p_input->p_es_out );
1763 int64_t i_pts_delay;
1765 /* Get infos from access_demux */
1766 demux2_Control( in->p_demux,
1767 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1768 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1770 in->b_title_demux = VLC_TRUE;
1771 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1772 &in->title, &in->i_title,
1773 &in->i_title_offset, &in->i_seekpoint_offset ) )
1778 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1779 &in->b_can_pace_control );
1780 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1784 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1790 int64_t i_pts_delay;
1792 /* Now try a real access */
1793 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path );
1795 /* Access failed, URL encoded ? */
1796 if( in->p_access == NULL && strchr( psz_path, '%' ) )
1798 DecodeUrl( psz_path );
1800 msg_Dbg( p_input, "retying with access `%s' demux `%s' path `%s'",
1801 psz_access, psz_demux, psz_path );
1803 in->p_access = access2_New( p_input,
1804 psz_access, psz_demux, psz_path );
1806 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
1807 * are forbidden in filenames on Win32. */
1809 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1810 if( in->p_access == NULL &&
1811 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1814 psz_dup = strdup( psz_mrl );
1819 in->p_access = access2_New( p_input,
1820 psz_access, psz_demux, psz_path );
1824 if( in->p_access == NULL )
1826 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
1830 /* Get infos from access */
1831 access2_Control( in->p_access,
1832 ACCESS_GET_PTS_DELAY, &i_pts_delay );
1833 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1835 in->b_title_demux = VLC_FALSE;
1836 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
1837 &in->title, &in->i_title,
1838 &in->i_title_offset, &in->i_seekpoint_offset ) )
1844 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
1845 &in->b_can_pace_control );
1846 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
1848 access2_Control( in->p_access, ACCESS_CAN_SEEK,
1850 var_Set( p_input, "seekable", val );
1852 /* Create the stream_t */
1853 in->p_stream = stream_AccessNew( in->p_access );
1854 if( in->p_stream == NULL )
1856 msg_Warn( p_input, "cannot create a stream_t from access" );
1860 /* Open a demuxer */
1861 if( *psz_demux == '\0' && *in->p_access->psz_demux )
1863 psz_demux = in->p_access->psz_demux;
1865 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1866 in->p_stream, p_input->p_es_out );
1867 if( in->p_demux == NULL )
1869 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
1870 psz_access, psz_demux, psz_path );
1874 /* TODO get title from demux */
1875 if( in->i_title <= 0 )
1877 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1878 &in->title, &in->i_title,
1879 &in->i_title_offset, &in->i_seekpoint_offset ))
1886 in->b_title_demux = VLC_TRUE;
1895 demux2_Delete( in->p_demux );
1898 stream_AccessDelete( in->p_stream );
1901 access2_Delete( in->p_access );
1904 return VLC_EGENERIC;
1907 /*****************************************************************************
1909 *****************************************************************************/
1910 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
1913 demux2_Delete( in->p_demux );
1916 stream_AccessDelete( in->p_stream );
1919 access2_Delete( in->p_access );
1921 if( in->i_title > 0 )
1924 for( i = 0; i < in->i_title; i++ )
1926 vlc_input_title_Delete( in->title[i] );
1932 static void SlaveDemux( input_thread_t *p_input )
1936 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1938 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1942 for( i = 0; i < p_input->i_slave; i++ )
1944 input_source_t *in = p_input->slave[i];
1950 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
1955 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
1957 msg_Err( p_input, "slave[%d] doesn't like "
1958 "DEMUX_GET_TIME -> EOF", i );
1963 if( i_stime >= i_time )
1966 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
1972 i_ret = in->p_demux->pf_demux( in->p_demux );
1977 msg_Dbg( p_input, "slave %d EOF", i );
1978 in->b_eof = VLC_TRUE;
1983 static void SlaveSeek( input_thread_t *p_input )
1988 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1990 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1994 for( i = 0; i < p_input->i_slave; i++ )
1996 input_source_t *in = p_input->slave[i];
1998 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2000 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2001 in->b_eof = VLC_TRUE;
2005 /*****************************************************************************
2007 *****************************************************************************/
2008 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2013 if( ( p_meta = vlc_meta_New() ) == NULL )
2016 /* Get meta information from user */
2017 #define GET_META( c, s ) \
2018 var_Get( p_input, (s), &val ); \
2019 if( *val.psz_string ) \
2020 vlc_meta_Add( p_meta, c, val.psz_string ); \
2021 free( val.psz_string )
2023 GET_META( VLC_META_TITLE, "meta-title" );
2024 GET_META( VLC_META_AUTHOR, "meta-author" );
2025 GET_META( VLC_META_ARTIST, "meta-artist" );
2026 GET_META( VLC_META_GENRE, "meta-genre" );
2027 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2028 GET_META( VLC_META_DESCRIPTION, "meta-description" );
2029 GET_META( VLC_META_DATE, "meta-date" );
2030 GET_META( VLC_META_URL, "meta-url" );
2036 /*****************************************************************************
2037 * DecodeUrl: decode a given encoded url
2038 *****************************************************************************/
2039 static void DecodeUrl( char *psz )
2041 char *dup = strdup( psz );
2059 *psz++ = strtol( val, NULL, 16 );
2061 else if( *p == '+' )
2075 /*****************************************************************************
2076 * ParseOption: parses the options for the input
2077 *****************************************************************************
2078 * This function parses the input (config) options and creates their associated
2080 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
2081 * bar is the value of the option.
2082 *****************************************************************************/
2083 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2085 char *psz_name = (char *)psz_option;
2086 char *psz_value = strchr( psz_option, '=' );
2087 int i_name_len, i_type;
2088 vlc_bool_t b_isno = VLC_FALSE;
2091 if( psz_value ) i_name_len = psz_value - psz_option;
2092 else i_name_len = strlen( psz_option );
2094 /* It's too much of an hassle to remove the ':' when we parse
2095 * the cmd line :) */
2096 if( i_name_len && *psz_name == ':' )
2102 if( i_name_len == 0 ) return;
2104 psz_name = strndup( psz_name, i_name_len );
2105 if( psz_value ) psz_value++;
2107 i_type = config_GetType( p_input, psz_name );
2109 if( !i_type && !psz_value )
2111 /* check for "no-foo" or "nofoo" */
2112 if( !strncmp( psz_name, "no-", 3 ) )
2114 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2116 else if( !strncmp( psz_name, "no", 2 ) )
2118 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2120 else goto cleanup; /* Option doesn't exist */
2123 i_type = config_GetType( p_input, psz_name );
2125 if( !i_type ) goto cleanup; /* Option doesn't exist */
2127 else if( !i_type ) goto cleanup; /* Option doesn't exist */
2129 if( ( i_type != VLC_VAR_BOOL ) &&
2130 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2132 /* Create the variable in the input object.
2133 * Children of the input object will be able to retreive this value
2134 * thanks to the inheritance property of the object variables. */
2135 var_Create( p_input, psz_name, i_type );
2140 val.b_bool = !b_isno;
2143 case VLC_VAR_INTEGER:
2144 val.i_int = atoi( psz_value );
2148 val.f_float = atof( psz_value );
2151 case VLC_VAR_STRING:
2152 case VLC_VAR_MODULE:
2154 case VLC_VAR_DIRECTORY:
2155 val.psz_string = psz_value;
2163 var_Set( p_input, psz_name, val );
2165 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2168 if( psz_name ) free( psz_name );
2172 /*****************************************************************************
2173 * MRLSplit: parse the access, demux and url part of the
2174 * Media Resource Locator.
2175 *****************************************************************************/
2176 static void MRLSplit( input_thread_t *p_input, char *psz_dup,
2177 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2179 char *psz_access = NULL;
2180 char *psz_demux = NULL;
2181 char *psz_path = NULL;
2182 char *psz, *psz_check;
2184 psz = strchr( psz_dup, ':' );
2186 /* '@' not allowed in access/demux part */
2187 psz_check = strchr( psz_dup, '@' );
2188 if( psz_check && psz_check < psz ) psz = 0;
2190 #if defined( WIN32 ) || defined( UNDER_CE )
2191 if( psz - psz_dup == 1 )
2193 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2202 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2206 psz = strchr( psz_dup, '/' );
2213 psz_access = psz_dup;
2220 if( !psz_access ) *ppsz_access = "";
2221 else *ppsz_access = psz_access;
2223 if( !psz_demux ) *ppsz_demux = "";
2224 else *ppsz_demux = psz_demux;
2226 if( !psz_path ) *ppsz_path = "";
2227 else *ppsz_path = psz_path;
2230 /*****************************************************************************
2231 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2234 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2235 *****************************************************************************/
2236 static void MRLSections( input_thread_t *p_input, char *psz_source,
2237 int *pi_title_start, int *pi_title_end,
2238 int *pi_chapter_start, int *pi_chapter_end )
2240 char *psz, *psz_end, *psz_next, *psz_check;
2242 *pi_title_start = *pi_title_end = -1;
2243 *pi_chapter_start = *pi_chapter_end = -1;
2245 /* Start by parsing titles and chapters */
2246 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2248 /* Check we are really dealing with a title/chapter section */
2249 psz_check = psz + 1;
2250 if( !*psz_check ) return;
2251 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2252 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2253 if( *psz_check == ':' && ++psz_check )
2254 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2255 if( *psz_check != '-' && *psz_check ) return;
2256 if( *psz_check == '-' && ++psz_check )
2257 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2258 if( *psz_check != ':' && *psz_check ) return;
2259 if( *psz_check == ':' && ++psz_check )
2260 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2261 if( *psz_check ) return;
2263 /* Separate start and end */
2265 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2267 /* Look for the start title */
2268 *pi_title_start = strtol( psz, &psz_next, 0 );
2269 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2270 *pi_title_end = *pi_title_start;
2273 /* Look for the start chapter */
2275 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2276 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2277 *pi_chapter_end = *pi_chapter_start;
2281 /* Look for the end title */
2282 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2283 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2286 /* Look for the end chapter */
2287 if( *psz_end ) psz_end++;
2288 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2289 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2292 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2293 psz_source, *pi_title_start, *pi_chapter_start,
2294 *pi_title_end, *pi_chapter_end );