input.c: allow "intelligent" previous-chapter behaviour for access sources too
[vlc.git] / src / input / input.c
1 /*****************************************************************************
2  * input.c: input thread
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/decoder.h>
34 #include <vlc/vout.h>
35
36 #include "input_internal.h"
37
38 #include "stream_output.h"
39 #include "vlc_playlist.h"
40 #include "vlc_interface.h"
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static  int Run  ( input_thread_t *p_input );
46
47 static  int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
48 static void Error( input_thread_t *p_input );
49 static void End  ( input_thread_t *p_input );
50
51 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
52 static void       ControlReduce( input_thread_t * );
53 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
54
55
56 static int  UpdateFromAccess( input_thread_t * );
57 static int  UpdateFromDemux( input_thread_t * );
58 static int  UpdateMeta( input_thread_t *, vlc_bool_t );
59
60 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
61
62 static void ParseOption( input_thread_t *p_input, const char *psz_option );
63
64 static void DecodeUrl( char * );
65 static void MRLSplit( input_thread_t *, char *, char **, char **, char ** );
66 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
67
68 static input_source_t *InputSourceNew( input_thread_t *);
69 static int  InputSourceInit( input_thread_t *, input_source_t *,
70                              char *, char *psz_forced_demux,
71                              vlc_bool_t b_quick );
72 static void InputSourceClean( input_thread_t *, input_source_t * );
73
74 static void SlaveDemux( input_thread_t *p_input );
75 static void SlaveSeek( input_thread_t *p_input );
76
77 static vlc_meta_t *InputMetaUser( input_thread_t *p_input );
78
79 /*****************************************************************************
80  * input_CreateThread: creates a new input thread
81  *****************************************************************************
82  * This function creates a new input, and returns a pointer
83  * to its description. On error, it returns NULL.
84  *
85  * Variables for _public_ use:
86  * * Get and Set:
87  *  - state
88  *  - rate,rate-slower, rate-faster
89  *  - position, position-offset
90  *  - time, time-offset
91  *  - title,title-next,title-prev
92  *  - chapter,chapter-next, chapter-prev
93  *  - program, audio-es, video-es, spu-es
94  *  - audio-delay, spu-delay
95  *  - bookmark
96  * * Get only:
97  *  - length
98  *  - bookmarks
99  *  - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
100  * * For intf callback upon changes
101  *  - intf-change
102  * TODO explain when Callback is called
103  * TODO complete this list (?)
104  *****************************************************************************/
105 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
106                                       input_item_t *p_item )
107
108 {
109     input_thread_t *p_input;                        /* thread descriptor */
110     vlc_value_t val;
111     int i;
112
113     /* Allocate descriptor */
114     p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
115     if( p_input == NULL )
116     {
117         msg_Err( p_parent, "out of memory" );
118         return NULL;
119     }
120
121     /* Init Common fields */
122     p_input->b_eof = VLC_FALSE;
123     p_input->b_can_pace_control = VLC_TRUE;
124     p_input->i_start = 0;
125     p_input->i_time  = 0;
126     p_input->i_stop  = 0;
127     p_input->i_title = 0;
128     p_input->title   = NULL;
129     p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
130     p_input->i_state = INIT_S;
131     p_input->i_rate  = INPUT_RATE_DEFAULT;
132     p_input->i_bookmark = 0;
133     p_input->bookmark = NULL;
134     p_input->p_meta  = NULL;
135     p_input->p_es_out = NULL;
136     p_input->p_sout  = NULL;
137     p_input->b_out_pace_control = VLC_FALSE;
138     p_input->i_pts_delay = 0;
139
140     /* Init Input fields */
141     p_input->input.p_item = p_item;
142     p_input->input.p_access = NULL;
143     p_input->input.p_stream = NULL;
144     p_input->input.p_demux  = NULL;
145     p_input->input.b_title_demux = VLC_FALSE;
146     p_input->input.i_title  = 0;
147     p_input->input.title    = NULL;
148     p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
149     p_input->input.b_can_pace_control = VLC_TRUE;
150     p_input->input.b_eof = VLC_FALSE;
151     p_input->input.i_cr_average = 0;
152
153     /* No slave */
154     p_input->i_slave = 0;
155     p_input->slave   = NULL;
156
157     /* Init control buffer */
158     vlc_mutex_init( p_input, &p_input->lock_control );
159     p_input->i_control = 0;
160
161     /* Parse input options */
162     vlc_mutex_lock( &p_item->lock );
163     for( i = 0; i < p_item->i_options; i++ )
164     {
165 //        msg_Dbg( p_input, "option: %s", p_item->ppsz_options[i] );
166         ParseOption( p_input, p_item->ppsz_options[i] );
167     }
168     vlc_mutex_unlock( &p_item->lock );
169
170     /* Create Object Variables for private use only */
171     input_ConfigVarInit( p_input );
172
173     /* Create Objects variables for public Get and Set */
174     input_ControlVarInit( p_input );
175     p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
176
177     /* TODO */
178     var_Get( p_input, "bookmarks", &val );
179     if( val.psz_string )
180     {
181         /* FIXME: have a common cfg parsing routine used by sout and others */
182         char *psz_parser, *psz_start, *psz_end;
183         psz_parser = val.psz_string;
184         while( (psz_start = strchr( psz_parser, '{' ) ) )
185         {
186             seekpoint_t *p_seekpoint = vlc_seekpoint_New();
187             char backup;
188             psz_start++;
189             psz_end = strchr( psz_start, '}' );
190             if( !psz_end ) break;
191             psz_parser = psz_end + 1;
192             backup = *psz_parser;
193             *psz_parser = 0;
194             *psz_end = ',';
195
196             while( (psz_end = strchr( psz_start, ',' ) ) )
197             {
198                 *psz_end = 0;
199                 if( !strncmp( psz_start, "name=", 5 ) )
200                 {
201                     p_seekpoint->psz_name = psz_start + 5;
202                 }
203                 else if( !strncmp( psz_start, "bytes=", 6 ) )
204                 {
205                     p_seekpoint->i_byte_offset = atoll(psz_start + 6);
206                 }
207                 else if( !strncmp( psz_start, "time=", 5 ) )
208                 {
209                     p_seekpoint->i_time_offset = atoll(psz_start + 5) * 1000000;
210                 }
211                 psz_start = psz_end + 1;
212             }
213             msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
214                      p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
215                      p_seekpoint->i_time_offset );
216             input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
217             vlc_seekpoint_Delete( p_seekpoint );
218             *psz_parser = backup;
219         }
220         free( val.psz_string );
221     }
222
223     /* Now we can attach our new input */
224     vlc_object_attach( p_input, p_parent );
225
226     /* Create thread and wait for its readiness. */
227     if( vlc_thread_create( p_input, "input", Run,
228                            VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
229     {
230         msg_Err( p_input, "cannot create input thread" );
231         vlc_object_detach( p_input );
232         vlc_object_destroy( p_input );
233         return NULL;
234     }
235
236     return p_input;
237 }
238
239 /*****************************************************************************
240  * input_PreParse: Lightweight input for playlist item preparsing
241  *****************************************************************************/
242 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
243 {
244     input_thread_t *p_input;                        /* thread descriptor */
245     int i;
246
247     /* Allocate descriptor */
248     p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
249     if( p_input == NULL )
250     {
251         msg_Err( p_parent, "out of memory" );
252         return VLC_EGENERIC;
253     }
254
255     /* Init Common fields */
256     p_input->b_eof = VLC_FALSE;
257     p_input->b_can_pace_control = VLC_TRUE;
258     p_input->i_start = 0;
259     p_input->i_time  = 0;
260     p_input->i_stop  = 0;
261     p_input->i_title = 0;
262     p_input->title   = NULL;
263     p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
264     p_input->i_state = INIT_S;
265     p_input->i_rate  = INPUT_RATE_DEFAULT;
266     p_input->i_bookmark = 0;
267     p_input->bookmark = NULL;
268     p_input->p_meta  = NULL;
269     p_input->p_es_out = NULL;
270     p_input->p_sout  = NULL;
271     p_input->b_out_pace_control = VLC_FALSE;
272     p_input->i_pts_delay = 0;
273
274     /* Init Input fields */
275     p_input->input.p_item = p_item;
276     p_input->input.p_access = NULL;
277     p_input->input.p_stream = NULL;
278     p_input->input.p_demux  = NULL;
279     p_input->input.b_title_demux = VLC_FALSE;
280     p_input->input.i_title  = 0;
281     p_input->input.title    = NULL;
282     p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
283     p_input->input.b_can_pace_control = VLC_TRUE;
284     p_input->input.b_eof = VLC_FALSE;
285     p_input->input.i_cr_average = 0;
286
287     /* No slave */
288     p_input->i_slave = 0;
289     p_input->slave   = NULL;
290
291     /* Init control buffer */
292     vlc_mutex_init( p_input, &p_input->lock_control );
293     p_input->i_control = 0;
294
295     /* Parse input options */
296     vlc_mutex_lock( &p_item->lock );
297     for( i = 0; i < p_item->i_options; i++ )
298     {
299         ParseOption( p_input, p_item->ppsz_options[i] );
300     }
301     vlc_mutex_unlock( &p_item->lock );
302
303     /* Create Object Variables for private use only */
304     input_ConfigVarInit( p_input );
305     input_ControlVarInit( p_input );
306
307     p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
308
309     /* Now we can attach our new input */
310     vlc_object_attach( p_input, p_parent );
311
312     Init( p_input, VLC_TRUE );
313
314     /* Clean up master */
315     InputSourceClean( p_input, &p_input->input );
316
317     /* Kill access and demux */
318     if( p_input->input.p_access ) p_input->input.p_access->b_die = VLC_TRUE;
319     if( p_input->input.p_demux ) p_input->input.p_access->b_die = VLC_TRUE;
320
321     /* Unload all modules */
322     if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
323
324     /* Delete meta */
325     if( p_input->p_meta ) vlc_meta_Delete( p_input->p_meta );
326
327     vlc_object_detach( p_input );
328     vlc_object_destroy( p_input );
329
330     return VLC_SUCCESS;
331 }
332
333 /*****************************************************************************
334  * input_StopThread: mark an input thread as zombie
335  *****************************************************************************
336  * This function should not return until the thread is effectively cancelled.
337  *****************************************************************************/
338 void input_StopThread( input_thread_t *p_input )
339 {
340     vlc_list_t *p_list;
341     int i;
342
343     /* Set die for input */
344     p_input->b_die = VLC_TRUE;
345
346     /* We cannot touch p_input fields directly (we can from another thread),
347      * so use the vlc_object_find way, it's perfectly safe */
348
349     /* Set die for all access */
350     p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
351     for( i = 0; i < p_list->i_count; i++ )
352     {
353         p_list->p_values[i].p_object->b_die = VLC_TRUE;
354     }
355     vlc_list_release( p_list );
356
357     /* Set die for all stream */
358     p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
359     for( i = 0; i < p_list->i_count; i++ )
360     {
361         p_list->p_values[i].p_object->b_die = VLC_TRUE;
362     }
363     vlc_list_release( p_list );
364
365     /* Set die for all demux */
366     p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
367     for( i = 0; i < p_list->i_count; i++ )
368     {
369         p_list->p_values[i].p_object->b_die = VLC_TRUE;
370     }
371     vlc_list_release( p_list );
372
373     input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
374 }
375
376 /*****************************************************************************
377  * input_DestroyThread: mark an input thread as zombie
378  *****************************************************************************
379  * This function should not return until the thread is effectively cancelled.
380  *****************************************************************************/
381 void input_DestroyThread( input_thread_t *p_input )
382 {
383     /* Join the thread */
384     vlc_thread_join( p_input );
385
386     /* Delete input lock (only after thread joined) */
387     vlc_mutex_destroy( &p_input->lock_control );
388
389     /* TODO: maybe input_DestroyThread should also delete p_input instead
390      * of the playlist but I'm not sure if it's possible */
391 }
392
393 /*****************************************************************************
394  * Run: main thread loop
395  *****************************************************************************
396  * Thread in charge of processing the network packets and demultiplexing.
397  *
398  * TODO:
399  *  read subtitle support (XXX take care of spu-delay in the right way).
400  *  multi-input support (XXX may be done with subs)
401  *****************************************************************************/
402 static int Run( input_thread_t *p_input )
403 {
404     int64_t i_intf_update = 0;
405
406     /* Signal that the thread is launched */
407     vlc_thread_ready( p_input );
408
409     if( Init( p_input, VLC_FALSE ) )
410     {
411         /* If we failed, wait before we are killed, and exit */
412         p_input->b_error = VLC_TRUE;
413
414         Error( p_input );
415
416         /* Tell we're dead */
417         p_input->b_dead = VLC_TRUE;
418
419         return 0;
420     }
421
422     /* Main loop */
423     while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
424     {
425         vlc_bool_t b_force_update = VLC_FALSE;
426         int i_ret;
427         int i_type;
428         vlc_value_t val;
429
430         /* Do the read */
431         if( p_input->i_state != PAUSE_S  )
432         {
433             if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
434                 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
435             else
436                 i_ret = 0;  /* EOF */
437
438             if( i_ret > 0 )
439             {
440                 /* TODO */
441                 if( p_input->input.b_title_demux &&
442                     p_input->input.p_demux->info.i_update )
443                 {
444                     i_ret = UpdateFromDemux( p_input );
445                     b_force_update = VLC_TRUE;
446                 }
447                 else if( !p_input->input.b_title_demux &&
448                           p_input->input.p_access &&
449                           p_input->input.p_access->info.i_update )
450                 {
451                     i_ret = UpdateFromAccess( p_input );
452                     b_force_update = VLC_TRUE;
453                 }
454             }
455
456             if( i_ret == 0 )    /* EOF */
457             {
458                 vlc_value_t repeat;
459
460                 var_Get( p_input, "input-repeat", &repeat );
461                 if( repeat.i_int == 0 )
462                 {
463                     /* End of file - we do not set b_die because only the
464                      * playlist is allowed to do so. */
465                     msg_Dbg( p_input, "EOF reached" );
466                     p_input->input.b_eof = VLC_TRUE;
467                 }
468                 else
469                 {
470                     msg_Dbg( p_input, "repeating the same input (%d)",
471                              repeat.i_int );
472                     if( repeat.i_int > 0 )
473                     {
474                         repeat.i_int--;
475                         var_Set( p_input, "input-repeat", repeat );
476                     }
477
478                     /* Seek to start title/seekpoint */
479                     val.i_int = p_input->input.i_title_start -
480                         p_input->input.i_title_offset;
481                     if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
482                         val.i_int = 0;
483                     input_ControlPush( p_input,
484                                        INPUT_CONTROL_SET_TITLE, &val );
485
486                     val.i_int = p_input->input.i_seekpoint_start -
487                         p_input->input.i_seekpoint_offset;
488                     if( val.i_int > 0 /* TODO: check upper boundary */ )
489                         input_ControlPush( p_input,
490                                            INPUT_CONTROL_SET_SEEKPOINT, &val );
491
492                     /* Seek to start position */
493                     if( p_input->i_start > 0 )
494                     {
495                         val.i_time = p_input->i_start;
496                         input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
497                                            &val );
498                     }
499                     else
500                     {
501                         val.f_float = 0.0;
502                         input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
503                                            &val );
504                     }
505                 }
506             }
507             else if( i_ret < 0 )
508             {
509                 p_input->b_error = VLC_TRUE;
510             }
511
512             if( i_ret > 0 && p_input->i_slave > 0 )
513             {
514                 SlaveDemux( p_input );
515             }
516         }
517         else
518         {
519             /* Small wait */
520             msleep( 10*1000 );
521         }
522
523         /* Handle control */
524         vlc_mutex_lock( &p_input->lock_control );
525         ControlReduce( p_input );
526         while( !ControlPopNoLock( p_input, &i_type, &val ) )
527         {
528             msg_Dbg( p_input, "control type=%d", i_type );
529             if( Control( p_input, i_type, val ) )
530                 b_force_update = VLC_TRUE;
531         }
532         vlc_mutex_unlock( &p_input->lock_control );
533
534         if( b_force_update || i_intf_update < mdate() )
535         {
536             vlc_value_t val;
537             double f_pos;
538             int64_t i_time, i_length;
539             /* update input status variables */
540             if( !demux2_Control( p_input->input.p_demux,
541                                  DEMUX_GET_POSITION, &f_pos ) )
542             {
543                 val.f_float = (float)f_pos;
544                 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
545             }
546             if( !demux2_Control( p_input->input.p_demux,
547                                  DEMUX_GET_TIME, &i_time ) )
548             {
549                 p_input->i_time = i_time;
550                 val.i_time = i_time;
551                 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
552             }
553             if( !demux2_Control( p_input->input.p_demux,
554                                  DEMUX_GET_LENGTH, &i_length ) )
555             {
556                 vlc_value_t old_val;
557                 var_Get( p_input, "length", &old_val );
558                 val.i_time = i_length;
559                 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
560
561                 if( old_val.i_time != val.i_time )
562                 {
563                     UpdateItemLength( p_input, i_length, VLC_TRUE );
564                 }
565             }
566
567             var_SetBool( p_input, "intf-change", VLC_TRUE );
568             i_intf_update = mdate() + I64C(150000);
569         }
570     }
571
572     if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
573     {
574         /* We have finish to demux data but not to play them */
575         while( !p_input->b_die )
576         {
577             if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
578                 break;
579
580             msg_Dbg( p_input, "waiting decoder fifos to empty" );
581
582             msleep( INPUT_IDLE_SLEEP );
583         }
584
585         /* We have finished */
586         p_input->b_eof = VLC_TRUE;
587     }
588
589     /* Wait we are asked to die */
590     if( !p_input->b_die )
591     {
592         Error( p_input );
593     }
594
595     /* Clean up */
596     End( p_input );
597
598     return 0;
599 }
600
601
602 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
603 {
604     char *psz;
605     char *psz_subtitle;
606     vlc_value_t val;
607     double f_fps;
608     vlc_meta_t *p_meta, *p_meta_tmp;
609     int i_es_out_mode;
610     int i, i_delay;
611
612     /* Initialize optional stream output. (before access/demuxer) */
613     if( !b_quick )
614     {
615         psz = var_GetString( p_input, "sout" );
616         if( *psz )
617         {
618             p_input->p_sout = sout_NewInstance( p_input, psz );
619             if( p_input->p_sout == NULL )
620             {
621                 msg_Err( p_input, "cannot start stream output instance, " \
622                                   "aborting" );
623                 free( psz );
624                 return VLC_EGENERIC;
625             }
626         }
627         free( psz );
628     }
629
630     /* Create es out */
631     p_input->p_es_out = input_EsOutNew( p_input );
632     es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
633     es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
634
635     if( InputSourceInit( p_input, &p_input->input,
636                          p_input->input.p_item->psz_uri, NULL, b_quick ) )
637     {
638         goto error;
639     }
640
641     /* Create global title (from master) */
642     if( !b_quick )
643     {
644         p_input->i_title = p_input->input.i_title;
645         p_input->title   = p_input->input.title;
646         p_input->i_title_offset = p_input->input.i_title_offset;
647         p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
648         if( p_input->i_title > 0 )
649         {
650             /* Setup variables */
651             input_ControlVarNavigation( p_input );
652             input_ControlVarTitle( p_input, 0 );
653         }
654
655         /* Global flag */
656         p_input->b_can_pace_control = p_input->input.b_can_pace_control;
657         p_input->b_can_pause        = p_input->input.b_can_pause;
658
659         /* Fix pts delay */
660         if( p_input->i_pts_delay <= 0 )
661             p_input->i_pts_delay = DEFAULT_PTS_DELAY;
662
663         /* If the desynchronisation requested by the user is < 0, we need to
664          * cache more data. */
665         var_Get( p_input, "audio-desync", &val );
666         if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
667
668         /* Update cr_average depending on the caching */
669         p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
670         p_input->input.i_cr_average /= 10;
671         if( p_input->input.i_cr_average <= 0 ) p_input->input.i_cr_average = 1;
672     }
673
674     /* Load master infos */
675     /* Init length */
676     if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
677                          &val.i_time ) && val.i_time > 0 )
678     {
679         var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
680         UpdateItemLength( p_input, val.i_time, b_quick );
681         p_input->input.p_item->i_duration = val.i_time;
682     }
683
684     /* Start title/chapter */
685     if( !b_quick )
686     {
687         val.i_int = p_input->input.i_title_start -
688                     p_input->input.i_title_offset;
689         if( val.i_int > 0 && val.i_int < p_input->input.i_title )
690             input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
691         val.i_int = p_input->input.i_seekpoint_start -
692                     p_input->input.i_seekpoint_offset;
693         if( val.i_int > 0 /* TODO: check upper boundary */ )
694             input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
695
696         /* Start time*/
697         /* Set start time */
698         p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
699                            I64C(1000000);
700         p_input->i_stop  = (int64_t)var_GetInteger( p_input, "stop-time" ) *
701                            I64C(1000000);
702
703         if( p_input->i_start > 0 )
704         {
705             if( p_input->i_start >= val.i_time )
706             {
707                 msg_Warn( p_input, "invalid start-time ignored" );
708             }
709             else
710             {
711                 vlc_value_t s;
712
713                 msg_Dbg( p_input, "start-time: %ds",
714                                   (int)( p_input->i_start / I64C(1000000) ) );
715
716                 s.i_time = p_input->i_start;
717                 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
718             }
719         }
720         if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
721         {
722             msg_Warn( p_input, "invalid stop-time ignored" );
723             p_input->i_stop = 0;
724         }
725
726
727         /* Load subtitles */
728         /* Get fps and set it if not already set */
729         if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
730             f_fps > 1.0 )
731         {
732             float f_requested_fps;
733
734             var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
735             var_SetFloat( p_input, "sub-original-fps", f_fps );
736
737             f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
738             if( f_requested_fps != f_fps )
739             {
740                 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
741                                                 VLC_VAR_DOINHERIT );
742                 var_SetFloat( p_input, "sub-fps", f_requested_fps );
743             }
744         }
745
746         i_delay = var_CreateGetInteger( p_input, "sub-delay" );
747         if( i_delay != 0 )
748         {
749             var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
750         }
751
752
753         /* Look for and add subtitle files */
754         psz_subtitle = var_GetString( p_input, "sub-file" );
755         if( *psz_subtitle )
756         {
757             input_source_t *sub;
758             vlc_value_t count;
759             vlc_value_t list;
760
761             msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
762
763             var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
764
765             /* */
766             sub = InputSourceNew( p_input );
767             if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
768                                   VLC_FALSE ) )
769             {
770                 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
771
772                 /* Select the ES */
773                 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
774                                  NULL ) )
775                 {
776                     if( count.i_int == 0 )
777                         count.i_int++;
778                         /* if it was first one, there is disable too */
779
780                     if( count.i_int < list.p_list->i_count )
781                     {
782                         input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
783                                           &list.p_list->p_values[count.i_int] );
784                     }
785                     var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
786                                 NULL );
787                 }
788             }
789         }
790
791         var_Get( p_input, "sub-autodetect-file", &val );
792         if( val.b_bool )
793         {
794            char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
795             char **subs = subtitles_Detect( p_input, psz_autopath,
796                                             p_input->input.p_item->psz_uri );
797             input_source_t *sub;
798
799             for( i = 0; subs[i] != NULL; i++ )
800             {
801                 if( strcmp( psz_subtitle, subs[i] ) )
802                 {
803                     sub = InputSourceNew( p_input );
804                     if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
805                                           VLC_FALSE ) )
806                     {
807                          TAB_APPEND( p_input->i_slave, p_input->slave, sub );
808                     }
809                 }
810                 free( subs[i] );
811             }
812             free( subs );
813             free( psz_autopath );
814         }
815         free( psz_subtitle );
816
817         /* Look for slave */
818         psz = var_GetString( p_input, "input-slave" );
819         if( *psz )
820         {
821             char *psz_delim;
822             input_source_t *slave;
823             while( psz && *psz )
824             {
825                 while( *psz == ' ' || *psz == '#' )
826                 {
827                     psz++;
828                 }
829                 if( ( psz_delim = strchr( psz, '#' ) ) )
830                 {
831                     *psz_delim++ = '\0';
832                 }
833                 if( *psz == 0 )
834                 {
835                     break;
836                 }
837
838                 msg_Dbg( p_input, "adding slave '%s'", psz );
839                 slave = InputSourceNew( p_input );
840                 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
841                 {
842                     TAB_APPEND( p_input->i_slave, p_input->slave, slave );
843                 }
844                 psz = psz_delim;
845             }
846         }
847         if( psz ) free( psz );
848     }
849     else
850     {
851         p_input->i_start = 0;
852         p_input->i_start = 0;
853     }
854
855     /* Set up es_out */
856     if( !b_quick )
857     {
858         es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
859         i_es_out_mode = ES_OUT_MODE_AUTO;
860         val.p_list = NULL;
861         if( p_input->p_sout )
862         {
863             var_Get( p_input, "sout-all", &val );
864             if ( val.b_bool )
865             {
866                 i_es_out_mode = ES_OUT_MODE_ALL;
867                 val.p_list = NULL;
868             }
869             else
870             {
871                 var_Get( p_input, "programs", &val );
872                 if ( val.p_list && val.p_list->i_count )
873                 {
874                     i_es_out_mode = ES_OUT_MODE_PARTIAL;
875                     /* Note : we should remove the "program" callback. */
876                 }
877                 else
878                     var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
879                                 NULL );
880             }
881         }
882         es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
883
884         /* Inform the demuxer about waited group (needed only for DVB) */
885         if( i_es_out_mode == ES_OUT_MODE_ALL )
886         {
887             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
888         }
889         else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
890         {
891             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
892                             val.p_list );
893         }
894         else
895         {
896             demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
897                            (int) var_GetInteger( p_input, "program" ), NULL );
898         }
899
900         if( p_input->p_sout )
901         {
902             if( p_input->p_sout->i_out_pace_nocontrol > 0 )
903             {
904                 p_input->b_out_pace_control = VLC_FALSE;
905             }
906             else
907             {
908                 p_input->b_out_pace_control = VLC_TRUE;
909             }
910             msg_Dbg( p_input, "starting in %s mode",
911                      p_input->b_out_pace_control ? "asynch" : "synch" );
912         }
913     }
914
915     /* Get meta data from users */
916     p_meta_tmp = InputMetaUser( p_input );
917
918     /* Get meta data from master input */
919     if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
920         p_meta = NULL;
921
922     /* Merge them */
923     if( p_meta == NULL )
924     {
925         p_meta = p_meta_tmp;
926     }
927     else if( p_meta_tmp )
928     {
929         vlc_meta_Merge( p_meta, p_meta_tmp );
930         vlc_meta_Delete( p_meta_tmp );
931     }
932
933     /* Access_file does not give any meta, and there are no slave */
934     if( !b_quick )
935     {
936         if( !p_input->input.p_access ||
937             access2_Control( p_input->input.p_access, ACCESS_GET_META,
938                              &p_meta_tmp))
939             p_meta_tmp = NULL;
940
941         if( p_meta == NULL )
942         {
943             p_meta = p_meta_tmp;
944         }
945         else if( p_meta_tmp )
946         {
947             vlc_meta_Merge( p_meta, p_meta_tmp );
948             vlc_meta_Delete( p_meta_tmp );
949         }
950
951         /* Get meta data from slave input */
952         for( i = 0; i < p_input->i_slave; i++ )
953         {
954             vlc_meta_t *p_meta_slave;
955
956             if( !demux2_Control( p_input->slave[i]->p_demux,
957                                  DEMUX_GET_META, &p_meta_slave ) )
958             {
959                 if( p_meta == NULL )
960                 {
961                     p_meta = p_meta_slave;
962                 }
963                 else if( p_meta_slave )
964                 {
965                     vlc_meta_Merge( p_meta, p_meta_slave );
966                     vlc_meta_Delete( p_meta_slave );
967                 }
968             }
969
970             if( p_input->slave[i]->p_access &&
971                 !access2_Control( p_input->slave[i]->p_access,
972                                   ACCESS_GET_META, &p_meta_slave ) )
973             {
974                 if( p_meta == NULL )
975                 {
976                     p_meta = p_meta_slave;
977                 }
978                 else if( p_meta_slave )
979                 {
980                     vlc_meta_Merge( p_meta, p_meta_slave );
981                     vlc_meta_Delete( p_meta_slave );
982                 }
983             }
984         }
985     }
986
987     p_input->p_meta = p_meta;
988     UpdateMeta( p_input, b_quick );
989
990     if( !b_quick )
991     {
992         msg_Dbg( p_input, "`%s' sucessfully opened",
993                  p_input->input.p_item->psz_uri );
994
995     }
996
997     /* Trigger intf update for this item */
998     /* Playlist has a callback on this variable and will forward
999      * it to intf */
1000     var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
1001
1002     /* initialization is complete */
1003     p_input->i_state = PLAYING_S;
1004
1005     val.i_int = PLAYING_S;
1006     var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1007
1008     return VLC_SUCCESS;
1009
1010 error:
1011     if( p_input->p_es_out )
1012         input_EsOutDelete( p_input->p_es_out );
1013
1014     if( p_input->p_sout )
1015         sout_DeleteInstance( p_input->p_sout );
1016
1017     /* Mark them deleted */
1018     p_input->input.p_demux = NULL;
1019     p_input->input.p_stream = NULL;
1020     p_input->input.p_access = NULL;
1021     p_input->p_es_out = NULL;
1022     p_input->p_sout = NULL;
1023
1024     return VLC_EGENERIC;
1025 }
1026
1027 /*****************************************************************************
1028  * Error: RunThread() error loop
1029  *****************************************************************************
1030  * This function is called when an error occurred during thread main's loop.
1031  *****************************************************************************/
1032 static void Error( input_thread_t *p_input )
1033 {
1034     while( !p_input->b_die )
1035     {
1036         /* Sleep a while */
1037         msleep( INPUT_IDLE_SLEEP );
1038     }
1039 }
1040
1041 /*****************************************************************************
1042  * End: end the input thread
1043  *****************************************************************************/
1044 static void End( input_thread_t * p_input )
1045 {
1046     vlc_value_t val;
1047     int i;
1048
1049     msg_Dbg( p_input, "closing input" );
1050
1051     /* We are at the end */
1052     p_input->i_state = END_S;
1053
1054     val.i_int = END_S;
1055     var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1056
1057     /* Clean control variables */
1058     input_ControlVarClean( p_input );
1059
1060     /* Clean up master */
1061     InputSourceClean( p_input, &p_input->input );
1062
1063     /* Delete slave */
1064     for( i = 0; i < p_input->i_slave; i++ )
1065     {
1066         InputSourceClean( p_input, p_input->slave[i] );
1067         free( p_input->slave[i] );
1068     }
1069     if( p_input->slave ) free( p_input->slave );
1070
1071     /* Unload all modules */
1072     if( p_input->p_es_out )
1073         input_EsOutDelete( p_input->p_es_out );
1074
1075     /* Close optional stream output instance */
1076     if( p_input->p_sout )
1077     {
1078         vlc_object_t *p_pl =
1079             vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1080         vlc_value_t keep;
1081
1082         if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1083         {
1084             /* attach sout to the playlist */
1085             msg_Warn( p_input, "keeping sout" );
1086             vlc_object_detach( p_input->p_sout );
1087             vlc_object_attach( p_input->p_sout, p_pl );
1088         }
1089         else
1090         {
1091             msg_Warn( p_input, "destroying sout" );
1092             sout_DeleteInstance( p_input->p_sout );
1093         }
1094         if( p_pl )
1095             vlc_object_release( p_pl );
1096     }
1097
1098     /* Delete meta */
1099     if( p_input->p_meta )
1100         vlc_meta_Delete( p_input->p_meta );
1101
1102     /* Tell we're dead */
1103     p_input->b_dead = VLC_TRUE;
1104 }
1105
1106 /*****************************************************************************
1107  * Control
1108  *****************************************************************************/
1109 static inline int ControlPopNoLock( input_thread_t *p_input,
1110                                     int *pi_type, vlc_value_t *p_val )
1111 {
1112     if( p_input->i_control <= 0 )
1113     {
1114         return VLC_EGENERIC;
1115     }
1116
1117     *pi_type = p_input->control[0].i_type;
1118     *p_val   = p_input->control[0].val;
1119
1120     p_input->i_control--;
1121     if( p_input->i_control > 0 )
1122     {
1123         int i;
1124
1125         for( i = 0; i < p_input->i_control; i++ )
1126         {
1127             p_input->control[i].i_type = p_input->control[i+1].i_type;
1128             p_input->control[i].val    = p_input->control[i+1].val;
1129         }
1130     }
1131
1132     return VLC_SUCCESS;
1133 }
1134
1135 static void ControlReduce( input_thread_t *p_input )
1136 {
1137     int i;
1138     for( i = 1; i < p_input->i_control; i++ )
1139     {
1140         const int i_lt = p_input->control[i-1].i_type;
1141         const int i_ct = p_input->control[i].i_type;
1142
1143         /* XXX We can't merge INPUT_CONTROL_SET_ES */
1144         msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1145                  i_lt, i_ct );
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: INPUT_CONTROL_SET_DIE proceed" );
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