]> git.sesse.net Git - vlc/blob - src/input/input.c
Don't notify playlist several times while preparsing
[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 );
59 static void             Error   ( input_thread_t *p_input );
60 static void             End     ( input_thread_t *p_input );
61 static void             MainLoop( input_thread_t *p_input );
62
63 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
64 static void       ControlReduce( input_thread_t * );
65 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
66
67 static int  UpdateFromAccess( input_thread_t * );
68 static int  UpdateFromDemux( input_thread_t * );
69 static int  UpdateMeta( input_thread_t * );
70
71 static void UpdateItemLength( input_thread_t *, int64_t i_length );
72
73 static void 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 static void InputSourceClean( input_thread_t *, input_source_t * );
80
81 static void SlaveDemux( input_thread_t *p_input );
82 static void SlaveSeek( input_thread_t *p_input );
83
84 static void InputMetaUser( input_thread_t *p_input );
85
86 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item );
87
88 /*****************************************************************************
89  * This function creates a new input, and returns a pointer
90  * to its description. On error, it returns NULL.
91  *
92  * Variables for _public_ use:
93  * * Get and Set:
94  *  - state
95  *  - rate,rate-slower, rate-faster
96  *  - position, position-offset
97  *  - time, time-offset
98  *  - title,title-next,title-prev
99  *  - chapter,chapter-next, chapter-prev
100  *  - program, audio-es, video-es, spu-es
101  *  - audio-delay, spu-delay
102  *  - bookmark
103  * * Get only:
104  *  - length
105  *  - bookmarks
106  *  - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
107  * * For intf callback upon changes
108  *  - intf-change
109  * TODO explain when Callback is called
110  * TODO complete this list (?)
111  *****************************************************************************/
112 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
113                                char *psz_header, vlc_bool_t b_quick )
114 {
115     input_thread_t *p_input = NULL;                 /* thread descriptor */
116     vlc_value_t val;
117     int i;
118
119     /* Allocate descriptor */
120     p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
121     if( p_input == NULL )
122     {
123         msg_Err( p_parent, "out of memory" );
124         return NULL;
125     }
126     p_input->b_preparsing = b_quick;
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( !p_input->b_preparsing )
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 );
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 ) )
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 ) )
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 );
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 )
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( !p_input->b_preparsing )
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 ) )
794     {
795         goto error;
796     }
797
798     /* Create global title (from master) */
799     if( !p_input->b_preparsing )
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 );
838         p_input->input.p_item->i_duration = val.i_time;
839     }
840
841     /* Start title/chapter */
842     if( !p_input->b_preparsing )
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                     {
945                         TAB_APPEND( p_input->i_slave, p_input->slave, sub );
946                     }
947                 }
948                 free( subs[i] );
949             }
950             if( subs ) free( subs );
951             if( psz_autopath ) free( psz_autopath );
952         }
953         free( psz_subtitle );
954
955         /* Look for slave */
956         psz = var_GetString( p_input, "input-slave" );
957         if( *psz )
958         {
959             char *psz_delim;
960             input_source_t *slave;
961             while( psz && *psz )
962             {
963                 while( *psz == ' ' || *psz == '#' )
964                 {
965                     psz++;
966                 }
967                 if( ( psz_delim = strchr( psz, '#' ) ) )
968                 {
969                     *psz_delim++ = '\0';
970                 }
971                 if( *psz == 0 )
972                 {
973                     break;
974                 }
975
976                 msg_Dbg( p_input, "adding slave input '%s'", psz );
977                 slave = InputSourceNew( p_input );
978                 if( !InputSourceInit( p_input, slave, psz, NULL ) )
979                 {
980                     TAB_APPEND( p_input->i_slave, p_input->slave, slave );
981                 }
982                 psz = psz_delim;
983             }
984         }
985         if( psz ) free( psz );
986     }
987     else
988     {
989         p_input->i_start = 0;
990         p_input->i_start = 0;
991     }
992
993     /* Set up es_out */
994     if( !p_input->b_preparsing )
995     {
996         es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
997         i_es_out_mode = ES_OUT_MODE_AUTO;
998         val.p_list = NULL;
999         if( p_input->p_sout )
1000         {
1001             var_Get( p_input, "sout-all", &val );
1002             if ( val.b_bool )
1003             {
1004                 i_es_out_mode = ES_OUT_MODE_ALL;
1005                 val.p_list = NULL;
1006             }
1007             else
1008             {
1009                 var_Get( p_input, "programs", &val );
1010                 if ( val.p_list && val.p_list->i_count )
1011                 {
1012                     i_es_out_mode = ES_OUT_MODE_PARTIAL;
1013                     /* Note : we should remove the "program" callback. */
1014                 }
1015                 else
1016                     var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1017                                 NULL );
1018             }
1019         }
1020         es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1021
1022         /* Inform the demuxer about waited group (needed only for DVB) */
1023         if( i_es_out_mode == ES_OUT_MODE_ALL )
1024         {
1025             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1026         }
1027         else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1028         {
1029             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
1030                             val.p_list );
1031         }
1032         else
1033         {
1034             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
1035                            (int) var_GetInteger( p_input, "program" ), NULL );
1036         }
1037
1038         if( p_input->p_sout )
1039         {
1040             if( p_input->p_sout->i_out_pace_nocontrol > 0 )
1041             {
1042                 p_input->b_out_pace_control = VLC_FALSE;
1043             }
1044             else
1045             {
1046                 p_input->b_out_pace_control = VLC_TRUE;
1047             }
1048
1049             if( p_input->b_can_pace_control && p_input->b_out_pace_control )
1050             {
1051                 /* We don't want a high input priority here or we'll
1052                  * end-up sucking up all the CPU time */
1053                 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1054             }
1055
1056             msg_Dbg( p_input, "starting in %s mode",
1057                      p_input->b_out_pace_control ? "async" : "sync" );
1058         }
1059     }
1060
1061     p_meta = p_input->input.p_item->p_meta;
1062     /* Get meta data from users */
1063     InputMetaUser( p_input );
1064     /* Get meta data from master input */
1065     demux2_Control( p_input->input.p_demux, DEMUX_GET_META, p_meta );
1066
1067     /* Access_file does not give any meta, and there are no slave */
1068     if( !p_input->b_preparsing )
1069     {
1070         if( p_input->input.p_access )
1071             access2_Control( p_input->input.p_access, ACCESS_GET_META,
1072                              p_meta );
1073
1074         /* Get meta data from slave input */
1075         for( i = 0; i < p_input->i_slave; i++ )
1076         {
1077             demux2_Control( p_input->slave[i]->p_demux,
1078                             DEMUX_GET_META, p_meta );
1079             if( p_input->slave[i]->p_access )
1080             {
1081                 access2_Control( p_input->slave[i]->p_access,
1082                                  ACCESS_GET_META, p_meta );
1083             }
1084         }
1085     }
1086
1087     UpdateMeta( p_input );
1088
1089     if( !p_input->b_preparsing )
1090     {
1091         msg_Dbg( p_input, "`%s' successfully opened",
1092                  p_input->input.p_item->psz_uri );
1093
1094     }
1095
1096     /* initialization is complete */
1097     input_ChangeState(p_input, PLAYING_S);
1098
1099     return VLC_SUCCESS;
1100
1101 error:
1102     if( p_input->p_es_out )
1103         input_EsOutDelete( p_input->p_es_out );
1104
1105     if( p_input->p_sout )
1106         sout_DeleteInstance( p_input->p_sout );
1107
1108     /* Mark them deleted */
1109     p_input->input.p_demux = NULL;
1110     p_input->input.p_stream = NULL;
1111     p_input->input.p_access = NULL;
1112     p_input->p_es_out = NULL;
1113     p_input->p_sout = NULL;
1114
1115     return VLC_EGENERIC;
1116 }
1117
1118 /*****************************************************************************
1119  * Error: RunThread() error loop
1120  *****************************************************************************
1121  * This function is called when an error occurred during thread main's loop.
1122  *****************************************************************************/
1123 static void Error( input_thread_t *p_input )
1124 {
1125     while( !p_input->b_die )
1126     {
1127         /* Sleep a while */
1128         msleep( INPUT_IDLE_SLEEP );
1129     }
1130 }
1131
1132 /*****************************************************************************
1133  * End: end the input thread
1134  *****************************************************************************/
1135 static void End( input_thread_t * p_input )
1136 {
1137     int i;
1138
1139     msg_Dbg( p_input, "closing input" );
1140
1141     /* We are at the end */
1142     input_ChangeState(p_input, END_S);
1143
1144     /* Clean control variables */
1145     input_ControlVarClean( p_input );
1146
1147     /* Clean up master */
1148     InputSourceClean( p_input, &p_input->input );
1149
1150     /* Delete slave */
1151     for( i = 0; i < p_input->i_slave; i++ )
1152     {
1153         InputSourceClean( p_input, p_input->slave[i] );
1154         free( p_input->slave[i] );
1155     }
1156     if( p_input->slave ) free( p_input->slave );
1157
1158     /* Unload all modules */
1159     if( p_input->p_es_out )
1160         input_EsOutDelete( p_input->p_es_out );
1161
1162 #define CL_CO( c ) stats_CounterClean( p_input->counters.p_##c ); p_input->counters.p_##c = NULL;
1163     if( p_input->p_libvlc->b_stats )
1164     {
1165         vlc_mutex_lock( &p_input->counters.counters_lock );
1166         CL_CO( read_bytes );
1167         CL_CO( read_packets );
1168         CL_CO( demux_read );
1169         CL_CO( input_bitrate );
1170         CL_CO( demux_bitrate );
1171         CL_CO( played_abuffers );
1172         CL_CO( lost_abuffers );
1173         CL_CO( displayed_pictures );
1174         CL_CO( lost_pictures );
1175         CL_CO( decoded_audio) ;
1176         CL_CO( decoded_video );
1177         CL_CO( decoded_sub) ;
1178         vlc_mutex_unlock( &p_input->counters.counters_lock );
1179     }
1180
1181     /* Close optional stream output instance */
1182     if( p_input->p_sout )
1183     {
1184         vlc_value_t keep;
1185
1186         vlc_mutex_lock( &p_input->counters.counters_lock );
1187         CL_CO( sout_sent_packets );
1188         CL_CO( sout_sent_bytes );
1189         CL_CO( sout_send_bitrate );
1190         vlc_mutex_unlock( &p_input->counters.counters_lock );
1191
1192         if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool )
1193         {
1194             /* attach sout to the playlist */
1195             msg_Dbg( p_input, "keeping sout" );
1196             vlc_object_detach( p_input->p_sout );
1197             vlc_object_attach( p_input->p_sout, p_input->p_libvlc->p_playlist );
1198         }
1199         else
1200         {
1201             msg_Dbg( p_input, "destroying sout" );
1202             sout_DeleteInstance( p_input->p_sout );
1203         }
1204     }
1205
1206 #undef CL_CO
1207     /* Tell we're dead */
1208     p_input->b_dead = VLC_TRUE;
1209 }
1210
1211 /*****************************************************************************
1212  * Control
1213  *****************************************************************************/
1214 static inline int ControlPopNoLock( input_thread_t *p_input,
1215                                     int *pi_type, vlc_value_t *p_val )
1216 {
1217     if( p_input->i_control <= 0 )
1218     {
1219         return VLC_EGENERIC;
1220     }
1221
1222     *pi_type = p_input->control[0].i_type;
1223     *p_val   = p_input->control[0].val;
1224
1225     p_input->i_control--;
1226     if( p_input->i_control > 0 )
1227     {
1228         int i;
1229
1230         for( i = 0; i < p_input->i_control; i++ )
1231         {
1232             p_input->control[i].i_type = p_input->control[i+1].i_type;
1233             p_input->control[i].val    = p_input->control[i+1].val;
1234         }
1235     }
1236
1237     return VLC_SUCCESS;
1238 }
1239
1240 static void ControlReduce( input_thread_t *p_input )
1241 {
1242     int i;
1243     for( i = 1; i < p_input->i_control; i++ )
1244     {
1245         const int i_lt = p_input->control[i-1].i_type;
1246         const int i_ct = p_input->control[i].i_type;
1247
1248         /* XXX We can't merge INPUT_CONTROL_SET_ES */
1249 /*        msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1250                  i_lt, i_ct );
1251 */
1252         if( i_lt == i_ct &&
1253             ( i_ct == INPUT_CONTROL_SET_STATE ||
1254               i_ct == INPUT_CONTROL_SET_RATE ||
1255               i_ct == INPUT_CONTROL_SET_POSITION ||
1256               i_ct == INPUT_CONTROL_SET_TIME ||
1257               i_ct == INPUT_CONTROL_SET_PROGRAM ||
1258               i_ct == INPUT_CONTROL_SET_TITLE ||
1259               i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1260               i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1261         {
1262             int j;
1263 //            msg_Dbg( p_input, "merged at %d", i );
1264             /* Remove the i-1 */
1265             for( j = i; j <  p_input->i_control; j++ )
1266                 p_input->control[j-1] = p_input->control[j];
1267             p_input->i_control--;
1268         }
1269         else
1270         {
1271             /* TODO but that's not that important
1272                 - merge SET_X with SET_X_CMD
1273                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1274                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1275                 - ?
1276                 */
1277         }
1278     }
1279 }
1280
1281 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1282                            vlc_value_t val )
1283 {
1284     vlc_bool_t b_force_update = VLC_FALSE;
1285
1286     if( !p_input ) return b_force_update;
1287
1288     switch( i_type )
1289     {
1290         case INPUT_CONTROL_SET_DIE:
1291             msg_Dbg( p_input, "control: stopping input" );
1292             /* Mark all submodules to die */
1293             if( p_input->input.p_access )
1294                 p_input->input.p_access->b_die = VLC_TRUE;
1295             if( p_input->input.p_stream )
1296                 p_input->input.p_stream->b_die = VLC_TRUE;
1297             p_input->input.p_demux->b_die = VLC_TRUE;
1298
1299             p_input->b_die = VLC_TRUE;
1300             break;
1301
1302         case INPUT_CONTROL_SET_POSITION:
1303         case INPUT_CONTROL_SET_POSITION_OFFSET:
1304         {
1305             double f_pos;
1306             if( i_type == INPUT_CONTROL_SET_POSITION )
1307             {
1308                 f_pos = val.f_float;
1309             }
1310             else
1311             {
1312                 /* Should not fail */
1313                 demux2_Control( p_input->input.p_demux,
1314                                 DEMUX_GET_POSITION, &f_pos );
1315                 f_pos += val.f_float;
1316             }
1317             if( f_pos < 0.0 ) f_pos = 0.0;
1318             if( f_pos > 1.0 ) f_pos = 1.0;
1319             /* Reset the decoders states and clock sync (before calling the demuxer */
1320             es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1321             input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1322             if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1323                                 f_pos ) )
1324             {
1325                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1326                          "%2.1f%% failed", f_pos * 100 );
1327             }
1328             else
1329             {
1330                 if( p_input->i_slave > 0 )
1331                     SlaveSeek( p_input );
1332
1333                 b_force_update = VLC_TRUE;
1334             }
1335             break;
1336         }
1337
1338         case INPUT_CONTROL_SET_TIME:
1339         case INPUT_CONTROL_SET_TIME_OFFSET:
1340         {
1341             int64_t i_time;
1342             int i_ret;
1343
1344             if( i_type == INPUT_CONTROL_SET_TIME )
1345             {
1346                 i_time = val.i_time;
1347             }
1348             else
1349             {
1350                 /* Should not fail */
1351                 demux2_Control( p_input->input.p_demux,
1352                                 DEMUX_GET_TIME, &i_time );
1353                 i_time += val.i_time;
1354             }
1355             if( i_time < 0 ) i_time = 0;
1356
1357             /* Reset the decoders states and clock sync (before calling the demuxer */
1358             es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1359             input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1360
1361             i_ret = demux2_Control( p_input->input.p_demux,
1362                                     DEMUX_SET_TIME, i_time );
1363             if( i_ret )
1364             {
1365                 int64_t i_length;
1366
1367                 /* Emulate it with a SET_POS */
1368                 demux2_Control( p_input->input.p_demux,
1369                                 DEMUX_GET_LENGTH, &i_length );
1370                 if( i_length > 0 )
1371                 {
1372                     double f_pos = (double)i_time / (double)i_length;
1373                     i_ret = demux2_Control( p_input->input.p_demux,
1374                                             DEMUX_SET_POSITION, f_pos );
1375                 }
1376             }
1377             if( i_ret )
1378             {
1379                 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1380                          " failed or not possible", i_time );
1381             }
1382             else
1383             {
1384                 if( p_input->i_slave > 0 )
1385                     SlaveSeek( p_input );
1386
1387                 b_force_update = VLC_TRUE;
1388             }
1389             break;
1390         }
1391
1392         case INPUT_CONTROL_SET_STATE:
1393             if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1394                 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1395             {
1396                 int i_ret;
1397                 if( p_input->input.p_access )
1398                     i_ret = access2_Control( p_input->input.p_access,
1399                                              ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1400                 else
1401                     i_ret = demux2_Control( p_input->input.p_demux,
1402                                             DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1403
1404                 if( i_ret )
1405                 {
1406                     /* FIXME What to do ? */
1407                     msg_Warn( p_input, "cannot unset pause -> EOF" );
1408                     vlc_mutex_unlock( &p_input->lock_control );
1409                     input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1410                     vlc_mutex_lock( &p_input->lock_control );
1411                 }
1412
1413                 b_force_update = VLC_TRUE;
1414
1415                 /* Switch to play */
1416                 p_input->i_state = PLAYING_S;
1417                 val.i_int = PLAYING_S;
1418                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1419
1420                 /* Reset clock */
1421                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1422                 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1423             }
1424             else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1425                      p_input->b_can_pause )
1426             {
1427                 int i_ret;
1428                 if( p_input->input.p_access )
1429                     i_ret = access2_Control( p_input->input.p_access,
1430                                              ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1431                 else
1432                     i_ret = demux2_Control( p_input->input.p_demux,
1433                                             DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1434
1435                 b_force_update = VLC_TRUE;
1436
1437                 if( i_ret )
1438                 {
1439                     msg_Warn( p_input, "cannot set pause state" );
1440                     val.i_int = p_input->i_state;
1441                 }
1442                 else
1443                 {
1444                     val.i_int = PAUSE_S;
1445                 }
1446
1447                 /* Switch to new state */
1448                 p_input->i_state = val.i_int;
1449                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1450             }
1451             else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1452             {
1453                 b_force_update = VLC_TRUE;
1454
1455                 /* Correct "state" value */
1456                 val.i_int = p_input->i_state;
1457                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1458             }
1459             else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1460             {
1461                 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1462             }
1463             break;
1464
1465         case INPUT_CONTROL_SET_RATE:
1466         case INPUT_CONTROL_SET_RATE_SLOWER:
1467         case INPUT_CONTROL_SET_RATE_FASTER:
1468         {
1469             int i_rate;
1470
1471             if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1472                 i_rate = p_input->i_rate * 2;
1473             else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1474                 i_rate = p_input->i_rate / 2;
1475             else
1476                 i_rate = val.i_int;
1477
1478             if( i_rate < INPUT_RATE_MIN )
1479             {
1480                 msg_Dbg( p_input, "cannot set rate faster" );
1481                 i_rate = INPUT_RATE_MIN;
1482             }
1483             else if( i_rate > INPUT_RATE_MAX )
1484             {
1485                 msg_Dbg( p_input, "cannot set rate slower" );
1486                 i_rate = INPUT_RATE_MAX;
1487             }
1488             if( i_rate != INPUT_RATE_DEFAULT &&
1489                 ( !p_input->b_can_pace_control ||
1490                   ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1491             {
1492                 msg_Dbg( p_input, "cannot change rate" );
1493                 i_rate = INPUT_RATE_DEFAULT;
1494             }
1495             if( i_rate != p_input->i_rate )
1496             {
1497                 p_input->i_rate  = i_rate;
1498                 val.i_int = i_rate;
1499                 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1500
1501                 /* We haven't send data to decoder when rate != default */
1502                 if( i_rate == INPUT_RATE_DEFAULT )
1503                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1504
1505                 /* Reset clock */
1506                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1507
1508                 b_force_update = VLC_TRUE;
1509             }
1510             break;
1511         }
1512
1513         case INPUT_CONTROL_SET_PROGRAM:
1514             /* No need to force update, es_out does it if needed */
1515             es_out_Control( p_input->p_es_out,
1516                             ES_OUT_SET_GROUP, val.i_int );
1517
1518             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1519                             NULL );
1520             break;
1521
1522         case INPUT_CONTROL_SET_ES:
1523             /* No need to force update, es_out does it if needed */
1524             es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1525                             input_EsOutGetFromID( p_input->p_es_out,
1526                                                   val.i_int ) );
1527             break;
1528
1529         case INPUT_CONTROL_SET_AUDIO_DELAY:
1530             input_EsOutSetDelay( p_input->p_es_out,
1531                                  AUDIO_ES, val.i_time );
1532             var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1533             break;
1534
1535         case INPUT_CONTROL_SET_SPU_DELAY:
1536             input_EsOutSetDelay( p_input->p_es_out,
1537                                  SPU_ES, val.i_time );
1538             var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1539             break;
1540
1541         case INPUT_CONTROL_SET_TITLE:
1542         case INPUT_CONTROL_SET_TITLE_NEXT:
1543         case INPUT_CONTROL_SET_TITLE_PREV:
1544             if( p_input->input.b_title_demux &&
1545                 p_input->input.i_title > 0 )
1546             {
1547                 /* TODO */
1548                 /* FIXME handle demux title */
1549                 demux_t *p_demux = p_input->input.p_demux;
1550                 int i_title;
1551
1552                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1553                     i_title = p_demux->info.i_title - 1;
1554                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1555                     i_title = p_demux->info.i_title + 1;
1556                 else
1557                     i_title = val.i_int;
1558
1559                 if( i_title >= 0 && i_title < p_input->input.i_title )
1560                 {
1561                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1562                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1563
1564                     demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1565                     input_ControlVarTitle( p_input, i_title );
1566                 }
1567             }
1568             else if( p_input->input.i_title > 0 )
1569             {
1570                 access_t *p_access = p_input->input.p_access;
1571                 int i_title;
1572
1573                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1574                     i_title = p_access->info.i_title - 1;
1575                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1576                     i_title = p_access->info.i_title + 1;
1577                 else
1578                     i_title = val.i_int;
1579
1580                 if( i_title >= 0 && i_title < p_input->input.i_title )
1581                 {
1582                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1583                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1584
1585                     access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1586                     stream_AccessReset( p_input->input.p_stream );
1587                 }
1588             }
1589             break;
1590         case INPUT_CONTROL_SET_SEEKPOINT:
1591         case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1592         case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1593             if( p_input->input.b_title_demux &&
1594                 p_input->input.i_title > 0 )
1595             {
1596                 demux_t *p_demux = p_input->input.p_demux;
1597                 int i_seekpoint;
1598                 int64_t i_input_time;
1599                 int64_t i_seekpoint_time;
1600
1601                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1602                 {
1603                     i_seekpoint = p_demux->info.i_seekpoint;
1604                     i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1605                     if( i_seekpoint_time >= 0 &&
1606                          !demux2_Control( p_demux,
1607                                           DEMUX_GET_TIME, &i_input_time ) )
1608                     {
1609                         if ( i_input_time < i_seekpoint_time + 3000000 )
1610                             i_seekpoint--;
1611                     }
1612                     else
1613                         i_seekpoint--;
1614                 }
1615                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1616                     i_seekpoint = p_demux->info.i_seekpoint + 1;
1617                 else
1618                     i_seekpoint = val.i_int;
1619
1620                 if( i_seekpoint >= 0 && i_seekpoint <
1621                     p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1622                 {
1623                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1624                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1625
1626                     demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1627                 }
1628             }
1629             else if( p_input->input.i_title > 0 )
1630             {
1631                 demux_t *p_demux = p_input->input.p_demux;
1632                 access_t *p_access = p_input->input.p_access;
1633                 int i_seekpoint;
1634                 int64_t i_input_time;
1635                 int64_t i_seekpoint_time;
1636
1637                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1638                 {
1639                     i_seekpoint = p_access->info.i_seekpoint;
1640                     i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1641                     if( i_seekpoint_time >= 0 &&
1642                         demux2_Control( p_demux,
1643                                         DEMUX_GET_TIME, &i_input_time ) )
1644                     {
1645                         if ( i_input_time < i_seekpoint_time + 3000000 )
1646                             i_seekpoint--;
1647                     }
1648                     else
1649                         i_seekpoint--;
1650                 }
1651                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT ) 
1652                     i_seekpoint = p_access->info.i_seekpoint + 1;
1653                 else
1654                     i_seekpoint = val.i_int;
1655
1656                 if( i_seekpoint >= 0 && i_seekpoint <
1657                     p_input->input.title[p_access->info.i_title]->i_seekpoint )
1658                 {
1659                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1660                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1661
1662                     access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1663                                     i_seekpoint );
1664                     stream_AccessReset( p_input->input.p_stream );
1665                 }
1666             }
1667             break;
1668
1669         case INPUT_CONTROL_ADD_SLAVE:
1670             if( val.psz_string )
1671             {
1672                 input_source_t *slave = InputSourceNew( p_input );
1673
1674                 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1675                 {
1676                     vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1677                     int64_t i_time;
1678
1679                     /* Add the slave */
1680                     msg_Dbg( p_input, "adding %s as slave on the fly",
1681                              val.psz_string );
1682
1683                     /* Set position */
1684                     if( demux2_Control( p_input->input.p_demux,
1685                                         DEMUX_GET_TIME, &i_time ) )
1686                     {
1687                         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1688                         InputSourceClean( p_input, slave );
1689                         free( slave );
1690                         break;
1691                     }
1692                     if( demux2_Control( slave->p_demux,
1693                                         DEMUX_SET_TIME, i_time ) )
1694                     {
1695                         msg_Err( p_input, "seek failed for new slave" );
1696                         InputSourceClean( p_input, slave );
1697                         free( slave );
1698                         break;
1699                     }
1700
1701                     /* Get meta (access and demux) */
1702                     access2_Control( slave->p_access, ACCESS_GET_META,
1703                                      p_meta );
1704                     demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1705                     UpdateMeta( p_input );
1706
1707                     TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1708                 }
1709                 else
1710                 {
1711                     msg_Warn( p_input, "failed to add %s as slave",
1712                               val.psz_string );
1713                 }
1714
1715                 free( val.psz_string );
1716             }
1717             break;
1718
1719         case INPUT_CONTROL_SET_BOOKMARK:
1720         default:
1721             msg_Err( p_input, "not yet implemented" );
1722             break;
1723     }
1724
1725     return b_force_update;
1726 }
1727
1728 /*****************************************************************************
1729  * UpdateFromDemux:
1730  *****************************************************************************/
1731 static int UpdateFromDemux( input_thread_t *p_input )
1732 {
1733     demux_t *p_demux = p_input->input.p_demux;
1734     vlc_value_t v;
1735
1736     if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1737     {
1738         v.i_int = p_demux->info.i_title;
1739         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1740
1741         input_ControlVarTitle( p_input, p_demux->info.i_title );
1742
1743         p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1744     }
1745     if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1746     {
1747         v.i_int = p_demux->info.i_seekpoint;
1748         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1749
1750         p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1751     }
1752     p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1753
1754     /* Hmmm only works with master input */
1755     if( p_input->input.p_demux == p_demux )
1756     {
1757         int i_title_end = p_input->input.i_title_end -
1758             p_input->input.i_title_offset;
1759         int i_seekpoint_end = p_input->input.i_seekpoint_end -
1760             p_input->input.i_seekpoint_offset;
1761
1762         if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1763         {
1764             if( p_demux->info.i_title > i_title_end ||
1765                 ( p_demux->info.i_title == i_title_end &&
1766                   p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1767         }
1768         else if( i_seekpoint_end >=0 )
1769         {
1770             if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1771         }
1772         else if( i_title_end >= 0 )
1773         {
1774             if( p_demux->info.i_title > i_title_end ) return 0;
1775         }
1776     }
1777
1778     return 1;
1779 }
1780
1781 /*****************************************************************************
1782  * UpdateFromAccess:
1783  *****************************************************************************/
1784 static int UpdateFromAccess( input_thread_t *p_input )
1785 {
1786     access_t *p_access = p_input->input.p_access;
1787     vlc_value_t v;
1788
1789     if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1790     {
1791         v.i_int = p_access->info.i_title;
1792         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1793
1794         input_ControlVarTitle( p_input, p_access->info.i_title );
1795
1796         stream_AccessUpdate( p_input->input.p_stream );
1797
1798         p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1799     }
1800     if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1801     {
1802         v.i_int = p_access->info.i_seekpoint;
1803         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1804         p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1805     }
1806     if( p_access->info.i_update & INPUT_UPDATE_META )
1807     {
1808         /* TODO maybe multi - access ? */
1809         vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1810         access2_Control( p_input->input.p_access,ACCESS_GET_META, p_meta );
1811         UpdateMeta( p_input );
1812         var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1813         p_access->info.i_update &= ~INPUT_UPDATE_META;
1814     }
1815
1816     p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1817
1818     /* Hmmm only works with master input */
1819     if( p_input->input.p_access == p_access )
1820     {
1821         int i_title_end = p_input->input.i_title_end -
1822             p_input->input.i_title_offset;
1823         int i_seekpoint_end = p_input->input.i_seekpoint_end -
1824             p_input->input.i_seekpoint_offset;
1825
1826         if( i_title_end >= 0 && i_seekpoint_end >=0 )
1827         {
1828             if( p_access->info.i_title > i_title_end ||
1829                 ( p_access->info.i_title == i_title_end &&
1830                   p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1831         }
1832         else if( i_seekpoint_end >=0 )
1833         {
1834             if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1835         }
1836         else if( i_title_end >= 0 )
1837         {
1838             if( p_access->info.i_title > i_title_end ) return 0;
1839         }
1840     }
1841
1842     return 1;
1843 }
1844
1845 /*****************************************************************************
1846  * UpdateMeta:
1847  *****************************************************************************/
1848 static int  UpdateMeta( input_thread_t *p_input )
1849 {
1850     vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1851     if( !p_meta )
1852         return VLC_SUCCESS;
1853
1854     if( p_meta->psz_title && !p_input->input.p_item->b_fixed_name )
1855         input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1856
1857     /** \todo handle sout meta */
1858
1859     return VLC_SUCCESS;
1860 }
1861
1862 /*****************************************************************************
1863  * UpdateItemLength:
1864  *****************************************************************************/
1865 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
1866 {
1867     vlc_mutex_lock( &p_input->input.p_item->lock );
1868     p_input->input.p_item->i_duration = i_length;
1869     vlc_mutex_unlock( &p_input->input.p_item->lock );
1870
1871     if( !p_input->b_preparsing )
1872     {
1873         pl_Yield( p_input );
1874         var_SetInteger( pl_Get( p_input ), "item-change",
1875                         p_input->input.p_item->i_id );
1876         pl_Release( p_input )
1877     }
1878 }
1879
1880 /*****************************************************************************
1881  * InputSourceNew:
1882  *****************************************************************************/
1883 static input_source_t *InputSourceNew( input_thread_t *p_input )
1884 {
1885     input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1886
1887     if( !in )
1888     {
1889         msg_Err( p_input, "out of memory for new input source" );
1890         return NULL;
1891     }
1892
1893     in->p_item   = NULL;
1894     in->p_access = NULL;
1895     in->p_stream = NULL;
1896     in->p_demux  = NULL;
1897     in->b_title_demux = VLC_FALSE;
1898     in->i_title  = 0;
1899     in->title    = NULL;
1900     in->b_can_pace_control = VLC_TRUE;
1901     in->b_eof = VLC_FALSE;
1902     in->i_cr_average = 0;
1903
1904     return in;
1905 }
1906
1907 /*****************************************************************************
1908  * InputSourceInit:
1909  *****************************************************************************/
1910 static int InputSourceInit( input_thread_t *p_input,
1911                             input_source_t *in, char *psz_mrl,
1912                             char *psz_forced_demux )
1913 {
1914     char *psz_dup = strdup( psz_mrl );
1915     char *psz_access;
1916     char *psz_demux;
1917     char *psz_path;
1918     char *psz_tmp;
1919     char *psz;
1920     vlc_value_t val;
1921
1922     if( !in ) return VLC_EGENERIC;
1923
1924     /* Split uri */
1925     if( !p_input->b_preparsing )
1926     {
1927         MRLSplit( VLC_OBJECT(p_input), psz_dup,
1928                   &psz_access, &psz_demux, &psz_path );
1929
1930         msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1931                  psz_mrl, psz_access, psz_demux, psz_path );
1932
1933         /* Hack to allow udp://@:port syntax */
1934         if( !psz_access ||
1935             (strncmp( psz_access, "udp", 3 ) &&
1936              strncmp( psz_access, "rtp", 3 )) )
1937
1938         /* Find optional titles and seekpoints */
1939         MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1940                      &in->i_seekpoint_start, &in->i_seekpoint_end );
1941
1942         if( psz_forced_demux && *psz_forced_demux )
1943         {
1944             psz_demux = psz_forced_demux;
1945         }
1946         else if( !psz_demux || *psz_demux == '\0' )
1947         {
1948             /* special hack for forcing a demuxer with --demux=module
1949              * (and do nothing with a list) */
1950             char *psz_var_demux = var_GetString( p_input, "demux" );
1951
1952             if( *psz_var_demux != '\0' &&
1953                 !strchr(psz_var_demux, ',' ) &&
1954                 !strchr(psz_var_demux, ':' ) )
1955             {
1956                 psz_demux = psz_var_demux;
1957
1958                 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
1959             }
1960             else if( psz_var_demux )
1961             {
1962                 free( psz_var_demux );
1963             }
1964         }
1965
1966         /* Try access_demux if no demux given */
1967         if( *psz_demux == '\0' )
1968         {
1969             in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1970                                       NULL, p_input->p_es_out, VLC_FALSE );
1971         }
1972     }
1973     else
1974     {
1975         psz_path = psz_mrl;
1976         msg_Dbg( p_input, "trying to pre-parse %s",  psz_path );
1977         psz_demux = strdup( "" );
1978         psz_access = strdup( "file" );
1979     }
1980
1981     if( in->p_demux )
1982     {
1983         int64_t i_pts_delay;
1984
1985         /* Get infos from access_demux */
1986         demux2_Control( in->p_demux,
1987                         DEMUX_GET_PTS_DELAY, &i_pts_delay );
1988         p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1989
1990         in->b_title_demux = VLC_TRUE;
1991         if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1992                             &in->title, &in->i_title,
1993                             &in->i_title_offset, &in->i_seekpoint_offset ) )
1994         {
1995             in->i_title = 0;
1996             in->title   = NULL;
1997         }
1998         demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1999                         &in->b_can_pace_control );
2000         demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2001                         &in->b_can_pause );
2002
2003         /* FIXME todo
2004         demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2005                         &val.b_bool );
2006         */
2007     }
2008     else
2009     {
2010         int64_t i_pts_delay;
2011
2012         input_ChangeState( p_input, OPENING_S);
2013
2014         /* Now try a real access */
2015         in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2016                                     p_input->b_preparsing );
2017
2018         /* Access failed, URL encoded ? */
2019         if( in->p_access == NULL && strchr( psz_path, '%' ) )
2020         {
2021             DecodeUrl( psz_path );
2022
2023             msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2024                      psz_access, psz_demux, psz_path );
2025
2026             in->p_access = access2_New( p_input,
2027                                         psz_access, psz_demux, psz_path,
2028                                         p_input->b_preparsing );
2029         }
2030 #ifndef WIN32      /* Remove this gross hack from the win32 build as colons
2031                         * are forbidden in filenames on Win32. */
2032
2033         /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2034         if( in->p_access == NULL &&
2035             *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2036         {
2037             if( psz_dup ) free( psz_dup );
2038             psz_dup = strdup( psz_mrl );
2039             psz_access = "";
2040             if( psz_forced_demux && *psz_forced_demux )
2041             {
2042                 psz_demux = psz_forced_demux;
2043             }
2044             else psz_demux = "";
2045             psz_path = psz_dup;
2046
2047             in->p_access = access2_New( p_input,
2048                                         psz_access, psz_demux, psz_path,
2049                                         p_input->b_preparsing );
2050         }
2051 #endif
2052
2053         if( in->p_access == NULL )
2054         {
2055             msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2056             intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2057                             _("Your input can't be opened"),
2058                             _("VLC is unable to open the MRL '%s'."
2059                             " Check the log for details."), psz_mrl );
2060             goto error;
2061         }
2062
2063         /* */
2064         psz_tmp = psz = var_GetString( p_input, "access-filter" );
2065         while( psz && *psz )
2066         {
2067             access_t *p_access = in->p_access;
2068             char *end = strchr( psz, ':' );
2069
2070             if( end )
2071                 *end++ = '\0';
2072
2073             in->p_access = access2_FilterNew( in->p_access, psz );
2074             if( in->p_access == NULL )
2075             {
2076                 in->p_access = p_access;
2077                 msg_Warn( p_input, "failed to insert access filter %s",
2078                           psz );
2079             }
2080
2081             psz = end;
2082         }
2083         if( psz_tmp ) free( psz_tmp );
2084
2085         /* Get infos from access */
2086         if( !p_input->b_preparsing )
2087         {
2088             access2_Control( in->p_access,
2089                              ACCESS_GET_PTS_DELAY, &i_pts_delay );
2090             p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2091
2092             in->b_title_demux = VLC_FALSE;
2093             if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2094                                  &in->title, &in->i_title,
2095                                 &in->i_title_offset, &in->i_seekpoint_offset ) )
2096
2097             {
2098                 in->i_title = 0;
2099                 in->title   = NULL;
2100             }
2101             access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2102                              &in->b_can_pace_control );
2103             access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2104                              &in->b_can_pause );
2105             access2_Control( in->p_access, ACCESS_CAN_SEEK,
2106                              &val.b_bool );
2107             var_Set( p_input, "seekable", val );
2108         }
2109
2110         input_ChangeState( p_input, BUFFERING_S);
2111
2112         /* Create the stream_t */
2113         in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2114         if( in->p_stream == NULL )
2115         {
2116             msg_Warn( p_input, "cannot create a stream_t from access" );
2117             goto error;
2118         }
2119
2120         /* Open a demuxer */
2121         if( *psz_demux == '\0' && *in->p_access->psz_demux )
2122         {
2123             psz_demux = in->p_access->psz_demux;
2124         }
2125         in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2126                                   in->p_stream, p_input->p_es_out,
2127                                   p_input->b_preparsing );
2128         if( in->p_demux == NULL )
2129         {
2130             msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2131                      psz_access, psz_demux, psz_path );
2132             intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE, 
2133                             _("Can't recognize the input's format"),
2134                             _("The format of '%s' can't be detected. "
2135                             "Have a look the log for details."), psz_mrl );
2136             goto error;
2137         }
2138
2139         /* TODO get title from demux */
2140         if( !p_input->b_preparsing && in->i_title <= 0 )
2141         {
2142             if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2143                                 &in->title, &in->i_title,
2144                                 &in->i_title_offset, &in->i_seekpoint_offset ))
2145             {
2146                 in->i_title = 0;
2147                 in->title   = NULL;
2148             }
2149             else
2150             {
2151                 in->b_title_demux = VLC_TRUE;
2152             }
2153         }
2154     }
2155
2156     if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2157         in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2158
2159     if( psz_dup ) free( psz_dup );
2160     return VLC_SUCCESS;
2161
2162 error:
2163     if( in->p_demux )
2164         demux2_Delete( in->p_demux );
2165
2166     if( in->p_stream )
2167         stream_Delete( in->p_stream );
2168
2169     if( in->p_access )
2170         access2_Delete( in->p_access );
2171     if( psz_dup ) free( psz_dup );
2172
2173     return VLC_EGENERIC;
2174 }
2175
2176 /*****************************************************************************
2177  * InputSourceClean:
2178  *****************************************************************************/
2179 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2180 {
2181     if( in->p_demux )
2182         demux2_Delete( in->p_demux );
2183
2184     if( in->p_stream )
2185         stream_Delete( in->p_stream );
2186
2187     if( in->p_access )
2188         access2_Delete( in->p_access );
2189
2190     if( in->i_title > 0 )
2191     {
2192         int i;
2193         for( i = 0; i < in->i_title; i++ )
2194         {
2195             vlc_input_title_Delete( in->title[i] );
2196         }
2197         free( in->title );
2198     }
2199 }
2200
2201 static void SlaveDemux( input_thread_t *p_input )
2202 {
2203     int64_t i_time;
2204     int i;
2205  
2206     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2207     {
2208         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2209         return;
2210     }
2211
2212     for( i = 0; i < p_input->i_slave; i++ )
2213     {
2214         input_source_t *in = p_input->slave[i];
2215         int i_ret = 1;
2216
2217         if( in->b_eof )
2218             continue;
2219
2220         if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2221         {
2222             for( ;; )
2223             {
2224                 int64_t i_stime;
2225                 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2226                 {
2227                     msg_Err( p_input, "slave[%d] doesn't like "
2228                              "DEMUX_GET_TIME -> EOF", i );
2229                     i_ret = 0;
2230                     break;
2231                 }
2232
2233                 if( i_stime >= i_time )
2234                     break;
2235
2236                 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2237                     break;
2238             }
2239         }
2240         else
2241         {
2242             i_ret = in->p_demux->pf_demux( in->p_demux );
2243         }
2244
2245         if( i_ret <= 0 )
2246         {
2247             msg_Dbg( p_input, "slave %d EOF", i );
2248             in->b_eof = VLC_TRUE;
2249         }
2250     }
2251 }
2252
2253 static void SlaveSeek( input_thread_t *p_input )
2254 {
2255     int64_t i_time;
2256     int i;
2257
2258     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2259     {
2260         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2261         return;
2262     }
2263
2264     for( i = 0; i < p_input->i_slave; i++ )
2265     {
2266         input_source_t *in = p_input->slave[i];
2267
2268         if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2269         {
2270             msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2271             in->b_eof = VLC_TRUE;
2272         }
2273     }
2274 }
2275 /*****************************************************************************
2276  * InputMetaUser:
2277  *****************************************************************************/
2278 static void InputMetaUser( input_thread_t *p_input )
2279 {
2280     vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
2281     vlc_value_t val;
2282
2283     if( !p_meta ) return;
2284
2285     /* Get meta information from user */
2286 #define GET_META( field, s ) \
2287     var_Get( p_input, (s), &val );  \
2288     if( *val.psz_string ) { \
2289         if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2290         p_meta->psz_ ## field = strdup( val.psz_string ); \
2291     } \
2292     free( val.psz_string )
2293
2294     GET_META( title, "meta-title" );
2295     GET_META( author, "meta-author" );
2296     GET_META( artist, "meta-artist" );
2297     GET_META( genre, "meta-genre" );
2298     GET_META( copyright, "meta-copyright" );
2299     GET_META( description, "meta-description" );
2300     GET_META( date, "meta-date" );
2301     GET_META( url, "meta-url" );
2302 #undef GET_META
2303 }
2304
2305 /*****************************************************************************
2306  * DecodeUrl: decode a given encoded url
2307  *****************************************************************************/
2308 static void DecodeUrl( char *psz )
2309 {
2310     char *dup = strdup( psz );
2311     char *p = dup;
2312
2313     while( *p )
2314     {
2315         if( *p == '%' )
2316         {
2317             char val[3];
2318             p++;
2319             if( !*p )
2320             {
2321                 break;
2322             }
2323
2324             val[0] = *p++;
2325             val[1] = *p++;
2326             val[2] = '\0';
2327
2328             *psz++ = strtol( val, NULL, 16 );
2329         }
2330         else if( *p == '+' )
2331         {
2332             *psz++ = ' ';
2333             p++;
2334         }
2335         else
2336         {
2337             *psz++ = *p++;
2338         }
2339     }
2340     if( psz ) *psz++  ='\0';
2341     if( dup ) free( dup );
2342 }
2343
2344 /*****************************************************************************
2345  * MRLSplit: parse the access, demux and url part of the
2346  *           Media Resource Locator.
2347  *****************************************************************************/
2348 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2349                char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2350 {
2351     char *psz_access = NULL;
2352     char *psz_demux  = NULL;
2353     char *psz_path   = NULL;
2354     char *psz, *psz_check;
2355
2356     psz = strchr( psz_dup, ':' );
2357
2358     /* '@' not allowed in access/demux part */
2359     psz_check = strchr( psz_dup, '@' );
2360     if( psz_check && psz_check < psz ) psz = 0;
2361
2362 #if defined( WIN32 ) || defined( UNDER_CE )
2363     if( psz - psz_dup == 1 )
2364     {
2365         msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2366         psz_path = psz_dup;
2367     }
2368     else
2369 #endif
2370
2371     if( psz )
2372     {
2373         *psz++ = '\0';
2374         if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2375
2376         psz_path = psz;
2377
2378         psz = strchr( psz_dup, '/' );
2379         if( psz )
2380         {
2381             *psz++ = '\0';
2382             psz_demux = psz;
2383         }
2384
2385         psz_access = psz_dup;
2386     }
2387     else
2388     {
2389         psz_path = psz_dup;
2390     }
2391
2392     if( !psz_access ) *ppsz_access = "";
2393     else *ppsz_access = psz_access;
2394
2395     if( !psz_demux ) *ppsz_demux = "";
2396     else *ppsz_demux = psz_demux;
2397
2398     if( !psz_path ) *ppsz_path = "";
2399     else *ppsz_path = psz_path;
2400 }
2401
2402 /*****************************************************************************
2403  * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2404  *
2405  * Syntax:
2406  * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2407  *****************************************************************************/
2408 static void MRLSections( input_thread_t *p_input, char *psz_source,
2409                          int *pi_title_start, int *pi_title_end,
2410                          int *pi_chapter_start, int *pi_chapter_end )
2411 {
2412     char *psz, *psz_end, *psz_next, *psz_check;
2413
2414     *pi_title_start = *pi_title_end = -1;
2415     *pi_chapter_start = *pi_chapter_end = -1;
2416
2417     /* Start by parsing titles and chapters */
2418     if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2419
2420     /* Check we are really dealing with a title/chapter section */
2421     psz_check = psz + 1;
2422     if( !*psz_check ) return;
2423     if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2424     if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2425     if( *psz_check == ':' && ++psz_check )
2426         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2427     if( *psz_check != '-' && *psz_check ) return;
2428     if( *psz_check == '-' && ++psz_check )
2429         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2430     if( *psz_check != ':' && *psz_check ) return;
2431     if( *psz_check == ':' && ++psz_check )
2432         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2433     if( *psz_check ) return;
2434
2435     /* Separate start and end */
2436     *psz++ = 0;
2437     if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2438
2439     /* Look for the start title */
2440     *pi_title_start = strtol( psz, &psz_next, 0 );
2441     if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2442     *pi_title_end = *pi_title_start;
2443     psz = psz_next;
2444
2445     /* Look for the start chapter */
2446     if( *psz ) psz++;
2447     *pi_chapter_start = strtol( psz, &psz_next, 0 );
2448     if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2449     *pi_chapter_end = *pi_chapter_start;
2450
2451     if( psz_end )
2452     {
2453         /* Look for the end title */
2454         *pi_title_end = strtol( psz_end, &psz_next, 0 );
2455         if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2456         psz_end = psz_next;
2457
2458         /* Look for the end chapter */
2459         if( *psz_end ) psz_end++;
2460         *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2461         if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2462     }
2463
2464     msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2465              psz_source, *pi_title_start, *pi_chapter_start,
2466              *pi_title_end, *pi_chapter_end );
2467 }
2468
2469 /*****************************************************************************
2470  * input_AddSubtitles: add a subtitles file and enable it
2471  *****************************************************************************/
2472 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2473                                vlc_bool_t b_check_extension )
2474 {
2475     input_source_t *sub;
2476     vlc_value_t count;
2477     vlc_value_t list;
2478     char *psz_path, *psz_extension;
2479
2480     if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2481     {
2482         return VLC_FALSE;
2483     }
2484
2485     /* if we are provided a subtitle.sub file,
2486      * see if we don't have a subtitle.idx and use it instead */
2487     psz_path = strdup( psz_subtitle );
2488     if( psz_path )
2489     {
2490         psz_extension = strrchr( psz_path, '.');
2491         if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2492         {
2493             FILE *f;
2494
2495             strcpy( psz_extension, ".idx" );
2496             /* FIXME: a portable wrapper for stat() or access() would be more suited */
2497             if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2498             {
2499                 fclose( f );
2500                 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2501                          psz_path, psz_subtitle );
2502                 strcpy( psz_subtitle, psz_path );
2503             }
2504         }
2505         free( psz_path );
2506     }
2507
2508     var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2509
2510     sub = InputSourceNew( p_input );
2511     if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2512     {
2513         TAB_APPEND( p_input->i_slave, p_input->slave, sub );
2514
2515         /* Select the ES */
2516         if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2517         {
2518             if( count.i_int == 0 )
2519                 count.i_int++;
2520             /* if it was first one, there is disable too */
2521
2522             if( count.i_int < list.p_list->i_count )
2523             {
2524                 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2525                                    &list.p_list->p_values[count.i_int] );
2526             }
2527             var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2528         }
2529     }
2530
2531     return VLC_TRUE;
2532 }
2533
2534 #ifndef MAX_PATH
2535 #   define MAX_PATH 250
2536 #endif
2537 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item )
2538 {
2539     char *psz_artist;
2540     char *psz_album;
2541     char *psz_type;
2542     char *psz_filename;
2543     int i_status = VLC_EGENERIC;
2544     int i_ret;
2545     struct stat a;
2546
2547     if( !p_item->p_meta
2548         || !p_item->p_meta->psz_arturl
2549         || !*p_item->p_meta->psz_arturl )
2550     {
2551         return VLC_EGENERIC;
2552     }
2553     if( !strncmp( p_item->p_meta->psz_arturl, "file", 4 ) )
2554     {
2555         return VLC_SUCCESS;
2556     }
2557
2558     psz_artist = p_item->p_meta->psz_artist;
2559     psz_album = p_item->p_meta->psz_album;
2560     psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
2561     psz_filename = (char *)malloc( MAX_PATH );
2562
2563
2564     snprintf( psz_filename, MAX_PATH,
2565               "file://%s/" CONFIG_DIR "/art/%s/%s/art%s",
2566               p_parent->p_libvlc->psz_homedir,
2567               psz_artist, psz_album, psz_type );
2568     msg_Dbg( p_parent, "Saving album art to %s", psz_filename );
2569
2570     /* Check if file exists */
2571     i_ret = utf8_stat( psz_filename+7, &a );
2572     if( i_ret == 0 )
2573     {
2574         msg_Dbg( p_parent, "Album art %s already exists", psz_filename );
2575     }
2576     else
2577     {
2578         //if( i_ret == -1 && errno == ENOTDIR )
2579         {
2580             /* GRUIKKKKKKKKKK (make sure that all the directories exist) */
2581             char *psz_dir = malloc( MAX_PATH );
2582             snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR,
2583                       p_parent->p_libvlc->psz_homedir );
2584             utf8_mkdir( psz_dir );
2585             snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR "/art",
2586                       p_parent->p_libvlc->psz_homedir );
2587             utf8_mkdir( psz_dir );
2588             snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR "/art/%s",
2589                       p_parent->p_libvlc->psz_homedir, psz_artist );
2590             utf8_mkdir( psz_dir );
2591             snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR "/art/%s/%s",
2592                       p_parent->p_libvlc->psz_homedir,
2593                       psz_artist, psz_album );
2594             utf8_mkdir( psz_dir );
2595             free( psz_dir );
2596         }
2597
2598         stream_t *p_stream = stream_UrlNew( p_parent,
2599                                             p_item->p_meta->psz_arturl );
2600
2601         if( p_stream )
2602         {
2603             void *p_buffer = malloc( 1<<16 );
2604             long int l_read;
2605             FILE *p_file = utf8_fopen( psz_filename+7, "w" );
2606             while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
2607             {
2608                 fwrite( p_buffer, l_read, 1, p_file );
2609             }
2610             free( p_buffer );
2611             fclose( p_file );
2612             stream_Delete( p_stream );
2613             msg_Dbg( p_parent, "Album art saved to %s\n", psz_filename );
2614             free( p_item->p_meta->psz_arturl );
2615             p_item->p_meta->psz_arturl = strdup( psz_filename );
2616             i_status = VLC_SUCCESS;
2617         }
2618     }
2619     free( psz_filename );
2620
2621     return i_status;
2622 }