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