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