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