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