]> git.sesse.net Git - vlc/blob - src/input/input.c
input.c: fixed segfault with access_demux.
[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
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 = strchr( psz, '#' );
719
720         for( ;; )
721         {
722             input_source_t *slave;
723
724             if( psz_delim )
725             {
726                 *psz_delim++ = '\0';
727             }
728
729             if( *psz == '\0' )
730             {
731                 if( psz_delim )
732                     continue;
733                 else
734                     break;
735             }
736
737             msg_Dbg( p_input, "adding slave '%s'", psz );
738             slave = InputSourceNew( p_input );
739             if( !InputSourceInit( p_input, slave, psz, NULL ) )
740             {
741                 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
742             }
743             if( !psz_delim )
744                 break;
745         }
746     }
747     free( psz );
748
749     /* Set up es_out */
750     es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
751     i_es_out_mode = ES_OUT_MODE_AUTO;
752     val.p_list = NULL;
753     if( p_input->p_sout )
754     {
755         var_Get( p_input, "sout-all", &val );
756         if ( val.b_bool )
757         {
758             i_es_out_mode = ES_OUT_MODE_ALL;
759             val.p_list = NULL;
760         }
761         else
762         {
763             var_Get( p_input, "programs", &val );
764             if ( val.p_list && val.p_list->i_count )
765             {
766                 i_es_out_mode = ES_OUT_MODE_PARTIAL;
767                 /* Note : we should remove the "program" callback. */
768             }
769             else
770                 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val, NULL );
771         }
772     }
773     es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
774
775     /* Inform the demuxer about waited group (needed only for DVB) */
776     if( i_es_out_mode == ES_OUT_MODE_ALL )
777     {
778         demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
779     }
780     else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
781     {
782         demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
783                         val.p_list );
784     }
785     else
786     {
787         demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
788                        (int) var_GetInteger( p_input, "program" ), NULL );
789     }
790
791     if( p_input->p_sout )
792     {
793         if( p_input->p_sout->i_out_pace_nocontrol > 0 )
794         {
795             p_input->b_out_pace_control = VLC_FALSE;
796         }
797         else
798         {
799             p_input->b_out_pace_control = VLC_TRUE;
800         }
801         msg_Dbg( p_input, "starting in %s mode",
802                  p_input->b_out_pace_control ? "asynch" : "synch" );
803     }
804
805     /* Get meta data from users */
806     p_meta_tmp = InputMetaUser( p_input );
807
808     /* Get meta data from master input */
809     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
810         p_meta = NULL;
811
812     /* Merge them */
813     if( p_meta == NULL )
814     {
815         p_meta = p_meta_tmp;
816     }
817     else if( p_meta_tmp )
818     {
819         vlc_meta_Merge( p_meta, p_meta_tmp );
820         vlc_meta_Delete( p_meta_tmp );
821     }
822
823     if( !p_input->input.p_access ||
824         access2_Control( p_input->input.p_access, ACCESS_GET_META, &p_meta_tmp))
825         p_meta_tmp = NULL;
826
827     if( p_meta == NULL )
828     {
829         p_meta = p_meta_tmp;
830     }
831     else if( p_meta_tmp )
832     {
833         vlc_meta_Merge( p_meta, p_meta_tmp );
834         vlc_meta_Delete( p_meta_tmp );
835     }
836
837     /* Get meta data from slave input */
838     for( i = 0; i < p_input->i_slave; i++ )
839     {
840         vlc_meta_t *p_meta_slave;
841
842         if( !demux2_Control( p_input->slave[i]->p_demux,
843                              DEMUX_GET_META, &p_meta_slave ) )
844         {
845             if( p_meta == NULL )
846             {
847                 p_meta = p_meta_slave;
848             }
849             else if( p_meta_slave )
850             {
851                 vlc_meta_Merge( p_meta, p_meta_slave );
852                 vlc_meta_Delete( p_meta_slave );
853             }
854         }
855
856         if( p_input->slave[i]->p_access && 
857             !access2_Control( p_input->slave[i]->p_access,
858                               ACCESS_GET_META, &p_meta_slave ) )
859         {
860             if( p_meta == NULL )
861             {
862                 p_meta = p_meta_slave;
863             }
864             else if( p_meta_slave )
865             {
866                 vlc_meta_Merge( p_meta, p_meta_slave );
867                 vlc_meta_Delete( p_meta_slave );
868             }
869         }
870
871     }
872
873     p_input->p_meta = p_meta;
874     UpdateMeta( p_input );
875
876     msg_Dbg( p_input, "`%s' sucessfully opened",
877              p_input->input.p_item->psz_uri );
878
879     /* Trigger intf update for this item */
880     /* Playlist has a callback on this variable and will forward
881      * it to intf */
882     var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
883
884     /* initialization is complete */
885     p_input->i_state = PLAYING_S;
886
887     val.i_int = PLAYING_S;
888     var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
889
890     return VLC_SUCCESS;
891
892 error:
893     if( p_input->p_es_out )
894         input_EsOutDelete( p_input->p_es_out );
895
896     if( p_input->p_sout )
897         sout_DeleteInstance( p_input->p_sout );
898
899     /* Mark them deleted */
900     p_input->input.p_demux = NULL;
901     p_input->input.p_stream = NULL;
902     p_input->input.p_access = NULL;
903     p_input->p_es_out = NULL;
904     p_input->p_sout = NULL;
905
906     return VLC_EGENERIC;
907 }
908
909 /*****************************************************************************
910  * Error: RunThread() error loop
911  *****************************************************************************
912  * This function is called when an error occurred during thread main's loop.
913  *****************************************************************************/
914 static void Error( input_thread_t *p_input )
915 {
916     while( !p_input->b_die )
917     {
918         /* Sleep a while */
919         msleep( INPUT_IDLE_SLEEP );
920     }
921 }
922
923 /*****************************************************************************
924  * End: end the input thread
925  *****************************************************************************/
926 static void End( input_thread_t * p_input )
927 {
928     vlc_value_t val;
929     int i;
930
931     msg_Dbg( p_input, "closing input" );
932
933     /* We are at the end */
934     p_input->i_state = END_S;
935
936     val.i_int = END_S;
937     var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
938
939     /* Clean control variables */
940     input_ControlVarClean( p_input );
941
942     /* Clean up master */
943     InputSourceClean( p_input, &p_input->input );
944
945     /* Delete slave */
946     for( i = 0; i < p_input->i_slave; i++ )
947     {
948         InputSourceClean( p_input, p_input->slave[i] );
949         free( p_input->slave[i] );
950     }
951     if( p_input->slave ) free( p_input->slave );
952
953     /* Unload all modules */
954     if( p_input->p_es_out )
955         input_EsOutDelete( p_input->p_es_out );
956
957     /* Close optional stream output instance */
958     if( p_input->p_sout )
959     {
960         vlc_object_t *p_pl =
961             vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
962         vlc_value_t keep;
963
964         if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
965         {
966             /* attach sout to the playlist */
967             msg_Warn( p_input, "keeping sout" );
968             vlc_object_detach( p_input->p_sout );
969             vlc_object_attach( p_input->p_sout, p_pl );
970         }
971         else
972         {
973             msg_Warn( p_input, "destroying sout" );
974             sout_DeleteInstance( p_input->p_sout );
975         }
976         if( p_pl )
977             vlc_object_release( p_pl );
978     }
979
980     /* Delete meta */
981     if( p_input->p_meta )
982         vlc_meta_Delete( p_input->p_meta );
983
984     /* Tell we're dead */
985     p_input->b_dead = VLC_TRUE;
986 }
987
988 /*****************************************************************************
989  * Control
990  *****************************************************************************/
991 static inline int ControlPopNoLock( input_thread_t *p_input,
992                                     int *pi_type, vlc_value_t *p_val )
993 {
994     if( p_input->i_control <= 0 )
995     {
996         return VLC_EGENERIC;
997     }
998
999     *pi_type = p_input->control[0].i_type;
1000     *p_val   = p_input->control[0].val;
1001
1002     p_input->i_control--;
1003     if( p_input->i_control > 0 )
1004     {
1005         int i;
1006
1007         for( i = 0; i < p_input->i_control; i++ )
1008         {
1009             p_input->control[i].i_type = p_input->control[i+1].i_type;
1010             p_input->control[i].val    = p_input->control[i+1].val;
1011         }
1012     }
1013
1014     return VLC_SUCCESS;
1015 }
1016
1017 static void ControlReduce( input_thread_t *p_input )
1018 {
1019     int i;
1020     for( i = 1; i < p_input->i_control; i++ )
1021     {
1022         const int i_lt = p_input->control[i-1].i_type;
1023         const int i_ct = p_input->control[i].i_type;
1024
1025         /* XXX We can't merge INPUT_CONTROL_SET_ES */
1026         msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1027                  i_lt, i_ct );
1028         if( i_lt == i_ct &&
1029             ( i_ct == INPUT_CONTROL_SET_STATE ||
1030               i_ct == INPUT_CONTROL_SET_RATE ||
1031               i_ct == INPUT_CONTROL_SET_POSITION ||
1032               i_ct == INPUT_CONTROL_SET_TIME ||
1033               i_ct == INPUT_CONTROL_SET_PROGRAM ||
1034               i_ct == INPUT_CONTROL_SET_TITLE ||
1035               i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1036               i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1037         {
1038             int j;
1039             msg_Dbg( p_input, "merged at %d", i );
1040             /* Remove the i-1 */
1041             for( j = i; j <  p_input->i_control; j++ )
1042                 p_input->control[j-1] = p_input->control[j];
1043             p_input->i_control--;
1044         }
1045         else
1046         {
1047             /* TODO but that's not that important
1048                 - merge SET_X with SET_X_CMD
1049                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1050                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1051                 - ?
1052                 */
1053         }
1054     }
1055 }
1056
1057 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1058                            vlc_value_t val )
1059 {
1060     vlc_bool_t b_force_update = VLC_FALSE;
1061
1062     switch( i_type )
1063     {
1064         case INPUT_CONTROL_SET_DIE:
1065             msg_Dbg( p_input, "control: INPUT_CONTROL_SET_DIE proceed" );
1066             /* Mark all submodules to die */
1067             if( p_input->input.p_access )
1068                 p_input->input.p_access->b_die = VLC_TRUE;
1069             if( p_input->input.p_stream )
1070                 p_input->input.p_stream->b_die = VLC_TRUE;
1071             p_input->input.p_demux->b_die = VLC_TRUE;
1072
1073             p_input->b_die = VLC_TRUE;
1074             break;
1075
1076         case INPUT_CONTROL_SET_POSITION:
1077         case INPUT_CONTROL_SET_POSITION_OFFSET:
1078         {
1079             double f_pos;
1080             if( i_type == INPUT_CONTROL_SET_POSITION )
1081             {
1082                 f_pos = val.f_float;
1083             }
1084             else
1085             {
1086                 /* Should not fail */
1087                 demux2_Control( p_input->input.p_demux,
1088                                 DEMUX_GET_POSITION, &f_pos );
1089                 f_pos += val.f_float;
1090             }
1091             if( f_pos < 0.0 ) f_pos = 0.0;
1092             if( f_pos > 1.0 ) f_pos = 1.0;
1093             if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1094                                 f_pos ) )
1095             {
1096                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1097                          "%2.1f%% failed", f_pos * 100 );
1098             }
1099             else
1100             {
1101                 if( p_input->i_slave > 0 )
1102                     SlaveSeek( p_input );
1103
1104                 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1105                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1106                 b_force_update = VLC_TRUE;
1107             }
1108             break;
1109         }
1110
1111         case INPUT_CONTROL_SET_TIME:
1112         case INPUT_CONTROL_SET_TIME_OFFSET:
1113         {
1114             int64_t i_time;
1115             int i_ret;
1116
1117             if( i_type == INPUT_CONTROL_SET_TIME )
1118             {
1119                 i_time = val.i_time;
1120             }
1121             else
1122             {
1123                 /* Should not fail */
1124                 demux2_Control( p_input->input.p_demux,
1125                                 DEMUX_GET_TIME, &i_time );
1126                 i_time += val.i_time;
1127             }
1128             if( i_time < 0 ) i_time = 0;
1129             i_ret = demux2_Control( p_input->input.p_demux,
1130                                     DEMUX_SET_TIME, i_time );
1131             if( i_ret )
1132             {
1133                 int64_t i_length;
1134                 /* Emulate it with a SET_POS */
1135
1136                 demux2_Control( p_input->input.p_demux,
1137                                 DEMUX_GET_LENGTH, &i_length );
1138                 if( i_length > 0 )
1139                 {
1140                     double f_pos = (double)i_time / (double)i_length;
1141                     i_ret = demux2_Control( p_input->input.p_demux,
1142                                             DEMUX_SET_POSITION, f_pos );
1143                 }
1144             }
1145             if( i_ret )
1146             {
1147                 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1148                          " failed", i_time );
1149             }
1150             else
1151             {
1152                 if( p_input->i_slave > 0 )
1153                     SlaveSeek( p_input );
1154
1155                 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1156
1157                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1158                 b_force_update = VLC_TRUE;
1159             }
1160             break;
1161         }
1162
1163         case INPUT_CONTROL_SET_STATE:
1164             if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1165                 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1166             {
1167                 int i_ret;
1168                 if( p_input->input.p_access )
1169                     i_ret = access2_Control( p_input->input.p_access,
1170                                              ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1171                 else
1172                     i_ret = demux2_Control( p_input->input.p_demux,
1173                                             DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1174
1175                 if( i_ret )
1176                 {
1177                     /* FIXME What to do ? */
1178                     msg_Warn( p_input, "cannot unset pause -> EOF" );
1179                     input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1180                 }
1181
1182                 b_force_update = VLC_TRUE;
1183
1184                 /* Switch to play */
1185                 p_input->i_state = PLAYING_S;
1186                 val.i_int = PLAYING_S;
1187                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1188
1189                 /* Reset clock */
1190                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1191             }
1192             else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1193                      p_input->b_can_pause )
1194             {
1195                 int i_ret;
1196                 if( p_input->input.p_access )
1197                     i_ret = access2_Control( p_input->input.p_access,
1198                                              ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1199                 else
1200                     i_ret = demux2_Control( p_input->input.p_demux,
1201                                             DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1202
1203                 b_force_update = VLC_TRUE;
1204
1205                 if( i_ret )
1206                 {
1207                     msg_Warn( p_input, "cannot set pause state" );
1208                     val.i_int = p_input->i_state;
1209                 }
1210                 else
1211                 {
1212                     val.i_int = PAUSE_S;
1213                 }
1214
1215                 /* Switch to new state */
1216                 p_input->i_state = val.i_int;
1217                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1218             }
1219             else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1220             {
1221                 b_force_update = VLC_TRUE;
1222
1223                 /* Correct "state" value */
1224                 val.i_int = p_input->i_state;
1225                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1226             }
1227             else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1228             {
1229                 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1230             }
1231             break;
1232
1233         case INPUT_CONTROL_SET_RATE:
1234         case INPUT_CONTROL_SET_RATE_SLOWER:
1235         case INPUT_CONTROL_SET_RATE_FASTER:
1236         {
1237             int i_rate;
1238
1239             if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1240                 i_rate = p_input->i_rate * 2;
1241             else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1242                 i_rate = p_input->i_rate / 2;
1243             else
1244                 i_rate = val.i_int;
1245
1246             if( i_rate < INPUT_RATE_MIN )
1247             {
1248                 msg_Dbg( p_input, "cannot set rate faster" );
1249                 i_rate = INPUT_RATE_MIN;
1250             }
1251             else if( i_rate > INPUT_RATE_MAX )
1252             {
1253                 msg_Dbg( p_input, "cannot set rate slower" );
1254                 i_rate = INPUT_RATE_MAX;
1255             }
1256             if( i_rate != INPUT_RATE_DEFAULT &&
1257                 ( !p_input->b_can_pace_control ||
1258                   ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1259             {
1260                 msg_Dbg( p_input, "cannot change rate" );
1261                 i_rate = INPUT_RATE_DEFAULT;
1262             }
1263             if( i_rate != p_input->i_rate )
1264             {
1265                 p_input->i_rate  = i_rate;
1266                 val.i_int = i_rate;
1267                 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1268
1269                 /* We haven't send data to decoder when rate != default */
1270                 if( i_rate == INPUT_RATE_DEFAULT )
1271                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1272
1273                 /* Reset clock */
1274                 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1275
1276                 b_force_update = VLC_TRUE;
1277             }
1278             break;
1279         }
1280
1281         case INPUT_CONTROL_SET_PROGRAM:
1282             /* No need to force update, es_out does it if needed */
1283             es_out_Control( p_input->p_es_out,
1284                             ES_OUT_SET_GROUP, val.i_int );
1285
1286             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1287                             NULL );
1288             break;
1289
1290         case INPUT_CONTROL_SET_ES:
1291             /* No need to force update, es_out does it if needed */
1292             es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1293                             input_EsOutGetFromID( p_input->p_es_out,
1294                                                   val.i_int ) );
1295             break;
1296
1297         case INPUT_CONTROL_SET_AUDIO_DELAY:
1298             input_EsOutSetDelay( p_input->p_es_out,
1299                                  AUDIO_ES, val.i_time );
1300             var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1301             break;
1302
1303         case INPUT_CONTROL_SET_SPU_DELAY:
1304             input_EsOutSetDelay( p_input->p_es_out,
1305                                  SPU_ES, val.i_time );
1306             var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1307             break;
1308
1309         case INPUT_CONTROL_SET_TITLE:
1310         case INPUT_CONTROL_SET_TITLE_NEXT:
1311         case INPUT_CONTROL_SET_TITLE_PREV:
1312             if( p_input->input.b_title_demux &&
1313                 p_input->input.i_title > 0 )
1314             {
1315                 /* TODO */
1316                 /* FIXME handle demux title */
1317                 demux_t *p_demux = p_input->input.p_demux;
1318                 int i_title;
1319
1320                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1321                     i_title = p_demux->info.i_title - 1;
1322                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1323                     i_title = p_demux->info.i_title + 1;
1324                 else
1325                     i_title = val.i_int;
1326
1327                 if( i_title >= 0 && i_title < p_input->input.i_title )
1328                 {
1329                     demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1330
1331                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1332                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1333
1334                     input_ControlVarTitle( p_input, i_title );
1335                 }
1336             }
1337             else if( p_input->input.i_title > 0 )
1338             {
1339                 access_t *p_access = p_input->input.p_access;
1340                 int i_title;
1341
1342                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1343                     i_title = p_access->info.i_title - 1;
1344                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1345                     i_title = p_access->info.i_title + 1;
1346                 else
1347                     i_title = val.i_int;
1348
1349                 if( i_title >= 0 && i_title < p_input->input.i_title )
1350                 {
1351                     access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1352                     stream_AccessReset( p_input->input.p_stream );
1353
1354                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1355                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1356                 }
1357             }
1358             break;
1359         case INPUT_CONTROL_SET_SEEKPOINT:
1360         case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1361         case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1362             if( p_input->input.b_title_demux &&
1363                 p_input->input.i_title > 0 )
1364             {
1365                 demux_t *p_demux = p_input->input.p_demux;
1366                 int i_seekpoint;
1367
1368                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1369                     i_seekpoint = p_demux->info.i_seekpoint - 1;
1370                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1371                     i_seekpoint = p_demux->info.i_seekpoint + 1;
1372                 else
1373                     i_seekpoint = val.i_int;
1374
1375                 if( i_seekpoint >= 0 && i_seekpoint <
1376                     p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1377                 {
1378                     demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1379
1380                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1381                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1382                 }
1383             }
1384             else if( p_input->input.i_title > 0 )
1385             {
1386                 access_t *p_access = p_input->input.p_access;
1387                 int i_seekpoint;
1388
1389                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1390                     i_seekpoint = p_access->info.i_seekpoint - 1;
1391                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1392                     i_seekpoint = p_access->info.i_seekpoint + 1;
1393                 else
1394                     i_seekpoint = val.i_int;
1395
1396                 if( i_seekpoint >= 0 && i_seekpoint <
1397                     p_input->input.title[p_access->info.i_title]->i_seekpoint )
1398                 {
1399                     access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1400                     stream_AccessReset( p_input->input.p_stream );
1401
1402                     input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1403                     es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1404                 }
1405             }
1406             break;
1407
1408         case INPUT_CONTROL_SET_BOOKMARK:
1409         default:
1410             msg_Err( p_input, "not yet implemented" );
1411             break;
1412     }
1413
1414     return b_force_update;
1415 }
1416
1417 /*****************************************************************************
1418  * UpdateFromDemux:
1419  *****************************************************************************/
1420 static int UpdateFromDemux( input_thread_t *p_input )
1421 {
1422     demux_t *p_demux = p_input->input.p_demux;
1423     vlc_value_t v;
1424
1425     if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1426     {
1427         v.i_int = p_demux->info.i_title;
1428         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1429
1430         input_ControlVarTitle( p_input, p_demux->info.i_title );
1431
1432         p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1433     }
1434     if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1435     {
1436         v.i_int = p_demux->info.i_seekpoint;
1437         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1438
1439         p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1440     }
1441     p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1442
1443     /* Hmmm only works with master input */
1444     if( p_input->input.p_demux == p_demux )
1445     {
1446         int i_title_end = p_input->input.i_title_end -
1447             p_input->input.i_title_offset;
1448         int i_seekpoint_end = p_input->input.i_seekpoint_end -
1449             p_input->input.i_seekpoint_offset;
1450
1451         if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1452         {
1453             if( p_demux->info.i_title > i_title_end ||
1454                 ( p_demux->info.i_title == i_title_end &&
1455                   p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1456         }
1457         else if( i_seekpoint_end >=0 )
1458         {
1459             if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1460         }
1461         else if( i_title_end >= 0 )
1462         {
1463             if( p_demux->info.i_title > i_title_end ) return 0;
1464         }
1465     }
1466
1467     return 1;
1468 }
1469
1470 /*****************************************************************************
1471  * UpdateFromAccess:
1472  *****************************************************************************/
1473 static int UpdateFromAccess( input_thread_t *p_input )
1474 {
1475     access_t *p_access = p_input->input.p_access;
1476     vlc_value_t v;
1477
1478     if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1479     {
1480         v.i_int = p_access->info.i_title;
1481         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1482
1483         input_ControlVarTitle( p_input, p_access->info.i_title );
1484
1485         stream_AccessUpdate( p_input->input.p_stream );
1486
1487         p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1488     }
1489     if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1490     {
1491         v.i_int = p_access->info.i_seekpoint;
1492         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1493
1494         p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1495     }
1496     if( p_access->info.i_update & INPUT_UPDATE_META )
1497     {
1498         /* TODO maybe multi - access ? */
1499         vlc_meta_t *p_meta;
1500         if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1501         {
1502             if( p_input->p_meta )
1503             {
1504                 vlc_meta_Merge( p_input->p_meta, p_meta );
1505                 vlc_meta_Delete( p_meta );
1506             }
1507             else
1508             {
1509                 p_input->p_meta = p_meta;
1510             }
1511
1512             UpdateMeta( p_input );
1513         }
1514         p_access->info.i_update &= ~INPUT_UPDATE_META;
1515     }
1516
1517     p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1518
1519     /* Hmmm only works with master input */
1520     if( p_input->input.p_access == p_access )
1521     {
1522         int i_title_end = p_input->input.i_title_end -
1523             p_input->input.i_title_offset;
1524         int i_seekpoint_end = p_input->input.i_seekpoint_end -
1525             p_input->input.i_seekpoint_offset;
1526
1527         if( i_title_end >= 0 && i_seekpoint_end >=0 )
1528         {
1529             if( p_access->info.i_title > i_title_end ||
1530                 ( p_access->info.i_title == i_title_end &&
1531                   p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1532         }
1533         else if( i_seekpoint_end >=0 )
1534         {
1535             if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1536         }
1537         else if( i_title_end >= 0 )
1538         {
1539             if( p_access->info.i_title > i_title_end ) return 0;
1540         }
1541     }
1542
1543     return 1;
1544 }
1545
1546 /*****************************************************************************
1547  * UpdateMeta:
1548  *****************************************************************************/
1549 static int  UpdateMeta( input_thread_t *p_input )
1550 {
1551     vlc_meta_t *p_meta = p_input->p_meta;
1552     int i;
1553
1554     if( !p_meta || p_meta->i_meta == 0 )
1555         return VLC_SUCCESS;
1556
1557     msg_Dbg( p_input, "meta information:" );
1558     for( i = 0; i < p_meta->i_meta; i++ )
1559     {
1560         msg_Dbg( p_input, "  - '%s' = '%s'",
1561                 _(p_meta->name[i]), p_meta->value[i] );
1562
1563         if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] )
1564             input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1565
1566         if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1567             input_Control( p_input, INPUT_ADD_INFO, _("General"),
1568                            _("Author"), p_meta->value[i] );
1569
1570         input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1571                       _(p_meta->name[i]), "%s", p_meta->value[i] );
1572     }
1573
1574     for( i = 0; i < p_meta->i_track; i++ )
1575     {
1576         vlc_meta_t *tk = p_meta->track[i];
1577         int j;
1578
1579         if( tk->i_meta > 0 )
1580         {
1581             char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1582
1583             msg_Dbg( p_input, "  - track[%d]:", i );
1584
1585             sprintf( psz_cat, "%s %d", _("Stream"), i );
1586             for( j = 0; j < tk->i_meta; j++ )
1587             {
1588                 msg_Dbg( p_input, "     - '%s' = '%s'", _(tk->name[j]),
1589                          tk->value[j] );
1590
1591                 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1592                                _(tk->name[j]), "%s", tk->value[j] );
1593             }
1594         }
1595     }
1596
1597     if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1598     {
1599         p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1600     }
1601
1602     return VLC_SUCCESS;
1603 }
1604
1605 /*****************************************************************************
1606  * UpdateItemLength:
1607  *****************************************************************************/
1608 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
1609 {
1610     char psz_buffer[MSTRTIME_MAX_SIZE];
1611
1612     vlc_mutex_lock( &p_input->input.p_item->lock );
1613     p_input->input.p_item->i_duration = i_length;
1614     vlc_mutex_unlock( &p_input->input.p_item->lock );
1615
1616     input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1617                    msecstotimestr( psz_buffer, i_length / 1000 ) );
1618 }
1619
1620 /*****************************************************************************
1621  * InputSourceNew:
1622  *****************************************************************************/
1623 static input_source_t *InputSourceNew( input_thread_t *p_input )
1624 {
1625     input_source_t *in = malloc( sizeof( input_source_t ) );
1626
1627     in->p_item   = NULL;
1628     in->p_access = NULL;
1629     in->p_stream = NULL;
1630     in->p_demux  = NULL;
1631     in->b_title_demux = VLC_FALSE;
1632     in->i_title  = 0;
1633     in->title    = NULL;
1634     in->b_can_pace_control = VLC_TRUE;
1635     in->b_eof = VLC_FALSE;
1636     in->i_cr_average = 0;
1637
1638     return in;
1639 }
1640
1641 /*****************************************************************************
1642  * InputSourceInit:
1643  *****************************************************************************/
1644 static int InputSourceInit( input_thread_t *p_input,
1645                             input_source_t *in, char *psz_mrl,
1646                             char *psz_forced_demux )
1647 {
1648     char *psz_dup = strdup( psz_mrl );
1649     char *psz_access;
1650     char *psz_demux;
1651     char *psz_path;
1652     vlc_value_t val;
1653
1654     /* Split uri */
1655     MRLSplit( p_input, psz_dup, &psz_access, &psz_demux, &psz_path );
1656
1657     msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1658              psz_mrl, psz_access, psz_demux, psz_path );
1659
1660     /* Hack to allow udp://@:port syntax */
1661     if( !psz_access ||
1662         (strncmp( psz_access, "udp", 3 ) && strncmp( psz_access, "rtp", 3 )) )
1663
1664     /* Find optional titles and seekpoints */
1665     MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1666                  &in->i_seekpoint_start, &in->i_seekpoint_end );
1667
1668     if( psz_forced_demux && *psz_forced_demux )
1669         psz_demux = psz_forced_demux;
1670
1671     /* Try access_demux if no demux given */
1672     if( *psz_demux == '\0' )
1673     {
1674         in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1675                                   NULL, p_input->p_es_out );
1676     }
1677
1678     if( in->p_demux )
1679     {
1680         int64_t i_pts_delay;
1681
1682         /* Get infos from access_demux */
1683         demux2_Control( in->p_demux,
1684                         DEMUX_GET_PTS_DELAY, &i_pts_delay );
1685         p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1686
1687         in->b_title_demux = VLC_TRUE;
1688         if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1689                             &in->title, &in->i_title,
1690                             &in->i_title_offset, &in->i_seekpoint_offset ) )
1691         {
1692             in->i_title = 0;
1693             in->title   = NULL;
1694         }
1695         demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1696                         &in->b_can_pace_control );
1697         demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1698                         &in->b_can_pause );
1699
1700         /* FIXME todo
1701         demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1702                         &val.b_bool );
1703         */
1704     }
1705     else
1706     {
1707         int64_t i_pts_delay;
1708
1709         /* Now try a real access */
1710         in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path );
1711
1712         /* Access failed, URL encoded ? */
1713         if( in->p_access == NULL && strchr( psz_path, '%' ) )
1714         {
1715             DecodeUrl( psz_path );
1716
1717             msg_Dbg( p_input, "retying with access `%s' demux `%s' path `%s'",
1718                      psz_access, psz_demux, psz_path );
1719
1720             in->p_access = access2_New( p_input,
1721                                         psz_access, psz_demux, psz_path );
1722         }
1723 #ifndef WIN32      /* Remove this gross hack from the win32 build as colons
1724                         * are forbidden in filenames on Win32. */
1725
1726         /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1727         if( in->p_access == NULL &&
1728             *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1729         {
1730             free( psz_dup );
1731             psz_dup = strdup( psz_mrl );
1732             psz_access = "";
1733             psz_demux = "";
1734             psz_path = psz_dup;
1735
1736             in->p_access = access2_New( p_input,
1737                                         psz_access, psz_demux, psz_path );
1738         }
1739 #endif
1740
1741         if( in->p_access == NULL )
1742         {
1743             msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
1744             goto error;
1745         }
1746
1747         /* Get infos from access */
1748         access2_Control( in->p_access,
1749                          ACCESS_GET_PTS_DELAY, &i_pts_delay );
1750         p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1751
1752         in->b_title_demux = VLC_FALSE;
1753         if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
1754                              &in->title, &in->i_title,
1755                              &in->i_title_offset, &in->i_seekpoint_offset ) )
1756
1757         {
1758             in->i_title = 0;
1759             in->title   = NULL;
1760         }
1761         access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
1762                          &in->b_can_pace_control );
1763         access2_Control( in->p_access, ACCESS_CAN_PAUSE,
1764                          &in->b_can_pause );
1765         access2_Control( in->p_access, ACCESS_CAN_SEEK,
1766                          &val.b_bool );
1767         var_Set( p_input, "seekable", val );
1768
1769         /* Create the stream_t */
1770         in->p_stream = stream_AccessNew( in->p_access );
1771         if( in->p_stream == NULL )
1772         {
1773             msg_Warn( p_input, "cannot create a stream_t from access" );
1774             goto error;
1775         }
1776
1777         /* Open a demuxer */
1778         if( *psz_demux == '\0' && *in->p_access->psz_demux )
1779         {
1780             psz_demux = in->p_access->psz_demux;
1781         }
1782         in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1783                                   in->p_stream, p_input->p_es_out );
1784         if( in->p_demux == NULL )
1785         {
1786             msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
1787                      psz_access, psz_demux, psz_path );
1788             goto error;
1789         }
1790
1791         /* TODO get title from demux */
1792         if( in->i_title <= 0 )
1793         {
1794             if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1795                                 &in->title, &in->i_title,
1796                                 &in->i_title_offset, &in->i_seekpoint_offset ))
1797             {
1798                 in->i_title = 0;
1799                 in->title   = NULL;
1800             }
1801             else
1802             {
1803                 in->b_title_demux = VLC_TRUE;
1804             }
1805         }
1806     }
1807     free( psz_dup );
1808     return VLC_SUCCESS;
1809
1810 error:
1811     if( in->p_demux )
1812         demux2_Delete( in->p_demux );
1813
1814     if( in->p_stream )
1815         stream_AccessDelete( in->p_stream );
1816
1817     if( in->p_access )
1818         access2_Delete( in->p_access );
1819     free( psz_dup );
1820
1821     return VLC_EGENERIC;
1822 }
1823
1824 /*****************************************************************************
1825  * InputSourceClean:
1826  *****************************************************************************/
1827 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
1828 {
1829     if( in->p_demux )
1830         demux2_Delete( in->p_demux );
1831
1832     if( in->p_stream )
1833         stream_AccessDelete( in->p_stream );
1834
1835     if( in->p_access )
1836         access2_Delete( in->p_access );
1837
1838     if( in->i_title > 0 )
1839     {
1840         int i;
1841         for( i = 0; i < in->i_title; i++ )
1842         {
1843             vlc_input_title_Delete( in->title[i] );
1844         }
1845         free( in->title );
1846     }
1847 }
1848
1849 static void SlaveDemux( input_thread_t *p_input )
1850 {
1851     int64_t i_time;
1852     int i;
1853     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1854     {
1855         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1856         return;
1857     }
1858
1859     for( i = 0; i < p_input->i_slave; i++ )
1860     {
1861         input_source_t *in = p_input->slave[i];
1862         int i_ret = 1;
1863
1864         if( in->b_eof )
1865             continue;
1866
1867         if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
1868         {
1869             for( ;; )
1870             {
1871                 int64_t i_stime;
1872                 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
1873                 {
1874                     msg_Err( p_input, "slave[%d] doesn't like "
1875                              "DEMUX_GET_TIME -> EOF", i );
1876                     i_ret = 0;
1877                     break;
1878                 }
1879
1880                 if( i_stime >= i_time )
1881                     break;
1882
1883                 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
1884                     break;
1885             }
1886         }
1887         else
1888         {
1889             i_ret = in->p_demux->pf_demux( in->p_demux );
1890         }
1891
1892         if( i_ret <= 0 )
1893         {
1894             msg_Dbg( p_input, "slave %d EOF", i );
1895             in->b_eof = VLC_TRUE;
1896         }
1897     }
1898 }
1899
1900 static void SlaveSeek( input_thread_t *p_input )
1901 {
1902     int64_t i_time;
1903     int i;
1904
1905     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
1906     {
1907         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1908         return;
1909     }
1910
1911     for( i = 0; i < p_input->i_slave; i++ )
1912     {
1913         input_source_t *in = p_input->slave[i];
1914
1915         if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
1916         {
1917             msg_Err( p_input, "seek failed for slave %d -> EOF", i );
1918             in->b_eof = VLC_TRUE;
1919         }
1920     }
1921 }
1922 /*****************************************************************************
1923  * InputMetaUser:
1924  *****************************************************************************/
1925 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
1926 {
1927     vlc_meta_t *p_meta;
1928     vlc_value_t val;
1929
1930     if( ( p_meta = vlc_meta_New() ) == NULL )
1931         return NULL;
1932
1933     /* Get meta information from user */
1934 #define GET_META( c, s ) \
1935     var_Get( p_input, (s), &val );  \
1936     if( *val.psz_string )       \
1937         vlc_meta_Add( p_meta, c, val.psz_string ); \
1938     free( val.psz_string )
1939
1940     GET_META( VLC_META_TITLE, "meta-title" );
1941     GET_META( VLC_META_AUTHOR, "meta-author" );
1942     GET_META( VLC_META_ARTIST, "meta-artist" );
1943     GET_META( VLC_META_GENRE, "meta-genre" );
1944     GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
1945     GET_META( VLC_META_DESCRIPTION, "meta-description" );
1946     GET_META( VLC_META_DATE, "meta-date" );
1947     GET_META( VLC_META_URL, "meta-url" );
1948 #undef GET_META
1949
1950     return p_meta;
1951 }
1952
1953 /*****************************************************************************
1954  * DecodeUrl: decode a given encoded url
1955  *****************************************************************************/
1956 static void DecodeUrl( char *psz )
1957 {
1958     char *dup = strdup( psz );
1959     char *p = dup;
1960
1961     while( *p )
1962     {
1963         if( *p == '%' )
1964         {
1965             char val[3];
1966             p++;
1967             if( !*p )
1968             {
1969                 break;
1970             }
1971
1972             val[0] = *p++;
1973             val[1] = *p++;
1974             val[2] = '\0';
1975
1976             *psz++ = strtol( val, NULL, 16 );
1977         }
1978         else if( *p == '+' )
1979         {
1980             *psz++ = ' ';
1981             p++;
1982         }
1983         else
1984         {
1985             *psz++ = *p++;
1986         }
1987     }
1988     *psz++  ='\0';
1989     free( dup );
1990 }
1991
1992 /*****************************************************************************
1993  * ParseOption: parses the options for the input
1994  *****************************************************************************
1995  * This function parses the input (config) options and creates their associated
1996  * object variables.
1997  * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1998  * bar is the value of the option.
1999  *****************************************************************************/
2000 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2001 {
2002     char *psz_name = (char *)psz_option;
2003     char *psz_value = strchr( psz_option, '=' );
2004     int  i_name_len, i_type;
2005     vlc_bool_t b_isno = VLC_FALSE;
2006     vlc_value_t val;
2007
2008     if( psz_value ) i_name_len = psz_value - psz_option;
2009     else i_name_len = strlen( psz_option );
2010
2011     /* It's too much of an hassle to remove the ':' when we parse
2012      * the cmd line :) */
2013     if( i_name_len && *psz_name == ':' )
2014     {
2015         psz_name++;
2016         i_name_len--;
2017     }
2018
2019     if( i_name_len == 0 ) return;
2020
2021     psz_name = strndup( psz_name, i_name_len );
2022     if( psz_value ) psz_value++;
2023
2024     i_type = config_GetType( p_input, psz_name );
2025
2026     if( !i_type && !psz_value )
2027     {
2028         /* check for "no-foo" or "nofoo" */
2029         if( !strncmp( psz_name, "no-", 3 ) )
2030         {
2031             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2032         }
2033         else if( !strncmp( psz_name, "no", 2 ) )
2034         {
2035             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2036         }
2037         else goto cleanup;           /* Option doesn't exist */
2038
2039         b_isno = VLC_TRUE;
2040         i_type = config_GetType( p_input, psz_name );
2041
2042         if( !i_type ) goto cleanup;  /* Option doesn't exist */
2043     }
2044     else if( !i_type ) goto cleanup; /* Option doesn't exist */
2045
2046     if( ( i_type != VLC_VAR_BOOL ) &&
2047         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2048
2049     /* Create the variable in the input object.
2050      * Children of the input object will be able to retreive this value
2051      * thanks to the inheritance property of the object variables. */
2052     var_Create( p_input, psz_name, i_type );
2053
2054     switch( i_type )
2055     {
2056     case VLC_VAR_BOOL:
2057         val.b_bool = !b_isno;
2058         break;
2059
2060     case VLC_VAR_INTEGER:
2061         val.i_int = atoi( psz_value );
2062         break;
2063
2064     case VLC_VAR_FLOAT:
2065         val.f_float = atof( psz_value );
2066         break;
2067
2068     case VLC_VAR_STRING:
2069     case VLC_VAR_MODULE:
2070     case VLC_VAR_FILE:
2071     case VLC_VAR_DIRECTORY:
2072         val.psz_string = psz_value;
2073         break;
2074
2075     default:
2076         goto cleanup;
2077         break;
2078     }
2079
2080     var_Set( p_input, psz_name, val );
2081
2082     msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2083
2084   cleanup:
2085     if( psz_name ) free( psz_name );
2086     return;
2087 }
2088
2089 /*****************************************************************************
2090  * MRLSplit: parse the access, demux and url part of the
2091  *           Media Resource Locator.
2092  *****************************************************************************/
2093 static void MRLSplit( input_thread_t *p_input, char *psz_dup,
2094                       char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2095 {
2096     char *psz_access = NULL;
2097     char *psz_demux  = NULL;
2098     char *psz_path   = NULL;
2099     char *psz, *psz_check;
2100
2101     psz = strchr( psz_dup, ':' );
2102
2103     /* '@' not allowed in access/demux part */
2104     psz_check = strchr( psz_dup, '@' );
2105     if( psz_check && psz_check < psz ) psz = 0;
2106
2107 #if defined( WIN32 ) || defined( UNDER_CE )
2108     if( psz - psz_dup == 1 )
2109     {
2110         msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2111         psz_path = psz_dup;
2112     }
2113     else
2114 #endif
2115
2116     if( psz )
2117     {
2118         *psz++ = '\0';
2119         if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2120
2121         psz_path = psz;
2122
2123         psz = strchr( psz_dup, '/' );
2124         if( psz )
2125         {
2126             *psz++ = '\0';
2127             psz_demux = psz;
2128         }
2129
2130         psz_access = psz_dup;
2131     }
2132     else
2133     {
2134         psz_path = psz_dup;
2135     }
2136
2137     if( !psz_access ) *ppsz_access = "";
2138     else *ppsz_access = psz_access;
2139
2140     if( !psz_demux ) *ppsz_demux = "";
2141     else *ppsz_demux = psz_demux;
2142
2143     if( !psz_path ) *ppsz_path = "";
2144     else *ppsz_path = psz_path;
2145 }
2146
2147 /*****************************************************************************
2148  * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2149  *
2150  * Syntax:
2151  * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2152  *****************************************************************************/
2153 static void MRLSections( input_thread_t *p_input, char *psz_source,
2154                          int *pi_title_start, int *pi_title_end,
2155                          int *pi_chapter_start, int *pi_chapter_end )
2156 {
2157     char *psz, *psz_end, *psz_next, *psz_check;
2158
2159     *pi_title_start = *pi_title_end = -1;
2160     *pi_chapter_start = *pi_chapter_end = -1;
2161
2162     /* Start by parsing titles and chapters */
2163     if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2164
2165     /* Check we are really dealing with a title/chapter section */
2166     psz_check = psz + 1;
2167     if( !*psz_check ) return;
2168     if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2169     if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2170     if( *psz_check == ':' && ++psz_check )
2171         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2172     if( *psz_check != '-' && *psz_check ) return;
2173     if( *psz_check == '-' && ++psz_check )
2174         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2175     if( *psz_check != ':' && *psz_check ) return;
2176     if( *psz_check == ':' && ++psz_check )
2177         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2178     if( *psz_check ) return;
2179
2180     /* Separate start and end */
2181     *psz++ = 0;
2182     if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2183
2184     /* Look for the start title */
2185     *pi_title_start = strtol( psz, &psz_next, 0 );
2186     if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2187     *pi_title_end = *pi_title_start;
2188     psz = psz_next;
2189
2190     /* Look for the start chapter */
2191     if( *psz ) psz++;
2192     *pi_chapter_start = strtol( psz, &psz_next, 0 );
2193     if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2194     *pi_chapter_end = *pi_chapter_start;
2195
2196     if( psz_end )
2197     {
2198         /* Look for the end title */
2199         *pi_title_end = strtol( psz_end, &psz_next, 0 );
2200         if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2201         psz_end = psz_next;
2202
2203         /* Look for the end chapter */
2204         if( *psz_end ) psz_end++;
2205         *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2206         if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2207     }
2208
2209     msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2210              psz_source, *pi_title_start, *pi_chapter_start,
2211              *pi_title_end, *pi_chapter_end );
2212 }