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