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