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