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