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