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