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