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