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