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