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