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