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