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