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