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