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