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