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