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