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