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, "demux_read", VLC_VAR_INTEGER, STATS_COUNTER );
681 stats_Create( p_input, "input_bitrate", VLC_VAR_FLOAT,
683 stats_Create( p_input, "demux_bitrate", VLC_VAR_FLOAT,
685 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
687 if( p_counter ) p_counter->update_interval = 1000000;
688 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
690 if( p_counter ) p_counter->update_interval = 1000000;
692 psz = var_GetString( p_input, "sout" );
693 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
695 p_input->p_sout = sout_NewInstance( p_input, psz );
696 if( p_input->p_sout == NULL )
698 msg_Err( p_input, "cannot start stream output instance, " \
708 p_input->p_es_out = input_EsOutNew( p_input );
709 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
710 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
712 if( InputSourceInit( p_input, &p_input->input,
713 p_input->input.p_item->psz_uri, NULL, b_quick ) )
718 /* Create global title (from master) */
721 p_input->i_title = p_input->input.i_title;
722 p_input->title = p_input->input.title;
723 p_input->i_title_offset = p_input->input.i_title_offset;
724 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
725 if( p_input->i_title > 0 )
727 /* Setup variables */
728 input_ControlVarNavigation( p_input );
729 input_ControlVarTitle( p_input, 0 );
733 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
734 p_input->b_can_pause = p_input->input.b_can_pause;
737 if( p_input->i_pts_delay < 0 )
738 p_input->i_pts_delay = 0;
740 /* If the desynchronisation requested by the user is < 0, we need to
741 * cache more data. */
742 var_Get( p_input, "audio-desync", &val );
743 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
745 /* Update cr_average depending on the caching */
746 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
747 p_input->input.i_cr_average /= 10;
748 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
751 /* Load master infos */
753 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
754 &val.i_time ) && val.i_time > 0 )
756 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
757 UpdateItemLength( p_input, val.i_time, b_quick );
758 p_input->input.p_item->i_duration = val.i_time;
761 /* Start title/chapter */
764 val.i_int = p_input->input.i_title_start -
765 p_input->input.i_title_offset;
766 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
767 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
768 val.i_int = p_input->input.i_seekpoint_start -
769 p_input->input.i_seekpoint_offset;
770 if( val.i_int > 0 /* TODO: check upper boundary */ )
771 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
775 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
777 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
780 if( p_input->i_start > 0 )
782 if( p_input->i_start >= val.i_time )
784 msg_Warn( p_input, "invalid start-time ignored" );
790 msg_Dbg( p_input, "starting at time: %ds",
791 (int)( p_input->i_start / I64C(1000000) ) );
793 s.i_time = p_input->i_start;
794 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
797 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
799 msg_Warn( p_input, "invalid stop-time ignored" );
805 /* Get fps and set it if not already set */
806 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
809 float f_requested_fps;
811 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
812 var_SetFloat( p_input, "sub-original-fps", f_fps );
814 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
815 if( f_requested_fps != f_fps )
817 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
819 var_SetFloat( p_input, "sub-fps", f_requested_fps );
823 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
826 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
830 /* Look for and add subtitle files */
831 psz_subtitle = var_GetString( p_input, "sub-file" );
838 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
840 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
843 sub = InputSourceNew( p_input );
844 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
847 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
850 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
853 if( count.i_int == 0 )
855 /* if it was first one, there is disable too */
857 if( count.i_int < list.p_list->i_count )
859 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
860 &list.p_list->p_values[count.i_int] );
862 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
868 var_Get( p_input, "sub-autodetect-file", &val );
871 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
872 char **subs = subtitles_Detect( p_input, psz_autopath,
873 p_input->input.p_item->psz_uri );
876 for( i = 0; subs && subs[i]; i++ )
878 if( strcmp( psz_subtitle, subs[i] ) )
880 sub = InputSourceNew( p_input );
881 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
884 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
889 if( subs ) free( subs );
890 if( psz_autopath ) free( psz_autopath );
892 free( psz_subtitle );
895 psz = var_GetString( p_input, "input-slave" );
899 input_source_t *slave;
902 while( *psz == ' ' || *psz == '#' )
906 if( ( psz_delim = strchr( psz, '#' ) ) )
915 msg_Dbg( p_input, "adding slave input '%s'", psz );
916 slave = InputSourceNew( p_input );
917 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
919 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
924 if( psz ) free( psz );
928 p_input->i_start = 0;
929 p_input->i_start = 0;
935 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
936 i_es_out_mode = ES_OUT_MODE_AUTO;
938 if( p_input->p_sout )
940 var_Get( p_input, "sout-all", &val );
943 i_es_out_mode = ES_OUT_MODE_ALL;
948 var_Get( p_input, "programs", &val );
949 if ( val.p_list && val.p_list->i_count )
951 i_es_out_mode = ES_OUT_MODE_PARTIAL;
952 /* Note : we should remove the "program" callback. */
955 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
959 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
961 /* Inform the demuxer about waited group (needed only for DVB) */
962 if( i_es_out_mode == ES_OUT_MODE_ALL )
964 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
966 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
968 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
973 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
974 (int) var_GetInteger( p_input, "program" ), NULL );
977 if( p_input->p_sout )
979 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
981 p_input->b_out_pace_control = VLC_FALSE;
985 p_input->b_out_pace_control = VLC_TRUE;
988 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
990 /* We don't want a high input priority here or we'll
991 * end-up sucking up all the CPU time */
992 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
995 msg_Dbg( p_input, "starting in %s mode",
996 p_input->b_out_pace_control ? "asynch" : "synch" );
1000 /* Get meta data from users */
1001 p_meta_tmp = InputMetaUser( p_input );
1003 /* Get meta data from master input */
1004 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
1008 if( p_meta == NULL )
1010 p_meta = p_meta_tmp;
1012 else if( p_meta_tmp )
1014 vlc_meta_Merge( p_meta, p_meta_tmp );
1015 vlc_meta_Delete( p_meta_tmp );
1018 /* Access_file does not give any meta, and there are no slave */
1021 if( !p_input->input.p_access ||
1022 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1026 if( p_meta == NULL )
1028 p_meta = p_meta_tmp;
1030 else if( p_meta_tmp )
1032 vlc_meta_Merge( p_meta, p_meta_tmp );
1033 vlc_meta_Delete( p_meta_tmp );
1036 /* Get meta data from slave input */
1037 for( i = 0; i < p_input->i_slave; i++ )
1039 vlc_meta_t *p_meta_slave;
1041 if( !demux2_Control( p_input->slave[i]->p_demux,
1042 DEMUX_GET_META, &p_meta_slave ) )
1044 if( p_meta == NULL )
1046 p_meta = p_meta_slave;
1048 else if( p_meta_slave )
1050 vlc_meta_Merge( p_meta, p_meta_slave );
1051 vlc_meta_Delete( p_meta_slave );
1055 if( p_input->slave[i]->p_access &&
1056 !access2_Control( p_input->slave[i]->p_access,
1057 ACCESS_GET_META, &p_meta_slave ) )
1059 if( p_meta == NULL )
1061 p_meta = p_meta_slave;
1063 else if( p_meta_slave )
1065 vlc_meta_Merge( p_meta, p_meta_slave );
1066 vlc_meta_Delete( p_meta_slave );
1072 p_input->p_meta = p_meta;
1073 UpdateMeta( p_input, b_quick );
1077 msg_Dbg( p_input, "`%s' successfully opened",
1078 p_input->input.p_item->psz_uri );
1082 /* initialization is complete */
1083 p_input->i_state = PLAYING_S;
1085 val.i_int = PLAYING_S;
1086 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1091 if( p_input->p_es_out )
1092 input_EsOutDelete( p_input->p_es_out );
1094 if( p_input->p_sout )
1095 sout_DeleteInstance( p_input->p_sout );
1097 /* Mark them deleted */
1098 p_input->input.p_demux = NULL;
1099 p_input->input.p_stream = NULL;
1100 p_input->input.p_access = NULL;
1101 p_input->p_es_out = NULL;
1102 p_input->p_sout = NULL;
1104 return VLC_EGENERIC;
1107 /*****************************************************************************
1108 * Error: RunThread() error loop
1109 *****************************************************************************
1110 * This function is called when an error occurred during thread main's loop.
1111 *****************************************************************************/
1112 static void Error( input_thread_t *p_input )
1114 while( !p_input->b_die )
1117 msleep( INPUT_IDLE_SLEEP );
1121 /*****************************************************************************
1122 * End: end the input thread
1123 *****************************************************************************/
1124 static void End( input_thread_t * p_input )
1129 msg_Dbg( p_input, "closing input" );
1131 /* We are at the end */
1132 p_input->i_state = END_S;
1135 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1137 /* Clean control variables */
1138 input_ControlVarClean( p_input );
1140 /* Clean up master */
1141 InputSourceClean( p_input, &p_input->input );
1144 for( i = 0; i < p_input->i_slave; i++ )
1146 InputSourceClean( p_input, p_input->slave[i] );
1147 free( p_input->slave[i] );
1149 if( p_input->slave ) free( p_input->slave );
1151 /* Unload all modules */
1152 if( p_input->p_es_out )
1153 input_EsOutDelete( p_input->p_es_out );
1155 /* Close optional stream output instance */
1156 if( p_input->p_sout )
1158 vlc_object_t *p_pl =
1159 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1162 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1164 /* attach sout to the playlist */
1165 msg_Dbg( p_input, "keeping sout" );
1166 vlc_object_detach( p_input->p_sout );
1167 vlc_object_attach( p_input->p_sout, p_pl );
1171 msg_Dbg( p_input, "destroying sout" );
1172 sout_DeleteInstance( p_input->p_sout );
1175 vlc_object_release( p_pl );
1179 if( p_input->p_meta )
1180 vlc_meta_Delete( p_input->p_meta );
1182 /* Tell we're dead */
1183 p_input->b_dead = VLC_TRUE;
1186 /*****************************************************************************
1188 *****************************************************************************/
1189 static inline int ControlPopNoLock( input_thread_t *p_input,
1190 int *pi_type, vlc_value_t *p_val )
1192 if( p_input->i_control <= 0 )
1194 return VLC_EGENERIC;
1197 *pi_type = p_input->control[0].i_type;
1198 *p_val = p_input->control[0].val;
1200 p_input->i_control--;
1201 if( p_input->i_control > 0 )
1205 for( i = 0; i < p_input->i_control; i++ )
1207 p_input->control[i].i_type = p_input->control[i+1].i_type;
1208 p_input->control[i].val = p_input->control[i+1].val;
1215 static void ControlReduce( input_thread_t *p_input )
1218 for( i = 1; i < p_input->i_control; i++ )
1220 const int i_lt = p_input->control[i-1].i_type;
1221 const int i_ct = p_input->control[i].i_type;
1223 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1224 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1228 ( i_ct == INPUT_CONTROL_SET_STATE ||
1229 i_ct == INPUT_CONTROL_SET_RATE ||
1230 i_ct == INPUT_CONTROL_SET_POSITION ||
1231 i_ct == INPUT_CONTROL_SET_TIME ||
1232 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1233 i_ct == INPUT_CONTROL_SET_TITLE ||
1234 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1235 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1238 // msg_Dbg( p_input, "merged at %d", i );
1239 /* Remove the i-1 */
1240 for( j = i; j < p_input->i_control; j++ )
1241 p_input->control[j-1] = p_input->control[j];
1242 p_input->i_control--;
1246 /* TODO but that's not that important
1247 - merge SET_X with SET_X_CMD
1248 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1249 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1256 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1259 vlc_bool_t b_force_update = VLC_FALSE;
1261 if( !p_input ) return b_force_update;
1265 case INPUT_CONTROL_SET_DIE:
1266 msg_Dbg( p_input, "control: stopping input" );
1267 /* Mark all submodules to die */
1268 if( p_input->input.p_access )
1269 p_input->input.p_access->b_die = VLC_TRUE;
1270 if( p_input->input.p_stream )
1271 p_input->input.p_stream->b_die = VLC_TRUE;
1272 p_input->input.p_demux->b_die = VLC_TRUE;
1274 p_input->b_die = VLC_TRUE;
1277 case INPUT_CONTROL_SET_POSITION:
1278 case INPUT_CONTROL_SET_POSITION_OFFSET:
1281 if( i_type == INPUT_CONTROL_SET_POSITION )
1283 f_pos = val.f_float;
1287 /* Should not fail */
1288 demux2_Control( p_input->input.p_demux,
1289 DEMUX_GET_POSITION, &f_pos );
1290 f_pos += val.f_float;
1292 if( f_pos < 0.0 ) f_pos = 0.0;
1293 if( f_pos > 1.0 ) f_pos = 1.0;
1294 /* Reset the decoders states and clock synch (before calling the demuxer */
1295 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1296 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1297 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1300 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1301 "%2.1f%% failed", f_pos * 100 );
1305 if( p_input->i_slave > 0 )
1306 SlaveSeek( p_input );
1308 b_force_update = VLC_TRUE;
1313 case INPUT_CONTROL_SET_TIME:
1314 case INPUT_CONTROL_SET_TIME_OFFSET:
1319 if( i_type == INPUT_CONTROL_SET_TIME )
1321 i_time = val.i_time;
1325 /* Should not fail */
1326 demux2_Control( p_input->input.p_demux,
1327 DEMUX_GET_TIME, &i_time );
1328 i_time += val.i_time;
1330 if( i_time < 0 ) i_time = 0;
1332 /* Reset the decoders states and clock synch (before calling the demuxer */
1333 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1334 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1336 i_ret = demux2_Control( p_input->input.p_demux,
1337 DEMUX_SET_TIME, i_time );
1342 /* Emulate it with a SET_POS */
1343 demux2_Control( p_input->input.p_demux,
1344 DEMUX_GET_LENGTH, &i_length );
1347 double f_pos = (double)i_time / (double)i_length;
1348 i_ret = demux2_Control( p_input->input.p_demux,
1349 DEMUX_SET_POSITION, f_pos );
1354 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1355 " failed or not possible", i_time );
1359 if( p_input->i_slave > 0 )
1360 SlaveSeek( p_input );
1362 b_force_update = VLC_TRUE;
1367 case INPUT_CONTROL_SET_STATE:
1368 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1369 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1372 if( p_input->input.p_access )
1373 i_ret = access2_Control( p_input->input.p_access,
1374 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1376 i_ret = demux2_Control( p_input->input.p_demux,
1377 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1381 /* FIXME What to do ? */
1382 msg_Warn( p_input, "cannot unset pause -> EOF" );
1383 vlc_mutex_unlock( &p_input->lock_control );
1384 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1385 vlc_mutex_lock( &p_input->lock_control );
1388 b_force_update = VLC_TRUE;
1390 /* Switch to play */
1391 p_input->i_state = PLAYING_S;
1392 val.i_int = PLAYING_S;
1393 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1396 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1397 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1399 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1400 p_input->b_can_pause )
1403 if( p_input->input.p_access )
1404 i_ret = access2_Control( p_input->input.p_access,
1405 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1407 i_ret = demux2_Control( p_input->input.p_demux,
1408 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1410 b_force_update = VLC_TRUE;
1414 msg_Warn( p_input, "cannot set pause state" );
1415 val.i_int = p_input->i_state;
1419 val.i_int = PAUSE_S;
1422 /* Switch to new state */
1423 p_input->i_state = val.i_int;
1424 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1426 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1428 b_force_update = VLC_TRUE;
1430 /* Correct "state" value */
1431 val.i_int = p_input->i_state;
1432 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1434 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1436 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1440 case INPUT_CONTROL_SET_RATE:
1441 case INPUT_CONTROL_SET_RATE_SLOWER:
1442 case INPUT_CONTROL_SET_RATE_FASTER:
1446 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1447 i_rate = p_input->i_rate * 2;
1448 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1449 i_rate = p_input->i_rate / 2;
1453 if( i_rate < INPUT_RATE_MIN )
1455 msg_Dbg( p_input, "cannot set rate faster" );
1456 i_rate = INPUT_RATE_MIN;
1458 else if( i_rate > INPUT_RATE_MAX )
1460 msg_Dbg( p_input, "cannot set rate slower" );
1461 i_rate = INPUT_RATE_MAX;
1463 if( i_rate != INPUT_RATE_DEFAULT &&
1464 ( !p_input->b_can_pace_control ||
1465 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1467 msg_Dbg( p_input, "cannot change rate" );
1468 i_rate = INPUT_RATE_DEFAULT;
1470 if( i_rate != p_input->i_rate )
1472 p_input->i_rate = i_rate;
1474 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1476 /* We haven't send data to decoder when rate != default */
1477 if( i_rate == INPUT_RATE_DEFAULT )
1478 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1481 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1483 b_force_update = VLC_TRUE;
1488 case INPUT_CONTROL_SET_PROGRAM:
1489 /* No need to force update, es_out does it if needed */
1490 es_out_Control( p_input->p_es_out,
1491 ES_OUT_SET_GROUP, val.i_int );
1493 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1497 case INPUT_CONTROL_SET_ES:
1498 /* No need to force update, es_out does it if needed */
1499 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1500 input_EsOutGetFromID( p_input->p_es_out,
1504 case INPUT_CONTROL_SET_AUDIO_DELAY:
1505 input_EsOutSetDelay( p_input->p_es_out,
1506 AUDIO_ES, val.i_time );
1507 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1510 case INPUT_CONTROL_SET_SPU_DELAY:
1511 input_EsOutSetDelay( p_input->p_es_out,
1512 SPU_ES, val.i_time );
1513 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1516 case INPUT_CONTROL_SET_TITLE:
1517 case INPUT_CONTROL_SET_TITLE_NEXT:
1518 case INPUT_CONTROL_SET_TITLE_PREV:
1519 if( p_input->input.b_title_demux &&
1520 p_input->input.i_title > 0 )
1523 /* FIXME handle demux title */
1524 demux_t *p_demux = p_input->input.p_demux;
1527 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1528 i_title = p_demux->info.i_title - 1;
1529 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1530 i_title = p_demux->info.i_title + 1;
1532 i_title = val.i_int;
1534 if( i_title >= 0 && i_title < p_input->input.i_title )
1536 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1537 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1539 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1540 input_ControlVarTitle( p_input, i_title );
1543 else if( p_input->input.i_title > 0 )
1545 access_t *p_access = p_input->input.p_access;
1548 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1549 i_title = p_access->info.i_title - 1;
1550 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1551 i_title = p_access->info.i_title + 1;
1553 i_title = val.i_int;
1555 if( i_title >= 0 && i_title < p_input->input.i_title )
1557 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1558 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1560 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1561 stream_AccessReset( p_input->input.p_stream );
1565 case INPUT_CONTROL_SET_SEEKPOINT:
1566 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1567 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1568 if( p_input->input.b_title_demux &&
1569 p_input->input.i_title > 0 )
1571 demux_t *p_demux = p_input->input.p_demux;
1573 int64_t i_input_time;
1574 int64_t i_seekpoint_time;
1576 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1578 i_seekpoint = p_demux->info.i_seekpoint;
1579 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1580 if( i_seekpoint_time >= 0 &&
1581 !demux2_Control( p_demux,
1582 DEMUX_GET_TIME, &i_input_time ) )
1584 if ( i_input_time < i_seekpoint_time + 3000000 )
1590 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1591 i_seekpoint = p_demux->info.i_seekpoint + 1;
1593 i_seekpoint = val.i_int;
1595 if( i_seekpoint >= 0 && i_seekpoint <
1596 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1598 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1599 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1601 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1604 else if( p_input->input.i_title > 0 )
1606 demux_t *p_demux = p_input->input.p_demux;
1607 access_t *p_access = p_input->input.p_access;
1609 int64_t i_input_time;
1610 int64_t i_seekpoint_time;
1612 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1614 i_seekpoint = p_access->info.i_seekpoint;
1615 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1616 if( i_seekpoint_time >= 0 &&
1617 demux2_Control( p_demux,
1618 DEMUX_GET_TIME, &i_input_time ) )
1620 if ( i_input_time < i_seekpoint_time + 3000000 )
1626 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1627 i_seekpoint = p_access->info.i_seekpoint + 1;
1629 i_seekpoint = val.i_int;
1631 if( i_seekpoint >= 0 && i_seekpoint <
1632 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1634 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1635 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1637 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1638 stream_AccessReset( p_input->input.p_stream );
1643 case INPUT_CONTROL_ADD_SLAVE:
1644 if( val.psz_string )
1646 input_source_t *slave = InputSourceNew( p_input );
1648 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1651 vlc_meta_t *p_meta_new = NULL;
1656 msg_Dbg( p_input, "adding %s as slave on the fly",
1660 if( demux2_Control( p_input->input.p_demux,
1661 DEMUX_GET_TIME, &i_time ) )
1663 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1664 InputSourceClean( p_input, slave );
1668 if( demux2_Control( slave->p_demux,
1669 DEMUX_SET_TIME, i_time ) )
1671 msg_Err( p_input, "seek failed for new slave" );
1672 InputSourceClean( p_input, slave );
1678 /* Get meta (access and demux) */
1679 if( access2_Control( slave->p_access,
1680 ACCESS_GET_META, &p_meta_new ) )
1682 if( !demux2_Control( slave->p_demux,
1683 DEMUX_GET_META, &p_meta ) )
1687 vlc_meta_Merge( p_meta_new, p_meta );
1688 vlc_meta_Delete( p_meta );
1692 p_meta_new = p_meta;
1698 if( p_input->p_meta )
1700 vlc_meta_Merge( p_input->p_meta, p_meta_new );
1701 vlc_meta_Delete( p_meta_new );
1705 p_input->p_meta = p_meta_new;
1707 UpdateMeta( p_input, VLC_FALSE );
1710 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1714 msg_Warn( p_input, "failed to add %s as slave",
1718 free( val.psz_string );
1722 case INPUT_CONTROL_SET_BOOKMARK:
1724 msg_Err( p_input, "not yet implemented" );
1728 return b_force_update;
1731 /*****************************************************************************
1733 *****************************************************************************/
1734 static int UpdateFromDemux( input_thread_t *p_input )
1736 demux_t *p_demux = p_input->input.p_demux;
1739 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1741 v.i_int = p_demux->info.i_title;
1742 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1744 input_ControlVarTitle( p_input, p_demux->info.i_title );
1746 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1748 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1750 v.i_int = p_demux->info.i_seekpoint;
1751 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1753 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1755 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1757 /* Hmmm only works with master input */
1758 if( p_input->input.p_demux == p_demux )
1760 int i_title_end = p_input->input.i_title_end -
1761 p_input->input.i_title_offset;
1762 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1763 p_input->input.i_seekpoint_offset;
1765 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1767 if( p_demux->info.i_title > i_title_end ||
1768 ( p_demux->info.i_title == i_title_end &&
1769 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1771 else if( i_seekpoint_end >=0 )
1773 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1775 else if( i_title_end >= 0 )
1777 if( p_demux->info.i_title > i_title_end ) return 0;
1784 /*****************************************************************************
1786 *****************************************************************************/
1787 static int UpdateFromAccess( input_thread_t *p_input )
1789 access_t *p_access = p_input->input.p_access;
1792 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1794 v.i_int = p_access->info.i_title;
1795 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1797 input_ControlVarTitle( p_input, p_access->info.i_title );
1799 stream_AccessUpdate( p_input->input.p_stream );
1801 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1803 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1805 v.i_int = p_access->info.i_seekpoint;
1806 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1808 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1810 if( p_access->info.i_update & INPUT_UPDATE_META )
1812 /* TODO maybe multi - access ? */
1814 if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1816 if( p_input->p_meta )
1818 vlc_meta_Merge( p_input->p_meta, p_meta );
1819 vlc_meta_Delete( p_meta );
1823 p_input->p_meta = p_meta;
1826 UpdateMeta( p_input, VLC_FALSE );
1827 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1829 p_access->info.i_update &= ~INPUT_UPDATE_META;
1832 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1834 /* Hmmm only works with master input */
1835 if( p_input->input.p_access == p_access )
1837 int i_title_end = p_input->input.i_title_end -
1838 p_input->input.i_title_offset;
1839 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1840 p_input->input.i_seekpoint_offset;
1842 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1844 if( p_access->info.i_title > i_title_end ||
1845 ( p_access->info.i_title == i_title_end &&
1846 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1848 else if( i_seekpoint_end >=0 )
1850 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1852 else if( i_title_end >= 0 )
1854 if( p_access->info.i_title > i_title_end ) return 0;
1861 /*****************************************************************************
1863 *****************************************************************************/
1864 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1866 vlc_meta_t *p_meta = p_input->p_meta;
1869 if( !p_meta || p_meta->i_meta == 0 )
1872 if( !b_quick ) msg_Dbg( p_input, "meta information:" );
1873 for( i = 0; i < p_meta->i_meta; i++ )
1876 msg_Dbg( p_input, " - '%s' = '%s'",
1877 _(p_meta->name[i]), p_meta->value[i] );
1879 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] &&
1880 !p_input->input.p_item->b_fixed_name )
1881 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1883 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1884 input_Control( p_input, INPUT_ADD_INFO, _("General"),
1885 _("Author"), p_meta->value[i] );
1887 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1888 _(p_meta->name[i]), "%s", p_meta->value[i] );
1891 for( i = 0; i < p_meta->i_track; i++ )
1893 vlc_meta_t *tk = p_meta->track[i];
1896 if( tk->i_meta > 0 )
1898 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1900 msg_Dbg( p_input, " - track[%d]:", i );
1902 sprintf( psz_cat, "%s %d", _("Stream"), i );
1903 for( j = 0; j < tk->i_meta; j++ )
1905 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1908 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1909 _(tk->name[j]), "%s", tk->value[j] );
1914 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1916 p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1922 /*****************************************************************************
1924 *****************************************************************************/
1925 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1926 vlc_bool_t b_quick )
1928 playlist_t *p_playlist;
1929 char psz_buffer[MSTRTIME_MAX_SIZE];
1931 vlc_mutex_lock( &p_input->input.p_item->lock );
1932 p_input->input.p_item->i_duration = i_length;
1933 vlc_mutex_unlock( &p_input->input.p_item->lock );
1935 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1939 var_SetInteger( p_playlist, "item-change",
1940 p_input->input.p_item->i_id );
1941 vlc_object_release( p_playlist );
1944 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1945 msecstotimestr( psz_buffer, i_length / 1000 ) );
1948 /*****************************************************************************
1950 *****************************************************************************/
1951 static input_source_t *InputSourceNew( input_thread_t *p_input )
1953 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1957 msg_Err( p_input, "out of memory for new input source" );
1962 in->p_access = NULL;
1963 in->p_stream = NULL;
1965 in->b_title_demux = VLC_FALSE;
1968 in->b_can_pace_control = VLC_TRUE;
1969 in->b_eof = VLC_FALSE;
1970 in->i_cr_average = 0;
1975 /*****************************************************************************
1977 *****************************************************************************/
1978 static int InputSourceInit( input_thread_t *p_input,
1979 input_source_t *in, char *psz_mrl,
1980 char *psz_forced_demux, vlc_bool_t b_quick )
1982 char *psz_dup = strdup( psz_mrl );
1990 if( !in ) return VLC_EGENERIC;
1995 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1996 &psz_access, &psz_demux, &psz_path );
1998 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1999 psz_mrl, psz_access, psz_demux, psz_path );
2001 /* Hack to allow udp://@:port syntax */
2003 (strncmp( psz_access, "udp", 3 ) &&
2004 strncmp( psz_access, "rtp", 3 )) )
2006 /* Find optional titles and seekpoints */
2007 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2008 &in->i_seekpoint_start, &in->i_seekpoint_end );
2010 if( psz_forced_demux && *psz_forced_demux )
2012 psz_demux = psz_forced_demux;
2014 else if( !psz_demux || *psz_demux == '\0' )
2016 /* special hack for forcing a demuxer with --demux=module
2017 * (and do nothing with a list) */
2018 char *psz_var_demux = var_GetString( p_input, "demux" );
2020 if( *psz_var_demux != '\0' &&
2021 !strchr(psz_var_demux, ',' ) &&
2022 !strchr(psz_var_demux, ':' ) )
2024 psz_demux = psz_var_demux;
2026 msg_Dbg( p_input, "Enforce demux ` %s'", psz_demux );
2028 else if( psz_var_demux )
2030 free( psz_var_demux );
2034 /* Try access_demux if no demux given */
2035 if( *psz_demux == '\0' )
2037 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2038 NULL, p_input->p_es_out, VLC_FALSE );
2044 msg_Dbg( p_input, "trying to preparse %s", psz_path );
2045 psz_demux = strdup( "" );
2046 psz_access = strdup( "file" );
2051 int64_t i_pts_delay;
2053 /* Get infos from access_demux */
2054 demux2_Control( in->p_demux,
2055 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2056 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2058 in->b_title_demux = VLC_TRUE;
2059 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2060 &in->title, &in->i_title,
2061 &in->i_title_offset, &in->i_seekpoint_offset ) )
2066 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2067 &in->b_can_pace_control );
2068 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2072 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2078 int64_t i_pts_delay;
2080 /* Now try a real access */
2081 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2084 /* Access failed, URL encoded ? */
2085 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2087 DecodeUrl( psz_path );
2089 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2090 psz_access, psz_demux, psz_path );
2092 in->p_access = access2_New( p_input,
2093 psz_access, psz_demux, psz_path,
2096 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2097 * are forbidden in filenames on Win32. */
2099 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2100 if( in->p_access == NULL &&
2101 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2103 if( psz_dup ) free( psz_dup );
2104 psz_dup = strdup( psz_mrl );
2109 in->p_access = access2_New( p_input,
2110 psz_access, psz_demux, psz_path,
2115 if( in->p_access == NULL )
2117 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2118 intf_UserFatal( VLC_OBJECT( p_input),
2119 _("Errors"),"Unable to open '%s'", psz_mrl );
2124 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2125 while( psz && *psz )
2127 access_t *p_access = in->p_access;
2128 char *end = strchr( psz, ':' );
2133 in->p_access = access2_FilterNew( in->p_access, psz );
2134 if( in->p_access == NULL )
2136 in->p_access = p_access;
2137 msg_Warn( p_input, "failed to insert access filter %s",
2143 if( psz_tmp ) free( psz_tmp );
2145 /* Get infos from access */
2148 access2_Control( in->p_access,
2149 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2150 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2152 in->b_title_demux = VLC_FALSE;
2153 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2154 &in->title, &in->i_title,
2155 &in->i_title_offset, &in->i_seekpoint_offset ) )
2161 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2162 &in->b_can_pace_control );
2163 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2165 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2167 var_Set( p_input, "seekable", val );
2170 /* Create the stream_t */
2171 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2172 if( in->p_stream == NULL )
2174 msg_Warn( p_input, "cannot create a stream_t from access" );
2178 /* Open a demuxer */
2179 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2181 psz_demux = in->p_access->psz_demux;
2183 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2184 in->p_stream, p_input->p_es_out, b_quick );
2185 if( in->p_demux == NULL )
2187 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2188 psz_access, psz_demux, psz_path );
2189 intf_UserFatal( VLC_OBJECT( p_input), _("Errors"),
2190 "Unrecognized format for '%s'", psz_mrl );
2194 /* TODO get title from demux */
2195 if( !b_quick && in->i_title <= 0 )
2197 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2198 &in->title, &in->i_title,
2199 &in->i_title_offset, &in->i_seekpoint_offset ))
2206 in->b_title_demux = VLC_TRUE;
2211 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2212 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2214 if( psz_dup ) free( psz_dup );
2219 demux2_Delete( in->p_demux );
2222 stream_Delete( in->p_stream );
2225 access2_Delete( in->p_access );
2226 if( psz_dup ) free( psz_dup );
2228 return VLC_EGENERIC;
2231 /*****************************************************************************
2233 *****************************************************************************/
2234 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2237 demux2_Delete( in->p_demux );
2240 stream_Delete( in->p_stream );
2243 access2_Delete( in->p_access );
2245 if( in->i_title > 0 )
2248 for( i = 0; i < in->i_title; i++ )
2250 vlc_input_title_Delete( in->title[i] );
2256 static void SlaveDemux( input_thread_t *p_input )
2261 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2263 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2267 for( i = 0; i < p_input->i_slave; i++ )
2269 input_source_t *in = p_input->slave[i];
2275 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2280 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2282 msg_Err( p_input, "slave[%d] doesn't like "
2283 "DEMUX_GET_TIME -> EOF", i );
2288 if( i_stime >= i_time )
2291 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2297 i_ret = in->p_demux->pf_demux( in->p_demux );
2302 msg_Dbg( p_input, "slave %d EOF", i );
2303 in->b_eof = VLC_TRUE;
2308 static void SlaveSeek( input_thread_t *p_input )
2313 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2315 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2319 for( i = 0; i < p_input->i_slave; i++ )
2321 input_source_t *in = p_input->slave[i];
2323 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2325 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2326 in->b_eof = VLC_TRUE;
2330 /*****************************************************************************
2332 *****************************************************************************/
2333 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2338 if( ( p_meta = vlc_meta_New() ) == NULL )
2341 /* Get meta information from user */
2342 #define GET_META( c, s ) \
2343 var_Get( p_input, (s), &val ); \
2344 if( *val.psz_string ) \
2345 vlc_meta_Add( p_meta, c, val.psz_string ); \
2346 free( val.psz_string )
2348 GET_META( VLC_META_TITLE, "meta-title" );
2349 GET_META( VLC_META_AUTHOR, "meta-author" );
2350 GET_META( VLC_META_ARTIST, "meta-artist" );
2351 GET_META( VLC_META_GENRE, "meta-genre" );
2352 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2353 GET_META( VLC_META_DESCRIPTION, "meta-description" );
2354 GET_META( VLC_META_DATE, "meta-date" );
2355 GET_META( VLC_META_URL, "meta-url" );
2361 /*****************************************************************************
2362 * DecodeUrl: decode a given encoded url
2363 *****************************************************************************/
2364 static void DecodeUrl( char *psz )
2366 char *dup = strdup( psz );
2384 *psz++ = strtol( val, NULL, 16 );
2386 else if( *p == '+' )
2396 if( psz ) *psz++ ='\0';
2397 if( dup ) free( dup );
2400 /*****************************************************************************
2401 * ParseOption: parses the options for the input
2402 *****************************************************************************
2403 * This function parses the input (config) options and creates their associated
2405 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
2406 * bar is the value of the option.
2407 *****************************************************************************/
2408 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2410 char *psz_name = (char *)psz_option;
2411 char *psz_value = strchr( psz_option, '=' );
2412 int i_name_len, i_type;
2413 vlc_bool_t b_isno = VLC_FALSE;
2416 if( psz_value ) i_name_len = psz_value - psz_option;
2417 else i_name_len = strlen( psz_option );
2419 /* It's too much of an hassle to remove the ':' when we parse
2420 * the cmd line :) */
2421 if( i_name_len && *psz_name == ':' )
2427 if( i_name_len == 0 ) return;
2429 psz_name = strndup( psz_name, i_name_len );
2430 if( psz_value ) psz_value++;
2432 /* FIXME: :programs should be handled generically */
2433 if( !strcmp( psz_name, "programs" ) )
2434 i_type = VLC_VAR_LIST;
2436 i_type = config_GetType( p_input, psz_name );
2438 if( !i_type && !psz_value )
2440 /* check for "no-foo" or "nofoo" */
2441 if( !strncmp( psz_name, "no-", 3 ) )
2443 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2445 else if( !strncmp( psz_name, "no", 2 ) )
2447 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2449 else goto cleanup; /* Option doesn't exist */
2452 i_type = config_GetType( p_input, psz_name );
2454 if( !i_type ) goto cleanup; /* Option doesn't exist */
2456 else if( !i_type ) goto cleanup; /* Option doesn't exist */
2458 if( ( i_type != VLC_VAR_BOOL ) &&
2459 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2461 /* Create the variable in the input object.
2462 * Children of the input object will be able to retreive this value
2463 * thanks to the inheritance property of the object variables. */
2464 var_Create( p_input, psz_name, i_type );
2469 val.b_bool = !b_isno;
2472 case VLC_VAR_INTEGER:
2473 val.i_int = strtol( psz_value, NULL, 0 );
2477 val.f_float = atof( psz_value );
2480 case VLC_VAR_STRING:
2481 case VLC_VAR_MODULE:
2483 case VLC_VAR_DIRECTORY:
2484 val.psz_string = psz_value;
2489 char *psz_orig, *psz_var;
2490 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
2491 val.p_list = p_list;
2492 p_list->i_count = 0;
2494 psz_var = psz_orig = strdup(psz_value);
2495 while( psz_var && *psz_var )
2497 char *psz_item = psz_var;
2499 while( *psz_var && *psz_var != ',' ) psz_var++;
2500 if( *psz_var == ',' )
2505 val2.i_int = strtol( psz_item, NULL, 0 );
2506 INSERT_ELEM( p_list->p_values, p_list->i_count,
2507 p_list->i_count, val2 );
2508 /* p_list->i_count is incremented twice by INSERT_ELEM */
2510 INSERT_ELEM( p_list->pi_types, p_list->i_count,
2511 p_list->i_count, VLC_VAR_INTEGER );
2513 if( psz_orig ) free( psz_orig );
2522 var_Set( p_input, psz_name, val );
2524 msg_Dbg( p_input, "set input option: %s to %s", psz_name,
2525 psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2528 if( psz_name ) free( psz_name );
2532 /*****************************************************************************
2533 * MRLSplit: parse the access, demux and url part of the
2534 * Media Resource Locator.
2535 *****************************************************************************/
2536 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2537 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2539 char *psz_access = NULL;
2540 char *psz_demux = NULL;
2541 char *psz_path = NULL;
2542 char *psz, *psz_check;
2544 psz = strchr( psz_dup, ':' );
2546 /* '@' not allowed in access/demux part */
2547 psz_check = strchr( psz_dup, '@' );
2548 if( psz_check && psz_check < psz ) psz = 0;
2550 #if defined( WIN32 ) || defined( UNDER_CE )
2551 if( psz - psz_dup == 1 )
2553 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2562 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2566 psz = strchr( psz_dup, '/' );
2573 psz_access = psz_dup;
2580 if( !psz_access ) *ppsz_access = "";
2581 else *ppsz_access = psz_access;
2583 if( !psz_demux ) *ppsz_demux = "";
2584 else *ppsz_demux = psz_demux;
2586 if( !psz_path ) *ppsz_path = "";
2587 else *ppsz_path = psz_path;
2590 /*****************************************************************************
2591 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2594 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2595 *****************************************************************************/
2596 static void MRLSections( input_thread_t *p_input, char *psz_source,
2597 int *pi_title_start, int *pi_title_end,
2598 int *pi_chapter_start, int *pi_chapter_end )
2600 char *psz, *psz_end, *psz_next, *psz_check;
2602 *pi_title_start = *pi_title_end = -1;
2603 *pi_chapter_start = *pi_chapter_end = -1;
2605 /* Start by parsing titles and chapters */
2606 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2608 /* Check we are really dealing with a title/chapter section */
2609 psz_check = psz + 1;
2610 if( !*psz_check ) return;
2611 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2612 if( *psz_check != ':' && *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 != '-' && *psz_check ) return;
2616 if( *psz_check == '-' && ++psz_check )
2617 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2618 if( *psz_check != ':' && *psz_check ) return;
2619 if( *psz_check == ':' && ++psz_check )
2620 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2621 if( *psz_check ) return;
2623 /* Separate start and end */
2625 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2627 /* Look for the start title */
2628 *pi_title_start = strtol( psz, &psz_next, 0 );
2629 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2630 *pi_title_end = *pi_title_start;
2633 /* Look for the start chapter */
2635 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2636 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2637 *pi_chapter_end = *pi_chapter_start;
2641 /* Look for the end title */
2642 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2643 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2646 /* Look for the end chapter */
2647 if( *psz_end ) psz_end++;
2648 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2649 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2652 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2653 psz_source, *pi_title_start, *pi_chapter_start,
2654 *pi_title_end, *pi_chapter_end );
2658 /***********************************************************************
2659 * Info management functions
2660 ***********************************************************************/
2662 * Get a info item from a given category in a given input item.
2664 * \param p_i The input item to get info from
2665 * \param psz_cat String representing the category for the info
2666 * \param psz_name String representing the name of the desired info
2667 * \return A pointer to the string with the given info if found, or an
2668 * empty string otherwise. The caller should free the returned
2671 char *vlc_input_item_GetInfo( input_item_t *p_i,
2672 const char *psz_cat,
2673 const char *psz_name )
2677 vlc_mutex_lock( &p_i->lock );
2679 for( i = 0 ; i< p_i->i_categories ; i++ )
2681 info_category_t *p_cat = p_i->pp_categories[i];
2683 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
2686 for( j = 0; j < p_cat->i_infos ; j++ )
2688 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
2690 char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
2691 vlc_mutex_unlock( &p_i->lock );
2696 vlc_mutex_unlock( &p_i->lock );
2697 return strdup( "" );
2700 int vlc_input_item_AddInfo( input_item_t *p_i,
2701 const char *psz_cat,
2702 const char *psz_name,
2703 const char *psz_format, ... )
2707 info_t *p_info = NULL;
2708 info_category_t *p_cat = NULL ;
2710 vlc_mutex_lock( &p_i->lock );
2712 for( i = 0 ; i < p_i->i_categories ; i ++ )
2714 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
2716 p_cat = p_i->pp_categories[i];
2722 if( !(p_cat = (info_category_t *)malloc( sizeof(info_category_t) )) )
2724 vlc_mutex_unlock( &p_i->lock );
2725 return VLC_EGENERIC;
2727 p_cat->psz_name = strdup( psz_cat );
2729 p_cat->pp_infos = 0;
2730 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
2734 for( i = 0; i< p_cat->i_infos; i++ )
2736 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
2738 p_info = p_cat->pp_infos[i];
2745 if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
2747 vlc_mutex_unlock( &p_i->lock );
2748 return VLC_EGENERIC;
2750 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
2751 p_info->psz_name = strdup( psz_name );
2755 if( p_info->psz_value ) free( p_info->psz_value );
2758 va_start( args, psz_format );
2759 vasprintf( &p_info->psz_value, psz_format, args);
2762 vlc_mutex_unlock( &p_i->lock );