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