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