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