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