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