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 *, 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 counter_t *p_counter;
678 stats_Create( p_input, "read_bytes", VLC_VAR_INTEGER, STATS_COUNTER );
679 stats_Create( p_input, "read_packets", VLC_VAR_INTEGER, STATS_COUNTER );
680 stats_Create( p_input, "input_bitrate", VLC_VAR_FLOAT,
682 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
684 if( p_counter ) p_counter->update_interval = 1000000;
686 psz = var_GetString( p_input, "sout" );
687 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
689 p_input->p_sout = sout_NewInstance( p_input, psz );
690 if( p_input->p_sout == NULL )
692 msg_Err( p_input, "cannot start stream output instance, " \
702 p_input->p_es_out = input_EsOutNew( p_input );
703 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
704 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
706 if( InputSourceInit( p_input, &p_input->input,
707 p_input->input.p_item->psz_uri, NULL, b_quick ) )
712 /* Create global title (from master) */
715 p_input->i_title = p_input->input.i_title;
716 p_input->title = p_input->input.title;
717 p_input->i_title_offset = p_input->input.i_title_offset;
718 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
719 if( p_input->i_title > 0 )
721 /* Setup variables */
722 input_ControlVarNavigation( p_input );
723 input_ControlVarTitle( p_input, 0 );
727 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
728 p_input->b_can_pause = p_input->input.b_can_pause;
731 if( p_input->i_pts_delay < 0 )
732 p_input->i_pts_delay = 0;
734 /* If the desynchronisation requested by the user is < 0, we need to
735 * cache more data. */
736 var_Get( p_input, "audio-desync", &val );
737 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
739 /* Update cr_average depending on the caching */
740 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
741 p_input->input.i_cr_average /= 10;
742 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
745 /* Load master infos */
747 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
748 &val.i_time ) && val.i_time > 0 )
750 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
751 UpdateItemLength( p_input, val.i_time, b_quick );
752 p_input->input.p_item->i_duration = val.i_time;
755 /* Start title/chapter */
758 val.i_int = p_input->input.i_title_start -
759 p_input->input.i_title_offset;
760 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
761 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
762 val.i_int = p_input->input.i_seekpoint_start -
763 p_input->input.i_seekpoint_offset;
764 if( val.i_int > 0 /* TODO: check upper boundary */ )
765 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
769 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
771 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
774 if( p_input->i_start > 0 )
776 if( p_input->i_start >= val.i_time )
778 msg_Warn( p_input, "invalid start-time ignored" );
784 msg_Dbg( p_input, "starting at time: %ds",
785 (int)( p_input->i_start / I64C(1000000) ) );
787 s.i_time = p_input->i_start;
788 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
791 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
793 msg_Warn( p_input, "invalid stop-time ignored" );
799 /* Get fps and set it if not already set */
800 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
803 float f_requested_fps;
805 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
806 var_SetFloat( p_input, "sub-original-fps", f_fps );
808 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
809 if( f_requested_fps != f_fps )
811 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
813 var_SetFloat( p_input, "sub-fps", f_requested_fps );
817 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
820 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
824 /* Look for and add subtitle files */
825 psz_subtitle = var_GetString( p_input, "sub-file" );
832 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
834 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
837 sub = InputSourceNew( p_input );
838 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
841 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
844 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
847 if( count.i_int == 0 )
849 /* if it was first one, there is disable too */
851 if( count.i_int < list.p_list->i_count )
853 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
854 &list.p_list->p_values[count.i_int] );
856 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
862 var_Get( p_input, "sub-autodetect-file", &val );
865 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
866 char **subs = subtitles_Detect( p_input, psz_autopath,
867 p_input->input.p_item->psz_uri );
870 for( i = 0; subs && subs[i]; i++ )
872 if( strcmp( psz_subtitle, subs[i] ) )
874 sub = InputSourceNew( p_input );
875 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
878 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
883 if( subs ) free( subs );
884 if( psz_autopath ) free( psz_autopath );
886 free( psz_subtitle );
889 psz = var_GetString( p_input, "input-slave" );
893 input_source_t *slave;
896 while( *psz == ' ' || *psz == '#' )
900 if( ( psz_delim = strchr( psz, '#' ) ) )
909 msg_Dbg( p_input, "adding slave input '%s'", psz );
910 slave = InputSourceNew( p_input );
911 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
913 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
918 if( psz ) free( psz );
922 p_input->i_start = 0;
923 p_input->i_start = 0;
929 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
930 i_es_out_mode = ES_OUT_MODE_AUTO;
932 if( p_input->p_sout )
934 var_Get( p_input, "sout-all", &val );
937 i_es_out_mode = ES_OUT_MODE_ALL;
942 var_Get( p_input, "programs", &val );
943 if ( val.p_list && val.p_list->i_count )
945 i_es_out_mode = ES_OUT_MODE_PARTIAL;
946 /* Note : we should remove the "program" callback. */
949 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
953 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
955 /* Inform the demuxer about waited group (needed only for DVB) */
956 if( i_es_out_mode == ES_OUT_MODE_ALL )
958 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
960 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
962 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
967 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
968 (int) var_GetInteger( p_input, "program" ), NULL );
971 if( p_input->p_sout )
973 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
975 p_input->b_out_pace_control = VLC_FALSE;
979 p_input->b_out_pace_control = VLC_TRUE;
982 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
984 /* We don't want a high input priority here or we'll
985 * end-up sucking up all the CPU time */
986 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
989 msg_Dbg( p_input, "starting in %s mode",
990 p_input->b_out_pace_control ? "asynch" : "synch" );
994 /* Get meta data from users */
995 p_meta_tmp = InputMetaUser( p_input );
997 /* Get meta data from master input */
998 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
1002 if( p_meta == NULL )
1004 p_meta = p_meta_tmp;
1006 else if( p_meta_tmp )
1008 vlc_meta_Merge( p_meta, p_meta_tmp );
1009 vlc_meta_Delete( p_meta_tmp );
1012 /* Access_file does not give any meta, and there are no slave */
1015 if( !p_input->input.p_access ||
1016 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1020 if( p_meta == NULL )
1022 p_meta = p_meta_tmp;
1024 else if( p_meta_tmp )
1026 vlc_meta_Merge( p_meta, p_meta_tmp );
1027 vlc_meta_Delete( p_meta_tmp );
1030 /* Get meta data from slave input */
1031 for( i = 0; i < p_input->i_slave; i++ )
1033 vlc_meta_t *p_meta_slave;
1035 if( !demux2_Control( p_input->slave[i]->p_demux,
1036 DEMUX_GET_META, &p_meta_slave ) )
1038 if( p_meta == NULL )
1040 p_meta = p_meta_slave;
1042 else if( p_meta_slave )
1044 vlc_meta_Merge( p_meta, p_meta_slave );
1045 vlc_meta_Delete( p_meta_slave );
1049 if( p_input->slave[i]->p_access &&
1050 !access2_Control( p_input->slave[i]->p_access,
1051 ACCESS_GET_META, &p_meta_slave ) )
1053 if( p_meta == NULL )
1055 p_meta = p_meta_slave;
1057 else if( p_meta_slave )
1059 vlc_meta_Merge( p_meta, p_meta_slave );
1060 vlc_meta_Delete( p_meta_slave );
1066 p_input->p_meta = p_meta;
1067 UpdateMeta( p_input, b_quick );
1071 msg_Dbg( p_input, "`%s' successfully opened",
1072 p_input->input.p_item->psz_uri );
1076 /* initialization is complete */
1077 p_input->i_state = PLAYING_S;
1079 val.i_int = PLAYING_S;
1080 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1085 if( p_input->p_es_out )
1086 input_EsOutDelete( p_input->p_es_out );
1088 if( p_input->p_sout )
1089 sout_DeleteInstance( p_input->p_sout );
1091 /* Mark them deleted */
1092 p_input->input.p_demux = NULL;
1093 p_input->input.p_stream = NULL;
1094 p_input->input.p_access = NULL;
1095 p_input->p_es_out = NULL;
1096 p_input->p_sout = NULL;
1098 return VLC_EGENERIC;
1101 /*****************************************************************************
1102 * Error: RunThread() error loop
1103 *****************************************************************************
1104 * This function is called when an error occurred during thread main's loop.
1105 *****************************************************************************/
1106 static void Error( input_thread_t *p_input )
1108 while( !p_input->b_die )
1111 msleep( INPUT_IDLE_SLEEP );
1115 /*****************************************************************************
1116 * End: end the input thread
1117 *****************************************************************************/
1118 static void End( input_thread_t * p_input )
1123 msg_Dbg( p_input, "closing input" );
1125 /* We are at the end */
1126 p_input->i_state = END_S;
1129 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1131 /* Clean control variables */
1132 input_ControlVarClean( p_input );
1134 /* Clean up master */
1135 InputSourceClean( p_input, &p_input->input );
1138 for( i = 0; i < p_input->i_slave; i++ )
1140 InputSourceClean( p_input, p_input->slave[i] );
1141 free( p_input->slave[i] );
1143 if( p_input->slave ) free( p_input->slave );
1145 /* Unload all modules */
1146 if( p_input->p_es_out )
1147 input_EsOutDelete( p_input->p_es_out );
1149 /* Close optional stream output instance */
1150 if( p_input->p_sout )
1152 vlc_object_t *p_pl =
1153 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1156 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1158 /* attach sout to the playlist */
1159 msg_Dbg( p_input, "keeping sout" );
1160 vlc_object_detach( p_input->p_sout );
1161 vlc_object_attach( p_input->p_sout, p_pl );
1165 msg_Dbg( p_input, "destroying sout" );
1166 sout_DeleteInstance( p_input->p_sout );
1169 vlc_object_release( p_pl );
1173 if( p_input->p_meta )
1174 vlc_meta_Delete( p_input->p_meta );
1176 /* Tell we're dead */
1177 p_input->b_dead = VLC_TRUE;
1180 /*****************************************************************************
1182 *****************************************************************************/
1183 static inline int ControlPopNoLock( input_thread_t *p_input,
1184 int *pi_type, vlc_value_t *p_val )
1186 if( p_input->i_control <= 0 )
1188 return VLC_EGENERIC;
1191 *pi_type = p_input->control[0].i_type;
1192 *p_val = p_input->control[0].val;
1194 p_input->i_control--;
1195 if( p_input->i_control > 0 )
1199 for( i = 0; i < p_input->i_control; i++ )
1201 p_input->control[i].i_type = p_input->control[i+1].i_type;
1202 p_input->control[i].val = p_input->control[i+1].val;
1209 static void ControlReduce( input_thread_t *p_input )
1212 for( i = 1; i < p_input->i_control; i++ )
1214 const int i_lt = p_input->control[i-1].i_type;
1215 const int i_ct = p_input->control[i].i_type;
1217 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1218 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1222 ( i_ct == INPUT_CONTROL_SET_STATE ||
1223 i_ct == INPUT_CONTROL_SET_RATE ||
1224 i_ct == INPUT_CONTROL_SET_POSITION ||
1225 i_ct == INPUT_CONTROL_SET_TIME ||
1226 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1227 i_ct == INPUT_CONTROL_SET_TITLE ||
1228 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1229 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1232 // msg_Dbg( p_input, "merged at %d", i );
1233 /* Remove the i-1 */
1234 for( j = i; j < p_input->i_control; j++ )
1235 p_input->control[j-1] = p_input->control[j];
1236 p_input->i_control--;
1240 /* TODO but that's not that important
1241 - merge SET_X with SET_X_CMD
1242 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1243 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1250 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1253 vlc_bool_t b_force_update = VLC_FALSE;
1255 if( !p_input ) return b_force_update;
1259 case INPUT_CONTROL_SET_DIE:
1260 msg_Dbg( p_input, "control: stopping input" );
1261 /* Mark all submodules to die */
1262 if( p_input->input.p_access )
1263 p_input->input.p_access->b_die = VLC_TRUE;
1264 if( p_input->input.p_stream )
1265 p_input->input.p_stream->b_die = VLC_TRUE;
1266 p_input->input.p_demux->b_die = VLC_TRUE;
1268 p_input->b_die = VLC_TRUE;
1271 case INPUT_CONTROL_SET_POSITION:
1272 case INPUT_CONTROL_SET_POSITION_OFFSET:
1275 if( i_type == INPUT_CONTROL_SET_POSITION )
1277 f_pos = val.f_float;
1281 /* Should not fail */
1282 demux2_Control( p_input->input.p_demux,
1283 DEMUX_GET_POSITION, &f_pos );
1284 f_pos += val.f_float;
1286 if( f_pos < 0.0 ) f_pos = 0.0;
1287 if( f_pos > 1.0 ) f_pos = 1.0;
1288 /* Reset the decoders states and clock synch (before calling the demuxer */
1289 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1290 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1291 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1294 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1295 "%2.1f%% failed", f_pos * 100 );
1299 if( p_input->i_slave > 0 )
1300 SlaveSeek( p_input );
1302 b_force_update = VLC_TRUE;
1307 case INPUT_CONTROL_SET_TIME:
1308 case INPUT_CONTROL_SET_TIME_OFFSET:
1313 if( i_type == INPUT_CONTROL_SET_TIME )
1315 i_time = val.i_time;
1319 /* Should not fail */
1320 demux2_Control( p_input->input.p_demux,
1321 DEMUX_GET_TIME, &i_time );
1322 i_time += val.i_time;
1324 if( i_time < 0 ) i_time = 0;
1326 /* Reset the decoders states and clock synch (before calling the demuxer */
1327 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1328 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1330 i_ret = demux2_Control( p_input->input.p_demux,
1331 DEMUX_SET_TIME, i_time );
1336 /* Emulate it with a SET_POS */
1337 demux2_Control( p_input->input.p_demux,
1338 DEMUX_GET_LENGTH, &i_length );
1341 double f_pos = (double)i_time / (double)i_length;
1342 i_ret = demux2_Control( p_input->input.p_demux,
1343 DEMUX_SET_POSITION, f_pos );
1348 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1349 " failed or not possible", i_time );
1353 if( p_input->i_slave > 0 )
1354 SlaveSeek( p_input );
1356 b_force_update = VLC_TRUE;
1361 case INPUT_CONTROL_SET_STATE:
1362 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1363 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1366 if( p_input->input.p_access )
1367 i_ret = access2_Control( p_input->input.p_access,
1368 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1370 i_ret = demux2_Control( p_input->input.p_demux,
1371 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1375 /* FIXME What to do ? */
1376 msg_Warn( p_input, "cannot unset pause -> EOF" );
1377 vlc_mutex_unlock( &p_input->lock_control );
1378 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1379 vlc_mutex_lock( &p_input->lock_control );
1382 b_force_update = VLC_TRUE;
1384 /* Switch to play */
1385 p_input->i_state = PLAYING_S;
1386 val.i_int = PLAYING_S;
1387 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1390 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1391 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1393 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1394 p_input->b_can_pause )
1397 if( p_input->input.p_access )
1398 i_ret = access2_Control( p_input->input.p_access,
1399 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1401 i_ret = demux2_Control( p_input->input.p_demux,
1402 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1404 b_force_update = VLC_TRUE;
1408 msg_Warn( p_input, "cannot set pause state" );
1409 val.i_int = p_input->i_state;
1413 val.i_int = PAUSE_S;
1416 /* Switch to new state */
1417 p_input->i_state = val.i_int;
1418 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1420 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1422 b_force_update = VLC_TRUE;
1424 /* Correct "state" value */
1425 val.i_int = p_input->i_state;
1426 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1428 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1430 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1434 case INPUT_CONTROL_SET_RATE:
1435 case INPUT_CONTROL_SET_RATE_SLOWER:
1436 case INPUT_CONTROL_SET_RATE_FASTER:
1440 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1441 i_rate = p_input->i_rate * 2;
1442 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1443 i_rate = p_input->i_rate / 2;
1447 if( i_rate < INPUT_RATE_MIN )
1449 msg_Dbg( p_input, "cannot set rate faster" );
1450 i_rate = INPUT_RATE_MIN;
1452 else if( i_rate > INPUT_RATE_MAX )
1454 msg_Dbg( p_input, "cannot set rate slower" );
1455 i_rate = INPUT_RATE_MAX;
1457 if( i_rate != INPUT_RATE_DEFAULT &&
1458 ( !p_input->b_can_pace_control ||
1459 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1461 msg_Dbg( p_input, "cannot change rate" );
1462 i_rate = INPUT_RATE_DEFAULT;
1464 if( i_rate != p_input->i_rate )
1466 p_input->i_rate = i_rate;
1468 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1470 /* We haven't send data to decoder when rate != default */
1471 if( i_rate == INPUT_RATE_DEFAULT )
1472 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1475 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1477 b_force_update = VLC_TRUE;
1482 case INPUT_CONTROL_SET_PROGRAM:
1483 /* No need to force update, es_out does it if needed */
1484 es_out_Control( p_input->p_es_out,
1485 ES_OUT_SET_GROUP, val.i_int );
1487 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1491 case INPUT_CONTROL_SET_ES:
1492 /* No need to force update, es_out does it if needed */
1493 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1494 input_EsOutGetFromID( p_input->p_es_out,
1498 case INPUT_CONTROL_SET_AUDIO_DELAY:
1499 input_EsOutSetDelay( p_input->p_es_out,
1500 AUDIO_ES, val.i_time );
1501 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1504 case INPUT_CONTROL_SET_SPU_DELAY:
1505 input_EsOutSetDelay( p_input->p_es_out,
1506 SPU_ES, val.i_time );
1507 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1510 case INPUT_CONTROL_SET_TITLE:
1511 case INPUT_CONTROL_SET_TITLE_NEXT:
1512 case INPUT_CONTROL_SET_TITLE_PREV:
1513 if( p_input->input.b_title_demux &&
1514 p_input->input.i_title > 0 )
1517 /* FIXME handle demux title */
1518 demux_t *p_demux = p_input->input.p_demux;
1521 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1522 i_title = p_demux->info.i_title - 1;
1523 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1524 i_title = p_demux->info.i_title + 1;
1526 i_title = val.i_int;
1528 if( i_title >= 0 && i_title < p_input->input.i_title )
1530 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1531 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1533 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1534 input_ControlVarTitle( p_input, i_title );
1537 else if( p_input->input.i_title > 0 )
1539 access_t *p_access = p_input->input.p_access;
1542 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1543 i_title = p_access->info.i_title - 1;
1544 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1545 i_title = p_access->info.i_title + 1;
1547 i_title = val.i_int;
1549 if( i_title >= 0 && i_title < p_input->input.i_title )
1551 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1552 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1554 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1555 stream_AccessReset( p_input->input.p_stream );
1559 case INPUT_CONTROL_SET_SEEKPOINT:
1560 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1561 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1562 if( p_input->input.b_title_demux &&
1563 p_input->input.i_title > 0 )
1565 demux_t *p_demux = p_input->input.p_demux;
1567 int64_t i_input_time;
1568 int64_t i_seekpoint_time;
1570 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1572 i_seekpoint = p_demux->info.i_seekpoint;
1573 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1574 if( i_seekpoint_time >= 0 &&
1575 !demux2_Control( p_demux,
1576 DEMUX_GET_TIME, &i_input_time ) )
1578 if ( i_input_time < i_seekpoint_time + 3000000 )
1584 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1585 i_seekpoint = p_demux->info.i_seekpoint + 1;
1587 i_seekpoint = val.i_int;
1589 if( i_seekpoint >= 0 && i_seekpoint <
1590 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1592 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1593 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1595 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1598 else if( p_input->input.i_title > 0 )
1600 demux_t *p_demux = p_input->input.p_demux;
1601 access_t *p_access = p_input->input.p_access;
1603 int64_t i_input_time;
1604 int64_t i_seekpoint_time;
1606 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1608 i_seekpoint = p_access->info.i_seekpoint;
1609 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1610 if( i_seekpoint_time >= 0 &&
1611 demux2_Control( p_demux,
1612 DEMUX_GET_TIME, &i_input_time ) )
1614 if ( i_input_time < i_seekpoint_time + 3000000 )
1620 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1621 i_seekpoint = p_access->info.i_seekpoint + 1;
1623 i_seekpoint = val.i_int;
1625 if( i_seekpoint >= 0 && i_seekpoint <
1626 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1628 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1629 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1631 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1632 stream_AccessReset( p_input->input.p_stream );
1637 case INPUT_CONTROL_ADD_SLAVE:
1638 if( val.psz_string )
1640 input_source_t *slave = InputSourceNew( p_input );
1642 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1645 vlc_meta_t *p_meta_new = NULL;
1650 msg_Dbg( p_input, "adding %s as slave on the fly",
1654 if( demux2_Control( p_input->input.p_demux,
1655 DEMUX_GET_TIME, &i_time ) )
1657 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1658 InputSourceClean( p_input, slave );
1662 if( demux2_Control( slave->p_demux,
1663 DEMUX_SET_TIME, i_time ) )
1665 msg_Err( p_input, "seek failed for new slave" );
1666 InputSourceClean( p_input, slave );
1672 /* Get meta (access and demux) */
1673 if( access2_Control( slave->p_access,
1674 ACCESS_GET_META, &p_meta_new ) )
1676 if( !demux2_Control( slave->p_demux,
1677 DEMUX_GET_META, &p_meta ) )
1681 vlc_meta_Merge( p_meta_new, p_meta );
1682 vlc_meta_Delete( p_meta );
1686 p_meta_new = p_meta;
1692 if( p_input->p_meta )
1694 vlc_meta_Merge( p_input->p_meta, p_meta_new );
1695 vlc_meta_Delete( p_meta_new );
1699 p_input->p_meta = p_meta_new;
1701 UpdateMeta( p_input, VLC_FALSE );
1704 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1708 msg_Warn( p_input, "failed to add %s as slave",
1712 free( val.psz_string );
1716 case INPUT_CONTROL_SET_BOOKMARK:
1718 msg_Err( p_input, "not yet implemented" );
1722 return b_force_update;
1725 /*****************************************************************************
1727 *****************************************************************************/
1728 static int UpdateFromDemux( input_thread_t *p_input )
1730 demux_t *p_demux = p_input->input.p_demux;
1733 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1735 v.i_int = p_demux->info.i_title;
1736 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1738 input_ControlVarTitle( p_input, p_demux->info.i_title );
1740 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1742 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1744 v.i_int = p_demux->info.i_seekpoint;
1745 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1747 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1749 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1751 /* Hmmm only works with master input */
1752 if( p_input->input.p_demux == p_demux )
1754 int i_title_end = p_input->input.i_title_end -
1755 p_input->input.i_title_offset;
1756 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1757 p_input->input.i_seekpoint_offset;
1759 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1761 if( p_demux->info.i_title > i_title_end ||
1762 ( p_demux->info.i_title == i_title_end &&
1763 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1765 else if( i_seekpoint_end >=0 )
1767 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1769 else if( i_title_end >= 0 )
1771 if( p_demux->info.i_title > i_title_end ) return 0;
1778 /*****************************************************************************
1780 *****************************************************************************/
1781 static int UpdateFromAccess( input_thread_t *p_input )
1783 access_t *p_access = p_input->input.p_access;
1786 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1788 v.i_int = p_access->info.i_title;
1789 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1791 input_ControlVarTitle( p_input, p_access->info.i_title );
1793 stream_AccessUpdate( p_input->input.p_stream );
1795 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1797 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1799 v.i_int = p_access->info.i_seekpoint;
1800 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1802 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1804 if( p_access->info.i_update & INPUT_UPDATE_META )
1806 /* TODO maybe multi - access ? */
1808 if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1810 if( p_input->p_meta )
1812 vlc_meta_Merge( p_input->p_meta, p_meta );
1813 vlc_meta_Delete( p_meta );
1817 p_input->p_meta = p_meta;
1820 UpdateMeta( p_input, VLC_FALSE );
1821 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1823 p_access->info.i_update &= ~INPUT_UPDATE_META;
1826 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1828 /* Hmmm only works with master input */
1829 if( p_input->input.p_access == p_access )
1831 int i_title_end = p_input->input.i_title_end -
1832 p_input->input.i_title_offset;
1833 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1834 p_input->input.i_seekpoint_offset;
1836 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1838 if( p_access->info.i_title > i_title_end ||
1839 ( p_access->info.i_title == i_title_end &&
1840 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1842 else if( i_seekpoint_end >=0 )
1844 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1846 else if( i_title_end >= 0 )
1848 if( p_access->info.i_title > i_title_end ) return 0;
1855 /*****************************************************************************
1857 *****************************************************************************/
1858 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1860 vlc_meta_t *p_meta = p_input->p_meta;
1863 if( !p_meta || p_meta->i_meta == 0 )
1866 if( !b_quick ) msg_Dbg( p_input, "meta information:" );
1867 for( i = 0; i < p_meta->i_meta; i++ )
1870 msg_Dbg( p_input, " - '%s' = '%s'",
1871 _(p_meta->name[i]), p_meta->value[i] );
1873 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] &&
1874 !p_input->input.p_item->b_fixed_name )
1875 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1877 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1878 input_Control( p_input, INPUT_ADD_INFO, _("General"),
1879 _("Author"), p_meta->value[i] );
1881 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1882 _(p_meta->name[i]), "%s", p_meta->value[i] );
1885 for( i = 0; i < p_meta->i_track; i++ )
1887 vlc_meta_t *tk = p_meta->track[i];
1890 if( tk->i_meta > 0 )
1892 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1894 msg_Dbg( p_input, " - track[%d]:", i );
1896 sprintf( psz_cat, "%s %d", _("Stream"), i );
1897 for( j = 0; j < tk->i_meta; j++ )
1899 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1902 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1903 _(tk->name[j]), "%s", tk->value[j] );
1908 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1910 p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1916 /*****************************************************************************
1918 *****************************************************************************/
1919 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1920 vlc_bool_t b_quick )
1922 playlist_t *p_playlist;
1923 char psz_buffer[MSTRTIME_MAX_SIZE];
1925 vlc_mutex_lock( &p_input->input.p_item->lock );
1926 p_input->input.p_item->i_duration = i_length;
1927 vlc_mutex_unlock( &p_input->input.p_item->lock );
1929 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1933 var_SetInteger( p_playlist, "item-change",
1934 p_input->input.p_item->i_id );
1935 vlc_object_release( p_playlist );
1938 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1939 msecstotimestr( psz_buffer, i_length / 1000 ) );
1942 /*****************************************************************************
1944 *****************************************************************************/
1945 static input_source_t *InputSourceNew( input_thread_t *p_input )
1947 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1951 msg_Err( p_input, "out of memory for new input source" );
1956 in->p_access = NULL;
1957 in->p_stream = NULL;
1959 in->b_title_demux = VLC_FALSE;
1962 in->b_can_pace_control = VLC_TRUE;
1963 in->b_eof = VLC_FALSE;
1964 in->i_cr_average = 0;
1969 /*****************************************************************************
1971 *****************************************************************************/
1972 static int InputSourceInit( input_thread_t *p_input,
1973 input_source_t *in, char *psz_mrl,
1974 char *psz_forced_demux, vlc_bool_t b_quick )
1976 char *psz_dup = strdup( psz_mrl );
1984 if( !in ) return VLC_EGENERIC;
1989 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1990 &psz_access, &psz_demux, &psz_path );
1992 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1993 psz_mrl, psz_access, psz_demux, psz_path );
1995 /* Hack to allow udp://@:port syntax */
1997 (strncmp( psz_access, "udp", 3 ) &&
1998 strncmp( psz_access, "rtp", 3 )) )
2000 /* Find optional titles and seekpoints */
2001 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2002 &in->i_seekpoint_start, &in->i_seekpoint_end );
2004 if( psz_forced_demux && *psz_forced_demux )
2006 psz_demux = psz_forced_demux;
2008 else if( !psz_demux || *psz_demux == '\0' )
2010 /* special hack for forcing a demuxer with --demux=module
2011 * (and do nothing with a list) */
2012 char *psz_var_demux = var_GetString( p_input, "demux" );
2014 if( *psz_var_demux != '\0' &&
2015 !strchr(psz_var_demux, ',' ) &&
2016 !strchr(psz_var_demux, ':' ) )
2018 psz_demux = psz_var_demux;
2020 msg_Dbg( p_input, "Enforce demux ` %s'", psz_demux );
2022 else if( psz_var_demux )
2024 free( psz_var_demux );
2028 /* Try access_demux if no demux given */
2029 if( *psz_demux == '\0' )
2031 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2032 NULL, p_input->p_es_out, VLC_FALSE );
2038 msg_Dbg( p_input, "trying to preparse %s", psz_path );
2039 psz_demux = strdup( "" );
2040 psz_access = strdup( "file" );
2045 int64_t i_pts_delay;
2047 /* Get infos from access_demux */
2048 demux2_Control( in->p_demux,
2049 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2050 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2052 in->b_title_demux = VLC_TRUE;
2053 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2054 &in->title, &in->i_title,
2055 &in->i_title_offset, &in->i_seekpoint_offset ) )
2060 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2061 &in->b_can_pace_control );
2062 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2066 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2072 int64_t i_pts_delay;
2074 /* Now try a real access */
2075 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2078 /* Access failed, URL encoded ? */
2079 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2081 DecodeUrl( psz_path );
2083 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2084 psz_access, psz_demux, psz_path );
2086 in->p_access = access2_New( p_input,
2087 psz_access, psz_demux, psz_path,
2090 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2091 * are forbidden in filenames on Win32. */
2093 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2094 if( in->p_access == NULL &&
2095 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2097 if( psz_dup ) free( psz_dup );
2098 psz_dup = strdup( psz_mrl );
2103 in->p_access = access2_New( p_input,
2104 psz_access, psz_demux, psz_path,
2109 if( in->p_access == NULL )
2111 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2112 intf_UserFatal( VLC_OBJECT( p_input),
2113 _("Errors"),"Unable to open '%s'", psz_mrl );
2118 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2119 while( psz && *psz )
2121 access_t *p_access = in->p_access;
2122 char *end = strchr( psz, ':' );
2127 in->p_access = access2_FilterNew( in->p_access, psz );
2128 if( in->p_access == NULL )
2130 in->p_access = p_access;
2131 msg_Warn( p_input, "failed to insert access filter %s",
2137 if( psz_tmp ) free( psz_tmp );
2139 /* Get infos from access */
2142 access2_Control( in->p_access,
2143 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2144 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2146 in->b_title_demux = VLC_FALSE;
2147 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2148 &in->title, &in->i_title,
2149 &in->i_title_offset, &in->i_seekpoint_offset ) )
2155 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2156 &in->b_can_pace_control );
2157 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2159 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2161 var_Set( p_input, "seekable", val );
2164 /* Create the stream_t */
2165 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2166 if( in->p_stream == NULL )
2168 msg_Warn( p_input, "cannot create a stream_t from access" );
2172 /* Open a demuxer */
2173 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2175 psz_demux = in->p_access->psz_demux;
2177 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2178 in->p_stream, p_input->p_es_out, b_quick );
2179 if( in->p_demux == NULL )
2181 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2182 psz_access, psz_demux, psz_path );
2183 intf_UserFatal( VLC_OBJECT( p_input), _("Errors"),
2184 "Unrecognized format for '%s'", psz_mrl );
2188 /* TODO get title from demux */
2189 if( !b_quick && in->i_title <= 0 )
2191 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2192 &in->title, &in->i_title,
2193 &in->i_title_offset, &in->i_seekpoint_offset ))
2200 in->b_title_demux = VLC_TRUE;
2205 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2206 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2208 if( psz_dup ) free( psz_dup );
2213 demux2_Delete( in->p_demux );
2216 stream_Delete( in->p_stream );
2219 access2_Delete( in->p_access );
2220 if( psz_dup ) free( psz_dup );
2222 return VLC_EGENERIC;
2225 /*****************************************************************************
2227 *****************************************************************************/
2228 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2231 demux2_Delete( in->p_demux );
2234 stream_Delete( in->p_stream );
2237 access2_Delete( in->p_access );
2239 if( in->i_title > 0 )
2242 for( i = 0; i < in->i_title; i++ )
2244 vlc_input_title_Delete( in->title[i] );
2250 static void SlaveDemux( input_thread_t *p_input )
2255 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2257 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2261 for( i = 0; i < p_input->i_slave; i++ )
2263 input_source_t *in = p_input->slave[i];
2269 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2274 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2276 msg_Err( p_input, "slave[%d] doesn't like "
2277 "DEMUX_GET_TIME -> EOF", i );
2282 if( i_stime >= i_time )
2285 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2291 i_ret = in->p_demux->pf_demux( in->p_demux );
2296 msg_Dbg( p_input, "slave %d EOF", i );
2297 in->b_eof = VLC_TRUE;
2302 static void SlaveSeek( input_thread_t *p_input )
2307 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2309 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2313 for( i = 0; i < p_input->i_slave; i++ )
2315 input_source_t *in = p_input->slave[i];
2317 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2319 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2320 in->b_eof = VLC_TRUE;
2324 /*****************************************************************************
2326 *****************************************************************************/
2327 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2332 if( ( p_meta = vlc_meta_New() ) == NULL )
2335 /* Get meta information from user */
2336 #define GET_META( c, s ) \
2337 var_Get( p_input, (s), &val ); \
2338 if( *val.psz_string ) \
2339 vlc_meta_Add( p_meta, c, val.psz_string ); \
2340 free( val.psz_string )
2342 GET_META( VLC_META_TITLE, "meta-title" );
2343 GET_META( VLC_META_AUTHOR, "meta-author" );
2344 GET_META( VLC_META_ARTIST, "meta-artist" );
2345 GET_META( VLC_META_GENRE, "meta-genre" );
2346 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2347 GET_META( VLC_META_DESCRIPTION, "meta-description" );
2348 GET_META( VLC_META_DATE, "meta-date" );
2349 GET_META( VLC_META_URL, "meta-url" );
2355 /*****************************************************************************
2356 * DecodeUrl: decode a given encoded url
2357 *****************************************************************************/
2358 static void DecodeUrl( char *psz )
2360 char *dup = strdup( psz );
2378 *psz++ = strtol( val, NULL, 16 );
2380 else if( *p == '+' )
2390 if( psz ) *psz++ ='\0';
2391 if( dup ) free( dup );
2394 /*****************************************************************************
2395 * ParseOption: parses the options for the input
2396 *****************************************************************************
2397 * This function parses the input (config) options and creates their associated
2399 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
2400 * bar is the value of the option.
2401 *****************************************************************************/
2402 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2404 char *psz_name = (char *)psz_option;
2405 char *psz_value = strchr( psz_option, '=' );
2406 int i_name_len, i_type;
2407 vlc_bool_t b_isno = VLC_FALSE;
2410 if( psz_value ) i_name_len = psz_value - psz_option;
2411 else i_name_len = strlen( psz_option );
2413 /* It's too much of an hassle to remove the ':' when we parse
2414 * the cmd line :) */
2415 if( i_name_len && *psz_name == ':' )
2421 if( i_name_len == 0 ) return;
2423 psz_name = strndup( psz_name, i_name_len );
2424 if( psz_value ) psz_value++;
2426 /* FIXME: :programs should be handled generically */
2427 if( !strcmp( psz_name, "programs" ) )
2428 i_type = VLC_VAR_LIST;
2430 i_type = config_GetType( p_input, psz_name );
2432 if( !i_type && !psz_value )
2434 /* check for "no-foo" or "nofoo" */
2435 if( !strncmp( psz_name, "no-", 3 ) )
2437 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2439 else if( !strncmp( psz_name, "no", 2 ) )
2441 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2443 else goto cleanup; /* Option doesn't exist */
2446 i_type = config_GetType( p_input, psz_name );
2448 if( !i_type ) goto cleanup; /* Option doesn't exist */
2450 else if( !i_type ) goto cleanup; /* Option doesn't exist */
2452 if( ( i_type != VLC_VAR_BOOL ) &&
2453 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2455 /* Create the variable in the input object.
2456 * Children of the input object will be able to retreive this value
2457 * thanks to the inheritance property of the object variables. */
2458 var_Create( p_input, psz_name, i_type );
2463 val.b_bool = !b_isno;
2466 case VLC_VAR_INTEGER:
2467 val.i_int = strtol( psz_value, NULL, 0 );
2471 val.f_float = atof( psz_value );
2474 case VLC_VAR_STRING:
2475 case VLC_VAR_MODULE:
2477 case VLC_VAR_DIRECTORY:
2478 val.psz_string = psz_value;
2483 char *psz_orig, *psz_var;
2484 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
2485 val.p_list = p_list;
2486 p_list->i_count = 0;
2488 psz_var = psz_orig = strdup(psz_value);
2489 while( psz_var && *psz_var )
2491 char *psz_item = psz_var;
2493 while( *psz_var && *psz_var != ',' ) psz_var++;
2494 if( *psz_var == ',' )
2499 val2.i_int = strtol( psz_item, NULL, 0 );
2500 INSERT_ELEM( p_list->p_values, p_list->i_count,
2501 p_list->i_count, val2 );
2502 /* p_list->i_count is incremented twice by INSERT_ELEM */
2504 INSERT_ELEM( p_list->pi_types, p_list->i_count,
2505 p_list->i_count, VLC_VAR_INTEGER );
2507 if( psz_orig ) free( psz_orig );
2516 var_Set( p_input, psz_name, val );
2518 msg_Dbg( p_input, "set input option: %s to %s", psz_name,
2519 psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2522 if( psz_name ) free( psz_name );
2526 /*****************************************************************************
2527 * MRLSplit: parse the access, demux and url part of the
2528 * Media Resource Locator.
2529 *****************************************************************************/
2530 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2531 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2533 char *psz_access = NULL;
2534 char *psz_demux = NULL;
2535 char *psz_path = NULL;
2536 char *psz, *psz_check;
2538 psz = strchr( psz_dup, ':' );
2540 /* '@' not allowed in access/demux part */
2541 psz_check = strchr( psz_dup, '@' );
2542 if( psz_check && psz_check < psz ) psz = 0;
2544 #if defined( WIN32 ) || defined( UNDER_CE )
2545 if( psz - psz_dup == 1 )
2547 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2556 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2560 psz = strchr( psz_dup, '/' );
2567 psz_access = psz_dup;
2574 if( !psz_access ) *ppsz_access = "";
2575 else *ppsz_access = psz_access;
2577 if( !psz_demux ) *ppsz_demux = "";
2578 else *ppsz_demux = psz_demux;
2580 if( !psz_path ) *ppsz_path = "";
2581 else *ppsz_path = psz_path;
2584 /*****************************************************************************
2585 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2588 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2589 *****************************************************************************/
2590 static void MRLSections( input_thread_t *p_input, char *psz_source,
2591 int *pi_title_start, int *pi_title_end,
2592 int *pi_chapter_start, int *pi_chapter_end )
2594 char *psz, *psz_end, *psz_next, *psz_check;
2596 *pi_title_start = *pi_title_end = -1;
2597 *pi_chapter_start = *pi_chapter_end = -1;
2599 /* Start by parsing titles and chapters */
2600 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2602 /* Check we are really dealing with a title/chapter section */
2603 psz_check = psz + 1;
2604 if( !*psz_check ) return;
2605 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2606 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2607 if( *psz_check == ':' && ++psz_check )
2608 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2609 if( *psz_check != '-' && *psz_check ) return;
2610 if( *psz_check == '-' && ++psz_check )
2611 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2612 if( *psz_check != ':' && *psz_check ) return;
2613 if( *psz_check == ':' && ++psz_check )
2614 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2615 if( *psz_check ) return;
2617 /* Separate start and end */
2619 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2621 /* Look for the start title */
2622 *pi_title_start = strtol( psz, &psz_next, 0 );
2623 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2624 *pi_title_end = *pi_title_start;
2627 /* Look for the start chapter */
2629 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2630 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2631 *pi_chapter_end = *pi_chapter_start;
2635 /* Look for the end title */
2636 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2637 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2640 /* Look for the end chapter */
2641 if( *psz_end ) psz_end++;
2642 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2643 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2646 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2647 psz_source, *pi_title_start, *pi_chapter_start,
2648 *pi_title_end, *pi_chapter_end );
2652 /***********************************************************************
2653 * Info management functions
2654 ***********************************************************************/
2656 * Get a info item from a given category in a given input item.
2658 * \param p_i The input item to get info from
2659 * \param psz_cat String representing the category for the info
2660 * \param psz_name String representing the name of the desired info
2661 * \return A pointer to the string with the given info if found, or an
2662 * empty string otherwise. The caller should free the returned
2665 char *vlc_input_item_GetInfo( input_item_t *p_i,
2666 const char *psz_cat,
2667 const char *psz_name )
2671 vlc_mutex_lock( &p_i->lock );
2673 for( i = 0 ; i< p_i->i_categories ; i++ )
2675 info_category_t *p_cat = p_i->pp_categories[i];
2677 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
2680 for( j = 0; j < p_cat->i_infos ; j++ )
2682 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
2684 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
2685 vlc_mutex_unlock( &p_i->lock );
2690 vlc_mutex_unlock( &p_i->lock );
2691 return strdup( "" );
2694 int vlc_input_item_AddInfo( input_item_t *p_i,
2695 const char *psz_cat,
2696 const char *psz_name,
2697 const char *psz_format, ... )
2701 info_t *p_info = NULL;
2702 info_category_t *p_cat = NULL ;
2704 vlc_mutex_lock( &p_i->lock );
2706 for( i = 0 ; i < p_i->i_categories ; i ++ )
2708 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
2710 p_cat = p_i->pp_categories[i];
2716 if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
2718 vlc_mutex_unlock( &p_i->lock );
2719 return VLC_EGENERIC;
2721 p_cat->psz_name = strdup( psz_cat );
2723 p_cat->pp_infos = 0;
2724 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
2728 for( i = 0; i< p_cat->i_infos; i++ )
2730 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
2732 p_info = p_cat->pp_infos[i];
2739 if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
2741 vlc_mutex_unlock( &p_i->lock );
2742 return VLC_EGENERIC;
2744 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
2745 p_info->psz_name = strdup( psz_name );
2749 if( p_info->psz_value ) free( p_info->psz_value );
2752 va_start( args, psz_format );
2753 vasprintf( &p_info->psz_value, psz_format, args);
2756 vlc_mutex_unlock( &p_i->lock );