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