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