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