1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
33 #include <vlc/decoder.h>
36 #include "input_internal.h"
38 #include "stream_output.h"
39 #include "vlc_playlist.h"
40 #include "vlc_interface.h"
41 #include "vlc_interaction.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Run ( input_thread_t *p_input );
47 static int RunAndClean ( input_thread_t *p_input );
49 static input_thread_t * Create ( vlc_object_t *, input_item_t *, char *,
51 static int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
52 static void Error ( input_thread_t *p_input );
53 static void End ( input_thread_t *p_input );
54 static void MainLoop( input_thread_t *p_input );
56 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
57 static void ControlReduce( input_thread_t * );
58 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
61 static int UpdateFromAccess( input_thread_t * );
62 static int UpdateFromDemux( input_thread_t * );
63 static int UpdateMeta( input_thread_t *, vlc_bool_t );
65 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
67 static void ParseOption( input_thread_t *p_input, const char *psz_option );
69 static void DecodeUrl( char * );
70 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
72 static input_source_t *InputSourceNew( input_thread_t *);
73 static int InputSourceInit( input_thread_t *, input_source_t *,
74 char *, char *psz_forced_demux,
76 static void InputSourceClean( input_thread_t *, input_source_t * );
78 static void SlaveDemux( input_thread_t *p_input );
79 static void SlaveSeek( input_thread_t *p_input );
81 static vlc_meta_t *InputMetaUser( input_thread_t *p_input );
83 /*****************************************************************************
84 * This function creates a new input, and returns a pointer
85 * to its description. On error, it returns NULL.
87 * Variables for _public_ use:
90 * - rate,rate-slower, rate-faster
91 * - position, position-offset
93 * - title,title-next,title-prev
94 * - chapter,chapter-next, chapter-prev
95 * - program, audio-es, video-es, spu-es
96 * - audio-delay, spu-delay
101 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
102 * * For intf callback upon changes
104 * TODO explain when Callback is called
105 * TODO complete this list (?)
106 *****************************************************************************/
107 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
108 char *psz_header, vlc_bool_t b_quick )
110 input_thread_t *p_input = NULL; /* thread descriptor */
114 /* Allocate descriptor */
115 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
116 if( p_input == NULL )
118 msg_Err( p_parent, "out of memory" );
121 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
123 /* Init Common fields */
124 p_input->b_eof = VLC_FALSE;
125 p_input->b_can_pace_control = VLC_TRUE;
126 p_input->i_start = 0;
129 p_input->i_title = 0;
130 p_input->title = NULL;
131 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
132 p_input->i_state = INIT_S;
133 p_input->i_rate = INPUT_RATE_DEFAULT;
134 p_input->i_bookmark = 0;
135 p_input->bookmark = NULL;
136 p_input->p_meta = NULL;
137 p_input->p_es_out = NULL;
138 p_input->p_sout = NULL;
139 p_input->b_out_pace_control = VLC_FALSE;
140 p_input->i_pts_delay = 0;
142 /* Init Input fields */
143 p_input->input.p_item = p_item;
144 p_input->input.p_access = NULL;
145 p_input->input.p_stream = NULL;
146 p_input->input.p_demux = NULL;
147 p_input->input.b_title_demux = VLC_FALSE;
148 p_input->input.i_title = 0;
149 p_input->input.title = NULL;
150 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
151 p_input->input.b_can_pace_control = VLC_TRUE;
152 p_input->input.b_eof = VLC_FALSE;
153 p_input->input.i_cr_average = 0;
155 stats_ReinitInputStats( p_item->p_stats );
158 p_input->i_slave = 0;
159 p_input->slave = NULL;
161 /* Init control buffer */
162 vlc_mutex_init( p_input, &p_input->lock_control );
163 p_input->i_control = 0;
165 /* Parse input options */
166 vlc_mutex_lock( &p_item->lock );
167 for( i = 0; i < p_item->i_options; i++ )
169 ParseOption( p_input, p_item->ppsz_options[i] );
171 vlc_mutex_unlock( &p_item->lock );
173 /* Create Object Variables for private use only */
174 input_ConfigVarInit( p_input );
176 /* Create Objects variables for public Get and Set */
177 input_ControlVarInit( p_input );
178 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
182 var_Get( p_input, "bookmarks", &val );
185 /* FIXME: have a common cfg parsing routine used by sout and others */
186 char *psz_parser, *psz_start, *psz_end;
187 psz_parser = val.psz_string;
188 while( (psz_start = strchr( psz_parser, '{' ) ) )
190 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
193 psz_end = strchr( psz_start, '}' );
194 if( !psz_end ) break;
195 psz_parser = psz_end + 1;
196 backup = *psz_parser;
199 while( (psz_end = strchr( psz_start, ',' ) ) )
202 if( !strncmp( psz_start, "name=", 5 ) )
204 p_seekpoint->psz_name = psz_start + 5;
206 else if( !strncmp( psz_start, "bytes=", 6 ) )
208 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
210 else if( !strncmp( psz_start, "time=", 5 ) )
212 p_seekpoint->i_time_offset = atoll(psz_start + 5) * 1000000;
214 psz_start = psz_end + 1;
216 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
217 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
218 p_seekpoint->i_time_offset );
219 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
220 vlc_seekpoint_Delete( p_seekpoint );
221 *psz_parser = backup;
223 free( val.psz_string );
227 /* Remove 'Now playing' info as it is probably outdated */
228 input_Control( p_input, INPUT_DEL_INFO, _("Meta-information"),
229 VLC_META_NOW_PLAYING );
235 * Initialize an input thread and run it. You will need to monitor the thread to clean
236 * up after it is done
238 * \param p_parent a vlc_object
239 * \param p_item an input item
240 * \return a pointer to the spawned input thread
242 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
243 input_item_t *p_item )
245 return __input_CreateThread2( p_parent, p_item, NULL );
250 input_thread_t *__input_CreateThread2( vlc_object_t *p_parent,
251 input_item_t *p_item,
254 input_thread_t *p_input = NULL; /* thread descriptor */
256 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
257 /* Now we can attach our new input */
258 vlc_object_attach( p_input, p_parent );
260 /* Create thread and wait for its readiness. */
261 if( vlc_thread_create( p_input, "input", Run,
262 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
264 msg_Err( p_input, "cannot create input thread" );
265 vlc_object_detach( p_input );
266 vlc_object_destroy( p_input );
275 * Initialize an input thread and run it. This thread will clean after himself,
276 * you can forget about it. It can work either in blocking or non-blocking mode
278 * \param p_parent a vlc_object
279 * \param p_item an input item
280 * \param b_block should we block until read is finished ?
281 * \return the input object id if non blocking, an error code else
283 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
286 input_thread_t *p_input = NULL; /* thread descriptor */
288 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
289 /* Now we can attach our new input */
290 vlc_object_attach( p_input, p_parent );
294 RunAndClean( p_input );
299 if( vlc_thread_create( p_input, "input", RunAndClean,
300 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
302 msg_Err( p_input, "cannot create input thread" );
303 vlc_object_detach( p_input );
304 vlc_object_destroy( p_input );
308 return p_input->i_object_id;
312 * Initialize an input and initialize it to preparse the item
313 * This function is blocking. It will only accept to parse files
315 * \param p_parent a vlc_object_t
316 * \param p_item an input item
317 * \return VLC_SUCCESS or an error
319 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
321 input_thread_t *p_input = NULL; /* thread descriptor */
323 /* Allocate descriptor */
324 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
326 /* Now we can attach our new input */
327 vlc_object_attach( p_input, p_parent );
329 Init( p_input, VLC_TRUE );
331 /* Clean up master */
332 InputSourceClean( p_input, &p_input->input );
334 /* Kill access and demux */
335 if( p_input->input.p_access ) p_input->input.p_access->b_die = VLC_TRUE;
336 if( p_input->input.p_demux ) p_input->input.p_access->b_die = VLC_TRUE;
338 /* Unload all modules */
339 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
342 if( p_input->p_meta ) vlc_meta_Delete( p_input->p_meta );
344 vlc_object_detach( p_input );
345 vlc_object_destroy( p_input );
351 * Request a running input thread to stop and die
353 * \param the input thread to stop
355 void input_StopThread( input_thread_t *p_input )
360 /* Set die for input */
361 p_input->b_die = VLC_TRUE;
363 /* We cannot touch p_input fields directly (we can from another thread),
364 * so use the vlc_object_find way, it's perfectly safe */
366 /* Set die for all access */
367 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
368 for( i = 0; i < p_list->i_count; i++ )
370 p_list->p_values[i].p_object->b_die = VLC_TRUE;
372 vlc_list_release( p_list );
374 /* Set die for all stream */
375 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
376 for( i = 0; i < p_list->i_count; i++ )
378 p_list->p_values[i].p_object->b_die = VLC_TRUE;
380 vlc_list_release( p_list );
382 /* Set die for all demux */
383 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
384 for( i = 0; i < p_list->i_count; i++ )
386 p_list->p_values[i].p_object->b_die = VLC_TRUE;
388 vlc_list_release( p_list );
390 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
394 * Clean up a dead input thread
395 * This function does not return until the thread is effectively cancelled.
397 * \param the input thread to kill
399 void input_DestroyThread( input_thread_t *p_input )
401 /* Join the thread */
402 vlc_thread_join( p_input );
404 /* Delete input lock (only after thread joined) */
405 vlc_mutex_destroy( &p_input->lock_control );
407 /* TODO: maybe input_DestroyThread should also delete p_input instead
408 * of the playlist but I'm not sure if it's possible */
411 /*****************************************************************************
412 * Run: main thread loop
413 * This is the "normal" thread that spawns the input processing chain,
414 * reads the stream, cleans up and waits
415 *****************************************************************************/
416 static int Run( input_thread_t *p_input )
418 /* Signal that the thread is launched */
419 vlc_thread_ready( p_input );
421 if( Init( p_input, VLC_FALSE ) )
423 /* If we failed, wait before we are killed, and exit */
424 p_input->b_error = VLC_TRUE;
428 /* Tell we're dead */
429 p_input->b_dead = VLC_TRUE;
436 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
438 /* We have finish to demux data but not to play them */
439 while( !p_input->b_die )
441 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
444 msg_Dbg( p_input, "waiting decoder fifos to empty" );
446 msleep( INPUT_IDLE_SLEEP );
449 /* We have finished */
450 p_input->b_eof = VLC_TRUE;
453 /* Wait we are asked to die */
454 if( !p_input->b_die )
465 /*****************************************************************************
466 * RunAndClean: main thread loop
467 * This is the "just forget me" thread that spawns the input processing chain,
468 * reads the stream, cleans up and releases memory
469 *****************************************************************************/
470 static int RunAndClean( input_thread_t *p_input )
472 /* Signal that the thread is launched */
473 vlc_thread_ready( p_input );
475 if( Init( p_input, VLC_FALSE ) )
477 /* If we failed, just exit */
483 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
485 /* We have finish to demux data but not to play them */
486 while( !p_input->b_die )
488 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
491 msg_Dbg( p_input, "waiting decoder fifos to empty" );
493 msleep( INPUT_IDLE_SLEEP );
495 /* We have finished */
496 p_input->b_eof = VLC_TRUE;
503 vlc_object_detach( p_input );
504 vlc_object_destroy( p_input );
510 /*****************************************************************************
511 * Main loop: Fill buffers from access, and demux
512 *****************************************************************************/
513 static void MainLoop( input_thread_t *p_input )
515 int64_t i_intf_update = 0;
516 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
518 vlc_bool_t b_force_update = VLC_FALSE;
524 if( p_input->i_state != PAUSE_S )
526 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
527 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
534 if( p_input->input.b_title_demux &&
535 p_input->input.p_demux->info.i_update )
537 i_ret = UpdateFromDemux( p_input );
538 b_force_update = VLC_TRUE;
540 else if( !p_input->input.b_title_demux &&
541 p_input->input.p_access &&
542 p_input->input.p_access->info.i_update )
544 i_ret = UpdateFromAccess( p_input );
545 b_force_update = VLC_TRUE;
549 if( i_ret == 0 ) /* EOF */
553 var_Get( p_input, "input-repeat", &repeat );
554 if( repeat.i_int == 0 )
556 /* End of file - we do not set b_die because only the
557 * playlist is allowed to do so. */
558 msg_Dbg( p_input, "EOF reached" );
559 p_input->input.b_eof = VLC_TRUE;
563 msg_Dbg( p_input, "repeating the same input (%d)",
565 if( repeat.i_int > 0 )
568 var_Set( p_input, "input-repeat", repeat );
571 /* Seek to start title/seekpoint */
572 val.i_int = p_input->input.i_title_start -
573 p_input->input.i_title_offset;
574 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
576 input_ControlPush( p_input,
577 INPUT_CONTROL_SET_TITLE, &val );
579 val.i_int = p_input->input.i_seekpoint_start -
580 p_input->input.i_seekpoint_offset;
581 if( val.i_int > 0 /* TODO: check upper boundary */ )
582 input_ControlPush( p_input,
583 INPUT_CONTROL_SET_SEEKPOINT, &val );
585 /* Seek to start position */
586 if( p_input->i_start > 0 )
588 val.i_time = p_input->i_start;
589 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
595 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
602 p_input->b_error = VLC_TRUE;
605 if( i_ret > 0 && p_input->i_slave > 0 )
607 SlaveDemux( p_input );
617 vlc_mutex_lock( &p_input->lock_control );
618 ControlReduce( p_input );
619 while( !ControlPopNoLock( p_input, &i_type, &val ) )
621 msg_Dbg( p_input, "control type=%d", i_type );
622 if( Control( p_input, i_type, val ) )
623 b_force_update = VLC_TRUE;
625 vlc_mutex_unlock( &p_input->lock_control );
627 if( b_force_update || i_intf_update < mdate() )
631 int64_t i_time, i_length;
632 /* update input status variables */
633 if( !demux2_Control( p_input->input.p_demux,
634 DEMUX_GET_POSITION, &f_pos ) )
636 val.f_float = (float)f_pos;
637 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
639 if( !demux2_Control( p_input->input.p_demux,
640 DEMUX_GET_TIME, &i_time ) )
642 p_input->i_time = i_time;
644 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
646 if( !demux2_Control( p_input->input.p_demux,
647 DEMUX_GET_LENGTH, &i_length ) )
650 var_Get( p_input, "length", &old_val );
651 val.i_time = i_length;
652 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
654 if( old_val.i_time != val.i_time )
656 UpdateItemLength( p_input, i_length, VLC_TRUE );
660 var_SetBool( p_input, "intf-change", VLC_TRUE );
661 i_intf_update = mdate() + I64C(150000);
667 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
673 vlc_meta_t *p_meta, *p_meta_tmp;
677 /* Initialize optional stream output. (before access/demuxer)
678 * XXX: we add a special case if the uri starts by vlc.
679 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
680 * be destroyed in case of a file).
681 * (this will break playing of file starting by 'vlc:' but I don't
682 * want to add more logic, just force file by file:// or code it ;)
686 counter_t *p_counter;
687 stats_Create( p_input, "read_bytes", VLC_VAR_INTEGER, STATS_COUNTER );
688 stats_Create( p_input, "read_packets", VLC_VAR_INTEGER, STATS_COUNTER );
689 stats_Create( p_input, "demux_read", VLC_VAR_INTEGER, STATS_COUNTER );
690 stats_Create( p_input, "input_bitrate", VLC_VAR_FLOAT,
692 stats_Create( p_input, "demux_bitrate", VLC_VAR_FLOAT,
694 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
696 if( p_counter ) p_counter->update_interval = 1000000;
697 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
699 if( p_counter ) p_counter->update_interval = 1000000;
701 psz = var_GetString( p_input, "sout" );
702 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
704 p_input->p_sout = sout_NewInstance( p_input, psz );
705 if( p_input->p_sout == NULL )
707 msg_Err( p_input, "cannot start stream output instance, " \
717 p_input->p_es_out = input_EsOutNew( p_input );
718 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
719 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
721 if( InputSourceInit( p_input, &p_input->input,
722 p_input->input.p_item->psz_uri, NULL, b_quick ) )
727 /* Create global title (from master) */
730 p_input->i_title = p_input->input.i_title;
731 p_input->title = p_input->input.title;
732 p_input->i_title_offset = p_input->input.i_title_offset;
733 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
734 if( p_input->i_title > 0 )
736 /* Setup variables */
737 input_ControlVarNavigation( p_input );
738 input_ControlVarTitle( p_input, 0 );
742 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
743 p_input->b_can_pause = p_input->input.b_can_pause;
746 if( p_input->i_pts_delay < 0 )
747 p_input->i_pts_delay = 0;
749 /* If the desynchronisation requested by the user is < 0, we need to
750 * cache more data. */
751 var_Get( p_input, "audio-desync", &val );
752 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
754 /* Update cr_average depending on the caching */
755 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
756 p_input->input.i_cr_average /= 10;
757 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
760 /* Load master infos */
762 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
763 &val.i_time ) && val.i_time > 0 )
765 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
766 UpdateItemLength( p_input, val.i_time, b_quick );
767 p_input->input.p_item->i_duration = val.i_time;
770 /* Start title/chapter */
773 val.i_int = p_input->input.i_title_start -
774 p_input->input.i_title_offset;
775 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
776 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
777 val.i_int = p_input->input.i_seekpoint_start -
778 p_input->input.i_seekpoint_offset;
779 if( val.i_int > 0 /* TODO: check upper boundary */ )
780 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
784 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
786 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
789 if( p_input->i_start > 0 )
791 if( p_input->i_start >= val.i_time )
793 msg_Warn( p_input, "invalid start-time ignored" );
799 msg_Dbg( p_input, "starting at time: %ds",
800 (int)( p_input->i_start / I64C(1000000) ) );
802 s.i_time = p_input->i_start;
803 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
806 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
808 msg_Warn( p_input, "invalid stop-time ignored" );
814 /* Get fps and set it if not already set */
815 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
818 float f_requested_fps;
820 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
821 var_SetFloat( p_input, "sub-original-fps", f_fps );
823 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
824 if( f_requested_fps != f_fps )
826 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
828 var_SetFloat( p_input, "sub-fps", f_requested_fps );
832 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
835 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
839 /* Look for and add subtitle files */
840 psz_subtitle = var_GetString( p_input, "sub-file" );
847 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
849 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
852 sub = InputSourceNew( p_input );
853 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
856 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
859 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
862 if( count.i_int == 0 )
864 /* if it was first one, there is disable too */
866 if( count.i_int < list.p_list->i_count )
868 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
869 &list.p_list->p_values[count.i_int] );
871 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
877 var_Get( p_input, "sub-autodetect-file", &val );
880 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
881 char **subs = subtitles_Detect( p_input, psz_autopath,
882 p_input->input.p_item->psz_uri );
885 for( i = 0; subs && subs[i]; i++ )
887 if( strcmp( psz_subtitle, subs[i] ) )
889 sub = InputSourceNew( p_input );
890 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
893 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
898 if( subs ) free( subs );
899 if( psz_autopath ) free( psz_autopath );
901 free( psz_subtitle );
904 psz = var_GetString( p_input, "input-slave" );
908 input_source_t *slave;
911 while( *psz == ' ' || *psz == '#' )
915 if( ( psz_delim = strchr( psz, '#' ) ) )
924 msg_Dbg( p_input, "adding slave input '%s'", psz );
925 slave = InputSourceNew( p_input );
926 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
928 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
933 if( psz ) free( psz );
937 p_input->i_start = 0;
938 p_input->i_start = 0;
944 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
945 i_es_out_mode = ES_OUT_MODE_AUTO;
947 if( p_input->p_sout )
949 var_Get( p_input, "sout-all", &val );
952 i_es_out_mode = ES_OUT_MODE_ALL;
957 var_Get( p_input, "programs", &val );
958 if ( val.p_list && val.p_list->i_count )
960 i_es_out_mode = ES_OUT_MODE_PARTIAL;
961 /* Note : we should remove the "program" callback. */
964 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
968 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
970 /* Inform the demuxer about waited group (needed only for DVB) */
971 if( i_es_out_mode == ES_OUT_MODE_ALL )
973 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
975 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
977 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
982 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
983 (int) var_GetInteger( p_input, "program" ), NULL );
986 if( p_input->p_sout )
988 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
990 p_input->b_out_pace_control = VLC_FALSE;
994 p_input->b_out_pace_control = VLC_TRUE;
997 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
999 /* We don't want a high input priority here or we'll
1000 * end-up sucking up all the CPU time */
1001 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1004 msg_Dbg( p_input, "starting in %s mode",
1005 p_input->b_out_pace_control ? "asynch" : "synch" );
1009 /* Get meta data from users */
1010 p_meta_tmp = InputMetaUser( p_input );
1012 /* Get meta data from master input */
1013 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
1017 if( p_meta == NULL )
1019 p_meta = p_meta_tmp;
1021 else if( p_meta_tmp )
1023 vlc_meta_Merge( p_meta, p_meta_tmp );
1024 vlc_meta_Delete( p_meta_tmp );
1027 /* Access_file does not give any meta, and there are no slave */
1030 if( !p_input->input.p_access ||
1031 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1035 if( p_meta == NULL )
1037 p_meta = p_meta_tmp;
1039 else if( p_meta_tmp )
1041 vlc_meta_Merge( p_meta, p_meta_tmp );
1042 vlc_meta_Delete( p_meta_tmp );
1045 /* Get meta data from slave input */
1046 for( i = 0; i < p_input->i_slave; i++ )
1048 vlc_meta_t *p_meta_slave;
1050 if( !demux2_Control( p_input->slave[i]->p_demux,
1051 DEMUX_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 );
1064 if( p_input->slave[i]->p_access &&
1065 !access2_Control( p_input->slave[i]->p_access,
1066 ACCESS_GET_META, &p_meta_slave ) )
1068 if( p_meta == NULL )
1070 p_meta = p_meta_slave;
1072 else if( p_meta_slave )
1074 vlc_meta_Merge( p_meta, p_meta_slave );
1075 vlc_meta_Delete( p_meta_slave );
1081 p_input->p_meta = p_meta;
1082 UpdateMeta( p_input, b_quick );
1086 msg_Dbg( p_input, "`%s' successfully opened",
1087 p_input->input.p_item->psz_uri );
1091 /* initialization is complete */
1092 p_input->i_state = PLAYING_S;
1094 val.i_int = PLAYING_S;
1095 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1100 if( p_input->p_es_out )
1101 input_EsOutDelete( p_input->p_es_out );
1103 if( p_input->p_sout )
1104 sout_DeleteInstance( p_input->p_sout );
1106 /* Mark them deleted */
1107 p_input->input.p_demux = NULL;
1108 p_input->input.p_stream = NULL;
1109 p_input->input.p_access = NULL;
1110 p_input->p_es_out = NULL;
1111 p_input->p_sout = NULL;
1113 return VLC_EGENERIC;
1116 /*****************************************************************************
1117 * Error: RunThread() error loop
1118 *****************************************************************************
1119 * This function is called when an error occurred during thread main's loop.
1120 *****************************************************************************/
1121 static void Error( input_thread_t *p_input )
1123 while( !p_input->b_die )
1126 msleep( INPUT_IDLE_SLEEP );
1130 /*****************************************************************************
1131 * End: end the input thread
1132 *****************************************************************************/
1133 static void End( input_thread_t * p_input )
1138 msg_Dbg( p_input, "closing input" );
1140 /* We are at the end */
1141 p_input->i_state = END_S;
1144 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1146 /* Clean control variables */
1147 input_ControlVarClean( p_input );
1149 /* Clean up master */
1150 InputSourceClean( p_input, &p_input->input );
1153 for( i = 0; i < p_input->i_slave; i++ )
1155 InputSourceClean( p_input, p_input->slave[i] );
1156 free( p_input->slave[i] );
1158 if( p_input->slave ) free( p_input->slave );
1160 /* Unload all modules */
1161 if( p_input->p_es_out )
1162 input_EsOutDelete( p_input->p_es_out );
1164 /* Close optional stream output instance */
1165 if( p_input->p_sout )
1167 vlc_object_t *p_pl =
1168 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1171 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1173 /* attach sout to the playlist */
1174 msg_Dbg( p_input, "keeping sout" );
1175 vlc_object_detach( p_input->p_sout );
1176 vlc_object_attach( p_input->p_sout, p_pl );
1180 msg_Dbg( p_input, "destroying sout" );
1181 sout_DeleteInstance( p_input->p_sout );
1184 vlc_object_release( p_pl );
1188 if( p_input->p_meta )
1189 vlc_meta_Delete( p_input->p_meta );
1191 /* Tell we're dead */
1192 p_input->b_dead = VLC_TRUE;
1195 /*****************************************************************************
1197 *****************************************************************************/
1198 static inline int ControlPopNoLock( input_thread_t *p_input,
1199 int *pi_type, vlc_value_t *p_val )
1201 if( p_input->i_control <= 0 )
1203 return VLC_EGENERIC;
1206 *pi_type = p_input->control[0].i_type;
1207 *p_val = p_input->control[0].val;
1209 p_input->i_control--;
1210 if( p_input->i_control > 0 )
1214 for( i = 0; i < p_input->i_control; i++ )
1216 p_input->control[i].i_type = p_input->control[i+1].i_type;
1217 p_input->control[i].val = p_input->control[i+1].val;
1224 static void ControlReduce( input_thread_t *p_input )
1227 for( i = 1; i < p_input->i_control; i++ )
1229 const int i_lt = p_input->control[i-1].i_type;
1230 const int i_ct = p_input->control[i].i_type;
1232 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1233 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1237 ( i_ct == INPUT_CONTROL_SET_STATE ||
1238 i_ct == INPUT_CONTROL_SET_RATE ||
1239 i_ct == INPUT_CONTROL_SET_POSITION ||
1240 i_ct == INPUT_CONTROL_SET_TIME ||
1241 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1242 i_ct == INPUT_CONTROL_SET_TITLE ||
1243 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1244 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1247 // msg_Dbg( p_input, "merged at %d", i );
1248 /* Remove the i-1 */
1249 for( j = i; j < p_input->i_control; j++ )
1250 p_input->control[j-1] = p_input->control[j];
1251 p_input->i_control--;
1255 /* TODO but that's not that important
1256 - merge SET_X with SET_X_CMD
1257 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1258 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1265 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1268 vlc_bool_t b_force_update = VLC_FALSE;
1270 if( !p_input ) return b_force_update;
1274 case INPUT_CONTROL_SET_DIE:
1275 msg_Dbg( p_input, "control: stopping input" );
1276 /* Mark all submodules to die */
1277 if( p_input->input.p_access )
1278 p_input->input.p_access->b_die = VLC_TRUE;
1279 if( p_input->input.p_stream )
1280 p_input->input.p_stream->b_die = VLC_TRUE;
1281 p_input->input.p_demux->b_die = VLC_TRUE;
1283 p_input->b_die = VLC_TRUE;
1286 case INPUT_CONTROL_SET_POSITION:
1287 case INPUT_CONTROL_SET_POSITION_OFFSET:
1290 if( i_type == INPUT_CONTROL_SET_POSITION )
1292 f_pos = val.f_float;
1296 /* Should not fail */
1297 demux2_Control( p_input->input.p_demux,
1298 DEMUX_GET_POSITION, &f_pos );
1299 f_pos += val.f_float;
1301 if( f_pos < 0.0 ) f_pos = 0.0;
1302 if( f_pos > 1.0 ) f_pos = 1.0;
1303 /* Reset the decoders states and clock synch (before calling the demuxer */
1304 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1305 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1306 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1309 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1310 "%2.1f%% failed", f_pos * 100 );
1314 if( p_input->i_slave > 0 )
1315 SlaveSeek( p_input );
1317 b_force_update = VLC_TRUE;
1322 case INPUT_CONTROL_SET_TIME:
1323 case INPUT_CONTROL_SET_TIME_OFFSET:
1328 if( i_type == INPUT_CONTROL_SET_TIME )
1330 i_time = val.i_time;
1334 /* Should not fail */
1335 demux2_Control( p_input->input.p_demux,
1336 DEMUX_GET_TIME, &i_time );
1337 i_time += val.i_time;
1339 if( i_time < 0 ) i_time = 0;
1341 /* Reset the decoders states and clock synch (before calling the demuxer */
1342 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1343 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1345 i_ret = demux2_Control( p_input->input.p_demux,
1346 DEMUX_SET_TIME, i_time );
1351 /* Emulate it with a SET_POS */
1352 demux2_Control( p_input->input.p_demux,
1353 DEMUX_GET_LENGTH, &i_length );
1356 double f_pos = (double)i_time / (double)i_length;
1357 i_ret = demux2_Control( p_input->input.p_demux,
1358 DEMUX_SET_POSITION, f_pos );
1363 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1364 " failed or not possible", i_time );
1368 if( p_input->i_slave > 0 )
1369 SlaveSeek( p_input );
1371 b_force_update = VLC_TRUE;
1376 case INPUT_CONTROL_SET_STATE:
1377 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1378 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1381 if( p_input->input.p_access )
1382 i_ret = access2_Control( p_input->input.p_access,
1383 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1385 i_ret = demux2_Control( p_input->input.p_demux,
1386 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1390 /* FIXME What to do ? */
1391 msg_Warn( p_input, "cannot unset pause -> EOF" );
1392 vlc_mutex_unlock( &p_input->lock_control );
1393 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1394 vlc_mutex_lock( &p_input->lock_control );
1397 b_force_update = VLC_TRUE;
1399 /* Switch to play */
1400 p_input->i_state = PLAYING_S;
1401 val.i_int = PLAYING_S;
1402 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1405 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1406 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1408 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1409 p_input->b_can_pause )
1412 if( p_input->input.p_access )
1413 i_ret = access2_Control( p_input->input.p_access,
1414 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1416 i_ret = demux2_Control( p_input->input.p_demux,
1417 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1419 b_force_update = VLC_TRUE;
1423 msg_Warn( p_input, "cannot set pause state" );
1424 val.i_int = p_input->i_state;
1428 val.i_int = PAUSE_S;
1431 /* Switch to new state */
1432 p_input->i_state = val.i_int;
1433 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1435 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1437 b_force_update = VLC_TRUE;
1439 /* Correct "state" value */
1440 val.i_int = p_input->i_state;
1441 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1443 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1445 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1449 case INPUT_CONTROL_SET_RATE:
1450 case INPUT_CONTROL_SET_RATE_SLOWER:
1451 case INPUT_CONTROL_SET_RATE_FASTER:
1455 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1456 i_rate = p_input->i_rate * 2;
1457 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1458 i_rate = p_input->i_rate / 2;
1462 if( i_rate < INPUT_RATE_MIN )
1464 msg_Dbg( p_input, "cannot set rate faster" );
1465 i_rate = INPUT_RATE_MIN;
1467 else if( i_rate > INPUT_RATE_MAX )
1469 msg_Dbg( p_input, "cannot set rate slower" );
1470 i_rate = INPUT_RATE_MAX;
1472 if( i_rate != INPUT_RATE_DEFAULT &&
1473 ( !p_input->b_can_pace_control ||
1474 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1476 msg_Dbg( p_input, "cannot change rate" );
1477 i_rate = INPUT_RATE_DEFAULT;
1479 if( i_rate != p_input->i_rate )
1481 p_input->i_rate = i_rate;
1483 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1485 /* We haven't send data to decoder when rate != default */
1486 if( i_rate == INPUT_RATE_DEFAULT )
1487 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1490 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1492 b_force_update = VLC_TRUE;
1497 case INPUT_CONTROL_SET_PROGRAM:
1498 /* No need to force update, es_out does it if needed */
1499 es_out_Control( p_input->p_es_out,
1500 ES_OUT_SET_GROUP, val.i_int );
1502 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1506 case INPUT_CONTROL_SET_ES:
1507 /* No need to force update, es_out does it if needed */
1508 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1509 input_EsOutGetFromID( p_input->p_es_out,
1513 case INPUT_CONTROL_SET_AUDIO_DELAY:
1514 input_EsOutSetDelay( p_input->p_es_out,
1515 AUDIO_ES, val.i_time );
1516 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1519 case INPUT_CONTROL_SET_SPU_DELAY:
1520 input_EsOutSetDelay( p_input->p_es_out,
1521 SPU_ES, val.i_time );
1522 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1525 case INPUT_CONTROL_SET_TITLE:
1526 case INPUT_CONTROL_SET_TITLE_NEXT:
1527 case INPUT_CONTROL_SET_TITLE_PREV:
1528 if( p_input->input.b_title_demux &&
1529 p_input->input.i_title > 0 )
1532 /* FIXME handle demux title */
1533 demux_t *p_demux = p_input->input.p_demux;
1536 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1537 i_title = p_demux->info.i_title - 1;
1538 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1539 i_title = p_demux->info.i_title + 1;
1541 i_title = val.i_int;
1543 if( i_title >= 0 && i_title < p_input->input.i_title )
1545 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1546 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1548 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1549 input_ControlVarTitle( p_input, i_title );
1552 else if( p_input->input.i_title > 0 )
1554 access_t *p_access = p_input->input.p_access;
1557 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1558 i_title = p_access->info.i_title - 1;
1559 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1560 i_title = p_access->info.i_title + 1;
1562 i_title = val.i_int;
1564 if( i_title >= 0 && i_title < p_input->input.i_title )
1566 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1567 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1569 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1570 stream_AccessReset( p_input->input.p_stream );
1574 case INPUT_CONTROL_SET_SEEKPOINT:
1575 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1576 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1577 if( p_input->input.b_title_demux &&
1578 p_input->input.i_title > 0 )
1580 demux_t *p_demux = p_input->input.p_demux;
1582 int64_t i_input_time;
1583 int64_t i_seekpoint_time;
1585 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1587 i_seekpoint = p_demux->info.i_seekpoint;
1588 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1589 if( i_seekpoint_time >= 0 &&
1590 !demux2_Control( p_demux,
1591 DEMUX_GET_TIME, &i_input_time ) )
1593 if ( i_input_time < i_seekpoint_time + 3000000 )
1599 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1600 i_seekpoint = p_demux->info.i_seekpoint + 1;
1602 i_seekpoint = val.i_int;
1604 if( i_seekpoint >= 0 && i_seekpoint <
1605 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1607 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1608 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1610 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1613 else if( p_input->input.i_title > 0 )
1615 demux_t *p_demux = p_input->input.p_demux;
1616 access_t *p_access = p_input->input.p_access;
1618 int64_t i_input_time;
1619 int64_t i_seekpoint_time;
1621 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1623 i_seekpoint = p_access->info.i_seekpoint;
1624 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1625 if( i_seekpoint_time >= 0 &&
1626 demux2_Control( p_demux,
1627 DEMUX_GET_TIME, &i_input_time ) )
1629 if ( i_input_time < i_seekpoint_time + 3000000 )
1635 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1636 i_seekpoint = p_access->info.i_seekpoint + 1;
1638 i_seekpoint = val.i_int;
1640 if( i_seekpoint >= 0 && i_seekpoint <
1641 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1643 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1644 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1646 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1647 stream_AccessReset( p_input->input.p_stream );
1652 case INPUT_CONTROL_ADD_SLAVE:
1653 if( val.psz_string )
1655 input_source_t *slave = InputSourceNew( p_input );
1657 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1660 vlc_meta_t *p_meta_new = NULL;
1665 msg_Dbg( p_input, "adding %s as slave on the fly",
1669 if( demux2_Control( p_input->input.p_demux,
1670 DEMUX_GET_TIME, &i_time ) )
1672 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1673 InputSourceClean( p_input, slave );
1677 if( demux2_Control( slave->p_demux,
1678 DEMUX_SET_TIME, i_time ) )
1680 msg_Err( p_input, "seek failed for new slave" );
1681 InputSourceClean( p_input, slave );
1687 /* Get meta (access and demux) */
1688 if( access2_Control( slave->p_access,
1689 ACCESS_GET_META, &p_meta_new ) )
1691 if( !demux2_Control( slave->p_demux,
1692 DEMUX_GET_META, &p_meta ) )
1696 vlc_meta_Merge( p_meta_new, p_meta );
1697 vlc_meta_Delete( p_meta );
1701 p_meta_new = p_meta;
1707 if( p_input->p_meta )
1709 vlc_meta_Merge( p_input->p_meta, p_meta_new );
1710 vlc_meta_Delete( p_meta_new );
1714 p_input->p_meta = p_meta_new;
1716 UpdateMeta( p_input, VLC_FALSE );
1719 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1723 msg_Warn( p_input, "failed to add %s as slave",
1727 free( val.psz_string );
1731 case INPUT_CONTROL_SET_BOOKMARK:
1733 msg_Err( p_input, "not yet implemented" );
1737 return b_force_update;
1740 /*****************************************************************************
1742 *****************************************************************************/
1743 static int UpdateFromDemux( input_thread_t *p_input )
1745 demux_t *p_demux = p_input->input.p_demux;
1748 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1750 v.i_int = p_demux->info.i_title;
1751 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1753 input_ControlVarTitle( p_input, p_demux->info.i_title );
1755 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1757 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1759 v.i_int = p_demux->info.i_seekpoint;
1760 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1762 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1764 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1766 /* Hmmm only works with master input */
1767 if( p_input->input.p_demux == p_demux )
1769 int i_title_end = p_input->input.i_title_end -
1770 p_input->input.i_title_offset;
1771 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1772 p_input->input.i_seekpoint_offset;
1774 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1776 if( p_demux->info.i_title > i_title_end ||
1777 ( p_demux->info.i_title == i_title_end &&
1778 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1780 else if( i_seekpoint_end >=0 )
1782 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1784 else if( i_title_end >= 0 )
1786 if( p_demux->info.i_title > i_title_end ) return 0;
1793 /*****************************************************************************
1795 *****************************************************************************/
1796 static int UpdateFromAccess( input_thread_t *p_input )
1798 access_t *p_access = p_input->input.p_access;
1801 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1803 v.i_int = p_access->info.i_title;
1804 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1806 input_ControlVarTitle( p_input, p_access->info.i_title );
1808 stream_AccessUpdate( p_input->input.p_stream );
1810 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1812 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1814 v.i_int = p_access->info.i_seekpoint;
1815 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1817 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1819 if( p_access->info.i_update & INPUT_UPDATE_META )
1821 /* TODO maybe multi - access ? */
1823 if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1825 if( p_input->p_meta )
1827 vlc_meta_Merge( p_input->p_meta, p_meta );
1828 vlc_meta_Delete( p_meta );
1832 p_input->p_meta = p_meta;
1835 UpdateMeta( p_input, VLC_FALSE );
1836 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1838 p_access->info.i_update &= ~INPUT_UPDATE_META;
1841 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1843 /* Hmmm only works with master input */
1844 if( p_input->input.p_access == p_access )
1846 int i_title_end = p_input->input.i_title_end -
1847 p_input->input.i_title_offset;
1848 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1849 p_input->input.i_seekpoint_offset;
1851 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1853 if( p_access->info.i_title > i_title_end ||
1854 ( p_access->info.i_title == i_title_end &&
1855 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1857 else if( i_seekpoint_end >=0 )
1859 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1861 else if( i_title_end >= 0 )
1863 if( p_access->info.i_title > i_title_end ) return 0;
1870 /*****************************************************************************
1872 *****************************************************************************/
1873 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1875 vlc_meta_t *p_meta = p_input->p_meta;
1878 if( !p_meta || p_meta->i_meta == 0 )
1881 if( !b_quick ) msg_Dbg( p_input, "meta information:" );
1882 for( i = 0; i < p_meta->i_meta; i++ )
1885 msg_Dbg( p_input, " - '%s' = '%s'",
1886 _(p_meta->name[i]), p_meta->value[i] );
1888 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] &&
1889 !p_input->input.p_item->b_fixed_name )
1890 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1892 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1893 input_Control( p_input, INPUT_ADD_INFO, _("General"),
1894 _("Author"), p_meta->value[i] );
1896 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1897 _(p_meta->name[i]), "%s", p_meta->value[i] );
1900 for( i = 0; i < p_meta->i_track; i++ )
1902 vlc_meta_t *tk = p_meta->track[i];
1905 if( tk->i_meta > 0 )
1907 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1909 msg_Dbg( p_input, " - track[%d]:", i );
1911 sprintf( psz_cat, "%s %d", _("Stream"), i );
1912 for( j = 0; j < tk->i_meta; j++ )
1914 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1917 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1918 _(tk->name[j]), "%s", tk->value[j] );
1923 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1925 p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1931 /*****************************************************************************
1933 *****************************************************************************/
1934 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1935 vlc_bool_t b_quick )
1937 playlist_t *p_playlist;
1938 char psz_buffer[MSTRTIME_MAX_SIZE];
1940 vlc_mutex_lock( &p_input->input.p_item->lock );
1941 p_input->input.p_item->i_duration = i_length;
1942 vlc_mutex_unlock( &p_input->input.p_item->lock );
1944 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1948 var_SetInteger( p_playlist, "item-change",
1949 p_input->input.p_item->i_id );
1950 vlc_object_release( p_playlist );
1953 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1954 msecstotimestr( psz_buffer, i_length / 1000 ) );
1957 /*****************************************************************************
1959 *****************************************************************************/
1960 static input_source_t *InputSourceNew( input_thread_t *p_input )
1962 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1966 msg_Err( p_input, "out of memory for new input source" );
1971 in->p_access = NULL;
1972 in->p_stream = NULL;
1974 in->b_title_demux = VLC_FALSE;
1977 in->b_can_pace_control = VLC_TRUE;
1978 in->b_eof = VLC_FALSE;
1979 in->i_cr_average = 0;
1984 /*****************************************************************************
1986 *****************************************************************************/
1987 static int InputSourceInit( input_thread_t *p_input,
1988 input_source_t *in, char *psz_mrl,
1989 char *psz_forced_demux, vlc_bool_t b_quick )
1991 char *psz_dup = strdup( psz_mrl );
1999 if( !in ) return VLC_EGENERIC;
2004 MRLSplit( VLC_OBJECT(p_input), psz_dup,
2005 &psz_access, &psz_demux, &psz_path );
2007 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2008 psz_mrl, psz_access, psz_demux, psz_path );
2010 /* Hack to allow udp://@:port syntax */
2012 (strncmp( psz_access, "udp", 3 ) &&
2013 strncmp( psz_access, "rtp", 3 )) )
2015 /* Find optional titles and seekpoints */
2016 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2017 &in->i_seekpoint_start, &in->i_seekpoint_end );
2019 if( psz_forced_demux && *psz_forced_demux )
2021 psz_demux = psz_forced_demux;
2023 else if( !psz_demux || *psz_demux == '\0' )
2025 /* special hack for forcing a demuxer with --demux=module
2026 * (and do nothing with a list) */
2027 char *psz_var_demux = var_GetString( p_input, "demux" );
2029 if( *psz_var_demux != '\0' &&
2030 !strchr(psz_var_demux, ',' ) &&
2031 !strchr(psz_var_demux, ':' ) )
2033 psz_demux = psz_var_demux;
2035 msg_Dbg( p_input, "Enforce demux ` %s'", psz_demux );
2037 else if( psz_var_demux )
2039 free( psz_var_demux );
2043 /* Try access_demux if no demux given */
2044 if( *psz_demux == '\0' )
2046 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2047 NULL, p_input->p_es_out, VLC_FALSE );
2053 msg_Dbg( p_input, "trying to preparse %s", psz_path );
2054 psz_demux = strdup( "" );
2055 psz_access = strdup( "file" );
2060 int64_t i_pts_delay;
2062 /* Get infos from access_demux */
2063 demux2_Control( in->p_demux,
2064 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2065 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2067 in->b_title_demux = VLC_TRUE;
2068 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2069 &in->title, &in->i_title,
2070 &in->i_title_offset, &in->i_seekpoint_offset ) )
2075 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2076 &in->b_can_pace_control );
2077 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2081 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2087 int64_t i_pts_delay;
2089 /* Now try a real access */
2090 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2093 /* Access failed, URL encoded ? */
2094 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2096 DecodeUrl( psz_path );
2098 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2099 psz_access, psz_demux, psz_path );
2101 in->p_access = access2_New( p_input,
2102 psz_access, psz_demux, psz_path,
2105 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2106 * are forbidden in filenames on Win32. */
2108 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2109 if( in->p_access == NULL &&
2110 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2112 if( psz_dup ) free( psz_dup );
2113 psz_dup = strdup( psz_mrl );
2118 in->p_access = access2_New( p_input,
2119 psz_access, psz_demux, psz_path,
2124 if( in->p_access == NULL )
2126 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2127 intf_UserFatal( VLC_OBJECT( p_input),
2128 _("Errors"),"Unable to open '%s'", psz_mrl );
2133 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2134 while( psz && *psz )
2136 access_t *p_access = in->p_access;
2137 char *end = strchr( psz, ':' );
2142 in->p_access = access2_FilterNew( in->p_access, psz );
2143 if( in->p_access == NULL )
2145 in->p_access = p_access;
2146 msg_Warn( p_input, "failed to insert access filter %s",
2152 if( psz_tmp ) free( psz_tmp );
2154 /* Get infos from access */
2157 access2_Control( in->p_access,
2158 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2159 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2161 in->b_title_demux = VLC_FALSE;
2162 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2163 &in->title, &in->i_title,
2164 &in->i_title_offset, &in->i_seekpoint_offset ) )
2170 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2171 &in->b_can_pace_control );
2172 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2174 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2176 var_Set( p_input, "seekable", val );
2179 /* Create the stream_t */
2180 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2181 if( in->p_stream == NULL )
2183 msg_Warn( p_input, "cannot create a stream_t from access" );
2187 /* Open a demuxer */
2188 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2190 psz_demux = in->p_access->psz_demux;
2192 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2193 in->p_stream, p_input->p_es_out, b_quick );
2194 if( in->p_demux == NULL )
2196 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2197 psz_access, psz_demux, psz_path );
2198 intf_UserFatal( VLC_OBJECT( p_input), _("Errors"),
2199 "Unrecognized format for '%s'", psz_mrl );
2203 /* TODO get title from demux */
2204 if( !b_quick && in->i_title <= 0 )
2206 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2207 &in->title, &in->i_title,
2208 &in->i_title_offset, &in->i_seekpoint_offset ))
2215 in->b_title_demux = VLC_TRUE;
2220 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2221 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2223 if( psz_dup ) free( psz_dup );
2228 demux2_Delete( in->p_demux );
2231 stream_Delete( in->p_stream );
2234 access2_Delete( in->p_access );
2235 if( psz_dup ) free( psz_dup );
2237 return VLC_EGENERIC;
2240 /*****************************************************************************
2242 *****************************************************************************/
2243 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2246 demux2_Delete( in->p_demux );
2249 stream_Delete( in->p_stream );
2252 access2_Delete( in->p_access );
2254 if( in->i_title > 0 )
2257 for( i = 0; i < in->i_title; i++ )
2259 vlc_input_title_Delete( in->title[i] );
2265 static void SlaveDemux( input_thread_t *p_input )
2270 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2272 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2276 for( i = 0; i < p_input->i_slave; i++ )
2278 input_source_t *in = p_input->slave[i];
2284 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2289 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2291 msg_Err( p_input, "slave[%d] doesn't like "
2292 "DEMUX_GET_TIME -> EOF", i );
2297 if( i_stime >= i_time )
2300 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2306 i_ret = in->p_demux->pf_demux( in->p_demux );
2311 msg_Dbg( p_input, "slave %d EOF", i );
2312 in->b_eof = VLC_TRUE;
2317 static void SlaveSeek( input_thread_t *p_input )
2322 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2324 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2328 for( i = 0; i < p_input->i_slave; i++ )
2330 input_source_t *in = p_input->slave[i];
2332 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2334 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2335 in->b_eof = VLC_TRUE;
2339 /*****************************************************************************
2341 *****************************************************************************/
2342 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2347 if( ( p_meta = vlc_meta_New() ) == NULL )
2350 /* Get meta information from user */
2351 #define GET_META( c, s ) \
2352 var_Get( p_input, (s), &val ); \
2353 if( *val.psz_string ) \
2354 vlc_meta_Add( p_meta, c, val.psz_string ); \
2355 free( val.psz_string )
2357 GET_META( VLC_META_TITLE, "meta-title" );
2358 GET_META( VLC_META_AUTHOR, "meta-author" );
2359 GET_META( VLC_META_ARTIST, "meta-artist" );
2360 GET_META( VLC_META_GENRE, "meta-genre" );
2361 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2362 GET_META( VLC_META_DESCRIPTION, "meta-description" );
2363 GET_META( VLC_META_DATE, "meta-date" );
2364 GET_META( VLC_META_URL, "meta-url" );
2370 /*****************************************************************************
2371 * DecodeUrl: decode a given encoded url
2372 *****************************************************************************/
2373 static void DecodeUrl( char *psz )
2375 char *dup = strdup( psz );
2393 *psz++ = strtol( val, NULL, 16 );
2395 else if( *p == '+' )
2405 if( psz ) *psz++ ='\0';
2406 if( dup ) free( dup );
2409 /*****************************************************************************
2410 * ParseOption: parses the options for the input
2411 *****************************************************************************
2412 * This function parses the input (config) options and creates their associated
2414 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
2415 * bar is the value of the option.
2416 *****************************************************************************/
2417 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2419 char *psz_name = (char *)psz_option;
2420 char *psz_value = strchr( psz_option, '=' );
2421 int i_name_len, i_type;
2422 vlc_bool_t b_isno = VLC_FALSE;
2425 if( psz_value ) i_name_len = psz_value - psz_option;
2426 else i_name_len = strlen( psz_option );
2428 /* It's too much of an hassle to remove the ':' when we parse
2429 * the cmd line :) */
2430 if( i_name_len && *psz_name == ':' )
2436 if( i_name_len == 0 ) return;
2438 psz_name = strndup( psz_name, i_name_len );
2439 if( psz_value ) psz_value++;
2441 /* FIXME: :programs should be handled generically */
2442 if( !strcmp( psz_name, "programs" ) )
2443 i_type = VLC_VAR_LIST;
2445 i_type = config_GetType( p_input, psz_name );
2447 if( !i_type && !psz_value )
2449 /* check for "no-foo" or "nofoo" */
2450 if( !strncmp( psz_name, "no-", 3 ) )
2452 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2454 else if( !strncmp( psz_name, "no", 2 ) )
2456 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2458 else goto cleanup; /* Option doesn't exist */
2461 i_type = config_GetType( p_input, psz_name );
2463 if( !i_type ) goto cleanup; /* Option doesn't exist */
2465 else if( !i_type ) goto cleanup; /* Option doesn't exist */
2467 if( ( i_type != VLC_VAR_BOOL ) &&
2468 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2470 /* Create the variable in the input object.
2471 * Children of the input object will be able to retreive this value
2472 * thanks to the inheritance property of the object variables. */
2473 var_Create( p_input, psz_name, i_type );
2478 val.b_bool = !b_isno;
2481 case VLC_VAR_INTEGER:
2482 val.i_int = strtol( psz_value, NULL, 0 );
2486 val.f_float = atof( psz_value );
2489 case VLC_VAR_STRING:
2490 case VLC_VAR_MODULE:
2492 case VLC_VAR_DIRECTORY:
2493 val.psz_string = psz_value;
2498 char *psz_orig, *psz_var;
2499 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
2500 val.p_list = p_list;
2501 p_list->i_count = 0;
2503 psz_var = psz_orig = strdup(psz_value);
2504 while( psz_var && *psz_var )
2506 char *psz_item = psz_var;
2508 while( *psz_var && *psz_var != ',' ) psz_var++;
2509 if( *psz_var == ',' )
2514 val2.i_int = strtol( psz_item, NULL, 0 );
2515 INSERT_ELEM( p_list->p_values, p_list->i_count,
2516 p_list->i_count, val2 );
2517 /* p_list->i_count is incremented twice by INSERT_ELEM */
2519 INSERT_ELEM( p_list->pi_types, p_list->i_count,
2520 p_list->i_count, VLC_VAR_INTEGER );
2522 if( psz_orig ) free( psz_orig );
2531 var_Set( p_input, psz_name, val );
2533 msg_Dbg( p_input, "set input option: %s to %s", psz_name,
2534 psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2537 if( psz_name ) free( psz_name );
2541 /*****************************************************************************
2542 * MRLSplit: parse the access, demux and url part of the
2543 * Media Resource Locator.
2544 *****************************************************************************/
2545 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2546 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2548 char *psz_access = NULL;
2549 char *psz_demux = NULL;
2550 char *psz_path = NULL;
2551 char *psz, *psz_check;
2553 psz = strchr( psz_dup, ':' );
2555 /* '@' not allowed in access/demux part */
2556 psz_check = strchr( psz_dup, '@' );
2557 if( psz_check && psz_check < psz ) psz = 0;
2559 #if defined( WIN32 ) || defined( UNDER_CE )
2560 if( psz - psz_dup == 1 )
2562 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2571 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2575 psz = strchr( psz_dup, '/' );
2582 psz_access = psz_dup;
2589 if( !psz_access ) *ppsz_access = "";
2590 else *ppsz_access = psz_access;
2592 if( !psz_demux ) *ppsz_demux = "";
2593 else *ppsz_demux = psz_demux;
2595 if( !psz_path ) *ppsz_path = "";
2596 else *ppsz_path = psz_path;
2599 /*****************************************************************************
2600 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2603 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2604 *****************************************************************************/
2605 static void MRLSections( input_thread_t *p_input, char *psz_source,
2606 int *pi_title_start, int *pi_title_end,
2607 int *pi_chapter_start, int *pi_chapter_end )
2609 char *psz, *psz_end, *psz_next, *psz_check;
2611 *pi_title_start = *pi_title_end = -1;
2612 *pi_chapter_start = *pi_chapter_end = -1;
2614 /* Start by parsing titles and chapters */
2615 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2617 /* Check we are really dealing with a title/chapter section */
2618 psz_check = psz + 1;
2619 if( !*psz_check ) return;
2620 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2621 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2622 if( *psz_check == ':' && ++psz_check )
2623 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2624 if( *psz_check != '-' && *psz_check ) return;
2625 if( *psz_check == '-' && ++psz_check )
2626 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2627 if( *psz_check != ':' && *psz_check ) return;
2628 if( *psz_check == ':' && ++psz_check )
2629 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2630 if( *psz_check ) return;
2632 /* Separate start and end */
2634 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2636 /* Look for the start title */
2637 *pi_title_start = strtol( psz, &psz_next, 0 );
2638 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2639 *pi_title_end = *pi_title_start;
2642 /* Look for the start chapter */
2644 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2645 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2646 *pi_chapter_end = *pi_chapter_start;
2650 /* Look for the end title */
2651 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2652 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2655 /* Look for the end chapter */
2656 if( *psz_end ) psz_end++;
2657 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2658 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2661 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2662 psz_source, *pi_title_start, *pi_chapter_start,
2663 *pi_title_end, *pi_chapter_end );
2667 /***********************************************************************
2668 * Info management functions
2669 ***********************************************************************/
2671 * Get a info item from a given category in a given input item.
2673 * \param p_i The input item to get info from
2674 * \param psz_cat String representing the category for the info
2675 * \param psz_name String representing the name of the desired info
2676 * \return A pointer to the string with the given info if found, or an
2677 * empty string otherwise. The caller should free the returned
2680 char *vlc_input_item_GetInfo( input_item_t *p_i,
2681 const char *psz_cat,
2682 const char *psz_name )
2686 vlc_mutex_lock( &p_i->lock );
2688 for( i = 0 ; i< p_i->i_categories ; i++ )
2690 info_category_t *p_cat = p_i->pp_categories[i];
2692 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
2695 for( j = 0; j < p_cat->i_infos ; j++ )
2697 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
2699 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
2700 vlc_mutex_unlock( &p_i->lock );
2705 vlc_mutex_unlock( &p_i->lock );
2706 return strdup( "" );
2709 int vlc_input_item_AddInfo( input_item_t *p_i,
2710 const char *psz_cat,
2711 const char *psz_name,
2712 const char *psz_format, ... )
2716 info_t *p_info = NULL;
2717 info_category_t *p_cat = NULL ;
2719 vlc_mutex_lock( &p_i->lock );
2721 for( i = 0 ; i < p_i->i_categories ; i ++ )
2723 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
2725 p_cat = p_i->pp_categories[i];
2731 if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
2733 vlc_mutex_unlock( &p_i->lock );
2734 return VLC_EGENERIC;
2736 p_cat->psz_name = strdup( psz_cat );
2738 p_cat->pp_infos = 0;
2739 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
2743 for( i = 0; i< p_cat->i_infos; i++ )
2745 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
2747 p_info = p_cat->pp_infos[i];
2754 if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
2756 vlc_mutex_unlock( &p_i->lock );
2757 return VLC_EGENERIC;
2759 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
2760 p_info->psz_name = strdup( psz_name );
2764 if( p_info->psz_value ) free( p_info->psz_value );
2767 va_start( args, psz_format );
2768 vasprintf( &p_info->psz_value, psz_format, args);
2771 vlc_mutex_unlock( &p_i->lock );