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