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