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