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