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