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