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