]> git.sesse.net Git - vlc/blob - src/input/input.c
*experimental* input pre-parsing support.
[vlc] / src / input / input.c
1 /*****************************************************************************
2  * input.c: input thread
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/decoder.h>
34 #include <vlc/vout.h>
35
36 #include "input_internal.h"
37
38 #include "stream_output.h"
39 #include "vlc_playlist.h"
40 #include "vlc_interface.h"
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static  int Run  ( input_thread_t *p_input );
46
47 static  int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
48 static void Error( input_thread_t *p_input );
49 static void End  ( input_thread_t *p_input );
50
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 );
54
55
56 static int  UpdateFromAccess( input_thread_t * );
57 static int  UpdateFromDemux( input_thread_t * );
58 static int  UpdateMeta( input_thread_t *, vlc_bool_t );
59
60 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
61
62 static void ParseOption( input_thread_t *p_input, const char *psz_option );
63
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 *);
67
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                              vlc_bool_t b_quick );
72 static void InputSourceClean( input_thread_t *, input_source_t * );
73
74 static void SlaveDemux( input_thread_t *p_input );
75 static void SlaveSeek( input_thread_t *p_input );
76
77 static vlc_meta_t *InputMetaUser( input_thread_t *p_input );
78
79 /*****************************************************************************
80  * input_CreateThread: creates a new input thread
81  *****************************************************************************
82  * This function creates a new input, and returns a pointer
83  * to its description. On error, it returns NULL.
84  *
85  * Variables for _public_ use:
86  * * Get and Set:
87  *  - state
88  *  - rate,rate-slower, rate-faster
89  *  - position, position-offset
90  *  - time, time-offset
91  *  - title,title-next,title-prev
92  *  - chapter,chapter-next, chapter-prev
93  *  - program, audio-es, video-es, spu-es
94  *  - audio-delay, spu-delay
95  *  - bookmark
96  * * Get only:
97  *  - length
98  *  - bookmarks
99  *  - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
100  * * For intf callback upon changes
101  *  - intf-change
102  * TODO explain when Callback is called
103  * TODO complete this list (?)
104  *****************************************************************************/
105 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
106                                       input_item_t *p_item )
107
108 {
109     input_thread_t *p_input;                        /* thread descriptor */
110     vlc_value_t val;
111     int i;
112
113     /* Allocate descriptor */
114     p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
115     if( p_input == NULL )
116     {
117         msg_Err( p_parent, "out of memory" );
118         return NULL;
119     }
120
121     /* Init Common fields */
122     p_input->b_eof = VLC_FALSE;
123     p_input->b_can_pace_control = VLC_TRUE;
124     p_input->i_start = 0;
125     p_input->i_time  = 0;
126     p_input->i_stop  = 0;
127     p_input->i_title = 0;
128     p_input->title   = NULL;
129     p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
130     p_input->i_state = INIT_S;
131     p_input->i_rate  = INPUT_RATE_DEFAULT;
132     p_input->i_bookmark = 0;
133     p_input->bookmark = NULL;
134     p_input->p_meta  = NULL;
135     p_input->p_es_out = NULL;
136     p_input->p_sout  = NULL;
137     p_input->b_out_pace_control = VLC_FALSE;
138     p_input->i_pts_delay = 0;
139
140     /* Init Input fields */
141     p_input->input.p_item = p_item;
142     p_input->input.p_access = NULL;
143     p_input->input.p_stream = NULL;
144     p_input->input.p_demux  = NULL;
145     p_input->input.b_title_demux = VLC_FALSE;
146     p_input->input.i_title  = 0;
147     p_input->input.title    = NULL;
148     p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
149     p_input->input.b_can_pace_control = VLC_TRUE;
150     p_input->input.b_eof = VLC_FALSE;
151     p_input->input.i_cr_average = 0;
152
153     /* No slave */
154     p_input->i_slave = 0;
155     p_input->slave   = NULL;
156
157     /* Init control buffer */
158     vlc_mutex_init( p_input, &p_input->lock_control );
159     p_input->i_control = 0;
160
161     /* Parse input options */
162     vlc_mutex_lock( &p_item->lock );
163     for( i = 0; i < p_item->i_options; i++ )
164     {
165 //        msg_Dbg( p_input, "option: %s", p_item->ppsz_options[i] );
166         ParseOption( p_input, p_item->ppsz_options[i] );
167     }
168     vlc_mutex_unlock( &p_item->lock );
169
170     /* Create Object Variables for private use only */
171     input_ConfigVarInit( p_input );
172
173     /* Create Objects variables for public Get and Set */
174     input_ControlVarInit( p_input );
175     p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
176
177     /* TODO */
178     var_Get( p_input, "bookmarks", &val );
179     if( val.psz_string )
180     {
181         /* FIXME: have a common cfg parsing routine used by sout and others */
182         char *psz_parser, *psz_start, *psz_end;
183         psz_parser = val.psz_string;
184         while( (psz_start = strchr( psz_parser, '{' ) ) )
185         {
186             seekpoint_t seekpoint;
187             char backup;
188             psz_start++;
189             psz_end = strchr( psz_start, '}' );
190             if( !psz_end ) break;
191             psz_parser = psz_end + 1;
192             backup = *psz_parser;
193             *psz_parser = 0;
194             *psz_end = ',';
195
196             seekpoint.psz_name = 0;
197             seekpoint.i_byte_offset = 0;
198             seekpoint.i_time_offset = 0;
199             while( (psz_end = strchr( psz_start, ',' ) ) )
200             {
201                 *psz_end = 0;
202                 if( !strncmp( psz_start, "name=", 5 ) )
203                 {
204                     seekpoint.psz_name = psz_start + 5;
205                 }
206                 else if( !strncmp( psz_start, "bytes=", 6 ) )
207                 {
208                     seekpoint.i_byte_offset = atoll(psz_start + 6);
209                 }
210                 else if( !strncmp( psz_start, "time=", 5 ) )
211                 {
212                     seekpoint.i_time_offset = atoll(psz_start + 5) * 1000000;
213                 }
214                 psz_start = psz_end + 1;
215             }
216             msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
217                      seekpoint.psz_name, seekpoint.i_byte_offset,
218                      seekpoint.i_time_offset );
219             input_Control( p_input, INPUT_ADD_BOOKMARK, &seekpoint );
220             *psz_parser = backup;
221         }
222         free( val.psz_string );
223     }
224
225     /* Now we can attach our new input */
226     vlc_object_attach( p_input, p_parent );
227
228     /* Create thread and wait for its readiness. */
229     if( vlc_thread_create( p_input, "input", Run,
230                            VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
231     {
232         msg_Err( p_input, "cannot create input thread" );
233         vlc_object_detach( p_input );
234         vlc_object_destroy( p_input );
235         return NULL;
236     }
237
238     return p_input;
239 }
240
241 /*****************************************************************************
242  * input_PreParse: Lightweight input for playlist item preparsing
243  *****************************************************************************/
244 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
245 {
246     input_thread_t *p_input;                        /* thread descriptor */
247     int i;
248
249     /* Allocate descriptor */
250     p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
251     if( p_input == NULL )
252     {
253         msg_Err( p_parent, "out of memory" );
254         return VLC_EGENERIC;
255     }
256
257     /* Init Common fields */
258     p_input->b_eof = VLC_FALSE;
259     p_input->b_can_pace_control = VLC_TRUE;
260     p_input->i_start = 0;
261     p_input->i_time  = 0;
262     p_input->i_stop  = 0;
263     p_input->i_title = 0;
264     p_input->title   = NULL;
265     p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
266     p_input->i_state = INIT_S;
267     p_input->i_rate  = INPUT_RATE_DEFAULT;
268     p_input->i_bookmark = 0;
269     p_input->bookmark = NULL;
270     p_input->p_meta  = NULL;
271     p_input->p_es_out = NULL;
272     p_input->p_sout  = NULL;
273     p_input->b_out_pace_control = VLC_FALSE;
274     p_input->i_pts_delay = 0;
275
276     /* Init Input fields */
277     p_input->input.p_item = p_item;
278     p_input->input.p_access = NULL;
279     p_input->input.p_stream = NULL;
280     p_input->input.p_demux  = NULL;
281     p_input->input.b_title_demux = VLC_FALSE;
282     p_input->input.i_title  = 0;
283     p_input->input.title    = NULL;
284     p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
285     p_input->input.b_can_pace_control = VLC_TRUE;
286     p_input->input.b_eof = VLC_FALSE;
287     p_input->input.i_cr_average = 0;
288
289     /* No slave */
290     p_input->i_slave = 0;
291     p_input->slave   = NULL;
292
293     /* Init control buffer */
294     vlc_mutex_init( p_input, &p_input->lock_control );
295     p_input->i_control = 0;
296
297     /* Parse input options */
298     vlc_mutex_lock( &p_item->lock );
299     for( i = 0; i < p_item->i_options; i++ )
300     {
301         ParseOption( p_input, p_item->ppsz_options[i] );
302     }
303     vlc_mutex_unlock( &p_item->lock );
304
305     /* Create Object Variables for private use only */
306     input_ConfigVarInit( p_input );
307
308     p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
309
310     /* Now we can attach our new input */
311     vlc_object_attach( p_input, p_parent );
312
313     Init( p_input, VLC_TRUE );
314
315     /* Clean up master */
316     InputSourceClean( p_input, &p_input->input );
317
318     /* Kill access and demux */
319     if( p_input->input.p_access ) p_input->input.p_access->b_die = VLC_TRUE;
320     if( p_input->input.p_demux ) p_input->input.p_access->b_die = VLC_TRUE;
321
322     /* Unload all modules */
323     if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
324
325     /* Delete meta */
326     if( p_input->p_meta ) vlc_meta_Delete( p_input->p_meta );
327
328     vlc_object_detach( p_input );
329     vlc_object_destroy( p_input );
330
331     return VLC_SUCCESS;
332 }
333
334 /*****************************************************************************
335  * input_StopThread: mark an input thread as zombie
336  *****************************************************************************
337  * This function should not return until the thread is effectively cancelled.
338  *****************************************************************************/
339 void input_StopThread( input_thread_t *p_input )
340 {
341     vlc_list_t *p_list;
342     int i;
343
344     /* Set die for input */
345     p_input->b_die = VLC_TRUE;
346
347     /* We cannot touch p_input fields directly (we can from another thread),
348      * so use the vlc_object_find way, it's perfectly safe */
349
350     /* Set die for all access */
351     p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
352     for( i = 0; i < p_list->i_count; i++ )
353     {
354         p_list->p_values[i].p_object->b_die = VLC_TRUE;
355     }
356     vlc_list_release( p_list );
357
358     /* Set die for all stream */
359     p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
360     for( i = 0; i < p_list->i_count; i++ )
361     {
362         p_list->p_values[i].p_object->b_die = VLC_TRUE;
363     }
364     vlc_list_release( p_list );
365
366     /* Set die for all demux */
367     p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
368     for( i = 0; i < p_list->i_count; i++ )
369     {
370         p_list->p_values[i].p_object->b_die = VLC_TRUE;
371     }
372     vlc_list_release( p_list );
373
374     input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
375 }
376
377 /*****************************************************************************
378  * input_DestroyThread: mark an input thread as zombie
379  *****************************************************************************
380  * This function should not return until the thread is effectively cancelled.
381  *****************************************************************************/
382 void input_DestroyThread( input_thread_t *p_input )
383 {
384     /* Join the thread */
385     vlc_thread_join( p_input );
386
387     /* Delete input lock (only after thread joined) */
388     vlc_mutex_destroy( &p_input->lock_control );
389
390     /* TODO: maybe input_DestroyThread should also delete p_input instead
391      * of the playlist but I'm not sure if it's possible */
392 }
393
394 /*****************************************************************************
395  * Run: main thread loop
396  *****************************************************************************
397  * Thread in charge of processing the network packets and demultiplexing.
398  *
399  * TODO:
400  *  read subtitle support (XXX take care of spu-delay in the right way).
401  *  multi-input support (XXX may be done with subs)
402  *****************************************************************************/
403 static int Run( input_thread_t *p_input )
404 {
405     int64_t i_intf_update = 0;
406
407     /* Signal that the thread is launched */
408     vlc_thread_ready( p_input );
409
410     if( Init( p_input, VLC_FALSE ) )
411     {
412         /* If we failed, wait before we are killed, and exit */
413         p_input->b_error = VLC_TRUE;
414
415         Error( p_input );
416
417         /* Tell we're dead */
418         p_input->b_dead = VLC_TRUE;
419
420         return 0;
421     }
422
423     /* Main loop */
424     while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
425     {
426         vlc_bool_t b_force_update = VLC_FALSE;
427         int i_ret;
428         int i_type;
429         vlc_value_t val;
430
431         /* Do the read */
432         if( p_input->i_state != PAUSE_S  )
433         {
434             if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
435                 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
436             else
437                 i_ret = 0;  /* EOF */
438
439             if( i_ret > 0 )
440             {
441                 /* TODO */
442                 if( p_input->input.b_title_demux &&
443                     p_input->input.p_demux->info.i_update )
444                 {
445                     i_ret = UpdateFromDemux( p_input );
446                     b_force_update = VLC_TRUE;
447                 }
448                 else if( !p_input->input.b_title_demux &&
449                           p_input->input.p_access &&
450                           p_input->input.p_access->info.i_update )
451                 {
452                     i_ret = UpdateFromAccess( p_input );
453                     b_force_update = VLC_TRUE;
454                 }
455             }
456
457             if( i_ret == 0 )    /* EOF */
458             {
459                 vlc_value_t repeat;
460
461                 var_Get( p_input, "input-repeat", &repeat );
462                 if( repeat.i_int == 0 )
463                 {
464                     /* End of file - we do not set b_die because only the
465                      * playlist is allowed to do so. */
466                     msg_Dbg( p_input, "EOF reached" );
467                     p_input->input.b_eof = VLC_TRUE;
468                 }
469                 else
470                 {
471                     msg_Dbg( p_input, "repeating the same input (%d)",
472                              repeat.i_int );
473                     if( repeat.i_int > 0 )
474                     {
475                         repeat.i_int--;
476                         var_Set( p_input, "input-repeat", repeat );
477                     }
478
479                     /* Seek to start title/seekpoint */
480                     val.i_int = p_input->input.i_title_start -
481                         p_input->input.i_title_offset;
482                     if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
483                         val.i_int = 0;
484                     input_ControlPush( p_input,
485                                        INPUT_CONTROL_SET_TITLE, &val );
486
487                     val.i_int = p_input->input.i_seekpoint_start -
488                         p_input->input.i_seekpoint_offset;
489                     if( val.i_int > 0 /* TODO: check upper boundary */ )
490                         input_ControlPush( p_input,
491                                            INPUT_CONTROL_SET_SEEKPOINT, &val );
492
493                     /* Seek to start position */
494                     if( p_input->i_start > 0 )
495                     {
496                         val.i_time = p_input->i_start;
497                         input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
498                                            &val );
499                     }
500                     else
501                     {
502                         val.f_float = 0.0;
503                         input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
504                                            &val );
505                     }
506                 }
507             }
508             else if( i_ret < 0 )
509             {
510                 p_input->b_error = VLC_TRUE;
511             }
512
513             if( i_ret > 0 && p_input->i_slave > 0 )
514             {
515                 SlaveDemux( p_input );
516             }
517         }
518         else
519         {
520             /* Small wait */
521             msleep( 10*1000 );
522         }
523
524         /* Handle control */
525         vlc_mutex_lock( &p_input->lock_control );
526         ControlReduce( p_input );
527         while( !ControlPopNoLock( p_input, &i_type, &val ) )
528         {
529             msg_Dbg( p_input, "control type=%d", i_type );
530             if( Control( p_input, i_type, val ) )
531                 b_force_update = VLC_TRUE;
532         }
533         vlc_mutex_unlock( &p_input->lock_control );
534
535         if( b_force_update || i_intf_update < mdate() )
536         {
537             vlc_value_t val;
538             double f_pos;
539             int64_t i_time, i_length;
540             /* update input status variables */
541             if( !demux2_Control( p_input->input.p_demux,
542                                  DEMUX_GET_POSITION, &f_pos ) )
543             {
544                 val.f_float = (float)f_pos;
545                 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
546             }
547             if( !demux2_Control( p_input->input.p_demux,
548                                  DEMUX_GET_TIME, &i_time ) )
549             {
550                 p_input->i_time = i_time;
551                 val.i_time = i_time;
552                 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
553             }
554             if( !demux2_Control( p_input->input.p_demux,
555                                  DEMUX_GET_LENGTH, &i_length ) )
556             {
557                 vlc_value_t old_val;
558                 var_Get( p_input, "length", &old_val );
559                 val.i_time = i_length;
560                 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
561
562                 if( old_val.i_time != val.i_time )
563                 {
564                     UpdateItemLength( p_input, i_length, VLC_TRUE );
565                 }
566             }
567
568             var_SetBool( p_input, "intf-change", VLC_TRUE );
569             i_intf_update = mdate() + I64C(150000);
570         }
571     }
572
573     if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
574     {
575         /* We have finish to demux data but not to play them */
576         while( !p_input->b_die )
577         {
578             if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
579                 break;
580
581             msg_Dbg( p_input, "waiting decoder fifos to empty" );
582
583             msleep( INPUT_IDLE_SLEEP );
584         }
585
586         /* We have finished */
587         p_input->b_eof = VLC_TRUE;
588     }
589
590     /* Wait we are asked to die */
591     if( !p_input->b_die )
592     {
593         Error( p_input );
594     }
595
596     /* Clean up */
597     End( p_input );
598
599     return 0;
600 }
601
602
603 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
604 {
605     char *psz;
606     char *psz_subtitle;
607     vlc_value_t val;
608     double f_fps;
609     vlc_meta_t *p_meta, *p_meta_tmp;
610     int i_es_out_mode;
611     int i, i_delay;
612
613     /* Initialize optional stream output. (before access/demuxer) */
614     if( !b_quick )
615     {
616         psz = var_GetString( p_input, "sout" );
617         if( *psz )
618         {
619             p_input->p_sout = sout_NewInstance( p_input, psz );
620             if( p_input->p_sout == NULL )
621             {
622                 msg_Err( p_input, "cannot start stream output instance," \
623                                   "aborting" );
624                 free( psz );
625                 return VLC_EGENERIC;
626             }
627         }
628         free( psz );
629     }
630
631     /* Create es out */
632     p_input->p_es_out = input_EsOutNew( p_input );
633     es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
634     es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
635
636     if( InputSourceInit( p_input, &p_input->input,
637                          p_input->input.p_item->psz_uri, NULL, b_quick ) )
638     {
639         goto error;
640     }
641
642     /* Create global title (from master) */
643     if( !b_quick )
644     {
645         p_input->i_title = p_input->input.i_title;
646         p_input->title   = p_input->input.title;
647         p_input->i_title_offset = p_input->input.i_title_offset;
648         p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
649         if( p_input->i_title > 0 )
650         {
651             /* Setup variables */
652             input_ControlVarNavigation( p_input );
653             input_ControlVarTitle( p_input, 0 );
654         }
655
656         /* Global flag */
657         p_input->b_can_pace_control = p_input->input.b_can_pace_control;
658         p_input->b_can_pause        = p_input->input.b_can_pause;
659
660         /* Fix pts delay */
661         if( p_input->i_pts_delay <= 0 )
662             p_input->i_pts_delay = DEFAULT_PTS_DELAY;
663
664         /* If the desynchronisation requested by the user is < 0, we need to
665          * cache more data. */
666         var_Get( p_input, "audio-desync", &val );
667         if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
668
669         /* Update cr_average depending on the caching */
670         p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
671         p_input->input.i_cr_average /= 10;
672         if( p_input->input.i_cr_average <= 0 ) p_input->input.i_cr_average = 1;
673     }
674
675     /* Load master infos */
676     /* Init length */
677     if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
678                          &val.i_time ) && val.i_time > 0 )
679     {
680         var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
681         UpdateItemLength( p_input, val.i_time, b_quick );
682         p_input->input.p_item->i_duration = val.i_time;
683     }
684
685     /* Start title/chapter */
686     if( !b_quick )
687     {
688         val.i_int = p_input->input.i_title_start -
689                     p_input->input.i_title_offset;
690         if( val.i_int > 0 && val.i_int < p_input->input.i_title )
691             input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
692         val.i_int = p_input->input.i_seekpoint_start -
693                     p_input->input.i_seekpoint_offset;
694         if( val.i_int > 0 /* TODO: check upper boundary */ )
695             input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
696
697         /* Start time*/
698         /* Set start time */
699         p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
700                            I64C(1000000);
701         p_input->i_stop  = (int64_t)var_GetInteger( p_input, "stop-time" ) *
702                            I64C(1000000);
703
704         if( p_input->i_start > 0 )
705         {
706             if( p_input->i_start >= val.i_time )
707             {
708                 msg_Warn( p_input, "invalid start-time ignored" );
709             }
710             else
711             {
712                 vlc_value_t s;
713
714                 msg_Dbg( p_input, "start-time: %ds",
715                                   (int)( p_input->i_start / I64C(1000000) ) );
716
717                 s.i_time = p_input->i_start;
718                 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
719             }
720         }
721         if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
722         {
723             msg_Warn( p_input, "invalid stop-time ignored" );
724             p_input->i_stop = 0;
725         }
726
727
728         /* Load subtitles */
729         /* Get fps and set it if not already set */
730         if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
731             f_fps > 1.0 )
732         {
733             float f_requested_fps;
734
735             var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
736             var_SetFloat( p_input, "sub-original-fps", f_fps );
737
738             f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
739             if( f_requested_fps != f_fps )
740             {
741                 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
742                                                 VLC_VAR_DOINHERIT );
743                 var_SetFloat( p_input, "sub-fps", f_requested_fps );
744             }
745         }
746
747         i_delay = var_CreateGetInteger( p_input, "sub-delay" );
748         if( i_delay != 0 )
749         {
750             var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
751         }
752
753
754         /* Look for and add subtitle files */
755         psz_subtitle = var_GetString( p_input, "sub-file" );
756         if( *psz_subtitle )
757         {
758             input_source_t *sub;
759             vlc_value_t count;
760             vlc_value_t list;
761
762             msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
763
764             var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
765
766             /* */
767             sub = InputSourceNew( p_input );
768             if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
769                                   VLC_FALSE ) )
770             {
771                 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
772
773                 /* Select the ES */
774                 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
775                                  NULL ) )
776                 {
777                     if( count.i_int == 0 )
778                         count.i_int++;
779                         /* if it was first one, there is disable too */
780
781                     if( count.i_int < list.p_list->i_count )
782                     {
783                         input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
784                                           &list.p_list->p_values[count.i_int] );
785                     }
786                     var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
787                                 NULL );
788                 }
789             }
790         }
791
792         var_Get( p_input, "sub-autodetect-file", &val );
793         if( val.b_bool )
794         {
795            char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
796             char **subs = subtitles_Detect( p_input, psz_autopath,
797                                             p_input->input.p_item->psz_uri );
798             input_source_t *sub;
799
800             for( i = 0; subs[i] != NULL; i++ )
801             {
802                 if( strcmp( psz_subtitle, subs[i] ) )
803                 {
804                     sub = InputSourceNew( p_input );
805                     if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
806                                           VLC_FALSE ) )
807                     {
808                          TAB_APPEND( p_input->i_slave, p_input->slave, sub );
809                     }
810                 }
811                 free( subs[i] );
812             }
813             free( subs );
814             free( psz_autopath );
815         }
816         free( psz_subtitle );
817
818         /* Look for slave */
819         psz = var_GetString( p_input, "input-slave" );
820         if( *psz )
821         {
822             char *psz_delim;
823             input_source_t *slave;
824             while( psz && *psz )
825             {
826                 while( *psz == ' ' || *psz == '#' )
827                 {
828                     psz++;
829                 }
830                 if( ( psz_delim = strchr( psz, '#' ) ) )
831                 {
832                     *psz_delim++ = '\0';
833                 }
834                 if( *psz == 0 )
835                 {
836                     break;
837                 }
838
839                 msg_Dbg( p_input, "adding slave '%s'", psz );
840                 slave = InputSourceNew( p_input );
841                 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
842                 {
843                     TAB_APPEND( p_input->i_slave, p_input->slave, slave );
844                 }
845                 psz = psz_delim;
846             }
847         }
848         if( psz ) free( psz );
849     }
850     else
851     {
852         p_input->i_start = 0;
853         p_input->i_start = 0;
854     }
855
856     /* Set up es_out */
857     if( !b_quick )
858     {
859         es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
860         i_es_out_mode = ES_OUT_MODE_AUTO;
861         val.p_list = NULL;
862         if( p_input->p_sout )
863         {
864             var_Get( p_input, "sout-all", &val );
865             if ( val.b_bool )
866             {
867                 i_es_out_mode = ES_OUT_MODE_ALL;
868                 val.p_list = NULL;
869             }
870             else
871             {
872                 var_Get( p_input, "programs", &val );
873                 if ( val.p_list && val.p_list->i_count )
874                 {
875                     i_es_out_mode = ES_OUT_MODE_PARTIAL;
876                     /* Note : we should remove the "program" callback. */
877                 }
878                 else
879                     var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
880                                 NULL );
881             }
882         }
883         es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
884
885         /* Inform the demuxer about waited group (needed only for DVB) */
886         if( i_es_out_mode == ES_OUT_MODE_ALL )
887         {
888             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
889         }
890         else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
891         {
892             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
893                             val.p_list );
894         }
895         else
896         {
897             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
898                            (int) var_GetInteger( p_input, "program" ), NULL );
899         }
900
901         if( p_input->p_sout )
902         {
903             if( p_input->p_sout->i_out_pace_nocontrol > 0 )
904             {
905                 p_input->b_out_pace_control = VLC_FALSE;
906             }
907             else
908             {
909                 p_input->b_out_pace_control = VLC_TRUE;
910             }
911             msg_Dbg( p_input, "starting in %s mode",
912                      p_input->b_out_pace_control ? "asynch" : "synch" );
913         }
914     }
915
916     /* Get meta data from users */
917     p_meta_tmp = InputMetaUser( p_input );
918
919     /* Get meta data from master input */
920     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
921         p_meta = NULL;
922
923     /* Merge them */
924     if( p_meta == NULL )
925     {
926         p_meta = p_meta_tmp;
927     }
928     else if( p_meta_tmp )
929     {
930         vlc_meta_Merge( p_meta, p_meta_tmp );
931         vlc_meta_Delete( p_meta_tmp );
932     }
933
934     /* Access_file does not give any meta, and there are no slave */
935     if( !b_quick )
936     {
937         if( !p_input->input.p_access ||
938             access2_Control( p_input->input.p_access, ACCESS_GET_META,
939                              &p_meta_tmp))
940             p_meta_tmp = NULL;
941
942         if( p_meta == NULL )
943         {
944             p_meta = p_meta_tmp;
945         }
946         else if( p_meta_tmp )
947         {
948             vlc_meta_Merge( p_meta, p_meta_tmp );
949             vlc_meta_Delete( p_meta_tmp );
950         }
951
952         /* Get meta data from slave input */
953         for( i = 0; i < p_input->i_slave; i++ )
954         {
955             vlc_meta_t *p_meta_slave;
956
957             if( !demux2_Control( p_input->slave[i]->p_demux,
958                                  DEMUX_GET_META, &p_meta_slave ) )
959             {
960                 if( p_meta == NULL )
961                 {
962                     p_meta = p_meta_slave;
963                 }
964                 else if( p_meta_slave )
965                 {
966                     vlc_meta_Merge( p_meta, p_meta_slave );
967                     vlc_meta_Delete( p_meta_slave );
968                 }
969             }
970
971             if( p_input->slave[i]->p_access &&
972                 !access2_Control( p_input->slave[i]->p_access,
973                                   ACCESS_GET_META, &p_meta_slave ) )
974             {
975                 if( p_meta == NULL )
976                 {
977                     p_meta = p_meta_slave;
978                 }
979                 else if( p_meta_slave )
980                 {
981                     vlc_meta_Merge( p_meta, p_meta_slave );
982                     vlc_meta_Delete( p_meta_slave );
983                 }
984             }
985         }
986     }
987
988     p_input->p_meta = p_meta;
989     UpdateMeta( p_input, b_quick );
990
991     if( !b_quick )
992     {
993         msg_Dbg( p_input, "`%s' sucessfully opened",
994                  p_input->input.p_item->psz_uri );
995
996     }
997
998     /* Trigger intf update for this item */
999     /* Playlist has a callback on this variable and will forward
1000      * it to intf */
1001     var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
1002
1003     /* initialization is complete */
1004     p_input->i_state = PLAYING_S;
1005
1006     val.i_int = PLAYING_S;
1007     var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1008
1009     return VLC_SUCCESS;
1010
1011 error:
1012     if( p_input->p_es_out )
1013         input_EsOutDelete( p_input->p_es_out );
1014
1015     if( p_input->p_sout )
1016         sout_DeleteInstance( p_input->p_sout );
1017
1018     /* Mark them deleted */
1019     p_input->input.p_demux = NULL;
1020     p_input->input.p_stream = NULL;
1021     p_input->input.p_access = NULL;
1022     p_input->p_es_out = NULL;
1023     p_input->p_sout = NULL;
1024
1025     return VLC_EGENERIC;
1026 }
1027
1028 /*****************************************************************************
1029  * Error: RunThread() error loop
1030  *****************************************************************************
1031  * This function is called when an error occurred during thread main's loop.
1032  *****************************************************************************/
1033 static void Error( input_thread_t *p_input )
1034 {
1035     while( !p_input->b_die )
1036     {
1037         /* Sleep a while */
1038         msleep( INPUT_IDLE_SLEEP );
1039     }
1040 }
1041
1042 /*****************************************************************************
1043  * End: end the input thread
1044  *****************************************************************************/
1045 static void End( input_thread_t * p_input )
1046 {
1047     vlc_value_t val;
1048     int i;
1049
1050     msg_Dbg( p_input, "closing input" );
1051
1052     /* We are at the end */
1053     p_input->i_state = END_S;
1054
1055     val.i_int = END_S;
1056     var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1057
1058     /* Clean control variables */
1059     input_ControlVarClean( p_input );
1060
1061     /* Clean up master */
1062     InputSourceClean( p_input, &p_input->input );
1063
1064     /* Delete slave */
1065     for( i = 0; i < p_input->i_slave; i++ )
1066     {
1067         InputSourceClean( p_input, p_input->slave[i] );
1068         free( p_input->slave[i] );
1069     }
1070     if( p_input->slave ) free( p_input->slave );
1071
1072     /* Unload all modules */
1073     if( p_input->p_es_out )
1074         input_EsOutDelete( p_input->p_es_out );
1075
1076     /* Close optional stream output instance */
1077     if( p_input->p_sout )
1078     {
1079         vlc_object_t *p_pl =
1080             vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1081         vlc_value_t keep;
1082
1083         if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1084         {
1085             /* attach sout to the playlist */
1086             msg_Warn( p_input, "keeping sout" );
1087             vlc_object_detach( p_input->p_sout );
1088             vlc_object_attach( p_input->p_sout, p_pl );
1089         }
1090         else
1091         {
1092             msg_Warn( p_input, "destroying sout" );
1093             sout_DeleteInstance( p_input->p_sout );
1094         }
1095         if( p_pl )
1096             vlc_object_release( p_pl );
1097     }
1098
1099     /* Delete meta */
1100     if( p_input->p_meta )
1101         vlc_meta_Delete( p_input->p_meta );
1102
1103     /* Tell we're dead */
1104     p_input->b_dead = VLC_TRUE;
1105 }
1106
1107 /*****************************************************************************
1108  * Control
1109  *****************************************************************************/
1110 static inline int ControlPopNoLock( input_thread_t *p_input,
1111                                     int *pi_type, vlc_value_t *p_val )
1112 {
1113     if( p_input->i_control <= 0 )
1114     {
1115         return VLC_EGENERIC;
1116     }
1117
1118     *pi_type = p_input->control[0].i_type;
1119     *p_val   = p_input->control[0].val;
1120
1121     p_input->i_control--;
1122     if( p_input->i_control > 0 )
1123     {
1124         int i;
1125
1126         for( i = 0; i < p_input->i_control; i++ )
1127         {
1128             p_input->control[i].i_type = p_input->control[i+1].i_type;
1129             p_input->control[i].val    = p_input->control[i+1].val;
1130         }
1131     }
1132
1133     return VLC_SUCCESS;
1134 }
1135
1136 static void ControlReduce( input_thread_t *p_input )
1137 {
1138     int i;
1139     for( i = 1; i < p_input->i_control; i++ )
1140     {
1141         const int i_lt = p_input->control[i-1].i_type;
1142         const int i_ct = p_input->control[i].i_type;
1143
1144         /* XXX We can't merge INPUT_CONTROL_SET_ES */
1145         msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1146                  i_lt, i_ct );
1147         if( i_lt == i_ct &&
1148             ( i_ct == INPUT_CONTROL_SET_STATE ||
1149               i_ct == INPUT_CONTROL_SET_RATE ||
1150               i_ct == INPUT_CONTROL_SET_POSITION ||
1151               i_ct == INPUT_CONTROL_SET_TIME ||
1152               i_ct == INPUT_CONTROL_SET_PROGRAM ||
1153               i_ct == INPUT_CONTROL_SET_TITLE ||
1154               i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1155               i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1156         {
1157             int j;
1158             msg_Dbg( p_input, "merged at %d", i );
1159             /* Remove the i-1 */
1160             for( j = i; j <  p_input->i_control; j++ )
1161                 p_input->control[j-1] = p_input->control[j];
1162             p_input->i_control--;
1163         }
1164         else
1165         {
1166             /* TODO but that's not that important
1167                 - merge SET_X with SET_X_CMD
1168                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1169                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1170                 - ?
1171                 */
1172         }
1173     }
1174 }
1175
1176 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1177                            vlc_value_t val )
1178 {
1179     vlc_bool_t b_force_update = VLC_FALSE;
1180
1181     switch( i_type )
1182     {
1183         case INPUT_CONTROL_SET_DIE:
1184             msg_Dbg( p_input, "control: INPUT_CONTROL_SET_DIE proceed" );
1185             /* Mark all submodules to die */
1186             if( p_input->input.p_access )
1187                 p_input->input.p_access->b_die = VLC_TRUE;
1188             if( p_input->input.p_stream )
1189                 p_input->input.p_stream->b_die = VLC_TRUE;
1190             p_input->input.p_demux->b_die = VLC_TRUE;
1191
1192             p_input->b_die = VLC_TRUE;
1193             break;
1194
1195         case INPUT_CONTROL_SET_POSITION:
1196         case INPUT_CONTROL_SET_POSITION_OFFSET:
1197         {
1198             double f_pos;
1199             if( i_type == INPUT_CONTROL_SET_POSITION )
1200             {
1201                 f_pos = val.f_float;
1202             }
1203             else
1204             {
1205                 /* Should not fail */
1206                 demux2_Control( p_input->input.p_demux,
1207                                 DEMUX_GET_POSITION, &f_pos );
1208                 f_pos += val.f_float;
1209             }
1210             if( f_pos < 0.0 ) f_pos = 0.0;
1211             if( f_pos > 1.0 ) f_pos = 1.0;
1212             if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1213                                 f_pos ) )
1214             {
1215                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1216                          "%2.1f%% failed", f_pos * 100 );
1217             }
1218             else
1219             {
1220                 if( p_input->i_slave > 0 )
1221                     SlaveSeek( p_input );
1222
1223                 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1224                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1225                 b_force_update = VLC_TRUE;
1226             }
1227             break;
1228         }
1229
1230         case INPUT_CONTROL_SET_TIME:
1231         case INPUT_CONTROL_SET_TIME_OFFSET:
1232         {
1233             int64_t i_time;
1234             int i_ret;
1235
1236             if( i_type == INPUT_CONTROL_SET_TIME )
1237             {
1238                 i_time = val.i_time;
1239             }
1240             else
1241             {
1242                 /* Should not fail */
1243                 demux2_Control( p_input->input.p_demux,
1244                                 DEMUX_GET_TIME, &i_time );
1245                 i_time += val.i_time;
1246             }
1247             if( i_time < 0 ) i_time = 0;
1248             i_ret = demux2_Control( p_input->input.p_demux,
1249                                     DEMUX_SET_TIME, i_time );
1250             if( i_ret )
1251             {
1252                 int64_t i_length;
1253                 /* Emulate it with a SET_POS */
1254
1255                 demux2_Control( p_input->input.p_demux,
1256                                 DEMUX_GET_LENGTH, &i_length );
1257                 if( i_length > 0 )
1258                 {
1259                     double f_pos = (double)i_time / (double)i_length;
1260                     i_ret = demux2_Control( p_input->input.p_demux,
1261                                             DEMUX_SET_POSITION, f_pos );
1262                 }
1263             }
1264             if( i_ret )
1265             {
1266                 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1267                          " failed", i_time );
1268             }
1269             else
1270             {
1271                 if( p_input->i_slave > 0 )
1272                     SlaveSeek( p_input );
1273
1274                 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1275
1276                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1277                 b_force_update = VLC_TRUE;
1278             }
1279             break;
1280         }
1281
1282         case INPUT_CONTROL_SET_STATE:
1283             if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1284                 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1285             {
1286                 int i_ret;
1287                 if( p_input->input.p_access )
1288                     i_ret = access2_Control( p_input->input.p_access,
1289                                              ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1290                 else
1291                     i_ret = demux2_Control( p_input->input.p_demux,
1292                                             DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1293
1294                 if( i_ret )
1295                 {
1296                     /* FIXME What to do ? */
1297                     msg_Warn( p_input, "cannot unset pause -> EOF" );
1298                     input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1299                 }
1300
1301                 b_force_update = VLC_TRUE;
1302
1303                 /* Switch to play */
1304                 p_input->i_state = PLAYING_S;
1305                 val.i_int = PLAYING_S;
1306                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1307
1308                 /* Reset clock */
1309                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1310             }
1311             else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1312                      p_input->b_can_pause )
1313             {
1314                 int i_ret;
1315                 if( p_input->input.p_access )
1316                     i_ret = access2_Control( p_input->input.p_access,
1317                                              ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1318                 else
1319                     i_ret = demux2_Control( p_input->input.p_demux,
1320                                             DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1321
1322                 b_force_update = VLC_TRUE;
1323
1324                 if( i_ret )
1325                 {
1326                     msg_Warn( p_input, "cannot set pause state" );
1327                     val.i_int = p_input->i_state;
1328                 }
1329                 else
1330                 {
1331                     val.i_int = PAUSE_S;
1332                 }
1333
1334                 /* Switch to new state */
1335                 p_input->i_state = val.i_int;
1336                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1337             }
1338             else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1339             {
1340                 b_force_update = VLC_TRUE;
1341
1342                 /* Correct "state" value */
1343                 val.i_int = p_input->i_state;
1344                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1345             }
1346             else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1347             {
1348                 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1349             }
1350             break;
1351
1352         case INPUT_CONTROL_SET_RATE:
1353         case INPUT_CONTROL_SET_RATE_SLOWER:
1354         case INPUT_CONTROL_SET_RATE_FASTER:
1355         {
1356             int i_rate;
1357
1358             if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1359                 i_rate = p_input->i_rate * 2;
1360             else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1361                 i_rate = p_input->i_rate / 2;
1362             else
1363                 i_rate = val.i_int;
1364
1365             if( i_rate < INPUT_RATE_MIN )
1366             {
1367                 msg_Dbg( p_input, "cannot set rate faster" );
1368                 i_rate = INPUT_RATE_MIN;
1369             }
1370             else if( i_rate > INPUT_RATE_MAX )
1371             {
1372                 msg_Dbg( p_input, "cannot set rate slower" );
1373                 i_rate = INPUT_RATE_MAX;
1374             }
1375             if( i_rate != INPUT_RATE_DEFAULT &&
1376                 ( !p_input->b_can_pace_control ||
1377                   ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1378             {
1379                 msg_Dbg( p_input, "cannot change rate" );
1380                 i_rate = INPUT_RATE_DEFAULT;
1381             }
1382             if( i_rate != p_input->i_rate )
1383             {
1384                 p_input->i_rate  = i_rate;
1385                 val.i_int = i_rate;
1386                 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1387
1388                 /* We haven't send data to decoder when rate != default */
1389                 if( i_rate == INPUT_RATE_DEFAULT )
1390                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1391
1392                 /* Reset clock */
1393                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1394
1395                 b_force_update = VLC_TRUE;
1396             }
1397             break;
1398         }
1399
1400         case INPUT_CONTROL_SET_PROGRAM:
1401             /* No need to force update, es_out does it if needed */
1402             es_out_Control( p_input->p_es_out,
1403                             ES_OUT_SET_GROUP, val.i_int );
1404
1405             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1406                             NULL );
1407             break;
1408
1409         case INPUT_CONTROL_SET_ES:
1410             /* No need to force update, es_out does it if needed */
1411             es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1412                             input_EsOutGetFromID( p_input->p_es_out,
1413                                                   val.i_int ) );
1414             break;
1415
1416         case INPUT_CONTROL_SET_AUDIO_DELAY:
1417             input_EsOutSetDelay( p_input->p_es_out,
1418                                  AUDIO_ES, val.i_time );
1419             var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1420             break;
1421
1422         case INPUT_CONTROL_SET_SPU_DELAY:
1423             input_EsOutSetDelay( p_input->p_es_out,
1424                                  SPU_ES, val.i_time );
1425             var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1426             break;
1427
1428         case INPUT_CONTROL_SET_TITLE:
1429         case INPUT_CONTROL_SET_TITLE_NEXT:
1430         case INPUT_CONTROL_SET_TITLE_PREV:
1431             if( p_input->input.b_title_demux &&
1432                 p_input->input.i_title > 0 )
1433             {
1434                 /* TODO */
1435                 /* FIXME handle demux title */
1436                 demux_t *p_demux = p_input->input.p_demux;
1437                 int i_title;
1438
1439                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1440                     i_title = p_demux->info.i_title - 1;
1441                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1442                     i_title = p_demux->info.i_title + 1;
1443                 else
1444                     i_title = val.i_int;
1445
1446                 if( i_title >= 0 && i_title < p_input->input.i_title )
1447                 {
1448                     demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1449
1450                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1451                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1452
1453                     input_ControlVarTitle( p_input, i_title );
1454                 }
1455             }
1456             else if( p_input->input.i_title > 0 )
1457             {
1458                 access_t *p_access = p_input->input.p_access;
1459                 int i_title;
1460
1461                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1462                     i_title = p_access->info.i_title - 1;
1463                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1464                     i_title = p_access->info.i_title + 1;
1465                 else
1466                     i_title = val.i_int;
1467
1468                 if( i_title >= 0 && i_title < p_input->input.i_title )
1469                 {
1470                     access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1471                     stream_AccessReset( p_input->input.p_stream );
1472
1473                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1474                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1475                 }
1476             }
1477             break;
1478         case INPUT_CONTROL_SET_SEEKPOINT:
1479         case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1480         case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1481             if( p_input->input.b_title_demux &&
1482                 p_input->input.i_title > 0 )
1483             {
1484                 demux_t *p_demux = p_input->input.p_demux;
1485                 int i_seekpoint;
1486
1487                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1488                     i_seekpoint = p_demux->info.i_seekpoint - 1;
1489                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1490                     i_seekpoint = p_demux->info.i_seekpoint + 1;
1491                 else
1492                     i_seekpoint = val.i_int;
1493
1494                 if( i_seekpoint >= 0 && i_seekpoint <
1495                     p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1496                 {
1497                     demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1498
1499                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1500                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1501                 }
1502             }
1503             else if( p_input->input.i_title > 0 )
1504             {
1505                 access_t *p_access = p_input->input.p_access;
1506                 int i_seekpoint;
1507
1508                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1509                     i_seekpoint = p_access->info.i_seekpoint - 1;
1510                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1511                     i_seekpoint = p_access->info.i_seekpoint + 1;
1512                 else
1513                     i_seekpoint = val.i_int;
1514
1515                 if( i_seekpoint >= 0 && i_seekpoint <
1516                     p_input->input.title[p_access->info.i_title]->i_seekpoint )
1517                 {
1518                     access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1519                     stream_AccessReset( p_input->input.p_stream );
1520
1521                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1522                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1523                 }
1524             }
1525             break;
1526
1527         case INPUT_CONTROL_ADD_SLAVE:
1528             if( val.psz_string )
1529             {
1530                 input_source_t *slave = InputSourceNew( p_input );
1531
1532                 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1533                                       VLC_FALSE ) )
1534                 {
1535                     vlc_meta_t *p_meta_new = NULL;
1536                     vlc_meta_t *p_meta;
1537                     int64_t i_time;
1538
1539                     /* Add the slave */
1540                     msg_Dbg( p_input, "adding %s as slave on the fly",
1541                              val.psz_string );
1542
1543                     /* Set position */
1544                     if( demux2_Control( p_input->input.p_demux,
1545                                         DEMUX_GET_TIME, &i_time ) )
1546                     {
1547                         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1548                         InputSourceClean( p_input, slave );
1549                         free( slave );
1550                         break;
1551                     }
1552                     if( demux2_Control( slave->p_demux,
1553                                         DEMUX_SET_TIME, i_time ) )
1554                     {
1555                         msg_Err( p_input, "seek failed for new slave" );
1556                         InputSourceClean( p_input, slave );
1557                         free( slave );
1558                         break;
1559                     }
1560
1561
1562                     /* Get meta (access and demux) */
1563                     if( access2_Control( slave->p_access,
1564                                           ACCESS_GET_META, &p_meta_new ) )
1565                         p_meta_new = NULL;
1566                     if( !demux2_Control( slave->p_demux,
1567                                          DEMUX_GET_META, &p_meta ) )
1568                     {
1569                         if( p_meta_new )
1570                         {
1571                             vlc_meta_Merge( p_meta_new, p_meta );
1572                             vlc_meta_Delete( p_meta );
1573                         }
1574                         else
1575                         {
1576                             p_meta_new = p_meta;
1577                         }
1578                     }
1579                     /* Update meta */
1580                     if( p_meta_new )
1581                     {
1582                         if( p_input->p_meta )
1583                         {
1584                             vlc_meta_Merge( p_input->p_meta, p_meta_new );
1585                             vlc_meta_Delete( p_meta_new );
1586                         }
1587                         else
1588                         {
1589                             p_input->p_meta = p_meta_new;
1590                         }
1591                         UpdateMeta( p_input, VLC_FALSE );
1592                     }
1593
1594                     TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1595                 }
1596                 else
1597                 {
1598                     msg_Warn( p_input, "failed to add %s as slave",
1599                               val.psz_string );
1600                 }
1601
1602                 free( val.psz_string );
1603             }
1604             break;
1605
1606         case INPUT_CONTROL_SET_BOOKMARK:
1607         default:
1608             msg_Err( p_input, "not yet implemented" );
1609             break;
1610     }
1611
1612     return b_force_update;
1613 }
1614
1615 /*****************************************************************************
1616  * UpdateFromDemux:
1617  *****************************************************************************/
1618 static int UpdateFromDemux( input_thread_t *p_input )
1619 {
1620     demux_t *p_demux = p_input->input.p_demux;
1621     vlc_value_t v;
1622
1623     if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1624     {
1625         v.i_int = p_demux->info.i_title;
1626         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1627
1628         input_ControlVarTitle( p_input, p_demux->info.i_title );
1629
1630         p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1631     }
1632     if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1633     {
1634         v.i_int = p_demux->info.i_seekpoint;
1635         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1636
1637         p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1638     }
1639     p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1640
1641     /* Hmmm only works with master input */
1642     if( p_input->input.p_demux == p_demux )
1643     {
1644         int i_title_end = p_input->input.i_title_end -
1645             p_input->input.i_title_offset;
1646         int i_seekpoint_end = p_input->input.i_seekpoint_end -
1647             p_input->input.i_seekpoint_offset;
1648
1649         if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1650         {
1651             if( p_demux->info.i_title > i_title_end ||
1652                 ( p_demux->info.i_title == i_title_end &&
1653                   p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1654         }
1655         else if( i_seekpoint_end >=0 )
1656         {
1657             if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1658         }
1659         else if( i_title_end >= 0 )
1660         {
1661             if( p_demux->info.i_title > i_title_end ) return 0;
1662         }
1663     }
1664
1665     return 1;
1666 }
1667
1668 /*****************************************************************************
1669  * UpdateFromAccess:
1670  *****************************************************************************/
1671 static int UpdateFromAccess( input_thread_t *p_input )
1672 {
1673     access_t *p_access = p_input->input.p_access;
1674     vlc_value_t v;
1675
1676     if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1677     {
1678         v.i_int = p_access->info.i_title;
1679         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1680
1681         input_ControlVarTitle( p_input, p_access->info.i_title );
1682
1683         stream_AccessUpdate( p_input->input.p_stream );
1684
1685         p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1686     }
1687     if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1688     {
1689         v.i_int = p_access->info.i_seekpoint;
1690         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1691
1692         p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1693     }
1694     if( p_access->info.i_update & INPUT_UPDATE_META )
1695     {
1696         /* TODO maybe multi - access ? */
1697         vlc_meta_t *p_meta;
1698         if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1699         {
1700             if( p_input->p_meta )
1701             {
1702                 vlc_meta_Merge( p_input->p_meta, p_meta );
1703                 vlc_meta_Delete( p_meta );
1704             }
1705             else
1706             {
1707                 p_input->p_meta = p_meta;
1708             }
1709
1710             UpdateMeta( p_input, VLC_FALSE );
1711             var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1712         }
1713         p_access->info.i_update &= ~INPUT_UPDATE_META;
1714     }
1715
1716     p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1717
1718     /* Hmmm only works with master input */
1719     if( p_input->input.p_access == p_access )
1720     {
1721         int i_title_end = p_input->input.i_title_end -
1722             p_input->input.i_title_offset;
1723         int i_seekpoint_end = p_input->input.i_seekpoint_end -
1724             p_input->input.i_seekpoint_offset;
1725
1726         if( i_title_end >= 0 && i_seekpoint_end >=0 )
1727         {
1728             if( p_access->info.i_title > i_title_end ||
1729                 ( p_access->info.i_title == i_title_end &&
1730                   p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1731         }
1732         else if( i_seekpoint_end >=0 )
1733         {
1734             if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1735         }
1736         else if( i_title_end >= 0 )
1737         {
1738             if( p_access->info.i_title > i_title_end ) return 0;
1739         }
1740     }
1741
1742     return 1;
1743 }
1744
1745 /*****************************************************************************
1746  * UpdateMeta:
1747  *****************************************************************************/
1748 static int  UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1749 {
1750     vlc_meta_t *p_meta = p_input->p_meta;
1751     int i;
1752
1753     if( !p_meta || p_meta->i_meta == 0 )
1754         return VLC_SUCCESS;
1755
1756     if( !b_quick ) msg_Dbg( p_input, "meta information:" );
1757     for( i = 0; i < p_meta->i_meta; i++ )
1758     {
1759         if( !b_quick )
1760             msg_Dbg( p_input, "  - '%s' = '%s'",
1761                      _(p_meta->name[i]), p_meta->value[i] );
1762
1763         if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] &&
1764             !p_input->input.p_item->b_fixed_name )
1765             input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1766
1767         if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1768             input_Control( p_input, INPUT_ADD_INFO, _("General"),
1769                            _("Author"), p_meta->value[i] );
1770
1771         input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1772                       _(p_meta->name[i]), "%s", p_meta->value[i] );
1773     }
1774
1775     for( i = 0; i < p_meta->i_track; i++ )
1776     {
1777         vlc_meta_t *tk = p_meta->track[i];
1778         int j;
1779
1780         if( tk->i_meta > 0 )
1781         {
1782             char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1783
1784             msg_Dbg( p_input, "  - track[%d]:", i );
1785
1786             sprintf( psz_cat, "%s %d", _("Stream"), i );
1787             for( j = 0; j < tk->i_meta; j++ )
1788             {
1789                 msg_Dbg( p_input, "     - '%s' = '%s'", _(tk->name[j]),
1790                          tk->value[j] );
1791
1792                 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1793                                _(tk->name[j]), "%s", tk->value[j] );
1794             }
1795         }
1796     }
1797
1798     if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1799     {
1800         p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1801     }
1802
1803     return VLC_SUCCESS;
1804 }
1805
1806 /*****************************************************************************
1807  * UpdateItemLength:
1808  *****************************************************************************/
1809 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1810                               vlc_bool_t b_quick )
1811 {
1812     playlist_t *p_playlist;
1813     char psz_buffer[MSTRTIME_MAX_SIZE];
1814
1815     vlc_mutex_lock( &p_input->input.p_item->lock );
1816     p_input->input.p_item->i_duration = i_length;
1817     vlc_mutex_unlock( &p_input->input.p_item->lock );
1818
1819         p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1820                                                FIND_PARENT);
1821     if( p_playlist )
1822     {
1823         var_SetInteger( p_playlist, "item-change",
1824                         p_input->input.p_item->i_id );
1825         vlc_object_release( p_playlist );
1826     }
1827
1828     input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1829                    msecstotimestr( psz_buffer, i_length / 1000 ) );
1830 }
1831
1832 /*****************************************************************************
1833  * InputSourceNew:
1834  *****************************************************************************/
1835 static input_source_t *InputSourceNew( input_thread_t *p_input )
1836 {
1837     input_source_t *in = malloc( sizeof( input_source_t ) );
1838
1839     in->p_item   = NULL;
1840     in->p_access = NULL;
1841     in->p_stream = NULL;
1842     in->p_demux  = NULL;
1843     in->b_title_demux = VLC_FALSE;
1844     in->i_title  = 0;
1845     in->title    = NULL;
1846     in->b_can_pace_control = VLC_TRUE;
1847     in->b_eof = VLC_FALSE;
1848     in->i_cr_average = 0;
1849
1850     return in;
1851 }
1852
1853 /*****************************************************************************
1854  * InputSourceInit:
1855  *****************************************************************************/
1856 static int InputSourceInit( input_thread_t *p_input,
1857                             input_source_t *in, char *psz_mrl,
1858                             char *psz_forced_demux, vlc_bool_t b_quick )
1859 {
1860     char *psz_dup = strdup( psz_mrl );
1861     char *psz_access;
1862     char *psz_demux;
1863     char *psz_path;
1864     vlc_value_t val;
1865
1866     /* Split uri */
1867     if( !b_quick )
1868     {
1869         MRLSplit( p_input, psz_dup, &psz_access, &psz_demux, &psz_path );
1870
1871         msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1872                  psz_mrl, psz_access, psz_demux, psz_path );
1873
1874         /* Hack to allow udp://@:port syntax */
1875         if( !psz_access ||
1876             (strncmp( psz_access, "udp", 3 ) &&
1877              strncmp( psz_access, "rtp", 3 )) )
1878
1879         /* Find optional titles and seekpoints */
1880         MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1881                      &in->i_seekpoint_start, &in->i_seekpoint_end );
1882
1883         if( psz_forced_demux && *psz_forced_demux )
1884             psz_demux = psz_forced_demux;
1885
1886         /* Try access_demux if no demux given */
1887         if( *psz_demux == '\0' )
1888         {
1889             in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1890                                       NULL, p_input->p_es_out, VLC_FALSE );
1891         }
1892     }
1893     else
1894     {
1895         psz_path = psz_mrl;
1896         msg_Dbg( p_input, "trying to preparse %s",  psz_path );
1897         psz_demux = strdup( "" );
1898         psz_access = strdup( "file" );
1899     }
1900
1901     if( in->p_demux )
1902     {
1903         int64_t i_pts_delay;
1904
1905         /* Get infos from access_demux */
1906         demux2_Control( in->p_demux,
1907                         DEMUX_GET_PTS_DELAY, &i_pts_delay );
1908         p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1909
1910         in->b_title_demux = VLC_TRUE;
1911         if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1912                             &in->title, &in->i_title,
1913                             &in->i_title_offset, &in->i_seekpoint_offset ) )
1914         {
1915             in->i_title = 0;
1916             in->title   = NULL;
1917         }
1918         demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1919                         &in->b_can_pace_control );
1920         demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1921                         &in->b_can_pause );
1922
1923         /* FIXME todo
1924         demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1925                         &val.b_bool );
1926         */
1927     }
1928     else
1929     {
1930         int64_t i_pts_delay;
1931
1932         /* Now try a real access */
1933         in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
1934                                     b_quick );
1935
1936         /* Access failed, URL encoded ? */
1937         if( in->p_access == NULL && strchr( psz_path, '%' ) )
1938         {
1939             DecodeUrl( psz_path );
1940
1941             msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
1942                      psz_access, psz_demux, psz_path );
1943
1944             in->p_access = access2_New( p_input,
1945                                         psz_access, psz_demux, psz_path,
1946                                         b_quick );
1947         }
1948 #ifndef WIN32      /* Remove this gross hack from the win32 build as colons
1949                         * are forbidden in filenames on Win32. */
1950
1951         /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1952         if( in->p_access == NULL &&
1953             *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1954         {
1955             free( psz_dup );
1956             psz_dup = strdup( psz_mrl );
1957             psz_access = "";
1958             psz_demux = "";
1959             psz_path = psz_dup;
1960
1961             in->p_access = access2_New( p_input,
1962                                         psz_access, psz_demux, psz_path,
1963                                         b_quick );
1964         }
1965 #endif
1966
1967         if( in->p_access == NULL )
1968         {
1969             msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
1970             goto error;
1971         }
1972
1973         /* Get infos from access */
1974         if( !b_quick )
1975         {
1976             access2_Control( in->p_access,
1977                              ACCESS_GET_PTS_DELAY, &i_pts_delay );
1978             p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1979
1980             in->b_title_demux = VLC_FALSE;
1981             if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
1982                                  &in->title, &in->i_title,
1983                                 &in->i_title_offset, &in->i_seekpoint_offset ) )
1984
1985             {
1986                 in->i_title = 0;
1987                 in->title   = NULL;
1988             }
1989             access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
1990                              &in->b_can_pace_control );
1991             access2_Control( in->p_access, ACCESS_CAN_PAUSE,
1992                              &in->b_can_pause );
1993             access2_Control( in->p_access, ACCESS_CAN_SEEK,
1994                              &val.b_bool );
1995             var_Set( p_input, "seekable", val );
1996         }
1997
1998         /* Create the stream_t */
1999         in->p_stream = stream_AccessNew( in->p_access, b_quick );
2000         if( in->p_stream == NULL )
2001         {
2002             msg_Warn( p_input, "cannot create a stream_t from access" );
2003             goto error;
2004         }
2005
2006         /* Open a demuxer */
2007         if( *psz_demux == '\0' && *in->p_access->psz_demux )
2008         {
2009             psz_demux = in->p_access->psz_demux;
2010         }
2011         in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2012                                   in->p_stream, p_input->p_es_out, b_quick );
2013         if( in->p_demux == NULL )
2014         {
2015             msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2016                      psz_access, psz_demux, psz_path );
2017             goto error;
2018         }
2019
2020         /* TODO get title from demux */
2021         if( !b_quick && in->i_title <= 0 )
2022         {
2023             if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2024                                 &in->title, &in->i_title,
2025                                 &in->i_title_offset, &in->i_seekpoint_offset ))
2026             {
2027                 in->i_title = 0;
2028                 in->title   = NULL;
2029             }
2030             else
2031             {
2032                 in->b_title_demux = VLC_TRUE;
2033             }
2034         }
2035     }
2036     free( psz_dup );
2037     return VLC_SUCCESS;
2038
2039 error:
2040     if( in->p_demux )
2041         demux2_Delete( in->p_demux );
2042
2043     if( in->p_stream )
2044         stream_AccessDelete( in->p_stream );
2045
2046     if( in->p_access )
2047         access2_Delete( in->p_access );
2048     free( psz_dup );
2049
2050     return VLC_EGENERIC;
2051 }
2052
2053 /*****************************************************************************
2054  * InputSourceClean:
2055  *****************************************************************************/
2056 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2057 {
2058     if( in->p_demux )
2059         demux2_Delete( in->p_demux );
2060
2061     if( in->p_stream )
2062         stream_AccessDelete( in->p_stream );
2063
2064     if( in->p_access )
2065         access2_Delete( in->p_access );
2066
2067     if( in->i_title > 0 )
2068     {
2069         int i;
2070         for( i = 0; i < in->i_title; i++ )
2071         {
2072             vlc_input_title_Delete( in->title[i] );
2073         }
2074         free( in->title );
2075     }
2076 }
2077
2078 static void SlaveDemux( input_thread_t *p_input )
2079 {
2080     int64_t i_time;
2081     int i;
2082     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2083     {
2084         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2085         return;
2086     }
2087
2088     for( i = 0; i < p_input->i_slave; i++ )
2089     {
2090         input_source_t *in = p_input->slave[i];
2091         int i_ret = 1;
2092
2093         if( in->b_eof )
2094             continue;
2095
2096         if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2097         {
2098             for( ;; )
2099             {
2100                 int64_t i_stime;
2101                 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2102                 {
2103                     msg_Err( p_input, "slave[%d] doesn't like "
2104                              "DEMUX_GET_TIME -> EOF", i );
2105                     i_ret = 0;
2106                     break;
2107                 }
2108
2109                 if( i_stime >= i_time )
2110                     break;
2111
2112                 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2113                     break;
2114             }
2115         }
2116         else
2117         {
2118             i_ret = in->p_demux->pf_demux( in->p_demux );
2119         }
2120
2121         if( i_ret <= 0 )
2122         {
2123             msg_Dbg( p_input, "slave %d EOF", i );
2124             in->b_eof = VLC_TRUE;
2125         }
2126     }
2127 }
2128
2129 static void SlaveSeek( input_thread_t *p_input )
2130 {
2131     int64_t i_time;
2132     int i;
2133
2134     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2135     {
2136         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2137         return;
2138     }
2139
2140     for( i = 0; i < p_input->i_slave; i++ )
2141     {
2142         input_source_t *in = p_input->slave[i];
2143
2144         if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2145         {
2146             msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2147             in->b_eof = VLC_TRUE;
2148         }
2149     }
2150 }
2151 /*****************************************************************************
2152  * InputMetaUser:
2153  *****************************************************************************/
2154 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2155 {
2156     vlc_meta_t *p_meta;
2157     vlc_value_t val;
2158
2159     if( ( p_meta = vlc_meta_New() ) == NULL )
2160         return NULL;
2161
2162     /* Get meta information from user */
2163 #define GET_META( c, s ) \
2164     var_Get( p_input, (s), &val );  \
2165     if( *val.psz_string )       \
2166         vlc_meta_Add( p_meta, c, val.psz_string ); \
2167     free( val.psz_string )
2168
2169     GET_META( VLC_META_TITLE, "meta-title" );
2170     GET_META( VLC_META_AUTHOR, "meta-author" );
2171     GET_META( VLC_META_ARTIST, "meta-artist" );
2172     GET_META( VLC_META_GENRE, "meta-genre" );
2173     GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2174     GET_META( VLC_META_DESCRIPTION, "meta-description" );
2175     GET_META( VLC_META_DATE, "meta-date" );
2176     GET_META( VLC_META_URL, "meta-url" );
2177 #undef GET_META
2178
2179     return p_meta;
2180 }
2181
2182 /*****************************************************************************
2183  * DecodeUrl: decode a given encoded url
2184  *****************************************************************************/
2185 static void DecodeUrl( char *psz )
2186 {
2187     char *dup = strdup( psz );
2188     char *p = dup;
2189
2190     while( *p )
2191     {
2192         if( *p == '%' )
2193         {
2194             char val[3];
2195             p++;
2196             if( !*p )
2197             {
2198                 break;
2199             }
2200
2201             val[0] = *p++;
2202             val[1] = *p++;
2203             val[2] = '\0';
2204
2205             *psz++ = strtol( val, NULL, 16 );
2206         }
2207         else if( *p == '+' )
2208         {
2209             *psz++ = ' ';
2210             p++;
2211         }
2212         else
2213         {
2214             *psz++ = *p++;
2215         }
2216     }
2217     *psz++  ='\0';
2218     free( dup );
2219 }
2220
2221 /*****************************************************************************
2222  * ParseOption: parses the options for the input
2223  *****************************************************************************
2224  * This function parses the input (config) options and creates their associated
2225  * object variables.
2226  * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
2227  * bar is the value of the option.
2228  *****************************************************************************/
2229 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2230 {
2231     char *psz_name = (char *)psz_option;
2232     char *psz_value = strchr( psz_option, '=' );
2233     int  i_name_len, i_type;
2234     vlc_bool_t b_isno = VLC_FALSE;
2235     vlc_value_t val;
2236
2237     if( psz_value ) i_name_len = psz_value - psz_option;
2238     else i_name_len = strlen( psz_option );
2239
2240     /* It's too much of an hassle to remove the ':' when we parse
2241      * the cmd line :) */
2242     if( i_name_len && *psz_name == ':' )
2243     {
2244         psz_name++;
2245         i_name_len--;
2246     }
2247
2248     if( i_name_len == 0 ) return;
2249
2250     psz_name = strndup( psz_name, i_name_len );
2251     if( psz_value ) psz_value++;
2252
2253     i_type = config_GetType( p_input, psz_name );
2254
2255     if( !i_type && !psz_value )
2256     {
2257         /* check for "no-foo" or "nofoo" */
2258         if( !strncmp( psz_name, "no-", 3 ) )
2259         {
2260             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2261         }
2262         else if( !strncmp( psz_name, "no", 2 ) )
2263         {
2264             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2265         }
2266         else goto cleanup;           /* Option doesn't exist */
2267
2268         b_isno = VLC_TRUE;
2269         i_type = config_GetType( p_input, psz_name );
2270
2271         if( !i_type ) goto cleanup;  /* Option doesn't exist */
2272     }
2273     else if( !i_type ) goto cleanup; /* Option doesn't exist */
2274
2275     if( ( i_type != VLC_VAR_BOOL ) &&
2276         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2277
2278     /* Create the variable in the input object.
2279      * Children of the input object will be able to retreive this value
2280      * thanks to the inheritance property of the object variables. */
2281     var_Create( p_input, psz_name, i_type );
2282
2283     switch( i_type )
2284     {
2285     case VLC_VAR_BOOL:
2286         val.b_bool = !b_isno;
2287         break;
2288
2289     case VLC_VAR_INTEGER:
2290         val.i_int = atoi( psz_value );
2291         break;
2292
2293     case VLC_VAR_FLOAT:
2294         val.f_float = atof( psz_value );
2295         break;
2296
2297     case VLC_VAR_STRING:
2298     case VLC_VAR_MODULE:
2299     case VLC_VAR_FILE:
2300     case VLC_VAR_DIRECTORY:
2301         val.psz_string = psz_value;
2302         break;
2303
2304     default:
2305         goto cleanup;
2306         break;
2307     }
2308
2309     var_Set( p_input, psz_name, val );
2310
2311     msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2312
2313   cleanup:
2314     if( psz_name ) free( psz_name );
2315     return;
2316 }
2317
2318 /*****************************************************************************
2319  * MRLSplit: parse the access, demux and url part of the
2320  *           Media Resource Locator.
2321  *****************************************************************************/
2322 static void MRLSplit( input_thread_t *p_input, char *psz_dup,
2323                       char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2324 {
2325     char *psz_access = NULL;
2326     char *psz_demux  = NULL;
2327     char *psz_path   = NULL;
2328     char *psz, *psz_check;
2329
2330     psz = strchr( psz_dup, ':' );
2331
2332     /* '@' not allowed in access/demux part */
2333     psz_check = strchr( psz_dup, '@' );
2334     if( psz_check && psz_check < psz ) psz = 0;
2335
2336 #if defined( WIN32 ) || defined( UNDER_CE )
2337     if( psz - psz_dup == 1 )
2338     {
2339         msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2340         psz_path = psz_dup;
2341     }
2342     else
2343 #endif
2344
2345     if( psz )
2346     {
2347         *psz++ = '\0';
2348         if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2349
2350         psz_path = psz;
2351
2352         psz = strchr( psz_dup, '/' );
2353         if( psz )
2354         {
2355             *psz++ = '\0';
2356             psz_demux = psz;
2357         }
2358
2359         psz_access = psz_dup;
2360     }
2361     else
2362     {
2363         psz_path = psz_dup;
2364     }
2365
2366     if( !psz_access ) *ppsz_access = "";
2367     else *ppsz_access = psz_access;
2368
2369     if( !psz_demux ) *ppsz_demux = "";
2370     else *ppsz_demux = psz_demux;
2371
2372     if( !psz_path ) *ppsz_path = "";
2373     else *ppsz_path = psz_path;
2374 }
2375
2376 /*****************************************************************************
2377  * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2378  *
2379  * Syntax:
2380  * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2381  *****************************************************************************/
2382 static void MRLSections( input_thread_t *p_input, char *psz_source,
2383                          int *pi_title_start, int *pi_title_end,
2384                          int *pi_chapter_start, int *pi_chapter_end )
2385 {
2386     char *psz, *psz_end, *psz_next, *psz_check;
2387
2388     *pi_title_start = *pi_title_end = -1;
2389     *pi_chapter_start = *pi_chapter_end = -1;
2390
2391     /* Start by parsing titles and chapters */
2392     if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2393
2394     /* Check we are really dealing with a title/chapter section */
2395     psz_check = psz + 1;
2396     if( !*psz_check ) return;
2397     if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2398     if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2399     if( *psz_check == ':' && ++psz_check )
2400         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2401     if( *psz_check != '-' && *psz_check ) return;
2402     if( *psz_check == '-' && ++psz_check )
2403         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2404     if( *psz_check != ':' && *psz_check ) return;
2405     if( *psz_check == ':' && ++psz_check )
2406         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2407     if( *psz_check ) return;
2408
2409     /* Separate start and end */
2410     *psz++ = 0;
2411     if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2412
2413     /* Look for the start title */
2414     *pi_title_start = strtol( psz, &psz_next, 0 );
2415     if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2416     *pi_title_end = *pi_title_start;
2417     psz = psz_next;
2418
2419     /* Look for the start chapter */
2420     if( *psz ) psz++;
2421     *pi_chapter_start = strtol( psz, &psz_next, 0 );
2422     if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2423     *pi_chapter_end = *pi_chapter_start;
2424
2425     if( psz_end )
2426     {
2427         /* Look for the end title */
2428         *pi_title_end = strtol( psz_end, &psz_next, 0 );
2429         if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2430         psz_end = psz_next;
2431
2432         /* Look for the end chapter */
2433         if( *psz_end ) psz_end++;
2434         *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2435         if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2436     }
2437
2438     msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2439              psz_source, *pi_title_start, *pi_chapter_start,
2440              *pi_title_end, *pi_chapter_end );
2441 }