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