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