]> git.sesse.net Git - vlc/blob - src/input/input.c
3f51cf58b6746d49a816e4aa1d0e330af5642ec5
[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(60000000)
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_t)(1000000.0
922                                      * var_GetFloat( p_input, "start-time" ));
923     p_input->p->i_stop  = (int64_t)(1000000.0
924                                      * var_GetFloat( p_input, "stop-time" ));
925     p_input->p->i_run   = (int64_t)(1000000.0
926                                      * var_GetFloat( p_input, "run-time" ));
927     if( p_input->p->i_run < 0 )
928     {
929         msg_Warn( p_input, "invalid run-time ignored" );
930         p_input->p->i_run = 0;
931     }
932
933     const mtime_t i_length = var_GetTime( p_input, "length" );
934     if( p_input->p->i_start > 0 )
935     {
936         if( p_input->p->i_start >= i_length )
937         {
938             msg_Warn( p_input, "invalid start-time ignored" );
939         }
940         else
941         {
942             vlc_value_t s;
943
944             msg_Dbg( p_input, "starting at time: %ds",
945                               (int)( p_input->p->i_start / INT64_C(1000000) ) );
946
947             s.i_time = p_input->p->i_start;
948             input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
949         }
950     }
951     if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
952     {
953         msg_Warn( p_input, "invalid stop-time ignored" );
954         p_input->p->i_stop = 0;
955     }
956     p_input->p->b_fast_seek = var_GetBool( p_input, "input-fast-seek" );
957 }
958
959 static void LoadSubtitles( input_thread_t *p_input )
960 {
961     /* Load subtitles */
962     /* Get fps and set it if not already set */
963     const double f_fps = p_input->p->f_fps;
964     if( f_fps > 1.0 )
965     {
966         float f_requested_fps;
967
968         var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
969         var_SetFloat( p_input, "sub-original-fps", f_fps );
970
971         f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
972         if( f_requested_fps != f_fps )
973         {
974             var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
975                                             VLC_VAR_DOINHERIT );
976             var_SetFloat( p_input, "sub-fps", f_requested_fps );
977         }
978     }
979
980     const int i_delay = var_CreateGetInteger( p_input, "sub-delay" );
981     if( i_delay != 0 )
982         var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
983
984     /* Look for and add subtitle files */
985     char *psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
986     if( psz_subtitle != NULL )
987     {
988         msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
989         SubtitleAdd( p_input, psz_subtitle, true );
990     }
991
992     if( var_GetBool( p_input, "sub-autodetect-file" ) )
993     {
994         char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
995         char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
996                                              p_input->p->p_item->psz_uri );
997         free( psz_autopath );
998
999         for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1000         {
1001             /* Try to autoselect the first autodetected subtitles file
1002              * if no subtitles file was specified */
1003             bool b_forced = i == 0 && !psz_subtitle;
1004
1005             if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1006                 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1007
1008             free( ppsz_subs[i] );
1009         }
1010         free( ppsz_subs );
1011     }
1012     free( psz_subtitle );
1013 }
1014
1015 static void LoadSlaves( input_thread_t *p_input )
1016 {
1017     char *psz = var_GetNonEmptyString( p_input, "input-slave" );
1018     if( !psz )
1019         return;
1020
1021     char *psz_org = psz;
1022     while( psz && *psz )
1023     {
1024         while( *psz == ' ' || *psz == '#' )
1025             psz++;
1026
1027         char *psz_delim = strchr( psz, '#' );
1028         if( psz_delim )
1029             *psz_delim++ = '\0';
1030
1031         if( *psz == 0 )
1032             break;
1033
1034         msg_Dbg( p_input, "adding slave input '%s'", psz );
1035
1036         input_source_t *p_slave = InputSourceNew( p_input );
1037         if( p_slave && !InputSourceInit( p_input, p_slave, psz, NULL ) )
1038             TAB_APPEND( p_input->p->i_slave, p_input->p->slave, p_slave );
1039         else
1040             free( p_slave );
1041
1042         psz = psz_delim;
1043     }
1044     free( psz_org );
1045 }
1046
1047 static void UpdatePtsDelay( input_thread_t *p_input )
1048 {
1049     input_thread_private_t *p_sys = p_input->p;
1050
1051     /* Get max pts delay from input source */
1052     mtime_t i_pts_delay = p_sys->input.i_pts_delay;
1053     for( int i = 0; i < p_sys->i_slave; i++ )
1054         i_pts_delay = __MAX( i_pts_delay, p_sys->slave[i]->i_pts_delay );
1055
1056     if( i_pts_delay < 0 )
1057         i_pts_delay = 0;
1058
1059     /* Take care of audio/spu delay */
1060     const mtime_t i_audio_delay = var_GetTime( p_input, "audio-delay" );
1061     const mtime_t i_spu_delay   = var_GetTime( p_input, "spu-delay" );
1062     const mtime_t i_extra_delay = __MIN( i_audio_delay, i_spu_delay );
1063     if( i_extra_delay < 0 )
1064         i_pts_delay -= i_extra_delay;
1065
1066     /* Update cr_average depending on the caching */
1067     const int i_cr_average = var_GetInteger( p_input, "cr-average" ) * i_pts_delay / DEFAULT_PTS_DELAY;
1068
1069     /* */
1070     es_out_SetJitter( p_input->p->p_es_out, i_pts_delay, i_cr_average );
1071 }
1072
1073 static void InitPrograms( input_thread_t * p_input )
1074 {
1075     int i_es_out_mode;
1076     vlc_value_t val;
1077
1078     /* Compute correct pts_delay */
1079     UpdatePtsDelay( p_input );
1080
1081     /* Set up es_out */
1082     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1083     i_es_out_mode = ES_OUT_MODE_AUTO;
1084     val.p_list = NULL;
1085     if( p_input->p->p_sout )
1086     {
1087         if( var_GetBool( p_input, "sout-all" ) )
1088         {
1089             i_es_out_mode = ES_OUT_MODE_ALL;
1090             val.p_list = NULL;
1091         }
1092         else
1093         {
1094             var_Get( p_input, "programs", &val );
1095             if( val.p_list && val.p_list->i_count )
1096             {
1097                 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1098                 /* Note : we should remove the "program" callback. */
1099             }
1100             else
1101             {
1102                 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1103                             NULL );
1104             }
1105         }
1106     }
1107     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1108
1109     /* Inform the demuxer about waited group (needed only for DVB) */
1110     if( i_es_out_mode == ES_OUT_MODE_ALL )
1111     {
1112         demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1113     }
1114     else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1115     {
1116         demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1117                         val.p_list );
1118     }
1119     else
1120     {
1121         demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1122                        (int) var_GetInteger( p_input, "program" ), NULL );
1123     }
1124 }
1125
1126 static int Init( input_thread_t * p_input )
1127 {
1128     vlc_meta_t *p_meta;
1129     int i, ret;
1130
1131     for( i = 0; i < p_input->p->p_item->i_options; i++ )
1132     {
1133         if( !strncmp( p_input->p->p_item->ppsz_options[i], "meta-file", 9 ) )
1134         {
1135             msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1136             var_SetString( p_input, "sout", "" );
1137             var_SetBool( p_input, "sout-all", false );
1138             var_SetString( p_input, "input-slave", "" );
1139             var_SetInteger( p_input, "input-repeat", 0 );
1140             var_SetString( p_input, "sub-file", "" );
1141             var_SetBool( p_input, "sub-autodetect-file", false );
1142         }
1143     }
1144
1145     InitStatistics( p_input );
1146 #ifdef ENABLE_SOUT
1147     ret = InitSout( p_input );
1148     if( ret != VLC_SUCCESS )
1149         goto error_stats;
1150 #endif
1151
1152     /* Create es out */
1153     p_input->p->p_es_out_display = input_EsOutNew( p_input, p_input->p->i_rate );
1154     p_input->p->p_es_out         = input_EsOutTimeshiftNew( p_input, p_input->p->p_es_out_display, p_input->p->i_rate );
1155     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1156     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1157
1158     var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1159     var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1160
1161     /* */
1162     input_ChangeState( p_input, OPENING_S );
1163     input_SendEventCache( p_input, 0.0 );
1164
1165     /* */
1166     if( InputSourceInit( p_input, &p_input->p->input,
1167                          p_input->p->p_item->psz_uri, NULL ) )
1168     {
1169         goto error;
1170     }
1171
1172     InitTitle( p_input );
1173
1174     /* Load master infos */
1175     /* Init length */
1176     mtime_t i_length;
1177     if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1178                          &i_length ) )
1179         i_length = 0;
1180     if( i_length <= 0 )
1181         i_length = input_item_GetDuration( p_input->p->p_item );
1182     input_SendEventTimes( p_input, 0.0, 0, i_length );
1183
1184     if( !p_input->b_preparsing )
1185     {
1186         StartTitle( p_input );
1187         LoadSubtitles( p_input );
1188         LoadSlaves( p_input );
1189         InitPrograms( p_input );
1190     }
1191
1192     if( !p_input->b_preparsing && p_input->p->p_sout )
1193     {
1194         p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1195
1196         if( p_input->p->b_can_pace_control && p_input->p->b_out_pace_control )
1197         {
1198             /* We don't want a high input priority here or we'll
1199              * end-up sucking up all the CPU time */
1200             vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1201         }
1202
1203         msg_Dbg( p_input, "starting in %s mode",
1204                  p_input->p->b_out_pace_control ? "async" : "sync" );
1205     }
1206
1207     p_meta = vlc_meta_New();
1208     if( p_meta )
1209     {
1210         /* Get meta data from users */
1211         InputMetaUser( p_input, p_meta );
1212
1213         /* Get meta data from master input */
1214         InputSourceMeta( p_input, &p_input->p->input, p_meta );
1215
1216         /* And from slave */
1217         for( int i = 0; i < p_input->p->i_slave; i++ )
1218             InputSourceMeta( p_input, p_input->p->slave[i], p_meta );
1219
1220         /* */
1221         InputUpdateMeta( p_input, p_meta );
1222     }
1223
1224     msg_Dbg( p_input, "`%s' successfully opened",
1225              p_input->p->p_item->psz_uri );
1226
1227     /* initialization is complete */
1228     input_ChangeState( p_input, PLAYING_S );
1229
1230     return VLC_SUCCESS;
1231
1232 error:
1233     input_ChangeState( p_input, ERROR_S );
1234
1235     if( p_input->p->p_es_out )
1236         es_out_Delete( p_input->p->p_es_out );
1237     if( p_input->p->p_es_out_display )
1238         es_out_Delete( p_input->p->p_es_out_display );
1239     if( p_input->p->p_resource )
1240     {
1241         if( p_input->p->p_sout )
1242             input_resource_RequestSout( p_input->p->p_resource,
1243                                          p_input->p->p_sout, NULL );
1244         input_resource_SetInput( p_input->p->p_resource, NULL );
1245     }
1246
1247 #ifdef ENABLE_SOUT
1248 error_stats:
1249 #endif
1250     if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1251     {
1252 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1253                                    stats_CounterClean( p_input->p->counters.p_##c );\
1254                                p_input->p->counters.p_##c = NULL; } while(0)
1255         EXIT_COUNTER( read_bytes );
1256         EXIT_COUNTER( read_packets );
1257         EXIT_COUNTER( demux_read );
1258         EXIT_COUNTER( input_bitrate );
1259         EXIT_COUNTER( demux_bitrate );
1260         EXIT_COUNTER( demux_corrupted );
1261         EXIT_COUNTER( demux_discontinuity );
1262         EXIT_COUNTER( played_abuffers );
1263         EXIT_COUNTER( lost_abuffers );
1264         EXIT_COUNTER( displayed_pictures );
1265         EXIT_COUNTER( lost_pictures );
1266         EXIT_COUNTER( decoded_audio );
1267         EXIT_COUNTER( decoded_video );
1268         EXIT_COUNTER( decoded_sub );
1269
1270         if( p_input->p->p_sout )
1271         {
1272             EXIT_COUNTER( sout_sent_packets );
1273             EXIT_COUNTER( sout_sent_bytes );
1274             EXIT_COUNTER( sout_send_bitrate );
1275         }
1276 #undef EXIT_COUNTER
1277     }
1278
1279     /* Mark them deleted */
1280     p_input->p->input.p_demux = NULL;
1281     p_input->p->input.p_stream = NULL;
1282     p_input->p->input.p_access = NULL;
1283     p_input->p->p_es_out = NULL;
1284     p_input->p->p_es_out_display = NULL;
1285     p_input->p->p_sout = NULL;
1286
1287     return VLC_EGENERIC;
1288 }
1289
1290 /*****************************************************************************
1291  * End: end the input thread
1292  *****************************************************************************/
1293 static void End( input_thread_t * p_input )
1294 {
1295     int i;
1296
1297     /* We are at the end */
1298     input_ChangeState( p_input, END_S );
1299
1300     /* Clean control variables */
1301     input_ControlVarStop( p_input );
1302
1303     /* Stop es out activity */
1304     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1305     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1306
1307     /* Clean up master */
1308     InputSourceClean( &p_input->p->input );
1309
1310     /* Delete slave */
1311     for( i = 0; i < p_input->p->i_slave; i++ )
1312     {
1313         InputSourceClean( p_input->p->slave[i] );
1314         free( p_input->p->slave[i] );
1315     }
1316     free( p_input->p->slave );
1317
1318     /* Unload all modules */
1319     if( p_input->p->p_es_out )
1320         es_out_Delete( p_input->p->p_es_out );
1321     if( p_input->p->p_es_out_display )
1322         es_out_Delete( p_input->p->p_es_out_display );
1323
1324     if( !p_input->b_preparsing )
1325     {
1326 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1327         if( libvlc_stats( p_input ) )
1328         {
1329             /* make sure we are up to date */
1330             stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
1331             CL_CO( read_bytes );
1332             CL_CO( read_packets );
1333             CL_CO( demux_read );
1334             CL_CO( input_bitrate );
1335             CL_CO( demux_bitrate );
1336             CL_CO( demux_corrupted );
1337             CL_CO( demux_discontinuity );
1338             CL_CO( played_abuffers );
1339             CL_CO( lost_abuffers );
1340             CL_CO( displayed_pictures );
1341             CL_CO( lost_pictures );
1342             CL_CO( decoded_audio) ;
1343             CL_CO( decoded_video );
1344             CL_CO( decoded_sub) ;
1345         }
1346
1347         /* Close optional stream output instance */
1348         if( p_input->p->p_sout )
1349         {
1350             CL_CO( sout_sent_packets );
1351             CL_CO( sout_sent_bytes );
1352             CL_CO( sout_send_bitrate );
1353         }
1354 #undef CL_CO
1355     }
1356
1357     vlc_mutex_lock( &p_input->p->p_item->lock );
1358     if( p_input->p->i_attachment > 0 )
1359     {
1360         for( i = 0; i < p_input->p->i_attachment; i++ )
1361             vlc_input_attachment_Delete( p_input->p->attachment[i] );
1362         TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1363     }
1364     vlc_mutex_unlock( &p_input->p->p_item->lock );
1365
1366     /* */
1367     input_resource_RequestSout( p_input->p->p_resource,
1368                                  p_input->p->p_sout, NULL );
1369     input_resource_SetInput( p_input->p->p_resource, NULL );
1370 }
1371
1372 /*****************************************************************************
1373  * Control
1374  *****************************************************************************/
1375 void input_ControlPush( input_thread_t *p_input,
1376                         int i_type, vlc_value_t *p_val )
1377 {
1378     vlc_mutex_lock( &p_input->p->lock_control );
1379     if( i_type == INPUT_CONTROL_SET_DIE )
1380     {
1381         /* Special case, empty the control */
1382         for( int i = 0; i < p_input->p->i_control; i++ )
1383         {
1384             input_control_t *p_ctrl = &p_input->p->control[i];
1385             ControlRelease( p_ctrl->i_type, p_ctrl->val );
1386         }
1387         p_input->p->i_control = 0;
1388     }
1389
1390     if( p_input->p->i_control >= INPUT_CONTROL_FIFO_SIZE )
1391     {
1392         msg_Err( p_input, "input control fifo overflow, trashing type=%d",
1393                  i_type );
1394         if( p_val )
1395             ControlRelease( i_type, *p_val );
1396     }
1397     else
1398     {
1399         input_control_t c;
1400         c.i_type = i_type;
1401         if( p_val )
1402             c.val = *p_val;
1403         else
1404             memset( &c.val, 0, sizeof(c.val) );
1405
1406         p_input->p->control[p_input->p->i_control++] = c;
1407     }
1408     vlc_cond_signal( &p_input->p->wait_control );
1409     vlc_mutex_unlock( &p_input->p->lock_control );
1410 }
1411
1412 static inline int ControlPop( input_thread_t *p_input,
1413                               int *pi_type, vlc_value_t *p_val,
1414                               mtime_t i_deadline )
1415 {
1416     input_thread_private_t *p_sys = p_input->p;
1417
1418     vlc_mutex_lock( &p_sys->lock_control );
1419     while( p_sys->i_control <= 0 )
1420     {
1421         if( !vlc_object_alive( p_input ) || i_deadline < 0 )
1422         {
1423             vlc_mutex_unlock( &p_sys->lock_control );
1424             return VLC_EGENERIC;
1425         }
1426
1427         if( vlc_cond_timedwait( &p_sys->wait_control, &p_sys->lock_control,
1428                                 i_deadline ) )
1429         {
1430             vlc_mutex_unlock( &p_sys->lock_control );
1431             return VLC_EGENERIC;
1432         }
1433     }
1434
1435     /* */
1436     *pi_type = p_sys->control[0].i_type;
1437     *p_val   = p_sys->control[0].val;
1438
1439     p_sys->i_control--;
1440     if( p_sys->i_control > 0 )
1441         memmove( &p_sys->control[0], &p_sys->control[1],
1442                  sizeof(*p_sys->control) * p_sys->i_control );
1443     vlc_mutex_unlock( &p_sys->lock_control );
1444
1445     return VLC_SUCCESS;
1446 }
1447
1448 static void ControlReduce( input_thread_t *p_input )
1449 {
1450     vlc_mutex_lock( &p_input->p->lock_control );
1451
1452     for( int i = 1; i < p_input->p->i_control; i++ )
1453     {
1454         const int i_lt = p_input->p->control[i-1].i_type;
1455         const int i_ct = p_input->p->control[i].i_type;
1456
1457         /* XXX We can't merge INPUT_CONTROL_SET_ES */
1458 /*        msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1459                  i_lt, i_ct );
1460 */
1461         if( i_lt == i_ct &&
1462             ( i_ct == INPUT_CONTROL_SET_STATE ||
1463               i_ct == INPUT_CONTROL_SET_RATE ||
1464               i_ct == INPUT_CONTROL_SET_POSITION ||
1465               i_ct == INPUT_CONTROL_SET_TIME ||
1466               i_ct == INPUT_CONTROL_SET_PROGRAM ||
1467               i_ct == INPUT_CONTROL_SET_TITLE ||
1468               i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1469               i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1470         {
1471             int j;
1472 //            msg_Dbg( p_input, "merged at %d", i );
1473             /* Remove the i-1 */
1474             for( j = i; j <  p_input->p->i_control; j++ )
1475                 p_input->p->control[j-1] = p_input->p->control[j];
1476             p_input->p->i_control--;
1477         }
1478         else
1479         {
1480             /* TODO but that's not that important
1481                 - merge SET_X with SET_X_CMD
1482                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1483                 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1484                 - ?
1485                 */
1486         }
1487     }
1488     vlc_mutex_unlock( &p_input->p->lock_control );
1489 }
1490
1491 static void ControlRelease( int i_type, vlc_value_t val )
1492 {
1493     switch( i_type )
1494     {
1495     case INPUT_CONTROL_ADD_SUBTITLE:
1496     case INPUT_CONTROL_ADD_SLAVE:
1497         free( val.psz_string );
1498         break;
1499
1500     default:
1501         break;
1502     }
1503 }
1504
1505 /* Pause input */
1506 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1507 {
1508     int i_ret = VLC_SUCCESS;
1509     int i_state = PAUSE_S;
1510
1511     if( p_input->p->b_can_pause )
1512     {
1513         if( p_input->p->input.p_access )
1514             i_ret = access_Control( p_input->p->input.p_access,
1515                                      ACCESS_SET_PAUSE_STATE, true );
1516         else
1517             i_ret = demux_Control( p_input->p->input.p_demux,
1518                                     DEMUX_SET_PAUSE_STATE, true );
1519
1520         if( i_ret )
1521         {
1522             msg_Warn( p_input, "cannot set pause state" );
1523             i_state = p_input->p->i_state;
1524         }
1525     }
1526
1527     /* */
1528     if( !i_ret )
1529     {
1530         i_ret = es_out_SetPauseState( p_input->p->p_es_out, p_input->p->b_can_pause, true, i_control_date );
1531         if( i_ret )
1532         {
1533             msg_Warn( p_input, "cannot set pause state at es_out level" );
1534             i_state = p_input->p->i_state;
1535         }
1536     }
1537
1538     /* Switch to new state */
1539     input_ChangeState( p_input, i_state );
1540
1541 }
1542
1543 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1544 {
1545     int i_ret = VLC_SUCCESS;
1546
1547     if( p_input->p->b_can_pause )
1548     {
1549         if( p_input->p->input.p_access )
1550             i_ret = access_Control( p_input->p->input.p_access,
1551                                      ACCESS_SET_PAUSE_STATE, false );
1552         else
1553             i_ret = demux_Control( p_input->p->input.p_demux,
1554                                     DEMUX_SET_PAUSE_STATE, false );
1555         if( i_ret )
1556         {
1557             /* FIXME What to do ? */
1558             msg_Warn( p_input, "cannot unset pause -> EOF" );
1559             input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1560         }
1561     }
1562
1563     /* Switch to play */
1564     input_ChangeState( p_input, PLAYING_S );
1565
1566     /* */
1567     if( !i_ret )
1568         es_out_SetPauseState( p_input->p->p_es_out, false, false, i_control_date );
1569 }
1570
1571 static bool Control( input_thread_t *p_input,
1572                      int i_type, vlc_value_t val )
1573 {
1574     const mtime_t i_control_date = mdate();
1575     /* FIXME b_force_update is abused, it should be carefully checked */
1576     bool b_force_update = false;
1577
1578     if( !p_input )
1579         return b_force_update;
1580
1581     switch( i_type )
1582     {
1583         case INPUT_CONTROL_SET_DIE:
1584             msg_Dbg( p_input, "control: stopping input" );
1585
1586             /* Mark all submodules to die */
1587             ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1588             break;
1589
1590         case INPUT_CONTROL_SET_POSITION:
1591         case INPUT_CONTROL_SET_POSITION_OFFSET:
1592         {
1593             double f_pos;
1594
1595             if( p_input->p->b_recording )
1596             {
1597                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1598                 break;
1599             }
1600             f_pos = val.f_float;
1601             if( i_type != INPUT_CONTROL_SET_POSITION )
1602                 f_pos += var_GetFloat( p_input, "position" );
1603             if( f_pos < 0.0 )
1604                 f_pos = 0.0;
1605             else if( f_pos > 1.0 )
1606                 f_pos = 1.0;
1607             /* Reset the decoders states and clock sync (before calling the demuxer */
1608             es_out_SetTime( p_input->p->p_es_out, -1 );
1609             if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1610                                 f_pos, !p_input->p->b_fast_seek ) )
1611             {
1612                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1613                          "%2.1f%% failed", f_pos * 100 );
1614             }
1615             else
1616             {
1617                 if( p_input->p->i_slave > 0 )
1618                     SlaveSeek( p_input );
1619                 p_input->p->input.b_eof = false;
1620
1621                 b_force_update = true;
1622             }
1623             break;
1624         }
1625
1626         case INPUT_CONTROL_SET_TIME:
1627         case INPUT_CONTROL_SET_TIME_OFFSET:
1628         {
1629             int64_t i_time;
1630             int i_ret;
1631
1632             if( p_input->p->b_recording )
1633             {
1634                 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1635                 break;
1636             }
1637
1638             i_time = val.i_time;
1639             if( i_type != INPUT_CONTROL_SET_TIME )
1640                 i_time += var_GetTime( p_input, "time" );
1641
1642             if( i_time < 0 )
1643                 i_time = 0;
1644
1645             /* Reset the decoders states and clock sync (before calling the demuxer */
1646             es_out_SetTime( p_input->p->p_es_out, -1 );
1647
1648             i_ret = demux_Control( p_input->p->input.p_demux,
1649                                    DEMUX_SET_TIME, i_time,
1650                                    !p_input->p->b_fast_seek );
1651             if( i_ret )
1652             {
1653                 int64_t i_length;
1654
1655                 /* Emulate it with a SET_POS */
1656                 demux_Control( p_input->p->input.p_demux,
1657                                 DEMUX_GET_LENGTH, &i_length );
1658                 if( i_length > 0 )
1659                 {
1660                     double f_pos = (double)i_time / (double)i_length;
1661                     i_ret = demux_Control( p_input->p->input.p_demux,
1662                                             DEMUX_SET_POSITION, f_pos,
1663                                             !p_input->p->b_fast_seek );
1664                 }
1665             }
1666             if( i_ret )
1667             {
1668                 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1669                          " failed or not possible", i_time );
1670             }
1671             else
1672             {
1673                 if( p_input->p->i_slave > 0 )
1674                     SlaveSeek( p_input );
1675                 p_input->p->input.b_eof = false;
1676
1677                 b_force_update = true;
1678             }
1679             break;
1680         }
1681
1682         case INPUT_CONTROL_SET_STATE:
1683             if( ( val.i_int == PLAYING_S && p_input->p->i_state == PAUSE_S ) ||
1684                 ( val.i_int == PAUSE_S && p_input->p->i_state == PAUSE_S ) )
1685             {
1686                 ControlUnpause( p_input, i_control_date );
1687
1688                 b_force_update = true;
1689             }
1690             else if( val.i_int == PAUSE_S && p_input->p->i_state == PLAYING_S /* &&
1691                      p_input->p->b_can_pause */ )
1692             {
1693                 ControlPause( p_input, i_control_date );
1694
1695                 b_force_update = true;
1696             }
1697             else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause && 0 )
1698             {
1699                 b_force_update = true;
1700
1701                 /* Correct "state" value */
1702                 input_ChangeState( p_input, p_input->p->i_state );
1703             }
1704             else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1705             {
1706                 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1707             }
1708             break;
1709
1710         case INPUT_CONTROL_SET_RATE:
1711         case INPUT_CONTROL_SET_RATE_SLOWER:
1712         case INPUT_CONTROL_SET_RATE_FASTER:
1713         {
1714             int i_rate;
1715             int i_rate_sign;
1716
1717             /* Get rate and direction */
1718             if( i_type == INPUT_CONTROL_SET_RATE )
1719             {
1720                 i_rate = abs( val.i_int );
1721                 i_rate_sign = val.i_int < 0 ? -1 : 1;
1722             }
1723             else
1724             {
1725                 static const int ppi_factor[][2] = {
1726                     {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1727                     {1,1},
1728                     {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1729                     {0,0}
1730                 };
1731                 int i_error;
1732                 int i_idx;
1733                 int i;
1734
1735                 i_rate_sign = p_input->p->i_rate < 0 ? -1 : 1;
1736
1737                 i_error = INT_MAX;
1738                 i_idx = -1;
1739                 for( i = 0; ppi_factor[i][0] != 0; i++ )
1740                 {
1741                     const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1742                     const int i_test_e = abs( abs( p_input->p->i_rate ) - i_test_r );
1743                     if( i_test_e < i_error )
1744                     {
1745                         i_idx = i;
1746                         i_error = i_test_e;
1747                     }
1748                 }
1749
1750                 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1751
1752                 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1753                 {
1754                     if( ppi_factor[i_idx+1][0] > 0 )
1755                         i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1756                     else
1757                         i_rate = INPUT_RATE_MAX+1;
1758                 }
1759                 else
1760                 {
1761                     assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1762                     if( i_idx > 0 )
1763                         i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1764                     else
1765                         i_rate = INPUT_RATE_MIN-1;
1766                 }
1767             }
1768
1769             /* Check rate bound */
1770             if( i_rate < INPUT_RATE_MIN )
1771             {
1772                 msg_Dbg( p_input, "cannot set rate faster" );
1773                 i_rate = INPUT_RATE_MIN;
1774             }
1775             else if( i_rate > INPUT_RATE_MAX )
1776             {
1777                 msg_Dbg( p_input, "cannot set rate slower" );
1778                 i_rate = INPUT_RATE_MAX;
1779             }
1780
1781             /* Apply direction */
1782             if( i_rate_sign < 0 )
1783             {
1784                 if( p_input->p->input.b_rescale_ts )
1785                 {
1786                     msg_Dbg( p_input, "cannot set negative rate" );
1787                     i_rate = p_input->p->i_rate;
1788                     assert( i_rate > 0 );
1789                 }
1790                 else
1791                 {
1792                     i_rate *= i_rate_sign;
1793                 }
1794             }
1795
1796             if( i_rate != INPUT_RATE_DEFAULT &&
1797                 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1798                   ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1799             {
1800                 msg_Dbg( p_input, "cannot change rate" );
1801                 i_rate = INPUT_RATE_DEFAULT;
1802             }
1803             if( i_rate != p_input->p->i_rate &&
1804                 !p_input->p->b_can_pace_control && p_input->p->b_can_rate_control )
1805             {
1806                 int i_ret;
1807                 if( p_input->p->input.p_access )
1808                 {
1809                     i_ret = VLC_EGENERIC;
1810                 }
1811                 else
1812                 {
1813                     if( !p_input->p->input.b_rescale_ts )
1814                         es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1815
1816                     i_ret = demux_Control( p_input->p->input.p_demux,
1817                                             DEMUX_SET_RATE, &i_rate );
1818                 }
1819                 if( i_ret )
1820                 {
1821                     msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1822                     i_rate = p_input->p->i_rate;
1823                 }
1824             }
1825
1826             /* */
1827             if( i_rate != p_input->p->i_rate )
1828             {
1829                 p_input->p->i_rate = i_rate;
1830                 input_SendEventRate( p_input, i_rate );
1831
1832                 if( p_input->p->input.b_rescale_ts )
1833                 {
1834                     const int i_rate_source = (p_input->p->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1835                     es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1836                 }
1837
1838                 b_force_update = true;
1839             }
1840             break;
1841         }
1842
1843         case INPUT_CONTROL_SET_PROGRAM:
1844             /* No need to force update, es_out does it if needed */
1845             es_out_Control( p_input->p->p_es_out,
1846                             ES_OUT_SET_GROUP, val.i_int );
1847
1848             demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1849                             NULL );
1850             break;
1851
1852         case INPUT_CONTROL_SET_ES:
1853             /* No need to force update, es_out does it if needed */
1854             es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, val.i_int );
1855             break;
1856
1857         case INPUT_CONTROL_RESTART_ES:
1858             es_out_Control( p_input->p->p_es_out_display, ES_OUT_RESTART_ES_BY_ID, val.i_int );
1859             break;
1860
1861         case INPUT_CONTROL_SET_AUDIO_DELAY:
1862             if( !es_out_SetDelay( p_input->p->p_es_out_display, AUDIO_ES, val.i_time ) )
1863             {
1864                 input_SendEventAudioDelay( p_input, val.i_time );
1865                 UpdatePtsDelay( p_input );
1866             }
1867             break;
1868
1869         case INPUT_CONTROL_SET_SPU_DELAY:
1870             if( !es_out_SetDelay( p_input->p->p_es_out_display, SPU_ES, val.i_time ) )
1871             {
1872                 input_SendEventSubtitleDelay( p_input, val.i_time );
1873                 UpdatePtsDelay( p_input );
1874             }
1875             break;
1876
1877         case INPUT_CONTROL_SET_TITLE:
1878         case INPUT_CONTROL_SET_TITLE_NEXT:
1879         case INPUT_CONTROL_SET_TITLE_PREV:
1880             if( p_input->p->b_recording )
1881             {
1882                 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1883                 break;
1884             }
1885             if( p_input->p->input.b_title_demux &&
1886                 p_input->p->input.i_title > 0 )
1887             {
1888                 /* TODO */
1889                 /* FIXME handle demux title */
1890                 demux_t *p_demux = p_input->p->input.p_demux;
1891                 int i_title;
1892
1893                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1894                     i_title = p_demux->info.i_title - 1;
1895                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1896                     i_title = p_demux->info.i_title + 1;
1897                 else
1898                     i_title = val.i_int;
1899
1900                 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1901                 {
1902                     es_out_SetTime( p_input->p->p_es_out, -1 );
1903
1904                     demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1905                     input_SendEventTitle( p_input, i_title );
1906                 }
1907             }
1908             else if( p_input->p->input.i_title > 0 )
1909             {
1910                 access_t *p_access = p_input->p->input.p_access;
1911                 int i_title;
1912
1913                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1914                     i_title = p_access->info.i_title - 1;
1915                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1916                     i_title = p_access->info.i_title + 1;
1917                 else
1918                     i_title = val.i_int;
1919
1920                 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1921                 {
1922                     es_out_SetTime( p_input->p->p_es_out, -1 );
1923
1924                     stream_Control( p_input->p->input.p_stream, STREAM_CONTROL_ACCESS,
1925                                     ACCESS_SET_TITLE, i_title );
1926                     input_SendEventTitle( p_input, i_title );
1927                 }
1928             }
1929             break;
1930         case INPUT_CONTROL_SET_SEEKPOINT:
1931         case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1932         case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1933             if( p_input->p->b_recording )
1934             {
1935                 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1936                 break;
1937             }
1938
1939             if( p_input->p->input.b_title_demux &&
1940                 p_input->p->input.i_title > 0 )
1941             {
1942                 demux_t *p_demux = p_input->p->input.p_demux;
1943                 int i_seekpoint;
1944                 int64_t i_input_time;
1945                 int64_t i_seekpoint_time;
1946
1947                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1948                 {
1949                     i_seekpoint = p_demux->info.i_seekpoint;
1950                     i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1951                     i_input_time = var_GetTime( p_input, "time" );
1952                     if( i_seekpoint_time >= 0 && i_input_time >= 0 )
1953                     {
1954                         if( i_input_time < i_seekpoint_time + 3000000 )
1955                             i_seekpoint--;
1956                     }
1957                     else
1958                         i_seekpoint--;
1959                 }
1960                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1961                     i_seekpoint = p_demux->info.i_seekpoint + 1;
1962                 else
1963                     i_seekpoint = val.i_int;
1964
1965                 if( i_seekpoint >= 0 && i_seekpoint <
1966                     p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1967                 {
1968
1969                     es_out_SetTime( p_input->p->p_es_out, -1 );
1970
1971                     demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1972                     input_SendEventSeekpoint( p_input, p_demux->info.i_title, i_seekpoint );
1973                 }
1974             }
1975             else if( p_input->p->input.i_title > 0 )
1976             {
1977                 access_t *p_access = p_input->p->input.p_access;
1978                 int i_seekpoint;
1979                 int64_t i_input_time;
1980                 int64_t i_seekpoint_time;
1981
1982                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1983                 {
1984                     i_seekpoint = p_access->info.i_seekpoint;
1985                     i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1986                     i_input_time = var_GetTime( p_input, "time" );
1987                     if( i_seekpoint_time >= 0 && i_input_time >= 0 )
1988                     {
1989                         if( i_input_time < i_seekpoint_time + 3000000 )
1990                             i_seekpoint--;
1991                     }
1992                     else
1993                         i_seekpoint--;
1994                 }
1995                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1996                     i_seekpoint = p_access->info.i_seekpoint + 1;
1997                 else
1998                     i_seekpoint = val.i_int;
1999
2000                 if( i_seekpoint >= 0 && i_seekpoint <
2001                     p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
2002                 {
2003                     es_out_SetTime( p_input->p->p_es_out, -1 );
2004
2005                     stream_Control( p_input->p->input.p_stream, STREAM_CONTROL_ACCESS,
2006                                     ACCESS_SET_SEEKPOINT, i_seekpoint );
2007                     input_SendEventSeekpoint( p_input, p_access->info.i_title, i_seekpoint );
2008                 }
2009             }
2010             break;
2011
2012         case INPUT_CONTROL_ADD_SUBTITLE:
2013             if( val.psz_string )
2014                 SubtitleAdd( p_input, val.psz_string, true );
2015             break;
2016
2017         case INPUT_CONTROL_ADD_SLAVE:
2018             if( val.psz_string )
2019             {
2020                 input_source_t *slave = InputSourceNew( p_input );
2021
2022                 if( slave && !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
2023                 {
2024                     vlc_meta_t *p_meta;
2025                     int64_t i_time;
2026
2027                     /* Add the slave */
2028                     msg_Dbg( p_input, "adding %s as slave on the fly",
2029                              val.psz_string );
2030
2031                     /* Set position */
2032                     if( demux_Control( p_input->p->input.p_demux,
2033                                         DEMUX_GET_TIME, &i_time ) )
2034                     {
2035                         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2036                         InputSourceClean( slave );
2037                         free( slave );
2038                         break;
2039                     }
2040                     if( demux_Control( slave->p_demux,
2041                                        DEMUX_SET_TIME, i_time, true ) )
2042                     {
2043                         msg_Err( p_input, "seek failed for new slave" );
2044                         InputSourceClean( slave );
2045                         free( slave );
2046                         break;
2047                     }
2048
2049                     /* Get meta (access and demux) */
2050                     p_meta = vlc_meta_New();
2051                     if( p_meta )
2052                     {
2053                         access_Control( slave->p_access, ACCESS_GET_META, p_meta );
2054                         demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
2055                         InputUpdateMeta( p_input, p_meta );
2056                     }
2057
2058                     TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2059                 }
2060                 else
2061                 {
2062                     free( slave );
2063                     msg_Warn( p_input, "failed to add %s as slave",
2064                               val.psz_string );
2065                 }
2066             }
2067             break;
2068
2069         case INPUT_CONTROL_SET_RECORD_STATE:
2070             if( !!p_input->p->b_recording != !!val.b_bool )
2071             {
2072                 if( p_input->p->input.b_can_stream_record )
2073                 {
2074                     if( demux_Control( p_input->p->input.p_demux,
2075                                        DEMUX_SET_RECORD_STATE, val.b_bool ) )
2076                         val.b_bool = false;
2077                 }
2078                 else
2079                 {
2080                     if( es_out_SetRecordState( p_input->p->p_es_out_display, val.b_bool ) )
2081                         val.b_bool = false;
2082                 }
2083                 p_input->p->b_recording = val.b_bool;
2084
2085                 input_SendEventRecord( p_input, val.b_bool );
2086
2087                 b_force_update = true;
2088             }
2089             break;
2090
2091         case INPUT_CONTROL_SET_FRAME_NEXT:
2092             if( p_input->p->i_state == PAUSE_S )
2093             {
2094                 es_out_SetFrameNext( p_input->p->p_es_out );
2095             }
2096             else if( p_input->p->i_state == PLAYING_S )
2097             {
2098                 ControlPause( p_input, i_control_date );
2099             }
2100             else
2101             {
2102                 msg_Err( p_input, "invalid state for frame next" );
2103             }
2104             b_force_update = true;
2105             break;
2106
2107         case INPUT_CONTROL_SET_BOOKMARK:
2108         {
2109             seekpoint_t bookmark;
2110
2111             bookmark.i_time_offset = -1;
2112             bookmark.i_byte_offset = -1;
2113
2114             vlc_mutex_lock( &p_input->p->p_item->lock );
2115             if( val.i_int >= 0 && val.i_int < p_input->p->i_bookmark )
2116             {
2117                 const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[val.i_int];
2118                 bookmark.i_time_offset = p_bookmark->i_time_offset;
2119                 bookmark.i_byte_offset = p_bookmark->i_byte_offset;
2120             }
2121             vlc_mutex_unlock( &p_input->p->p_item->lock );
2122
2123             if( bookmark.i_time_offset < 0 && bookmark.i_byte_offset < 0 )
2124             {
2125                 msg_Err( p_input, "invalid bookmark %d", val.i_int );
2126                 break;
2127             }
2128
2129             if( bookmark.i_time_offset >= 0 )
2130             {
2131                 val.i_time = bookmark.i_time_offset;
2132                 b_force_update = Control( p_input, INPUT_CONTROL_SET_TIME, val );
2133             }
2134             else if( bookmark.i_byte_offset >= 0 &&
2135                      p_input->p->input.p_stream )
2136             {
2137                 const int64_t i_size = stream_Size( p_input->p->input.p_stream );
2138                 if( i_size > 0 && bookmark.i_byte_offset <= i_size )
2139                 {
2140                     val.f_float = (double)bookmark.i_byte_offset / i_size;
2141                     b_force_update = Control( p_input, INPUT_CONTROL_SET_POSITION, val );
2142                 }
2143             }
2144             break;
2145         }
2146
2147         default:
2148             msg_Err( p_input, "not yet implemented" );
2149             break;
2150     }
2151
2152     ControlRelease( i_type, val );
2153     return b_force_update;
2154 }
2155
2156 /*****************************************************************************
2157  * UpdateTitleSeekpoint
2158  *****************************************************************************/
2159 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2160                                  int i_title, int i_seekpoint )
2161 {
2162     int i_title_end = p_input->p->input.i_title_end -
2163                         p_input->p->input.i_title_offset;
2164     int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2165                             p_input->p->input.i_seekpoint_offset;
2166
2167     if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2168     {
2169         if( i_title > i_title_end ||
2170             ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2171             return 0;
2172     }
2173     else if( i_seekpoint_end >= 0 )
2174     {
2175         if( i_seekpoint > i_seekpoint_end )
2176             return 0;
2177     }
2178     else if( i_title_end >= 0 )
2179     {
2180         if( i_title > i_title_end )
2181             return 0;
2182     }
2183     return 1;
2184 }
2185 /*****************************************************************************
2186  * Update*FromDemux:
2187  *****************************************************************************/
2188 static int UpdateTitleSeekpointFromDemux( input_thread_t *p_input )
2189 {
2190     demux_t *p_demux = p_input->p->input.p_demux;
2191
2192     /* TODO event-like */
2193     if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2194     {
2195         input_SendEventTitle( p_input, p_demux->info.i_title );
2196
2197         p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2198     }
2199     if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2200     {
2201         input_SendEventSeekpoint( p_input,
2202                                   p_demux->info.i_title, p_demux->info.i_seekpoint );
2203
2204         p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2205     }
2206
2207     /* Hmmm only works with master input */
2208     if( p_input->p->input.p_demux == p_demux )
2209         return UpdateTitleSeekpoint( p_input,
2210                                      p_demux->info.i_title,
2211                                      p_demux->info.i_seekpoint );
2212     return 1;
2213 }
2214
2215 static void UpdateGenericFromDemux( input_thread_t *p_input )
2216 {
2217     demux_t *p_demux = p_input->p->input.p_demux;
2218
2219     if( p_demux->info.i_update & INPUT_UPDATE_META )
2220     {
2221         vlc_meta_t *p_meta = vlc_meta_New();
2222         if( p_meta )
2223         {
2224             demux_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
2225             InputUpdateMeta( p_input, p_meta );
2226         }
2227         p_demux->info.i_update &= ~INPUT_UPDATE_META;
2228     }
2229
2230     p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2231 }
2232
2233
2234 /*****************************************************************************
2235  * Update*FromAccess:
2236  *****************************************************************************/
2237 static int UpdateTitleSeekpointFromAccess( input_thread_t *p_input )
2238 {
2239     access_t *p_access = p_input->p->input.p_access;
2240
2241     if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2242     {
2243         input_SendEventTitle( p_input, p_access->info.i_title );
2244
2245         stream_Control( p_input->p->input.p_stream, STREAM_UPDATE_SIZE );
2246
2247         p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2248     }
2249     if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2250     {
2251         input_SendEventSeekpoint( p_input,
2252                                   p_access->info.i_title, p_access->info.i_seekpoint );
2253
2254         p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2255     }
2256     /* Hmmm only works with master input */
2257     if( p_input->p->input.p_access == p_access )
2258         return UpdateTitleSeekpoint( p_input,
2259                                      p_access->info.i_title,
2260                                      p_access->info.i_seekpoint );
2261     return 1;
2262 }
2263 static void UpdateGenericFromAccess( input_thread_t *p_input )
2264 {
2265     access_t *p_access = p_input->p->input.p_access;
2266
2267     if( p_access->info.i_update & INPUT_UPDATE_META )
2268     {
2269         /* TODO maybe multi - access ? */
2270         vlc_meta_t *p_meta = vlc_meta_New();
2271         if( p_meta )
2272         {
2273             access_Control( p_input->p->input.p_access, ACCESS_GET_META, p_meta );
2274             InputUpdateMeta( p_input, p_meta );
2275         }
2276         p_access->info.i_update &= ~INPUT_UPDATE_META;
2277     }
2278     if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2279     {
2280         double f_quality;
2281         double f_strength;
2282
2283         if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2284             f_quality = f_strength = -1;
2285
2286         input_SendEventSignal( p_input, f_quality, f_strength );
2287
2288         p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2289     }
2290
2291     p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2292 }
2293
2294 /*****************************************************************************
2295  * InputSourceNew:
2296  *****************************************************************************/
2297 static input_source_t *InputSourceNew( input_thread_t *p_input )
2298 {
2299     VLC_UNUSED(p_input);
2300
2301     return calloc( 1,  sizeof( input_source_t ) );
2302 }
2303
2304 /*****************************************************************************
2305  * InputSourceInit:
2306  *****************************************************************************/
2307 static int InputSourceInit( input_thread_t *p_input,
2308                             input_source_t *in, const char *psz_mrl,
2309                             const char *psz_forced_demux )
2310 {
2311     const char *psz_access;
2312     const char *psz_demux;
2313     char *psz_path;
2314     double f_fps;
2315
2316     char *psz_dup = strdup( psz_mrl );
2317
2318     if( psz_dup == NULL )
2319         goto error;
2320
2321     /* Split uri */
2322     input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2323
2324     msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2325              psz_mrl, psz_access, psz_demux, psz_path );
2326     if( !p_input->b_preparsing )
2327     {
2328         /* Hack to allow udp://@:port syntax */
2329         if( !psz_access ||
2330             (strncmp( psz_access, "udp", 3 ) &&
2331              strncmp( psz_access, "rtp", 3 )) )
2332         {
2333             /* Find optional titles and seekpoints */
2334             MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2335                      &in->i_seekpoint_start, &in->i_seekpoint_end );
2336         }
2337
2338         if( psz_forced_demux && *psz_forced_demux )
2339         {
2340             psz_demux = psz_forced_demux;
2341         }
2342         else if( *psz_demux == '\0' )
2343         {
2344             /* special hack for forcing a demuxer with --demux=module
2345              * (and do nothing with a list) */
2346             char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2347
2348             if( psz_var_demux != NULL &&
2349                 !strchr(psz_var_demux, ',' ) &&
2350                 !strchr(psz_var_demux, ':' ) )
2351             {
2352                 psz_demux = psz_var_demux;
2353
2354                 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2355             }
2356         }
2357
2358         /* Try access_demux first */
2359         in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2360                                   NULL, p_input->p->p_es_out, false );
2361     }
2362     else
2363     {
2364         /* Preparsing is only for file:// */
2365         if( *psz_demux )
2366             goto error;
2367         if( !*psz_access ) /* path without scheme:// */
2368             psz_access = "file";
2369         if( strcmp( psz_access, "file" ) )
2370             goto error;
2371         msg_Dbg( p_input, "trying to pre-parse %s",  psz_path );
2372     }
2373
2374     if( in->p_demux )
2375     {
2376         /* Get infos from access_demux */
2377         demux_Control( in->p_demux,
2378                         DEMUX_GET_PTS_DELAY, &in->i_pts_delay );
2379         in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2380
2381
2382         in->b_title_demux = true;
2383         if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2384                             &in->title, &in->i_title,
2385                             &in->i_title_offset, &in->i_seekpoint_offset ) )
2386         {
2387             TAB_INIT( in->i_title, in->title );
2388         }
2389         if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2390                             &in->b_can_pace_control ) )
2391             in->b_can_pace_control = false;
2392
2393         if( !in->b_can_pace_control )
2394         {
2395             if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2396                                 &in->b_can_rate_control, &in->b_rescale_ts ) )
2397             {
2398                 in->b_can_rate_control = false;
2399                 in->b_rescale_ts = true; /* not used */
2400             }
2401         }
2402         else
2403         {
2404             in->b_can_rate_control = true;
2405             in->b_rescale_ts = true;
2406         }
2407         if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2408                             &in->b_can_pause ) )
2409             in->b_can_pause = false;
2410         var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2411         var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2412         var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2413
2414         bool b_can_seek;
2415         if( demux_Control( in->p_demux, DEMUX_CAN_SEEK, &b_can_seek ) )
2416             b_can_seek = false;
2417         var_SetBool( p_input, "can-seek", b_can_seek );
2418     }
2419     else
2420     {
2421         /* Now try a real access */
2422         in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2423
2424         /* Access failed, URL encoded ? */
2425         if( in->p_access == NULL && strchr( psz_path, '%' ) )
2426         {
2427             decode_URI( psz_path );
2428
2429             msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2430                      psz_access, psz_demux, psz_path );
2431
2432             in->p_access = access_New( p_input,
2433                                         psz_access, psz_demux, psz_path );
2434         }
2435         if( in->p_access == NULL )
2436         {
2437             msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2438                                                          msg_StackMsg() );
2439             dialog_Fatal( p_input, _("Your input can't be opened"),
2440                           _("VLC is unable to open the MRL '%s'."
2441                             " Check the log for details."), psz_mrl );
2442             goto error;
2443         }
2444
2445         /* Get infos from access */
2446         if( !p_input->b_preparsing )
2447         {
2448             bool b_can_seek;
2449             access_Control( in->p_access,
2450                              ACCESS_GET_PTS_DELAY, &in->i_pts_delay );
2451             in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2452
2453             in->b_title_demux = false;
2454             if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2455                                  &in->title, &in->i_title,
2456                                 &in->i_title_offset, &in->i_seekpoint_offset ) )
2457
2458             {
2459                 TAB_INIT( in->i_title, in->title );
2460             }
2461             access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2462                              &in->b_can_pace_control );
2463             in->b_can_rate_control = in->b_can_pace_control;
2464             in->b_rescale_ts = true;
2465
2466             access_Control( in->p_access, ACCESS_CAN_PAUSE, &in->b_can_pause );
2467             var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2468             var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2469             var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2470
2471             access_Control( in->p_access, ACCESS_CAN_SEEK, &b_can_seek );
2472             var_SetBool( p_input, "can-seek", b_can_seek );
2473         }
2474
2475         /* */
2476         int  i_input_list;
2477         char **ppsz_input_list;
2478
2479         TAB_INIT( i_input_list, ppsz_input_list );
2480
2481         /* On master stream only, use input-list */
2482         if( &p_input->p->input == in )
2483         {
2484             char *psz_list;
2485             char *psz_parser;
2486
2487             psz_list =
2488             psz_parser = var_CreateGetNonEmptyString( p_input, "input-list" );
2489
2490             while( psz_parser && *psz_parser )
2491             {
2492                 char *p = strchr( psz_parser, ',' );
2493                 if( p )
2494                     *p++ = '\0';
2495
2496                 if( *psz_parser )
2497                 {
2498                     char *psz_name = strdup( psz_parser );
2499                     if( psz_name )
2500                         TAB_APPEND( i_input_list, ppsz_input_list, psz_name );
2501                 }
2502
2503                 psz_parser = p;
2504             }
2505             free( psz_list );
2506         }
2507         /* Autodetect extra files if none specified */
2508         if( i_input_list <= 0 )
2509         {
2510             InputGetExtraFiles( p_input, &i_input_list, &ppsz_input_list,
2511                                 psz_access, psz_path );
2512         }
2513         if( i_input_list > 0 )
2514             TAB_APPEND( i_input_list, ppsz_input_list, NULL );
2515
2516         /* Create the stream_t */
2517         in->p_stream = stream_AccessNew( in->p_access, ppsz_input_list );
2518         if( ppsz_input_list )
2519         {
2520             for( int i = 0; ppsz_input_list[i] != NULL; i++ )
2521                 free( ppsz_input_list[i] );
2522             TAB_CLEAN( i_input_list, ppsz_input_list );
2523         }
2524
2525         if( in->p_stream == NULL )
2526         {
2527             msg_Warn( p_input, "cannot create a stream_t from access" );
2528             goto error;
2529         }
2530
2531         /* Add stream filters */
2532         char *psz_stream_filter = var_GetNonEmptyString( p_input,
2533                                                          "stream-filter" );
2534         in->p_stream = stream_FilterChainNew( in->p_stream,
2535                                               psz_stream_filter,
2536                                               var_GetBool( p_input, "input-record-native" ) );
2537         free( psz_stream_filter );
2538
2539         /* Open a demuxer */
2540         if( *psz_demux == '\0' && *in->p_access->psz_demux )
2541         {
2542             psz_demux = in->p_access->psz_demux;
2543         }
2544
2545         {
2546             /* Take access/stream redirections into account */
2547             char *psz_real_path;
2548             char *psz_buf = NULL;
2549             if( in->p_stream->psz_path )
2550             {
2551                 const char *psz_a, *psz_d;
2552                 psz_buf = strdup( in->p_stream->psz_path );
2553                 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2554             }
2555             else
2556             {
2557                 psz_real_path = psz_path;
2558             }
2559             in->p_demux = demux_New( p_input, psz_access, psz_demux,
2560                                       psz_real_path,
2561                                       in->p_stream, p_input->p->p_es_out,
2562                                       p_input->b_preparsing );
2563             free( psz_buf );
2564         }
2565
2566         if( in->p_demux == NULL )
2567         {
2568             msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2569                      psz_access, psz_demux, psz_path );
2570             dialog_Fatal( VLC_OBJECT( p_input ),
2571                           _("VLC can't recognize the input's format"),
2572                           _("The format of '%s' cannot be detected. "
2573                             "Have a look at the log for details."), psz_mrl );
2574             goto error;
2575         }
2576
2577         /* Get title from demux */
2578         if( !p_input->b_preparsing && in->i_title <= 0 )
2579         {
2580             if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2581                                 &in->title, &in->i_title,
2582                                 &in->i_title_offset, &in->i_seekpoint_offset ))
2583             {
2584                 TAB_INIT( in->i_title, in->title );
2585             }
2586             else
2587             {
2588                 in->b_title_demux = true;
2589             }
2590         }
2591     }
2592
2593     free( psz_dup );
2594
2595     /* Set record capabilities */
2596     if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2597         in->b_can_stream_record = false;
2598 #ifdef ENABLE_SOUT
2599     if( !var_GetBool( p_input, "input-record-native" ) )
2600         in->b_can_stream_record = false;
2601     var_SetBool( p_input, "can-record", true );
2602 #else
2603     var_SetBool( p_input, "can-record", in->b_can_stream_record );
2604 #endif
2605
2606     /* get attachment
2607      * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2608     if( 1 || !p_input->b_preparsing )
2609     {
2610         int i_attachment;
2611         input_attachment_t **attachment;
2612         if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2613                              &attachment, &i_attachment ) )
2614         {
2615             vlc_mutex_lock( &p_input->p->p_item->lock );
2616             AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2617                               i_attachment, attachment );
2618             vlc_mutex_unlock( &p_input->p->p_item->lock );
2619         }
2620     }
2621     if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) && f_fps > 0.0 )
2622     {
2623         vlc_mutex_lock( &p_input->p->p_item->lock );
2624         p_input->p->f_fps = f_fps;
2625         vlc_mutex_unlock( &p_input->p->p_item->lock );
2626     }
2627
2628     if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2629         in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2630
2631     return VLC_SUCCESS;
2632
2633 error:
2634     if( in->p_demux )
2635         demux_Delete( in->p_demux );
2636
2637     if( in->p_stream )
2638         stream_Delete( in->p_stream );
2639
2640     if( in->p_access )
2641         access_Delete( in->p_access );
2642     free( psz_dup );
2643
2644     return VLC_EGENERIC;
2645 }
2646
2647 /*****************************************************************************
2648  * InputSourceClean:
2649  *****************************************************************************/
2650 static void InputSourceClean( input_source_t *in )
2651 {
2652     int i;
2653
2654     if( in->p_demux )
2655         demux_Delete( in->p_demux );
2656
2657     if( in->p_stream )
2658         stream_Delete( in->p_stream );
2659
2660     if( in->p_access )
2661         access_Delete( in->p_access );
2662
2663     if( in->i_title > 0 )
2664     {
2665         for( i = 0; i < in->i_title; i++ )
2666             vlc_input_title_Delete( in->title[i] );
2667         TAB_CLEAN( in->i_title, in->title );
2668     }
2669 }
2670
2671 /*****************************************************************************
2672  * InputSourceMeta:
2673  *****************************************************************************/
2674 static void InputSourceMeta( input_thread_t *p_input,
2675                              input_source_t *p_source, vlc_meta_t *p_meta )
2676 {
2677     access_t *p_access = p_source->p_access;
2678     demux_t *p_demux = p_source->p_demux;
2679
2680     /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
2681      * is a bad idea */
2682
2683     /* Read access meta */
2684     if( p_access )
2685         access_Control( p_access, ACCESS_GET_META, p_meta );
2686
2687     /* Read demux meta */
2688     demux_Control( p_demux, DEMUX_GET_META, p_meta );
2689
2690     /* If the demux report unsupported meta data, try an external "meta reader" */
2691     bool b_bool;
2692     if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2693         return;
2694     if( !b_bool )
2695         return;
2696
2697     demux_meta_t *p_demux_meta = p_demux->p_private = calloc( 1, sizeof(*p_demux_meta) );
2698     if( !p_demux_meta )
2699         return;
2700
2701     module_t *p_id3 = module_need( p_demux, "meta reader", NULL, false );
2702     if( p_id3 )
2703     {
2704         if( p_demux_meta->p_meta )
2705         {
2706             vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2707             vlc_meta_Delete( p_demux_meta->p_meta );
2708         }
2709
2710         if( p_demux_meta->i_attachments > 0 )
2711         {
2712             vlc_mutex_lock( &p_input->p->p_item->lock );
2713             AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2714                               p_demux_meta->i_attachments, p_demux_meta->attachments );
2715             vlc_mutex_unlock( &p_input->p->p_item->lock );
2716         }
2717         module_unneed( p_demux, p_id3 );
2718     }
2719     free( p_demux_meta );
2720 }
2721
2722
2723 static void SlaveDemux( input_thread_t *p_input )
2724 {
2725     int64_t i_time;
2726     int i;
2727
2728     if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2729     {
2730         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2731         return;
2732     }
2733
2734     for( i = 0; i < p_input->p->i_slave; i++ )
2735     {
2736         input_source_t *in = p_input->p->slave[i];
2737         int i_ret = 1;
2738
2739         if( in->b_eof )
2740             continue;
2741
2742         if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2743         {
2744             for( ;; )
2745             {
2746                 int64_t i_stime;
2747                 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2748                 {
2749                     msg_Err( p_input, "slave[%d] doesn't like "
2750                              "DEMUX_GET_TIME -> EOF", i );
2751                     i_ret = 0;
2752                     break;
2753                 }
2754
2755                 if( i_stime >= i_time )
2756                     break;
2757
2758                 if( ( i_ret = demux_Demux( in->p_demux ) ) <= 0 )
2759                     break;
2760             }
2761         }
2762         else
2763         {
2764             i_ret = demux_Demux( in->p_demux );
2765         }
2766
2767         if( i_ret <= 0 )
2768         {
2769             msg_Dbg( p_input, "slave %d EOF", i );
2770             in->b_eof = true;
2771         }
2772     }
2773 }
2774
2775 static void SlaveSeek( input_thread_t *p_input )
2776 {
2777     int64_t i_time;
2778     int i;
2779
2780     if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2781     {
2782         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2783         return;
2784     }
2785
2786     for( i = 0; i < p_input->p->i_slave; i++ )
2787     {
2788         input_source_t *in = p_input->p->slave[i];
2789
2790         if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time, true ) )
2791         {
2792             if( !in->b_eof )
2793                 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2794             in->b_eof = true;
2795         }
2796         else
2797         {
2798             in->b_eof = false;
2799         }
2800     }
2801 }
2802
2803 /*****************************************************************************
2804  * InputMetaUser:
2805  *****************************************************************************/
2806 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2807 {
2808     static const struct { int i_meta; const char *psz_name; } p_list[] = {
2809         { vlc_meta_Title,       "meta-title" },
2810         { vlc_meta_Artist,      "meta-artist" },
2811         { vlc_meta_Genre,       "meta-genre" },
2812         { vlc_meta_Copyright,   "meta-copyright" },
2813         { vlc_meta_Description, "meta-description" },
2814         { vlc_meta_Date,        "meta-date" },
2815         { vlc_meta_URL,         "meta-url" },
2816         { 0, NULL }
2817     };
2818
2819     /* Get meta information from user */
2820     for( int i = 0; p_list[i].psz_name; i++ )
2821     {
2822         char *psz_string = var_GetNonEmptyString( p_input, p_list[i].psz_name );
2823         if( !psz_string )
2824             continue;
2825
2826         EnsureUTF8( psz_string );
2827         vlc_meta_Set( p_meta, p_list[i].i_meta, psz_string );
2828         free( psz_string );
2829     }
2830 }
2831
2832 /*****************************************************************************
2833  * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2834  * arturl and locking issue.
2835  *****************************************************************************/
2836 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2837 {
2838     input_item_t *p_item = p_input->p->p_item;
2839
2840     char *psz_title = NULL;
2841     char *psz_arturl = input_item_GetArtURL( p_item );
2842
2843     vlc_mutex_lock( &p_item->lock );
2844
2845     if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2846         psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2847
2848     vlc_meta_Merge( p_item->p_meta, p_meta );
2849
2850     vlc_meta_Delete( p_meta );
2851
2852     if( !psz_arturl || *psz_arturl == '\0' )
2853     {
2854         const char *psz_tmp = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
2855         if( psz_tmp )
2856             psz_arturl = strdup( psz_tmp );
2857     }
2858     vlc_mutex_unlock( &p_item->lock );
2859
2860     if( psz_arturl && *psz_arturl )
2861     {
2862         input_item_SetArtURL( p_item, psz_arturl );
2863
2864         if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2865         {
2866             /* Don't look for art cover if sout
2867              * XXX It can change when sout has meta data support */
2868             if( p_input->p->p_sout && !p_input->b_preparsing )
2869                 input_item_SetArtURL( p_item, "" );
2870             else
2871                 input_ExtractAttachmentAndCacheArt( p_input );
2872         }
2873     }
2874     free( psz_arturl );
2875
2876     if( psz_title )
2877     {
2878         input_item_SetName( p_item, psz_title );
2879         free( psz_title );
2880     }
2881     input_item_SetPreparsed( p_item, true );
2882
2883     input_SendEventMeta( p_input );
2884
2885     /** \todo handle sout meta */
2886 }
2887
2888 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2889                               int i_new, input_attachment_t **pp_new )
2890 {
2891     int i_attachment = *pi_attachment;
2892     input_attachment_t **attachment = *ppp_attachment;
2893     int i;
2894
2895     attachment = realloc( attachment,
2896                           sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2897     for( i = 0; i < i_new; i++ )
2898         attachment[i_attachment++] = pp_new[i];
2899     free( pp_new );
2900
2901     /* */
2902     *pi_attachment = i_attachment;
2903     *ppp_attachment = attachment;
2904 }
2905 /*****************************************************************************
2906  * InputGetExtraFiles
2907  *  Autodetect extra input list
2908  *****************************************************************************/
2909 static void InputGetExtraFilesPattern( input_thread_t *p_input,
2910                                        int *pi_list, char ***pppsz_list,
2911                                        const char *psz_path,
2912                                        const char *psz_match,
2913                                        const char *psz_format,
2914                                        int i_start, int i_stop )
2915 {
2916     int i_list;
2917     char **ppsz_list;
2918
2919     TAB_INIT( i_list, ppsz_list );
2920
2921     char *psz_base = strdup( psz_path );
2922     if( !psz_base )
2923         goto exit;
2924
2925     /* Remove the extension */
2926     char *psz_end = &psz_base[strlen(psz_base)-strlen(psz_match)];
2927     assert( psz_end >= psz_base);
2928     *psz_end = '\0';
2929
2930     /* Try to list files */
2931     for( int i = i_start; i <= i_stop; i++ )
2932     {
2933         struct stat st;
2934         char *psz_file;
2935
2936         if( asprintf( &psz_file, psz_format, psz_base, i ) < 0 )
2937             break;
2938
2939         if( utf8_stat( psz_file, &st ) || !S_ISREG( st.st_mode ) || !st.st_size )
2940         {
2941             free( psz_file );
2942             break;
2943         }
2944
2945         msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2946         TAB_APPEND( i_list, ppsz_list, psz_file );
2947     }
2948     free( psz_base );
2949 exit:
2950     *pi_list = i_list;
2951     *pppsz_list = ppsz_list;
2952 }
2953
2954 static void InputGetExtraFiles( input_thread_t *p_input,
2955                                 int *pi_list, char ***pppsz_list,
2956                                 const char *psz_access, const char *psz_path )
2957 {
2958     static const struct
2959     {
2960         const char *psz_match;
2961         const char *psz_format;
2962         int i_start;
2963         int i_stop;
2964     } p_pattern[] = {
2965         /* XXX the order is important */
2966         { ".001",         "%s.%.3d",        2, 999 },
2967         { ".part1.rar",   "%s.part%.1d.rar",2, 9 },
2968         { ".part01.rar",  "%s.part%.2d.rar",2, 99, },
2969         { ".part001.rar", "%s.part%.3d.rar",2, 999 },
2970         { ".rar",         "%s.r%.2d",       1, 99 },
2971         { NULL, NULL, 0, 0 }
2972     };
2973
2974     TAB_INIT( *pi_list, *pppsz_list );
2975
2976     if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2977         return;
2978
2979     const size_t i_path = strlen(psz_path);
2980
2981     for( int i = 0; p_pattern[i].psz_match != NULL; i++ )
2982     {
2983         const size_t i_ext = strlen(p_pattern[i].psz_match );
2984
2985         if( i_path < i_ext )
2986             continue;
2987         if( !strcmp( &psz_path[i_path-i_ext], p_pattern[i].psz_match ) )
2988         {
2989             InputGetExtraFilesPattern( p_input, pi_list, pppsz_list,
2990                                        psz_path,
2991                                        p_pattern[i].psz_match, p_pattern[i].psz_format,
2992                                        p_pattern[i].i_start, p_pattern[i].i_stop );
2993             return;
2994         }
2995     }
2996 }
2997
2998 /* */
2999 static void input_ChangeState( input_thread_t *p_input, int i_state )
3000 {
3001     const bool b_changed = p_input->p->i_state != i_state;
3002
3003     p_input->p->i_state = i_state;
3004     if( i_state == ERROR_S )
3005         p_input->b_error = true;
3006     else if( i_state == END_S )
3007         p_input->b_eof = true;
3008
3009     if( b_changed )
3010     {
3011         input_item_SetErrorWhenReading( p_input->p->p_item, p_input->b_error );
3012         input_SendEventState( p_input, i_state );
3013     }
3014 }
3015
3016
3017 /*****************************************************************************
3018  * MRLSplit: parse the access, demux and url part of the
3019  *           Media Resource Locator.
3020  *****************************************************************************/
3021 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
3022                      char *psz_dup )
3023 {
3024     char *psz_access = NULL;
3025     char *psz_demux  = NULL;
3026     char *psz_path;
3027
3028     /* Either there is an access/demux specification before ://
3029      * or we have a plain local file path. */
3030     psz_path = strstr( psz_dup, "://" );
3031     if( psz_path != NULL )
3032     {
3033         *psz_path = '\0';
3034         psz_path += 3; /* skips "://" */
3035
3036         /* Separate access from demux (<access>/<demux>://<path>) */
3037         psz_access = psz_dup;
3038         psz_demux = strchr( psz_access, '/' );
3039         if( psz_demux )
3040             *psz_demux++ = '\0';
3041
3042         /* We really don't want module name substitution here! */
3043         if( psz_access[0] == '$' )
3044             psz_access++;
3045         if( psz_demux && psz_demux[0] == '$' )
3046             psz_demux++;
3047     }
3048     else
3049     {
3050         psz_path = psz_dup;
3051     }
3052     *ppsz_access = psz_access ? psz_access : (char*)"";
3053     *ppsz_demux = psz_demux ? psz_demux : (char*)"";
3054     *ppsz_path = psz_path;
3055 }
3056
3057 static inline bool next(char ** src)
3058 {
3059     char *end;
3060     errno = 0;
3061     long result = strtol( *src, &end, 0 );
3062     if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
3063         end == *src )
3064     {
3065         return false;
3066     }
3067     *src = end;
3068     return true;
3069 }
3070
3071 /*****************************************************************************
3072  * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3073  *
3074  * Syntax:
3075  * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
3076  *****************************************************************************/
3077 static void MRLSections( input_thread_t *p_input, char *psz_source,
3078                          int *pi_title_start, int *pi_title_end,
3079                          int *pi_chapter_start, int *pi_chapter_end )
3080 {
3081     char *psz, *psz_end, *psz_next, *psz_check;
3082
3083     *pi_title_start = *pi_title_end = -1;
3084     *pi_chapter_start = *pi_chapter_end = -1;
3085
3086     /* Start by parsing titles and chapters */
3087     if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
3088
3089
3090     /* Check we are really dealing with a title/chapter section */
3091     psz_check = psz + 1;
3092     if( !*psz_check ) return;
3093     if( isdigit(*psz_check) )
3094         if(!next(&psz_check)) return;
3095     if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
3096     if( *psz_check == ':' && ++psz_check )
3097     {
3098         if( isdigit(*psz_check) )
3099             if(!next(&psz_check)) return;
3100     }
3101     if( *psz_check != '-' && *psz_check ) return;
3102     if( *psz_check == '-' && ++psz_check )
3103     {
3104         if( isdigit(*psz_check) )
3105             if(!next(&psz_check)) return;
3106     }
3107     if( *psz_check != ':' && *psz_check ) return;
3108     if( *psz_check == ':' && ++psz_check )
3109     {
3110         if( isdigit(*psz_check) )
3111             if(!next(&psz_check)) return;
3112     }
3113     if( *psz_check ) return;
3114
3115     /* Separate start and end */
3116     *psz++ = 0;
3117     if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
3118
3119     /* Look for the start title */
3120     *pi_title_start = strtol( psz, &psz_next, 0 );
3121     if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
3122     *pi_title_end = *pi_title_start;
3123     psz = psz_next;
3124
3125     /* Look for the start chapter */
3126     if( *psz ) psz++;
3127     *pi_chapter_start = strtol( psz, &psz_next, 0 );
3128     if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
3129     *pi_chapter_end = *pi_chapter_start;
3130
3131     if( psz_end )
3132     {
3133         /* Look for the end title */
3134         *pi_title_end = strtol( psz_end, &psz_next, 0 );
3135         if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3136         psz_end = psz_next;
3137
3138         /* Look for the end chapter */
3139         if( *psz_end ) psz_end++;
3140         *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3141         if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3142     }
3143
3144     msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3145              psz_source, *pi_title_start, *pi_chapter_start,
3146              *pi_title_end, *pi_chapter_end );
3147 }
3148
3149 /*****************************************************************************
3150  * input_AddSubtitles: add a subtitles file and enable it
3151  *****************************************************************************/
3152 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3153 {
3154     input_source_t *sub;
3155     vlc_value_t count;
3156     vlc_value_t list;
3157     char *psz_path, *psz_extension;
3158
3159     /* if we are provided a subtitle.sub file,
3160      * see if we don't have a subtitle.idx and use it instead */
3161     psz_path = strdup( psz_subtitle );
3162     if( psz_path )
3163     {
3164         psz_extension = strrchr( psz_path, '.');
3165         if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3166         {
3167             struct stat st;
3168
3169             strcpy( psz_extension, ".idx" );
3170
3171             if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3172             {
3173                 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3174                          psz_path, psz_subtitle );
3175                 strcpy( psz_subtitle, psz_path );
3176             }
3177         }
3178         free( psz_path );
3179     }
3180
3181     var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3182
3183     sub = InputSourceNew( p_input );
3184     if( !sub || InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3185     {
3186         free( sub );
3187         return;
3188     }
3189     TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3190
3191     /* Select the ES */
3192     if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3193     {
3194         if( count.i_int == 0 )
3195             count.i_int++;
3196         /* if it was first one, there is disable too */
3197
3198         if( count.i_int < list.p_list->i_count )
3199         {
3200             const int i_id = list.p_list->p_values[count.i_int].i_int;
3201
3202             es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3203             es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3204         }
3205         var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3206     }
3207 }
3208
3209 /*****************************************************************************
3210  * Statistics
3211  *****************************************************************************/
3212 void input_UpdateStatistic( input_thread_t *p_input,
3213                             input_statistic_t i_type, int i_delta )
3214 {
3215     assert( p_input->p->i_state != INIT_S );
3216
3217     vlc_mutex_lock( &p_input->p->counters.counters_lock);
3218     switch( i_type )
3219     {
3220 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
3221     case INPUT_STATISTIC_DECODED_VIDEO:
3222         I(p_decoded_video);
3223         break;
3224     case INPUT_STATISTIC_DECODED_AUDIO:
3225         I(p_decoded_audio);
3226         break;
3227     case INPUT_STATISTIC_DECODED_SUBTITLE:
3228         I(p_decoded_sub);
3229         break;
3230     case INPUT_STATISTIC_SENT_PACKET:
3231         I(p_sout_sent_packets);
3232         break;
3233 #undef I
3234     case INPUT_STATISTIC_SENT_BYTE:
3235     {
3236         int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
3237                         really fast ... */
3238         if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
3239             stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
3240         break;
3241     }
3242     default:
3243         msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3244         break;
3245     }
3246     vlc_mutex_unlock( &p_input->p->counters.counters_lock);
3247 }
3248
3249 /**/
3250 /* TODO FIXME nearly the same logic that snapshot code */
3251 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3252 {
3253     char *psz_file;
3254     DIR *path;
3255
3256     path = utf8_opendir( psz_path );
3257     if( path )
3258     {
3259         closedir( path );
3260
3261         char *psz_tmp = str_format( p_obj, psz_prefix );
3262         if( !psz_tmp )
3263             return NULL;
3264
3265         char *psz_tmp2 = filename_sanitize( psz_tmp );
3266         free( psz_tmp );
3267
3268         if( !psz_tmp2 ||
3269             asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3270                       psz_path, psz_tmp2,
3271                       psz_extension ? "." : "",
3272                       psz_extension ? psz_extension : "" ) < 0 )
3273             psz_file = NULL;
3274         free( psz_tmp2 );
3275         return psz_file;
3276     }
3277     else
3278     {
3279         psz_file = str_format( p_obj, psz_path );
3280         path_sanitize( psz_file );
3281         return psz_file;
3282     }
3283 }
3284