Remove useless test before freeing something.
[vlc/pdherbemont.git] / src / input / input.c
blob4edba5df776476ec3b57886ff60a533c7511e184
1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 the VideoLAN team
5 * $Id$
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc/vlc.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <assert.h>
38 #include "input_internal.h"
40 #include <vlc_sout.h>
41 #include "../stream_output/stream_output.h"
43 #include <vlc_playlist.h>
44 #include <vlc_interface.h>
45 #include <vlc_url.h>
46 #include <vlc_demux.h>
47 #include <vlc_charset.h>
49 #ifdef HAVE_SYS_STAT_H
50 # include <sys/stat.h>
51 #endif
53 /*****************************************************************************
54 * Local prototypes
55 *****************************************************************************/
56 static int Run ( input_thread_t *p_input );
57 static int RunAndDestroy ( input_thread_t *p_input );
59 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
60 const char *, vlc_bool_t, sout_instance_t * );
61 static int Init ( input_thread_t *p_input );
62 static void Error ( input_thread_t *p_input );
63 static void End ( input_thread_t *p_input );
64 static void MainLoop( input_thread_t *p_input );
65 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout );
67 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
68 static void ControlReduce( input_thread_t * );
69 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
71 static int UpdateFromAccess( input_thread_t * );
72 static int UpdateFromDemux( input_thread_t * );
74 static void UpdateItemLength( input_thread_t *, int64_t i_length );
76 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
78 static input_source_t *InputSourceNew( input_thread_t *);
79 static int InputSourceInit( input_thread_t *, input_source_t *,
80 const char *, const char *psz_forced_demux );
81 static void InputSourceClean( input_source_t * );
82 /* TODO */
83 //static void InputGetAttachments( input_thread_t *, input_source_t * );
84 static void SlaveDemux( input_thread_t *p_input );
85 static void SlaveSeek( input_thread_t *p_input );
87 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
88 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
90 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t * );
91 static void SoutKeep( sout_instance_t * );
93 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
94 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
95 int i_new, input_attachment_t **pp_new );
97 /*****************************************************************************
98 * This function creates a new input, and returns a pointer
99 * to its description. On error, it returns NULL.
101 * Variables for _public_ use:
102 * * Get and Set:
103 * - state
104 * - rate,rate-slower, rate-faster
105 * - position, position-offset
106 * - time, time-offset
107 * - title,title-next,title-prev
108 * - chapter,chapter-next, chapter-prev
109 * - program, audio-es, video-es, spu-es
110 * - audio-delay, spu-delay
111 * - bookmark
112 * * Get only:
113 * - length
114 * - bookmarks
115 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
116 * - can-pause
117 * * For intf callback upon changes
118 * - intf-change
119 * - rate-change for when playback rate changes
120 * TODO explain when Callback is called
121 * TODO complete this list (?)
122 *****************************************************************************/
123 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
124 const char *psz_header, vlc_bool_t b_quick, sout_instance_t *p_sout )
126 input_thread_t *p_input = NULL; /* thread descriptor */
127 vlc_value_t val;
128 int i;
130 /* Allocate descriptor */
131 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
132 if( p_input == NULL )
134 msg_Err( p_parent, "out of memory" );
135 return NULL;
137 MALLOC_NULL( p_input->p, input_thread_private_t );
139 /* One "randomly" selected input thread is responsible for computing
140 * the global stats. Check if there is already someone doing this */
141 if( p_input->p_libvlc->p_playlist->p_stats && !b_quick )
143 vlc_mutex_lock( &p_input->p_libvlc->p_playlist->p_stats->lock );
144 if( p_input->p_libvlc->p_playlist->p_stats_computer == NULL )
146 p_input->p_libvlc->p_playlist->p_stats_computer = p_input;
148 vlc_mutex_unlock( &p_input->p_libvlc->p_playlist->p_stats->lock );
151 p_input->b_preparsing = b_quick;
152 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
154 /* Init Common fields */
155 p_input->b_eof = VLC_FALSE;
156 p_input->b_can_pace_control = VLC_TRUE;
157 p_input->p->i_start = 0;
158 p_input->i_time = 0;
159 p_input->p->i_stop = 0;
160 p_input->p->i_run = 0;
161 p_input->p->i_title = 0;
162 p_input->p->title = NULL;
163 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
164 p_input->i_state = INIT_S;
165 p_input->p->i_rate = INPUT_RATE_DEFAULT;
166 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
167 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
168 p_input->p->p_es_out = NULL;
169 p_input->p->p_sout = NULL;
170 p_input->p->b_sout_keep = VLC_FALSE;
171 p_input->p->b_out_pace_control = VLC_FALSE;
172 p_input->i_pts_delay = 0;
174 /* Init Input fields */
175 p_input->p->input.p_item = p_item;
176 p_input->p->input.p_access = NULL;
177 p_input->p->input.p_stream = NULL;
178 p_input->p->input.p_demux = NULL;
179 p_input->p->input.b_title_demux = VLC_FALSE;
180 p_input->p->input.i_title = 0;
181 p_input->p->input.title = NULL;
182 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
183 p_input->p->input.b_can_pace_control = VLC_TRUE;
184 p_input->p->input.b_can_rate_control = VLC_TRUE;
185 p_input->p->input.b_rescale_ts = VLC_TRUE;
186 p_input->p->input.b_eof = VLC_FALSE;
187 p_input->p->input.i_cr_average = 0;
189 vlc_mutex_lock( &p_item->lock );
191 if( !p_item->p_stats )
192 p_item->p_stats = stats_NewInputStats( p_input );
193 vlc_mutex_unlock( &p_item->lock );
195 /* No slave */
196 p_input->p->i_slave = 0;
197 p_input->p->slave = NULL;
199 /* Init control buffer */
200 vlc_mutex_init( p_input, &p_input->p->lock_control );
201 p_input->p->i_control = 0;
203 /* Parse input options */
204 vlc_mutex_lock( &p_item->lock );
205 for( i = 0; i < p_item->i_options; i++ )
206 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i] );
207 vlc_mutex_unlock( &p_item->lock );
209 /* Create Object Variables for private use only */
210 input_ConfigVarInit( p_input );
212 /* Create Objects variables for public Get and Set */
213 input_ControlVarInit( p_input );
215 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
217 if( !p_input->b_preparsing )
219 var_Get( p_input, "bookmarks", &val );
220 if( val.psz_string )
222 /* FIXME: have a common cfg parsing routine used by sout and others */
223 char *psz_parser, *psz_start, *psz_end;
224 psz_parser = val.psz_string;
225 while( (psz_start = strchr( psz_parser, '{' ) ) )
227 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
228 char backup;
229 psz_start++;
230 psz_end = strchr( psz_start, '}' );
231 if( !psz_end ) break;
232 psz_parser = psz_end + 1;
233 backup = *psz_parser;
234 *psz_parser = 0;
235 *psz_end = ',';
236 while( (psz_end = strchr( psz_start, ',' ) ) )
238 *psz_end = 0;
239 if( !strncmp( psz_start, "name=", 5 ) )
241 p_seekpoint->psz_name = strdup(psz_start + 5);
243 else if( !strncmp( psz_start, "bytes=", 6 ) )
245 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
247 else if( !strncmp( psz_start, "time=", 5 ) )
249 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
250 1000000;
252 psz_start = psz_end + 1;
254 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
255 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
256 p_seekpoint->i_time_offset );
257 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
258 vlc_seekpoint_Delete( p_seekpoint );
259 *psz_parser = backup;
261 free( val.psz_string );
265 /* Remove 'Now playing' info as it is probably outdated */
266 input_Control( p_input, INPUT_DEL_INFO,
267 _(VLC_META_INFO_CAT),
268 _(VLC_META_NOW_PLAYING) );
269 input_item_SetNowPlaying( p_item, NULL );
271 /* */
272 if( p_input->b_preparsing )
273 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
275 /* */
276 if( p_sout )
277 p_input->p->p_sout = p_sout;
279 /* Attach only once we are ready */
280 vlc_object_attach( p_input, p_parent );
282 return p_input;
285 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout )
287 vlc_object_detach( p_input );
288 input_thread_private_t *priv = p_input->p;
290 if( pp_sout )
291 *pp_sout = NULL;
292 if( priv->p_sout )
294 if( pp_sout )
295 *pp_sout = priv->p_sout;
296 else if( priv->b_sout_keep )
297 SoutKeep( priv->p_sout );
298 else
299 sout_DeleteInstance( priv->p_sout );
302 vlc_object_release( p_input );
303 vlc_mutex_destroy( &priv->lock_control );
304 free( priv );
308 * Initialize an input thread and run it. You will need to monitor the
309 * thread to clean up after it is done
311 * \param p_parent a vlc_object
312 * \param p_item an input item
313 * \return a pointer to the spawned input thread
315 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
316 input_item_t *p_item )
318 vlc_bool_t b_sout_keep;
319 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
320 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
322 if( !p_input && p_sout )
323 SoutKeep( p_sout );
325 p_input->p->b_sout_keep = b_sout_keep;
326 return p_input;
329 /* */
330 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
331 input_item_t *p_item,
332 const char *psz_log, sout_instance_t *p_sout )
334 input_thread_t *p_input;
336 p_input = Create( p_parent, p_item, psz_log, VLC_FALSE, p_sout );
337 if( !p_input )
338 return NULL;
340 /* Create thread and wait for its readiness. */
341 if( vlc_thread_create( p_input, "input", Run,
342 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
344 input_ChangeState( p_input, ERROR_S );
345 msg_Err( p_input, "cannot create input thread" );
346 Destroy( p_input, &p_sout );
347 return NULL;
350 return p_input;
354 * Initialize an input thread and run it. This thread will clean after himself,
355 * you can forget about it. It can work either in blocking or non-blocking mode
357 * \param p_parent a vlc_object
358 * \param p_item an input item
359 * \param b_block should we block until read is finished ?
360 * \return the input object id if non blocking, an error code else
362 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
363 vlc_bool_t b_block )
365 vlc_bool_t b_sout_keep;
366 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
367 input_thread_t *p_input;
369 p_input = Create( p_parent, p_item, NULL, VLC_FALSE, p_sout );
370 if( !p_input && p_sout )
372 SoutKeep( p_sout );
373 return VLC_EGENERIC;
375 p_input->p->b_sout_keep = b_sout_keep;
377 if( b_block )
379 RunAndDestroy( p_input );
380 return VLC_SUCCESS;
382 else
384 if( vlc_thread_create( p_input, "input", RunAndDestroy,
385 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
387 input_ChangeState( p_input, ERROR_S );
388 msg_Err( p_input, "cannot create input thread" );
389 Destroy( p_input, NULL );
390 return VLC_EGENERIC;
393 return p_input->i_object_id;
397 * Initialize an input and initialize it to preparse the item
398 * This function is blocking. It will only accept to parse files
400 * \param p_parent a vlc_object_t
401 * \param p_item an input item
402 * \return VLC_SUCCESS or an error
404 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
406 input_thread_t *p_input;
408 /* Allocate descriptor */
409 p_input = Create( p_parent, p_item, NULL, VLC_TRUE, NULL );
410 if( !p_input )
411 return VLC_EGENERIC;
413 if( !Init( p_input ) )
414 End( p_input );
416 Destroy( p_input, NULL );
418 return VLC_SUCCESS;
422 * Request a running input thread to stop and die
424 * \param the input thread to stop
426 void input_StopThread( input_thread_t *p_input )
428 vlc_list_t *p_list;
429 int i;
431 /* Set die for input */
432 vlc_object_kill( p_input );
433 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
435 /* We cannot touch p_input fields directly (we come from another thread),
436 * so use the vlc_object_find way, it's perfectly safe */
438 /* Set die for all access */
439 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
440 for( i = 0; i < p_list->i_count; i++ )
442 vlc_object_kill( p_list->p_values[i].p_object );
444 vlc_list_release( p_list );
446 /* Set die for all stream */
447 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
448 for( i = 0; i < p_list->i_count; i++ )
450 vlc_object_kill( p_list->p_values[i].p_object );
452 vlc_list_release( p_list );
454 /* Set die for all demux */
455 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
456 for( i = 0; i < p_list->i_count; i++ )
458 vlc_object_kill( p_list->p_values[i].p_object );
460 vlc_list_release( p_list );
462 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
466 * Clean up a dead input thread
467 * This function does not return until the thread is effectively cancelled.
469 * \param the input thread to kill
471 void input_DestroyThread( input_thread_t *p_input )
473 input_DestroyThreadExtended( p_input, NULL );
476 void input_DestroyThreadExtended( input_thread_t *p_input, sout_instance_t **pp_sout )
478 /* Join the thread */
479 vlc_thread_join( p_input );
481 /* */
482 Destroy( p_input, pp_sout );
485 /*****************************************************************************
486 * Run: main thread loop
487 * This is the "normal" thread that spawns the input processing chain,
488 * reads the stream, cleans up and waits
489 *****************************************************************************/
490 static int Run( input_thread_t *p_input )
492 /* Signal that the thread is launched */
493 vlc_thread_ready( p_input );
495 if( Init( p_input ) )
497 /* If we failed, wait before we are killed, and exit */
498 p_input->b_error = VLC_TRUE;
499 playlist_Signal( pl_Get( p_input ) );
501 Error( p_input );
503 /* Tell we're dead */
504 p_input->b_dead = VLC_TRUE;
506 return 0;
509 MainLoop( p_input );
511 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
513 /* We have finish to demux data but not to play them */
514 while( !p_input->b_die )
516 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
517 break;
519 msg_Dbg( p_input, "waiting decoder fifos to empty" );
521 msleep( INPUT_IDLE_SLEEP );
524 /* We have finished */
525 p_input->b_eof = VLC_TRUE;
526 playlist_Signal( pl_Get( p_input ) );
529 /* Wait until we are asked to die */
530 if( !p_input->b_die )
532 Error( p_input );
535 /* Clean up */
536 End( p_input );
538 return 0;
541 /*****************************************************************************
542 * RunAndDestroy: main thread loop
543 * This is the "just forget me" thread that spawns the input processing chain,
544 * reads the stream, cleans up and releases memory
545 *****************************************************************************/
546 static int RunAndDestroy( input_thread_t *p_input )
548 /* Signal that the thread is launched */
549 vlc_thread_ready( p_input );
551 if( Init( p_input ) )
552 goto exit;
554 MainLoop( p_input );
556 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
558 /* We have finished demuxing data but not playing it */
559 while( !p_input->b_die )
561 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
562 break;
564 msg_Dbg( p_input, "waiting decoder fifos to empty" );
566 msleep( INPUT_IDLE_SLEEP );
569 /* We have finished */
570 p_input->b_eof = VLC_TRUE;
573 /* Clean up */
574 End( p_input );
576 exit:
577 /* Release memory */
578 Destroy( p_input, NULL );
579 return 0;
582 /*****************************************************************************
583 * Main loop: Fill buffers from access, and demux
584 *****************************************************************************/
585 static void MainLoop( input_thread_t *p_input )
587 int64_t i_start_mdate = mdate();
588 int64_t i_intf_update = 0;
589 int i_updates = 0;
591 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
593 vlc_bool_t b_force_update = VLC_FALSE;
594 int i_ret;
595 int i_type;
596 vlc_value_t val;
598 /* Do the read */
599 if( p_input->i_state != PAUSE_S )
601 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
602 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
603 i_ret = 0; /* EOF */
604 else
605 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
607 if( i_ret > 0 )
609 /* TODO */
610 if( p_input->p->input.b_title_demux &&
611 p_input->p->input.p_demux->info.i_update )
613 i_ret = UpdateFromDemux( p_input );
614 b_force_update = VLC_TRUE;
616 else if( !p_input->p->input.b_title_demux &&
617 p_input->p->input.p_access &&
618 p_input->p->input.p_access->info.i_update )
620 i_ret = UpdateFromAccess( p_input );
621 b_force_update = VLC_TRUE;
625 if( i_ret == 0 ) /* EOF */
627 vlc_value_t repeat;
629 var_Get( p_input, "input-repeat", &repeat );
630 if( repeat.i_int == 0 )
632 /* End of file - we do not set b_die because only the
633 * playlist is allowed to do so. */
634 input_ChangeState( p_input, END_S );
635 msg_Dbg( p_input, "EOF reached" );
636 p_input->p->input.b_eof = VLC_TRUE;
638 else
640 msg_Dbg( p_input, "repeating the same input (%d)",
641 repeat.i_int );
642 if( repeat.i_int > 0 )
644 repeat.i_int--;
645 var_Set( p_input, "input-repeat", repeat );
648 /* Seek to start title/seekpoint */
649 val.i_int = p_input->p->input.i_title_start -
650 p_input->p->input.i_title_offset;
651 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
652 val.i_int = 0;
653 input_ControlPush( p_input,
654 INPUT_CONTROL_SET_TITLE, &val );
656 val.i_int = p_input->p->input.i_seekpoint_start -
657 p_input->p->input.i_seekpoint_offset;
658 if( val.i_int > 0 /* TODO: check upper boundary */ )
659 input_ControlPush( p_input,
660 INPUT_CONTROL_SET_SEEKPOINT, &val );
662 /* Seek to start position */
663 if( p_input->p->i_start > 0 )
665 val.i_time = p_input->p->i_start;
666 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
667 &val );
669 else
671 val.f_float = 0.0;
672 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
673 &val );
676 /* */
677 i_start_mdate = mdate();
680 else if( i_ret < 0 )
682 p_input->b_error = VLC_TRUE;
685 if( i_ret > 0 && p_input->p->i_slave > 0 )
687 SlaveDemux( p_input );
690 else
692 /* Small wait */
693 msleep( 10*1000 );
696 /* Handle control */
697 vlc_mutex_lock( &p_input->p->lock_control );
698 ControlReduce( p_input );
699 while( !ControlPopNoLock( p_input, &i_type, &val ) )
701 msg_Dbg( p_input, "control type=%d", i_type );
702 if( Control( p_input, i_type, val ) )
703 b_force_update = VLC_TRUE;
705 vlc_mutex_unlock( &p_input->p->lock_control );
707 if( b_force_update || i_intf_update < mdate() )
709 vlc_value_t val;
710 double f_pos;
711 int64_t i_time, i_length;
712 /* update input status variables */
713 if( !demux2_Control( p_input->p->input.p_demux,
714 DEMUX_GET_POSITION, &f_pos ) )
716 val.f_float = (float)f_pos;
717 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
719 if( !demux2_Control( p_input->p->input.p_demux,
720 DEMUX_GET_TIME, &i_time ) )
722 p_input->i_time = i_time;
723 val.i_time = i_time;
724 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
726 if( !demux2_Control( p_input->p->input.p_demux,
727 DEMUX_GET_LENGTH, &i_length ) )
729 vlc_value_t old_val;
730 var_Get( p_input, "length", &old_val );
731 val.i_time = i_length;
732 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
734 if( old_val.i_time != val.i_time )
736 UpdateItemLength( p_input, i_length );
740 var_SetBool( p_input, "intf-change", VLC_TRUE );
741 i_intf_update = mdate() + I64C(150000);
743 /* 150ms * 8 = ~ 1 second */
744 if( ++i_updates % 8 == 0 )
746 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
747 /* Are we the thread responsible for computing global stats ? */
748 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
750 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
751 p_input->p_libvlc->p_playlist->p_stats );
757 static int Init( input_thread_t * p_input )
759 char *psz;
760 char *psz_subtitle;
761 vlc_value_t val;
762 double f_fps;
763 vlc_meta_t *p_meta;
764 int i_es_out_mode;
765 int i, i_delay;
767 /* Initialize optional stream output. (before access/demuxer)
768 * XXX: we add a special case if the uri starts by vlc.
769 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
770 * be destroyed in case of a file).
771 * (this will break playing of file starting by 'vlc:' but I don't
772 * want to add more logic, just force file by file:// or code it ;)
774 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
775 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
777 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
779 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
781 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
782 var_SetString( p_input, "sout", "" );
783 var_SetBool( p_input, "sout-all", VLC_FALSE );
784 var_SetString( p_input, "input-slave", "" );
785 var_SetInteger( p_input, "input-repeat", 0 );
786 var_SetString( p_input, "sub-file", "" );
787 var_SetBool( p_input, "sub-autodetect-file", VLC_FALSE );
791 if( !p_input->b_preparsing )
793 /* Prepare statistics */
794 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
795 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
796 if( p_input->p_libvlc->b_stats )
798 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
799 INIT_COUNTER( read_packets, INTEGER, COUNTER );
800 INIT_COUNTER( demux_read, INTEGER, COUNTER );
801 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
802 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
803 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
804 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
805 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
806 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
807 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
808 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
809 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
810 p_input->p->counters.p_sout_send_bitrate = NULL;
811 p_input->p->counters.p_sout_sent_packets = NULL;
812 p_input->p->counters.p_sout_sent_bytes = NULL;
813 if( p_input->p->counters.p_demux_bitrate )
814 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
815 if( p_input->p->counters.p_input_bitrate )
816 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
819 /* Find a usable sout and attach it to p_input */
820 psz = var_GetNonEmptyString( p_input, "sout" );
821 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
823 /* Check the validity of the provided sout */
824 if( p_input->p->p_sout )
826 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
828 msg_Dbg( p_input, "destroying unusable sout" );
830 sout_DeleteInstance( p_input->p->p_sout );
831 p_input->p->p_sout = NULL;
835 if( p_input->p->p_sout )
837 /* Reuse it */
838 msg_Dbg( p_input, "sout keep: reusing sout" );
839 msg_Dbg( p_input, "sout keep: you probably want to use "
840 "gather stream_out" );
841 vlc_object_attach( p_input->p->p_sout, p_input );
843 else
845 /* Create a new one */
846 p_input->p->p_sout = sout_NewInstance( p_input, psz );
847 if( !p_input->p->p_sout )
849 input_ChangeState( p_input, ERROR_S );
850 msg_Err( p_input, "cannot start stream output instance, " \
851 "aborting" );
852 free( psz );
853 return VLC_EGENERIC;
857 if( p_input->p_libvlc->b_stats )
859 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
860 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
861 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
862 if( p_input->p->counters.p_sout_send_bitrate )
863 p_input->p->counters.p_sout_send_bitrate->update_interval =
864 1000000;
867 else if( p_input->p->p_sout )
869 msg_Dbg( p_input, "destroying useless sout" );
871 sout_DeleteInstance( p_input->p->p_sout );
872 p_input->p->p_sout = NULL;
874 free( psz );
877 /* Create es out */
878 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
879 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
880 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
882 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
883 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
885 if( InputSourceInit( p_input, &p_input->p->input,
886 p_input->p->input.p_item->psz_uri, NULL ) )
888 goto error;
891 /* Create global title (from master) */
892 if( !p_input->b_preparsing )
894 p_input->p->i_title = p_input->p->input.i_title;
895 p_input->p->title = p_input->p->input.title;
896 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
897 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
898 if( p_input->p->i_title > 0 )
900 /* Setup variables */
901 input_ControlVarNavigation( p_input );
902 input_ControlVarTitle( p_input, 0 );
905 /* Global flag */
906 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
907 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
908 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
910 /* Fix pts delay */
911 if( p_input->i_pts_delay < 0 )
912 p_input->i_pts_delay = 0;
914 /* If the desynchronisation requested by the user is < 0, we need to
915 * cache more data. */
916 var_Get( p_input, "audio-desync", &val );
917 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
919 /* Update cr_average depending on the caching */
920 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
921 p_input->p->input.i_cr_average /= 10;
922 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
925 /* Load master infos */
926 /* Init length */
927 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
928 &val.i_time ) && val.i_time > 0 )
930 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
931 UpdateItemLength( p_input, val.i_time );
933 else
935 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
936 if( val.i_time > 0 )
937 { /* fallback: gets length from metadata */
938 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
939 UpdateItemLength( p_input, val.i_time );
943 /* Start title/chapter */
944 if( !p_input->b_preparsing )
946 val.i_int = p_input->p->input.i_title_start -
947 p_input->p->input.i_title_offset;
948 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
949 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
950 val.i_int = p_input->p->input.i_seekpoint_start -
951 p_input->p->input.i_seekpoint_offset;
952 if( val.i_int > 0 /* TODO: check upper boundary */ )
953 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
955 /* Start time*/
956 /* Set start time */
957 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
958 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
959 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
960 if( p_input->p->i_run < 0 )
962 msg_Warn( p_input, "invalid run-time ignored" );
963 p_input->p->i_run = 0;
966 if( p_input->p->i_start > 0 )
968 if( p_input->p->i_start >= val.i_time )
970 msg_Warn( p_input, "invalid start-time ignored" );
972 else
974 vlc_value_t s;
976 msg_Dbg( p_input, "starting at time: %ds",
977 (int)( p_input->p->i_start / I64C(1000000) ) );
979 s.i_time = p_input->p->i_start;
980 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
983 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
985 msg_Warn( p_input, "invalid stop-time ignored" );
986 p_input->p->i_stop = 0;
989 /* Load subtitles */
990 /* Get fps and set it if not already set */
991 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
992 f_fps > 1.0 )
994 float f_requested_fps;
996 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
997 var_SetFloat( p_input, "sub-original-fps", f_fps );
999 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1000 if( f_requested_fps != f_fps )
1002 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1003 VLC_VAR_DOINHERIT );
1004 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1008 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1009 if( i_delay != 0 )
1011 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1014 /* Look for and add subtitle files */
1015 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1016 if( psz_subtitle != NULL )
1018 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1019 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
1022 var_Get( p_input, "sub-autodetect-file", &val );
1023 if( val.b_bool )
1025 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1026 char **subs = subtitles_Detect( p_input, psz_autopath,
1027 p_input->p->input.p_item->psz_uri );
1028 input_source_t *sub;
1029 i = 0;
1030 if( psz_autopath == NULL )
1031 psz_autopath = strdup("");
1033 /* Try to autoselect the first autodetected subtitles file
1034 * if no subtitles file was specified */
1035 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1037 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
1038 free( subs[0] );
1039 i = 1;
1042 /* Then, just add the following subtitles files */
1043 for( ; subs && subs[i]; i++ )
1045 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1047 sub = InputSourceNew( p_input );
1048 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1050 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1052 else free( sub );
1054 free( subs[i] );
1056 free( subs );
1057 free( psz_autopath );
1059 free( psz_subtitle );
1061 /* Look for slave */
1062 psz = var_GetNonEmptyString( p_input, "input-slave" );
1063 if( psz != NULL )
1065 char *psz_delim;
1066 input_source_t *slave;
1067 while( psz && *psz )
1069 while( *psz == ' ' || *psz == '#' )
1071 psz++;
1073 if( ( psz_delim = strchr( psz, '#' ) ) )
1075 *psz_delim++ = '\0';
1077 if( *psz == 0 )
1079 break;
1082 msg_Dbg( p_input, "adding slave input '%s'", psz );
1083 slave = InputSourceNew( p_input );
1084 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1086 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1088 else free( slave );
1089 psz = psz_delim;
1091 free( psz );
1094 else
1096 p_input->p->i_start = 0;
1097 p_input->p->i_start = 0;
1100 /* Set up es_out */
1101 if( !p_input->b_preparsing )
1103 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1104 i_es_out_mode = ES_OUT_MODE_AUTO;
1105 val.p_list = NULL;
1106 if( p_input->p->p_sout )
1108 var_Get( p_input, "sout-all", &val );
1109 if ( val.b_bool )
1111 i_es_out_mode = ES_OUT_MODE_ALL;
1112 val.p_list = NULL;
1114 else
1116 var_Get( p_input, "programs", &val );
1117 if ( val.p_list && val.p_list->i_count )
1119 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1120 /* Note : we should remove the "program" callback. */
1122 else
1123 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1124 NULL );
1127 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1129 /* Inform the demuxer about waited group (needed only for DVB) */
1130 if( i_es_out_mode == ES_OUT_MODE_ALL )
1132 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1134 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1136 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1137 val.p_list );
1139 else
1141 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1142 (int) var_GetInteger( p_input, "program" ), NULL );
1145 if( p_input->p->p_sout )
1147 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1149 p_input->p->b_out_pace_control = VLC_FALSE;
1151 else
1153 p_input->p->b_out_pace_control = VLC_TRUE;
1156 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1158 /* We don't want a high input priority here or we'll
1159 * end-up sucking up all the CPU time */
1160 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1163 msg_Dbg( p_input, "starting in %s mode",
1164 p_input->p->b_out_pace_control ? "async" : "sync" );
1168 p_meta = vlc_meta_New();
1169 /* Get meta data from users */
1170 InputMetaUser( p_input, p_meta );
1172 /* Get meta data from master input */
1173 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1175 /* Access_file does not give any meta, and there are no slave */
1176 if( !p_input->b_preparsing )
1178 if( p_input->p->input.p_access )
1179 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1180 p_meta );
1182 /* Get meta data from slave input */
1183 for( i = 0; i < p_input->p->i_slave; i++ )
1185 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
1186 if( p_input->p->slave[i]->p_access )
1188 access2_Control( p_input->p->slave[i]->p_access,
1189 ACCESS_GET_META, p_meta );
1194 InputUpdateMeta( p_input, p_meta );
1196 if( !p_input->b_preparsing )
1198 msg_Dbg( p_input, "`%s' successfully opened",
1199 p_input->p->input.p_item->psz_uri );
1203 /* initialization is complete */
1204 input_ChangeState( p_input, PLAYING_S );
1206 return VLC_SUCCESS;
1208 error:
1209 input_ChangeState( p_input, ERROR_S );
1211 if( p_input->p->p_es_out )
1212 input_EsOutDelete( p_input->p->p_es_out );
1214 if( p_input->p->p_sout )
1216 vlc_object_detach( p_input->p->p_sout );
1217 sout_DeleteInstance( p_input->p->p_sout );
1221 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1223 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1224 stats_CounterClean( p_input->p->counters.p_##c );\
1225 p_input->p->counters.p_##c = NULL; } while(0)
1226 EXIT_COUNTER( read_bytes );
1227 EXIT_COUNTER( read_packets );
1228 EXIT_COUNTER( demux_read );
1229 EXIT_COUNTER( input_bitrate );
1230 EXIT_COUNTER( demux_bitrate );
1231 EXIT_COUNTER( played_abuffers );
1232 EXIT_COUNTER( lost_abuffers );
1233 EXIT_COUNTER( displayed_pictures );
1234 EXIT_COUNTER( lost_pictures );
1235 EXIT_COUNTER( decoded_audio );
1236 EXIT_COUNTER( decoded_video );
1237 EXIT_COUNTER( decoded_sub );
1239 if( p_input->p->p_sout )
1241 EXIT_COUNTER( sout_sent_packets );
1242 EXIT_COUNTER (sout_sent_bytes );
1243 EXIT_COUNTER( sout_send_bitrate );
1245 #undef EXIT_COUNTER
1248 /* Mark them deleted */
1249 p_input->p->input.p_demux = NULL;
1250 p_input->p->input.p_stream = NULL;
1251 p_input->p->input.p_access = NULL;
1252 p_input->p->p_es_out = NULL;
1253 p_input->p->p_sout = NULL;
1255 return VLC_EGENERIC;
1258 /*****************************************************************************
1259 * Error: RunThread() error loop
1260 *****************************************************************************
1261 * This function is called when an error occurred during thread main's loop.
1262 *****************************************************************************/
1263 static void Error( input_thread_t *p_input )
1265 input_ChangeState( p_input, ERROR_S );
1266 while( !p_input->b_die )
1268 /* Sleep a while */
1269 msleep( INPUT_IDLE_SLEEP );
1273 /*****************************************************************************
1274 * End: end the input thread
1275 *****************************************************************************/
1276 static void End( input_thread_t * p_input )
1278 int i;
1280 /* We are at the end */
1281 input_ChangeState( p_input, END_S );
1283 /* Clean control variables */
1284 input_ControlVarClean( p_input );
1286 /* Clean up master */
1287 InputSourceClean( &p_input->p->input );
1289 /* Delete slave */
1290 for( i = 0; i < p_input->p->i_slave; i++ )
1292 InputSourceClean( p_input->p->slave[i] );
1293 free( p_input->p->slave[i] );
1295 free( p_input->p->slave );
1297 /* Unload all modules */
1298 if( p_input->p->p_es_out )
1299 input_EsOutDelete( p_input->p->p_es_out );
1301 if( !p_input->b_preparsing )
1303 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1304 if( p_input->p_libvlc->b_stats )
1306 /* make sure we are up to date */
1307 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1308 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1310 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1311 p_input->p_libvlc->p_playlist->p_stats );
1312 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1314 CL_CO( read_bytes );
1315 CL_CO( read_packets );
1316 CL_CO( demux_read );
1317 CL_CO( input_bitrate );
1318 CL_CO( demux_bitrate );
1319 CL_CO( played_abuffers );
1320 CL_CO( lost_abuffers );
1321 CL_CO( displayed_pictures );
1322 CL_CO( lost_pictures );
1323 CL_CO( decoded_audio) ;
1324 CL_CO( decoded_video );
1325 CL_CO( decoded_sub) ;
1328 /* Close optional stream output instance */
1329 if( p_input->p->p_sout )
1331 CL_CO( sout_sent_packets );
1332 CL_CO( sout_sent_bytes );
1333 CL_CO( sout_send_bitrate );
1335 vlc_object_detach( p_input->p->p_sout );
1337 #undef CL_CO
1340 if( p_input->p->i_attachment > 0 )
1342 for( i = 0; i < p_input->p->i_attachment; i++ )
1343 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1344 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1347 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1349 /* Tell we're dead */
1350 p_input->b_dead = VLC_TRUE;
1353 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t *pb_sout_keep )
1355 vlc_bool_t b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1356 sout_instance_t *p_sout = NULL;
1357 int i;
1359 /* Search sout-keep options
1360 * XXX it has to be done here, but it is duplicated work :( */
1361 vlc_mutex_lock( &p_item->lock );
1362 for( i = 0; i < p_item->i_options; i++ )
1364 const char *psz_option = p_item->ppsz_options[i];
1365 if( !psz_option )
1366 continue;
1367 if( *psz_option == ':' )
1368 psz_option++;
1370 if( !strcmp( psz_option, "sout-keep" ) )
1371 b_keep_sout = VLC_TRUE;
1372 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1373 b_keep_sout = VLC_FALSE;
1375 vlc_mutex_unlock( &p_item->lock );
1377 /* Find a potential sout to reuse
1378 * XXX it might be unusable but this will be checked later */
1379 if( b_keep_sout )
1381 /* Remove the sout from the playlist garbage collector */
1382 playlist_t *p_playlist = pl_Yield( p_parent );
1384 vlc_mutex_lock( &p_playlist->gc_lock );
1385 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1386 if( p_sout )
1388 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1390 vlc_object_release( p_sout );
1391 p_sout = NULL;
1393 else
1395 vlc_object_detach( p_sout ); /* Remove it from the GC */
1397 vlc_object_release( p_sout );
1400 vlc_mutex_unlock( &p_playlist->gc_lock );
1402 pl_Release( p_parent );
1405 if( pb_sout_keep )
1406 *pb_sout_keep = b_keep_sout;
1408 return p_sout;
1410 static void SoutKeep( sout_instance_t *p_sout )
1412 /* attach sout to the playlist */
1413 playlist_t *p_playlist = pl_Yield( p_sout );
1415 msg_Dbg( p_sout, "sout has been kept" );
1416 vlc_object_attach( p_sout, p_playlist );
1418 pl_Release( p_sout );
1421 /*****************************************************************************
1422 * Control
1423 *****************************************************************************/
1424 static inline int ControlPopNoLock( input_thread_t *p_input,
1425 int *pi_type, vlc_value_t *p_val )
1427 if( p_input->p->i_control <= 0 )
1429 return VLC_EGENERIC;
1432 *pi_type = p_input->p->control[0].i_type;
1433 *p_val = p_input->p->control[0].val;
1435 p_input->p->i_control--;
1436 if( p_input->p->i_control > 0 )
1438 int i;
1440 for( i = 0; i < p_input->p->i_control; i++ )
1442 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1443 p_input->p->control[i].val = p_input->p->control[i+1].val;
1447 return VLC_SUCCESS;
1450 static void ControlReduce( input_thread_t *p_input )
1452 int i;
1454 if( !p_input )
1455 return;
1457 for( i = 1; i < p_input->p->i_control; i++ )
1459 const int i_lt = p_input->p->control[i-1].i_type;
1460 const int i_ct = p_input->p->control[i].i_type;
1462 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1463 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1464 i_lt, i_ct );
1466 if( i_lt == i_ct &&
1467 ( i_ct == INPUT_CONTROL_SET_STATE ||
1468 i_ct == INPUT_CONTROL_SET_RATE ||
1469 i_ct == INPUT_CONTROL_SET_POSITION ||
1470 i_ct == INPUT_CONTROL_SET_TIME ||
1471 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1472 i_ct == INPUT_CONTROL_SET_TITLE ||
1473 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1474 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1476 int j;
1477 // msg_Dbg( p_input, "merged at %d", i );
1478 /* Remove the i-1 */
1479 for( j = i; j < p_input->p->i_control; j++ )
1480 p_input->p->control[j-1] = p_input->p->control[j];
1481 p_input->p->i_control--;
1483 else
1485 /* TODO but that's not that important
1486 - merge SET_X with SET_X_CMD
1487 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1488 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1495 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1496 vlc_value_t val )
1498 vlc_bool_t b_force_update = VLC_FALSE;
1500 if( !p_input ) return b_force_update;
1502 switch( i_type )
1504 case INPUT_CONTROL_SET_DIE:
1505 msg_Dbg( p_input, "control: stopping input" );
1506 /* Mark all submodules to die */
1507 if( p_input->p->input.p_access )
1508 vlc_object_kill( p_input->p->input.p_access );
1509 if( p_input->p->input.p_stream )
1510 vlc_object_kill( p_input->p->input.p_stream );
1511 vlc_object_kill( p_input->p->input.p_demux );
1513 vlc_object_kill( p_input );
1514 break;
1516 case INPUT_CONTROL_SET_POSITION:
1517 case INPUT_CONTROL_SET_POSITION_OFFSET:
1519 double f_pos;
1520 if( i_type == INPUT_CONTROL_SET_POSITION )
1522 f_pos = val.f_float;
1524 else
1526 /* Should not fail */
1527 demux2_Control( p_input->p->input.p_demux,
1528 DEMUX_GET_POSITION, &f_pos );
1529 f_pos += val.f_float;
1531 if( f_pos < 0.0 ) f_pos = 0.0;
1532 if( f_pos > 1.0 ) f_pos = 1.0;
1533 /* Reset the decoders states and clock sync (before calling the demuxer */
1534 input_EsOutChangePosition( p_input->p->p_es_out );
1535 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1536 f_pos ) )
1538 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1539 "%2.1f%% failed", f_pos * 100 );
1541 else
1543 if( p_input->p->i_slave > 0 )
1544 SlaveSeek( p_input );
1546 b_force_update = VLC_TRUE;
1548 break;
1551 case INPUT_CONTROL_SET_TIME:
1552 case INPUT_CONTROL_SET_TIME_OFFSET:
1554 int64_t i_time;
1555 int i_ret;
1557 if( i_type == INPUT_CONTROL_SET_TIME )
1559 i_time = val.i_time;
1561 else
1563 /* Should not fail */
1564 demux2_Control( p_input->p->input.p_demux,
1565 DEMUX_GET_TIME, &i_time );
1566 i_time += val.i_time;
1568 if( i_time < 0 ) i_time = 0;
1570 /* Reset the decoders states and clock sync (before calling the demuxer */
1571 input_EsOutChangePosition( p_input->p->p_es_out );
1573 i_ret = demux2_Control( p_input->p->input.p_demux,
1574 DEMUX_SET_TIME, i_time );
1575 if( i_ret )
1577 int64_t i_length;
1579 /* Emulate it with a SET_POS */
1580 demux2_Control( p_input->p->input.p_demux,
1581 DEMUX_GET_LENGTH, &i_length );
1582 if( i_length > 0 )
1584 double f_pos = (double)i_time / (double)i_length;
1585 i_ret = demux2_Control( p_input->p->input.p_demux,
1586 DEMUX_SET_POSITION, f_pos );
1589 if( i_ret )
1591 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1592 " failed or not possible", i_time );
1594 else
1596 if( p_input->p->i_slave > 0 )
1597 SlaveSeek( p_input );
1599 b_force_update = VLC_TRUE;
1601 break;
1604 case INPUT_CONTROL_SET_STATE:
1605 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1606 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1608 int i_ret;
1609 if( p_input->p->input.p_access )
1610 i_ret = access2_Control( p_input->p->input.p_access,
1611 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1612 else
1613 i_ret = demux2_Control( p_input->p->input.p_demux,
1614 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1616 if( i_ret )
1618 /* FIXME What to do ? */
1619 msg_Warn( p_input, "cannot unset pause -> EOF" );
1620 vlc_mutex_unlock( &p_input->p->lock_control );
1621 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1622 vlc_mutex_lock( &p_input->p->lock_control );
1625 b_force_update = VLC_TRUE;
1627 /* Switch to play */
1628 p_input->i_state = PLAYING_S;
1629 val.i_int = PLAYING_S;
1630 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1632 /* */
1633 if( !i_ret )
1634 input_EsOutChangeState( p_input->p->p_es_out );
1636 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1637 p_input->p->b_can_pause )
1639 int i_ret;
1640 if( p_input->p->input.p_access )
1641 i_ret = access2_Control( p_input->p->input.p_access,
1642 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1643 else
1644 i_ret = demux2_Control( p_input->p->input.p_demux,
1645 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1647 b_force_update = VLC_TRUE;
1649 if( i_ret )
1651 msg_Warn( p_input, "cannot set pause state" );
1652 val.i_int = p_input->i_state;
1654 else
1656 val.i_int = PAUSE_S;
1659 /* Switch to new state */
1660 p_input->i_state = val.i_int;
1661 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1663 /* */
1664 if( !i_ret )
1665 input_EsOutChangeState( p_input->p->p_es_out );
1667 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1669 b_force_update = VLC_TRUE;
1671 /* Correct "state" value */
1672 val.i_int = p_input->i_state;
1673 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1675 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1677 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1679 break;
1681 case INPUT_CONTROL_SET_RATE:
1682 case INPUT_CONTROL_SET_RATE_SLOWER:
1683 case INPUT_CONTROL_SET_RATE_FASTER:
1685 int i_rate;
1687 if( i_type == INPUT_CONTROL_SET_RATE )
1689 i_rate = val.i_int;
1691 else
1693 static const int ppi_factor[][2] = {
1694 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1695 {1,1},
1696 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1697 {0,0}
1699 int i_error;
1700 int i_idx;
1701 int i;
1703 i_error = INT_MAX;
1704 i_idx = -1;
1705 for( i = 0; ppi_factor[i][0] != 0; i++ )
1707 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1708 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1709 if( i_test_e < i_error )
1711 i_idx = i;
1712 i_error = i_test_e;
1715 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1717 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1719 if( ppi_factor[i_idx+1][0] > 0 )
1720 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1721 else
1722 i_rate = INPUT_RATE_MAX+1;
1724 else
1726 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1727 if( i_idx > 0 )
1728 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1729 else
1730 i_rate = INPUT_RATE_MIN-1;
1734 if( i_rate < INPUT_RATE_MIN )
1736 msg_Dbg( p_input, "cannot set rate faster" );
1737 i_rate = INPUT_RATE_MIN;
1739 else if( i_rate > INPUT_RATE_MAX )
1741 msg_Dbg( p_input, "cannot set rate slower" );
1742 i_rate = INPUT_RATE_MAX;
1744 if( i_rate != INPUT_RATE_DEFAULT &&
1745 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1746 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1748 msg_Dbg( p_input, "cannot change rate" );
1749 i_rate = INPUT_RATE_DEFAULT;
1751 if( i_rate != p_input->p->i_rate &&
1752 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1754 int i_ret;
1755 if( p_input->p->input.p_access )
1756 i_ret = VLC_EGENERIC;
1757 else
1758 i_ret = demux2_Control( p_input->p->input.p_demux,
1759 DEMUX_SET_RATE, &i_rate );
1760 if( i_ret )
1762 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1763 i_rate = p_input->p->i_rate;
1767 /* */
1768 if( i_rate != p_input->p->i_rate )
1770 val.i_int = i_rate;
1771 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1772 var_SetBool( p_input, "rate-change", VLC_TRUE );
1774 p_input->p->i_rate = i_rate;
1776 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1777 if( p_input->p->input.b_rescale_ts )
1778 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1780 b_force_update = VLC_TRUE;
1782 break;
1785 case INPUT_CONTROL_SET_PROGRAM:
1786 /* No need to force update, es_out does it if needed */
1787 es_out_Control( p_input->p->p_es_out,
1788 ES_OUT_SET_GROUP, val.i_int );
1790 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1791 NULL );
1792 break;
1794 case INPUT_CONTROL_SET_ES:
1795 /* No need to force update, es_out does it if needed */
1796 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1797 input_EsOutGetFromID( p_input->p->p_es_out,
1798 val.i_int ) );
1799 break;
1801 case INPUT_CONTROL_SET_AUDIO_DELAY:
1802 input_EsOutSetDelay( p_input->p->p_es_out,
1803 AUDIO_ES, val.i_time );
1804 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1805 break;
1807 case INPUT_CONTROL_SET_SPU_DELAY:
1808 input_EsOutSetDelay( p_input->p->p_es_out,
1809 SPU_ES, val.i_time );
1810 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1811 break;
1813 case INPUT_CONTROL_SET_TITLE:
1814 case INPUT_CONTROL_SET_TITLE_NEXT:
1815 case INPUT_CONTROL_SET_TITLE_PREV:
1816 if( p_input->p->input.b_title_demux &&
1817 p_input->p->input.i_title > 0 )
1819 /* TODO */
1820 /* FIXME handle demux title */
1821 demux_t *p_demux = p_input->p->input.p_demux;
1822 int i_title;
1824 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1825 i_title = p_demux->info.i_title - 1;
1826 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1827 i_title = p_demux->info.i_title + 1;
1828 else
1829 i_title = val.i_int;
1831 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1833 input_EsOutChangePosition( p_input->p->p_es_out );
1835 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1836 input_ControlVarTitle( p_input, i_title );
1839 else if( p_input->p->input.i_title > 0 )
1841 access_t *p_access = p_input->p->input.p_access;
1842 int i_title;
1844 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1845 i_title = p_access->info.i_title - 1;
1846 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1847 i_title = p_access->info.i_title + 1;
1848 else
1849 i_title = val.i_int;
1851 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1853 input_EsOutChangePosition( p_input->p->p_es_out );
1855 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1856 stream_AccessReset( p_input->p->input.p_stream );
1859 break;
1860 case INPUT_CONTROL_SET_SEEKPOINT:
1861 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1862 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1863 if( p_input->p->input.b_title_demux &&
1864 p_input->p->input.i_title > 0 )
1866 demux_t *p_demux = p_input->p->input.p_demux;
1867 int i_seekpoint;
1868 int64_t i_input_time;
1869 int64_t i_seekpoint_time;
1871 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1873 i_seekpoint = p_demux->info.i_seekpoint;
1874 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1875 if( i_seekpoint_time >= 0 &&
1876 !demux2_Control( p_demux,
1877 DEMUX_GET_TIME, &i_input_time ) )
1879 if ( i_input_time < i_seekpoint_time + 3000000 )
1880 i_seekpoint--;
1882 else
1883 i_seekpoint--;
1885 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1886 i_seekpoint = p_demux->info.i_seekpoint + 1;
1887 else
1888 i_seekpoint = val.i_int;
1890 if( i_seekpoint >= 0 && i_seekpoint <
1891 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1894 input_EsOutChangePosition( p_input->p->p_es_out );
1896 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1899 else if( p_input->p->input.i_title > 0 )
1901 demux_t *p_demux = p_input->p->input.p_demux;
1902 access_t *p_access = p_input->p->input.p_access;
1903 int i_seekpoint;
1904 int64_t i_input_time;
1905 int64_t i_seekpoint_time;
1907 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1909 i_seekpoint = p_access->info.i_seekpoint;
1910 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1911 if( i_seekpoint_time >= 0 &&
1912 demux2_Control( p_demux,
1913 DEMUX_GET_TIME, &i_input_time ) )
1915 if ( i_input_time < i_seekpoint_time + 3000000 )
1916 i_seekpoint--;
1918 else
1919 i_seekpoint--;
1921 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1922 i_seekpoint = p_access->info.i_seekpoint + 1;
1923 else
1924 i_seekpoint = val.i_int;
1926 if( i_seekpoint >= 0 && i_seekpoint <
1927 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1929 input_EsOutChangePosition( p_input->p->p_es_out );
1931 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1932 i_seekpoint );
1933 stream_AccessReset( p_input->p->input.p_stream );
1936 break;
1938 case INPUT_CONTROL_ADD_SLAVE:
1939 if( val.psz_string )
1941 input_source_t *slave = InputSourceNew( p_input );
1943 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1945 vlc_meta_t *p_meta;
1946 int64_t i_time;
1948 /* Add the slave */
1949 msg_Dbg( p_input, "adding %s as slave on the fly",
1950 val.psz_string );
1952 /* Set position */
1953 if( demux2_Control( p_input->p->input.p_demux,
1954 DEMUX_GET_TIME, &i_time ) )
1956 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1957 InputSourceClean( slave );
1958 free( slave );
1959 break;
1961 if( demux2_Control( slave->p_demux,
1962 DEMUX_SET_TIME, i_time ) )
1964 msg_Err( p_input, "seek failed for new slave" );
1965 InputSourceClean( slave );
1966 free( slave );
1967 break;
1970 /* Get meta (access and demux) */
1971 p_meta = vlc_meta_New();
1972 access2_Control( slave->p_access, ACCESS_GET_META,
1973 p_meta );
1974 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1975 InputUpdateMeta( p_input, p_meta );
1977 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1979 else
1981 free( slave );
1982 msg_Warn( p_input, "failed to add %s as slave",
1983 val.psz_string );
1986 free( val.psz_string );
1988 break;
1990 case INPUT_CONTROL_SET_BOOKMARK:
1991 default:
1992 msg_Err( p_input, "not yet implemented" );
1993 break;
1996 return b_force_update;
1999 /*****************************************************************************
2000 * UpdateFromDemux:
2001 *****************************************************************************/
2002 static int UpdateFromDemux( input_thread_t *p_input )
2004 demux_t *p_demux = p_input->p->input.p_demux;
2005 vlc_value_t v;
2007 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2009 v.i_int = p_demux->info.i_title;
2010 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2012 input_ControlVarTitle( p_input, p_demux->info.i_title );
2014 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2016 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2018 v.i_int = p_demux->info.i_seekpoint;
2019 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2021 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2023 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2025 /* Hmmm only works with master input */
2026 if( p_input->p->input.p_demux == p_demux )
2028 int i_title_end = p_input->p->input.i_title_end -
2029 p_input->p->input.i_title_offset;
2030 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2031 p_input->p->input.i_seekpoint_offset;
2033 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2035 if( p_demux->info.i_title > i_title_end ||
2036 ( p_demux->info.i_title == i_title_end &&
2037 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2039 else if( i_seekpoint_end >=0 )
2041 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2043 else if( i_title_end >= 0 )
2045 if( p_demux->info.i_title > i_title_end ) return 0;
2049 return 1;
2052 /*****************************************************************************
2053 * UpdateFromAccess:
2054 *****************************************************************************/
2055 static int UpdateFromAccess( input_thread_t *p_input )
2057 access_t *p_access = p_input->p->input.p_access;
2058 vlc_value_t v;
2060 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2062 v.i_int = p_access->info.i_title;
2063 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2065 input_ControlVarTitle( p_input, p_access->info.i_title );
2067 stream_AccessUpdate( p_input->p->input.p_stream );
2069 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2071 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2073 v.i_int = p_access->info.i_seekpoint;
2074 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2075 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2077 if( p_access->info.i_update & INPUT_UPDATE_META )
2079 /* TODO maybe multi - access ? */
2080 vlc_meta_t *p_meta = vlc_meta_New();
2081 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2082 InputUpdateMeta( p_input, p_meta );
2083 var_SetInteger( pl_Get( p_input ), "item-change", p_input->p->input.p_item->i_id );
2084 p_access->info.i_update &= ~INPUT_UPDATE_META;
2087 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2089 /* Hmmm only works with master input */
2090 if( p_input->p->input.p_access == p_access )
2092 int i_title_end = p_input->p->input.i_title_end -
2093 p_input->p->input.i_title_offset;
2094 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2095 p_input->p->input.i_seekpoint_offset;
2097 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2099 if( p_access->info.i_title > i_title_end ||
2100 ( p_access->info.i_title == i_title_end &&
2101 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2103 else if( i_seekpoint_end >=0 )
2105 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2107 else if( i_title_end >= 0 )
2109 if( p_access->info.i_title > i_title_end ) return 0;
2113 return 1;
2116 /*****************************************************************************
2117 * UpdateItemLength:
2118 *****************************************************************************/
2119 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2121 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2123 if( !p_input->b_preparsing )
2125 pl_Yield( p_input );
2126 var_SetInteger( pl_Get( p_input ), "item-change",
2127 p_input->p->input.p_item->i_id );
2128 pl_Release( p_input );
2132 /*****************************************************************************
2133 * InputSourceNew:
2134 *****************************************************************************/
2135 static input_source_t *InputSourceNew( input_thread_t *p_input )
2137 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2139 if( !in )
2141 msg_Err( p_input, "out of memory for new input source" );
2142 return NULL;
2145 in->p_item = NULL;
2146 in->p_access = NULL;
2147 in->p_stream = NULL;
2148 in->p_demux = NULL;
2149 in->b_title_demux = VLC_FALSE;
2150 TAB_INIT( in->i_title, in->title );
2151 in->b_can_pause = VLC_TRUE;
2152 in->b_can_pace_control = VLC_TRUE;
2153 in->b_can_rate_control = VLC_TRUE;
2154 in->b_rescale_ts = VLC_TRUE;
2155 in->b_eof = VLC_FALSE;
2156 in->f_fps = 0.0;
2157 in->i_cr_average = 0;
2159 return in;
2162 /*****************************************************************************
2163 * InputSourceInit:
2164 *****************************************************************************/
2165 static int InputSourceInit( input_thread_t *p_input,
2166 input_source_t *in, const char *psz_mrl,
2167 const char *psz_forced_demux )
2169 char psz_dup[strlen (psz_mrl) + 1];
2170 const char *psz_access;
2171 const char *psz_demux;
2172 char *psz_path;
2173 char *psz_tmp;
2174 char *psz;
2175 vlc_value_t val;
2176 double f_fps;
2178 strcpy( psz_dup, psz_mrl );
2180 if( !in ) return VLC_EGENERIC;
2181 if( !p_input ) return VLC_EGENERIC;
2183 /* Split uri */
2184 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2186 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2187 psz_mrl, psz_access, psz_demux, psz_path );
2188 if( !p_input->b_preparsing )
2190 /* Hack to allow udp://@:port syntax */
2191 if( !psz_access ||
2192 (strncmp( psz_access, "udp", 3 ) &&
2193 strncmp( psz_access, "rtp", 3 )) )
2195 /* Find optional titles and seekpoints */
2196 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2197 &in->i_seekpoint_start, &in->i_seekpoint_end );
2200 if( psz_forced_demux && *psz_forced_demux )
2202 psz_demux = psz_forced_demux;
2204 else if( *psz_demux == '\0' )
2206 /* special hack for forcing a demuxer with --demux=module
2207 * (and do nothing with a list) */
2208 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2210 if( psz_var_demux != NULL &&
2211 !strchr(psz_var_demux, ',' ) &&
2212 !strchr(psz_var_demux, ':' ) )
2214 psz_demux = psz_var_demux;
2216 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2220 /* Try access_demux if no demux given */
2221 if( *psz_demux == '\0' )
2223 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2224 NULL, p_input->p->p_es_out, VLC_FALSE );
2227 else
2229 /* Preparsing is only for file:// */
2230 if( *psz_demux )
2231 goto error;
2232 if( !*psz_access ) /* path without scheme:// */
2233 psz_access = "file";
2234 if( strcmp( psz_access, "file" ) )
2235 goto error;
2236 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2239 if( in->p_demux )
2241 int64_t i_pts_delay;
2243 /* Get infos from access_demux */
2244 demux2_Control( in->p_demux,
2245 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2246 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2248 in->b_title_demux = VLC_TRUE;
2249 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2250 &in->title, &in->i_title,
2251 &in->i_title_offset, &in->i_seekpoint_offset ) )
2253 in->i_title = 0;
2254 in->title = NULL;
2256 if( demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2257 &in->b_can_pace_control ) )
2258 in->b_can_pace_control = VLC_FALSE;
2260 if( !in->b_can_pace_control )
2262 if( demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2263 &in->b_can_rate_control, &in->b_rescale_ts ) )
2265 in->b_can_rate_control = VLC_FALSE;
2266 in->b_rescale_ts = VLC_TRUE; /* not used */
2269 else
2271 in->b_can_rate_control = VLC_TRUE;
2272 in->b_rescale_ts = VLC_TRUE;
2274 if( demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2275 &in->b_can_pause ) )
2276 in->b_can_pause = VLC_FALSE;
2277 var_SetBool( p_input, "can-pause", in->b_can_pause );
2279 int ret = demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2280 &val.b_bool );
2281 if( ret != VLC_SUCCESS )
2282 val.b_bool = VLC_FALSE;
2283 var_Set( p_input, "seekable", val );
2285 else
2287 int64_t i_pts_delay;
2289 input_ChangeState( p_input, OPENING_S );
2291 /* Now try a real access */
2292 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path );
2294 /* Access failed, URL encoded ? */
2295 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2297 decode_URI( psz_path );
2299 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2300 psz_access, psz_demux, psz_path );
2302 in->p_access = access2_New( p_input,
2303 psz_access, psz_demux, psz_path );
2305 if( in->p_access == NULL )
2307 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2308 msg_StackMsg() );
2309 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2310 _("Your input can't be opened"),
2311 _("VLC is unable to open the MRL '%s'."
2312 " Check the log for details."), psz_mrl );
2313 goto error;
2316 /* */
2317 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2318 while( psz && *psz )
2320 access_t *p_access = in->p_access;
2321 char *end = strchr( psz, ':' );
2323 if( end )
2324 *end++ = '\0';
2326 in->p_access = access2_FilterNew( in->p_access, psz );
2327 if( in->p_access == NULL )
2329 in->p_access = p_access;
2330 msg_Warn( p_input, "failed to insert access filter %s",
2331 psz );
2334 psz = end;
2336 free( psz_tmp );
2338 /* Get infos from access */
2339 if( !p_input->b_preparsing )
2341 access2_Control( in->p_access,
2342 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2343 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2345 in->b_title_demux = VLC_FALSE;
2346 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2347 &in->title, &in->i_title,
2348 &in->i_title_offset, &in->i_seekpoint_offset ) )
2351 in->i_title = 0;
2352 in->title = NULL;
2354 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2355 &in->b_can_pace_control );
2356 in->b_can_rate_control = in->b_can_pace_control;
2357 in->b_rescale_ts = VLC_TRUE;
2359 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2360 &in->b_can_pause );
2361 var_SetBool( p_input, "can-pause", in->b_can_pause );
2362 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2363 &val.b_bool );
2364 var_Set( p_input, "seekable", val );
2367 input_ChangeState( p_input, BUFFERING_S );
2369 /* Create the stream_t */
2370 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2371 if( in->p_stream == NULL )
2373 msg_Warn( p_input, "cannot create a stream_t from access" );
2374 goto error;
2377 /* Open a demuxer */
2378 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2380 psz_demux = in->p_access->psz_demux;
2384 /* Take access redirections into account */
2385 char *psz_real_path;
2386 char *psz_buf = NULL;
2387 if( in->p_access->psz_path )
2389 const char *psz_a, *psz_d;
2390 psz_buf = strdup( in->p_access->psz_path );
2391 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2393 else
2395 psz_real_path = psz_path;
2397 in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2398 psz_real_path,
2399 in->p_stream, p_input->p->p_es_out,
2400 p_input->b_preparsing );
2401 free( psz_buf );
2404 if( in->p_demux == NULL )
2406 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2407 psz_access, psz_demux, psz_path );
2408 intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2409 _("VLC can't recognize the input's format"),
2410 _("The format of '%s' cannot be detected. "
2411 "Have a look the log for details."), psz_mrl );
2412 goto error;
2415 /* Get title from demux */
2416 if( !p_input->b_preparsing && in->i_title <= 0 )
2418 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2419 &in->title, &in->i_title,
2420 &in->i_title_offset, &in->i_seekpoint_offset ))
2422 TAB_INIT( in->i_title, in->title );
2424 else
2426 in->b_title_demux = VLC_TRUE;
2431 /* get attachment
2432 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2433 if( 1 || !p_input->b_preparsing )
2435 int i_attachment;
2436 input_attachment_t **attachment;
2437 if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2438 &attachment, &i_attachment ) )
2440 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2441 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2442 i_attachment, attachment );
2443 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2446 if( !demux2_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2448 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2449 in->f_fps = f_fps;
2450 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2453 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2454 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2456 return VLC_SUCCESS;
2458 error:
2459 input_ChangeState( p_input, ERROR_S );
2461 if( in->p_demux )
2462 demux2_Delete( in->p_demux );
2464 if( in->p_stream )
2465 stream_Delete( in->p_stream );
2467 if( in->p_access )
2468 access2_Delete( in->p_access );
2470 return VLC_EGENERIC;
2473 /*****************************************************************************
2474 * InputSourceClean:
2475 *****************************************************************************/
2476 static void InputSourceClean( input_source_t *in )
2478 int i;
2480 if( in->p_demux )
2481 demux2_Delete( in->p_demux );
2483 if( in->p_stream )
2484 stream_Delete( in->p_stream );
2486 if( in->p_access )
2487 access2_Delete( in->p_access );
2489 if( in->i_title > 0 )
2491 for( i = 0; i < in->i_title; i++ )
2492 vlc_input_title_Delete( in->title[i] );
2493 TAB_CLEAN( in->i_title, in->title );
2497 static void SlaveDemux( input_thread_t *p_input )
2499 int64_t i_time;
2500 int i;
2502 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2504 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2505 return;
2508 for( i = 0; i < p_input->p->i_slave; i++ )
2510 input_source_t *in = p_input->p->slave[i];
2511 int i_ret = 1;
2513 if( in->b_eof )
2514 continue;
2516 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2518 for( ;; )
2520 int64_t i_stime;
2521 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2523 msg_Err( p_input, "slave[%d] doesn't like "
2524 "DEMUX_GET_TIME -> EOF", i );
2525 i_ret = 0;
2526 break;
2529 if( i_stime >= i_time )
2530 break;
2532 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2533 break;
2536 else
2538 i_ret = in->p_demux->pf_demux( in->p_demux );
2541 if( i_ret <= 0 )
2543 msg_Dbg( p_input, "slave %d EOF", i );
2544 in->b_eof = VLC_TRUE;
2549 static void SlaveSeek( input_thread_t *p_input )
2551 int64_t i_time;
2552 int i;
2554 if( !p_input ) return;
2556 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2558 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2559 return;
2562 for( i = 0; i < p_input->p->i_slave; i++ )
2564 input_source_t *in = p_input->p->slave[i];
2566 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2568 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2569 in->b_eof = VLC_TRUE;
2574 /*****************************************************************************
2575 * InputMetaUser:
2576 *****************************************************************************/
2577 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2579 vlc_value_t val;
2581 if( !p_meta ) return;
2583 /* Get meta information from user */
2584 #define GET_META( field, s ) \
2585 var_Get( p_input, (s), &val ); \
2586 if( *val.psz_string ) \
2587 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2588 free( val.psz_string )
2590 GET_META( Title, "meta-title" );
2591 GET_META( Artist, "meta-artist" );
2592 GET_META( Genre, "meta-genre" );
2593 GET_META( Copyright, "meta-copyright" );
2594 GET_META( Description, "meta-description" );
2595 GET_META( Date, "meta-date" );
2596 GET_META( URL, "meta-url" );
2597 #undef GET_META
2600 /*****************************************************************************
2601 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2602 * arturl and locking issue.
2603 *****************************************************************************/
2604 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2606 input_item_t *p_item = p_input->p->input.p_item;
2607 char * psz_arturl = NULL;
2608 char *psz_title = NULL;
2609 int i;
2610 int i_arturl_event = VLC_FALSE;
2612 if( !p_meta )
2613 return;
2615 psz_arturl = input_item_GetArtURL( p_item );
2617 vlc_mutex_lock( &p_item->lock );
2618 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2619 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2621 vlc_meta_Merge( p_item->p_meta, p_meta );
2623 if( psz_arturl && *psz_arturl )
2625 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2626 i_arturl_event = VLC_TRUE;
2629 vlc_meta_Delete( p_meta );
2631 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2633 /* Don't look for art cover if sout
2634 * XXX It can change when sout has meta data support */
2635 if( p_input->p->p_sout && !p_input->b_preparsing )
2637 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2638 i_arturl_event = VLC_TRUE;
2641 else
2642 input_ExtractAttachmentAndCacheArt( p_input );
2644 free( psz_arturl );
2646 /* A bit ugly */
2647 p_meta = NULL;
2648 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2650 p_meta = vlc_meta_New();
2651 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2653 vlc_mutex_unlock( &p_item->lock );
2655 input_item_SetPreparsed( p_item, VLC_TRUE );
2657 if( i_arturl_event == VLC_TRUE )
2659 vlc_event_t event;
2661 /* Notify interested third parties */
2662 event.type = vlc_InputItemMetaChanged;
2663 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2664 vlc_event_send( &p_item->event_manager, &event );
2667 if( psz_title )
2669 input_Control( p_input, INPUT_SET_NAME, psz_title );
2670 free( psz_title );
2673 if( p_meta )
2675 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2676 for( i = 0; ppsz_all_keys[i]; i++ )
2678 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2679 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2680 free( ppsz_all_keys[i] );
2682 free( ppsz_all_keys );
2683 vlc_meta_Delete( p_meta );
2686 /** \todo handle sout meta */
2690 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2691 int i_new, input_attachment_t **pp_new )
2693 int i_attachment = *pi_attachment;
2694 input_attachment_t **attachment = *ppp_attachment;
2695 int i;
2697 attachment = realloc( attachment,
2698 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2699 for( i = 0; i < i_new; i++ )
2700 attachment[i_attachment++] = pp_new[i];
2701 free( pp_new );
2703 /* */
2704 *pi_attachment = i_attachment;
2705 *ppp_attachment = attachment;
2708 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2710 vlc_bool_t b_bool;
2711 module_t *p_id3;
2714 #if 0
2715 /* XXX I am not sure it is a great idea, besides, there is more than that
2716 * if we want to do it right */
2717 vlc_mutex_lock( &p_item->lock );
2718 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2720 vlc_mutex_unlock( &p_item->lock );
2721 return;
2723 vlc_mutex_unlock( &p_item->lock );
2724 #endif
2726 demux2_Control( p_demux, DEMUX_GET_META, p_meta );
2727 if( demux2_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2728 return;
2729 if( !b_bool )
2730 return;
2732 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2733 if(! p_demux->p_private )
2734 return;
2736 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2737 if( p_id3 )
2739 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2741 if( p_demux_meta->p_meta )
2743 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2744 vlc_meta_Delete( p_demux_meta->p_meta );
2747 if( p_demux_meta->i_attachments > 0 )
2749 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2750 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2751 p_demux_meta->i_attachments, p_demux_meta->attachments );
2752 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2754 module_Unneed( p_demux, p_id3 );
2756 free( p_demux->p_private );
2760 /*****************************************************************************
2761 * MRLSplit: parse the access, demux and url part of the
2762 * Media Resource Locator.
2763 *****************************************************************************/
2764 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2765 char **ppsz_path )
2767 char *psz_access = NULL;
2768 char *psz_demux = NULL;
2769 char *psz_path;
2771 /* Either there is an access/demux specification before ://
2772 * or we have a plain local file path. */
2773 psz_path = strstr( psz_dup, "://" );
2774 if( psz_path != NULL )
2776 *psz_path = '\0';
2777 psz_path += 3; /* skips "://" */
2779 /* Separate access from demux (<access>/<demux>://<path>) */
2780 psz_access = psz_dup;
2781 psz_demux = strchr( psz_access, '/' );
2782 if( psz_demux )
2783 *psz_demux++ = '\0';
2785 else
2787 psz_path = psz_dup;
2790 *ppsz_access = psz_access ? psz_access : "";
2791 *ppsz_demux = psz_demux ? psz_demux : "";
2792 *ppsz_path = psz_path ? psz_path : "";
2795 /*****************************************************************************
2796 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2798 * Syntax:
2799 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2800 *****************************************************************************/
2801 static void MRLSections( input_thread_t *p_input, char *psz_source,
2802 int *pi_title_start, int *pi_title_end,
2803 int *pi_chapter_start, int *pi_chapter_end )
2805 char *psz, *psz_end, *psz_next, *psz_check;
2807 *pi_title_start = *pi_title_end = -1;
2808 *pi_chapter_start = *pi_chapter_end = -1;
2810 /* Start by parsing titles and chapters */
2811 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2813 /* Check we are really dealing with a title/chapter section */
2814 psz_check = psz + 1;
2815 if( !*psz_check ) return;
2816 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2817 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2818 if( *psz_check == ':' && ++psz_check )
2819 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2820 if( *psz_check != '-' && *psz_check ) return;
2821 if( *psz_check == '-' && ++psz_check )
2822 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2823 if( *psz_check != ':' && *psz_check ) return;
2824 if( *psz_check == ':' && ++psz_check )
2825 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2826 if( *psz_check ) return;
2828 /* Separate start and end */
2829 *psz++ = 0;
2830 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2832 /* Look for the start title */
2833 *pi_title_start = strtol( psz, &psz_next, 0 );
2834 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2835 *pi_title_end = *pi_title_start;
2836 psz = psz_next;
2838 /* Look for the start chapter */
2839 if( *psz ) psz++;
2840 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2841 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2842 *pi_chapter_end = *pi_chapter_start;
2844 if( psz_end )
2846 /* Look for the end title */
2847 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2848 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2849 psz_end = psz_next;
2851 /* Look for the end chapter */
2852 if( *psz_end ) psz_end++;
2853 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2854 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2857 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2858 psz_source, *pi_title_start, *pi_chapter_start,
2859 *pi_title_end, *pi_chapter_end );
2862 /*****************************************************************************
2863 * input_AddSubtitles: add a subtitles file and enable it
2864 *****************************************************************************/
2865 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2866 vlc_bool_t b_check_extension )
2868 input_source_t *sub;
2869 vlc_value_t count;
2870 vlc_value_t list;
2871 char *psz_path, *psz_extension;
2873 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2875 return VLC_FALSE;
2878 /* if we are provided a subtitle.sub file,
2879 * see if we don't have a subtitle.idx and use it instead */
2880 psz_path = strdup( psz_subtitle );
2881 if( psz_path )
2883 psz_extension = strrchr( psz_path, '.');
2884 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2886 FILE *f;
2888 strcpy( psz_extension, ".idx" );
2889 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2890 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2892 fclose( f );
2893 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2894 psz_path, psz_subtitle );
2895 strcpy( psz_subtitle, psz_path );
2898 free( psz_path );
2901 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2903 sub = InputSourceNew( p_input );
2904 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2906 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2908 /* Select the ES */
2909 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2911 if( count.i_int == 0 )
2912 count.i_int++;
2913 /* if it was first one, there is disable too */
2915 if( count.i_int < list.p_list->i_count )
2917 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2918 &list.p_list->p_values[count.i_int] );
2920 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2923 else free( sub );
2925 return VLC_TRUE;