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