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