]> git.sesse.net Git - vlc/blob - src/input/input.c
a3692dc93eae7300c9951c82590f38957cbdf3de
[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     if( InputSourceInit( p_input, &p_input->input,
730                          p_input->input.p_item->psz_uri, NULL, b_quick ) )
731     {
732         goto error;
733     }
734
735     /* Create global title (from master) */
736     if( !b_quick )
737     {
738         p_input->i_title = p_input->input.i_title;
739         p_input->title   = p_input->input.title;
740         p_input->i_title_offset = p_input->input.i_title_offset;
741         p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
742         if( p_input->i_title > 0 )
743         {
744             /* Setup variables */
745             input_ControlVarNavigation( p_input );
746             input_ControlVarTitle( p_input, 0 );
747         }
748
749         /* Global flag */
750         p_input->b_can_pace_control = p_input->input.b_can_pace_control;
751         p_input->b_can_pause        = p_input->input.b_can_pause;
752
753         /* Fix pts delay */
754         if( p_input->i_pts_delay < 0 )
755             p_input->i_pts_delay = 0;
756
757         /* If the desynchronisation requested by the user is < 0, we need to
758          * cache more data. */
759         var_Get( p_input, "audio-desync", &val );
760         if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
761
762         /* Update cr_average depending on the caching */
763         p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
764         p_input->input.i_cr_average /= 10;
765         if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
766     }
767
768     /* Load master infos */
769     /* Init length */
770     if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
771                          &val.i_time ) && val.i_time > 0 )
772     {
773         var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
774         UpdateItemLength( p_input, val.i_time, b_quick );
775         p_input->input.p_item->i_duration = val.i_time;
776     }
777
778     /* Start title/chapter */
779     if( !b_quick )
780     {
781         val.i_int = p_input->input.i_title_start -
782                     p_input->input.i_title_offset;
783         if( val.i_int > 0 && val.i_int < p_input->input.i_title )
784             input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
785         val.i_int = p_input->input.i_seekpoint_start -
786                     p_input->input.i_seekpoint_offset;
787         if( val.i_int > 0 /* TODO: check upper boundary */ )
788             input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
789
790         /* Start time*/
791         /* Set start time */
792         p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
793                            I64C(1000000);
794         p_input->i_stop  = (int64_t)var_GetInteger( p_input, "stop-time" ) *
795                            I64C(1000000);
796
797         if( p_input->i_start > 0 )
798         {
799             if( p_input->i_start >= val.i_time )
800             {
801                 msg_Warn( p_input, "invalid start-time ignored" );
802             }
803             else
804             {
805                 vlc_value_t s;
806
807                 msg_Dbg( p_input, "starting at time: %ds",
808                                   (int)( p_input->i_start / I64C(1000000) ) );
809
810                 s.i_time = p_input->i_start;
811                 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
812             }
813         }
814         if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
815         {
816             msg_Warn( p_input, "invalid stop-time ignored" );
817             p_input->i_stop = 0;
818         }
819
820
821         /* Load subtitles */
822         /* Get fps and set it if not already set */
823         if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
824             f_fps > 1.0 )
825         {
826             float f_requested_fps;
827
828             var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
829             var_SetFloat( p_input, "sub-original-fps", f_fps );
830
831             f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
832             if( f_requested_fps != f_fps )
833             {
834                 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
835                                                 VLC_VAR_DOINHERIT );
836                 var_SetFloat( p_input, "sub-fps", f_requested_fps );
837             }
838         }
839
840         i_delay = var_CreateGetInteger( p_input, "sub-delay" );
841         if( i_delay != 0 )
842         {
843             var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
844         }
845
846
847         /* Look for and add subtitle files */
848         psz_subtitle = var_GetString( p_input, "sub-file" );
849         if( *psz_subtitle )
850         {
851             input_source_t *sub;
852             vlc_value_t count;
853             vlc_value_t list;
854
855             msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
856
857             var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
858
859             /* */
860             sub = InputSourceNew( p_input );
861             if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
862                                   VLC_FALSE ) )
863             {
864                 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
865
866                 /* Select the ES */
867                 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
868                                  NULL ) )
869                 {
870                     if( count.i_int == 0 )
871                         count.i_int++;
872                         /* if it was first one, there is disable too */
873
874                     if( count.i_int < list.p_list->i_count )
875                     {
876                         input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
877                                           &list.p_list->p_values[count.i_int] );
878                     }
879                     var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
880                                 NULL );
881                 }
882             }
883         }
884
885         var_Get( p_input, "sub-autodetect-file", &val );
886         if( val.b_bool )
887         {
888             char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
889             char **subs = subtitles_Detect( p_input, psz_autopath,
890                                             p_input->input.p_item->psz_uri );
891             input_source_t *sub;
892
893             for( i = 0; subs && subs[i]; i++ )
894             {
895                 if( strcmp( psz_subtitle, subs[i] ) )
896                 {
897                     sub = InputSourceNew( p_input );
898                     if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
899                                           VLC_FALSE ) )
900                     {
901                          TAB_APPEND( p_input->i_slave, p_input->slave, sub );
902                     }
903                 }
904                 free( subs[i] );
905             }
906             if( subs ) free( subs );
907             if( psz_autopath ) free( psz_autopath );
908         }
909         free( psz_subtitle );
910
911         /* Look for slave */
912         psz = var_GetString( p_input, "input-slave" );
913         if( *psz )
914         {
915             char *psz_delim;
916             input_source_t *slave;
917             while( psz && *psz )
918             {
919                 while( *psz == ' ' || *psz == '#' )
920                 {
921                     psz++;
922                 }
923                 if( ( psz_delim = strchr( psz, '#' ) ) )
924                 {
925                     *psz_delim++ = '\0';
926                 }
927                 if( *psz == 0 )
928                 {
929                     break;
930                 }
931
932                 msg_Dbg( p_input, "adding slave input '%s'", psz );
933                 slave = InputSourceNew( p_input );
934                 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
935                 {
936                     TAB_APPEND( p_input->i_slave, p_input->slave, slave );
937                 }
938                 psz = psz_delim;
939             }
940         }
941         if( psz ) free( psz );
942     }
943     else
944     {
945         p_input->i_start = 0;
946         p_input->i_start = 0;
947     }
948
949     /* Set up es_out */
950     if( !b_quick )
951     {
952         es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
953         i_es_out_mode = ES_OUT_MODE_AUTO;
954         val.p_list = NULL;
955         if( p_input->p_sout )
956         {
957             var_Get( p_input, "sout-all", &val );
958             if ( val.b_bool )
959             {
960                 i_es_out_mode = ES_OUT_MODE_ALL;
961                 val.p_list = NULL;
962             }
963             else
964             {
965                 var_Get( p_input, "programs", &val );
966                 if ( val.p_list && val.p_list->i_count )
967                 {
968                     i_es_out_mode = ES_OUT_MODE_PARTIAL;
969                     /* Note : we should remove the "program" callback. */
970                 }
971                 else
972                     var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
973                                 NULL );
974             }
975         }
976         es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
977
978         /* Inform the demuxer about waited group (needed only for DVB) */
979         if( i_es_out_mode == ES_OUT_MODE_ALL )
980         {
981             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
982         }
983         else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
984         {
985             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
986                             val.p_list );
987         }
988         else
989         {
990             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
991                            (int) var_GetInteger( p_input, "program" ), NULL );
992         }
993
994         if( p_input->p_sout )
995         {
996             if( p_input->p_sout->i_out_pace_nocontrol > 0 )
997             {
998                 p_input->b_out_pace_control = VLC_FALSE;
999             }
1000             else
1001             {
1002                 p_input->b_out_pace_control = VLC_TRUE;
1003             }
1004
1005             if( p_input->b_can_pace_control && p_input->b_out_pace_control )
1006             {
1007                 /* We don't want a high input priority here or we'll
1008                  * end-up sucking up all the CPU time */
1009                 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1010             }
1011
1012             msg_Dbg( p_input, "starting in %s mode",
1013                      p_input->b_out_pace_control ? "asynch" : "synch" );
1014         }
1015     }
1016
1017     /* Get meta data from users */
1018     p_meta_tmp = InputMetaUser( p_input );
1019
1020     /* Get meta data from master input */
1021     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
1022         p_meta = NULL;
1023
1024     /* Merge them */
1025     if( p_meta == NULL )
1026     {
1027         p_meta = p_meta_tmp;
1028     }
1029     else if( p_meta_tmp )
1030     {
1031         vlc_meta_Merge( p_meta, p_meta_tmp );
1032         vlc_meta_Delete( p_meta_tmp );
1033     }
1034
1035     /* Access_file does not give any meta, and there are no slave */
1036     if( !b_quick )
1037     {
1038         if( !p_input->input.p_access ||
1039             access2_Control( p_input->input.p_access, ACCESS_GET_META,
1040                              &p_meta_tmp))
1041             p_meta_tmp = NULL;
1042
1043         if( p_meta == NULL )
1044         {
1045             p_meta = p_meta_tmp;
1046         }
1047         else if( p_meta_tmp )
1048         {
1049             vlc_meta_Merge( p_meta, p_meta_tmp );
1050             vlc_meta_Delete( p_meta_tmp );
1051         }
1052
1053         /* Get meta data from slave input */
1054         for( i = 0; i < p_input->i_slave; i++ )
1055         {
1056             vlc_meta_t *p_meta_slave;
1057
1058             if( !demux2_Control( p_input->slave[i]->p_demux,
1059                                  DEMUX_GET_META, &p_meta_slave ) )
1060             {
1061                 if( p_meta == NULL )
1062                 {
1063                     p_meta = p_meta_slave;
1064                 }
1065                 else if( p_meta_slave )
1066                 {
1067                     vlc_meta_Merge( p_meta, p_meta_slave );
1068                     vlc_meta_Delete( p_meta_slave );
1069                 }
1070             }
1071
1072             if( p_input->slave[i]->p_access &&
1073                 !access2_Control( p_input->slave[i]->p_access,
1074                                   ACCESS_GET_META, &p_meta_slave ) )
1075             {
1076                 if( p_meta == NULL )
1077                 {
1078                     p_meta = p_meta_slave;
1079                 }
1080                 else if( p_meta_slave )
1081                 {
1082                     vlc_meta_Merge( p_meta, p_meta_slave );
1083                     vlc_meta_Delete( p_meta_slave );
1084                 }
1085             }
1086         }
1087     }
1088
1089     p_input->p_meta = p_meta;
1090     UpdateMeta( p_input, b_quick );
1091
1092     if( !b_quick )
1093     {
1094         msg_Dbg( p_input, "`%s' successfully opened",
1095                  p_input->input.p_item->psz_uri );
1096
1097     }
1098
1099     /* initialization is complete */
1100     p_input->i_state = PLAYING_S;
1101
1102     val.i_int = PLAYING_S;
1103     var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1104
1105     return VLC_SUCCESS;
1106
1107 error:
1108     if( p_input->p_es_out )
1109         input_EsOutDelete( p_input->p_es_out );
1110
1111     if( p_input->p_sout )
1112         sout_DeleteInstance( p_input->p_sout );
1113
1114     /* Mark them deleted */
1115     p_input->input.p_demux = NULL;
1116     p_input->input.p_stream = NULL;
1117     p_input->input.p_access = NULL;
1118     p_input->p_es_out = NULL;
1119     p_input->p_sout = NULL;
1120
1121     return VLC_EGENERIC;
1122 }
1123
1124 /*****************************************************************************
1125  * Error: RunThread() error loop
1126  *****************************************************************************
1127  * This function is called when an error occurred during thread main's loop.
1128  *****************************************************************************/
1129 static void Error( input_thread_t *p_input )
1130 {
1131     while( !p_input->b_die )
1132     {
1133         /* Sleep a while */
1134         msleep( INPUT_IDLE_SLEEP );
1135     }
1136 }
1137
1138 /*****************************************************************************
1139  * End: end the input thread
1140  *****************************************************************************/
1141 static void End( input_thread_t * p_input )
1142 {
1143     vlc_value_t val;
1144     int i;
1145
1146     msg_Dbg( p_input, "closing input" );
1147
1148     /* We are at the end */
1149     p_input->i_state = END_S;
1150
1151     val.i_int = END_S;
1152     var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1153
1154     /* Clean control variables */
1155     input_ControlVarClean( p_input );
1156
1157     /* Clean up master */
1158     InputSourceClean( p_input, &p_input->input );
1159
1160     /* Delete slave */
1161     for( i = 0; i < p_input->i_slave; i++ )
1162     {
1163         InputSourceClean( p_input, p_input->slave[i] );
1164         free( p_input->slave[i] );
1165     }
1166     if( p_input->slave ) free( p_input->slave );
1167
1168     /* Unload all modules */
1169     if( p_input->p_es_out )
1170         input_EsOutDelete( p_input->p_es_out );
1171
1172     /* Close optional stream output instance */
1173     if( p_input->p_sout )
1174     {
1175         vlc_object_t *p_pl =
1176             vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1177         vlc_value_t keep;
1178
1179         if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1180         {
1181             /* attach sout to the playlist */
1182             msg_Dbg( p_input, "keeping sout" );
1183             vlc_object_detach( p_input->p_sout );
1184             vlc_object_attach( p_input->p_sout, p_pl );
1185         }
1186         else
1187         {
1188             msg_Dbg( p_input, "destroying sout" );
1189             sout_DeleteInstance( p_input->p_sout );
1190         }
1191         if( p_pl )
1192             vlc_object_release( p_pl );
1193     }
1194
1195     /* Delete meta */
1196     if( p_input->p_meta )
1197         vlc_meta_Delete( p_input->p_meta );
1198
1199     /* Tell we're dead */
1200     p_input->b_dead = VLC_TRUE;
1201 }
1202
1203 /*****************************************************************************
1204  * Control
1205  *****************************************************************************/
1206 static inline int ControlPopNoLock( input_thread_t *p_input,
1207                                     int *pi_type, vlc_value_t *p_val )
1208 {
1209     if( p_input->i_control <= 0 )
1210     {
1211         return VLC_EGENERIC;
1212     }
1213
1214     *pi_type = p_input->control[0].i_type;
1215     *p_val   = p_input->control[0].val;
1216
1217     p_input->i_control--;
1218     if( p_input->i_control > 0 )
1219     {
1220         int i;
1221
1222         for( i = 0; i < p_input->i_control; i++ )
1223         {
1224             p_input->control[i].i_type = p_input->control[i+1].i_type;
1225             p_input->control[i].val    = p_input->control[i+1].val;
1226         }
1227     }
1228
1229     return VLC_SUCCESS;
1230 }
1231
1232 static void ControlReduce( input_thread_t *p_input )
1233 {
1234     int i;
1235     for( i = 1; i < p_input->i_control; i++ )
1236     {
1237         const int i_lt = p_input->control[i-1].i_type;
1238         const int i_ct = p_input->control[i].i_type;
1239
1240         /* XXX We can't merge INPUT_CONTROL_SET_ES */
1241 /*        msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1242                  i_lt, i_ct );
1243 */
1244         if( i_lt == i_ct &&
1245             ( i_ct == INPUT_CONTROL_SET_STATE ||
1246               i_ct == INPUT_CONTROL_SET_RATE ||
1247               i_ct == INPUT_CONTROL_SET_POSITION ||
1248               i_ct == INPUT_CONTROL_SET_TIME ||
1249               i_ct == INPUT_CONTROL_SET_PROGRAM ||
1250               i_ct == INPUT_CONTROL_SET_TITLE ||
1251               i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1252               i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1253         {
1254             int j;
1255 //            msg_Dbg( p_input, "merged at %d", i );
1256             /* Remove the i-1 */
1257             for( j = i; j <  p_input->i_control; j++ )
1258                 p_input->control[j-1] = p_input->control[j];
1259             p_input->i_control--;
1260         }
1261         else
1262         {
1263             /* TODO but that's not that important
1264                 - merge SET_X with SET_X_CMD
1265                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1266                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1267                 - ?
1268                 */
1269         }
1270     }
1271 }
1272
1273 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1274                            vlc_value_t val )
1275 {
1276     vlc_bool_t b_force_update = VLC_FALSE;
1277
1278     if( !p_input ) return b_force_update;
1279
1280     switch( i_type )
1281     {
1282         case INPUT_CONTROL_SET_DIE:
1283             msg_Dbg( p_input, "control: stopping input" );
1284             /* Mark all submodules to die */
1285             if( p_input->input.p_access )
1286                 p_input->input.p_access->b_die = VLC_TRUE;
1287             if( p_input->input.p_stream )
1288                 p_input->input.p_stream->b_die = VLC_TRUE;
1289             p_input->input.p_demux->b_die = VLC_TRUE;
1290
1291             p_input->b_die = VLC_TRUE;
1292             break;
1293
1294         case INPUT_CONTROL_SET_POSITION:
1295         case INPUT_CONTROL_SET_POSITION_OFFSET:
1296         {
1297             double f_pos;
1298             if( i_type == INPUT_CONTROL_SET_POSITION )
1299             {
1300                 f_pos = val.f_float;
1301             }
1302             else
1303             {
1304                 /* Should not fail */
1305                 demux2_Control( p_input->input.p_demux,
1306                                 DEMUX_GET_POSITION, &f_pos );
1307                 f_pos += val.f_float;
1308             }
1309             if( f_pos < 0.0 ) f_pos = 0.0;
1310             if( f_pos > 1.0 ) f_pos = 1.0;
1311             /* Reset the decoders states and clock synch (before calling the demuxer */
1312             es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1313             input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1314             if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1315                                 f_pos ) )
1316             {
1317                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1318                          "%2.1f%% failed", f_pos * 100 );
1319             }
1320             else
1321             {
1322                 if( p_input->i_slave > 0 )
1323                     SlaveSeek( p_input );
1324
1325                 b_force_update = VLC_TRUE;
1326             }
1327             break;
1328         }
1329
1330         case INPUT_CONTROL_SET_TIME:
1331         case INPUT_CONTROL_SET_TIME_OFFSET:
1332         {
1333             int64_t i_time;
1334             int i_ret;
1335
1336             if( i_type == INPUT_CONTROL_SET_TIME )
1337             {
1338                 i_time = val.i_time;
1339             }
1340             else
1341             {
1342                 /* Should not fail */
1343                 demux2_Control( p_input->input.p_demux,
1344                                 DEMUX_GET_TIME, &i_time );
1345                 i_time += val.i_time;
1346             }
1347             if( i_time < 0 ) i_time = 0;
1348
1349             /* Reset the decoders states and clock synch (before calling the demuxer */
1350             es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1351             input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1352
1353             i_ret = demux2_Control( p_input->input.p_demux,
1354                                     DEMUX_SET_TIME, i_time );
1355             if( i_ret )
1356             {
1357                 int64_t i_length;
1358
1359                 /* Emulate it with a SET_POS */
1360                 demux2_Control( p_input->input.p_demux,
1361                                 DEMUX_GET_LENGTH, &i_length );
1362                 if( i_length > 0 )
1363                 {
1364                     double f_pos = (double)i_time / (double)i_length;
1365                     i_ret = demux2_Control( p_input->input.p_demux,
1366                                             DEMUX_SET_POSITION, f_pos );
1367                 }
1368             }
1369             if( i_ret )
1370             {
1371                 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1372                          " failed or not possible", i_time );
1373             }
1374             else
1375             {
1376                 if( p_input->i_slave > 0 )
1377                     SlaveSeek( p_input );
1378
1379                 b_force_update = VLC_TRUE;
1380             }
1381             break;
1382         }
1383
1384         case INPUT_CONTROL_SET_STATE:
1385             if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1386                 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1387             {
1388                 int i_ret;
1389                 if( p_input->input.p_access )
1390                     i_ret = access2_Control( p_input->input.p_access,
1391                                              ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1392                 else
1393                     i_ret = demux2_Control( p_input->input.p_demux,
1394                                             DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1395
1396                 if( i_ret )
1397                 {
1398                     /* FIXME What to do ? */
1399                     msg_Warn( p_input, "cannot unset pause -> EOF" );
1400                     vlc_mutex_unlock( &p_input->lock_control );
1401                     input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1402                     vlc_mutex_lock( &p_input->lock_control );
1403                 }
1404
1405                 b_force_update = VLC_TRUE;
1406
1407                 /* Switch to play */
1408                 p_input->i_state = PLAYING_S;
1409                 val.i_int = PLAYING_S;
1410                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1411
1412                 /* Reset clock */
1413                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1414                 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1415             }
1416             else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1417                      p_input->b_can_pause )
1418             {
1419                 int i_ret;
1420                 if( p_input->input.p_access )
1421                     i_ret = access2_Control( p_input->input.p_access,
1422                                              ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1423                 else
1424                     i_ret = demux2_Control( p_input->input.p_demux,
1425                                             DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1426
1427                 b_force_update = VLC_TRUE;
1428
1429                 if( i_ret )
1430                 {
1431                     msg_Warn( p_input, "cannot set pause state" );
1432                     val.i_int = p_input->i_state;
1433                 }
1434                 else
1435                 {
1436                     val.i_int = PAUSE_S;
1437                 }
1438
1439                 /* Switch to new state */
1440                 p_input->i_state = val.i_int;
1441                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1442             }
1443             else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1444             {
1445                 b_force_update = VLC_TRUE;
1446
1447                 /* Correct "state" value */
1448                 val.i_int = p_input->i_state;
1449                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1450             }
1451             else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1452             {
1453                 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1454             }
1455             break;
1456
1457         case INPUT_CONTROL_SET_RATE:
1458         case INPUT_CONTROL_SET_RATE_SLOWER:
1459         case INPUT_CONTROL_SET_RATE_FASTER:
1460         {
1461             int i_rate;
1462
1463             if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1464                 i_rate = p_input->i_rate * 2;
1465             else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1466                 i_rate = p_input->i_rate / 2;
1467             else
1468                 i_rate = val.i_int;
1469
1470             if( i_rate < INPUT_RATE_MIN )
1471             {
1472                 msg_Dbg( p_input, "cannot set rate faster" );
1473                 i_rate = INPUT_RATE_MIN;
1474             }
1475             else if( i_rate > INPUT_RATE_MAX )
1476             {
1477                 msg_Dbg( p_input, "cannot set rate slower" );
1478                 i_rate = INPUT_RATE_MAX;
1479             }
1480             if( i_rate != INPUT_RATE_DEFAULT &&
1481                 ( !p_input->b_can_pace_control ||
1482                   ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1483             {
1484                 msg_Dbg( p_input, "cannot change rate" );
1485                 i_rate = INPUT_RATE_DEFAULT;
1486             }
1487             if( i_rate != p_input->i_rate )
1488             {
1489                 p_input->i_rate  = i_rate;
1490                 val.i_int = i_rate;
1491                 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1492
1493                 /* We haven't send data to decoder when rate != default */
1494                 if( i_rate == INPUT_RATE_DEFAULT )
1495                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1496
1497                 /* Reset clock */
1498                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1499
1500                 b_force_update = VLC_TRUE;
1501             }
1502             break;
1503         }
1504
1505         case INPUT_CONTROL_SET_PROGRAM:
1506             /* No need to force update, es_out does it if needed */
1507             es_out_Control( p_input->p_es_out,
1508                             ES_OUT_SET_GROUP, val.i_int );
1509
1510             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1511                             NULL );
1512             break;
1513
1514         case INPUT_CONTROL_SET_ES:
1515             /* No need to force update, es_out does it if needed */
1516             es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1517                             input_EsOutGetFromID( p_input->p_es_out,
1518                                                   val.i_int ) );
1519             break;
1520
1521         case INPUT_CONTROL_SET_AUDIO_DELAY:
1522             input_EsOutSetDelay( p_input->p_es_out,
1523                                  AUDIO_ES, val.i_time );
1524             var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1525             break;
1526
1527         case INPUT_CONTROL_SET_SPU_DELAY:
1528             input_EsOutSetDelay( p_input->p_es_out,
1529                                  SPU_ES, val.i_time );
1530             var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1531             break;
1532
1533         case INPUT_CONTROL_SET_TITLE:
1534         case INPUT_CONTROL_SET_TITLE_NEXT:
1535         case INPUT_CONTROL_SET_TITLE_PREV:
1536             if( p_input->input.b_title_demux &&
1537                 p_input->input.i_title > 0 )
1538             {
1539                 /* TODO */
1540                 /* FIXME handle demux title */
1541                 demux_t *p_demux = p_input->input.p_demux;
1542                 int i_title;
1543
1544                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1545                     i_title = p_demux->info.i_title - 1;
1546                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1547                     i_title = p_demux->info.i_title + 1;
1548                 else
1549                     i_title = val.i_int;
1550
1551                 if( i_title >= 0 && i_title < p_input->input.i_title )
1552                 {
1553                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1554                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1555
1556                     demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1557                     input_ControlVarTitle( p_input, i_title );
1558                 }
1559             }
1560             else if( p_input->input.i_title > 0 )
1561             {
1562                 access_t *p_access = p_input->input.p_access;
1563                 int i_title;
1564
1565                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1566                     i_title = p_access->info.i_title - 1;
1567                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1568                     i_title = p_access->info.i_title + 1;
1569                 else
1570                     i_title = val.i_int;
1571
1572                 if( i_title >= 0 && i_title < p_input->input.i_title )
1573                 {
1574                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1575                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1576
1577                     access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1578                     stream_AccessReset( p_input->input.p_stream );
1579                 }
1580             }
1581             break;
1582         case INPUT_CONTROL_SET_SEEKPOINT:
1583         case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1584         case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1585             if( p_input->input.b_title_demux &&
1586                 p_input->input.i_title > 0 )
1587             {
1588                 demux_t *p_demux = p_input->input.p_demux;
1589                 int i_seekpoint;
1590                 int64_t i_input_time;
1591                 int64_t i_seekpoint_time;
1592
1593                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1594                 {
1595                     i_seekpoint = p_demux->info.i_seekpoint;
1596                     i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1597                     if( i_seekpoint_time >= 0 &&
1598                          !demux2_Control( p_demux,
1599                                           DEMUX_GET_TIME, &i_input_time ) )
1600                     {
1601                         if ( i_input_time < i_seekpoint_time + 3000000 )
1602                             i_seekpoint--;
1603                     }
1604                     else
1605                         i_seekpoint--;
1606                 }
1607                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1608                     i_seekpoint = p_demux->info.i_seekpoint + 1;
1609                 else
1610                     i_seekpoint = val.i_int;
1611
1612                 if( i_seekpoint >= 0 && i_seekpoint <
1613                     p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1614                 {
1615                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1616                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1617
1618                     demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1619                 }
1620             }
1621             else if( p_input->input.i_title > 0 )
1622             {
1623                 demux_t *p_demux = p_input->input.p_demux;
1624                 access_t *p_access = p_input->input.p_access;
1625                 int i_seekpoint;
1626                 int64_t i_input_time;
1627                 int64_t i_seekpoint_time;
1628
1629                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1630                 {
1631                     i_seekpoint = p_access->info.i_seekpoint;
1632                     i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1633                     if( i_seekpoint_time >= 0 &&
1634                         demux2_Control( p_demux,
1635                                         DEMUX_GET_TIME, &i_input_time ) )
1636                     {
1637                         if ( i_input_time < i_seekpoint_time + 3000000 )
1638                             i_seekpoint--;
1639                     }
1640                     else
1641                         i_seekpoint--;
1642                 }
1643                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT ) 
1644                     i_seekpoint = p_access->info.i_seekpoint + 1;
1645                 else
1646                     i_seekpoint = val.i_int;
1647
1648                 if( i_seekpoint >= 0 && i_seekpoint <
1649                     p_input->input.title[p_access->info.i_title]->i_seekpoint )
1650                 {
1651                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1652                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1653
1654                     access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1655                                     i_seekpoint );
1656                     stream_AccessReset( p_input->input.p_stream );
1657                 }
1658             }
1659             break;
1660
1661         case INPUT_CONTROL_ADD_SLAVE:
1662             if( val.psz_string )
1663             {
1664                 input_source_t *slave = InputSourceNew( p_input );
1665
1666                 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1667                                       VLC_FALSE ) )
1668                 {
1669                     vlc_meta_t *p_meta_new = NULL;
1670                     vlc_meta_t *p_meta;
1671                     int64_t i_time;
1672
1673                     /* Add the slave */
1674                     msg_Dbg( p_input, "adding %s as slave on the fly",
1675                              val.psz_string );
1676
1677                     /* Set position */
1678                     if( demux2_Control( p_input->input.p_demux,
1679                                         DEMUX_GET_TIME, &i_time ) )
1680                     {
1681                         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1682                         InputSourceClean( p_input, slave );
1683                         free( slave );
1684                         break;
1685                     }
1686                     if( demux2_Control( slave->p_demux,
1687                                         DEMUX_SET_TIME, i_time ) )
1688                     {
1689                         msg_Err( p_input, "seek failed for new slave" );
1690                         InputSourceClean( p_input, slave );
1691                         free( slave );
1692                         break;
1693                     }
1694
1695
1696                     /* Get meta (access and demux) */
1697                     if( access2_Control( slave->p_access,
1698                                           ACCESS_GET_META, &p_meta_new ) )
1699                         p_meta_new = NULL;
1700                     if( !demux2_Control( slave->p_demux,
1701                                          DEMUX_GET_META, &p_meta ) )
1702                     {
1703                         if( p_meta_new )
1704                         {
1705                             vlc_meta_Merge( p_meta_new, p_meta );
1706                             vlc_meta_Delete( p_meta );
1707                         }
1708                         else
1709                         {
1710                             p_meta_new = p_meta;
1711                         }
1712                     }
1713                     /* Update meta */
1714                     if( p_meta_new )
1715                     {
1716                         if( p_input->p_meta )
1717                         {
1718                             vlc_meta_Merge( p_input->p_meta, p_meta_new );
1719                             vlc_meta_Delete( p_meta_new );
1720                         }
1721                         else
1722                         {
1723                             p_input->p_meta = p_meta_new;
1724                         }
1725                         UpdateMeta( p_input, VLC_FALSE );
1726                     }
1727
1728                     TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1729                 }
1730                 else
1731                 {
1732                     msg_Warn( p_input, "failed to add %s as slave",
1733                               val.psz_string );
1734                 }
1735
1736                 free( val.psz_string );
1737             }
1738             break;
1739
1740         case INPUT_CONTROL_SET_BOOKMARK:
1741         default:
1742             msg_Err( p_input, "not yet implemented" );
1743             break;
1744     }
1745
1746     return b_force_update;
1747 }
1748
1749 /*****************************************************************************
1750  * UpdateFromDemux:
1751  *****************************************************************************/
1752 static int UpdateFromDemux( input_thread_t *p_input )
1753 {
1754     demux_t *p_demux = p_input->input.p_demux;
1755     vlc_value_t v;
1756
1757     if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1758     {
1759         v.i_int = p_demux->info.i_title;
1760         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1761
1762         input_ControlVarTitle( p_input, p_demux->info.i_title );
1763
1764         p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1765     }
1766     if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1767     {
1768         v.i_int = p_demux->info.i_seekpoint;
1769         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1770
1771         p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1772     }
1773     p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1774
1775     /* Hmmm only works with master input */
1776     if( p_input->input.p_demux == p_demux )
1777     {
1778         int i_title_end = p_input->input.i_title_end -
1779             p_input->input.i_title_offset;
1780         int i_seekpoint_end = p_input->input.i_seekpoint_end -
1781             p_input->input.i_seekpoint_offset;
1782
1783         if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1784         {
1785             if( p_demux->info.i_title > i_title_end ||
1786                 ( p_demux->info.i_title == i_title_end &&
1787                   p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1788         }
1789         else if( i_seekpoint_end >=0 )
1790         {
1791             if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1792         }
1793         else if( i_title_end >= 0 )
1794         {
1795             if( p_demux->info.i_title > i_title_end ) return 0;
1796         }
1797     }
1798
1799     return 1;
1800 }
1801
1802 /*****************************************************************************
1803  * UpdateFromAccess:
1804  *****************************************************************************/
1805 static int UpdateFromAccess( input_thread_t *p_input )
1806 {
1807     access_t *p_access = p_input->input.p_access;
1808     vlc_value_t v;
1809
1810     if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1811     {
1812         v.i_int = p_access->info.i_title;
1813         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1814
1815         input_ControlVarTitle( p_input, p_access->info.i_title );
1816
1817         stream_AccessUpdate( p_input->input.p_stream );
1818
1819         p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1820     }
1821     if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1822     {
1823         v.i_int = p_access->info.i_seekpoint;
1824         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1825
1826         p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1827     }
1828     if( p_access->info.i_update & INPUT_UPDATE_META )
1829     {
1830         /* TODO maybe multi - access ? */
1831         vlc_meta_t *p_meta;
1832         if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1833         {
1834             if( p_input->p_meta )
1835             {
1836                 vlc_meta_Merge( p_input->p_meta, p_meta );
1837                 vlc_meta_Delete( p_meta );
1838             }
1839             else
1840             {
1841                 p_input->p_meta = p_meta;
1842             }
1843
1844             UpdateMeta( p_input, VLC_FALSE );
1845             var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1846         }
1847         p_access->info.i_update &= ~INPUT_UPDATE_META;
1848     }
1849
1850     p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1851
1852     /* Hmmm only works with master input */
1853     if( p_input->input.p_access == p_access )
1854     {
1855         int i_title_end = p_input->input.i_title_end -
1856             p_input->input.i_title_offset;
1857         int i_seekpoint_end = p_input->input.i_seekpoint_end -
1858             p_input->input.i_seekpoint_offset;
1859
1860         if( i_title_end >= 0 && i_seekpoint_end >=0 )
1861         {
1862             if( p_access->info.i_title > i_title_end ||
1863                 ( p_access->info.i_title == i_title_end &&
1864                   p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1865         }
1866         else if( i_seekpoint_end >=0 )
1867         {
1868             if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1869         }
1870         else if( i_title_end >= 0 )
1871         {
1872             if( p_access->info.i_title > i_title_end ) return 0;
1873         }
1874     }
1875
1876     return 1;
1877 }
1878
1879 /*****************************************************************************
1880  * UpdateMeta:
1881  *****************************************************************************/
1882 static int  UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1883 {
1884     vlc_meta_t *p_meta = p_input->p_meta;
1885     int i;
1886
1887     if( !p_meta || p_meta->i_meta == 0 )
1888         return VLC_SUCCESS;
1889
1890     if( !b_quick ) msg_Dbg( p_input, "meta information:" );
1891     for( i = 0; i < p_meta->i_meta; i++ )
1892     {
1893         if( !b_quick )
1894             msg_Dbg( p_input, "  - '%s' = '%s'",
1895                      _(p_meta->name[i]), p_meta->value[i] );
1896
1897         if( !strcmp(p_meta->name[i], _(VLC_META_TITLE)) && p_meta->value[i] &&
1898             !p_input->input.p_item->b_fixed_name )
1899             input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1900
1901         if( !strcmp( p_meta->name[i], _(VLC_META_AUTHOR) ) )
1902             input_Control( p_input, INPUT_ADD_INFO, _("General"),
1903                            _(VLC_META_AUTHOR), p_meta->value[i] );
1904
1905         input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT),
1906                       _(p_meta->name[i]), "%s", p_meta->value[i] );
1907     }
1908
1909     for( i = 0; i < p_meta->i_track; i++ )
1910     {
1911         vlc_meta_t *tk = p_meta->track[i];
1912         int j;
1913
1914         if( tk->i_meta > 0 )
1915         {
1916             char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1917
1918             msg_Dbg( p_input, "  - track[%d]:", i );
1919
1920             sprintf( psz_cat, "%s %d", _("Stream"), i );
1921             for( j = 0; j < tk->i_meta; j++ )
1922             {
1923                 msg_Dbg( p_input, "     - '%s' = '%s'", _(tk->name[j]),
1924                          tk->value[j] );
1925
1926                 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1927                                _(tk->name[j]), "%s", tk->value[j] );
1928             }
1929         }
1930     }
1931
1932     if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1933     {
1934         p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1935     }
1936
1937     return VLC_SUCCESS;
1938 }
1939
1940 /*****************************************************************************
1941  * UpdateItemLength:
1942  *****************************************************************************/
1943 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1944                               vlc_bool_t b_quick )
1945 {
1946     playlist_t *p_playlist;
1947     char psz_buffer[MSTRTIME_MAX_SIZE];
1948
1949     vlc_mutex_lock( &p_input->input.p_item->lock );
1950     p_input->input.p_item->i_duration = i_length;
1951     vlc_mutex_unlock( &p_input->input.p_item->lock );
1952
1953         p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1954                                                FIND_PARENT);
1955     if( p_playlist )
1956     {
1957         var_SetInteger( p_playlist, "item-change",
1958                         p_input->input.p_item->i_id );
1959         vlc_object_release( p_playlist );
1960     }
1961
1962     input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1963                    msecstotimestr( psz_buffer, i_length / 1000 ) );
1964 }
1965
1966 /*****************************************************************************
1967  * InputSourceNew:
1968  *****************************************************************************/
1969 static input_source_t *InputSourceNew( input_thread_t *p_input )
1970 {
1971     input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1972
1973     if( !in )
1974     {
1975         msg_Err( p_input, "out of memory for new input source" );
1976         return NULL;
1977     }
1978
1979     in->p_item   = NULL;
1980     in->p_access = NULL;
1981     in->p_stream = NULL;
1982     in->p_demux  = NULL;
1983     in->b_title_demux = VLC_FALSE;
1984     in->i_title  = 0;
1985     in->title    = NULL;
1986     in->b_can_pace_control = VLC_TRUE;
1987     in->b_eof = VLC_FALSE;
1988     in->i_cr_average = 0;
1989
1990     return in;
1991 }
1992
1993 /*****************************************************************************
1994  * InputSourceInit:
1995  *****************************************************************************/
1996 static int InputSourceInit( input_thread_t *p_input,
1997                             input_source_t *in, char *psz_mrl,
1998                             char *psz_forced_demux, vlc_bool_t b_quick )
1999 {
2000     char *psz_dup = strdup( psz_mrl );
2001     char *psz_access;
2002     char *psz_demux;
2003     char *psz_path;
2004     char *psz_tmp;
2005     char *psz;
2006     vlc_value_t val;
2007
2008     if( !in ) return VLC_EGENERIC;
2009
2010     /* Split uri */
2011     if( !b_quick )
2012     {
2013         MRLSplit( VLC_OBJECT(p_input), psz_dup,
2014                   &psz_access, &psz_demux, &psz_path );
2015
2016         msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2017                  psz_mrl, psz_access, psz_demux, psz_path );
2018
2019         /* Hack to allow udp://@:port syntax */
2020         if( !psz_access ||
2021             (strncmp( psz_access, "udp", 3 ) &&
2022              strncmp( psz_access, "rtp", 3 )) )
2023
2024         /* Find optional titles and seekpoints */
2025         MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2026                      &in->i_seekpoint_start, &in->i_seekpoint_end );
2027
2028         if( psz_forced_demux && *psz_forced_demux )
2029         {
2030             psz_demux = psz_forced_demux;
2031         }
2032         else if( !psz_demux || *psz_demux == '\0' )
2033         {
2034             /* special hack for forcing a demuxer with --demux=module
2035              * (and do nothing with a list) */
2036             char *psz_var_demux = var_GetString( p_input, "demux" );
2037
2038             if( *psz_var_demux != '\0' &&
2039                 !strchr(psz_var_demux, ',' ) &&
2040                 !strchr(psz_var_demux, ':' ) )
2041             {
2042                 psz_demux = psz_var_demux;
2043
2044                 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2045             }
2046             else if( psz_var_demux )
2047             {
2048                 free( psz_var_demux );
2049             }
2050         }
2051
2052         /* Try access_demux if no demux given */
2053         if( *psz_demux == '\0' )
2054         {
2055             in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2056                                       NULL, p_input->p_es_out, VLC_FALSE );
2057         }
2058     }
2059     else
2060     {
2061         psz_path = psz_mrl;
2062         msg_Dbg( p_input, "trying to pre-parse %s",  psz_path );
2063         psz_demux = strdup( "" );
2064         psz_access = strdup( "file" );
2065     }
2066
2067     if( in->p_demux )
2068     {
2069         int64_t i_pts_delay;
2070
2071         /* Get infos from access_demux */
2072         demux2_Control( in->p_demux,
2073                         DEMUX_GET_PTS_DELAY, &i_pts_delay );
2074         p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2075
2076         in->b_title_demux = VLC_TRUE;
2077         if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2078                             &in->title, &in->i_title,
2079                             &in->i_title_offset, &in->i_seekpoint_offset ) )
2080         {
2081             in->i_title = 0;
2082             in->title   = NULL;
2083         }
2084         demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2085                         &in->b_can_pace_control );
2086         demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2087                         &in->b_can_pause );
2088
2089         /* FIXME todo
2090         demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2091                         &val.b_bool );
2092         */
2093     }
2094     else
2095     {
2096         int64_t i_pts_delay;
2097
2098         /* Now try a real access */
2099         in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2100                                     b_quick );
2101
2102         /* Access failed, URL encoded ? */
2103         if( in->p_access == NULL && strchr( psz_path, '%' ) )
2104         {
2105             DecodeUrl( psz_path );
2106
2107             msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2108                      psz_access, psz_demux, psz_path );
2109
2110             in->p_access = access2_New( p_input,
2111                                         psz_access, psz_demux, psz_path,
2112                                         b_quick );
2113         }
2114 #ifndef WIN32      /* Remove this gross hack from the win32 build as colons
2115                         * are forbidden in filenames on Win32. */
2116
2117         /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2118         if( in->p_access == NULL &&
2119             *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2120         {
2121             if( psz_dup ) free( psz_dup );
2122             psz_dup = strdup( psz_mrl );
2123             psz_access = "";
2124             if( psz_forced_demux && *psz_forced_demux )
2125             {
2126                 psz_demux = psz_forced_demux;
2127             }
2128             else psz_demux = "";
2129             psz_path = psz_dup;
2130
2131             in->p_access = access2_New( p_input,
2132                                         psz_access, psz_demux, psz_path,
2133                                         b_quick );
2134         }
2135 #endif
2136
2137         if( in->p_access == NULL )
2138         {
2139             msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2140             intf_UserFatal( VLC_OBJECT( p_input),
2141                             _("Errors"),"Unable to open '%s'", psz_mrl );
2142             goto error;
2143         }
2144
2145         /* */
2146         psz_tmp = psz = var_GetString( p_input, "access-filter" );
2147         while( psz && *psz )
2148         {
2149             access_t *p_access = in->p_access;
2150             char *end = strchr( psz, ':' );
2151
2152             if( end )
2153                 *end++ = '\0';
2154
2155             in->p_access = access2_FilterNew( in->p_access, psz );
2156             if( in->p_access == NULL )
2157             {
2158                 in->p_access = p_access;
2159                 msg_Warn( p_input, "failed to insert access filter %s",
2160                           psz );
2161             }
2162
2163             psz = end;
2164         }
2165         if( psz_tmp ) free( psz_tmp );
2166
2167         /* Get infos from access */
2168         if( !b_quick )
2169         {
2170             access2_Control( in->p_access,
2171                              ACCESS_GET_PTS_DELAY, &i_pts_delay );
2172             p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2173
2174             in->b_title_demux = VLC_FALSE;
2175             if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2176                                  &in->title, &in->i_title,
2177                                 &in->i_title_offset, &in->i_seekpoint_offset ) )
2178
2179             {
2180                 in->i_title = 0;
2181                 in->title   = NULL;
2182             }
2183             access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2184                              &in->b_can_pace_control );
2185             access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2186                              &in->b_can_pause );
2187             access2_Control( in->p_access, ACCESS_CAN_SEEK,
2188                              &val.b_bool );
2189             var_Set( p_input, "seekable", val );
2190         }
2191
2192         /* Create the stream_t */
2193         in->p_stream = stream_AccessNew( in->p_access, b_quick );
2194         if( in->p_stream == NULL )
2195         {
2196             msg_Warn( p_input, "cannot create a stream_t from access" );
2197             goto error;
2198         }
2199
2200         /* Open a demuxer */
2201         if( *psz_demux == '\0' && *in->p_access->psz_demux )
2202         {
2203             psz_demux = in->p_access->psz_demux;
2204         }
2205         in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2206                                   in->p_stream, p_input->p_es_out, b_quick );
2207         if( in->p_demux == NULL )
2208         {
2209             msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2210                      psz_access, psz_demux, psz_path );
2211             intf_UserFatal( VLC_OBJECT( p_input), _("Errors"),
2212                             "Unrecognized format for '%s'", psz_mrl );
2213             goto error;
2214         }
2215
2216         /* TODO get title from demux */
2217         if( !b_quick && in->i_title <= 0 )
2218         {
2219             if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2220                                 &in->title, &in->i_title,
2221                                 &in->i_title_offset, &in->i_seekpoint_offset ))
2222             {
2223                 in->i_title = 0;
2224                 in->title   = NULL;
2225             }
2226             else
2227             {
2228                 in->b_title_demux = VLC_TRUE;
2229             }
2230         }
2231     }
2232
2233     if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2234         in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2235
2236     if( psz_dup ) free( psz_dup );
2237     return VLC_SUCCESS;
2238
2239 error:
2240     if( in->p_demux )
2241         demux2_Delete( in->p_demux );
2242
2243     if( in->p_stream )
2244         stream_Delete( in->p_stream );
2245
2246     if( in->p_access )
2247         access2_Delete( in->p_access );
2248     if( psz_dup ) free( psz_dup );
2249
2250     return VLC_EGENERIC;
2251 }
2252
2253 /*****************************************************************************
2254  * InputSourceClean:
2255  *****************************************************************************/
2256 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2257 {
2258     if( in->p_demux )
2259         demux2_Delete( in->p_demux );
2260
2261     if( in->p_stream )
2262         stream_Delete( in->p_stream );
2263
2264     if( in->p_access )
2265         access2_Delete( in->p_access );
2266
2267     if( in->i_title > 0 )
2268     {
2269         int i;
2270         for( i = 0; i < in->i_title; i++ )
2271         {
2272             vlc_input_title_Delete( in->title[i] );
2273         }
2274         free( in->title );
2275     }
2276 }
2277
2278 static void SlaveDemux( input_thread_t *p_input )
2279 {
2280     int64_t i_time;
2281     int i;
2282  
2283     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2284     {
2285         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2286         return;
2287     }
2288
2289     for( i = 0; i < p_input->i_slave; i++ )
2290     {
2291         input_source_t *in = p_input->slave[i];
2292         int i_ret = 1;
2293
2294         if( in->b_eof )
2295             continue;
2296
2297         if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2298         {
2299             for( ;; )
2300             {
2301                 int64_t i_stime;
2302                 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2303                 {
2304                     msg_Err( p_input, "slave[%d] doesn't like "
2305                              "DEMUX_GET_TIME -> EOF", i );
2306                     i_ret = 0;
2307                     break;
2308                 }
2309
2310                 if( i_stime >= i_time )
2311                     break;
2312
2313                 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2314                     break;
2315             }
2316         }
2317         else
2318         {
2319             i_ret = in->p_demux->pf_demux( in->p_demux );
2320         }
2321
2322         if( i_ret <= 0 )
2323         {
2324             msg_Dbg( p_input, "slave %d EOF", i );
2325             in->b_eof = VLC_TRUE;
2326         }
2327     }
2328 }
2329
2330 static void SlaveSeek( input_thread_t *p_input )
2331 {
2332     int64_t i_time;
2333     int i;
2334
2335     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2336     {
2337         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2338         return;
2339     }
2340
2341     for( i = 0; i < p_input->i_slave; i++ )
2342     {
2343         input_source_t *in = p_input->slave[i];
2344
2345         if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2346         {
2347             msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2348             in->b_eof = VLC_TRUE;
2349         }
2350     }
2351 }
2352 /*****************************************************************************
2353  * InputMetaUser:
2354  *****************************************************************************/
2355 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2356 {
2357     vlc_meta_t *p_meta;
2358     vlc_value_t val;
2359
2360     if( ( p_meta = vlc_meta_New() ) == NULL )
2361         return NULL;
2362
2363     /* Get meta information from user */
2364 #define GET_META( c, s ) \
2365     var_Get( p_input, (s), &val );  \
2366     if( *val.psz_string )       \
2367         vlc_meta_Add( p_meta, _(c), val.psz_string ); \
2368     free( val.psz_string )
2369
2370     GET_META( VLC_META_TITLE, "meta-title" );
2371     GET_META( VLC_META_AUTHOR, "meta-author" );
2372     GET_META( VLC_META_ARTIST, "meta-artist" );
2373     GET_META( VLC_META_GENRE, "meta-genre" );
2374     GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2375     GET_META( VLC_META_DESCRIPTION, "meta-description" );
2376     GET_META( VLC_META_DATE, "meta-date" );
2377     GET_META( VLC_META_URL, "meta-url" );
2378 #undef GET_META
2379
2380     return p_meta;
2381 }
2382
2383 /*****************************************************************************
2384  * DecodeUrl: decode a given encoded url
2385  *****************************************************************************/
2386 static void DecodeUrl( char *psz )
2387 {
2388     char *dup = strdup( psz );
2389     char *p = dup;
2390
2391     while( *p )
2392     {
2393         if( *p == '%' )
2394         {
2395             char val[3];
2396             p++;
2397             if( !*p )
2398             {
2399                 break;
2400             }
2401
2402             val[0] = *p++;
2403             val[1] = *p++;
2404             val[2] = '\0';
2405
2406             *psz++ = strtol( val, NULL, 16 );
2407         }
2408         else if( *p == '+' )
2409         {
2410             *psz++ = ' ';
2411             p++;
2412         }
2413         else
2414         {
2415             *psz++ = *p++;
2416         }
2417     }
2418     if( psz ) *psz++  ='\0';
2419     if( dup ) free( dup );
2420 }
2421
2422 /*****************************************************************************
2423  * MRLSplit: parse the access, demux and url part of the
2424  *           Media Resource Locator.
2425  *****************************************************************************/
2426 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2427                char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2428 {
2429     char *psz_access = NULL;
2430     char *psz_demux  = NULL;
2431     char *psz_path   = NULL;
2432     char *psz, *psz_check;
2433
2434     psz = strchr( psz_dup, ':' );
2435
2436     /* '@' not allowed in access/demux part */
2437     psz_check = strchr( psz_dup, '@' );
2438     if( psz_check && psz_check < psz ) psz = 0;
2439
2440 #if defined( WIN32 ) || defined( UNDER_CE )
2441     if( psz - psz_dup == 1 )
2442     {
2443         msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2444         psz_path = psz_dup;
2445     }
2446     else
2447 #endif
2448
2449     if( psz )
2450     {
2451         *psz++ = '\0';
2452         if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2453
2454         psz_path = psz;
2455
2456         psz = strchr( psz_dup, '/' );
2457         if( psz )
2458         {
2459             *psz++ = '\0';
2460             psz_demux = psz;
2461         }
2462
2463         psz_access = psz_dup;
2464     }
2465     else
2466     {
2467         psz_path = psz_dup;
2468     }
2469
2470     if( !psz_access ) *ppsz_access = "";
2471     else *ppsz_access = psz_access;
2472
2473     if( !psz_demux ) *ppsz_demux = "";
2474     else *ppsz_demux = psz_demux;
2475
2476     if( !psz_path ) *ppsz_path = "";
2477     else *ppsz_path = psz_path;
2478 }
2479
2480 /*****************************************************************************
2481  * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2482  *
2483  * Syntax:
2484  * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2485  *****************************************************************************/
2486 static void MRLSections( input_thread_t *p_input, char *psz_source,
2487                          int *pi_title_start, int *pi_title_end,
2488                          int *pi_chapter_start, int *pi_chapter_end )
2489 {
2490     char *psz, *psz_end, *psz_next, *psz_check;
2491
2492     *pi_title_start = *pi_title_end = -1;
2493     *pi_chapter_start = *pi_chapter_end = -1;
2494
2495     /* Start by parsing titles and chapters */
2496     if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2497
2498     /* Check we are really dealing with a title/chapter section */
2499     psz_check = psz + 1;
2500     if( !*psz_check ) return;
2501     if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2502     if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2503     if( *psz_check == ':' && ++psz_check )
2504         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2505     if( *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 ) return;
2512
2513     /* Separate start and end */
2514     *psz++ = 0;
2515     if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2516
2517     /* Look for the start title */
2518     *pi_title_start = strtol( psz, &psz_next, 0 );
2519     if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2520     *pi_title_end = *pi_title_start;
2521     psz = psz_next;
2522
2523     /* Look for the start chapter */
2524     if( *psz ) psz++;
2525     *pi_chapter_start = strtol( psz, &psz_next, 0 );
2526     if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2527     *pi_chapter_end = *pi_chapter_start;
2528
2529     if( psz_end )
2530     {
2531         /* Look for the end title */
2532         *pi_title_end = strtol( psz_end, &psz_next, 0 );
2533         if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2534         psz_end = psz_next;
2535
2536         /* Look for the end chapter */
2537         if( *psz_end ) psz_end++;
2538         *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2539         if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2540     }
2541
2542     msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2543              psz_source, *pi_title_start, *pi_chapter_start,
2544              *pi_title_end, *pi_chapter_end );
2545 }
2546
2547
2548 /***********************************************************************
2549  * Info management functions
2550  ***********************************************************************/
2551 /**
2552  * Get a info item from a given category in a given input item.
2553  *
2554  * \param p_i The input item to get info from
2555  * \param psz_cat String representing the category for the info
2556  * \param psz_name String representing the name of the desired info
2557  * \return A pointer to the string with the given info if found, or an
2558  *         empty string otherwise. The caller should free the returned
2559  *         pointer.
2560  */
2561 char *vlc_input_item_GetInfo( input_item_t *p_i,
2562                               const char *psz_cat,
2563                               const char *psz_name )
2564 {
2565     int i,j;
2566
2567     vlc_mutex_lock( &p_i->lock );
2568
2569     for( i = 0 ; i< p_i->i_categories  ; i++ )
2570     {
2571         info_category_t *p_cat = p_i->pp_categories[i];
2572
2573         if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
2574             continue;
2575
2576         for( j = 0; j < p_cat->i_infos ; j++ )
2577         {
2578             if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
2579             {
2580                 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
2581                 vlc_mutex_unlock( &p_i->lock );
2582                 return psz_ret;
2583             }
2584         }
2585     }
2586     vlc_mutex_unlock( &p_i->lock );
2587     return strdup( "" );
2588 }
2589
2590 int vlc_input_item_AddInfo( input_item_t *p_i,
2591                             const char *psz_cat,
2592                             const char *psz_name,
2593                             const char *psz_format, ... )
2594 {
2595     va_list args;
2596     int i;
2597     info_t *p_info = NULL;
2598     info_category_t *p_cat = NULL ;
2599
2600     vlc_mutex_lock( &p_i->lock );
2601
2602     for( i = 0 ; i < p_i->i_categories ; i ++ )
2603     {
2604         if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
2605         {
2606             p_cat = p_i->pp_categories[i];
2607             break;
2608         }
2609     }
2610     if( !p_cat )
2611     {
2612         if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
2613         {
2614             vlc_mutex_unlock( &p_i->lock );
2615             return VLC_EGENERIC;
2616         }
2617         p_cat->psz_name = strdup( psz_cat );
2618         p_cat->i_infos = 0;
2619         p_cat->pp_infos = 0;
2620         INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
2621                      p_cat );
2622     }
2623
2624     for( i = 0; i< p_cat->i_infos; i++ )
2625     {
2626         if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
2627         {
2628             p_info = p_cat->pp_infos[i];
2629             break;
2630         }
2631     }
2632
2633     if( !p_info )
2634     {
2635         if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
2636         {
2637             vlc_mutex_unlock( &p_i->lock );
2638             return VLC_EGENERIC;
2639         }
2640         INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
2641         p_info->psz_name = strdup( psz_name );
2642     }
2643     else
2644     {
2645         if( p_info->psz_value ) free( p_info->psz_value );
2646     }
2647
2648     va_start( args, psz_format );
2649     vasprintf( &p_info->psz_value, psz_format, args);
2650     va_end( args );
2651
2652     vlc_mutex_unlock( &p_i->lock );
2653
2654     return VLC_SUCCESS;
2655 }
2656