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