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