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