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