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