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