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