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