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