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