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