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