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