1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
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 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
33 #include <vlc/decoder.h>
36 #include "input_internal.h"
38 #include "stream_output.h"
39 #include "vlc_playlist.h"
40 #include "vlc_interface.h"
41 #include "vlc_interaction.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Run ( input_thread_t *p_input );
47 static int RunAndClean ( input_thread_t *p_input );
49 static input_thread_t * Create ( vlc_object_t *, input_item_t *, char *,
51 static int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
52 static void Error ( input_thread_t *p_input );
53 static void End ( input_thread_t *p_input );
54 static void MainLoop( input_thread_t *p_input );
56 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
57 static void ControlReduce( input_thread_t * );
58 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
61 static int UpdateFromAccess( input_thread_t * );
62 static int UpdateFromDemux( input_thread_t * );
63 static int UpdateMeta( input_thread_t *, vlc_bool_t );
65 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
67 static void DecodeUrl( char * );
68 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
70 static input_source_t *InputSourceNew( input_thread_t *);
71 static int InputSourceInit( input_thread_t *, input_source_t *,
72 char *, char *psz_forced_demux,
74 static void InputSourceClean( input_thread_t *, input_source_t * );
76 static void SlaveDemux( input_thread_t *p_input );
77 static void SlaveSeek( input_thread_t *p_input );
79 static vlc_meta_t *InputMetaUser( input_thread_t *p_input );
81 /*****************************************************************************
82 * This function creates a new input, and returns a pointer
83 * to its description. On error, it returns NULL.
85 * Variables for _public_ use:
88 * - rate,rate-slower, rate-faster
89 * - position, position-offset
91 * - title,title-next,title-prev
92 * - chapter,chapter-next, chapter-prev
93 * - program, audio-es, video-es, spu-es
94 * - audio-delay, spu-delay
99 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
100 * * For intf callback upon changes
102 * TODO explain when Callback is called
103 * TODO complete this list (?)
104 *****************************************************************************/
105 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
106 char *psz_header, vlc_bool_t b_quick )
108 input_thread_t *p_input = NULL; /* thread descriptor */
112 /* Allocate descriptor */
113 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
114 if( p_input == NULL )
116 msg_Err( p_parent, "out of memory" );
119 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
121 /* Init Common fields */
122 p_input->b_eof = VLC_FALSE;
123 p_input->b_can_pace_control = VLC_TRUE;
124 p_input->i_start = 0;
127 p_input->i_title = 0;
128 p_input->title = NULL;
129 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
130 p_input->i_state = INIT_S;
131 p_input->i_rate = INPUT_RATE_DEFAULT;
132 p_input->i_bookmark = 0;
133 p_input->bookmark = NULL;
134 p_input->p_meta = NULL;
135 p_input->p_es_out = NULL;
136 p_input->p_sout = NULL;
137 p_input->b_out_pace_control = VLC_FALSE;
138 p_input->i_pts_delay = 0;
140 /* Init Input fields */
141 p_input->input.p_item = p_item;
142 p_input->input.p_access = NULL;
143 p_input->input.p_stream = NULL;
144 p_input->input.p_demux = NULL;
145 p_input->input.b_title_demux = VLC_FALSE;
146 p_input->input.i_title = 0;
147 p_input->input.title = NULL;
148 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
149 p_input->input.b_can_pace_control = VLC_TRUE;
150 p_input->input.b_eof = VLC_FALSE;
151 p_input->input.i_cr_average = 0;
153 stats_ReinitInputStats( p_item->p_stats );
156 p_input->i_slave = 0;
157 p_input->slave = NULL;
159 /* Init control buffer */
160 vlc_mutex_init( p_input, &p_input->lock_control );
161 p_input->i_control = 0;
163 /* Parse input options */
164 vlc_mutex_lock( &p_item->lock );
165 for( i = 0; i < p_item->i_options; i++ )
167 var_OptionParse( p_input, p_item->ppsz_options[i] );
169 vlc_mutex_unlock( &p_item->lock );
171 /* Create Object Variables for private use only */
172 input_ConfigVarInit( p_input );
174 /* Create Objects variables for public Get and Set */
175 input_ControlVarInit( p_input );
176 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
180 var_Get( p_input, "bookmarks", &val );
183 /* FIXME: have a common cfg parsing routine used by sout and others */
184 char *psz_parser, *psz_start, *psz_end;
185 psz_parser = val.psz_string;
186 while( (psz_start = strchr( psz_parser, '{' ) ) )
188 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
191 psz_end = strchr( psz_start, '}' );
192 if( !psz_end ) break;
193 psz_parser = psz_end + 1;
194 backup = *psz_parser;
197 while( (psz_end = strchr( psz_start, ',' ) ) )
200 if( !strncmp( psz_start, "name=", 5 ) )
202 p_seekpoint->psz_name = psz_start + 5;
204 else if( !strncmp( psz_start, "bytes=", 6 ) )
206 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
208 else if( !strncmp( psz_start, "time=", 5 ) )
210 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
213 psz_start = psz_end + 1;
215 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
216 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
217 p_seekpoint->i_time_offset );
218 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
219 vlc_seekpoint_Delete( p_seekpoint );
220 *psz_parser = backup;
222 free( val.psz_string );
226 /* Remove 'Now playing' info as it is probably outdated */
227 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT),
228 VLC_META_NOW_PLAYING ); /* ? Don't translate as it might has been copied ? */
234 * Initialize an input thread and run it. You will need to monitor the thread to clean
235 * up after it is done
237 * \param p_parent a vlc_object
238 * \param p_item an input item
239 * \return a pointer to the spawned input thread
241 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
242 input_item_t *p_item )
244 return __input_CreateThread2( p_parent, p_item, NULL );
249 input_thread_t *__input_CreateThread2( vlc_object_t *p_parent,
250 input_item_t *p_item,
253 input_thread_t *p_input = NULL; /* thread descriptor */
255 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
256 /* Now we can attach our new input */
257 vlc_object_attach( p_input, p_parent );
259 /* Create thread and wait for its readiness. */
260 if( vlc_thread_create( p_input, "input", Run,
261 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
263 msg_Err( p_input, "cannot create input thread" );
264 vlc_object_detach( p_input );
265 vlc_object_destroy( p_input );
274 * Initialize an input thread and run it. This thread will clean after himself,
275 * you can forget about it. It can work either in blocking or non-blocking mode
277 * \param p_parent a vlc_object
278 * \param p_item an input item
279 * \param b_block should we block until read is finished ?
280 * \return the input object id if non blocking, an error code else
282 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
285 input_thread_t *p_input = NULL; /* thread descriptor */
287 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
288 /* Now we can attach our new input */
289 vlc_object_attach( p_input, p_parent );
293 RunAndClean( p_input );
298 if( vlc_thread_create( p_input, "input", RunAndClean,
299 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
301 msg_Err( p_input, "cannot create input thread" );
302 vlc_object_detach( p_input );
303 vlc_object_destroy( p_input );
307 return p_input->i_object_id;
311 * Initialize an input and initialize it to preparse the item
312 * This function is blocking. It will only accept to parse files
314 * \param p_parent a vlc_object_t
315 * \param p_item an input item
316 * \return VLC_SUCCESS or an error
318 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
320 input_thread_t *p_input = NULL; /* thread descriptor */
322 /* Allocate descriptor */
323 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
324 p_input->i_flags |= OBJECT_FLAGS_NODBG;
325 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
327 /* Now we can attach our new input */
328 vlc_object_attach( p_input, p_parent );
330 Init( p_input, VLC_TRUE );
332 /* Clean up master */
333 InputSourceClean( p_input, &p_input->input );
335 /* Kill access and demux */
336 if( p_input->input.p_access ) p_input->input.p_access->b_die = VLC_TRUE;
337 if( p_input->input.p_demux ) p_input->input.p_access->b_die = VLC_TRUE;
339 /* Unload all modules */
340 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
343 if( p_input->p_meta ) vlc_meta_Delete( p_input->p_meta );
345 vlc_object_detach( p_input );
346 vlc_object_destroy( p_input );
352 * Request a running input thread to stop and die
354 * \param the input thread to stop
356 void input_StopThread( input_thread_t *p_input )
361 /* Set die for input */
362 p_input->b_die = VLC_TRUE;
364 /* We cannot touch p_input fields directly (we can from another thread),
365 * so use the vlc_object_find way, it's perfectly safe */
367 /* Set die for all access */
368 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
369 for( i = 0; i < p_list->i_count; i++ )
371 p_list->p_values[i].p_object->b_die = VLC_TRUE;
373 vlc_list_release( p_list );
375 /* Set die for all stream */
376 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
377 for( i = 0; i < p_list->i_count; i++ )
379 p_list->p_values[i].p_object->b_die = VLC_TRUE;
381 vlc_list_release( p_list );
383 /* Set die for all demux */
384 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
385 for( i = 0; i < p_list->i_count; i++ )
387 p_list->p_values[i].p_object->b_die = VLC_TRUE;
389 vlc_list_release( p_list );
391 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
395 * Clean up a dead input thread
396 * This function does not return until the thread is effectively cancelled.
398 * \param the input thread to kill
400 void input_DestroyThread( input_thread_t *p_input )
402 /* Join the thread */
403 vlc_thread_join( p_input );
405 /* Delete input lock (only after thread joined) */
406 vlc_mutex_destroy( &p_input->lock_control );
408 /* TODO: maybe input_DestroyThread should also delete p_input instead
409 * of the playlist but I'm not sure if it's possible */
412 /*****************************************************************************
413 * Run: main thread loop
414 * This is the "normal" thread that spawns the input processing chain,
415 * reads the stream, cleans up and waits
416 *****************************************************************************/
417 static int Run( input_thread_t *p_input )
419 /* Signal that the thread is launched */
420 vlc_thread_ready( p_input );
422 if( Init( p_input, VLC_FALSE ) )
424 /* If we failed, wait before we are killed, and exit */
425 p_input->b_error = VLC_TRUE;
429 /* Tell we're dead */
430 p_input->b_dead = VLC_TRUE;
437 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
439 /* We have finish to demux data but not to play them */
440 while( !p_input->b_die )
442 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
445 msg_Dbg( p_input, "waiting decoder fifos to empty" );
447 msleep( INPUT_IDLE_SLEEP );
450 /* We have finished */
451 p_input->b_eof = VLC_TRUE;
454 /* Wait we are asked to die */
455 if( !p_input->b_die )
466 /*****************************************************************************
467 * RunAndClean: main thread loop
468 * This is the "just forget me" thread that spawns the input processing chain,
469 * reads the stream, cleans up and releases memory
470 *****************************************************************************/
471 static int RunAndClean( input_thread_t *p_input )
473 /* Signal that the thread is launched */
474 vlc_thread_ready( p_input );
476 if( Init( p_input, VLC_FALSE ) )
478 /* If we failed, just exit */
484 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
486 /* We have finish to demux data but not to play them */
487 while( !p_input->b_die )
489 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
492 msg_Dbg( p_input, "waiting decoder fifos to empty" );
494 msleep( INPUT_IDLE_SLEEP );
496 /* We have finished */
497 p_input->b_eof = VLC_TRUE;
504 vlc_object_detach( p_input );
505 vlc_object_destroy( p_input );
511 /*****************************************************************************
512 * Main loop: Fill buffers from access, and demux
513 *****************************************************************************/
514 static void MainLoop( input_thread_t *p_input )
516 int64_t i_intf_update = 0;
517 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
519 vlc_bool_t b_force_update = VLC_FALSE;
525 if( p_input->i_state != PAUSE_S )
527 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
528 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
535 if( p_input->input.b_title_demux &&
536 p_input->input.p_demux->info.i_update )
538 i_ret = UpdateFromDemux( p_input );
539 b_force_update = VLC_TRUE;
541 else if( !p_input->input.b_title_demux &&
542 p_input->input.p_access &&
543 p_input->input.p_access->info.i_update )
545 i_ret = UpdateFromAccess( p_input );
546 b_force_update = VLC_TRUE;
550 if( i_ret == 0 ) /* EOF */
554 var_Get( p_input, "input-repeat", &repeat );
555 if( repeat.i_int == 0 )
557 /* End of file - we do not set b_die because only the
558 * playlist is allowed to do so. */
559 msg_Dbg( p_input, "EOF reached" );
560 p_input->input.b_eof = VLC_TRUE;
564 msg_Dbg( p_input, "repeating the same input (%d)",
566 if( repeat.i_int > 0 )
569 var_Set( p_input, "input-repeat", repeat );
572 /* Seek to start title/seekpoint */
573 val.i_int = p_input->input.i_title_start -
574 p_input->input.i_title_offset;
575 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
577 input_ControlPush( p_input,
578 INPUT_CONTROL_SET_TITLE, &val );
580 val.i_int = p_input->input.i_seekpoint_start -
581 p_input->input.i_seekpoint_offset;
582 if( val.i_int > 0 /* TODO: check upper boundary */ )
583 input_ControlPush( p_input,
584 INPUT_CONTROL_SET_SEEKPOINT, &val );
586 /* Seek to start position */
587 if( p_input->i_start > 0 )
589 val.i_time = p_input->i_start;
590 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
596 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
603 p_input->b_error = VLC_TRUE;
606 if( i_ret > 0 && p_input->i_slave > 0 )
608 SlaveDemux( p_input );
618 vlc_mutex_lock( &p_input->lock_control );
619 ControlReduce( p_input );
620 while( !ControlPopNoLock( p_input, &i_type, &val ) )
622 msg_Dbg( p_input, "control type=%d", i_type );
623 if( Control( p_input, i_type, val ) )
624 b_force_update = VLC_TRUE;
626 vlc_mutex_unlock( &p_input->lock_control );
628 if( b_force_update || i_intf_update < mdate() )
632 int64_t i_time, i_length;
633 /* update input status variables */
634 if( !demux2_Control( p_input->input.p_demux,
635 DEMUX_GET_POSITION, &f_pos ) )
637 val.f_float = (float)f_pos;
638 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
640 if( !demux2_Control( p_input->input.p_demux,
641 DEMUX_GET_TIME, &i_time ) )
643 p_input->i_time = i_time;
645 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
647 if( !demux2_Control( p_input->input.p_demux,
648 DEMUX_GET_LENGTH, &i_length ) )
651 var_Get( p_input, "length", &old_val );
652 val.i_time = i_length;
653 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
655 if( old_val.i_time != val.i_time )
657 UpdateItemLength( p_input, i_length, VLC_TRUE );
661 var_SetBool( p_input, "intf-change", VLC_TRUE );
662 i_intf_update = mdate() + I64C(150000);
668 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
674 vlc_meta_t *p_meta, *p_meta_tmp;
678 /* Initialize optional stream output. (before access/demuxer)
679 * XXX: we add a special case if the uri starts by vlc.
680 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
681 * be destroyed in case of a file).
682 * (this will break playing of file starting by 'vlc:' but I don't
683 * want to add more logic, just force file by file:// or code it ;)
687 /* Prepare statistics */
688 counter_t *p_counter;
689 stats_Create( p_input, "read_bytes", STATS_READ_BYTES,
690 VLC_VAR_INTEGER, STATS_COUNTER );
691 stats_Create( p_input, "read_packets", STATS_READ_PACKETS,
692 VLC_VAR_INTEGER, STATS_COUNTER );
693 stats_Create( p_input, "demux_read", STATS_DEMUX_READ,
694 VLC_VAR_INTEGER, STATS_COUNTER );
695 stats_Create( p_input, "input_bitrate", STATS_INPUT_BITRATE,
696 VLC_VAR_FLOAT, STATS_DERIVATIVE );
697 stats_Create( p_input, "demux_bitrate", STATS_DEMUX_BITRATE,
698 VLC_VAR_FLOAT, STATS_DERIVATIVE );
700 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
701 STATS_INPUT_BITRATE );
702 if( p_counter ) p_counter->update_interval = 1000000;
703 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
704 STATS_DEMUX_BITRATE );
705 if( p_counter ) p_counter->update_interval = 1000000;
707 stats_Create( p_input, "played_abuffers", STATS_PLAYED_ABUFFERS,
708 VLC_VAR_INTEGER, STATS_COUNTER );
709 stats_Create( p_input, "lost_abuffers", STATS_LOST_ABUFFERS,
710 VLC_VAR_INTEGER, STATS_COUNTER );
713 psz = var_GetString( p_input, "sout" );
714 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
716 p_input->p_sout = sout_NewInstance( p_input, psz );
717 if( p_input->p_sout == NULL )
719 msg_Err( p_input, "cannot start stream output instance, " \
729 p_input->p_es_out = input_EsOutNew( p_input );
730 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
731 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
733 if( InputSourceInit( p_input, &p_input->input,
734 p_input->input.p_item->psz_uri, NULL, b_quick ) )
739 /* Create global title (from master) */
742 p_input->i_title = p_input->input.i_title;
743 p_input->title = p_input->input.title;
744 p_input->i_title_offset = p_input->input.i_title_offset;
745 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
746 if( p_input->i_title > 0 )
748 /* Setup variables */
749 input_ControlVarNavigation( p_input );
750 input_ControlVarTitle( p_input, 0 );
754 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
755 p_input->b_can_pause = p_input->input.b_can_pause;
758 if( p_input->i_pts_delay < 0 )
759 p_input->i_pts_delay = 0;
761 /* If the desynchronisation requested by the user is < 0, we need to
762 * cache more data. */
763 var_Get( p_input, "audio-desync", &val );
764 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
766 /* Update cr_average depending on the caching */
767 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
768 p_input->input.i_cr_average /= 10;
769 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
772 /* Load master infos */
774 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
775 &val.i_time ) && val.i_time > 0 )
777 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
778 UpdateItemLength( p_input, val.i_time, b_quick );
779 p_input->input.p_item->i_duration = val.i_time;
782 /* Start title/chapter */
785 val.i_int = p_input->input.i_title_start -
786 p_input->input.i_title_offset;
787 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
788 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
789 val.i_int = p_input->input.i_seekpoint_start -
790 p_input->input.i_seekpoint_offset;
791 if( val.i_int > 0 /* TODO: check upper boundary */ )
792 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
796 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
798 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
801 if( p_input->i_start > 0 )
803 if( p_input->i_start >= val.i_time )
805 msg_Warn( p_input, "invalid start-time ignored" );
811 msg_Dbg( p_input, "starting at time: %ds",
812 (int)( p_input->i_start / I64C(1000000) ) );
814 s.i_time = p_input->i_start;
815 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
818 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
820 msg_Warn( p_input, "invalid stop-time ignored" );
826 /* Get fps and set it if not already set */
827 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
830 float f_requested_fps;
832 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
833 var_SetFloat( p_input, "sub-original-fps", f_fps );
835 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
836 if( f_requested_fps != f_fps )
838 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
840 var_SetFloat( p_input, "sub-fps", f_requested_fps );
844 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
847 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
851 /* Look for and add subtitle files */
852 psz_subtitle = var_GetString( p_input, "sub-file" );
859 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
861 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
864 sub = InputSourceNew( p_input );
865 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
868 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
871 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
874 if( count.i_int == 0 )
876 /* if it was first one, there is disable too */
878 if( count.i_int < list.p_list->i_count )
880 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
881 &list.p_list->p_values[count.i_int] );
883 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
889 var_Get( p_input, "sub-autodetect-file", &val );
892 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
893 char **subs = subtitles_Detect( p_input, psz_autopath,
894 p_input->input.p_item->psz_uri );
897 for( i = 0; subs && subs[i]; i++ )
899 if( strcmp( psz_subtitle, subs[i] ) )
901 sub = InputSourceNew( p_input );
902 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
905 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
910 if( subs ) free( subs );
911 if( psz_autopath ) free( psz_autopath );
913 free( psz_subtitle );
916 psz = var_GetString( p_input, "input-slave" );
920 input_source_t *slave;
923 while( *psz == ' ' || *psz == '#' )
927 if( ( psz_delim = strchr( psz, '#' ) ) )
936 msg_Dbg( p_input, "adding slave input '%s'", psz );
937 slave = InputSourceNew( p_input );
938 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
940 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
945 if( psz ) free( psz );
949 p_input->i_start = 0;
950 p_input->i_start = 0;
956 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
957 i_es_out_mode = ES_OUT_MODE_AUTO;
959 if( p_input->p_sout )
961 var_Get( p_input, "sout-all", &val );
964 i_es_out_mode = ES_OUT_MODE_ALL;
969 var_Get( p_input, "programs", &val );
970 if ( val.p_list && val.p_list->i_count )
972 i_es_out_mode = ES_OUT_MODE_PARTIAL;
973 /* Note : we should remove the "program" callback. */
976 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
980 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
982 /* Inform the demuxer about waited group (needed only for DVB) */
983 if( i_es_out_mode == ES_OUT_MODE_ALL )
985 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
987 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
989 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
994 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
995 (int) var_GetInteger( p_input, "program" ), NULL );
998 if( p_input->p_sout )
1000 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
1002 p_input->b_out_pace_control = VLC_FALSE;
1006 p_input->b_out_pace_control = VLC_TRUE;
1009 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
1011 /* We don't want a high input priority here or we'll
1012 * end-up sucking up all the CPU time */
1013 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1016 msg_Dbg( p_input, "starting in %s mode",
1017 p_input->b_out_pace_control ? "asynch" : "synch" );
1021 /* Get meta data from users */
1022 p_meta_tmp = InputMetaUser( p_input );
1024 /* Get meta data from master input */
1025 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
1029 if( p_meta == NULL )
1031 p_meta = p_meta_tmp;
1033 else if( p_meta_tmp )
1035 vlc_meta_Merge( p_meta, p_meta_tmp );
1036 vlc_meta_Delete( p_meta_tmp );
1039 /* Access_file does not give any meta, and there are no slave */
1042 if( !p_input->input.p_access ||
1043 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1047 if( p_meta == NULL )
1049 p_meta = p_meta_tmp;
1051 else if( p_meta_tmp )
1053 vlc_meta_Merge( p_meta, p_meta_tmp );
1054 vlc_meta_Delete( p_meta_tmp );
1057 /* Get meta data from slave input */
1058 for( i = 0; i < p_input->i_slave; i++ )
1060 vlc_meta_t *p_meta_slave;
1062 if( !demux2_Control( p_input->slave[i]->p_demux,
1063 DEMUX_GET_META, &p_meta_slave ) )
1065 if( p_meta == NULL )
1067 p_meta = p_meta_slave;
1069 else if( p_meta_slave )
1071 vlc_meta_Merge( p_meta, p_meta_slave );
1072 vlc_meta_Delete( p_meta_slave );
1076 if( p_input->slave[i]->p_access &&
1077 !access2_Control( p_input->slave[i]->p_access,
1078 ACCESS_GET_META, &p_meta_slave ) )
1080 if( p_meta == NULL )
1082 p_meta = p_meta_slave;
1084 else if( p_meta_slave )
1086 vlc_meta_Merge( p_meta, p_meta_slave );
1087 vlc_meta_Delete( p_meta_slave );
1093 p_input->p_meta = p_meta;
1094 UpdateMeta( p_input, b_quick );
1098 msg_Dbg( p_input, "`%s' successfully opened",
1099 p_input->input.p_item->psz_uri );
1103 /* initialization is complete */
1104 p_input->i_state = PLAYING_S;
1106 val.i_int = PLAYING_S;
1107 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1112 if( p_input->p_es_out )
1113 input_EsOutDelete( p_input->p_es_out );
1115 if( p_input->p_sout )
1116 sout_DeleteInstance( p_input->p_sout );
1118 /* Mark them deleted */
1119 p_input->input.p_demux = NULL;
1120 p_input->input.p_stream = NULL;
1121 p_input->input.p_access = NULL;
1122 p_input->p_es_out = NULL;
1123 p_input->p_sout = NULL;
1125 return VLC_EGENERIC;
1128 /*****************************************************************************
1129 * Error: RunThread() error loop
1130 *****************************************************************************
1131 * This function is called when an error occurred during thread main's loop.
1132 *****************************************************************************/
1133 static void Error( input_thread_t *p_input )
1135 while( !p_input->b_die )
1138 msleep( INPUT_IDLE_SLEEP );
1142 /*****************************************************************************
1143 * End: end the input thread
1144 *****************************************************************************/
1145 static void End( input_thread_t * p_input )
1150 msg_Dbg( p_input, "closing input" );
1152 /* We are at the end */
1153 p_input->i_state = END_S;
1156 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1158 /* Clean control variables */
1159 input_ControlVarClean( p_input );
1161 /* Clean up master */
1162 InputSourceClean( p_input, &p_input->input );
1165 for( i = 0; i < p_input->i_slave; i++ )
1167 InputSourceClean( p_input, p_input->slave[i] );
1168 free( p_input->slave[i] );
1170 if( p_input->slave ) free( p_input->slave );
1172 /* Unload all modules */
1173 if( p_input->p_es_out )
1174 input_EsOutDelete( p_input->p_es_out );
1176 /* Close optional stream output instance */
1177 if( p_input->p_sout )
1179 vlc_object_t *p_pl =
1180 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1183 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1185 /* attach sout to the playlist */
1186 msg_Dbg( p_input, "keeping sout" );
1187 vlc_object_detach( p_input->p_sout );
1188 vlc_object_attach( p_input->p_sout, p_pl );
1192 msg_Dbg( p_input, "destroying sout" );
1193 sout_DeleteInstance( p_input->p_sout );
1196 vlc_object_release( p_pl );
1200 if( p_input->p_meta )
1201 vlc_meta_Delete( p_input->p_meta );
1203 /* Tell we're dead */
1204 p_input->b_dead = VLC_TRUE;
1207 /*****************************************************************************
1209 *****************************************************************************/
1210 static inline int ControlPopNoLock( input_thread_t *p_input,
1211 int *pi_type, vlc_value_t *p_val )
1213 if( p_input->i_control <= 0 )
1215 return VLC_EGENERIC;
1218 *pi_type = p_input->control[0].i_type;
1219 *p_val = p_input->control[0].val;
1221 p_input->i_control--;
1222 if( p_input->i_control > 0 )
1226 for( i = 0; i < p_input->i_control; i++ )
1228 p_input->control[i].i_type = p_input->control[i+1].i_type;
1229 p_input->control[i].val = p_input->control[i+1].val;
1236 static void ControlReduce( input_thread_t *p_input )
1239 for( i = 1; i < p_input->i_control; i++ )
1241 const int i_lt = p_input->control[i-1].i_type;
1242 const int i_ct = p_input->control[i].i_type;
1244 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1245 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1249 ( i_ct == INPUT_CONTROL_SET_STATE ||
1250 i_ct == INPUT_CONTROL_SET_RATE ||
1251 i_ct == INPUT_CONTROL_SET_POSITION ||
1252 i_ct == INPUT_CONTROL_SET_TIME ||
1253 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1254 i_ct == INPUT_CONTROL_SET_TITLE ||
1255 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1256 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1259 // msg_Dbg( p_input, "merged at %d", i );
1260 /* Remove the i-1 */
1261 for( j = i; j < p_input->i_control; j++ )
1262 p_input->control[j-1] = p_input->control[j];
1263 p_input->i_control--;
1267 /* TODO but that's not that important
1268 - merge SET_X with SET_X_CMD
1269 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1270 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1277 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1280 vlc_bool_t b_force_update = VLC_FALSE;
1282 if( !p_input ) return b_force_update;
1286 case INPUT_CONTROL_SET_DIE:
1287 msg_Dbg( p_input, "control: stopping input" );
1288 /* Mark all submodules to die */
1289 if( p_input->input.p_access )
1290 p_input->input.p_access->b_die = VLC_TRUE;
1291 if( p_input->input.p_stream )
1292 p_input->input.p_stream->b_die = VLC_TRUE;
1293 p_input->input.p_demux->b_die = VLC_TRUE;
1295 p_input->b_die = VLC_TRUE;
1298 case INPUT_CONTROL_SET_POSITION:
1299 case INPUT_CONTROL_SET_POSITION_OFFSET:
1302 if( i_type == INPUT_CONTROL_SET_POSITION )
1304 f_pos = val.f_float;
1308 /* Should not fail */
1309 demux2_Control( p_input->input.p_demux,
1310 DEMUX_GET_POSITION, &f_pos );
1311 f_pos += val.f_float;
1313 if( f_pos < 0.0 ) f_pos = 0.0;
1314 if( f_pos > 1.0 ) f_pos = 1.0;
1315 /* Reset the decoders states and clock synch (before calling the demuxer */
1316 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1317 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1318 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1321 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1322 "%2.1f%% failed", f_pos * 100 );
1326 if( p_input->i_slave > 0 )
1327 SlaveSeek( p_input );
1329 b_force_update = VLC_TRUE;
1334 case INPUT_CONTROL_SET_TIME:
1335 case INPUT_CONTROL_SET_TIME_OFFSET:
1340 if( i_type == INPUT_CONTROL_SET_TIME )
1342 i_time = val.i_time;
1346 /* Should not fail */
1347 demux2_Control( p_input->input.p_demux,
1348 DEMUX_GET_TIME, &i_time );
1349 i_time += val.i_time;
1351 if( i_time < 0 ) i_time = 0;
1353 /* Reset the decoders states and clock synch (before calling the demuxer */
1354 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1355 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1357 i_ret = demux2_Control( p_input->input.p_demux,
1358 DEMUX_SET_TIME, i_time );
1363 /* Emulate it with a SET_POS */
1364 demux2_Control( p_input->input.p_demux,
1365 DEMUX_GET_LENGTH, &i_length );
1368 double f_pos = (double)i_time / (double)i_length;
1369 i_ret = demux2_Control( p_input->input.p_demux,
1370 DEMUX_SET_POSITION, f_pos );
1375 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1376 " failed or not possible", i_time );
1380 if( p_input->i_slave > 0 )
1381 SlaveSeek( p_input );
1383 b_force_update = VLC_TRUE;
1388 case INPUT_CONTROL_SET_STATE:
1389 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1390 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1393 if( p_input->input.p_access )
1394 i_ret = access2_Control( p_input->input.p_access,
1395 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1397 i_ret = demux2_Control( p_input->input.p_demux,
1398 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1402 /* FIXME What to do ? */
1403 msg_Warn( p_input, "cannot unset pause -> EOF" );
1404 vlc_mutex_unlock( &p_input->lock_control );
1405 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1406 vlc_mutex_lock( &p_input->lock_control );
1409 b_force_update = VLC_TRUE;
1411 /* Switch to play */
1412 p_input->i_state = PLAYING_S;
1413 val.i_int = PLAYING_S;
1414 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1417 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1418 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1420 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1421 p_input->b_can_pause )
1424 if( p_input->input.p_access )
1425 i_ret = access2_Control( p_input->input.p_access,
1426 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1428 i_ret = demux2_Control( p_input->input.p_demux,
1429 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1431 b_force_update = VLC_TRUE;
1435 msg_Warn( p_input, "cannot set pause state" );
1436 val.i_int = p_input->i_state;
1440 val.i_int = PAUSE_S;
1443 /* Switch to new state */
1444 p_input->i_state = val.i_int;
1445 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1447 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1449 b_force_update = VLC_TRUE;
1451 /* Correct "state" value */
1452 val.i_int = p_input->i_state;
1453 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1455 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1457 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1461 case INPUT_CONTROL_SET_RATE:
1462 case INPUT_CONTROL_SET_RATE_SLOWER:
1463 case INPUT_CONTROL_SET_RATE_FASTER:
1467 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1468 i_rate = p_input->i_rate * 2;
1469 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1470 i_rate = p_input->i_rate / 2;
1474 if( i_rate < INPUT_RATE_MIN )
1476 msg_Dbg( p_input, "cannot set rate faster" );
1477 i_rate = INPUT_RATE_MIN;
1479 else if( i_rate > INPUT_RATE_MAX )
1481 msg_Dbg( p_input, "cannot set rate slower" );
1482 i_rate = INPUT_RATE_MAX;
1484 if( i_rate != INPUT_RATE_DEFAULT &&
1485 ( !p_input->b_can_pace_control ||
1486 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1488 msg_Dbg( p_input, "cannot change rate" );
1489 i_rate = INPUT_RATE_DEFAULT;
1491 if( i_rate != p_input->i_rate )
1493 p_input->i_rate = i_rate;
1495 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1497 /* We haven't send data to decoder when rate != default */
1498 if( i_rate == INPUT_RATE_DEFAULT )
1499 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1502 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1504 b_force_update = VLC_TRUE;
1509 case INPUT_CONTROL_SET_PROGRAM:
1510 /* No need to force update, es_out does it if needed */
1511 es_out_Control( p_input->p_es_out,
1512 ES_OUT_SET_GROUP, val.i_int );
1514 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1518 case INPUT_CONTROL_SET_ES:
1519 /* No need to force update, es_out does it if needed */
1520 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1521 input_EsOutGetFromID( p_input->p_es_out,
1525 case INPUT_CONTROL_SET_AUDIO_DELAY:
1526 input_EsOutSetDelay( p_input->p_es_out,
1527 AUDIO_ES, val.i_time );
1528 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1531 case INPUT_CONTROL_SET_SPU_DELAY:
1532 input_EsOutSetDelay( p_input->p_es_out,
1533 SPU_ES, val.i_time );
1534 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1537 case INPUT_CONTROL_SET_TITLE:
1538 case INPUT_CONTROL_SET_TITLE_NEXT:
1539 case INPUT_CONTROL_SET_TITLE_PREV:
1540 if( p_input->input.b_title_demux &&
1541 p_input->input.i_title > 0 )
1544 /* FIXME handle demux title */
1545 demux_t *p_demux = p_input->input.p_demux;
1548 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1549 i_title = p_demux->info.i_title - 1;
1550 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1551 i_title = p_demux->info.i_title + 1;
1553 i_title = val.i_int;
1555 if( i_title >= 0 && i_title < p_input->input.i_title )
1557 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1558 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1560 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1561 input_ControlVarTitle( p_input, i_title );
1564 else if( p_input->input.i_title > 0 )
1566 access_t *p_access = p_input->input.p_access;
1569 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1570 i_title = p_access->info.i_title - 1;
1571 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1572 i_title = p_access->info.i_title + 1;
1574 i_title = val.i_int;
1576 if( i_title >= 0 && i_title < p_input->input.i_title )
1578 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1579 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1581 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1582 stream_AccessReset( p_input->input.p_stream );
1586 case INPUT_CONTROL_SET_SEEKPOINT:
1587 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1588 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1589 if( p_input->input.b_title_demux &&
1590 p_input->input.i_title > 0 )
1592 demux_t *p_demux = p_input->input.p_demux;
1594 int64_t i_input_time;
1595 int64_t i_seekpoint_time;
1597 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1599 i_seekpoint = p_demux->info.i_seekpoint;
1600 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1601 if( i_seekpoint_time >= 0 &&
1602 !demux2_Control( p_demux,
1603 DEMUX_GET_TIME, &i_input_time ) )
1605 if ( i_input_time < i_seekpoint_time + 3000000 )
1611 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1612 i_seekpoint = p_demux->info.i_seekpoint + 1;
1614 i_seekpoint = val.i_int;
1616 if( i_seekpoint >= 0 && i_seekpoint <
1617 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1619 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1620 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1622 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1625 else if( p_input->input.i_title > 0 )
1627 demux_t *p_demux = p_input->input.p_demux;
1628 access_t *p_access = p_input->input.p_access;
1630 int64_t i_input_time;
1631 int64_t i_seekpoint_time;
1633 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1635 i_seekpoint = p_access->info.i_seekpoint;
1636 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1637 if( i_seekpoint_time >= 0 &&
1638 demux2_Control( p_demux,
1639 DEMUX_GET_TIME, &i_input_time ) )
1641 if ( i_input_time < i_seekpoint_time + 3000000 )
1647 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1648 i_seekpoint = p_access->info.i_seekpoint + 1;
1650 i_seekpoint = val.i_int;
1652 if( i_seekpoint >= 0 && i_seekpoint <
1653 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1655 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1656 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1658 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1660 stream_AccessReset( p_input->input.p_stream );
1665 case INPUT_CONTROL_ADD_SLAVE:
1666 if( val.psz_string )
1668 input_source_t *slave = InputSourceNew( p_input );
1670 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1673 vlc_meta_t *p_meta_new = NULL;
1678 msg_Dbg( p_input, "adding %s as slave on the fly",
1682 if( demux2_Control( p_input->input.p_demux,
1683 DEMUX_GET_TIME, &i_time ) )
1685 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1686 InputSourceClean( p_input, slave );
1690 if( demux2_Control( slave->p_demux,
1691 DEMUX_SET_TIME, i_time ) )
1693 msg_Err( p_input, "seek failed for new slave" );
1694 InputSourceClean( p_input, slave );
1700 /* Get meta (access and demux) */
1701 if( access2_Control( slave->p_access,
1702 ACCESS_GET_META, &p_meta_new ) )
1704 if( !demux2_Control( slave->p_demux,
1705 DEMUX_GET_META, &p_meta ) )
1709 vlc_meta_Merge( p_meta_new, p_meta );
1710 vlc_meta_Delete( p_meta );
1714 p_meta_new = p_meta;
1720 if( p_input->p_meta )
1722 vlc_meta_Merge( p_input->p_meta, p_meta_new );
1723 vlc_meta_Delete( p_meta_new );
1727 p_input->p_meta = p_meta_new;
1729 UpdateMeta( p_input, VLC_FALSE );
1732 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1736 msg_Warn( p_input, "failed to add %s as slave",
1740 free( val.psz_string );
1744 case INPUT_CONTROL_SET_BOOKMARK:
1746 msg_Err( p_input, "not yet implemented" );
1750 return b_force_update;
1753 /*****************************************************************************
1755 *****************************************************************************/
1756 static int UpdateFromDemux( input_thread_t *p_input )
1758 demux_t *p_demux = p_input->input.p_demux;
1761 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1763 v.i_int = p_demux->info.i_title;
1764 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1766 input_ControlVarTitle( p_input, p_demux->info.i_title );
1768 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1770 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1772 v.i_int = p_demux->info.i_seekpoint;
1773 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1775 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1777 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1779 /* Hmmm only works with master input */
1780 if( p_input->input.p_demux == p_demux )
1782 int i_title_end = p_input->input.i_title_end -
1783 p_input->input.i_title_offset;
1784 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1785 p_input->input.i_seekpoint_offset;
1787 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1789 if( p_demux->info.i_title > i_title_end ||
1790 ( p_demux->info.i_title == i_title_end &&
1791 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1793 else if( i_seekpoint_end >=0 )
1795 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1797 else if( i_title_end >= 0 )
1799 if( p_demux->info.i_title > i_title_end ) return 0;
1806 /*****************************************************************************
1808 *****************************************************************************/
1809 static int UpdateFromAccess( input_thread_t *p_input )
1811 access_t *p_access = p_input->input.p_access;
1814 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1816 v.i_int = p_access->info.i_title;
1817 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1819 input_ControlVarTitle( p_input, p_access->info.i_title );
1821 stream_AccessUpdate( p_input->input.p_stream );
1823 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1825 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1827 v.i_int = p_access->info.i_seekpoint;
1828 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1830 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1832 if( p_access->info.i_update & INPUT_UPDATE_META )
1834 /* TODO maybe multi - access ? */
1836 if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1838 if( p_input->p_meta )
1840 vlc_meta_Merge( p_input->p_meta, p_meta );
1841 vlc_meta_Delete( p_meta );
1845 p_input->p_meta = p_meta;
1848 UpdateMeta( p_input, VLC_FALSE );
1849 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1851 p_access->info.i_update &= ~INPUT_UPDATE_META;
1854 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1856 /* Hmmm only works with master input */
1857 if( p_input->input.p_access == p_access )
1859 int i_title_end = p_input->input.i_title_end -
1860 p_input->input.i_title_offset;
1861 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1862 p_input->input.i_seekpoint_offset;
1864 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1866 if( p_access->info.i_title > i_title_end ||
1867 ( p_access->info.i_title == i_title_end &&
1868 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1870 else if( i_seekpoint_end >=0 )
1872 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1874 else if( i_title_end >= 0 )
1876 if( p_access->info.i_title > i_title_end ) return 0;
1883 /*****************************************************************************
1885 *****************************************************************************/
1886 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1888 vlc_meta_t *p_meta = p_input->p_meta;
1891 if( !p_meta || p_meta->i_meta == 0 )
1894 if( !b_quick ) msg_Dbg( p_input, "meta information:" );
1895 for( i = 0; i < p_meta->i_meta; i++ )
1898 msg_Dbg( p_input, " - '%s' = '%s'",
1899 _(p_meta->name[i]), p_meta->value[i] );
1901 if( !strcmp(p_meta->name[i], _(VLC_META_TITLE)) && p_meta->value[i] &&
1902 !p_input->input.p_item->b_fixed_name )
1903 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1905 if( !strcmp( p_meta->name[i], _(VLC_META_AUTHOR) ) )
1906 input_Control( p_input, INPUT_ADD_INFO, _("General"),
1907 _(VLC_META_AUTHOR), p_meta->value[i] );
1909 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT),
1910 _(p_meta->name[i]), "%s", p_meta->value[i] );
1913 for( i = 0; i < p_meta->i_track; i++ )
1915 vlc_meta_t *tk = p_meta->track[i];
1918 if( tk->i_meta > 0 )
1920 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1922 msg_Dbg( p_input, " - track[%d]:", i );
1924 sprintf( psz_cat, "%s %d", _("Stream"), i );
1925 for( j = 0; j < tk->i_meta; j++ )
1927 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1930 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1931 _(tk->name[j]), "%s", tk->value[j] );
1936 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1938 p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1944 /*****************************************************************************
1946 *****************************************************************************/
1947 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1948 vlc_bool_t b_quick )
1950 playlist_t *p_playlist;
1951 char psz_buffer[MSTRTIME_MAX_SIZE];
1953 vlc_mutex_lock( &p_input->input.p_item->lock );
1954 p_input->input.p_item->i_duration = i_length;
1955 vlc_mutex_unlock( &p_input->input.p_item->lock );
1957 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1961 var_SetInteger( p_playlist, "item-change",
1962 p_input->input.p_item->i_id );
1963 vlc_object_release( p_playlist );
1966 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1967 msecstotimestr( psz_buffer, i_length / 1000 ) );
1970 /*****************************************************************************
1972 *****************************************************************************/
1973 static input_source_t *InputSourceNew( input_thread_t *p_input )
1975 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1979 msg_Err( p_input, "out of memory for new input source" );
1984 in->p_access = NULL;
1985 in->p_stream = NULL;
1987 in->b_title_demux = VLC_FALSE;
1990 in->b_can_pace_control = VLC_TRUE;
1991 in->b_eof = VLC_FALSE;
1992 in->i_cr_average = 0;
1997 /*****************************************************************************
1999 *****************************************************************************/
2000 static int InputSourceInit( input_thread_t *p_input,
2001 input_source_t *in, char *psz_mrl,
2002 char *psz_forced_demux, vlc_bool_t b_quick )
2004 char *psz_dup = strdup( psz_mrl );
2012 if( !in ) return VLC_EGENERIC;
2017 MRLSplit( VLC_OBJECT(p_input), psz_dup,
2018 &psz_access, &psz_demux, &psz_path );
2020 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2021 psz_mrl, psz_access, psz_demux, psz_path );
2023 /* Hack to allow udp://@:port syntax */
2025 (strncmp( psz_access, "udp", 3 ) &&
2026 strncmp( psz_access, "rtp", 3 )) )
2028 /* Find optional titles and seekpoints */
2029 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2030 &in->i_seekpoint_start, &in->i_seekpoint_end );
2032 if( psz_forced_demux && *psz_forced_demux )
2034 psz_demux = psz_forced_demux;
2036 else if( !psz_demux || *psz_demux == '\0' )
2038 /* special hack for forcing a demuxer with --demux=module
2039 * (and do nothing with a list) */
2040 char *psz_var_demux = var_GetString( p_input, "demux" );
2042 if( *psz_var_demux != '\0' &&
2043 !strchr(psz_var_demux, ',' ) &&
2044 !strchr(psz_var_demux, ':' ) )
2046 psz_demux = psz_var_demux;
2048 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2050 else if( psz_var_demux )
2052 free( psz_var_demux );
2056 /* Try access_demux if no demux given */
2057 if( *psz_demux == '\0' )
2059 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2060 NULL, p_input->p_es_out, VLC_FALSE );
2066 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2067 psz_demux = strdup( "" );
2068 psz_access = strdup( "file" );
2073 int64_t i_pts_delay;
2075 /* Get infos from access_demux */
2076 demux2_Control( in->p_demux,
2077 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2078 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2080 in->b_title_demux = VLC_TRUE;
2081 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2082 &in->title, &in->i_title,
2083 &in->i_title_offset, &in->i_seekpoint_offset ) )
2088 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2089 &in->b_can_pace_control );
2090 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2094 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2100 int64_t i_pts_delay;
2102 /* Now try a real access */
2103 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2106 /* Access failed, URL encoded ? */
2107 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2109 DecodeUrl( psz_path );
2111 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2112 psz_access, psz_demux, psz_path );
2114 in->p_access = access2_New( p_input,
2115 psz_access, psz_demux, psz_path,
2118 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2119 * are forbidden in filenames on Win32. */
2121 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2122 if( in->p_access == NULL &&
2123 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2125 if( psz_dup ) free( psz_dup );
2126 psz_dup = strdup( psz_mrl );
2128 if( psz_forced_demux && *psz_forced_demux )
2130 psz_demux = psz_forced_demux;
2132 else psz_demux = "";
2135 in->p_access = access2_New( p_input,
2136 psz_access, psz_demux, psz_path,
2141 if( in->p_access == NULL )
2143 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2144 intf_UserFatal( VLC_OBJECT( p_input),
2145 _("Errors"),"Unable to open '%s'", psz_mrl );
2150 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2151 while( psz && *psz )
2153 access_t *p_access = in->p_access;
2154 char *end = strchr( psz, ':' );
2159 in->p_access = access2_FilterNew( in->p_access, psz );
2160 if( in->p_access == NULL )
2162 in->p_access = p_access;
2163 msg_Warn( p_input, "failed to insert access filter %s",
2169 if( psz_tmp ) free( psz_tmp );
2171 /* Get infos from access */
2174 access2_Control( in->p_access,
2175 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2176 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2178 in->b_title_demux = VLC_FALSE;
2179 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2180 &in->title, &in->i_title,
2181 &in->i_title_offset, &in->i_seekpoint_offset ) )
2187 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2188 &in->b_can_pace_control );
2189 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2191 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2193 var_Set( p_input, "seekable", val );
2196 /* Create the stream_t */
2197 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2198 if( in->p_stream == NULL )
2200 msg_Warn( p_input, "cannot create a stream_t from access" );
2204 /* Open a demuxer */
2205 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2207 psz_demux = in->p_access->psz_demux;
2209 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2210 in->p_stream, p_input->p_es_out, b_quick );
2211 if( in->p_demux == NULL )
2213 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2214 psz_access, psz_demux, psz_path );
2215 intf_UserFatal( VLC_OBJECT( p_input), _("Errors"),
2216 "Unrecognized format for '%s'", psz_mrl );
2220 /* TODO get title from demux */
2221 if( !b_quick && in->i_title <= 0 )
2223 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2224 &in->title, &in->i_title,
2225 &in->i_title_offset, &in->i_seekpoint_offset ))
2232 in->b_title_demux = VLC_TRUE;
2237 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2238 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2240 if( psz_dup ) free( psz_dup );
2245 demux2_Delete( in->p_demux );
2248 stream_Delete( in->p_stream );
2251 access2_Delete( in->p_access );
2252 if( psz_dup ) free( psz_dup );
2254 return VLC_EGENERIC;
2257 /*****************************************************************************
2259 *****************************************************************************/
2260 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2263 demux2_Delete( in->p_demux );
2266 stream_Delete( in->p_stream );
2269 access2_Delete( in->p_access );
2271 if( in->i_title > 0 )
2274 for( i = 0; i < in->i_title; i++ )
2276 vlc_input_title_Delete( in->title[i] );
2282 static void SlaveDemux( input_thread_t *p_input )
2287 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2289 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2293 for( i = 0; i < p_input->i_slave; i++ )
2295 input_source_t *in = p_input->slave[i];
2301 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2306 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2308 msg_Err( p_input, "slave[%d] doesn't like "
2309 "DEMUX_GET_TIME -> EOF", i );
2314 if( i_stime >= i_time )
2317 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2323 i_ret = in->p_demux->pf_demux( in->p_demux );
2328 msg_Dbg( p_input, "slave %d EOF", i );
2329 in->b_eof = VLC_TRUE;
2334 static void SlaveSeek( input_thread_t *p_input )
2339 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2341 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2345 for( i = 0; i < p_input->i_slave; i++ )
2347 input_source_t *in = p_input->slave[i];
2349 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2351 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2352 in->b_eof = VLC_TRUE;
2356 /*****************************************************************************
2358 *****************************************************************************/
2359 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2364 if( ( p_meta = vlc_meta_New() ) == NULL )
2367 /* Get meta information from user */
2368 #define GET_META( c, s ) \
2369 var_Get( p_input, (s), &val ); \
2370 if( *val.psz_string ) \
2371 vlc_meta_Add( p_meta, _(c), val.psz_string ); \
2372 free( val.psz_string )
2374 GET_META( VLC_META_TITLE, "meta-title" );
2375 GET_META( VLC_META_AUTHOR, "meta-author" );
2376 GET_META( VLC_META_ARTIST, "meta-artist" );
2377 GET_META( VLC_META_GENRE, "meta-genre" );
2378 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2379 GET_META( VLC_META_DESCRIPTION, "meta-description" );
2380 GET_META( VLC_META_DATE, "meta-date" );
2381 GET_META( VLC_META_URL, "meta-url" );
2387 /*****************************************************************************
2388 * DecodeUrl: decode a given encoded url
2389 *****************************************************************************/
2390 static void DecodeUrl( char *psz )
2392 char *dup = strdup( psz );
2410 *psz++ = strtol( val, NULL, 16 );
2412 else if( *p == '+' )
2422 if( psz ) *psz++ ='\0';
2423 if( dup ) free( dup );
2426 /*****************************************************************************
2427 * MRLSplit: parse the access, demux and url part of the
2428 * Media Resource Locator.
2429 *****************************************************************************/
2430 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2431 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2433 char *psz_access = NULL;
2434 char *psz_demux = NULL;
2435 char *psz_path = NULL;
2436 char *psz, *psz_check;
2438 psz = strchr( psz_dup, ':' );
2440 /* '@' not allowed in access/demux part */
2441 psz_check = strchr( psz_dup, '@' );
2442 if( psz_check && psz_check < psz ) psz = 0;
2444 #if defined( WIN32 ) || defined( UNDER_CE )
2445 if( psz - psz_dup == 1 )
2447 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2456 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2460 psz = strchr( psz_dup, '/' );
2467 psz_access = psz_dup;
2474 if( !psz_access ) *ppsz_access = "";
2475 else *ppsz_access = psz_access;
2477 if( !psz_demux ) *ppsz_demux = "";
2478 else *ppsz_demux = psz_demux;
2480 if( !psz_path ) *ppsz_path = "";
2481 else *ppsz_path = psz_path;
2484 /*****************************************************************************
2485 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2488 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2489 *****************************************************************************/
2490 static void MRLSections( input_thread_t *p_input, char *psz_source,
2491 int *pi_title_start, int *pi_title_end,
2492 int *pi_chapter_start, int *pi_chapter_end )
2494 char *psz, *psz_end, *psz_next, *psz_check;
2496 *pi_title_start = *pi_title_end = -1;
2497 *pi_chapter_start = *pi_chapter_end = -1;
2499 /* Start by parsing titles and chapters */
2500 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2502 /* Check we are really dealing with a title/chapter section */
2503 psz_check = psz + 1;
2504 if( !*psz_check ) return;
2505 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2506 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2507 if( *psz_check == ':' && ++psz_check )
2508 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2509 if( *psz_check != '-' && *psz_check ) return;
2510 if( *psz_check == '-' && ++psz_check )
2511 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2512 if( *psz_check != ':' && *psz_check ) return;
2513 if( *psz_check == ':' && ++psz_check )
2514 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2515 if( *psz_check ) return;
2517 /* Separate start and end */
2519 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2521 /* Look for the start title */
2522 *pi_title_start = strtol( psz, &psz_next, 0 );
2523 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2524 *pi_title_end = *pi_title_start;
2527 /* Look for the start chapter */
2529 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2530 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2531 *pi_chapter_end = *pi_chapter_start;
2535 /* Look for the end title */
2536 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2537 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2540 /* Look for the end chapter */
2541 if( *psz_end ) psz_end++;
2542 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2543 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2546 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2547 psz_source, *pi_title_start, *pi_chapter_start,
2548 *pi_title_end, *pi_chapter_end );
2552 /***********************************************************************
2553 * Info management functions
2554 ***********************************************************************/
2556 * Get a info item from a given category in a given input item.
2558 * \param p_i The input item to get info from
2559 * \param psz_cat String representing the category for the info
2560 * \param psz_name String representing the name of the desired info
2561 * \return A pointer to the string with the given info if found, or an
2562 * empty string otherwise. The caller should free the returned
2565 char *vlc_input_item_GetInfo( input_item_t *p_i,
2566 const char *psz_cat,
2567 const char *psz_name )
2571 vlc_mutex_lock( &p_i->lock );
2573 for( i = 0 ; i< p_i->i_categories ; i++ )
2575 info_category_t *p_cat = p_i->pp_categories[i];
2577 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
2580 for( j = 0; j < p_cat->i_infos ; j++ )
2582 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
2584 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
2585 vlc_mutex_unlock( &p_i->lock );
2590 vlc_mutex_unlock( &p_i->lock );
2591 return strdup( "" );
2594 int vlc_input_item_AddInfo( input_item_t *p_i,
2595 const char *psz_cat,
2596 const char *psz_name,
2597 const char *psz_format, ... )
2601 info_t *p_info = NULL;
2602 info_category_t *p_cat = NULL ;
2604 vlc_mutex_lock( &p_i->lock );
2606 for( i = 0 ; i < p_i->i_categories ; i ++ )
2608 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
2610 p_cat = p_i->pp_categories[i];
2616 if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
2618 vlc_mutex_unlock( &p_i->lock );
2619 return VLC_EGENERIC;
2621 p_cat->psz_name = strdup( psz_cat );
2623 p_cat->pp_infos = 0;
2624 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
2628 for( i = 0; i< p_cat->i_infos; i++ )
2630 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
2632 p_info = p_cat->pp_infos[i];
2639 if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
2641 vlc_mutex_unlock( &p_i->lock );
2642 return VLC_EGENERIC;
2644 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
2645 p_info->psz_name = strdup( psz_name );
2649 if( p_info->psz_value ) free( p_info->psz_value );
2652 va_start( args, psz_format );
2653 vasprintf( &p_info->psz_value, psz_format, args);
2656 vlc_mutex_unlock( &p_i->lock );