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