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