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