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