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