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