1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
33 #include <vlc/decoder.h>
36 #include "input_internal.h"
38 #include "stream_output.h"
40 #include "vlc_interface.h"
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" );
718 char *psz_delim = strchr( psz, '#' );
722 input_source_t *slave;
737 msg_Dbg( p_input, "adding slave '%s'", psz );
738 slave = InputSourceNew( p_input );
739 if( !InputSourceInit( p_input, slave, psz, NULL ) )
741 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
750 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
751 i_es_out_mode = ES_OUT_MODE_AUTO;
753 if( p_input->p_sout )
755 var_Get( p_input, "sout-all", &val );
758 i_es_out_mode = ES_OUT_MODE_ALL;
763 var_Get( p_input, "programs", &val );
764 if ( val.p_list && val.p_list->i_count )
766 i_es_out_mode = ES_OUT_MODE_PARTIAL;
767 /* Note : we should remove the "program" callback. */
770 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
773 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
775 /* Inform the demuxer about waited group (needed only for DVB) */
776 if( i_es_out_mode == ES_OUT_MODE_ALL )
778 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
780 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
782 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
787 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
788 (int) var_GetInteger( p_input, "program" ), NULL );
791 if( p_input->p_sout )
793 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
795 p_input->b_out_pace_control = VLC_FALSE;
799 p_input->b_out_pace_control = VLC_TRUE;
801 msg_Dbg( p_input, "starting in %s mode",
802 p_input->b_out_pace_control ? "asynch" : "synch" );
805 /* Get meta data from users */
806 p_meta_tmp = InputMetaUser( p_input );
808 /* Get meta data from master input */
809 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
817 else if( p_meta_tmp )
819 vlc_meta_Merge( p_meta, p_meta_tmp );
820 vlc_meta_Delete( p_meta_tmp );
823 if( !p_input->input.p_access ||
824 access2_Control( p_input->input.p_access, ACCESS_GET_META, &p_meta_tmp))
831 else if( p_meta_tmp )
833 vlc_meta_Merge( p_meta, p_meta_tmp );
834 vlc_meta_Delete( p_meta_tmp );
837 /* Get meta data from slave input */
838 for( i = 0; i < p_input->i_slave; i++ )
840 vlc_meta_t *p_meta_slave;
842 if( !demux2_Control( p_input->slave[i]->p_demux,
843 DEMUX_GET_META, &p_meta_slave ) )
847 p_meta = p_meta_slave;
849 else if( p_meta_slave )
851 vlc_meta_Merge( p_meta, p_meta_slave );
852 vlc_meta_Delete( p_meta_slave );
856 if( p_input->slave[i]->p_access &&
857 !access2_Control( p_input->slave[i]->p_access,
858 ACCESS_GET_META, &p_meta_slave ) )
862 p_meta = p_meta_slave;
864 else if( p_meta_slave )
866 vlc_meta_Merge( p_meta, p_meta_slave );
867 vlc_meta_Delete( p_meta_slave );
873 p_input->p_meta = p_meta;
874 UpdateMeta( p_input );
876 msg_Dbg( p_input, "`%s' sucessfully opened",
877 p_input->input.p_item->psz_uri );
879 /* Trigger intf update for this item */
880 /* Playlist has a callback on this variable and will forward
882 var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
884 /* initialization is complete */
885 p_input->i_state = PLAYING_S;
887 val.i_int = PLAYING_S;
888 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
893 if( p_input->p_es_out )
894 input_EsOutDelete( p_input->p_es_out );
896 if( p_input->p_sout )
897 sout_DeleteInstance( p_input->p_sout );
899 /* Mark them deleted */
900 p_input->input.p_demux = NULL;
901 p_input->input.p_stream = NULL;
902 p_input->input.p_access = NULL;
903 p_input->p_es_out = NULL;
904 p_input->p_sout = NULL;
909 /*****************************************************************************
910 * Error: RunThread() error loop
911 *****************************************************************************
912 * This function is called when an error occurred during thread main's loop.
913 *****************************************************************************/
914 static void Error( input_thread_t *p_input )
916 while( !p_input->b_die )
919 msleep( INPUT_IDLE_SLEEP );
923 /*****************************************************************************
924 * End: end the input thread
925 *****************************************************************************/
926 static void End( input_thread_t * p_input )
931 msg_Dbg( p_input, "closing input" );
933 /* We are at the end */
934 p_input->i_state = END_S;
937 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
939 /* Clean control variables */
940 input_ControlVarClean( p_input );
942 /* Clean up master */
943 InputSourceClean( p_input, &p_input->input );
946 for( i = 0; i < p_input->i_slave; i++ )
948 InputSourceClean( p_input, p_input->slave[i] );
949 free( p_input->slave[i] );
951 if( p_input->slave ) free( p_input->slave );
953 /* Unload all modules */
954 if( p_input->p_es_out )
955 input_EsOutDelete( p_input->p_es_out );
957 /* Close optional stream output instance */
958 if( p_input->p_sout )
961 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
964 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
966 /* attach sout to the playlist */
967 msg_Warn( p_input, "keeping sout" );
968 vlc_object_detach( p_input->p_sout );
969 vlc_object_attach( p_input->p_sout, p_pl );
973 msg_Warn( p_input, "destroying sout" );
974 sout_DeleteInstance( p_input->p_sout );
977 vlc_object_release( p_pl );
981 if( p_input->p_meta )
982 vlc_meta_Delete( p_input->p_meta );
984 /* Tell we're dead */
985 p_input->b_dead = VLC_TRUE;
988 /*****************************************************************************
990 *****************************************************************************/
991 static inline int ControlPopNoLock( input_thread_t *p_input,
992 int *pi_type, vlc_value_t *p_val )
994 if( p_input->i_control <= 0 )
999 *pi_type = p_input->control[0].i_type;
1000 *p_val = p_input->control[0].val;
1002 p_input->i_control--;
1003 if( p_input->i_control > 0 )
1007 for( i = 0; i < p_input->i_control; i++ )
1009 p_input->control[i].i_type = p_input->control[i+1].i_type;
1010 p_input->control[i].val = p_input->control[i+1].val;
1017 static void ControlReduce( input_thread_t *p_input )
1020 for( i = 1; i < p_input->i_control; i++ )
1022 const int i_lt = p_input->control[i-1].i_type;
1023 const int i_ct = p_input->control[i].i_type;
1025 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1026 msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1029 ( i_ct == INPUT_CONTROL_SET_STATE ||
1030 i_ct == INPUT_CONTROL_SET_RATE ||
1031 i_ct == INPUT_CONTROL_SET_POSITION ||
1032 i_ct == INPUT_CONTROL_SET_TIME ||
1033 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1034 i_ct == INPUT_CONTROL_SET_TITLE ||
1035 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1036 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1039 msg_Dbg( p_input, "merged at %d", i );
1040 /* Remove the i-1 */
1041 for( j = i; j < p_input->i_control; j++ )
1042 p_input->control[j-1] = p_input->control[j];
1043 p_input->i_control--;
1047 /* TODO but that's not that important
1048 - merge SET_X with SET_X_CMD
1049 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1050 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1057 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1060 vlc_bool_t b_force_update = VLC_FALSE;
1064 case INPUT_CONTROL_SET_DIE:
1065 msg_Dbg( p_input, "control: INPUT_CONTROL_SET_DIE proceed" );
1066 /* Mark all submodules to die */
1067 if( p_input->input.p_access )
1068 p_input->input.p_access->b_die = VLC_TRUE;
1069 if( p_input->input.p_stream )
1070 p_input->input.p_stream->b_die = VLC_TRUE;
1071 p_input->input.p_demux->b_die = VLC_TRUE;
1073 p_input->b_die = VLC_TRUE;
1076 case INPUT_CONTROL_SET_POSITION:
1077 case INPUT_CONTROL_SET_POSITION_OFFSET:
1080 if( i_type == INPUT_CONTROL_SET_POSITION )
1082 f_pos = val.f_float;
1086 /* Should not fail */
1087 demux2_Control( p_input->input.p_demux,
1088 DEMUX_GET_POSITION, &f_pos );
1089 f_pos += val.f_float;
1091 if( f_pos < 0.0 ) f_pos = 0.0;
1092 if( f_pos > 1.0 ) f_pos = 1.0;
1093 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1096 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1097 "%2.1f%% failed", f_pos * 100 );
1101 if( p_input->i_slave > 0 )
1102 SlaveSeek( p_input );
1104 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1105 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1106 b_force_update = VLC_TRUE;
1111 case INPUT_CONTROL_SET_TIME:
1112 case INPUT_CONTROL_SET_TIME_OFFSET:
1117 if( i_type == INPUT_CONTROL_SET_TIME )
1119 i_time = val.i_time;
1123 /* Should not fail */
1124 demux2_Control( p_input->input.p_demux,
1125 DEMUX_GET_TIME, &i_time );
1126 i_time += val.i_time;
1128 if( i_time < 0 ) i_time = 0;
1129 i_ret = demux2_Control( p_input->input.p_demux,
1130 DEMUX_SET_TIME, i_time );
1134 /* Emulate it with a SET_POS */
1136 demux2_Control( p_input->input.p_demux,
1137 DEMUX_GET_LENGTH, &i_length );
1140 double f_pos = (double)i_time / (double)i_length;
1141 i_ret = demux2_Control( p_input->input.p_demux,
1142 DEMUX_SET_POSITION, f_pos );
1147 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1148 " failed", i_time );
1152 if( p_input->i_slave > 0 )
1153 SlaveSeek( p_input );
1155 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1157 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1158 b_force_update = VLC_TRUE;
1163 case INPUT_CONTROL_SET_STATE:
1164 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1165 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1168 if( p_input->input.p_access )
1169 i_ret = access2_Control( p_input->input.p_access,
1170 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1172 i_ret = demux2_Control( p_input->input.p_demux,
1173 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1177 /* FIXME What to do ? */
1178 msg_Warn( p_input, "cannot unset pause -> EOF" );
1179 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1182 b_force_update = VLC_TRUE;
1184 /* Switch to play */
1185 p_input->i_state = PLAYING_S;
1186 val.i_int = PLAYING_S;
1187 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1190 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1192 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1193 p_input->b_can_pause )
1196 if( p_input->input.p_access )
1197 i_ret = access2_Control( p_input->input.p_access,
1198 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1200 i_ret = demux2_Control( p_input->input.p_demux,
1201 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1203 b_force_update = VLC_TRUE;
1207 msg_Warn( p_input, "cannot set pause state" );
1208 val.i_int = p_input->i_state;
1212 val.i_int = PAUSE_S;
1215 /* Switch to new state */
1216 p_input->i_state = val.i_int;
1217 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1219 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1221 b_force_update = VLC_TRUE;
1223 /* Correct "state" value */
1224 val.i_int = p_input->i_state;
1225 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1227 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1229 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1233 case INPUT_CONTROL_SET_RATE:
1234 case INPUT_CONTROL_SET_RATE_SLOWER:
1235 case INPUT_CONTROL_SET_RATE_FASTER:
1239 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1240 i_rate = p_input->i_rate * 2;
1241 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1242 i_rate = p_input->i_rate / 2;
1246 if( i_rate < INPUT_RATE_MIN )
1248 msg_Dbg( p_input, "cannot set rate faster" );
1249 i_rate = INPUT_RATE_MIN;
1251 else if( i_rate > INPUT_RATE_MAX )
1253 msg_Dbg( p_input, "cannot set rate slower" );
1254 i_rate = INPUT_RATE_MAX;
1256 if( i_rate != INPUT_RATE_DEFAULT &&
1257 ( !p_input->b_can_pace_control ||
1258 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1260 msg_Dbg( p_input, "cannot change rate" );
1261 i_rate = INPUT_RATE_DEFAULT;
1263 if( i_rate != p_input->i_rate )
1265 p_input->i_rate = i_rate;
1267 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1269 /* We haven't send data to decoder when rate != default */
1270 if( i_rate == INPUT_RATE_DEFAULT )
1271 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1274 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1276 b_force_update = VLC_TRUE;
1281 case INPUT_CONTROL_SET_PROGRAM:
1282 /* No need to force update, es_out does it if needed */
1283 es_out_Control( p_input->p_es_out,
1284 ES_OUT_SET_GROUP, val.i_int );
1286 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1290 case INPUT_CONTROL_SET_ES:
1291 /* No need to force update, es_out does it if needed */
1292 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1293 input_EsOutGetFromID( p_input->p_es_out,
1297 case INPUT_CONTROL_SET_AUDIO_DELAY:
1298 input_EsOutSetDelay( p_input->p_es_out,
1299 AUDIO_ES, val.i_time );
1300 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1303 case INPUT_CONTROL_SET_SPU_DELAY:
1304 input_EsOutSetDelay( p_input->p_es_out,
1305 SPU_ES, val.i_time );
1306 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1309 case INPUT_CONTROL_SET_TITLE:
1310 case INPUT_CONTROL_SET_TITLE_NEXT:
1311 case INPUT_CONTROL_SET_TITLE_PREV:
1312 if( p_input->input.b_title_demux &&
1313 p_input->input.i_title > 0 )
1316 /* FIXME handle demux title */
1317 demux_t *p_demux = p_input->input.p_demux;
1320 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1321 i_title = p_demux->info.i_title - 1;
1322 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1323 i_title = p_demux->info.i_title + 1;
1325 i_title = val.i_int;
1327 if( i_title >= 0 && i_title < p_input->input.i_title )
1329 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1331 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1332 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1334 input_ControlVarTitle( p_input, i_title );
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_TITLE_PREV )
1343 i_title = p_access->info.i_title - 1;
1344 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1345 i_title = p_access->info.i_title + 1;
1347 i_title = val.i_int;
1349 if( i_title >= 0 && i_title < p_input->input.i_title )
1351 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1352 stream_AccessReset( p_input->input.p_stream );
1354 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1355 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1359 case INPUT_CONTROL_SET_SEEKPOINT:
1360 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1361 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1362 if( p_input->input.b_title_demux &&
1363 p_input->input.i_title > 0 )
1365 demux_t *p_demux = p_input->input.p_demux;
1368 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1369 i_seekpoint = p_demux->info.i_seekpoint - 1;
1370 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1371 i_seekpoint = p_demux->info.i_seekpoint + 1;
1373 i_seekpoint = val.i_int;
1375 if( i_seekpoint >= 0 && i_seekpoint <
1376 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1378 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1380 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1381 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1384 else if( p_input->input.i_title > 0 )
1386 access_t *p_access = p_input->input.p_access;
1389 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1390 i_seekpoint = p_access->info.i_seekpoint - 1;
1391 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1392 i_seekpoint = p_access->info.i_seekpoint + 1;
1394 i_seekpoint = val.i_int;
1396 if( i_seekpoint >= 0 && i_seekpoint <
1397 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1399 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1400 stream_AccessReset( p_input->input.p_stream );
1402 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1403 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1408 case INPUT_CONTROL_SET_BOOKMARK:
1410 msg_Err( p_input, "not yet implemented" );
1414 return b_force_update;
1417 /*****************************************************************************
1419 *****************************************************************************/
1420 static int UpdateFromDemux( input_thread_t *p_input )
1422 demux_t *p_demux = p_input->input.p_demux;
1425 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1427 v.i_int = p_demux->info.i_title;
1428 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1430 input_ControlVarTitle( p_input, p_demux->info.i_title );
1432 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1434 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1436 v.i_int = p_demux->info.i_seekpoint;
1437 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1439 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1441 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1443 /* Hmmm only works with master input */
1444 if( p_input->input.p_demux == p_demux )
1446 int i_title_end = p_input->input.i_title_end -
1447 p_input->input.i_title_offset;
1448 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1449 p_input->input.i_seekpoint_offset;
1451 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1453 if( p_demux->info.i_title > i_title_end ||
1454 ( p_demux->info.i_title == i_title_end &&
1455 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1457 else if( i_seekpoint_end >=0 )
1459 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1461 else if( i_title_end >= 0 )
1463 if( p_demux->info.i_title > i_title_end ) return 0;
1470 /*****************************************************************************
1472 *****************************************************************************/
1473 static int UpdateFromAccess( input_thread_t *p_input )
1475 access_t *p_access = p_input->input.p_access;
1478 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1480 v.i_int = p_access->info.i_title;
1481 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1483 input_ControlVarTitle( p_input, p_access->info.i_title );
1485 stream_AccessUpdate( p_input->input.p_stream );
1487 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1489 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1491 v.i_int = p_access->info.i_seekpoint;
1492 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1494 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1496 if( p_access->info.i_update & INPUT_UPDATE_META )
1498 /* TODO maybe multi - access ? */
1500 if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1502 if( p_input->p_meta )
1504 vlc_meta_Merge( p_input->p_meta, p_meta );
1505 vlc_meta_Delete( p_meta );
1509 p_input->p_meta = p_meta;
1512 UpdateMeta( p_input );
1514 p_access->info.i_update &= ~INPUT_UPDATE_META;
1517 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1519 /* Hmmm only works with master input */
1520 if( p_input->input.p_access == p_access )
1522 int i_title_end = p_input->input.i_title_end -
1523 p_input->input.i_title_offset;
1524 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1525 p_input->input.i_seekpoint_offset;
1527 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1529 if( p_access->info.i_title > i_title_end ||
1530 ( p_access->info.i_title == i_title_end &&
1531 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1533 else if( i_seekpoint_end >=0 )
1535 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1537 else if( i_title_end >= 0 )
1539 if( p_access->info.i_title > i_title_end ) return 0;
1546 /*****************************************************************************
1548 *****************************************************************************/
1549 static int UpdateMeta( input_thread_t *p_input )
1551 vlc_meta_t *p_meta = p_input->p_meta;
1554 if( !p_meta || p_meta->i_meta == 0 )
1557 msg_Dbg( p_input, "meta information:" );
1558 for( i = 0; i < p_meta->i_meta; i++ )
1560 msg_Dbg( p_input, " - '%s' = '%s'",
1561 _(p_meta->name[i]), p_meta->value[i] );
1563 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] )
1564 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1566 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1567 input_Control( p_input, INPUT_ADD_INFO, _("General"),
1568 _("Author"), p_meta->value[i] );
1570 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1571 _(p_meta->name[i]), "%s", p_meta->value[i] );
1574 for( i = 0; i < p_meta->i_track; i++ )
1576 vlc_meta_t *tk = p_meta->track[i];
1579 if( tk->i_meta > 0 )
1581 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1583 msg_Dbg( p_input, " - track[%d]:", i );
1585 sprintf( psz_cat, "%s %d", _("Stream"), i );
1586 for( j = 0; j < tk->i_meta; j++ )
1588 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1591 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1592 _(tk->name[j]), "%s", tk->value[j] );
1597 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1599 p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1605 /*****************************************************************************
1607 *****************************************************************************/
1608 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
1610 char psz_buffer[MSTRTIME_MAX_SIZE];
1612 vlc_mutex_lock( &p_input->input.p_item->lock );
1613 p_input->input.p_item->i_duration = i_length;
1614 vlc_mutex_unlock( &p_input->input.p_item->lock );
1616 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1617 msecstotimestr( psz_buffer, i_length / 1000 ) );
1620 /*****************************************************************************
1622 *****************************************************************************/
1623 static input_source_t *InputSourceNew( input_thread_t *p_input )
1625 input_source_t *in = malloc( sizeof( input_source_t ) );
1628 in->p_access = NULL;
1629 in->p_stream = NULL;
1631 in->b_title_demux = VLC_FALSE;
1634 in->b_can_pace_control = VLC_TRUE;
1635 in->b_eof = VLC_FALSE;
1636 in->i_cr_average = 0;
1641 /*****************************************************************************
1643 *****************************************************************************/
1644 static int InputSourceInit( input_thread_t *p_input,
1645 input_source_t *in, char *psz_mrl,
1646 char *psz_forced_demux )
1648 char *psz_dup = strdup( psz_mrl );
1655 MRLSplit( p_input, psz_dup, &psz_access, &psz_demux, &psz_path );
1657 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1658 psz_mrl, psz_access, psz_demux, psz_path );
1660 /* Hack to allow udp://@:port syntax */
1662 (strncmp( psz_access, "udp", 3 ) && strncmp( psz_access, "rtp", 3 )) )
1664 /* Find optional titles and seekpoints */
1665 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1666 &in->i_seekpoint_start, &in->i_seekpoint_end );
1668 if( psz_forced_demux && *psz_forced_demux )
1669 psz_demux = psz_forced_demux;
1671 /* Try access_demux if no demux given */
1672 if( *psz_demux == '\0' )
1674 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1675 NULL, p_input->p_es_out );
1680 int64_t i_pts_delay;
1682 /* Get infos from access_demux */
1683 demux2_Control( in->p_demux,
1684 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1685 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1687 in->b_title_demux = VLC_TRUE;
1688 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1689 &in->title, &in->i_title,
1690 &in->i_title_offset, &in->i_seekpoint_offset ) )
1695 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1696 &in->b_can_pace_control );
1697 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1701 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1707 int64_t i_pts_delay;
1709 /* Now try a real access */
1710 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path );
1712 /* Access failed, URL encoded ? */
1713 if( in->p_access == NULL && strchr( psz_path, '%' ) )
1715 DecodeUrl( psz_path );
1717 msg_Dbg( p_input, "retying with access `%s' demux `%s' path `%s'",
1718 psz_access, psz_demux, psz_path );
1720 in->p_access = access2_New( p_input,
1721 psz_access, psz_demux, psz_path );
1723 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
1724 * are forbidden in filenames on Win32. */
1726 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1727 if( in->p_access == NULL &&
1728 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1731 psz_dup = strdup( psz_mrl );
1736 in->p_access = access2_New( p_input,
1737 psz_access, psz_demux, psz_path );
1741 if( in->p_access == NULL )
1743 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
1747 /* Get infos from access */
1748 access2_Control( in->p_access,
1749 ACCESS_GET_PTS_DELAY, &i_pts_delay );
1750 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1752 in->b_title_demux = VLC_FALSE;
1753 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
1754 &in->title, &in->i_title,
1755 &in->i_title_offset, &in->i_seekpoint_offset ) )
1761 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
1762 &in->b_can_pace_control );
1763 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
1765 access2_Control( in->p_access, ACCESS_CAN_SEEK,
1767 var_Set( p_input, "seekable", val );
1769 /* Create the stream_t */
1770 in->p_stream = stream_AccessNew( in->p_access );
1771 if( in->p_stream == NULL )
1773 msg_Warn( p_input, "cannot create a stream_t from access" );
1777 /* Open a demuxer */
1778 if( *psz_demux == '\0' && *in->p_access->psz_demux )
1780 psz_demux = in->p_access->psz_demux;
1782 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1783 in->p_stream, p_input->p_es_out );
1784 if( in->p_demux == NULL )
1786 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
1787 psz_access, psz_demux, psz_path );
1791 /* TODO get title from demux */
1792 if( in->i_title <= 0 )
1794 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1795 &in->title, &in->i_title,
1796 &in->i_title_offset, &in->i_seekpoint_offset ))
1803 in->b_title_demux = VLC_TRUE;
1812 demux2_Delete( in->p_demux );
1815 stream_AccessDelete( in->p_stream );
1818 access2_Delete( in->p_access );
1821 return VLC_EGENERIC;
1824 /*****************************************************************************
1826 *****************************************************************************/
1827 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
1830 demux2_Delete( in->p_demux );
1833 stream_AccessDelete( in->p_stream );
1836 access2_Delete( in->p_access );
1838 if( in->i_title > 0 )
1841 for( i = 0; i < in->i_title; i++ )
1843 vlc_input_title_Delete( in->title[i] );
1849 static void SlaveDemux( input_thread_t *p_input )
1853 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1855 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1859 for( i = 0; i < p_input->i_slave; i++ )
1861 input_source_t *in = p_input->slave[i];
1867 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
1872 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
1874 msg_Err( p_input, "slave[%d] doesn't like "
1875 "DEMUX_GET_TIME -> EOF", i );
1880 if( i_stime >= i_time )
1883 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
1889 i_ret = in->p_demux->pf_demux( in->p_demux );
1894 msg_Dbg( p_input, "slave %d EOF", i );
1895 in->b_eof = VLC_TRUE;
1900 static void SlaveSeek( input_thread_t *p_input )
1905 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1907 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1911 for( i = 0; i < p_input->i_slave; i++ )
1913 input_source_t *in = p_input->slave[i];
1915 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
1917 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
1918 in->b_eof = VLC_TRUE;
1922 /*****************************************************************************
1924 *****************************************************************************/
1925 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
1930 if( ( p_meta = vlc_meta_New() ) == NULL )
1933 /* Get meta information from user */
1934 #define GET_META( c, s ) \
1935 var_Get( p_input, (s), &val ); \
1936 if( *val.psz_string ) \
1937 vlc_meta_Add( p_meta, c, val.psz_string ); \
1938 free( val.psz_string )
1940 GET_META( VLC_META_TITLE, "meta-title" );
1941 GET_META( VLC_META_AUTHOR, "meta-author" );
1942 GET_META( VLC_META_ARTIST, "meta-artist" );
1943 GET_META( VLC_META_GENRE, "meta-genre" );
1944 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
1945 GET_META( VLC_META_DESCRIPTION, "meta-description" );
1946 GET_META( VLC_META_DATE, "meta-date" );
1947 GET_META( VLC_META_URL, "meta-url" );
1953 /*****************************************************************************
1954 * DecodeUrl: decode a given encoded url
1955 *****************************************************************************/
1956 static void DecodeUrl( char *psz )
1958 char *dup = strdup( psz );
1976 *psz++ = strtol( val, NULL, 16 );
1978 else if( *p == '+' )
1992 /*****************************************************************************
1993 * ParseOption: parses the options for the input
1994 *****************************************************************************
1995 * This function parses the input (config) options and creates their associated
1997 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1998 * bar is the value of the option.
1999 *****************************************************************************/
2000 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2002 char *psz_name = (char *)psz_option;
2003 char *psz_value = strchr( psz_option, '=' );
2004 int i_name_len, i_type;
2005 vlc_bool_t b_isno = VLC_FALSE;
2008 if( psz_value ) i_name_len = psz_value - psz_option;
2009 else i_name_len = strlen( psz_option );
2011 /* It's too much of an hassle to remove the ':' when we parse
2012 * the cmd line :) */
2013 if( i_name_len && *psz_name == ':' )
2019 if( i_name_len == 0 ) return;
2021 psz_name = strndup( psz_name, i_name_len );
2022 if( psz_value ) psz_value++;
2024 i_type = config_GetType( p_input, psz_name );
2026 if( !i_type && !psz_value )
2028 /* check for "no-foo" or "nofoo" */
2029 if( !strncmp( psz_name, "no-", 3 ) )
2031 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2033 else if( !strncmp( psz_name, "no", 2 ) )
2035 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2037 else goto cleanup; /* Option doesn't exist */
2040 i_type = config_GetType( p_input, psz_name );
2042 if( !i_type ) goto cleanup; /* Option doesn't exist */
2044 else if( !i_type ) goto cleanup; /* Option doesn't exist */
2046 if( ( i_type != VLC_VAR_BOOL ) &&
2047 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2049 /* Create the variable in the input object.
2050 * Children of the input object will be able to retreive this value
2051 * thanks to the inheritance property of the object variables. */
2052 var_Create( p_input, psz_name, i_type );
2057 val.b_bool = !b_isno;
2060 case VLC_VAR_INTEGER:
2061 val.i_int = atoi( psz_value );
2065 val.f_float = atof( psz_value );
2068 case VLC_VAR_STRING:
2069 case VLC_VAR_MODULE:
2071 case VLC_VAR_DIRECTORY:
2072 val.psz_string = psz_value;
2080 var_Set( p_input, psz_name, val );
2082 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2085 if( psz_name ) free( psz_name );
2089 /*****************************************************************************
2090 * MRLSplit: parse the access, demux and url part of the
2091 * Media Resource Locator.
2092 *****************************************************************************/
2093 static void MRLSplit( input_thread_t *p_input, char *psz_dup,
2094 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2096 char *psz_access = NULL;
2097 char *psz_demux = NULL;
2098 char *psz_path = NULL;
2099 char *psz, *psz_check;
2101 psz = strchr( psz_dup, ':' );
2103 /* '@' not allowed in access/demux part */
2104 psz_check = strchr( psz_dup, '@' );
2105 if( psz_check && psz_check < psz ) psz = 0;
2107 #if defined( WIN32 ) || defined( UNDER_CE )
2108 if( psz - psz_dup == 1 )
2110 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2119 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2123 psz = strchr( psz_dup, '/' );
2130 psz_access = psz_dup;
2137 if( !psz_access ) *ppsz_access = "";
2138 else *ppsz_access = psz_access;
2140 if( !psz_demux ) *ppsz_demux = "";
2141 else *ppsz_demux = psz_demux;
2143 if( !psz_path ) *ppsz_path = "";
2144 else *ppsz_path = psz_path;
2147 /*****************************************************************************
2148 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2151 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2152 *****************************************************************************/
2153 static void MRLSections( input_thread_t *p_input, char *psz_source,
2154 int *pi_title_start, int *pi_title_end,
2155 int *pi_chapter_start, int *pi_chapter_end )
2157 char *psz, *psz_end, *psz_next, *psz_check;
2159 *pi_title_start = *pi_title_end = -1;
2160 *pi_chapter_start = *pi_chapter_end = -1;
2162 /* Start by parsing titles and chapters */
2163 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2165 /* Check we are really dealing with a title/chapter section */
2166 psz_check = psz + 1;
2167 if( !*psz_check ) return;
2168 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2169 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2170 if( *psz_check == ':' && ++psz_check )
2171 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2172 if( *psz_check != '-' && *psz_check ) return;
2173 if( *psz_check == '-' && ++psz_check )
2174 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2175 if( *psz_check != ':' && *psz_check ) return;
2176 if( *psz_check == ':' && ++psz_check )
2177 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2178 if( *psz_check ) return;
2180 /* Separate start and end */
2182 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2184 /* Look for the start title */
2185 *pi_title_start = strtol( psz, &psz_next, 0 );
2186 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2187 *pi_title_end = *pi_title_start;
2190 /* Look for the start chapter */
2192 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2193 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2194 *pi_chapter_end = *pi_chapter_start;
2198 /* Look for the end title */
2199 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2200 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2203 /* Look for the end chapter */
2204 if( *psz_end ) psz_end++;
2205 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2206 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2209 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2210 psz_source, *pi_title_start, *pi_chapter_start,
2211 *pi_title_end, *pi_chapter_end );