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