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