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