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