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