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