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