55fe92c6820dfbc2876679df8510790c4f498c86
[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             es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1523             input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1524             if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1525                                 f_pos ) )
1526             {
1527                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1528                          "%2.1f%% failed", f_pos * 100 );
1529             }
1530             else
1531             {
1532                 if( p_input->p->i_slave > 0 )
1533                     SlaveSeek( p_input );
1534
1535                 b_force_update = VLC_TRUE;
1536             }
1537             break;
1538         }
1539
1540         case INPUT_CONTROL_SET_TIME:
1541         case INPUT_CONTROL_SET_TIME_OFFSET:
1542         {
1543             int64_t i_time;
1544             int i_ret;
1545
1546             if( i_type == INPUT_CONTROL_SET_TIME )
1547             {
1548                 i_time = val.i_time;
1549             }
1550             else
1551             {
1552                 /* Should not fail */
1553                 demux2_Control( p_input->p->input.p_demux,
1554                                 DEMUX_GET_TIME, &i_time );
1555                 i_time += val.i_time;
1556             }
1557             if( i_time < 0 ) i_time = 0;
1558
1559             /* Reset the decoders states and clock sync (before calling the demuxer */
1560             es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1561             input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1562
1563             i_ret = demux2_Control( p_input->p->input.p_demux,
1564                                     DEMUX_SET_TIME, i_time );
1565             if( i_ret )
1566             {
1567                 int64_t i_length;
1568
1569                 /* Emulate it with a SET_POS */
1570                 demux2_Control( p_input->p->input.p_demux,
1571                                 DEMUX_GET_LENGTH, &i_length );
1572                 if( i_length > 0 )
1573                 {
1574                     double f_pos = (double)i_time / (double)i_length;
1575                     i_ret = demux2_Control( p_input->p->input.p_demux,
1576                                             DEMUX_SET_POSITION, f_pos );
1577                 }
1578             }
1579             if( i_ret )
1580             {
1581                 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1582                          " failed or not possible", i_time );
1583             }
1584             else
1585             {
1586                 if( p_input->p->i_slave > 0 )
1587                     SlaveSeek( p_input );
1588
1589                 b_force_update = VLC_TRUE;
1590             }
1591             break;
1592         }
1593
1594         case INPUT_CONTROL_SET_STATE:
1595             if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1596                 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1597             {
1598                 int i_ret;
1599                 if( p_input->p->input.p_access )
1600                     i_ret = access2_Control( p_input->p->input.p_access,
1601                                              ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1602                 else
1603                     i_ret = demux2_Control( p_input->p->input.p_demux,
1604                                             DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1605
1606                 if( i_ret )
1607                 {
1608                     /* FIXME What to do ? */
1609                     msg_Warn( p_input, "cannot unset pause -> EOF" );
1610                     vlc_mutex_unlock( &p_input->p->lock_control );
1611                     input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1612                     vlc_mutex_lock( &p_input->p->lock_control );
1613                 }
1614
1615                 b_force_update = VLC_TRUE;
1616
1617                 /* Switch to play */
1618                 p_input->i_state = PLAYING_S;
1619                 val.i_int = PLAYING_S;
1620                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1621
1622                 /* */
1623                 if( !i_ret )
1624                     input_EsOutChangeState( p_input->p->p_es_out );
1625             }
1626             else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1627                      p_input->p->b_can_pause )
1628             {
1629                 int i_ret;
1630                 if( p_input->p->input.p_access )
1631                     i_ret = access2_Control( p_input->p->input.p_access,
1632                                              ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1633                 else
1634                     i_ret = demux2_Control( p_input->p->input.p_demux,
1635                                             DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1636
1637                 b_force_update = VLC_TRUE;
1638
1639                 if( i_ret )
1640                 {
1641                     msg_Warn( p_input, "cannot set pause state" );
1642                     val.i_int = p_input->i_state;
1643                 }
1644                 else
1645                 {
1646                     val.i_int = PAUSE_S;
1647                 }
1648
1649                 /* Switch to new state */
1650                 p_input->i_state = val.i_int;
1651                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1652
1653                 /* */
1654                 if( !i_ret )
1655                     input_EsOutChangeState( p_input->p->p_es_out );
1656             }
1657             else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1658             {
1659                 b_force_update = VLC_TRUE;
1660
1661                 /* Correct "state" value */
1662                 val.i_int = p_input->i_state;
1663                 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1664             }
1665             else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1666             {
1667                 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1668             }
1669             break;
1670
1671         case INPUT_CONTROL_SET_RATE:
1672         case INPUT_CONTROL_SET_RATE_SLOWER:
1673         case INPUT_CONTROL_SET_RATE_FASTER:
1674         {
1675             int i_rate;
1676
1677             if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1678                 i_rate = p_input->p->i_rate * 2;
1679             else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1680                 i_rate = p_input->p->i_rate / 2;
1681             else
1682                 i_rate = val.i_int;
1683
1684             if( i_rate < INPUT_RATE_MIN )
1685             {
1686                 msg_Dbg( p_input, "cannot set rate faster" );
1687                 i_rate = INPUT_RATE_MIN;
1688             }
1689             else if( i_rate > INPUT_RATE_MAX )
1690             {
1691                 msg_Dbg( p_input, "cannot set rate slower" );
1692                 i_rate = INPUT_RATE_MAX;
1693             }
1694             if( i_rate != INPUT_RATE_DEFAULT &&
1695                 ( !p_input->b_can_pace_control ||
1696                   ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1697             {
1698                 msg_Dbg( p_input, "cannot change rate" );
1699                 i_rate = INPUT_RATE_DEFAULT;
1700             }
1701             if( i_rate != p_input->p->i_rate )
1702             {
1703                 val.i_int = i_rate;
1704                 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1705
1706                 p_input->p->i_rate  = i_rate;
1707
1708                 input_EsOutChangeRate( p_input->p->p_es_out );
1709
1710                 b_force_update = VLC_TRUE;
1711             }
1712             break;
1713         }
1714
1715         case INPUT_CONTROL_SET_PROGRAM:
1716             /* No need to force update, es_out does it if needed */
1717             es_out_Control( p_input->p->p_es_out,
1718                             ES_OUT_SET_GROUP, val.i_int );
1719
1720             demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1721                             NULL );
1722             break;
1723
1724         case INPUT_CONTROL_SET_ES:
1725             /* No need to force update, es_out does it if needed */
1726             es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1727                             input_EsOutGetFromID( p_input->p->p_es_out,
1728                                                   val.i_int ) );
1729             break;
1730
1731         case INPUT_CONTROL_SET_AUDIO_DELAY:
1732             input_EsOutSetDelay( p_input->p->p_es_out,
1733                                  AUDIO_ES, val.i_time );
1734             var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1735             break;
1736
1737         case INPUT_CONTROL_SET_SPU_DELAY:
1738             input_EsOutSetDelay( p_input->p->p_es_out,
1739                                  SPU_ES, val.i_time );
1740             var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1741             break;
1742
1743         case INPUT_CONTROL_SET_TITLE:
1744         case INPUT_CONTROL_SET_TITLE_NEXT:
1745         case INPUT_CONTROL_SET_TITLE_PREV:
1746             if( p_input->p->input.b_title_demux &&
1747                 p_input->p->input.i_title > 0 )
1748             {
1749                 /* TODO */
1750                 /* FIXME handle demux title */
1751                 demux_t *p_demux = p_input->p->input.p_demux;
1752                 int i_title;
1753
1754                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1755                     i_title = p_demux->info.i_title - 1;
1756                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1757                     i_title = p_demux->info.i_title + 1;
1758                 else
1759                     i_title = val.i_int;
1760
1761                 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1762                 {
1763                     es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1764                     input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1765
1766                     demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1767                     input_ControlVarTitle( p_input, i_title );
1768                 }
1769             }
1770             else if( p_input->p->input.i_title > 0 )
1771             {
1772                 access_t *p_access = p_input->p->input.p_access;
1773                 int i_title;
1774
1775                 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1776                     i_title = p_access->info.i_title - 1;
1777                 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1778                     i_title = p_access->info.i_title + 1;
1779                 else
1780                     i_title = val.i_int;
1781
1782                 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1783                 {
1784                     es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1785                     input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1786
1787                     access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1788                     stream_AccessReset( p_input->p->input.p_stream );
1789                 }
1790             }
1791             break;
1792         case INPUT_CONTROL_SET_SEEKPOINT:
1793         case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1794         case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1795             if( p_input->p->input.b_title_demux &&
1796                 p_input->p->input.i_title > 0 )
1797             {
1798                 demux_t *p_demux = p_input->p->input.p_demux;
1799                 int i_seekpoint;
1800                 int64_t i_input_time;
1801                 int64_t i_seekpoint_time;
1802
1803                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1804                 {
1805                     i_seekpoint = p_demux->info.i_seekpoint;
1806                     i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1807                     if( i_seekpoint_time >= 0 &&
1808                          !demux2_Control( p_demux,
1809                                           DEMUX_GET_TIME, &i_input_time ) )
1810                     {
1811                         if ( i_input_time < i_seekpoint_time + 3000000 )
1812                             i_seekpoint--;
1813                     }
1814                     else
1815                         i_seekpoint--;
1816                 }
1817                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1818                     i_seekpoint = p_demux->info.i_seekpoint + 1;
1819                 else
1820                     i_seekpoint = val.i_int;
1821
1822                 if( i_seekpoint >= 0 && i_seekpoint <
1823                     p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1824                 {
1825                     es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1826                     input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1827
1828                     demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1829                 }
1830             }
1831             else if( p_input->p->input.i_title > 0 )
1832             {
1833                 demux_t *p_demux = p_input->p->input.p_demux;
1834                 access_t *p_access = p_input->p->input.p_access;
1835                 int i_seekpoint;
1836                 int64_t i_input_time;
1837                 int64_t i_seekpoint_time;
1838
1839                 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1840                 {
1841                     i_seekpoint = p_access->info.i_seekpoint;
1842                     i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1843                     if( i_seekpoint_time >= 0 &&
1844                         demux2_Control( p_demux,
1845                                         DEMUX_GET_TIME, &i_input_time ) )
1846                     {
1847                         if ( i_input_time < i_seekpoint_time + 3000000 )
1848                             i_seekpoint--;
1849                     }
1850                     else
1851                         i_seekpoint--;
1852                 }
1853                 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1854                     i_seekpoint = p_access->info.i_seekpoint + 1;
1855                 else
1856                     i_seekpoint = val.i_int;
1857
1858                 if( i_seekpoint >= 0 && i_seekpoint <
1859                     p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1860                 {
1861                     es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1862                     input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1863
1864                     access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1865                                     i_seekpoint );
1866                     stream_AccessReset( p_input->p->input.p_stream );
1867                 }
1868             }
1869             break;
1870
1871         case INPUT_CONTROL_ADD_SLAVE:
1872             if( val.psz_string )
1873             {
1874                 input_source_t *slave = InputSourceNew( p_input );
1875
1876                 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1877                 {
1878                     vlc_meta_t *p_meta;
1879                     int64_t i_time;
1880
1881                     /* Add the slave */
1882                     msg_Dbg( p_input, "adding %s as slave on the fly",
1883                              val.psz_string );
1884
1885                     /* Set position */
1886                     if( demux2_Control( p_input->p->input.p_demux,
1887                                         DEMUX_GET_TIME, &i_time ) )
1888                     {
1889                         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1890                         InputSourceClean( slave );
1891                         free( slave );
1892                         break;
1893                     }
1894                     if( demux2_Control( slave->p_demux,
1895                                         DEMUX_SET_TIME, i_time ) )
1896                     {
1897                         msg_Err( p_input, "seek failed for new slave" );
1898                         InputSourceClean( slave );
1899                         free( slave );
1900                         break;
1901                     }
1902
1903                     /* Get meta (access and demux) */
1904                     p_meta = vlc_meta_New();
1905                     access2_Control( slave->p_access, ACCESS_GET_META,
1906                                      p_meta );
1907                     demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1908                     InputUpdateMeta( p_input, p_meta );
1909
1910                     TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1911                 }
1912                 else
1913                 {
1914                     free( slave );
1915                     msg_Warn( p_input, "failed to add %s as slave",
1916                               val.psz_string );
1917                 }
1918
1919                 free( val.psz_string );
1920             }
1921             break;
1922
1923         case INPUT_CONTROL_SET_BOOKMARK:
1924         default:
1925             msg_Err( p_input, "not yet implemented" );
1926             break;
1927     }
1928
1929     return b_force_update;
1930 }
1931
1932 /*****************************************************************************
1933  * UpdateFromDemux:
1934  *****************************************************************************/
1935 static int UpdateFromDemux( input_thread_t *p_input )
1936 {
1937     demux_t *p_demux = p_input->p->input.p_demux;
1938     vlc_value_t v;
1939
1940     if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1941     {
1942         v.i_int = p_demux->info.i_title;
1943         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1944
1945         input_ControlVarTitle( p_input, p_demux->info.i_title );
1946
1947         p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1948     }
1949     if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1950     {
1951         v.i_int = p_demux->info.i_seekpoint;
1952         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1953
1954         p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1955     }
1956     p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1957
1958     /* Hmmm only works with master input */
1959     if( p_input->p->input.p_demux == p_demux )
1960     {
1961         int i_title_end = p_input->p->input.i_title_end -
1962             p_input->p->input.i_title_offset;
1963         int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1964             p_input->p->input.i_seekpoint_offset;
1965
1966         if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1967         {
1968             if( p_demux->info.i_title > i_title_end ||
1969                 ( p_demux->info.i_title == i_title_end &&
1970                   p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1971         }
1972         else if( i_seekpoint_end >=0 )
1973         {
1974             if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1975         }
1976         else if( i_title_end >= 0 )
1977         {
1978             if( p_demux->info.i_title > i_title_end ) return 0;
1979         }
1980     }
1981
1982     return 1;
1983 }
1984
1985 /*****************************************************************************
1986  * UpdateFromAccess:
1987  *****************************************************************************/
1988 static int UpdateFromAccess( input_thread_t *p_input )
1989 {
1990     access_t *p_access = p_input->p->input.p_access;
1991     vlc_value_t v;
1992
1993     if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1994     {
1995         v.i_int = p_access->info.i_title;
1996         var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1997
1998         input_ControlVarTitle( p_input, p_access->info.i_title );
1999
2000         stream_AccessUpdate( p_input->p->input.p_stream );
2001
2002         p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2003     }
2004     if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2005     {
2006         v.i_int = p_access->info.i_seekpoint;
2007         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2008         p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2009     }
2010     if( p_access->info.i_update & INPUT_UPDATE_META )
2011     {
2012         /* TODO maybe multi - access ? */
2013         vlc_meta_t *p_meta = vlc_meta_New();
2014         access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2015         InputUpdateMeta( p_input, p_meta );
2016         var_SetBool( p_input, "item-change", p_input->p->input.p_item->i_id );
2017         p_access->info.i_update &= ~INPUT_UPDATE_META;
2018     }
2019
2020     p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2021
2022     /* Hmmm only works with master input */
2023     if( p_input->p->input.p_access == p_access )
2024     {
2025         int i_title_end = p_input->p->input.i_title_end -
2026             p_input->p->input.i_title_offset;
2027         int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2028             p_input->p->input.i_seekpoint_offset;
2029
2030         if( i_title_end >= 0 && i_seekpoint_end >=0 )
2031         {
2032             if( p_access->info.i_title > i_title_end ||
2033                 ( p_access->info.i_title == i_title_end &&
2034                   p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2035         }
2036         else if( i_seekpoint_end >=0 )
2037         {
2038             if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2039         }
2040         else if( i_title_end >= 0 )
2041         {
2042             if( p_access->info.i_title > i_title_end ) return 0;
2043         }
2044     }
2045
2046     return 1;
2047 }
2048
2049 /*****************************************************************************
2050  * UpdateItemLength:
2051  *****************************************************************************/
2052 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2053 {
2054     input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2055
2056     if( !p_input->b_preparsing )
2057     {
2058         pl_Yield( p_input );
2059         var_SetInteger( pl_Get( p_input ), "item-change",
2060                         p_input->p->input.p_item->i_id );
2061         pl_Release( p_input )
2062     }
2063 }
2064
2065 /*****************************************************************************
2066  * InputSourceNew:
2067  *****************************************************************************/
2068 static input_source_t *InputSourceNew( input_thread_t *p_input )
2069 {
2070     input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2071
2072     if( !in )
2073     {
2074         msg_Err( p_input, "out of memory for new input source" );
2075         return NULL;
2076     }
2077
2078     in->p_item   = NULL;
2079     in->p_access = NULL;
2080     in->p_stream = NULL;
2081     in->p_demux  = NULL;
2082     in->b_title_demux = VLC_FALSE;
2083     TAB_INIT( in->i_title, in->title );
2084     in->b_can_pace_control = VLC_TRUE;
2085     in->b_eof = VLC_FALSE;
2086     in->f_fps = 0.0;
2087     in->i_cr_average = 0;
2088
2089     return in;
2090 }
2091
2092 /*****************************************************************************
2093  * InputSourceInit:
2094  *****************************************************************************/
2095 static int InputSourceInit( input_thread_t *p_input,
2096                             input_source_t *in, const char *psz_mrl,
2097                             const char *psz_forced_demux )
2098 {
2099     char psz_dup[strlen (psz_mrl) + 1];
2100     const char *psz_access;
2101     const char *psz_demux;
2102     char *psz_path;
2103     char *psz_tmp;
2104     char *psz;
2105     vlc_value_t val;
2106     double f_fps;
2107
2108     strcpy( psz_dup, psz_mrl );
2109
2110     if( !in ) return VLC_EGENERIC;
2111     if( !p_input ) return VLC_EGENERIC;
2112
2113     /* Split uri */
2114     if( !p_input->b_preparsing )
2115     {
2116         MRLSplit( VLC_OBJECT(p_input), psz_dup,
2117                   &psz_access, &psz_demux, &psz_path );
2118
2119         msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2120                  psz_mrl, psz_access, psz_demux, psz_path );
2121
2122         /* Hack to allow udp://@:port syntax */
2123         if( !psz_access ||
2124             (strncmp( psz_access, "udp", 3 ) &&
2125              strncmp( psz_access, "rtp", 3 )) )
2126
2127         /* Find optional titles and seekpoints */
2128         MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2129                      &in->i_seekpoint_start, &in->i_seekpoint_end );
2130
2131         if( psz_forced_demux && *psz_forced_demux )
2132         {
2133             psz_demux = psz_forced_demux;
2134         }
2135         else if( !psz_demux || *psz_demux == '\0' )
2136         {
2137             /* special hack for forcing a demuxer with --demux=module
2138              * (and do nothing with a list) */
2139             char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2140
2141             if( psz_var_demux != NULL &&
2142                 !strchr(psz_var_demux, ',' ) &&
2143                 !strchr(psz_var_demux, ':' ) )
2144             {
2145                 psz_demux = psz_var_demux;
2146
2147                 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2148             }
2149         }
2150
2151         /* Try access_demux if no demux given */
2152         if( *psz_demux == '\0' )
2153         {
2154             in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2155                                       NULL, p_input->p->p_es_out, VLC_FALSE );
2156         }
2157     }
2158     else
2159     {
2160         psz_path = psz_dup;
2161         if( !strncmp( psz_path, "file://", 7 ) )
2162             psz_path += 7;
2163         msg_Dbg( p_input, "trying to pre-parse %s",  psz_path );
2164         psz_demux = "";
2165         psz_access = "file";
2166     }
2167
2168     if( in->p_demux )
2169     {
2170         int64_t i_pts_delay;
2171
2172         /* Get infos from access_demux */
2173         demux2_Control( in->p_demux,
2174                         DEMUX_GET_PTS_DELAY, &i_pts_delay );
2175         p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2176
2177         in->b_title_demux = VLC_TRUE;
2178         if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2179                             &in->title, &in->i_title,
2180                             &in->i_title_offset, &in->i_seekpoint_offset ) )
2181         {
2182             in->i_title = 0;
2183             in->title   = NULL;
2184         }
2185         demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2186                         &in->b_can_pace_control );
2187         demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2188                         &in->b_can_pause );
2189
2190         /* FIXME todo
2191         demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2192                         &val.b_bool );
2193         */
2194     }
2195     else
2196     {
2197         int64_t i_pts_delay;
2198
2199         input_ChangeState( p_input, OPENING_S );
2200
2201         /* Now try a real access */
2202         in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2203                                     p_input->b_preparsing );
2204
2205         /* Access failed, URL encoded ? */
2206         if( in->p_access == NULL && strchr( psz_path, '%' ) )
2207         {
2208             decode_URI( psz_path );
2209
2210             msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2211                      psz_access, psz_demux, psz_path );
2212
2213             in->p_access = access2_New( p_input,
2214                                         psz_access, psz_demux, psz_path,
2215                                         p_input->b_preparsing );
2216         }
2217         /* access failed, maybe our access detection was wrong.
2218          * Retry with the full name */
2219         if( in->p_access == NULL && strchr( psz_mrl, ':' ) )
2220         {
2221             msg_Dbg( p_input, "retrying with access `' demux `' path `%s'",
2222                      psz_mrl );
2223              in->p_access = access2_New( p_input,
2224                                          "", "", psz_mrl,
2225                                          p_input->b_preparsing );
2226         }
2227         if( in->p_access == NULL )
2228         {
2229             msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2230                                                          msg_StackMsg() );
2231             intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2232                             _("Your input can't be opened"),
2233                             _("VLC is unable to open the MRL '%s'."
2234                             " Check the log for details."), psz_mrl );
2235             goto error;
2236         }
2237
2238         /* */
2239         psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2240         while( psz && *psz )
2241         {
2242             access_t *p_access = in->p_access;
2243             char *end = strchr( psz, ':' );
2244
2245             if( end )
2246                 *end++ = '\0';
2247
2248             in->p_access = access2_FilterNew( in->p_access, psz );
2249             if( in->p_access == NULL )
2250             {
2251                 in->p_access = p_access;
2252                 msg_Warn( p_input, "failed to insert access filter %s",
2253                           psz );
2254             }
2255
2256             psz = end;
2257         }
2258         free( psz_tmp );
2259
2260         /* Get infos from access */
2261         if( !p_input->b_preparsing )
2262         {
2263             access2_Control( in->p_access,
2264                              ACCESS_GET_PTS_DELAY, &i_pts_delay );
2265             p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2266
2267             in->b_title_demux = VLC_FALSE;
2268             if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2269                                  &in->title, &in->i_title,
2270                                 &in->i_title_offset, &in->i_seekpoint_offset ) )
2271
2272             {
2273                 in->i_title = 0;
2274                 in->title   = NULL;
2275             }
2276             access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2277                              &in->b_can_pace_control );
2278             access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2279                              &in->b_can_pause );
2280             access2_Control( in->p_access, ACCESS_CAN_SEEK,
2281                              &val.b_bool );
2282             var_Set( p_input, "seekable", val );
2283         }
2284
2285         input_ChangeState( p_input, BUFFERING_S );
2286
2287         /* Create the stream_t */
2288         in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2289         if( in->p_stream == NULL )
2290         {
2291             msg_Warn( p_input, "cannot create a stream_t from access" );
2292             goto error;
2293         }
2294
2295         /* Open a demuxer */
2296         if( *psz_demux == '\0' && *in->p_access->psz_demux )
2297         {
2298             psz_demux = in->p_access->psz_demux;
2299         }
2300
2301         {
2302             /* Take access redirections into account */
2303             char *psz_real_path;
2304             char *psz_buf = NULL;
2305             if( in->p_access->psz_path )
2306             {
2307                 const char *psz_a, *psz_d;
2308                 psz_buf = strdup( in->p_access->psz_path );
2309                 MRLSplit( VLC_OBJECT(p_input), psz_buf,
2310                           &psz_a, &psz_d, &psz_real_path );
2311             }
2312             else
2313             {
2314                 psz_real_path = psz_path;
2315             }
2316             in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2317                                       psz_real_path,
2318                                       in->p_stream, p_input->p->p_es_out,
2319                                       p_input->b_preparsing );
2320             free( psz_buf );
2321         }
2322
2323         if( in->p_demux == NULL )
2324         {
2325             msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2326                      psz_access, psz_demux, psz_path );
2327             intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2328                             _("Can't recognize the input's format"),
2329                             _("The format of '%s' can't be detected. "
2330                             "Have a look the log for details."), psz_mrl );
2331             goto error;
2332         }
2333
2334         /* Get title from demux */
2335         if( !p_input->b_preparsing && in->i_title <= 0 )
2336         {
2337             if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2338                                 &in->title, &in->i_title,
2339                                 &in->i_title_offset, &in->i_seekpoint_offset ))
2340             {
2341                 TAB_INIT( in->i_title, in->title );
2342             }
2343             else
2344             {
2345                 in->b_title_demux = VLC_TRUE;
2346             }
2347         }
2348     }
2349
2350     /* get attachment
2351      * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2352     if( 1 || !p_input->b_preparsing )
2353     {
2354         int i_attachment;
2355         input_attachment_t **attachment;
2356         if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2357                              &attachment, &i_attachment ) )
2358         {
2359             int i;
2360             vlc_mutex_lock( &p_input->p->input.p_item->lock );
2361             p_input->p->attachment = realloc( p_input->p->attachment,
2362                     sizeof(input_attachment_t**) * ( p_input->p->i_attachment + i_attachment ) );
2363             for( i = 0; i < i_attachment; i++ )
2364                 p_input->p->attachment[p_input->p->i_attachment++] = attachment[i];
2365             if( attachment )
2366                 free( attachment );
2367             vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2368         }
2369     }
2370     if( !demux2_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2371     {
2372         vlc_mutex_lock( &p_input->p->input.p_item->lock );
2373         in->f_fps = f_fps;
2374         vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2375     }
2376
2377     if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2378         in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2379
2380     return VLC_SUCCESS;
2381
2382 error:
2383     input_ChangeState( p_input, ERROR_S );
2384
2385     if( in->p_demux )
2386         demux2_Delete( in->p_demux );
2387
2388     if( in->p_stream )
2389         stream_Delete( in->p_stream );
2390
2391     if( in->p_access )
2392         access2_Delete( in->p_access );
2393
2394     return VLC_EGENERIC;
2395 }
2396
2397 /*****************************************************************************
2398  * InputSourceClean:
2399  *****************************************************************************/
2400 static void InputSourceClean( input_source_t *in )
2401 {
2402     int i;
2403
2404     if( in->p_demux )
2405         demux2_Delete( in->p_demux );
2406
2407     if( in->p_stream )
2408         stream_Delete( in->p_stream );
2409
2410     if( in->p_access )
2411         access2_Delete( in->p_access );
2412
2413     if( in->i_title > 0 )
2414     {
2415         for( i = 0; i < in->i_title; i++ )
2416             vlc_input_title_Delete( in->title[i] );
2417         TAB_CLEAN( in->i_title, in->title );
2418     }
2419 }
2420
2421 static void SlaveDemux( input_thread_t *p_input )
2422 {
2423     int64_t i_time;
2424     int i;
2425
2426     if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2427     {
2428         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2429         return;
2430     }
2431
2432     for( i = 0; i < p_input->p->i_slave; i++ )
2433     {
2434         input_source_t *in = p_input->p->slave[i];
2435         int i_ret = 1;
2436
2437         if( in->b_eof )
2438             continue;
2439
2440         if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2441         {
2442             for( ;; )
2443             {
2444                 int64_t i_stime;
2445                 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2446                 {
2447                     msg_Err( p_input, "slave[%d] doesn't like "
2448                              "DEMUX_GET_TIME -> EOF", i );
2449                     i_ret = 0;
2450                     break;
2451                 }
2452
2453                 if( i_stime >= i_time )
2454                     break;
2455
2456                 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2457                     break;
2458             }
2459         }
2460         else
2461         {
2462             i_ret = in->p_demux->pf_demux( in->p_demux );
2463         }
2464
2465         if( i_ret <= 0 )
2466         {
2467             msg_Dbg( p_input, "slave %d EOF", i );
2468             in->b_eof = VLC_TRUE;
2469         }
2470     }
2471 }
2472
2473 static void SlaveSeek( input_thread_t *p_input )
2474 {
2475     int64_t i_time;
2476     int i;
2477
2478     if( !p_input ) return;
2479
2480     if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2481     {
2482         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2483         return;
2484     }
2485
2486     for( i = 0; i < p_input->p->i_slave; i++ )
2487     {
2488         input_source_t *in = p_input->p->slave[i];
2489
2490         if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2491         {
2492             msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2493             in->b_eof = VLC_TRUE;
2494         }
2495     }
2496 }
2497
2498 /*****************************************************************************
2499  * InputMetaUser:
2500  *****************************************************************************/
2501 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2502 {
2503     vlc_value_t val;
2504
2505     if( !p_meta ) return;
2506
2507     /* Get meta information from user */
2508 #define GET_META( field, s ) \
2509     var_Get( p_input, (s), &val );  \
2510     if( *val.psz_string ) \
2511         vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2512     free( val.psz_string )
2513
2514     GET_META( Title, "meta-title" );
2515     GET_META( Artist, "meta-artist" );
2516     GET_META( Genre, "meta-genre" );
2517     GET_META( Copyright, "meta-copyright" );
2518     GET_META( Description, "meta-description" );
2519     GET_META( Date, "meta-date" );
2520     GET_META( URL, "meta-url" );
2521 #undef GET_META
2522 }
2523
2524 /*****************************************************************************
2525  * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2526  * arturl and locking issue.
2527  *****************************************************************************/
2528 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2529 {
2530     input_item_t *p_item = p_input->p->input.p_item;
2531     char * psz_arturl = NULL;
2532     char *psz_title = NULL;
2533     int i;
2534     int i_arturl_event = VLC_FALSE;
2535
2536     if( !p_meta )
2537         return;
2538
2539     psz_arturl = input_item_GetArtURL( p_item );
2540
2541     vlc_mutex_lock( &p_item->lock );
2542     if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2543         psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2544
2545     vlc_meta_Merge( p_item->p_meta, p_meta );
2546
2547     if( psz_arturl && *psz_arturl )
2548     {
2549         vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2550         i_arturl_event = VLC_TRUE;
2551     }
2552
2553     vlc_meta_Delete( p_meta );
2554
2555     if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2556     {
2557         /* Don't look for art cover if sout
2558          * XXX It can change when sout has meta data support */
2559         if( p_input->p->p_sout && !p_input->b_preparsing )
2560         {
2561             vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2562             i_arturl_event = VLC_TRUE;
2563
2564         }
2565         else
2566             input_ExtractAttachmentAndCacheArt( p_input );
2567     }
2568     free( psz_arturl );
2569  
2570     input_item_SetPreparsed( p_item, VLC_TRUE );
2571
2572     /* A bit ugly */
2573     p_meta = NULL;
2574     if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2575     {
2576         p_meta = vlc_meta_New();
2577         vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2578     }
2579     vlc_mutex_unlock( &p_item->lock );
2580
2581     if( i_arturl_event == VLC_TRUE )
2582     {
2583         vlc_event_t event;
2584
2585         /* Notify interested third parties */
2586         event.type = vlc_InputItemMetaChanged;
2587         event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2588         vlc_event_send( &p_item->event_manager, &event );
2589     }
2590
2591     if( psz_title )
2592     {
2593         input_Control( p_input, INPUT_SET_NAME, psz_title );
2594         free( psz_title );
2595     }
2596
2597     if( p_meta )
2598     {
2599         char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2600         for( i = 0; ppsz_all_keys[i]; i++ )
2601         {
2602             input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2603                     vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2604             free( ppsz_all_keys[i] );
2605         }
2606         free( ppsz_all_keys );
2607         vlc_meta_Delete( p_meta );
2608     }
2609
2610     /** \todo handle sout meta */
2611 }
2612
2613
2614 static inline vlc_bool_t IsValidAccess( const char *psz )
2615 {
2616     char c;
2617
2618     while( ( c = *psz ) != '\0' )
2619     {
2620         if( c == ':' )
2621             return VLC_TRUE;
2622
2623         if( ( !isascii( c ) || !isalnum( c ) ) && c != '-' && ( c != '/' ) )
2624             return VLC_FALSE;
2625         psz++;
2626     }
2627     /* should not happen though */
2628     return VLC_FALSE;
2629 }
2630
2631
2632 /*****************************************************************************
2633  * MRLSplit: parse the access, demux and url part of the
2634  *           Media Resource Locator.
2635  *****************************************************************************/
2636 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2637                const char **ppsz_access, const char **ppsz_demux,
2638                char **ppsz_path )
2639 {
2640     const char *psz_access = "";
2641     const char *psz_demux  = "";
2642     char *psz_path;
2643     char *psz;
2644
2645     psz = strchr( psz_dup, ':' );
2646
2647     if( psz != NULL )
2648     {
2649         /* Guess whether ':' is part of a local filename, or separates
2650          * access/demux from path */
2651         if( !IsValidAccess( psz_dup ) )
2652             psz = NULL;
2653 #if defined( WIN32 ) || defined( UNDER_CE )
2654         else if( ( psz - psz_dup == 1 ) && isalpha( psz_dup[0] ) )
2655         {
2656             msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2657             psz = NULL;
2658         }
2659 #endif
2660     }
2661
2662     if( psz != NULL )
2663     {
2664         *psz++ = '\0';
2665         if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2666
2667         psz_path = psz;
2668
2669         psz = strchr( psz_dup, '/' );
2670         if( psz )
2671         {
2672             *psz++ = '\0';
2673             psz_demux = psz;
2674         }
2675
2676         psz_access = psz_dup;
2677     }
2678     else
2679     {
2680         psz_path = psz_dup;
2681     }
2682
2683     *ppsz_access = psz_access;
2684     *ppsz_demux = psz_demux;
2685     *ppsz_path = psz_path;
2686 }
2687
2688 /*****************************************************************************
2689  * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2690  *
2691  * Syntax:
2692  * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2693  *****************************************************************************/
2694 static void MRLSections( input_thread_t *p_input, char *psz_source,
2695                          int *pi_title_start, int *pi_title_end,
2696                          int *pi_chapter_start, int *pi_chapter_end )
2697 {
2698     char *psz, *psz_end, *psz_next, *psz_check;
2699
2700     *pi_title_start = *pi_title_end = -1;
2701     *pi_chapter_start = *pi_chapter_end = -1;
2702
2703     /* Start by parsing titles and chapters */
2704     if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2705
2706     /* Check we are really dealing with a title/chapter section */
2707     psz_check = psz + 1;
2708     if( !*psz_check ) return;
2709     if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2710     if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2711     if( *psz_check == ':' && ++psz_check )
2712         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2713     if( *psz_check != '-' && *psz_check ) return;
2714     if( *psz_check == '-' && ++psz_check )
2715         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2716     if( *psz_check != ':' && *psz_check ) return;
2717     if( *psz_check == ':' && ++psz_check )
2718         if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2719     if( *psz_check ) return;
2720
2721     /* Separate start and end */
2722     *psz++ = 0;
2723     if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2724
2725     /* Look for the start title */
2726     *pi_title_start = strtol( psz, &psz_next, 0 );
2727     if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2728     *pi_title_end = *pi_title_start;
2729     psz = psz_next;
2730
2731     /* Look for the start chapter */
2732     if( *psz ) psz++;
2733     *pi_chapter_start = strtol( psz, &psz_next, 0 );
2734     if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2735     *pi_chapter_end = *pi_chapter_start;
2736
2737     if( psz_end )
2738     {
2739         /* Look for the end title */
2740         *pi_title_end = strtol( psz_end, &psz_next, 0 );
2741         if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2742         psz_end = psz_next;
2743
2744         /* Look for the end chapter */
2745         if( *psz_end ) psz_end++;
2746         *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2747         if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2748     }
2749
2750     msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2751              psz_source, *pi_title_start, *pi_chapter_start,
2752              *pi_title_end, *pi_chapter_end );
2753 }
2754
2755 /*****************************************************************************
2756  * input_AddSubtitles: add a subtitles file and enable it
2757  *****************************************************************************/
2758 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2759                                vlc_bool_t b_check_extension )
2760 {
2761     input_source_t *sub;
2762     vlc_value_t count;
2763     vlc_value_t list;
2764     char *psz_path, *psz_extension;
2765
2766     if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2767     {
2768         return VLC_FALSE;
2769     }
2770
2771     /* if we are provided a subtitle.sub file,
2772      * see if we don't have a subtitle.idx and use it instead */
2773     psz_path = strdup( psz_subtitle );
2774     if( psz_path )
2775     {
2776         psz_extension = strrchr( psz_path, '.');
2777         if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2778         {
2779             FILE *f;
2780
2781             strcpy( psz_extension, ".idx" );
2782             /* FIXME: a portable wrapper for stat() or access() would be more suited */
2783             if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2784             {
2785                 fclose( f );
2786                 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2787                          psz_path, psz_subtitle );
2788                 strcpy( psz_subtitle, psz_path );
2789             }
2790         }
2791         free( psz_path );
2792     }
2793
2794     var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2795
2796     sub = InputSourceNew( p_input );
2797     if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2798     {
2799         TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2800
2801         /* Select the ES */
2802         if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2803         {
2804             if( count.i_int == 0 )
2805                 count.i_int++;
2806             /* if it was first one, there is disable too */
2807
2808             if( count.i_int < list.p_list->i_count )
2809             {
2810                 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2811                                    &list.p_list->p_values[count.i_int] );
2812             }
2813             var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2814         }
2815     }
2816     else free( sub );
2817
2818     return VLC_TRUE;
2819 }