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