]> git.sesse.net Git - vlc/blob - src/input/input.c
Added signal-quality/strength input variables for access signal report.
[vlc] / src / input / input.c
1 /*****************************************************************************
2  * input.c: input thread
3  *****************************************************************************
4  * Copyright (C) 1998-2007 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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33
34 #include <ctype.h>
35 #include <limits.h>
36 #include <assert.h>
37
38 #include "input_internal.h"
39
40 #include <vlc_sout.h>
41 #include "../stream_output/stream_output.h"
42
43 #include <vlc_interface.h>
44 #include <vlc_url.h>
45 #include <vlc_charset.h>
46 #include <vlc_strings.h>
47
48 #ifdef HAVE_SYS_STAT_H
49 #   include <sys/stat.h>
50 #endif
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static void Destructor( input_thread_t * p_input );
56
57 static  void* Run            ( vlc_object_t *p_this );
58 static  void* RunAndDestroy  ( vlc_object_t *p_this );
59
60 static input_thread_t * Create  ( vlc_object_t *, input_item_t *,
61                                   const char *, bool, sout_instance_t * );
62 static  int             Init    ( input_thread_t *p_input );
63 static void             WaitDie   ( input_thread_t *p_input );
64 static void             End     ( input_thread_t *p_input );
65 static void             MainLoop( input_thread_t *p_input );
66
67 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline );
68 static void       ControlReduce( input_thread_t * );
69 static bool Control( input_thread_t *, int, vlc_value_t );
70
71 static int  UpdateFromAccess( input_thread_t * );
72 static int  UpdateFromDemux( input_thread_t * );
73
74 static void UpdateItemLength( input_thread_t *, int64_t i_length );
75
76 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
77
78 static input_source_t *InputSourceNew( input_thread_t *);
79 static int  InputSourceInit( input_thread_t *, input_source_t *,
80                              const char *, const char *psz_forced_demux );
81 static void InputSourceClean( input_source_t * );
82 /* TODO */
83 //static void InputGetAttachments( input_thread_t *, input_source_t * );
84 static void SlaveDemux( input_thread_t *p_input );
85 static void SlaveSeek( input_thread_t *p_input );
86
87 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
88 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
89 static char *InputGetExtraFiles( input_thread_t *p_input,
90                                  const char *psz_access, const char *psz_path );
91
92 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
93 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
94 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
95                               int i_new, input_attachment_t **pp_new );
96
97 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced );
98
99 /*****************************************************************************
100  * This function creates a new input, and returns a pointer
101  * to its description. On error, it returns NULL.
102  *
103  * Variables for _public_ use:
104  * * Get and Set:
105  *  - state
106  *  - rate,rate-slower, rate-faster
107  *  - position, position-offset
108  *  - time, time-offset
109  *  - title,title-next,title-prev
110  *  - chapter,chapter-next, chapter-prev
111  *  - program, audio-es, video-es, spu-es
112  *  - audio-delay, spu-delay
113  *  - bookmark
114  * * Get only:
115  *  - length
116  *  - bookmarks
117  *  - seekable (if you can seek, it doesn't say if 'bar display' has be shown
118  *    or not, for that check position != 0.0)
119  *  - can-pause
120  *  - can-record (if a stream can be recorded while playing)
121  *  - teletext-es to get the index of spu track that is teletext --1 if no teletext)
122  * * For intf callback upon changes:
123  *  - intf-change
124  *  - intf-change-vout for when a vout is created or destroyed
125  *  - rate-change for when playback rate changes
126  * TODO explain when Callback is called
127  * TODO complete this list (?)
128  *****************************************************************************/
129 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
130                                const char *psz_header, bool b_quick,
131                                sout_instance_t *p_sout )
132 {
133     static const char input_name[] = "input";
134     input_thread_t *p_input = NULL;                 /* thread descriptor */
135     vlc_value_t val;
136     int i;
137
138     /* Allocate descriptor */
139     p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
140                                  VLC_OBJECT_INPUT, input_name );
141     if( p_input == NULL )
142         return NULL;
143
144     /* Construct a nice name for the input timer */
145     char psz_timer_name[255];
146     char * psz_name = input_item_GetName( p_item );
147     snprintf( psz_timer_name, sizeof(psz_timer_name),
148               "input launching for '%s'", psz_name );
149
150     msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
151
152     free( psz_name );
153
154     /* Start a timer to mesure how long it takes
155      * to launch an input */
156     stats_TimerStart( p_input, psz_timer_name,
157         STATS_TIMER_INPUT_LAUNCHING );
158
159     MALLOC_NULL( p_input->p, input_thread_private_t );
160     memset( p_input->p, 0, sizeof( input_thread_private_t ) );
161
162     /* One "randomly" selected input thread is responsible for computing
163      * the global stats. Check if there is already someone doing this */
164     if( p_input->p_libvlc->p_stats && !b_quick )
165     {
166         libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
167         vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
168         if( p_private->p_stats_computer == NULL )
169             p_private->p_stats_computer = p_input;
170         vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
171     }
172
173     p_input->b_preparsing = b_quick;
174     p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
175
176     /* Init events */
177     vlc_event_manager_t * p_em = &p_input->p->event_manager;
178     vlc_event_manager_init_with_vlc_object( p_em, p_input );
179     vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
180     vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
181
182     /* Init Common fields */
183     p_input->b_eof = false;
184     p_input->b_can_pace_control = true;
185     p_input->p->i_start = 0;
186     p_input->i_time  = 0;
187     p_input->p->i_stop  = 0;
188     p_input->p->i_run  = 0;
189     p_input->p->i_title = 0;
190     p_input->p->title   = NULL;
191     p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
192     p_input->i_state = INIT_S;
193     p_input->p->i_rate  = INPUT_RATE_DEFAULT;
194     p_input->p->b_recording = false;
195     TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
196     TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
197     p_input->p->p_es_out = NULL;
198     p_input->p->p_sout  = NULL;
199     p_input->p->b_out_pace_control = false;
200     p_input->i_pts_delay = 0;
201
202     /* Init Input fields */
203     vlc_gc_incref( p_item ); /* Released in Destructor() */
204     p_input->p->input.p_item = p_item;
205     p_input->p->input.p_access = NULL;
206     p_input->p->input.p_stream = NULL;
207     p_input->p->input.p_demux  = NULL;
208     p_input->p->input.b_title_demux = false;
209     p_input->p->input.i_title  = 0;
210     p_input->p->input.title    = NULL;
211     p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
212     p_input->p->input.b_can_pace_control = true;
213     p_input->p->input.b_can_rate_control = true;
214     p_input->p->input.b_rescale_ts = true;
215     p_input->p->input.b_eof = false;
216     p_input->p->input.i_cr_average = 0;
217
218     vlc_mutex_lock( &p_item->lock );
219
220     if( !p_item->p_stats )
221         p_item->p_stats = stats_NewInputStats( p_input );
222     vlc_mutex_unlock( &p_item->lock );
223
224     /* No slave */
225     p_input->p->i_slave = 0;
226     p_input->p->slave   = NULL;
227
228     /* Init control buffer */
229     vlc_mutex_init( &p_input->p->lock_control );
230     vlc_cond_init( &p_input->p->wait_control );
231     p_input->p->i_control = 0;
232
233     /* Parse input options */
234     vlc_mutex_lock( &p_item->lock );
235     assert( (int)p_item->optflagc == p_item->i_options );
236     for( i = 0; i < p_item->i_options; i++ )
237         var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
238                          !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
239     vlc_mutex_unlock( &p_item->lock );
240
241     /* Create Object Variables for private use only */
242     input_ConfigVarInit( p_input );
243
244     /* Create Objects variables for public Get and Set */
245     input_ControlVarInit( p_input );
246
247     /* */
248     p_input->p->pts_adjust.b_auto_adjust = var_GetBool( p_input, "auto-adjust-pts-delay" );
249     p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
250
251     if( !p_input->b_preparsing )
252     {
253         var_Get( p_input, "bookmarks", &val );
254         if( val.psz_string )
255         {
256             /* FIXME: have a common cfg parsing routine used by sout and others */
257             char *psz_parser, *psz_start, *psz_end;
258             psz_parser = val.psz_string;
259             while( (psz_start = strchr( psz_parser, '{' ) ) )
260             {
261                  seekpoint_t *p_seekpoint = vlc_seekpoint_New();
262                  char backup;
263                  psz_start++;
264                  psz_end = strchr( psz_start, '}' );
265                  if( !psz_end ) break;
266                  psz_parser = psz_end + 1;
267                  backup = *psz_parser;
268                  *psz_parser = 0;
269                  *psz_end = ',';
270                  while( (psz_end = strchr( psz_start, ',' ) ) )
271                  {
272                      *psz_end = 0;
273                      if( !strncmp( psz_start, "name=", 5 ) )
274                      {
275                          p_seekpoint->psz_name = strdup(psz_start + 5);
276                      }
277                      else if( !strncmp( psz_start, "bytes=", 6 ) )
278                      {
279                          p_seekpoint->i_byte_offset = atoll(psz_start + 6);
280                      }
281                      else if( !strncmp( psz_start, "time=", 5 ) )
282                      {
283                          p_seekpoint->i_time_offset = atoll(psz_start + 5) *
284                                                         1000000;
285                      }
286                      psz_start = psz_end + 1;
287                 }
288                 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
289                                   p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
290                                   p_seekpoint->i_time_offset );
291                 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
292                 vlc_seekpoint_Delete( p_seekpoint );
293                 *psz_parser = backup;
294             }
295             free( val.psz_string );
296         }
297     }
298
299     /* Remove 'Now playing' info as it is probably outdated */
300     input_item_SetNowPlaying( p_item, NULL );
301
302     /* */
303     if( p_input->b_preparsing )
304         p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
305
306     /* */
307     if( p_sout )
308         p_input->p->p_sout = p_sout;
309
310     memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
311     vlc_mutex_init( &p_input->p->counters.counters_lock );
312
313     /* Set the destructor when we are sure we are initialized */
314     vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
315
316     /* Attach only once we are ready */
317     vlc_object_attach( p_input, p_parent );
318
319     return p_input;
320 }
321
322 /**
323  * Input destructor (called when the object's refcount reaches 0).
324  */
325 static void Destructor( input_thread_t * p_input )
326 {
327 #ifndef NDEBUG
328     char * psz_name = input_item_GetName( p_input->p->input.p_item );
329     msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
330     free( psz_name );
331 #endif
332
333     vlc_event_manager_fini( &p_input->p->event_manager );
334
335     stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
336     stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
337 #ifdef ENABLE_SOUT
338     if( p_input->p->p_sout )
339         sout_DeleteInstance( p_input->p->p_sout );
340 #endif
341     vlc_gc_decref( p_input->p->input.p_item );
342
343     vlc_mutex_destroy( &p_input->p->counters.counters_lock );
344
345     vlc_cond_destroy( &p_input->p->wait_control );
346     vlc_mutex_destroy( &p_input->p->lock_control );
347     free( p_input->p );
348 }
349
350 /**
351  * Initialize an input thread and run it. You will need to monitor the
352  * thread to clean up after it is done
353  *
354  * \param p_parent a vlc_object
355  * \param p_item an input item
356  * \return a pointer to the spawned input thread
357  */
358 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
359                                       input_item_t *p_item )
360 {
361     return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
362 }
363
364 /* */
365 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
366                                               input_item_t *p_item,
367                                               const char *psz_log, sout_instance_t *p_sout )
368 {
369     input_thread_t *p_input;
370
371     p_input = Create( p_parent, p_item, psz_log, false, p_sout );
372     if( !p_input )
373         return NULL;
374
375     /* Create thread and wait for its readiness. */
376     if( vlc_thread_create( p_input, "input", Run,
377                            VLC_THREAD_PRIORITY_INPUT, false ) )
378     {
379         input_ChangeState( p_input, ERROR_S );
380         msg_Err( p_input, "cannot create input thread" );
381         vlc_object_detach( p_input );
382         vlc_object_release( p_input );
383         return NULL;
384     }
385
386     return p_input;
387 }
388
389 /**
390  * Initialize an input thread and run it. This thread will clean after itself,
391  * you can forget about it. It can work either in blocking or non-blocking mode
392  *
393  * \param p_parent a vlc_object
394  * \param p_item an input item
395  * \param b_block should we block until read is finished ?
396  * \return an error code, VLC_SUCCESS on success
397  */
398 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
399                    bool b_block )
400 {
401     input_thread_t *p_input;
402
403     p_input = Create( p_parent, p_item, NULL, false, NULL );
404     if( !p_input )
405         return VLC_EGENERIC;
406
407     if( b_block )
408     {
409         RunAndDestroy( VLC_OBJECT(p_input) );
410         return VLC_SUCCESS;
411     }
412     else
413     {
414         if( vlc_thread_create( p_input, "input", RunAndDestroy,
415                                VLC_THREAD_PRIORITY_INPUT, false ) )
416         {
417             input_ChangeState( p_input, ERROR_S );
418             msg_Err( p_input, "cannot create input thread" );
419             vlc_object_release( p_input );
420             return VLC_EGENERIC;
421         }
422     }
423     return VLC_SUCCESS;
424 }
425
426 /**
427  * Initialize an input and initialize it to preparse the item
428  * This function is blocking. It will only accept to parse files
429  *
430  * \param p_parent a vlc_object_t
431  * \param p_item an input item
432  * \return VLC_SUCCESS or an error
433  */
434 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
435 {
436     input_thread_t *p_input;
437
438     /* Allocate descriptor */
439     p_input = Create( p_parent, p_item, NULL, true, NULL );
440     if( !p_input )
441         return VLC_EGENERIC;
442
443     if( !Init( p_input ) )
444         End( p_input );
445
446     vlc_object_detach( p_input );
447     vlc_object_release( p_input );
448
449     return VLC_SUCCESS;
450 }
451
452 /**
453  * Request a running input thread to stop and die
454  *
455  * \param the input thread to stop
456  */
457 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
458 {
459     vlc_list_t *p_list;
460     int i;
461
462     if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
463         p_obj->i_object_type == VLC_OBJECT_AOUT ||
464         p_obj == VLC_OBJECT(p_input->p->p_sout) )
465         return;
466
467     vlc_object_kill( p_obj );
468
469     p_list = vlc_list_children( p_obj );
470     for( i = 0; i < p_list->i_count; i++ )
471         ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
472     vlc_list_release( p_list );
473 }
474 void input_StopThread( input_thread_t *p_input )
475 {
476     /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
477      * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
478      * unlock the control loop */
479     ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
480
481     input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
482 }
483
484 sout_instance_t * input_DetachSout( input_thread_t *p_input )
485 {
486     sout_instance_t *p_sout = p_input->p->p_sout;
487     vlc_object_detach( p_sout );
488     p_input->p->p_sout = NULL;
489     return p_sout;
490 }
491
492 /*****************************************************************************
493  * Run: main thread loop
494  * This is the "normal" thread that spawns the input processing chain,
495  * reads the stream, cleans up and waits
496  *****************************************************************************/
497 static void* Run( vlc_object_t *p_this )
498 {
499     input_thread_t *p_input = (input_thread_t *)p_this;
500     const int canc = vlc_savecancel();
501
502     if( Init( p_input ) )
503     {
504         /* If we failed, wait before we are killed, and exit */
505         WaitDie( p_input );
506         goto exit;
507     }
508
509     MainLoop( p_input );
510
511     /* Wait until we are asked to die */
512     if( !p_input->b_die )
513         WaitDie( p_input );
514
515     /* Clean up */
516     End( p_input );
517
518 exit:
519     p_input->b_dead = true;
520     vlc_restorecancel( canc );
521     return NULL;
522 }
523
524 /*****************************************************************************
525  * RunAndDestroy: main thread loop
526  * This is the "just forget me" thread that spawns the input processing chain,
527  * reads the stream, cleans up and releases memory
528  *****************************************************************************/
529 static void* RunAndDestroy( vlc_object_t *p_this )
530 {
531     input_thread_t *p_input = (input_thread_t *)p_this;
532     const int canc = vlc_savecancel();
533
534     if( Init( p_input ) )
535         goto exit;
536
537     MainLoop( p_input );
538
539     /* Clean up */
540     End( p_input );
541
542 exit:
543     /* Release memory */
544     vlc_object_release( p_input );
545     vlc_restorecancel( canc );
546     return NULL;
547 }
548
549 /*****************************************************************************
550  * Main loop: Fill buffers from access, and demux
551  *****************************************************************************/
552
553 /**
554  * MainLoopDemux
555  * It asks the demuxer to demux some data
556  */
557 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *pi_start_mdate )
558 {
559     int i_ret;
560
561     *pb_changed = false;
562
563     if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
564         ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
565         i_ret = 0; /* EOF */
566     else
567         i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
568
569     if( i_ret > 0 )
570     {
571         /* TODO */
572         if( p_input->p->input.b_title_demux &&
573             p_input->p->input.p_demux->info.i_update )
574         {
575             i_ret = UpdateFromDemux( p_input );
576             *pb_changed = true;
577         }
578         else if( !p_input->p->input.b_title_demux &&
579                   p_input->p->input.p_access &&
580                   p_input->p->input.p_access->info.i_update )
581         {
582             i_ret = UpdateFromAccess( p_input );
583             *pb_changed = true;
584         }
585     }
586
587     if( i_ret == 0 )    /* EOF */
588     {
589         vlc_value_t repeat;
590
591         var_Get( p_input, "input-repeat", &repeat );
592         if( repeat.i_int == 0 )
593         {
594             /* End of file - we do not set b_die because only the
595              * playlist is allowed to do so. */
596             msg_Dbg( p_input, "EOF reached" );
597             p_input->p->input.b_eof = true;
598         }
599         else
600         {
601             vlc_value_t val;
602
603             msg_Dbg( p_input, "repeating the same input (%d)",
604                      repeat.i_int );
605             if( repeat.i_int > 0 )
606             {
607                 repeat.i_int--;
608                 var_Set( p_input, "input-repeat", repeat );
609             }
610
611             /* Seek to start title/seekpoint */
612             val.i_int = p_input->p->input.i_title_start -
613                 p_input->p->input.i_title_offset;
614             if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
615                 val.i_int = 0;
616             input_ControlPush( p_input,
617                                INPUT_CONTROL_SET_TITLE, &val );
618
619             val.i_int = p_input->p->input.i_seekpoint_start -
620                 p_input->p->input.i_seekpoint_offset;
621             if( val.i_int > 0 /* TODO: check upper boundary */ )
622                 input_ControlPush( p_input,
623                                    INPUT_CONTROL_SET_SEEKPOINT, &val );
624
625             /* Seek to start position */
626             if( p_input->p->i_start > 0 )
627             {
628                 val.i_time = p_input->p->i_start;
629                 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
630                                    &val );
631             }
632             else
633             {
634                 val.f_float = 0.0;
635                 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
636                                    &val );
637             }
638
639             /* */
640             *pi_start_mdate = mdate();
641         }
642     }
643     else if( i_ret < 0 )
644     {
645         input_ChangeState( p_input, ERROR_S );
646     }
647
648     if( i_ret > 0 && p_input->p->i_slave > 0 )
649     {
650         SlaveDemux( p_input );
651     }
652 }
653
654 /**
655  * MainLoopInterface
656  * It update the variables used by the interfaces
657  */
658 static void MainLoopInterface( input_thread_t *p_input )
659 {
660     vlc_value_t val;
661     double f_pos;
662     int64_t i_time, i_length;
663
664     /* update input status variables */
665     if( !demux_Control( p_input->p->input.p_demux,
666                          DEMUX_GET_POSITION, &f_pos ) )
667     {
668         val.f_float = (float)f_pos;
669         var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
670     }
671     if( !demux_Control( p_input->p->input.p_demux,
672                          DEMUX_GET_TIME, &i_time ) )
673     {
674         p_input->i_time = i_time;
675         val.i_time = i_time;
676         var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
677     }
678     if( !demux_Control( p_input->p->input.p_demux,
679                          DEMUX_GET_LENGTH, &i_length ) )
680     {
681         vlc_value_t old_val;
682         var_Get( p_input, "length", &old_val );
683         val.i_time = i_length;
684         var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
685
686         if( old_val.i_time != val.i_time )
687         {
688             UpdateItemLength( p_input, i_length );
689         }
690     }
691
692     var_SetBool( p_input, "intf-change", true );
693 }
694
695 /**
696  * MainLoopStatistic
697  * It updates the globals statics
698  */
699 static void MainLoopStatistic( input_thread_t *p_input )
700 {
701     stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
702     /* Are we the thread responsible for computing global stats ? */
703     if( libvlc_priv( p_input->p_libvlc )->p_stats_computer == p_input )
704     {
705         stats_ComputeGlobalStats( p_input->p_libvlc,
706                                   p_input->p_libvlc->p_stats );
707     }
708 }
709
710 /**
711  * MainLoop
712  * The main input loop.
713  */
714 static void MainLoop( input_thread_t *p_input )
715 {
716     mtime_t i_start_mdate = mdate();
717     mtime_t i_intf_update = 0;
718     mtime_t i_statistic_update = 0;
719
720     /* Start the timer */
721     stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
722
723     while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
724     {
725         bool b_force_update;
726         int i_type;
727         vlc_value_t val;
728         mtime_t i_current;
729         mtime_t i_deadline;
730
731         /* Demux data */
732         b_force_update = false;
733         if( p_input->i_state != PAUSE_S )
734             MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
735
736         /* */
737         i_deadline = 0;
738         if( p_input->i_state == PAUSE_S )
739             i_deadline = __MIN( i_intf_update, i_statistic_update );
740
741         /* Handle control */
742         vlc_mutex_lock( &p_input->p->lock_control );
743         ControlReduce( p_input );
744         while( !ControlPopNoLock( p_input, &i_type, &val, i_deadline ) )
745         {
746             msg_Dbg( p_input, "control type=%d", i_type );
747             if( Control( p_input, i_type, val ) )
748                 b_force_update = true;
749         }
750         vlc_mutex_unlock( &p_input->p->lock_control );
751
752         /* Update interface and statistics */
753         i_current = mdate();
754         if( i_intf_update < i_current || b_force_update )
755         {
756             MainLoopInterface( p_input );
757             i_intf_update = i_current + INT64_C(250000);
758         }
759         if( i_statistic_update < i_current )
760         {
761             MainLoopStatistic( p_input );
762             i_statistic_update = i_current + INT64_C(1000000);
763         }
764     }
765
766     if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
767     {
768         /* We have finish to demux data but not to play them */
769         while( !p_input->b_die )
770         {
771             if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
772                 break;
773
774             msg_Dbg( p_input, "waiting decoder fifos to empty" );
775
776             msleep( INPUT_IDLE_SLEEP );
777         }
778
779         /* We have finished */
780         input_ChangeState( p_input, END_S );
781     }
782 }
783
784 static void InitStatistics( input_thread_t * p_input )
785 {
786     if( p_input->b_preparsing ) return;
787
788     /* Prepare statistics */
789 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
790  stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
791     if( libvlc_stats( p_input ) )
792     {
793         INIT_COUNTER( read_bytes, INTEGER, COUNTER );
794         INIT_COUNTER( read_packets, INTEGER, COUNTER );
795         INIT_COUNTER( demux_read, INTEGER, COUNTER );
796         INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
797         INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
798         INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
799         INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
800         INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
801         INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
802         INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
803         INIT_COUNTER( decoded_video, INTEGER, COUNTER );
804         INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
805         p_input->p->counters.p_sout_send_bitrate = NULL;
806         p_input->p->counters.p_sout_sent_packets = NULL;
807         p_input->p->counters.p_sout_sent_bytes = NULL;
808         if( p_input->p->counters.p_demux_bitrate )
809             p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
810         if( p_input->p->counters.p_input_bitrate )
811             p_input->p->counters.p_input_bitrate->update_interval = 1000000;
812     }
813 }
814
815 #ifdef ENABLE_SOUT
816 static int InitSout( input_thread_t * p_input )
817 {
818     char *psz;
819
820     if( p_input->b_preparsing ) return VLC_SUCCESS;
821
822     /* Find a usable sout and attach it to p_input */
823     psz = var_GetNonEmptyString( p_input, "sout" );
824     if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
825     {
826         /* Check the validity of the provided sout */
827         if( p_input->p->p_sout )
828         {
829             if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
830             {
831                 msg_Dbg( p_input, "destroying unusable sout" );
832
833                 sout_DeleteInstance( p_input->p->p_sout );
834                 p_input->p->p_sout = NULL;
835             }
836         }
837
838         if( p_input->p->p_sout )
839         {
840             /* Reuse it */
841             msg_Dbg( p_input, "sout keep: reusing sout" );
842             msg_Dbg( p_input, "sout keep: you probably want to use "
843                               "gather stream_out" );
844             vlc_object_attach( p_input->p->p_sout, p_input );
845         }
846         else
847         {
848             /* Create a new one */
849             p_input->p->p_sout = sout_NewInstance( p_input, psz );
850             if( !p_input->p->p_sout )
851             {
852                 input_ChangeState( p_input, ERROR_S );
853                 msg_Err( p_input, "cannot start stream output instance, " \
854                                   "aborting" );
855                 free( psz );
856                 return VLC_EGENERIC;
857             }
858         }
859         if( libvlc_stats( p_input ) )
860         {
861             INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
862             INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
863             INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
864             if( p_input->p->counters.p_sout_send_bitrate )
865                  p_input->p->counters.p_sout_send_bitrate->update_interval =
866                          1000000;
867         }
868     }
869     else if( p_input->p->p_sout )
870     {
871         msg_Dbg( p_input, "destroying useless sout" );
872
873         sout_DeleteInstance( p_input->p->p_sout );
874         p_input->p->p_sout = NULL;
875     }
876     free( psz );
877
878     return VLC_SUCCESS;
879 }
880 #endif
881
882 static void InitTitle( input_thread_t * p_input )
883 {
884     vlc_value_t val;
885
886     if( p_input->b_preparsing ) return;
887
888     /* Create global title (from master) */
889     p_input->p->i_title = p_input->p->input.i_title;
890     p_input->p->title   = p_input->p->input.title;
891     p_input->p->i_title_offset = p_input->p->input.i_title_offset;
892     p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
893     if( p_input->p->i_title > 0 )
894     {
895         /* Setup variables */
896         input_ControlVarNavigation( p_input );
897         input_ControlVarTitle( p_input, 0 );
898     }
899
900     /* Global flag */
901     p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
902     p_input->p->b_can_pause        = p_input->p->input.b_can_pause;
903     p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
904
905     /* Fix pts delay */
906     if( p_input->i_pts_delay < 0 )
907         p_input->i_pts_delay = 0;
908
909     /* If the desynchronisation requested by the user is < 0, we need to
910      * cache more data. */
911     var_Get( p_input, "audio-desync", &val );
912     if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
913
914     /* Update cr_average depending on the caching */
915     p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
916     p_input->p->input.i_cr_average /= 10;
917     if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
918 }
919
920 static void StartTitle( input_thread_t * p_input )
921 {
922     double f_fps;
923     vlc_value_t val;
924     int i_delay;
925     char *psz;
926     char *psz_subtitle;
927     int64_t i_length;
928
929     /* Start title/chapter */
930
931     if( p_input->b_preparsing )
932     {
933         p_input->p->i_start = 0;
934         return;
935     }
936
937     val.i_int = p_input->p->input.i_title_start -
938                 p_input->p->input.i_title_offset;
939     if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
940         input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
941     val.i_int = p_input->p->input.i_seekpoint_start -
942                 p_input->p->input.i_seekpoint_offset;
943     if( val.i_int > 0 /* TODO: check upper boundary */ )
944         input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
945
946     /* Start time*/
947     /* Set start time */
948     p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
949     p_input->p->i_stop  = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
950     p_input->p->i_run   = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
951     i_length = var_GetTime( p_input, "length" );
952     if( p_input->p->i_run < 0 )
953     {
954         msg_Warn( p_input, "invalid run-time ignored" );
955         p_input->p->i_run = 0;
956     }
957
958     if( p_input->p->i_start > 0 )
959     {
960         if( p_input->p->i_start >= i_length )
961         {
962             msg_Warn( p_input, "invalid start-time ignored" );
963         }
964         else
965         {
966             vlc_value_t s;
967
968             msg_Dbg( p_input, "starting at time: %ds",
969                               (int)( p_input->p->i_start / INT64_C(1000000) ) );
970
971             s.i_time = p_input->p->i_start;
972             input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
973         }
974     }
975     if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
976     {
977         msg_Warn( p_input, "invalid stop-time ignored" );
978         p_input->p->i_stop = 0;
979     }
980
981     /* Load subtitles */
982     /* Get fps and set it if not already set */
983     if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
984         f_fps > 1.0 )
985     {
986         float f_requested_fps;
987
988         var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
989         var_SetFloat( p_input, "sub-original-fps", f_fps );
990
991         f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
992         if( f_requested_fps != f_fps )
993         {
994             var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
995                                             VLC_VAR_DOINHERIT );
996             var_SetFloat( p_input, "sub-fps", f_requested_fps );
997         }
998     }
999
1000     i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1001     if( i_delay != 0 )
1002     {
1003         var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1004     }
1005
1006     /* Look for and add subtitle files */
1007     psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1008     if( psz_subtitle != NULL )
1009     {
1010         msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1011         SubtitleAdd( p_input, psz_subtitle, true );
1012     }
1013
1014     var_Get( p_input, "sub-autodetect-file", &val );
1015     if( val.b_bool )
1016     {
1017         char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1018         char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1019                                              p_input->p->input.p_item->psz_uri );
1020         free( psz_autopath );
1021
1022         for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1023         {
1024             /* Try to autoselect the first autodetected subtitles file
1025              * if no subtitles file was specified */
1026             bool b_forced = i == 0 && !psz_subtitle;
1027
1028             if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1029                 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1030
1031             free( ppsz_subs[i] );
1032         }
1033         free( ppsz_subs );
1034     }
1035     free( psz_subtitle );
1036
1037     /* Look for slave */
1038     psz = var_GetNonEmptyString( p_input, "input-slave" );
1039     if( psz != NULL )
1040     {
1041         char *psz_delim;
1042         input_source_t *slave;
1043         while( psz && *psz )
1044         {
1045             while( *psz == ' ' || *psz == '#' )
1046             {
1047                 psz++;
1048             }
1049             if( ( psz_delim = strchr( psz, '#' ) ) )
1050             {
1051                 *psz_delim++ = '\0';
1052             }
1053             if( *psz == 0 )
1054             {
1055                 break;
1056             }
1057
1058             msg_Dbg( p_input, "adding slave input '%s'", psz );
1059             slave = InputSourceNew( p_input );
1060             if( !InputSourceInit( p_input, slave, psz, NULL ) )
1061             {
1062                 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1063             }
1064             else free( slave );
1065             psz = psz_delim;
1066         }
1067         free( psz );
1068     }
1069 }
1070
1071 static void InitPrograms( input_thread_t * p_input )
1072 {
1073     int i_es_out_mode;
1074     vlc_value_t val;
1075
1076     if( p_input->b_preparsing ) return;
1077
1078     /* Set up es_out */
1079     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1080     i_es_out_mode = ES_OUT_MODE_AUTO;
1081     val.p_list = NULL;
1082     if( p_input->p->p_sout )
1083     {
1084         var_Get( p_input, "sout-all", &val );
1085         if( val.b_bool )
1086         {
1087             i_es_out_mode = ES_OUT_MODE_ALL;
1088             val.p_list = NULL;
1089         }
1090         else
1091         {
1092             var_Get( p_input, "programs", &val );
1093             if( val.p_list && val.p_list->i_count )
1094             {
1095                 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1096                 /* Note : we should remove the "program" callback. */
1097             }
1098             else
1099             {
1100                 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1101                             NULL );
1102             }
1103         }
1104     }
1105     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1106
1107     /* Inform the demuxer about waited group (needed only for DVB) */
1108     if( i_es_out_mode == ES_OUT_MODE_ALL )
1109     {
1110         demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1111     }
1112     else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1113     {
1114         demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1115                         val.p_list );
1116     }
1117     else
1118     {
1119         demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1120                        (int) var_GetInteger( p_input, "program" ), NULL );
1121     }
1122 }
1123
1124 static int Init( input_thread_t * p_input )
1125 {
1126     vlc_meta_t *p_meta;
1127     vlc_value_t val;
1128     int i, ret;
1129
1130     for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1131     {
1132         if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1133         {
1134             msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1135             var_SetString( p_input, "sout", "" );
1136             var_SetBool( p_input, "sout-all", false );
1137             var_SetString( p_input, "input-slave", "" );
1138             var_SetInteger( p_input, "input-repeat", 0 );
1139             var_SetString( p_input, "sub-file", "" );
1140             var_SetBool( p_input, "sub-autodetect-file", false );
1141         }
1142     }
1143
1144     InitStatistics( p_input );
1145 #ifdef ENABLE_SOUT
1146     ret = InitSout( p_input );
1147     if( ret != VLC_SUCCESS )
1148         return ret; /* FIXME: goto error; should be better here */
1149 #endif
1150
1151     /* Create es out */
1152     p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1153     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1154     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1155
1156     var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1157     var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1158
1159     if( InputSourceInit( p_input, &p_input->p->input,
1160                          p_input->p->input.p_item->psz_uri, NULL ) )
1161     {
1162         goto error;
1163     }
1164
1165     InitTitle( p_input );
1166
1167     /* Load master infos */
1168     /* Init length */
1169     if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1170                          &val.i_time ) && val.i_time > 0 )
1171     {
1172         var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1173         UpdateItemLength( p_input, val.i_time );
1174     }
1175     else
1176     {
1177         val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1178         if( val.i_time > 0 )
1179         { /* fallback: gets length from metadata */
1180             var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1181             UpdateItemLength( p_input, val.i_time );
1182         }
1183     }
1184
1185     StartTitle( p_input );
1186
1187     InitPrograms( p_input );
1188
1189     if( !p_input->b_preparsing && p_input->p->p_sout )
1190     {
1191         p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1192
1193         if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1194         {
1195             /* We don't want a high input priority here or we'll
1196              * end-up sucking up all the CPU time */
1197             vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1198         }
1199
1200         msg_Dbg( p_input, "starting in %s mode",
1201                  p_input->p->b_out_pace_control ? "async" : "sync" );
1202     }
1203
1204     p_meta = vlc_meta_New();
1205
1206     /* Get meta data from users */
1207     InputMetaUser( p_input, p_meta );
1208
1209     /* Get meta data from master input */
1210     DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1211
1212     /* Access_file does not give any meta, and there are no slave */
1213     AccessMeta( p_input, p_meta );
1214
1215     InputUpdateMeta( p_input, p_meta );
1216
1217     if( !p_input->b_preparsing )
1218     {
1219         msg_Dbg( p_input, "`%s' successfully opened",
1220                  p_input->p->input.p_item->psz_uri );
1221
1222     }
1223
1224     /* initialization is complete */
1225     input_ChangeState( p_input, PLAYING_S );
1226
1227     return VLC_SUCCESS;
1228
1229 error:
1230     input_ChangeState( p_input, ERROR_S );
1231
1232     if( p_input->p->p_es_out )
1233         input_EsOutDelete( p_input->p->p_es_out );
1234 #ifdef ENABLE_SOUT
1235     if( p_input->p->p_sout )
1236     {
1237         vlc_object_detach( p_input->p->p_sout );
1238         sout_DeleteInstance( p_input->p->p_sout );
1239     }
1240 #endif
1241
1242     if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1243     {
1244 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1245                                    stats_CounterClean( p_input->p->counters.p_##c );\
1246                                p_input->p->counters.p_##c = NULL; } while(0)
1247         EXIT_COUNTER( read_bytes );
1248         EXIT_COUNTER( read_packets );
1249         EXIT_COUNTER( demux_read );
1250         EXIT_COUNTER( input_bitrate );
1251         EXIT_COUNTER( demux_bitrate );
1252         EXIT_COUNTER( played_abuffers );
1253         EXIT_COUNTER( lost_abuffers );
1254         EXIT_COUNTER( displayed_pictures );
1255         EXIT_COUNTER( lost_pictures );
1256         EXIT_COUNTER( decoded_audio );
1257         EXIT_COUNTER( decoded_video );
1258         EXIT_COUNTER( decoded_sub );
1259
1260         if( p_input->p->p_sout )
1261         {
1262             EXIT_COUNTER( sout_sent_packets );
1263             EXIT_COUNTER( sout_sent_bytes );
1264             EXIT_COUNTER( sout_send_bitrate );
1265         }
1266 #undef EXIT_COUNTER
1267     }
1268
1269     /* Mark them deleted */
1270     p_input->p->input.p_demux = NULL;
1271     p_input->p->input.p_stream = NULL;
1272     p_input->p->input.p_access = NULL;
1273     p_input->p->p_es_out = NULL;
1274     p_input->p->p_sout = NULL;
1275
1276     return VLC_EGENERIC;
1277 }
1278
1279 /*****************************************************************************
1280  * WaitDie: Wait until we are asked to die.
1281  *****************************************************************************
1282  * This function is called when an error occurred during thread main's loop.
1283  *****************************************************************************/
1284 static void WaitDie( input_thread_t *p_input )
1285 {
1286     input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1287
1288     /* Wait a die order */
1289     vlc_object_lock( p_input );
1290     while( vlc_object_alive( p_input ) )
1291         vlc_object_wait( p_input );
1292     vlc_object_unlock( p_input );
1293 }
1294
1295 /*****************************************************************************
1296  * End: end the input thread
1297  *****************************************************************************/
1298 static void End( input_thread_t * p_input )
1299 {
1300     int i;
1301
1302     /* We are at the end */
1303     input_ChangeState( p_input, END_S );
1304
1305     /* Clean control variables */
1306     input_ControlVarStop( p_input );
1307
1308     /* Clean up master */
1309     InputSourceClean( &p_input->p->input );
1310
1311     /* Delete slave */
1312     for( i = 0; i < p_input->p->i_slave; i++ )
1313     {
1314         InputSourceClean( p_input->p->slave[i] );
1315         free( p_input->p->slave[i] );
1316     }
1317     free( p_input->p->slave );
1318
1319     /* Unload all modules */
1320     if( p_input->p->p_es_out )
1321         input_EsOutDelete( p_input->p->p_es_out );
1322
1323     if( !p_input->b_preparsing )
1324     {
1325 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1326         if( libvlc_stats( p_input ) )
1327         {
1328             libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1329
1330             /* make sure we are up to date */
1331             stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1332             if( p_private->p_stats_computer == p_input )
1333             {
1334                 stats_ComputeGlobalStats( p_input->p_libvlc,
1335                                           p_input->p_libvlc->p_stats );
1336                 p_private->p_stats_computer = NULL;
1337             }
1338             CL_CO( read_bytes );
1339             CL_CO( read_packets );
1340             CL_CO( demux_read );
1341             CL_CO( input_bitrate );
1342             CL_CO( demux_bitrate );
1343             CL_CO( played_abuffers );
1344             CL_CO( lost_abuffers );
1345             CL_CO( displayed_pictures );
1346             CL_CO( lost_pictures );
1347             CL_CO( decoded_audio) ;
1348             CL_CO( decoded_video );
1349             CL_CO( decoded_sub) ;
1350         }
1351
1352         /* Close optional stream output instance */
1353         if( p_input->p->p_sout )
1354         {
1355             CL_CO( sout_sent_packets );
1356             CL_CO( sout_sent_bytes );
1357             CL_CO( sout_send_bitrate );
1358
1359             vlc_object_detach( p_input->p->p_sout );
1360         }
1361 #undef CL_CO
1362     }
1363
1364     if( p_input->p->i_attachment > 0 )
1365     {
1366         for( i = 0; i < p_input->p->i_attachment; i++ )
1367             vlc_input_attachment_Delete( p_input->p->attachment[i] );
1368         TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1369     }
1370
1371     /* Tell we're dead */
1372     p_input->b_dead = true;
1373 }
1374
1375 /*****************************************************************************
1376  * Control
1377  *****************************************************************************/
1378 static inline int ControlPopNoLock( input_thread_t *p_input,
1379                                     int *pi_type, vlc_value_t *p_val,
1380                                     mtime_t i_deadline )
1381 {
1382
1383     while( p_input->p->i_control <= 0 )
1384     {
1385         if( i_deadline <= 0 )
1386             return VLC_EGENERIC;
1387
1388         if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1389             return VLC_EGENERIC;
1390     }
1391
1392     *pi_type = p_input->p->control[0].i_type;
1393     *p_val   = p_input->p->control[0].val;
1394
1395     p_input->p->i_control--;
1396     if( p_input->p->i_control > 0 )
1397     {
1398         int i;
1399
1400         for( i = 0; i < p_input->p->i_control; i++ )
1401         {
1402             p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1403             p_input->p->control[i].val    = p_input->p->control[i+1].val;
1404         }
1405     }
1406
1407     return VLC_SUCCESS;
1408 }
1409
1410 static void ControlReduce( input_thread_t *p_input )
1411 {
1412     int i;
1413
1414     if( !p_input )
1415         return;
1416
1417     for( i = 1; i < p_input->p->i_control; i++ )
1418     {
1419         const int i_lt = p_input->p->control[i-1].i_type;
1420         const int i_ct = p_input->p->control[i].i_type;
1421
1422         /* XXX We can't merge INPUT_CONTROL_SET_ES */
1423 /*        msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1424                  i_lt, i_ct );
1425 */
1426         if( i_lt == i_ct &&
1427             ( i_ct == INPUT_CONTROL_SET_STATE ||
1428               i_ct == INPUT_CONTROL_SET_RATE ||
1429               i_ct == INPUT_CONTROL_SET_POSITION ||
1430               i_ct == INPUT_CONTROL_SET_TIME ||
1431               i_ct == INPUT_CONTROL_SET_PROGRAM ||
1432               i_ct == INPUT_CONTROL_SET_TITLE ||
1433               i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1434               i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1435         {
1436             int j;
1437 //            msg_Dbg( p_input, "merged at %d", i );
1438             /* Remove the i-1 */
1439             for( j = i; j <  p_input->p->i_control; j++ )
1440                 p_input->p->control[j-1] = p_input->p->control[j];
1441             p_input->p->i_control--;
1442         }
1443         else
1444         {
1445             /* TODO but that's not that important
1446                 - merge SET_X with SET_X_CMD
1447                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1448                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1449                 - ?
1450                 */
1451         }
1452     }
1453 }
1454
1455 static bool Control( input_thread_t *p_input, int i_type,
1456                            vlc_value_t val )
1457 {
1458     bool b_force_update = false;
1459
1460     if( !p_input ) return b_force_update;
1461
1462     switch( i_type )
1463     {
1464         case INPUT_CONTROL_SET_DIE:
1465             msg_Dbg( p_input, "control: stopping input" );
1466
1467             /* Mark all submodules to die */
1468             ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1469             break;
1470
1471         case INPUT_CONTROL_SET_POSITION:
1472         case INPUT_CONTROL_SET_POSITION_OFFSET:
1473         {
1474             double f_pos;
1475
1476             if( p_input->p->b_recording )
1477             {
1478                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1479                 break;
1480             }
1481             if( i_type == INPUT_CONTROL_SET_POSITION )
1482             {
1483                 f_pos = val.f_float;
1484             }
1485             else
1486             {
1487                 /* Should not fail */
1488                 demux_Control( p_input->p->input.p_demux,
1489                                 DEMUX_GET_POSITION, &f_pos );
1490                 f_pos += val.f_float;
1491             }
1492             if( f_pos < 0.0 ) f_pos = 0.0;
1493             if( f_pos > 1.0 ) f_pos = 1.0;
1494             /* Reset the decoders states and clock sync (before calling the demuxer */
1495             input_EsOutChangePosition( p_input->p->p_es_out );
1496             if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1497                                 f_pos ) )
1498             {
1499                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1500                          "%2.1f%% failed", f_pos * 100 );
1501             }
1502             else
1503             {
1504                 if( p_input->p->i_slave > 0 )
1505                     SlaveSeek( p_input );
1506
1507                 b_force_update = true;
1508             }
1509             break;
1510         }
1511
1512         case INPUT_CONTROL_SET_TIME:
1513         case INPUT_CONTROL_SET_TIME_OFFSET:
1514         {
1515             int64_t i_time;
1516             int i_ret;
1517
1518             if( p_input->p->b_recording )
1519             {
1520                 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1521                 break;
1522             }
1523
1524             if( i_type == INPUT_CONTROL_SET_TIME )
1525             {
1526                 i_time = val.i_time;
1527             }
1528             else
1529             {
1530                 /* Should not fail */
1531                 demux_Control( p_input->p->input.p_demux,
1532                                 DEMUX_GET_TIME, &i_time );
1533                 i_time += val.i_time;
1534             }
1535             if( i_time < 0 ) i_time = 0;
1536
1537             /* Reset the decoders states and clock sync (before calling the demuxer */
1538             input_EsOutChangePosition( p_input->p->p_es_out );
1539
1540             i_ret = demux_Control( p_input->p->input.p_demux,
1541                                     DEMUX_SET_TIME, i_time );
1542             if( i_ret )
1543             {
1544                 int64_t i_length;
1545
1546                 /* Emulate it with a SET_POS */
1547                 demux_Control( p_input->p->input.p_demux,
1548                                 DEMUX_GET_LENGTH, &i_length );
1549                 if( i_length > 0 )
1550                 {
1551                     double f_pos = (double)i_time / (double)i_length;
1552                     i_ret = demux_Control( p_input->p->input.p_demux,
1553                                             DEMUX_SET_POSITION, f_pos );
1554                 }
1555             }
1556             if( i_ret )
1557             {
1558                 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1559                          " failed or not possible", i_time );
1560             }
1561             else
1562             {
1563                 if( p_input->p->i_slave > 0 )
1564                     SlaveSeek( p_input );
1565
1566                 b_force_update = true;
1567             }
1568             break;
1569         }
1570
1571         case INPUT_CONTROL_SET_STATE:
1572             if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1573                 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1574             {
1575                 int i_ret;
1576                 if( p_input->p->input.p_access )
1577                     i_ret = access_Control( p_input->p->input.p_access,
1578                                              ACCESS_SET_PAUSE_STATE, false );
1579                 else
1580                     i_ret = demux_Control( p_input->p->input.p_demux,
1581                                             DEMUX_SET_PAUSE_STATE, false );
1582
1583                 if( i_ret )
1584                 {
1585                     /* FIXME What to do ? */
1586                     msg_Warn( p_input, "cannot unset pause -> EOF" );
1587                     vlc_mutex_unlock( &p_input->p->lock_control );
1588                     input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1589                     vlc_mutex_lock( &p_input->p->lock_control );
1590                 }
1591
1592                 b_force_update = true;
1593
1594                 /* Switch to play */
1595                 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1596
1597                 /* */
1598                 if( !i_ret )
1599                     input_EsOutChangeState( p_input->p->p_es_out );
1600             }
1601             else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1602                      p_input->p->b_can_pause )
1603             {
1604                 int i_ret, state;
1605                 if( p_input->p->input.p_access )
1606                     i_ret = access_Control( p_input->p->input.p_access,
1607                                              ACCESS_SET_PAUSE_STATE, true );
1608                 else
1609                     i_ret = demux_Control( p_input->p->input.p_demux,
1610                                             DEMUX_SET_PAUSE_STATE, true );
1611
1612                 b_force_update = true;
1613
1614                 if( i_ret )
1615                 {
1616                     msg_Warn( p_input, "cannot set pause state" );
1617                     state = p_input->i_state;
1618                 }
1619                 else
1620                 {
1621                     state = PAUSE_S;
1622                 }
1623
1624                 /* Switch to new state */
1625                 input_ChangeStateWithVarCallback( p_input, state, false );
1626
1627                 /* */
1628                 if( !i_ret )
1629                     input_EsOutChangeState( p_input->p->p_es_out );
1630             }
1631             else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1632             {
1633                 b_force_update = true;
1634
1635                 /* Correct "state" value */
1636                 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1637             }
1638             else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1639             {
1640                 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1641             }
1642             break;
1643
1644         case INPUT_CONTROL_SET_RATE:
1645         case INPUT_CONTROL_SET_RATE_SLOWER:
1646         case INPUT_CONTROL_SET_RATE_FASTER:
1647         {
1648             int i_rate;
1649
1650             if( i_type == INPUT_CONTROL_SET_RATE )
1651             {
1652                 i_rate = val.i_int;
1653             }
1654             else
1655             {
1656                 static const int ppi_factor[][2] = {
1657                     {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1658                     {1,1},
1659                     {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1660                     {0,0}
1661                 };
1662                 int i_error;
1663                 int i_idx;
1664                 int i;
1665
1666                 i_error = INT_MAX;
1667                 i_idx = -1;
1668                 for( i = 0; ppi_factor[i][0] != 0; i++ )
1669                 {
1670                     const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1671                     const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1672                     if( i_test_e < i_error )
1673                     {
1674                         i_idx = i;
1675                         i_error = i_test_e;
1676                     }
1677                 }
1678                 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1679
1680                 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1681                 {
1682                     if( ppi_factor[i_idx+1][0] > 0 )
1683                         i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1684                     else
1685                         i_rate = INPUT_RATE_MAX+1;
1686                 }
1687                 else
1688                 {
1689                     assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1690                     if( i_idx > 0 )
1691                         i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1692                     else
1693                         i_rate = INPUT_RATE_MIN-1;
1694                 }
1695             }
1696
1697             if( i_rate < INPUT_RATE_MIN )
1698             {
1699                 msg_Dbg( p_input, "cannot set rate faster" );
1700                 i_rate = INPUT_RATE_MIN;
1701             }
1702             else if( i_rate > INPUT_RATE_MAX )
1703             {
1704                 msg_Dbg( p_input, "cannot set rate slower" );
1705                 i_rate = INPUT_RATE_MAX;
1706             }
1707             if( i_rate != INPUT_RATE_DEFAULT &&
1708                 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1709                   ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1710             {
1711                 msg_Dbg( p_input, "cannot change rate" );
1712                 i_rate = INPUT_RATE_DEFAULT;
1713             }
1714             if( i_rate != p_input->p->i_rate &&
1715                 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1716             {
1717                 int i_ret;
1718                 if( p_input->p->input.p_access )
1719                     i_ret = VLC_EGENERIC;
1720                 else
1721                     i_ret = demux_Control( p_input->p->input.p_demux,
1722                                             DEMUX_SET_RATE, &i_rate );
1723                 if( i_ret )
1724                 {
1725                     msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1726                     i_rate = p_input->p->i_rate;
1727                 }
1728             }
1729
1730             /* */
1731             if( i_rate != p_input->p->i_rate )
1732             {
1733                 val.i_int = i_rate;
1734                 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1735                 var_SetBool( p_input, "rate-change", true );
1736
1737                 p_input->p->i_rate  = i_rate;
1738
1739                 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1740                 if( p_input->p->input.b_rescale_ts )
1741                     input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1742
1743                 b_force_update = true;
1744             }
1745             break;
1746         }
1747
1748         case INPUT_CONTROL_SET_PROGRAM:
1749             /* No need to force update, es_out does it if needed */
1750             es_out_Control( p_input->p->p_es_out,
1751                             ES_OUT_SET_GROUP, val.i_int );
1752
1753             demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1754                             NULL );
1755             break;
1756
1757         case INPUT_CONTROL_SET_ES:
1758             /* No need to force update, es_out does it if needed */
1759             es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1760                             input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1761             break;
1762
1763         case INPUT_CONTROL_RESTART_ES:
1764             es_out_Control( p_input->p->p_es_out, ES_OUT_RESTART_ES,
1765                             input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1766             break;
1767
1768         case INPUT_CONTROL_SET_AUDIO_DELAY:
1769             input_EsOutSetDelay( p_input->p->p_es_out,
1770                                  AUDIO_ES, val.i_time );
1771             var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1772             break;
1773
1774         case INPUT_CONTROL_SET_SPU_DELAY:
1775             input_EsOutSetDelay( p_input->p->p_es_out,
1776                                  SPU_ES, val.i_time );
1777             var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1778             break;
1779
1780         case INPUT_CONTROL_SET_TITLE:
1781         case INPUT_CONTROL_SET_TITLE_NEXT:
1782         case INPUT_CONTROL_SET_TITLE_PREV:
1783             if( p_input->p->b_recording )
1784             {
1785                 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1786                 break;
1787             }
1788             if( p_input->p->input.b_title_demux &&
1789                 p_input->p->input.i_title > 0 )
1790             {
1791                 /* TODO */
1792                 /* FIXME handle demux title */
1793                 demux_t *p_demux = p_input->p->input.p_demux;
1794                 int i_title;
1795
1796                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1797                     i_title = p_demux->info.i_title - 1;
1798                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1799                     i_title = p_demux->info.i_title + 1;
1800                 else
1801                     i_title = val.i_int;
1802
1803                 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1804                 {
1805                     input_EsOutChangePosition( p_input->p->p_es_out );
1806
1807                     demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1808                     input_ControlVarTitle( p_input, i_title );
1809                 }
1810             }
1811             else if( p_input->p->input.i_title > 0 )
1812             {
1813                 access_t *p_access = p_input->p->input.p_access;
1814                 int i_title;
1815
1816                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1817                     i_title = p_access->info.i_title - 1;
1818                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1819                     i_title = p_access->info.i_title + 1;
1820                 else
1821                     i_title = val.i_int;
1822
1823                 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1824                 {
1825                     input_EsOutChangePosition( p_input->p->p_es_out );
1826
1827                     access_Control( p_access, ACCESS_SET_TITLE, i_title );
1828                     stream_AccessReset( p_input->p->input.p_stream );
1829                 }
1830             }
1831             break;
1832         case INPUT_CONTROL_SET_SEEKPOINT:
1833         case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1834         case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1835             if( p_input->p->b_recording )
1836             {
1837                 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1838                 break;
1839             }
1840
1841             if( p_input->p->input.b_title_demux &&
1842                 p_input->p->input.i_title > 0 )
1843             {
1844                 demux_t *p_demux = p_input->p->input.p_demux;
1845                 int i_seekpoint;
1846                 int64_t i_input_time;
1847                 int64_t i_seekpoint_time;
1848
1849                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1850                 {
1851                     i_seekpoint = p_demux->info.i_seekpoint;
1852                     i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1853                     if( i_seekpoint_time >= 0 &&
1854                          !demux_Control( p_demux,
1855                                           DEMUX_GET_TIME, &i_input_time ) )
1856                     {
1857                         if( i_input_time < i_seekpoint_time + 3000000 )
1858                             i_seekpoint--;
1859                     }
1860                     else
1861                         i_seekpoint--;
1862                 }
1863                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1864                     i_seekpoint = p_demux->info.i_seekpoint + 1;
1865                 else
1866                     i_seekpoint = val.i_int;
1867
1868                 if( i_seekpoint >= 0 && i_seekpoint <
1869                     p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1870                 {
1871
1872                     input_EsOutChangePosition( p_input->p->p_es_out );
1873
1874                     demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1875                 }
1876             }
1877             else if( p_input->p->input.i_title > 0 )
1878             {
1879                 demux_t *p_demux = p_input->p->input.p_demux;
1880                 access_t *p_access = p_input->p->input.p_access;
1881                 int i_seekpoint;
1882                 int64_t i_input_time;
1883                 int64_t i_seekpoint_time;
1884
1885                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1886                 {
1887                     i_seekpoint = p_access->info.i_seekpoint;
1888                     i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1889                     if( i_seekpoint_time >= 0 &&
1890                         demux_Control( p_demux,
1891                                         DEMUX_GET_TIME, &i_input_time ) )
1892                     {
1893                         if( i_input_time < i_seekpoint_time + 3000000 )
1894                             i_seekpoint--;
1895                     }
1896                     else
1897                         i_seekpoint--;
1898                 }
1899                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1900                     i_seekpoint = p_access->info.i_seekpoint + 1;
1901                 else
1902                     i_seekpoint = val.i_int;
1903
1904                 if( i_seekpoint >= 0 && i_seekpoint <
1905                     p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1906                 {
1907                     input_EsOutChangePosition( p_input->p->p_es_out );
1908
1909                     access_Control( p_access, ACCESS_SET_SEEKPOINT,
1910                                     i_seekpoint );
1911                     stream_AccessReset( p_input->p->input.p_stream );
1912                 }
1913             }
1914             break;
1915
1916         case INPUT_CONTROL_ADD_SUBTITLE:
1917             if( val.psz_string )
1918             {
1919                 SubtitleAdd( p_input, val.psz_string, true );
1920                 free( val.psz_string );
1921             }
1922             break;
1923
1924         case INPUT_CONTROL_ADD_SLAVE:
1925             if( val.psz_string )
1926             {
1927                 input_source_t *slave = InputSourceNew( p_input );
1928
1929                 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1930                 {
1931                     vlc_meta_t *p_meta;
1932                     int64_t i_time;
1933
1934                     /* Add the slave */
1935                     msg_Dbg( p_input, "adding %s as slave on the fly",
1936                              val.psz_string );
1937
1938                     /* Set position */
1939                     if( demux_Control( p_input->p->input.p_demux,
1940                                         DEMUX_GET_TIME, &i_time ) )
1941                     {
1942                         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1943                         InputSourceClean( slave );
1944                         free( slave );
1945                         break;
1946                     }
1947                     if( demux_Control( slave->p_demux,
1948                                        DEMUX_SET_TIME, i_time ) )
1949                     {
1950                         msg_Err( p_input, "seek failed for new slave" );
1951                         InputSourceClean( slave );
1952                         free( slave );
1953                         break;
1954                     }
1955
1956                     /* Get meta (access and demux) */
1957                     p_meta = vlc_meta_New();
1958                     access_Control( slave->p_access, ACCESS_GET_META,
1959                                      p_meta );
1960                     demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1961                     InputUpdateMeta( p_input, p_meta );
1962
1963                     TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1964                 }
1965                 else
1966                 {
1967                     free( slave );
1968                     msg_Warn( p_input, "failed to add %s as slave",
1969                               val.psz_string );
1970                 }
1971
1972                 free( val.psz_string );
1973             }
1974             break;
1975
1976         case INPUT_CONTROL_SET_RECORD_STATE:
1977             if( !!p_input->p->b_recording != !!val.b_bool )
1978             {
1979                 if( p_input->p->input.b_can_stream_record )
1980                 {
1981                     if( demux_Control( p_input->p->input.p_demux,
1982                                        DEMUX_SET_RECORD_STATE, val.b_bool ) )
1983                         val.b_bool = false;
1984                 }
1985                 else
1986                 {
1987                     if( input_EsOutSetRecord( p_input->p->p_es_out, val.b_bool ) )
1988                         val.b_bool = false;
1989                 }
1990                 p_input->p->b_recording = val.b_bool;
1991
1992                 var_Change( p_input, "record", VLC_VAR_SETVALUE, &val, NULL );
1993
1994                 b_force_update = true;
1995             }
1996             break;
1997
1998         case INPUT_CONTROL_SET_BOOKMARK:
1999         default:
2000             msg_Err( p_input, "not yet implemented" );
2001             break;
2002     }
2003
2004     return b_force_update;
2005 }
2006
2007 /*****************************************************************************
2008  * UpdateFromDemux:
2009  *****************************************************************************/
2010 static int UpdateFromDemux( input_thread_t *p_input )
2011 {
2012     demux_t *p_demux = p_input->p->input.p_demux;
2013     vlc_value_t v;
2014
2015     if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2016     {
2017         v.i_int = p_demux->info.i_title;
2018         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2019
2020         input_ControlVarTitle( p_input, p_demux->info.i_title );
2021
2022         p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2023     }
2024     if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2025     {
2026         v.i_int = p_demux->info.i_seekpoint;
2027         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2028
2029         p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2030     }
2031     p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2032
2033     /* Hmmm only works with master input */
2034     if( p_input->p->input.p_demux == p_demux )
2035     {
2036         int i_title_end = p_input->p->input.i_title_end -
2037             p_input->p->input.i_title_offset;
2038         int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2039             p_input->p->input.i_seekpoint_offset;
2040
2041         if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2042         {
2043             if( p_demux->info.i_title > i_title_end ||
2044                 ( p_demux->info.i_title == i_title_end &&
2045                   p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2046         }
2047         else if( i_seekpoint_end >=0 )
2048         {
2049             if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2050         }
2051         else if( i_title_end >= 0 )
2052         {
2053             if( p_demux->info.i_title > i_title_end ) return 0;
2054         }
2055     }
2056
2057     return 1;
2058 }
2059
2060 /*****************************************************************************
2061  * UpdateFromAccess:
2062  *****************************************************************************/
2063 static int UpdateFromAccess( input_thread_t *p_input )
2064 {
2065     access_t *p_access = p_input->p->input.p_access;
2066     vlc_value_t v;
2067
2068     if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2069     {
2070         v.i_int = p_access->info.i_title;
2071         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2072
2073         input_ControlVarTitle( p_input, p_access->info.i_title );
2074
2075         stream_AccessUpdate( p_input->p->input.p_stream );
2076
2077         p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2078     }
2079     if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2080     {
2081         v.i_int = p_access->info.i_seekpoint;
2082         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2083         p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2084     }
2085     if( p_access->info.i_update & INPUT_UPDATE_META )
2086     {
2087         /* TODO maybe multi - access ? */
2088         vlc_meta_t *p_meta = vlc_meta_New();
2089         access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2090         InputUpdateMeta( p_input, p_meta );
2091         p_access->info.i_update &= ~INPUT_UPDATE_META;
2092     }
2093     if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2094     {
2095         double f_quality;
2096         double f_strength;
2097
2098         if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2099             f_quality = f_strength = -1;
2100
2101         var_SetFloat( p_input, "signal-quality", f_quality );
2102         var_SetFloat( p_input, "signal-strength", f_strength );
2103
2104         p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2105     }
2106
2107     p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2108
2109     /* Hmmm only works with master input */
2110     if( p_input->p->input.p_access == p_access )
2111     {
2112         int i_title_end = p_input->p->input.i_title_end -
2113             p_input->p->input.i_title_offset;
2114         int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2115             p_input->p->input.i_seekpoint_offset;
2116
2117         if( i_title_end >= 0 && i_seekpoint_end >=0 )
2118         {
2119             if( p_access->info.i_title > i_title_end ||
2120                 ( p_access->info.i_title == i_title_end &&
2121                   p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2122         }
2123         else if( i_seekpoint_end >=0 )
2124         {
2125             if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2126         }
2127         else if( i_title_end >= 0 )
2128         {
2129             if( p_access->info.i_title > i_title_end ) return 0;
2130         }
2131     }
2132
2133     return 1;
2134 }
2135
2136 /*****************************************************************************
2137  * UpdateItemLength:
2138  *****************************************************************************/
2139 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2140 {
2141     input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2142 }
2143
2144 /*****************************************************************************
2145  * InputSourceNew:
2146  *****************************************************************************/
2147 static input_source_t *InputSourceNew( input_thread_t *p_input )
2148 {
2149     VLC_UNUSED(p_input);
2150     input_source_t *in = malloc( sizeof( input_source_t ) );
2151     if( in )
2152         memset( in, 0, sizeof( input_source_t ) );
2153     return in;
2154 }
2155
2156 /*****************************************************************************
2157  * InputSourceInit:
2158  *****************************************************************************/
2159 static int InputSourceInit( input_thread_t *p_input,
2160                             input_source_t *in, const char *psz_mrl,
2161                             const char *psz_forced_demux )
2162 {
2163     const bool b_master = in == &p_input->p->input;
2164
2165     char psz_dup[strlen(psz_mrl) + 1];
2166     const char *psz_access;
2167     const char *psz_demux;
2168     char *psz_path;
2169     char *psz_tmp;
2170     char *psz;
2171     vlc_value_t val;
2172     double f_fps;
2173
2174     strcpy( psz_dup, psz_mrl );
2175
2176     if( !in ) return VLC_EGENERIC;
2177     if( !p_input ) return VLC_EGENERIC;
2178
2179     /* Split uri */
2180     input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2181
2182     msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2183              psz_mrl, psz_access, psz_demux, psz_path );
2184     if( !p_input->b_preparsing )
2185     {
2186         /* Hack to allow udp://@:port syntax */
2187         if( !psz_access ||
2188             (strncmp( psz_access, "udp", 3 ) &&
2189              strncmp( psz_access, "rtp", 3 )) )
2190         {
2191             /* Find optional titles and seekpoints */
2192             MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2193                      &in->i_seekpoint_start, &in->i_seekpoint_end );
2194         }
2195
2196         if( psz_forced_demux && *psz_forced_demux )
2197         {
2198             psz_demux = psz_forced_demux;
2199         }
2200         else if( *psz_demux == '\0' )
2201         {
2202             /* special hack for forcing a demuxer with --demux=module
2203              * (and do nothing with a list) */
2204             char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2205
2206             if( psz_var_demux != NULL &&
2207                 !strchr(psz_var_demux, ',' ) &&
2208                 !strchr(psz_var_demux, ':' ) )
2209             {
2210                 psz_demux = psz_var_demux;
2211
2212                 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2213             }
2214         }
2215
2216         /* Try access_demux first */
2217         in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2218                                   NULL, p_input->p->p_es_out, false );
2219     }
2220     else
2221     {
2222         /* Preparsing is only for file:// */
2223         if( *psz_demux )
2224             goto error;
2225         if( !*psz_access ) /* path without scheme:// */
2226             psz_access = "file";
2227         if( strcmp( psz_access, "file" ) )
2228             goto error;
2229         msg_Dbg( p_input, "trying to pre-parse %s",  psz_path );
2230     }
2231
2232     if( in->p_demux )
2233     {
2234         int64_t i_pts_delay;
2235
2236         /* Get infos from access_demux */
2237         demux_Control( in->p_demux,
2238                         DEMUX_GET_PTS_DELAY, &i_pts_delay );
2239         p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2240
2241         in->b_title_demux = true;
2242         if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2243                             &in->title, &in->i_title,
2244                             &in->i_title_offset, &in->i_seekpoint_offset ) )
2245         {
2246             TAB_INIT( in->i_title, in->title );
2247         }
2248         if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2249                             &in->b_can_pace_control ) )
2250             in->b_can_pace_control = false;
2251
2252         if( !in->b_can_pace_control )
2253         {
2254             if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2255                                 &in->b_can_rate_control, &in->b_rescale_ts ) )
2256             {
2257                 in->b_can_rate_control = false;
2258                 in->b_rescale_ts = true; /* not used */
2259             }
2260         }
2261         else
2262         {
2263             in->b_can_rate_control = true;
2264             in->b_rescale_ts = true;
2265         }
2266         if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2267                             &in->b_can_pause ) )
2268             in->b_can_pause = false;
2269         var_SetBool( p_input, "can-pause", in->b_can_pause );
2270
2271         int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2272                         &val.b_bool );
2273         if( ret != VLC_SUCCESS )
2274             val.b_bool = false;
2275         var_Set( p_input, "seekable", val );
2276     }
2277     else
2278     {
2279         int64_t i_pts_delay;
2280
2281         if( b_master )
2282             input_ChangeState( p_input, OPENING_S );
2283
2284         /* Now try a real access */
2285         in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2286
2287         /* Access failed, URL encoded ? */
2288         if( in->p_access == NULL && strchr( psz_path, '%' ) )
2289         {
2290             decode_URI( psz_path );
2291
2292             msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2293                      psz_access, psz_demux, psz_path );
2294
2295             in->p_access = access_New( p_input,
2296                                         psz_access, psz_demux, psz_path );
2297         }
2298         if( in->p_access == NULL )
2299         {
2300             msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2301                                                          msg_StackMsg() );
2302             intf_UserFatal( VLC_OBJECT( p_input), false,
2303                             _("Your input can't be opened"),
2304                             _("VLC is unable to open the MRL '%s'."
2305                             " Check the log for details."), psz_mrl );
2306             goto error;
2307         }
2308
2309         /* */
2310         psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2311         while( psz && *psz )
2312         {
2313             access_t *p_access = in->p_access;
2314             char *end = strchr( psz, ':' );
2315
2316             if( end )
2317                 *end++ = '\0';
2318
2319             in->p_access = access_FilterNew( in->p_access, psz );
2320             if( in->p_access == NULL )
2321             {
2322                 in->p_access = p_access;
2323                 msg_Warn( p_input, "failed to insert access filter %s",
2324                           psz );
2325             }
2326
2327             psz = end;
2328         }
2329         free( psz_tmp );
2330
2331         /* Get infos from access */
2332         if( !p_input->b_preparsing )
2333         {
2334             access_Control( in->p_access,
2335                              ACCESS_GET_PTS_DELAY, &i_pts_delay );
2336             p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2337
2338             in->b_title_demux = false;
2339             if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2340                                  &in->title, &in->i_title,
2341                                 &in->i_title_offset, &in->i_seekpoint_offset ) )
2342
2343             {
2344                 TAB_INIT( in->i_title, in->title );
2345             }
2346             access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2347                              &in->b_can_pace_control );
2348             in->b_can_rate_control = in->b_can_pace_control;
2349             in->b_rescale_ts = true;
2350
2351             access_Control( in->p_access, ACCESS_CAN_PAUSE,
2352                              &in->b_can_pause );
2353             var_SetBool( p_input, "can-pause", in->b_can_pause );
2354             access_Control( in->p_access, ACCESS_CAN_SEEK,
2355                              &val.b_bool );
2356             var_Set( p_input, "seekable", val );
2357         }
2358
2359         if( b_master )
2360             input_ChangeState( p_input, BUFFERING_S );
2361
2362         /* Autodetect extra files if none specified */
2363         char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2364         if( !psz_input_list )
2365         {
2366             char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2367             if( psz_extra_files )
2368                 var_SetString( p_input, "input-list", psz_extra_files );
2369             free( psz_extra_files );
2370         }
2371
2372         /* Create the stream_t */
2373         in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2374
2375         /* Restor old value */
2376         if( !psz_input_list )
2377             var_SetString( p_input, "input-list", "" );
2378         free( psz_input_list );
2379
2380         if( in->p_stream == NULL )
2381         {
2382             msg_Warn( p_input, "cannot create a stream_t from access" );
2383             goto error;
2384         }
2385
2386         /* Open a demuxer */
2387         if( *psz_demux == '\0' && *in->p_access->psz_demux )
2388         {
2389             psz_demux = in->p_access->psz_demux;
2390         }
2391
2392         {
2393             /* Take access redirections into account */
2394             char *psz_real_path;
2395             char *psz_buf = NULL;
2396             if( in->p_access->psz_path )
2397             {
2398                 const char *psz_a, *psz_d;
2399                 psz_buf = strdup( in->p_access->psz_path );
2400                 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2401             }
2402             else
2403             {
2404                 psz_real_path = psz_path;
2405             }
2406             in->p_demux = demux_New( p_input, psz_access, psz_demux,
2407                                       psz_real_path,
2408                                       in->p_stream, p_input->p->p_es_out,
2409                                       p_input->b_preparsing );
2410             free( psz_buf );
2411         }
2412
2413         if( in->p_demux == NULL )
2414         {
2415             msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2416                      psz_access, psz_demux, psz_path );
2417             intf_UserFatal( VLC_OBJECT( p_input ), false,
2418                             _("VLC can't recognize the input's format"),
2419                             _("The format of '%s' cannot be detected. "
2420                             "Have a look at the log for details."), psz_mrl );
2421             goto error;
2422         }
2423
2424         /* Get title from demux */
2425         if( !p_input->b_preparsing && in->i_title <= 0 )
2426         {
2427             if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2428                                 &in->title, &in->i_title,
2429                                 &in->i_title_offset, &in->i_seekpoint_offset ))
2430             {
2431                 TAB_INIT( in->i_title, in->title );
2432             }
2433             else
2434             {
2435                 in->b_title_demux = true;
2436             }
2437         }
2438     }
2439
2440     /* Set record capabilities */
2441     if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2442         in->b_can_stream_record = false;
2443 #ifdef ENABLE_SOUT
2444     if( !var_CreateGetBool( p_input, "input-record-native" ) )
2445         in->b_can_stream_record = false;
2446     var_SetBool( p_input, "can-record", true );
2447 #else
2448     var_SetBool( p_input, "can-record", in->b_can_stream_record );
2449 #endif
2450
2451     /* get attachment
2452      * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2453     if( 1 || !p_input->b_preparsing )
2454     {
2455         int i_attachment;
2456         input_attachment_t **attachment;
2457         if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2458                              &attachment, &i_attachment ) )
2459         {
2460             vlc_mutex_lock( &p_input->p->input.p_item->lock );
2461             AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2462                               i_attachment, attachment );
2463             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2464         }
2465     }
2466     if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2467     {
2468         vlc_mutex_lock( &p_input->p->input.p_item->lock );
2469         in->f_fps = f_fps;
2470         vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2471     }
2472
2473     if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2474         in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2475
2476     return VLC_SUCCESS;
2477
2478 error:
2479     if( b_master )
2480         input_ChangeState( p_input, ERROR_S );
2481
2482     if( in->p_demux )
2483         demux_Delete( in->p_demux );
2484
2485     if( in->p_stream )
2486         stream_Delete( in->p_stream );
2487
2488     if( in->p_access )
2489         access_Delete( in->p_access );
2490
2491     return VLC_EGENERIC;
2492 }
2493
2494 /*****************************************************************************
2495  * InputSourceClean:
2496  *****************************************************************************/
2497 static void InputSourceClean( input_source_t *in )
2498 {
2499     int i;
2500
2501     if( in->p_demux )
2502         demux_Delete( in->p_demux );
2503
2504     if( in->p_stream )
2505         stream_Delete( in->p_stream );
2506
2507     if( in->p_access )
2508         access_Delete( in->p_access );
2509
2510     if( in->i_title > 0 )
2511     {
2512         for( i = 0; i < in->i_title; i++ )
2513             vlc_input_title_Delete( in->title[i] );
2514         TAB_CLEAN( in->i_title, in->title );
2515     }
2516 }
2517
2518 static void SlaveDemux( input_thread_t *p_input )
2519 {
2520     int64_t i_time;
2521     int i;
2522     bool b_set_time = true;
2523
2524     if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2525     {
2526         /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2527         b_set_time = false;
2528     }
2529
2530     for( i = 0; i < p_input->p->i_slave; i++ )
2531     {
2532         input_source_t *in = p_input->p->slave[i];
2533         int i_ret = 1;
2534
2535         if( in->b_eof )
2536             continue;
2537
2538         if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2539         {
2540             for( ;; )
2541             {
2542                 int64_t i_stime;
2543                 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2544                 {
2545                     msg_Err( p_input, "slave[%d] doesn't like "
2546                              "DEMUX_GET_TIME -> EOF", i );
2547                     i_ret = 0;
2548                     break;
2549                 }
2550
2551                 if( i_stime >= i_time )
2552                     break;
2553
2554                 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2555                     break;
2556             }
2557         }
2558         else
2559         {
2560             i_ret = in->p_demux->pf_demux( in->p_demux );
2561         }
2562
2563         if( i_ret <= 0 )
2564         {
2565             msg_Dbg( p_input, "slave %d EOF", i );
2566             in->b_eof = true;
2567         }
2568     }
2569 }
2570
2571 static void SlaveSeek( input_thread_t *p_input )
2572 {
2573     int64_t i_time;
2574     int i;
2575
2576     if( !p_input ) return;
2577
2578     if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2579     {
2580         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2581         return;
2582     }
2583
2584     for( i = 0; i < p_input->p->i_slave; i++ )
2585     {
2586         input_source_t *in = p_input->p->slave[i];
2587
2588         if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2589         {
2590             if( !in->b_eof )
2591                 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2592             in->b_eof = true;
2593         }
2594         else
2595         {
2596             in->b_eof = false;
2597         }
2598     }
2599 }
2600
2601 /*****************************************************************************
2602  * InputMetaUser:
2603  *****************************************************************************/
2604 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2605 {
2606     vlc_value_t val;
2607
2608     if( !p_meta ) return;
2609
2610     /* Get meta information from user */
2611 #define GET_META( field, s ) \
2612     var_Get( p_input, (s), &val );  \
2613     if( *val.psz_string ) \
2614         vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2615     free( val.psz_string )
2616
2617     GET_META( Title, "meta-title" );
2618     GET_META( Artist, "meta-artist" );
2619     GET_META( Genre, "meta-genre" );
2620     GET_META( Copyright, "meta-copyright" );
2621     GET_META( Description, "meta-description" );
2622     GET_META( Date, "meta-date" );
2623     GET_META( URL, "meta-url" );
2624 #undef GET_META
2625 }
2626
2627 /*****************************************************************************
2628  * InputGetExtraFiles
2629  *  Autodetect extra input list
2630  *****************************************************************************/
2631 static char *InputGetExtraFiles( input_thread_t *p_input,
2632                                  const char *psz_access, const char *psz_path )
2633 {
2634     char *psz_list = NULL;
2635
2636     if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2637         return NULL;
2638
2639
2640     const char *psz_ext = strrchr( psz_path, '.' );
2641     if( !psz_ext || strcmp( psz_ext, ".001" ) )
2642         return NULL;
2643
2644     char *psz_file = strdup( psz_path );
2645     if( !psz_file )
2646         return NULL;
2647
2648     /* Try to list .xyz files */
2649     for( int i = 2; i < 999; i++ )
2650     {
2651         char *psz_ext = strrchr( psz_file, '.' );
2652         struct stat st;
2653
2654         snprintf( psz_ext, 5, ".%.3d", i );
2655
2656         if( utf8_stat( psz_file, &st )
2657          || !S_ISREG( st.st_mode ) || !st.st_size )
2658             continue;
2659
2660         msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2661
2662         if( psz_list )
2663         {
2664             char *psz_old = psz_list;
2665             /* FIXME how to handle file with ',' ?*/
2666             if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2667             {
2668                 psz_list = psz_old;
2669                 break;
2670             }
2671         }
2672         else
2673         {
2674             psz_list = strdup( psz_file );
2675         }
2676     }
2677     free( psz_file );
2678
2679     return psz_list;
2680 }
2681
2682 /*****************************************************************************
2683  * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2684  * arturl and locking issue.
2685  *****************************************************************************/
2686 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2687 {
2688     input_item_t *p_item = p_input->p->input.p_item;
2689     char * psz_arturl = NULL;
2690     char *psz_title = NULL;
2691     int i_arturl_event = false;
2692
2693     if( !p_meta )
2694         return;
2695
2696     psz_arturl = input_item_GetArtURL( p_item );
2697
2698     vlc_mutex_lock( &p_item->lock );
2699     if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2700         psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2701
2702     vlc_meta_Merge( p_item->p_meta, p_meta );
2703
2704     if( psz_arturl && *psz_arturl )
2705     {
2706         vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2707         i_arturl_event = true;
2708     }
2709
2710     vlc_meta_Delete( p_meta );
2711
2712     if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2713     {
2714         /* Don't look for art cover if sout
2715          * XXX It can change when sout has meta data support */
2716         if( p_input->p->p_sout && !p_input->b_preparsing )
2717         {
2718             vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2719             i_arturl_event = true;
2720
2721         }
2722         else
2723             input_ExtractAttachmentAndCacheArt( p_input );
2724     }
2725     free( psz_arturl );
2726
2727     /* A bit ugly */
2728     p_meta = NULL;
2729     if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2730     {
2731         p_meta = vlc_meta_New();
2732         vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2733     }
2734     vlc_mutex_unlock( &p_item->lock );
2735
2736     input_item_SetPreparsed( p_item, true );
2737
2738     if( i_arturl_event == true )
2739     {
2740         vlc_event_t event;
2741
2742         /* Notify interested third parties */
2743         event.type = vlc_InputItemMetaChanged;
2744         event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2745         vlc_event_send( &p_item->event_manager, &event );
2746     }
2747
2748     if( psz_title )
2749     {
2750         input_Control( p_input, INPUT_SET_NAME, psz_title );
2751         free( psz_title );
2752     }
2753
2754     /** \todo handle sout meta */
2755 }
2756
2757
2758 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2759                               int i_new, input_attachment_t **pp_new )
2760 {
2761     int i_attachment = *pi_attachment;
2762     input_attachment_t **attachment = *ppp_attachment;
2763     int i;
2764
2765     attachment = realloc( attachment,
2766                           sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2767     for( i = 0; i < i_new; i++ )
2768         attachment[i_attachment++] = pp_new[i];
2769     free( pp_new );
2770
2771     /* */
2772     *pi_attachment = i_attachment;
2773     *ppp_attachment = attachment;
2774 }
2775
2776 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2777 {
2778     int i;
2779
2780     if( p_input->b_preparsing )
2781         return;
2782
2783     if( p_input->p->input.p_access )
2784         access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2785                          p_meta );
2786
2787     /* Get meta data from slave input */
2788     for( i = 0; i < p_input->p->i_slave; i++ )
2789     {
2790         DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2791         if( p_input->p->slave[i]->p_access )
2792         {
2793             access_Control( p_input->p->slave[i]->p_access,
2794                              ACCESS_GET_META, p_meta );
2795         }
2796     }
2797 }
2798
2799 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2800 {
2801     bool b_bool;
2802     module_t *p_id3;
2803
2804
2805 #if 0
2806     /* XXX I am not sure it is a great idea, besides, there is more than that
2807      * if we want to do it right */
2808     vlc_mutex_lock( &p_item->lock );
2809     if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2810     {
2811         vlc_mutex_unlock( &p_item->lock );
2812         return;
2813     }
2814     vlc_mutex_unlock( &p_item->lock );
2815 #endif
2816
2817     demux_Control( p_demux, DEMUX_GET_META, p_meta );
2818     if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2819         return;
2820     if( !b_bool )
2821         return;
2822
2823     p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2824     if(! p_demux->p_private )
2825         return;
2826
2827     p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2828     if( p_id3 )
2829     {
2830         demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2831
2832         if( p_demux_meta->p_meta )
2833         {
2834             vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2835             vlc_meta_Delete( p_demux_meta->p_meta );
2836         }
2837
2838         if( p_demux_meta->i_attachments > 0 )
2839         {
2840             vlc_mutex_lock( &p_input->p->input.p_item->lock );
2841             AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2842                               p_demux_meta->i_attachments, p_demux_meta->attachments );
2843             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2844         }
2845         module_unneed( p_demux, p_id3 );
2846     }
2847     free( p_demux->p_private );
2848 }
2849
2850
2851 /*****************************************************************************
2852  * MRLSplit: parse the access, demux and url part of the
2853  *           Media Resource Locator.
2854  *****************************************************************************/
2855 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2856                      char *psz_dup )
2857 {
2858     char *psz_access = NULL;
2859     char *psz_demux  = NULL;
2860     char *psz_path;
2861
2862     /* Either there is an access/demux specification before ://
2863      * or we have a plain local file path. */
2864     psz_path = strstr( psz_dup, "://" );
2865     if( psz_path != NULL )
2866     {
2867         *psz_path = '\0';
2868         psz_path += 3; /* skips "://" */
2869
2870         /* Separate access from demux (<access>/<demux>://<path>) */
2871         psz_access = psz_dup;
2872         psz_demux = strchr( psz_access, '/' );
2873         if( psz_demux )
2874             *psz_demux++ = '\0';
2875
2876         /* We really don't want module name substitution here! */
2877         if( psz_access[0] == '$' )
2878             psz_access++;
2879         if( psz_demux && psz_demux[0] == '$' )
2880             psz_demux++;
2881     }
2882     else
2883     {
2884         psz_path = psz_dup;
2885     }
2886     *ppsz_access = psz_access ? psz_access : (char*)"";
2887     *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2888     *ppsz_path = psz_path;
2889 }
2890
2891 static inline bool next(char ** src)
2892 {
2893     char *end;
2894     errno = 0;
2895     long result = strtol( *src, &end, 0 );
2896     if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2897         end == *src )
2898     {
2899         return false;
2900     }
2901     *src = end;
2902     return true;
2903 }
2904
2905 /*****************************************************************************
2906  * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2907  *
2908  * Syntax:
2909  * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2910  *****************************************************************************/
2911 static void MRLSections( input_thread_t *p_input, char *psz_source,
2912                          int *pi_title_start, int *pi_title_end,
2913                          int *pi_chapter_start, int *pi_chapter_end )
2914 {
2915     char *psz, *psz_end, *psz_next, *psz_check;
2916
2917     *pi_title_start = *pi_title_end = -1;
2918     *pi_chapter_start = *pi_chapter_end = -1;
2919
2920     /* Start by parsing titles and chapters */
2921     if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2922
2923
2924     /* Check we are really dealing with a title/chapter section */
2925     psz_check = psz + 1;
2926     if( !*psz_check ) return;
2927     if( isdigit(*psz_check) )
2928         if(!next(&psz_check)) return;
2929     if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2930     if( *psz_check == ':' && ++psz_check )
2931     {
2932         if( isdigit(*psz_check) )
2933             if(!next(&psz_check)) return;
2934     }
2935     if( *psz_check != '-' && *psz_check ) return;
2936     if( *psz_check == '-' && ++psz_check )
2937     {
2938         if( isdigit(*psz_check) )
2939             if(!next(&psz_check)) return;
2940     }
2941     if( *psz_check != ':' && *psz_check ) return;
2942     if( *psz_check == ':' && ++psz_check )
2943     {
2944         if( isdigit(*psz_check) )
2945             if(!next(&psz_check)) return;
2946     }
2947     if( *psz_check ) return;
2948
2949     /* Separate start and end */
2950     *psz++ = 0;
2951     if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2952
2953     /* Look for the start title */
2954     *pi_title_start = strtol( psz, &psz_next, 0 );
2955     if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2956     *pi_title_end = *pi_title_start;
2957     psz = psz_next;
2958
2959     /* Look for the start chapter */
2960     if( *psz ) psz++;
2961     *pi_chapter_start = strtol( psz, &psz_next, 0 );
2962     if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2963     *pi_chapter_end = *pi_chapter_start;
2964
2965     if( psz_end )
2966     {
2967         /* Look for the end title */
2968         *pi_title_end = strtol( psz_end, &psz_next, 0 );
2969         if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2970         psz_end = psz_next;
2971
2972         /* Look for the end chapter */
2973         if( *psz_end ) psz_end++;
2974         *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2975         if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2976     }
2977
2978     msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2979              psz_source, *pi_title_start, *pi_chapter_start,
2980              *pi_title_end, *pi_chapter_end );
2981 }
2982
2983 /*****************************************************************************
2984  * input_AddSubtitles: add a subtitles file and enable it
2985  *****************************************************************************/
2986 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
2987 {
2988     input_source_t *sub;
2989     vlc_value_t count;
2990     vlc_value_t list;
2991     char *psz_path, *psz_extension;
2992
2993     /* if we are provided a subtitle.sub file,
2994      * see if we don't have a subtitle.idx and use it instead */
2995     psz_path = strdup( psz_subtitle );
2996     if( psz_path )
2997     {
2998         psz_extension = strrchr( psz_path, '.');
2999         if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3000         {
3001             struct stat st;
3002
3003             strcpy( psz_extension, ".idx" );
3004
3005             if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3006             {
3007                 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3008                          psz_path, psz_subtitle );
3009                 strcpy( psz_subtitle, psz_path );
3010             }
3011         }
3012         free( psz_path );
3013     }
3014
3015     var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3016
3017     sub = InputSourceNew( p_input );
3018     if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3019     {
3020         free( sub );
3021         return;
3022     }
3023     TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3024
3025     /* Select the ES */
3026     if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3027     {
3028         if( count.i_int == 0 )
3029             count.i_int++;
3030         /* if it was first one, there is disable too */
3031
3032         if( count.i_int < list.p_list->i_count )
3033         {
3034             int i_id = list.p_list->p_values[count.i_int].i_int;
3035             es_out_id_t *p_es = input_EsOutGetFromID( p_input->p->p_es_out, i_id );
3036
3037             es_out_Control( p_input->p->p_es_out, ES_OUT_SET_DEFAULT, p_es );
3038             es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES, p_es );
3039         }
3040         var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3041     }
3042 }
3043
3044 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3045                                bool b_check_extension )
3046 {
3047     vlc_value_t val;
3048
3049     if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3050         return false;
3051
3052     assert( psz_subtitle != NULL );
3053
3054     val.psz_string = strdup( psz_subtitle );
3055     if( val.psz_string )
3056         input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3057     return true;
3058 }
3059
3060 /*****************************************************************************
3061  * input_get_event_manager
3062  *****************************************************************************/
3063 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3064 {
3065     return &p_input->p->event_manager;
3066 }
3067
3068 /**/
3069 /* TODO FIXME nearly the same logic that snapshot code */
3070 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3071 {
3072     char *psz_file;
3073     DIR *path;
3074
3075     path = utf8_opendir( psz_path );
3076     if( path )
3077     {
3078         closedir( path );
3079
3080         char *psz_tmp = str_format( p_obj, psz_prefix );
3081         if( !psz_tmp )
3082             return NULL;
3083
3084         filename_sanitize( psz_tmp );
3085         if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3086                       psz_path, psz_tmp,
3087                       psz_extension ? "." : "",
3088                       psz_extension ? psz_extension : "" ) < 0 )
3089             psz_file = NULL;
3090         free( psz_tmp );
3091         return psz_file;
3092     }
3093     else
3094     {
3095         psz_file = str_format( p_obj, psz_path );
3096         path_sanitize( psz_file );
3097         return psz_file;
3098     }
3099 }
3100