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