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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 *, vlc_bool_t );
50 static int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
51 static void Error ( input_thread_t *p_input );
52 static void End ( input_thread_t *p_input );
53 static void MainLoop( input_thread_t *p_input );
55 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
56 static void ControlReduce( input_thread_t * );
57 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
60 static int UpdateFromAccess( input_thread_t * );
61 static int UpdateFromDemux( input_thread_t * );
62 static int UpdateMeta( input_thread_t *, vlc_bool_t );
64 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
66 static void ParseOption( input_thread_t *p_input, const char *psz_option );
68 static void DecodeUrl( char * );
69 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
71 static input_source_t *InputSourceNew( input_thread_t *);
72 static int InputSourceInit( input_thread_t *, input_source_t *,
73 char *, char *psz_forced_demux,
75 static void InputSourceClean( input_thread_t *, input_source_t * );
77 static void SlaveDemux( input_thread_t *p_input );
78 static void SlaveSeek( input_thread_t *p_input );
80 static vlc_meta_t *InputMetaUser( input_thread_t *p_input );
82 /*****************************************************************************
83 * This function creates a new input, and returns a pointer
84 * to its description. On error, it returns NULL.
86 * Variables for _public_ use:
89 * - rate,rate-slower, rate-faster
90 * - position, position-offset
92 * - title,title-next,title-prev
93 * - chapter,chapter-next, chapter-prev
94 * - program, audio-es, video-es, spu-es
95 * - audio-delay, spu-delay
100 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
101 * * For intf callback upon changes
103 * TODO explain when Callback is called
104 * TODO complete this list (?)
105 *****************************************************************************/
106 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
109 input_thread_t *p_input; /* thread descriptor */
113 /* Allocate descriptor */
114 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
115 if( p_input == NULL )
117 msg_Err( p_parent, "out of memory" );
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 ParseOption( 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) * 1000000;
212 psz_start = psz_end + 1;
214 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
215 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
216 p_seekpoint->i_time_offset );
217 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
218 vlc_seekpoint_Delete( p_seekpoint );
219 *psz_parser = backup;
221 free( val.psz_string );
225 /* Remove 'Now playing' info as it is probably outdated */
226 input_Control( p_input, INPUT_DEL_INFO, _("Meta-information"),
227 VLC_META_NOW_PLAYING );
233 * Initialize an input thread and run it. You will need to monitor the thread to clean
234 * up after it is done
236 * \param p_parent a vlc_object
237 * \param p_item an input item
238 * \return a pointer to the spawned input thread
240 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
241 input_item_t *p_item )
244 input_thread_t *p_input; /* thread descriptor */
246 p_input = Create( p_parent, p_item, VLC_FALSE );
247 /* Now we can attach our new input */
248 vlc_object_attach( p_input, p_parent );
250 /* Create thread and wait for its readiness. */
251 if( vlc_thread_create( p_input, "input", Run,
252 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
254 msg_Err( p_input, "cannot create input thread" );
255 vlc_object_detach( p_input );
256 vlc_object_destroy( p_input );
265 * Initialize an input thread and run it. This thread will clean after himself,
266 * you can forget about it. It can work either in blocking or non-blocking mode
268 * \param p_parent a vlc_object
269 * \param p_item an input item
270 * \param b_block should we block until read is finished ?
271 * \return the input object id if non blocking, an error code else
273 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
276 input_thread_t *p_input; /* thread descriptor */
278 p_input = Create( p_parent, p_item, VLC_FALSE );
279 /* Now we can attach our new input */
280 vlc_object_attach( p_input, p_parent );
284 RunAndClean( p_input );
289 if( vlc_thread_create( p_input, "input", RunAndClean,
290 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
292 msg_Err( p_input, "cannot create input thread" );
293 vlc_object_detach( p_input );
294 vlc_object_destroy( p_input );
298 return p_input->i_object_id;
302 * Initialize an input and initialize it to preparse the item
303 * This function is blocking. It will only accept to parse files
305 * \param p_parent a vlc_object_t
306 * \param p_item an input item
307 * \return VLC_SUCCESS or an error
309 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
311 input_thread_t *p_input; /* thread descriptor */
313 /* Allocate descriptor */
314 p_input = Create( p_parent, p_item, VLC_TRUE );
316 /* Now we can attach our new input */
317 vlc_object_attach( p_input, p_parent );
319 Init( p_input, VLC_TRUE );
321 /* Clean up master */
322 InputSourceClean( p_input, &p_input->input );
324 /* Kill access and demux */
325 if( p_input->input.p_access ) p_input->input.p_access->b_die = VLC_TRUE;
326 if( p_input->input.p_demux ) p_input->input.p_access->b_die = VLC_TRUE;
328 /* Unload all modules */
329 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
332 if( p_input->p_meta ) vlc_meta_Delete( p_input->p_meta );
334 vlc_object_detach( p_input );
335 vlc_object_destroy( p_input );
341 * Request a running input thread to stop and die
343 * \param the input thread to stop
345 void input_StopThread( input_thread_t *p_input )
350 /* Set die for input */
351 p_input->b_die = VLC_TRUE;
353 /* We cannot touch p_input fields directly (we can from another thread),
354 * so use the vlc_object_find way, it's perfectly safe */
356 /* Set die for all access */
357 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
358 for( i = 0; i < p_list->i_count; i++ )
360 p_list->p_values[i].p_object->b_die = VLC_TRUE;
362 vlc_list_release( p_list );
364 /* Set die for all stream */
365 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
366 for( i = 0; i < p_list->i_count; i++ )
368 p_list->p_values[i].p_object->b_die = VLC_TRUE;
370 vlc_list_release( p_list );
372 /* Set die for all demux */
373 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
374 for( i = 0; i < p_list->i_count; i++ )
376 p_list->p_values[i].p_object->b_die = VLC_TRUE;
378 vlc_list_release( p_list );
380 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
384 * Clean up a dead input thread
385 * This function does not return until the thread is effectively cancelled.
387 * \param the input thread to kill
389 void input_DestroyThread( input_thread_t *p_input )
391 /* Join the thread */
392 vlc_thread_join( p_input );
394 /* Delete input lock (only after thread joined) */
395 vlc_mutex_destroy( &p_input->lock_control );
397 /* TODO: maybe input_DestroyThread should also delete p_input instead
398 * of the playlist but I'm not sure if it's possible */
401 /*****************************************************************************
402 * Run: main thread loop
403 * This is the "normal" thread that spawns the input processing chain,
404 * reads the stream, cleans up and waits
405 *****************************************************************************/
406 static int Run( input_thread_t *p_input )
409 /* Signal that the thread is launched */
410 vlc_thread_ready( p_input );
412 if( Init( p_input, VLC_FALSE ) )
414 /* If we failed, wait before we are killed, and exit */
415 p_input->b_error = VLC_TRUE;
419 /* Tell we're dead */
420 p_input->b_dead = VLC_TRUE;
427 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
429 /* We have finish to demux data but not to play them */
430 while( !p_input->b_die )
432 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
435 msg_Dbg( p_input, "waiting decoder fifos to empty" );
437 msleep( INPUT_IDLE_SLEEP );
440 /* We have finished */
441 p_input->b_eof = VLC_TRUE;
444 /* Wait we are asked to die */
445 if( !p_input->b_die )
456 /*****************************************************************************
457 * RunAndClean: main thread loop
458 * This is the "just forget me" thread that spawns the input processing chain,
459 * reads the stream, cleans up and releases memory
460 *****************************************************************************/
461 static int RunAndClean( input_thread_t *p_input )
463 /* Signal that the thread is launched */
464 vlc_thread_ready( p_input );
466 if( Init( p_input, VLC_FALSE ) )
468 /* If we failed, just exit */
474 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
476 /* We have finish to demux data but not to play them */
477 while( !p_input->b_die )
479 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
482 msg_Dbg( p_input, "waiting decoder fifos to empty" );
484 msleep( INPUT_IDLE_SLEEP );
486 /* We have finished */
487 p_input->b_eof = VLC_TRUE;
494 vlc_object_detach( p_input );
495 vlc_object_destroy( p_input );
501 /*****************************************************************************
502 * Main loop: Fill buffers from access, and demux
503 *****************************************************************************/
504 static void MainLoop( input_thread_t *p_input )
506 int64_t i_intf_update = 0;
507 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
509 vlc_bool_t b_force_update = VLC_FALSE;
515 if( p_input->i_state != PAUSE_S )
517 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
518 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
525 if( p_input->input.b_title_demux &&
526 p_input->input.p_demux->info.i_update )
528 i_ret = UpdateFromDemux( p_input );
529 b_force_update = VLC_TRUE;
531 else if( !p_input->input.b_title_demux &&
532 p_input->input.p_access &&
533 p_input->input.p_access->info.i_update )
535 i_ret = UpdateFromAccess( p_input );
536 b_force_update = VLC_TRUE;
540 if( i_ret == 0 ) /* EOF */
544 var_Get( p_input, "input-repeat", &repeat );
545 if( repeat.i_int == 0 )
547 /* End of file - we do not set b_die because only the
548 * playlist is allowed to do so. */
549 msg_Dbg( p_input, "EOF reached" );
550 p_input->input.b_eof = VLC_TRUE;
554 msg_Dbg( p_input, "repeating the same input (%d)",
556 if( repeat.i_int > 0 )
559 var_Set( p_input, "input-repeat", repeat );
562 /* Seek to start title/seekpoint */
563 val.i_int = p_input->input.i_title_start -
564 p_input->input.i_title_offset;
565 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
567 input_ControlPush( p_input,
568 INPUT_CONTROL_SET_TITLE, &val );
570 val.i_int = p_input->input.i_seekpoint_start -
571 p_input->input.i_seekpoint_offset;
572 if( val.i_int > 0 /* TODO: check upper boundary */ )
573 input_ControlPush( p_input,
574 INPUT_CONTROL_SET_SEEKPOINT, &val );
576 /* Seek to start position */
577 if( p_input->i_start > 0 )
579 val.i_time = p_input->i_start;
580 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
586 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
593 p_input->b_error = VLC_TRUE;
596 if( i_ret > 0 && p_input->i_slave > 0 )
598 SlaveDemux( p_input );
608 vlc_mutex_lock( &p_input->lock_control );
609 ControlReduce( p_input );
610 while( !ControlPopNoLock( p_input, &i_type, &val ) )
612 msg_Dbg( p_input, "control type=%d", i_type );
613 if( Control( p_input, i_type, val ) )
614 b_force_update = VLC_TRUE;
616 vlc_mutex_unlock( &p_input->lock_control );
618 if( b_force_update || i_intf_update < mdate() )
622 int64_t i_time, i_length;
623 /* update input status variables */
624 if( !demux2_Control( p_input->input.p_demux,
625 DEMUX_GET_POSITION, &f_pos ) )
627 val.f_float = (float)f_pos;
628 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
630 if( !demux2_Control( p_input->input.p_demux,
631 DEMUX_GET_TIME, &i_time ) )
633 p_input->i_time = i_time;
635 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
637 if( !demux2_Control( p_input->input.p_demux,
638 DEMUX_GET_LENGTH, &i_length ) )
641 var_Get( p_input, "length", &old_val );
642 val.i_time = i_length;
643 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
645 if( old_val.i_time != val.i_time )
647 UpdateItemLength( p_input, i_length, VLC_TRUE );
651 var_SetBool( p_input, "intf-change", VLC_TRUE );
652 i_intf_update = mdate() + I64C(150000);
658 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
664 vlc_meta_t *p_meta, *p_meta_tmp;
668 /* Initialize optional stream output. (before access/demuxer)
669 * XXX: we add a special case if the uri starts by vlc.
670 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
671 * be destroyed in case of a file).
672 * (this will break playing of file starting by 'vlc:' but I don't
673 * want to add more logic, just force file by file:// or code it ;)
677 stats_Create( p_input, "read_bytes", VLC_VAR_INTEGER, STATS_COUNTER );
678 stats_Create( p_input, "read_packets", VLC_VAR_INTEGER, STATS_COUNTER );
679 stats_Create( p_input, "input_bitrate", VLC_VAR_FLOAT,
681 psz = var_GetString( p_input, "sout" );
682 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
684 p_input->p_sout = sout_NewInstance( p_input, psz );
685 if( p_input->p_sout == NULL )
687 msg_Err( p_input, "cannot start stream output instance, " \
697 p_input->p_es_out = input_EsOutNew( p_input );
698 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
699 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
701 if( InputSourceInit( p_input, &p_input->input,
702 p_input->input.p_item->psz_uri, NULL, b_quick ) )
707 /* Create global title (from master) */
710 p_input->i_title = p_input->input.i_title;
711 p_input->title = p_input->input.title;
712 p_input->i_title_offset = p_input->input.i_title_offset;
713 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
714 if( p_input->i_title > 0 )
716 /* Setup variables */
717 input_ControlVarNavigation( p_input );
718 input_ControlVarTitle( p_input, 0 );
722 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
723 p_input->b_can_pause = p_input->input.b_can_pause;
726 if( p_input->i_pts_delay < 0 )
727 p_input->i_pts_delay = 0;
729 /* If the desynchronisation requested by the user is < 0, we need to
730 * cache more data. */
731 var_Get( p_input, "audio-desync", &val );
732 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
734 /* Update cr_average depending on the caching */
735 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
736 p_input->input.i_cr_average /= 10;
737 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
740 /* Load master infos */
742 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
743 &val.i_time ) && val.i_time > 0 )
745 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
746 UpdateItemLength( p_input, val.i_time, b_quick );
747 p_input->input.p_item->i_duration = val.i_time;
750 /* Start title/chapter */
753 val.i_int = p_input->input.i_title_start -
754 p_input->input.i_title_offset;
755 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
756 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
757 val.i_int = p_input->input.i_seekpoint_start -
758 p_input->input.i_seekpoint_offset;
759 if( val.i_int > 0 /* TODO: check upper boundary */ )
760 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
764 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
766 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
769 if( p_input->i_start > 0 )
771 if( p_input->i_start >= val.i_time )
773 msg_Warn( p_input, "invalid start-time ignored" );
779 msg_Dbg( p_input, "starting at time: %ds",
780 (int)( p_input->i_start / I64C(1000000) ) );
782 s.i_time = p_input->i_start;
783 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
786 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
788 msg_Warn( p_input, "invalid stop-time ignored" );
794 /* Get fps and set it if not already set */
795 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
798 float f_requested_fps;
800 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
801 var_SetFloat( p_input, "sub-original-fps", f_fps );
803 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
804 if( f_requested_fps != f_fps )
806 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
808 var_SetFloat( p_input, "sub-fps", f_requested_fps );
812 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
815 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
819 /* Look for and add subtitle files */
820 psz_subtitle = var_GetString( p_input, "sub-file" );
827 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
829 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
832 sub = InputSourceNew( p_input );
833 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
836 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
839 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
842 if( count.i_int == 0 )
844 /* if it was first one, there is disable too */
846 if( count.i_int < list.p_list->i_count )
848 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
849 &list.p_list->p_values[count.i_int] );
851 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
857 var_Get( p_input, "sub-autodetect-file", &val );
860 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
861 char **subs = subtitles_Detect( p_input, psz_autopath,
862 p_input->input.p_item->psz_uri );
865 for( i = 0; subs && subs[i]; i++ )
867 if( strcmp( psz_subtitle, subs[i] ) )
869 sub = InputSourceNew( p_input );
870 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
873 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
878 if( subs ) free( subs );
879 if( psz_autopath ) free( psz_autopath );
881 free( psz_subtitle );
884 psz = var_GetString( p_input, "input-slave" );
888 input_source_t *slave;
891 while( *psz == ' ' || *psz == '#' )
895 if( ( psz_delim = strchr( psz, '#' ) ) )
904 msg_Dbg( p_input, "adding slave input '%s'", psz );
905 slave = InputSourceNew( p_input );
906 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
908 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
913 if( psz ) free( psz );
917 p_input->i_start = 0;
918 p_input->i_start = 0;
924 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
925 i_es_out_mode = ES_OUT_MODE_AUTO;
927 if( p_input->p_sout )
929 var_Get( p_input, "sout-all", &val );
932 i_es_out_mode = ES_OUT_MODE_ALL;
937 var_Get( p_input, "programs", &val );
938 if ( val.p_list && val.p_list->i_count )
940 i_es_out_mode = ES_OUT_MODE_PARTIAL;
941 /* Note : we should remove the "program" callback. */
944 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
948 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
950 /* Inform the demuxer about waited group (needed only for DVB) */
951 if( i_es_out_mode == ES_OUT_MODE_ALL )
953 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
955 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
957 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
962 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
963 (int) var_GetInteger( p_input, "program" ), NULL );
966 if( p_input->p_sout )
968 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
970 p_input->b_out_pace_control = VLC_FALSE;
974 p_input->b_out_pace_control = VLC_TRUE;
977 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
979 /* We don't want a high input priority here or we'll
980 * end-up sucking up all the CPU time */
981 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
984 msg_Dbg( p_input, "starting in %s mode",
985 p_input->b_out_pace_control ? "asynch" : "synch" );
989 /* Get meta data from users */
990 p_meta_tmp = InputMetaUser( p_input );
992 /* Get meta data from master input */
993 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
1001 else if( p_meta_tmp )
1003 vlc_meta_Merge( p_meta, p_meta_tmp );
1004 vlc_meta_Delete( p_meta_tmp );
1007 /* Access_file does not give any meta, and there are no slave */
1010 if( !p_input->input.p_access ||
1011 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1015 if( p_meta == NULL )
1017 p_meta = p_meta_tmp;
1019 else if( p_meta_tmp )
1021 vlc_meta_Merge( p_meta, p_meta_tmp );
1022 vlc_meta_Delete( p_meta_tmp );
1025 /* Get meta data from slave input */
1026 for( i = 0; i < p_input->i_slave; i++ )
1028 vlc_meta_t *p_meta_slave;
1030 if( !demux2_Control( p_input->slave[i]->p_demux,
1031 DEMUX_GET_META, &p_meta_slave ) )
1033 if( p_meta == NULL )
1035 p_meta = p_meta_slave;
1037 else if( p_meta_slave )
1039 vlc_meta_Merge( p_meta, p_meta_slave );
1040 vlc_meta_Delete( p_meta_slave );
1044 if( p_input->slave[i]->p_access &&
1045 !access2_Control( p_input->slave[i]->p_access,
1046 ACCESS_GET_META, &p_meta_slave ) )
1048 if( p_meta == NULL )
1050 p_meta = p_meta_slave;
1052 else if( p_meta_slave )
1054 vlc_meta_Merge( p_meta, p_meta_slave );
1055 vlc_meta_Delete( p_meta_slave );
1061 p_input->p_meta = p_meta;
1062 UpdateMeta( p_input, b_quick );
1066 msg_Dbg( p_input, "`%s' successfully opened",
1067 p_input->input.p_item->psz_uri );
1071 /* initialization is complete */
1072 p_input->i_state = PLAYING_S;
1074 val.i_int = PLAYING_S;
1075 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1080 if( p_input->p_es_out )
1081 input_EsOutDelete( p_input->p_es_out );
1083 if( p_input->p_sout )
1084 sout_DeleteInstance( p_input->p_sout );
1086 /* Mark them deleted */
1087 p_input->input.p_demux = NULL;
1088 p_input->input.p_stream = NULL;
1089 p_input->input.p_access = NULL;
1090 p_input->p_es_out = NULL;
1091 p_input->p_sout = NULL;
1093 return VLC_EGENERIC;
1096 /*****************************************************************************
1097 * Error: RunThread() error loop
1098 *****************************************************************************
1099 * This function is called when an error occurred during thread main's loop.
1100 *****************************************************************************/
1101 static void Error( input_thread_t *p_input )
1103 while( !p_input->b_die )
1106 msleep( INPUT_IDLE_SLEEP );
1110 /*****************************************************************************
1111 * End: end the input thread
1112 *****************************************************************************/
1113 static void End( input_thread_t * p_input )
1118 msg_Dbg( p_input, "closing input" );
1120 /* We are at the end */
1121 p_input->i_state = END_S;
1124 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1126 /* Clean control variables */
1127 input_ControlVarClean( p_input );
1129 /* Clean up master */
1130 InputSourceClean( p_input, &p_input->input );
1133 for( i = 0; i < p_input->i_slave; i++ )
1135 InputSourceClean( p_input, p_input->slave[i] );
1136 free( p_input->slave[i] );
1138 if( p_input->slave ) free( p_input->slave );
1140 /* Unload all modules */
1141 if( p_input->p_es_out )
1142 input_EsOutDelete( p_input->p_es_out );
1144 /* Close optional stream output instance */
1145 if( p_input->p_sout )
1147 vlc_object_t *p_pl =
1148 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1151 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1153 /* attach sout to the playlist */
1154 msg_Dbg( p_input, "keeping sout" );
1155 vlc_object_detach( p_input->p_sout );
1156 vlc_object_attach( p_input->p_sout, p_pl );
1160 msg_Dbg( p_input, "destroying sout" );
1161 sout_DeleteInstance( p_input->p_sout );
1164 vlc_object_release( p_pl );
1168 if( p_input->p_meta )
1169 vlc_meta_Delete( p_input->p_meta );
1171 /* Tell we're dead */
1172 p_input->b_dead = VLC_TRUE;
1175 /*****************************************************************************
1177 *****************************************************************************/
1178 static inline int ControlPopNoLock( input_thread_t *p_input,
1179 int *pi_type, vlc_value_t *p_val )
1181 if( p_input->i_control <= 0 )
1183 return VLC_EGENERIC;
1186 *pi_type = p_input->control[0].i_type;
1187 *p_val = p_input->control[0].val;
1189 p_input->i_control--;
1190 if( p_input->i_control > 0 )
1194 for( i = 0; i < p_input->i_control; i++ )
1196 p_input->control[i].i_type = p_input->control[i+1].i_type;
1197 p_input->control[i].val = p_input->control[i+1].val;
1204 static void ControlReduce( input_thread_t *p_input )
1207 for( i = 1; i < p_input->i_control; i++ )
1209 const int i_lt = p_input->control[i-1].i_type;
1210 const int i_ct = p_input->control[i].i_type;
1212 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1213 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1217 ( i_ct == INPUT_CONTROL_SET_STATE ||
1218 i_ct == INPUT_CONTROL_SET_RATE ||
1219 i_ct == INPUT_CONTROL_SET_POSITION ||
1220 i_ct == INPUT_CONTROL_SET_TIME ||
1221 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1222 i_ct == INPUT_CONTROL_SET_TITLE ||
1223 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1224 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1227 // msg_Dbg( p_input, "merged at %d", i );
1228 /* Remove the i-1 */
1229 for( j = i; j < p_input->i_control; j++ )
1230 p_input->control[j-1] = p_input->control[j];
1231 p_input->i_control--;
1235 /* TODO but that's not that important
1236 - merge SET_X with SET_X_CMD
1237 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1238 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1245 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1248 vlc_bool_t b_force_update = VLC_FALSE;
1250 if( !p_input ) return b_force_update;
1254 case INPUT_CONTROL_SET_DIE:
1255 msg_Dbg( p_input, "control: stopping input" );
1256 /* Mark all submodules to die */
1257 if( p_input->input.p_access )
1258 p_input->input.p_access->b_die = VLC_TRUE;
1259 if( p_input->input.p_stream )
1260 p_input->input.p_stream->b_die = VLC_TRUE;
1261 p_input->input.p_demux->b_die = VLC_TRUE;
1263 p_input->b_die = VLC_TRUE;
1266 case INPUT_CONTROL_SET_POSITION:
1267 case INPUT_CONTROL_SET_POSITION_OFFSET:
1270 if( i_type == INPUT_CONTROL_SET_POSITION )
1272 f_pos = val.f_float;
1276 /* Should not fail */
1277 demux2_Control( p_input->input.p_demux,
1278 DEMUX_GET_POSITION, &f_pos );
1279 f_pos += val.f_float;
1281 if( f_pos < 0.0 ) f_pos = 0.0;
1282 if( f_pos > 1.0 ) f_pos = 1.0;
1283 /* Reset the decoders states and clock synch (before calling the demuxer */
1284 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1285 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1286 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1289 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1290 "%2.1f%% failed", f_pos * 100 );
1294 if( p_input->i_slave > 0 )
1295 SlaveSeek( p_input );
1297 b_force_update = VLC_TRUE;
1302 case INPUT_CONTROL_SET_TIME:
1303 case INPUT_CONTROL_SET_TIME_OFFSET:
1308 if( i_type == INPUT_CONTROL_SET_TIME )
1310 i_time = val.i_time;
1314 /* Should not fail */
1315 demux2_Control( p_input->input.p_demux,
1316 DEMUX_GET_TIME, &i_time );
1317 i_time += val.i_time;
1319 if( i_time < 0 ) i_time = 0;
1321 /* Reset the decoders states and clock synch (before calling the demuxer */
1322 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1323 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1325 i_ret = demux2_Control( p_input->input.p_demux,
1326 DEMUX_SET_TIME, i_time );
1331 /* Emulate it with a SET_POS */
1332 demux2_Control( p_input->input.p_demux,
1333 DEMUX_GET_LENGTH, &i_length );
1336 double f_pos = (double)i_time / (double)i_length;
1337 i_ret = demux2_Control( p_input->input.p_demux,
1338 DEMUX_SET_POSITION, f_pos );
1343 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1344 " failed or not possible", i_time );
1348 if( p_input->i_slave > 0 )
1349 SlaveSeek( p_input );
1351 b_force_update = VLC_TRUE;
1356 case INPUT_CONTROL_SET_STATE:
1357 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1358 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1361 if( p_input->input.p_access )
1362 i_ret = access2_Control( p_input->input.p_access,
1363 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1365 i_ret = demux2_Control( p_input->input.p_demux,
1366 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1370 /* FIXME What to do ? */
1371 msg_Warn( p_input, "cannot unset pause -> EOF" );
1372 vlc_mutex_unlock( &p_input->lock_control );
1373 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1374 vlc_mutex_lock( &p_input->lock_control );
1377 b_force_update = VLC_TRUE;
1379 /* Switch to play */
1380 p_input->i_state = PLAYING_S;
1381 val.i_int = PLAYING_S;
1382 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1385 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1386 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1388 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1389 p_input->b_can_pause )
1392 if( p_input->input.p_access )
1393 i_ret = access2_Control( p_input->input.p_access,
1394 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1396 i_ret = demux2_Control( p_input->input.p_demux,
1397 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1399 b_force_update = VLC_TRUE;
1403 msg_Warn( p_input, "cannot set pause state" );
1404 val.i_int = p_input->i_state;
1408 val.i_int = PAUSE_S;
1411 /* Switch to new state */
1412 p_input->i_state = val.i_int;
1413 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1415 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1417 b_force_update = VLC_TRUE;
1419 /* Correct "state" value */
1420 val.i_int = p_input->i_state;
1421 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1423 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1425 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1429 case INPUT_CONTROL_SET_RATE:
1430 case INPUT_CONTROL_SET_RATE_SLOWER:
1431 case INPUT_CONTROL_SET_RATE_FASTER:
1435 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1436 i_rate = p_input->i_rate * 2;
1437 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1438 i_rate = p_input->i_rate / 2;
1442 if( i_rate < INPUT_RATE_MIN )
1444 msg_Dbg( p_input, "cannot set rate faster" );
1445 i_rate = INPUT_RATE_MIN;
1447 else if( i_rate > INPUT_RATE_MAX )
1449 msg_Dbg( p_input, "cannot set rate slower" );
1450 i_rate = INPUT_RATE_MAX;
1452 if( i_rate != INPUT_RATE_DEFAULT &&
1453 ( !p_input->b_can_pace_control ||
1454 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1456 msg_Dbg( p_input, "cannot change rate" );
1457 i_rate = INPUT_RATE_DEFAULT;
1459 if( i_rate != p_input->i_rate )
1461 p_input->i_rate = i_rate;
1463 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1465 /* We haven't send data to decoder when rate != default */
1466 if( i_rate == INPUT_RATE_DEFAULT )
1467 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1470 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1472 b_force_update = VLC_TRUE;
1477 case INPUT_CONTROL_SET_PROGRAM:
1478 /* No need to force update, es_out does it if needed */
1479 es_out_Control( p_input->p_es_out,
1480 ES_OUT_SET_GROUP, val.i_int );
1482 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1486 case INPUT_CONTROL_SET_ES:
1487 /* No need to force update, es_out does it if needed */
1488 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1489 input_EsOutGetFromID( p_input->p_es_out,
1493 case INPUT_CONTROL_SET_AUDIO_DELAY:
1494 input_EsOutSetDelay( p_input->p_es_out,
1495 AUDIO_ES, val.i_time );
1496 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1499 case INPUT_CONTROL_SET_SPU_DELAY:
1500 input_EsOutSetDelay( p_input->p_es_out,
1501 SPU_ES, val.i_time );
1502 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1505 case INPUT_CONTROL_SET_TITLE:
1506 case INPUT_CONTROL_SET_TITLE_NEXT:
1507 case INPUT_CONTROL_SET_TITLE_PREV:
1508 if( p_input->input.b_title_demux &&
1509 p_input->input.i_title > 0 )
1512 /* FIXME handle demux title */
1513 demux_t *p_demux = p_input->input.p_demux;
1516 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1517 i_title = p_demux->info.i_title - 1;
1518 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1519 i_title = p_demux->info.i_title + 1;
1521 i_title = val.i_int;
1523 if( i_title >= 0 && i_title < p_input->input.i_title )
1525 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1526 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1528 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1529 input_ControlVarTitle( p_input, i_title );
1532 else if( p_input->input.i_title > 0 )
1534 access_t *p_access = p_input->input.p_access;
1537 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1538 i_title = p_access->info.i_title - 1;
1539 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1540 i_title = p_access->info.i_title + 1;
1542 i_title = val.i_int;
1544 if( i_title >= 0 && i_title < p_input->input.i_title )
1546 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1547 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1549 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1550 stream_AccessReset( p_input->input.p_stream );
1554 case INPUT_CONTROL_SET_SEEKPOINT:
1555 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1556 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1557 if( p_input->input.b_title_demux &&
1558 p_input->input.i_title > 0 )
1560 demux_t *p_demux = p_input->input.p_demux;
1562 int64_t i_input_time;
1563 int64_t i_seekpoint_time;
1565 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1567 i_seekpoint = p_demux->info.i_seekpoint;
1568 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1569 if( i_seekpoint_time >= 0 &&
1570 !demux2_Control( p_demux,
1571 DEMUX_GET_TIME, &i_input_time ) )
1573 if ( i_input_time < i_seekpoint_time + 3000000 )
1579 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1580 i_seekpoint = p_demux->info.i_seekpoint + 1;
1582 i_seekpoint = val.i_int;
1584 if( i_seekpoint >= 0 && i_seekpoint <
1585 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1587 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1588 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1590 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1593 else if( p_input->input.i_title > 0 )
1595 demux_t *p_demux = p_input->input.p_demux;
1596 access_t *p_access = p_input->input.p_access;
1598 int64_t i_input_time;
1599 int64_t i_seekpoint_time;
1601 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1603 i_seekpoint = p_access->info.i_seekpoint;
1604 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1605 if( i_seekpoint_time >= 0 &&
1606 demux2_Control( p_demux,
1607 DEMUX_GET_TIME, &i_input_time ) )
1609 if ( i_input_time < i_seekpoint_time + 3000000 )
1615 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1616 i_seekpoint = p_access->info.i_seekpoint + 1;
1618 i_seekpoint = val.i_int;
1620 if( i_seekpoint >= 0 && i_seekpoint <
1621 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1623 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1624 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1626 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1627 stream_AccessReset( p_input->input.p_stream );
1632 case INPUT_CONTROL_ADD_SLAVE:
1633 if( val.psz_string )
1635 input_source_t *slave = InputSourceNew( p_input );
1637 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1640 vlc_meta_t *p_meta_new = NULL;
1645 msg_Dbg( p_input, "adding %s as slave on the fly",
1649 if( demux2_Control( p_input->input.p_demux,
1650 DEMUX_GET_TIME, &i_time ) )
1652 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1653 InputSourceClean( p_input, slave );
1657 if( demux2_Control( slave->p_demux,
1658 DEMUX_SET_TIME, i_time ) )
1660 msg_Err( p_input, "seek failed for new slave" );
1661 InputSourceClean( p_input, slave );
1667 /* Get meta (access and demux) */
1668 if( access2_Control( slave->p_access,
1669 ACCESS_GET_META, &p_meta_new ) )
1671 if( !demux2_Control( slave->p_demux,
1672 DEMUX_GET_META, &p_meta ) )
1676 vlc_meta_Merge( p_meta_new, p_meta );
1677 vlc_meta_Delete( p_meta );
1681 p_meta_new = p_meta;
1687 if( p_input->p_meta )
1689 vlc_meta_Merge( p_input->p_meta, p_meta_new );
1690 vlc_meta_Delete( p_meta_new );
1694 p_input->p_meta = p_meta_new;
1696 UpdateMeta( p_input, VLC_FALSE );
1699 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1703 msg_Warn( p_input, "failed to add %s as slave",
1707 free( val.psz_string );
1711 case INPUT_CONTROL_SET_BOOKMARK:
1713 msg_Err( p_input, "not yet implemented" );
1717 return b_force_update;
1720 /*****************************************************************************
1722 *****************************************************************************/
1723 static int UpdateFromDemux( input_thread_t *p_input )
1725 demux_t *p_demux = p_input->input.p_demux;
1728 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1730 v.i_int = p_demux->info.i_title;
1731 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1733 input_ControlVarTitle( p_input, p_demux->info.i_title );
1735 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1737 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1739 v.i_int = p_demux->info.i_seekpoint;
1740 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1742 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1744 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1746 /* Hmmm only works with master input */
1747 if( p_input->input.p_demux == p_demux )
1749 int i_title_end = p_input->input.i_title_end -
1750 p_input->input.i_title_offset;
1751 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1752 p_input->input.i_seekpoint_offset;
1754 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1756 if( p_demux->info.i_title > i_title_end ||
1757 ( p_demux->info.i_title == i_title_end &&
1758 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1760 else if( i_seekpoint_end >=0 )
1762 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1764 else if( i_title_end >= 0 )
1766 if( p_demux->info.i_title > i_title_end ) return 0;
1773 /*****************************************************************************
1775 *****************************************************************************/
1776 static int UpdateFromAccess( input_thread_t *p_input )
1778 access_t *p_access = p_input->input.p_access;
1781 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1783 v.i_int = p_access->info.i_title;
1784 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1786 input_ControlVarTitle( p_input, p_access->info.i_title );
1788 stream_AccessUpdate( p_input->input.p_stream );
1790 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1792 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1794 v.i_int = p_access->info.i_seekpoint;
1795 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1797 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1799 if( p_access->info.i_update & INPUT_UPDATE_META )
1801 /* TODO maybe multi - access ? */
1803 if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1805 if( p_input->p_meta )
1807 vlc_meta_Merge( p_input->p_meta, p_meta );
1808 vlc_meta_Delete( p_meta );
1812 p_input->p_meta = p_meta;
1815 UpdateMeta( p_input, VLC_FALSE );
1816 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1818 p_access->info.i_update &= ~INPUT_UPDATE_META;
1821 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1823 /* Hmmm only works with master input */
1824 if( p_input->input.p_access == p_access )
1826 int i_title_end = p_input->input.i_title_end -
1827 p_input->input.i_title_offset;
1828 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1829 p_input->input.i_seekpoint_offset;
1831 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1833 if( p_access->info.i_title > i_title_end ||
1834 ( p_access->info.i_title == i_title_end &&
1835 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1837 else if( i_seekpoint_end >=0 )
1839 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1841 else if( i_title_end >= 0 )
1843 if( p_access->info.i_title > i_title_end ) return 0;
1850 /*****************************************************************************
1852 *****************************************************************************/
1853 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1855 vlc_meta_t *p_meta = p_input->p_meta;
1858 if( !p_meta || p_meta->i_meta == 0 )
1861 if( !b_quick ) msg_Dbg( p_input, "meta information:" );
1862 for( i = 0; i < p_meta->i_meta; i++ )
1865 msg_Dbg( p_input, " - '%s' = '%s'",
1866 _(p_meta->name[i]), p_meta->value[i] );
1868 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] &&
1869 !p_input->input.p_item->b_fixed_name )
1870 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1872 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1873 input_Control( p_input, INPUT_ADD_INFO, _("General"),
1874 _("Author"), p_meta->value[i] );
1876 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1877 _(p_meta->name[i]), "%s", p_meta->value[i] );
1880 for( i = 0; i < p_meta->i_track; i++ )
1882 vlc_meta_t *tk = p_meta->track[i];
1885 if( tk->i_meta > 0 )
1887 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1889 msg_Dbg( p_input, " - track[%d]:", i );
1891 sprintf( psz_cat, "%s %d", _("Stream"), i );
1892 for( j = 0; j < tk->i_meta; j++ )
1894 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1897 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1898 _(tk->name[j]), "%s", tk->value[j] );
1903 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1905 p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1911 /*****************************************************************************
1913 *****************************************************************************/
1914 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1915 vlc_bool_t b_quick )
1917 playlist_t *p_playlist;
1918 char psz_buffer[MSTRTIME_MAX_SIZE];
1920 vlc_mutex_lock( &p_input->input.p_item->lock );
1921 p_input->input.p_item->i_duration = i_length;
1922 vlc_mutex_unlock( &p_input->input.p_item->lock );
1924 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1928 var_SetInteger( p_playlist, "item-change",
1929 p_input->input.p_item->i_id );
1930 vlc_object_release( p_playlist );
1933 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1934 msecstotimestr( psz_buffer, i_length / 1000 ) );
1937 /*****************************************************************************
1939 *****************************************************************************/
1940 static input_source_t *InputSourceNew( input_thread_t *p_input )
1942 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1946 msg_Err( p_input, "out of memory for new input source" );
1951 in->p_access = NULL;
1952 in->p_stream = NULL;
1954 in->b_title_demux = VLC_FALSE;
1957 in->b_can_pace_control = VLC_TRUE;
1958 in->b_eof = VLC_FALSE;
1959 in->i_cr_average = 0;
1964 /*****************************************************************************
1966 *****************************************************************************/
1967 static int InputSourceInit( input_thread_t *p_input,
1968 input_source_t *in, char *psz_mrl,
1969 char *psz_forced_demux, vlc_bool_t b_quick )
1971 char *psz_dup = strdup( psz_mrl );
1979 if( !in ) return VLC_EGENERIC;
1984 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1985 &psz_access, &psz_demux, &psz_path );
1987 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1988 psz_mrl, psz_access, psz_demux, psz_path );
1990 /* Hack to allow udp://@:port syntax */
1992 (strncmp( psz_access, "udp", 3 ) &&
1993 strncmp( psz_access, "rtp", 3 )) )
1995 /* Find optional titles and seekpoints */
1996 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1997 &in->i_seekpoint_start, &in->i_seekpoint_end );
1999 if( psz_forced_demux && *psz_forced_demux )
2001 psz_demux = psz_forced_demux;
2003 else if( !psz_demux || *psz_demux == '\0' )
2005 /* special hack for forcing a demuxer with --demux=module
2006 * (and do nothing with a list) */
2007 char *psz_var_demux = var_GetString( p_input, "demux" );
2009 if( *psz_var_demux != '\0' &&
2010 !strchr(psz_var_demux, ',' ) &&
2011 !strchr(psz_var_demux, ':' ) )
2013 psz_demux = psz_var_demux;
2015 msg_Dbg( p_input, "Enforce demux ` %s'", psz_demux );
2017 else if( psz_var_demux )
2019 free( psz_var_demux );
2023 /* Try access_demux if no demux given */
2024 if( *psz_demux == '\0' )
2026 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2027 NULL, p_input->p_es_out, VLC_FALSE );
2033 msg_Dbg( p_input, "trying to preparse %s", psz_path );
2034 psz_demux = strdup( "" );
2035 psz_access = strdup( "file" );
2040 int64_t i_pts_delay;
2042 /* Get infos from access_demux */
2043 demux2_Control( in->p_demux,
2044 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2045 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2047 in->b_title_demux = VLC_TRUE;
2048 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2049 &in->title, &in->i_title,
2050 &in->i_title_offset, &in->i_seekpoint_offset ) )
2055 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2056 &in->b_can_pace_control );
2057 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2061 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2067 int64_t i_pts_delay;
2069 /* Now try a real access */
2070 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2073 /* Access failed, URL encoded ? */
2074 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2076 DecodeUrl( psz_path );
2078 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2079 psz_access, psz_demux, psz_path );
2081 in->p_access = access2_New( p_input,
2082 psz_access, psz_demux, psz_path,
2085 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2086 * are forbidden in filenames on Win32. */
2088 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2089 if( in->p_access == NULL &&
2090 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2092 if( psz_dup ) free( psz_dup );
2093 psz_dup = strdup( psz_mrl );
2098 in->p_access = access2_New( p_input,
2099 psz_access, psz_demux, psz_path,
2104 if( in->p_access == NULL )
2106 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2107 intf_UserFatal( VLC_OBJECT( p_input),
2108 _("Errors"),"Unable to open '%s'", psz_mrl );
2113 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2114 while( psz && *psz )
2116 access_t *p_access = in->p_access;
2117 char *end = strchr( psz, ':' );
2122 in->p_access = access2_FilterNew( in->p_access, psz );
2123 if( in->p_access == NULL )
2125 in->p_access = p_access;
2126 msg_Warn( p_input, "failed to insert access filter %s",
2132 if( psz_tmp ) free( psz_tmp );
2134 /* Get infos from access */
2137 access2_Control( in->p_access,
2138 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2139 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2141 in->b_title_demux = VLC_FALSE;
2142 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2143 &in->title, &in->i_title,
2144 &in->i_title_offset, &in->i_seekpoint_offset ) )
2150 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2151 &in->b_can_pace_control );
2152 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2154 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2156 var_Set( p_input, "seekable", val );
2159 /* Create the stream_t */
2160 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2161 if( in->p_stream == NULL )
2163 msg_Warn( p_input, "cannot create a stream_t from access" );
2167 /* Open a demuxer */
2168 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2170 psz_demux = in->p_access->psz_demux;
2172 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2173 in->p_stream, p_input->p_es_out, b_quick );
2174 if( in->p_demux == NULL )
2176 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2177 psz_access, psz_demux, psz_path );
2178 intf_UserFatal( VLC_OBJECT( p_input), _("Errors"),
2179 "Unrecognized format for '%s'", psz_mrl );
2183 /* TODO get title from demux */
2184 if( !b_quick && in->i_title <= 0 )
2186 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2187 &in->title, &in->i_title,
2188 &in->i_title_offset, &in->i_seekpoint_offset ))
2195 in->b_title_demux = VLC_TRUE;
2200 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2201 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2203 if( psz_dup ) free( psz_dup );
2208 demux2_Delete( in->p_demux );
2211 stream_Delete( in->p_stream );
2214 access2_Delete( in->p_access );
2215 if( psz_dup ) free( psz_dup );
2217 return VLC_EGENERIC;
2220 /*****************************************************************************
2222 *****************************************************************************/
2223 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2226 demux2_Delete( in->p_demux );
2229 stream_Delete( in->p_stream );
2232 access2_Delete( in->p_access );
2234 if( in->i_title > 0 )
2237 for( i = 0; i < in->i_title; i++ )
2239 vlc_input_title_Delete( in->title[i] );
2245 static void SlaveDemux( input_thread_t *p_input )
2250 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2252 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2256 for( i = 0; i < p_input->i_slave; i++ )
2258 input_source_t *in = p_input->slave[i];
2264 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2269 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2271 msg_Err( p_input, "slave[%d] doesn't like "
2272 "DEMUX_GET_TIME -> EOF", i );
2277 if( i_stime >= i_time )
2280 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2286 i_ret = in->p_demux->pf_demux( in->p_demux );
2291 msg_Dbg( p_input, "slave %d EOF", i );
2292 in->b_eof = VLC_TRUE;
2297 static void SlaveSeek( input_thread_t *p_input )
2302 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2304 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2308 for( i = 0; i < p_input->i_slave; i++ )
2310 input_source_t *in = p_input->slave[i];
2312 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2314 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2315 in->b_eof = VLC_TRUE;
2319 /*****************************************************************************
2321 *****************************************************************************/
2322 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2327 if( ( p_meta = vlc_meta_New() ) == NULL )
2330 /* Get meta information from user */
2331 #define GET_META( c, s ) \
2332 var_Get( p_input, (s), &val ); \
2333 if( *val.psz_string ) \
2334 vlc_meta_Add( p_meta, c, val.psz_string ); \
2335 free( val.psz_string )
2337 GET_META( VLC_META_TITLE, "meta-title" );
2338 GET_META( VLC_META_AUTHOR, "meta-author" );
2339 GET_META( VLC_META_ARTIST, "meta-artist" );
2340 GET_META( VLC_META_GENRE, "meta-genre" );
2341 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2342 GET_META( VLC_META_DESCRIPTION, "meta-description" );
2343 GET_META( VLC_META_DATE, "meta-date" );
2344 GET_META( VLC_META_URL, "meta-url" );
2350 /*****************************************************************************
2351 * DecodeUrl: decode a given encoded url
2352 *****************************************************************************/
2353 static void DecodeUrl( char *psz )
2355 char *dup = strdup( psz );
2373 *psz++ = strtol( val, NULL, 16 );
2375 else if( *p == '+' )
2385 if( psz ) *psz++ ='\0';
2386 if( dup ) free( dup );
2389 /*****************************************************************************
2390 * ParseOption: parses the options for the input
2391 *****************************************************************************
2392 * This function parses the input (config) options and creates their associated
2394 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
2395 * bar is the value of the option.
2396 *****************************************************************************/
2397 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2399 char *psz_name = (char *)psz_option;
2400 char *psz_value = strchr( psz_option, '=' );
2401 int i_name_len, i_type;
2402 vlc_bool_t b_isno = VLC_FALSE;
2405 if( psz_value ) i_name_len = psz_value - psz_option;
2406 else i_name_len = strlen( psz_option );
2408 /* It's too much of an hassle to remove the ':' when we parse
2409 * the cmd line :) */
2410 if( i_name_len && *psz_name == ':' )
2416 if( i_name_len == 0 ) return;
2418 psz_name = strndup( psz_name, i_name_len );
2419 if( psz_value ) psz_value++;
2421 /* FIXME: :programs should be handled generically */
2422 if( !strcmp( psz_name, "programs" ) )
2423 i_type = VLC_VAR_LIST;
2425 i_type = config_GetType( p_input, psz_name );
2427 if( !i_type && !psz_value )
2429 /* check for "no-foo" or "nofoo" */
2430 if( !strncmp( psz_name, "no-", 3 ) )
2432 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2434 else if( !strncmp( psz_name, "no", 2 ) )
2436 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2438 else goto cleanup; /* Option doesn't exist */
2441 i_type = config_GetType( p_input, psz_name );
2443 if( !i_type ) goto cleanup; /* Option doesn't exist */
2445 else if( !i_type ) goto cleanup; /* Option doesn't exist */
2447 if( ( i_type != VLC_VAR_BOOL ) &&
2448 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2450 /* Create the variable in the input object.
2451 * Children of the input object will be able to retreive this value
2452 * thanks to the inheritance property of the object variables. */
2453 var_Create( p_input, psz_name, i_type );
2458 val.b_bool = !b_isno;
2461 case VLC_VAR_INTEGER:
2462 val.i_int = strtol( psz_value, NULL, 0 );
2466 val.f_float = atof( psz_value );
2469 case VLC_VAR_STRING:
2470 case VLC_VAR_MODULE:
2472 case VLC_VAR_DIRECTORY:
2473 val.psz_string = psz_value;
2478 char *psz_orig, *psz_var;
2479 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
2480 val.p_list = p_list;
2481 p_list->i_count = 0;
2483 psz_var = psz_orig = strdup(psz_value);
2484 while( psz_var && *psz_var )
2486 char *psz_item = psz_var;
2488 while( *psz_var && *psz_var != ',' ) psz_var++;
2489 if( *psz_var == ',' )
2494 val2.i_int = strtol( psz_item, NULL, 0 );
2495 INSERT_ELEM( p_list->p_values, p_list->i_count,
2496 p_list->i_count, val2 );
2497 /* p_list->i_count is incremented twice by INSERT_ELEM */
2499 INSERT_ELEM( p_list->pi_types, p_list->i_count,
2500 p_list->i_count, VLC_VAR_INTEGER );
2502 if( psz_orig ) free( psz_orig );
2511 var_Set( p_input, psz_name, val );
2513 msg_Dbg( p_input, "set input option: %s to %s", psz_name,
2514 psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2517 if( psz_name ) free( psz_name );
2521 /*****************************************************************************
2522 * MRLSplit: parse the access, demux and url part of the
2523 * Media Resource Locator.
2524 *****************************************************************************/
2525 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2526 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2528 char *psz_access = NULL;
2529 char *psz_demux = NULL;
2530 char *psz_path = NULL;
2531 char *psz, *psz_check;
2533 psz = strchr( psz_dup, ':' );
2535 /* '@' not allowed in access/demux part */
2536 psz_check = strchr( psz_dup, '@' );
2537 if( psz_check && psz_check < psz ) psz = 0;
2539 #if defined( WIN32 ) || defined( UNDER_CE )
2540 if( psz - psz_dup == 1 )
2542 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2551 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2555 psz = strchr( psz_dup, '/' );
2562 psz_access = psz_dup;
2569 if( !psz_access ) *ppsz_access = "";
2570 else *ppsz_access = psz_access;
2572 if( !psz_demux ) *ppsz_demux = "";
2573 else *ppsz_demux = psz_demux;
2575 if( !psz_path ) *ppsz_path = "";
2576 else *ppsz_path = psz_path;
2579 /*****************************************************************************
2580 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2583 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2584 *****************************************************************************/
2585 static void MRLSections( input_thread_t *p_input, char *psz_source,
2586 int *pi_title_start, int *pi_title_end,
2587 int *pi_chapter_start, int *pi_chapter_end )
2589 char *psz, *psz_end, *psz_next, *psz_check;
2591 *pi_title_start = *pi_title_end = -1;
2592 *pi_chapter_start = *pi_chapter_end = -1;
2594 /* Start by parsing titles and chapters */
2595 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2597 /* Check we are really dealing with a title/chapter section */
2598 psz_check = psz + 1;
2599 if( !*psz_check ) return;
2600 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2601 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2602 if( *psz_check == ':' && ++psz_check )
2603 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2604 if( *psz_check != '-' && *psz_check ) return;
2605 if( *psz_check == '-' && ++psz_check )
2606 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2607 if( *psz_check != ':' && *psz_check ) return;
2608 if( *psz_check == ':' && ++psz_check )
2609 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2610 if( *psz_check ) return;
2612 /* Separate start and end */
2614 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2616 /* Look for the start title */
2617 *pi_title_start = strtol( psz, &psz_next, 0 );
2618 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2619 *pi_title_end = *pi_title_start;
2622 /* Look for the start chapter */
2624 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2625 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2626 *pi_chapter_end = *pi_chapter_start;
2630 /* Look for the end title */
2631 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2632 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2635 /* Look for the end chapter */
2636 if( *psz_end ) psz_end++;
2637 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2638 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2641 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2642 psz_source, *pi_title_start, *pi_chapter_start,
2643 *pi_title_end, *pi_chapter_end );
2647 /***********************************************************************
2648 * Info management functions
2649 ***********************************************************************/
2651 * Get a info item from a given category in a given input item.
2653 * \param p_i The input item to get info from
2654 * \param psz_cat String representing the category for the info
2655 * \param psz_name String representing the name of the desired info
2656 * \return A pointer to the string with the given info if found, or an
2657 * empty string otherwise. The caller should free the returned
2660 char *vlc_input_item_GetInfo( input_item_t *p_i,
2661 const char *psz_cat,
2662 const char *psz_name )
2666 vlc_mutex_lock( &p_i->lock );
2668 for( i = 0 ; i< p_i->i_categories ; i++ )
2670 info_category_t *p_cat = p_i->pp_categories[i];
2672 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
2675 for( j = 0; j < p_cat->i_infos ; j++ )
2677 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
2679 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
2680 vlc_mutex_unlock( &p_i->lock );
2685 vlc_mutex_unlock( &p_i->lock );
2686 return strdup( "" );
2689 int vlc_input_item_AddInfo( input_item_t *p_i,
2690 const char *psz_cat,
2691 const char *psz_name,
2692 const char *psz_format, ... )
2696 info_t *p_info = NULL;
2697 info_category_t *p_cat = NULL ;
2699 vlc_mutex_lock( &p_i->lock );
2701 for( i = 0 ; i < p_i->i_categories ; i ++ )
2703 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
2705 p_cat = p_i->pp_categories[i];
2711 if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
2713 vlc_mutex_unlock( &p_i->lock );
2714 return VLC_EGENERIC;
2716 p_cat->psz_name = strdup( psz_cat );
2718 p_cat->pp_infos = 0;
2719 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
2723 for( i = 0; i< p_cat->i_infos; i++ )
2725 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
2727 p_info = p_cat->pp_infos[i];
2734 if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
2736 vlc_mutex_unlock( &p_i->lock );
2737 return VLC_EGENERIC;
2739 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
2740 p_info->psz_name = strdup( psz_name );
2744 if( p_info->psz_value ) free( p_info->psz_value );
2747 va_start( args, psz_format );
2748 vasprintf( &p_info->psz_value, psz_format, args);
2751 vlc_mutex_unlock( &p_i->lock );