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