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