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