1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 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 "input_internal.h"
35 #include "../stream_output/stream_output.h"
37 #include <vlc_playlist.h>
38 #include <vlc_interface.h>
40 #include <vlc_demux.h>
41 #include <vlc_charset.h>
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Run ( input_thread_t *p_input );
51 static int RunAndDestroy ( input_thread_t *p_input );
53 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
54 const char *, vlc_bool_t, sout_instance_t * );
55 static int Init ( input_thread_t *p_input );
56 static void Error ( input_thread_t *p_input );
57 static void End ( input_thread_t *p_input );
58 static void MainLoop( input_thread_t *p_input );
59 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout );
61 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
62 static void ControlReduce( input_thread_t * );
63 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
65 static int UpdateFromAccess( input_thread_t * );
66 static int UpdateFromDemux( input_thread_t * );
68 static void UpdateItemLength( input_thread_t *, int64_t i_length );
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 const char *, const char *psz_forced_demux );
75 static void InputSourceClean( input_source_t * );
77 //static void InputGetAttachments( input_thread_t *, input_source_t * );
79 static void SlaveDemux( input_thread_t *p_input );
80 static void SlaveSeek( input_thread_t *p_input );
82 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
83 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
85 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t * );
86 static void SoutKeep( sout_instance_t * );
88 /*****************************************************************************
89 * This function creates a new input, and returns a pointer
90 * to its description. On error, it returns NULL.
92 * Variables for _public_ use:
95 * - rate,rate-slower, rate-faster
96 * - position, position-offset
98 * - title,title-next,title-prev
99 * - chapter,chapter-next, chapter-prev
100 * - program, audio-es, video-es, spu-es
101 * - audio-delay, spu-delay
106 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
107 * * For intf callback upon changes
109 * TODO explain when Callback is called
110 * TODO complete this list (?)
111 *****************************************************************************/
112 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
113 const char *psz_header, vlc_bool_t b_quick, sout_instance_t *p_sout )
115 input_thread_t *p_input = NULL; /* thread descriptor */
119 /* Allocate descriptor */
120 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
121 if( p_input == NULL )
123 msg_Err( p_parent, "out of memory" );
126 MALLOC_NULL( p_input->p, input_thread_private_t );
128 /* One "randomly" selected input thread is responsible for computing
129 * the global stats. Check if there is already someone doing this */
130 if( p_input->p_libvlc->p_playlist->p_stats && !b_quick )
132 vlc_mutex_lock( &p_input->p_libvlc->p_playlist->p_stats->lock );
133 if( p_input->p_libvlc->p_playlist->p_stats_computer == NULL )
135 p_input->p_libvlc->p_playlist->p_stats_computer = p_input;
137 vlc_mutex_unlock( &p_input->p_libvlc->p_playlist->p_stats->lock );
140 p_input->b_preparsing = b_quick;
141 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
143 /* Init Common fields */
144 p_input->b_eof = VLC_FALSE;
145 p_input->b_can_pace_control = VLC_TRUE;
146 p_input->p->i_start = 0;
148 p_input->p->i_stop = 0;
149 p_input->p->i_run = 0;
150 p_input->p->i_title = 0;
151 p_input->p->title = NULL;
152 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
153 p_input->i_state = INIT_S;
154 p_input->p->i_rate = INPUT_RATE_DEFAULT;
155 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
156 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
157 p_input->p->p_es_out = NULL;
158 p_input->p->p_sout = NULL;
159 p_input->p->b_sout_keep = VLC_FALSE;
160 p_input->p->b_out_pace_control = VLC_FALSE;
161 p_input->i_pts_delay = 0;
163 /* Init Input fields */
164 p_input->p->input.p_item = p_item;
165 p_input->p->input.p_access = NULL;
166 p_input->p->input.p_stream = NULL;
167 p_input->p->input.p_demux = NULL;
168 p_input->p->input.b_title_demux = VLC_FALSE;
169 p_input->p->input.i_title = 0;
170 p_input->p->input.title = NULL;
171 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
172 p_input->p->input.b_can_pace_control = VLC_TRUE;
173 p_input->p->input.b_eof = VLC_FALSE;
174 p_input->p->input.i_cr_average = 0;
176 vlc_mutex_lock( &p_item->lock );
178 if( !p_item->p_stats )
179 p_item->p_stats = stats_NewInputStats( p_input );
180 vlc_mutex_unlock( &p_item->lock );
183 p_input->p->i_slave = 0;
184 p_input->p->slave = NULL;
186 /* Init control buffer */
187 vlc_mutex_init( p_input, &p_input->p->lock_control );
188 p_input->p->i_control = 0;
190 /* Parse input options */
191 vlc_mutex_lock( &p_item->lock );
192 for( i = 0; i < p_item->i_options; i++ )
193 var_OptionParse( p_input, p_item->ppsz_options[i] );
194 vlc_mutex_unlock( &p_item->lock );
196 /* Create Object Variables for private use only */
197 input_ConfigVarInit( p_input );
199 /* Create Objects variables for public Get and Set */
200 input_ControlVarInit( p_input );
202 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
204 if( !p_input->b_preparsing )
206 var_Get( p_input, "bookmarks", &val );
209 /* FIXME: have a common cfg parsing routine used by sout and others */
210 char *psz_parser, *psz_start, *psz_end;
211 psz_parser = val.psz_string;
212 while( (psz_start = strchr( psz_parser, '{' ) ) )
214 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
217 psz_end = strchr( psz_start, '}' );
218 if( !psz_end ) break;
219 psz_parser = psz_end + 1;
220 backup = *psz_parser;
223 while( (psz_end = strchr( psz_start, ',' ) ) )
226 if( !strncmp( psz_start, "name=", 5 ) )
228 p_seekpoint->psz_name = strdup(psz_start + 5);
230 else if( !strncmp( psz_start, "bytes=", 6 ) )
232 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
234 else if( !strncmp( psz_start, "time=", 5 ) )
236 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
239 psz_start = psz_end + 1;
241 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
242 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
243 p_seekpoint->i_time_offset );
244 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
245 vlc_seekpoint_Delete( p_seekpoint );
246 *psz_parser = backup;
248 free( val.psz_string );
252 /* Remove 'Now playing' info as it is probably outdated */
253 input_Control( p_input, INPUT_DEL_INFO,
254 _(VLC_META_INFO_CAT),
255 _(VLC_META_NOW_PLAYING) );
256 input_item_SetNowPlaying( p_item, NULL );
259 if( p_input->b_preparsing )
260 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
264 p_input->p->p_sout = p_sout;
266 /* Attach only once we are ready */
267 vlc_object_attach( p_input, p_parent );
272 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout )
274 vlc_object_detach( p_input );
278 if( p_input->p->p_sout )
281 *pp_sout = p_input->p->p_sout;
282 else if( p_input->p->b_sout_keep )
283 SoutKeep( p_input->p->p_sout );
285 sout_DeleteInstance( p_input->p->p_sout );
288 vlc_mutex_destroy( &p_input->p->lock_control );
291 vlc_object_destroy( p_input );
295 * Initialize an input thread and run it. You will need to monitor the
296 * thread to clean up after it is done
298 * \param p_parent a vlc_object
299 * \param p_item an input item
300 * \return a pointer to the spawned input thread
302 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
303 input_item_t *p_item )
305 vlc_bool_t b_sout_keep;
306 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
307 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
309 if( !p_input && p_sout )
312 p_input->p->b_sout_keep = b_sout_keep;
317 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
318 input_item_t *p_item,
319 const char *psz_log, sout_instance_t *p_sout )
321 input_thread_t *p_input;
323 p_input = Create( p_parent, p_item, psz_log, VLC_FALSE, p_sout );
327 /* Create thread and wait for its readiness. */
328 if( vlc_thread_create( p_input, "input", Run,
329 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
331 input_ChangeState( p_input, ERROR_S );
332 msg_Err( p_input, "cannot create input thread" );
333 Destroy( p_input, &p_sout );
341 * Initialize an input thread and run it. This thread will clean after himself,
342 * you can forget about it. It can work either in blocking or non-blocking mode
344 * \param p_parent a vlc_object
345 * \param p_item an input item
346 * \param b_block should we block until read is finished ?
347 * \return the input object id if non blocking, an error code else
349 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
352 vlc_bool_t b_sout_keep;
353 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
354 input_thread_t *p_input;
356 p_input = Create( p_parent, p_item, NULL, VLC_FALSE, p_sout );
357 if( !p_input && p_sout )
362 p_input->p->b_sout_keep = b_sout_keep;
366 RunAndDestroy( p_input );
371 if( vlc_thread_create( p_input, "input", RunAndDestroy,
372 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
374 input_ChangeState( p_input, ERROR_S );
375 msg_Err( p_input, "cannot create input thread" );
376 Destroy( p_input, NULL );
380 return p_input->i_object_id;
384 * Initialize an input and initialize it to preparse the item
385 * This function is blocking. It will only accept to parse files
387 * \param p_parent a vlc_object_t
388 * \param p_item an input item
389 * \return VLC_SUCCESS or an error
391 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
393 input_thread_t *p_input;
395 /* Allocate descriptor */
396 p_input = Create( p_parent, p_item, NULL, VLC_TRUE, NULL );
400 if( !Init( p_input ) )
403 Destroy( p_input, NULL );
409 * Request a running input thread to stop and die
411 * \param the input thread to stop
413 void input_StopThread( input_thread_t *p_input )
418 /* Set die for input */
419 vlc_object_kill( p_input );
420 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
422 /* We cannot touch p_input fields directly (we come from another thread),
423 * so use the vlc_object_find way, it's perfectly safe */
425 /* Set die for all access */
426 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
427 for( i = 0; i < p_list->i_count; i++ )
429 vlc_object_kill( p_list->p_values[i].p_object );
431 vlc_list_release( p_list );
433 /* Set die for all stream */
434 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
435 for( i = 0; i < p_list->i_count; i++ )
437 vlc_object_kill( p_list->p_values[i].p_object );
439 vlc_list_release( p_list );
441 /* Set die for all demux */
442 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
443 for( i = 0; i < p_list->i_count; i++ )
445 vlc_object_kill( p_list->p_values[i].p_object );
447 vlc_list_release( p_list );
449 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
453 * Clean up a dead input thread
454 * This function does not return until the thread is effectively cancelled.
456 * \param the input thread to kill
458 void input_DestroyThread( input_thread_t *p_input )
460 input_DestroyThreadExtended( p_input, NULL );
463 void input_DestroyThreadExtended( input_thread_t *p_input, sout_instance_t **pp_sout )
465 /* Join the thread */
466 vlc_thread_join( p_input );
469 Destroy( p_input, pp_sout );
472 /*****************************************************************************
473 * Run: main thread loop
474 * This is the "normal" thread that spawns the input processing chain,
475 * reads the stream, cleans up and waits
476 *****************************************************************************/
477 static int Run( input_thread_t *p_input )
479 /* Signal that the thread is launched */
480 vlc_thread_ready( p_input );
482 if( Init( p_input ) )
484 /* If we failed, wait before we are killed, and exit */
485 p_input->b_error = VLC_TRUE;
486 playlist_Signal( pl_Get( p_input ) );
490 /* Tell we're dead */
491 p_input->b_dead = VLC_TRUE;
498 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
500 /* We have finish to demux data but not to play them */
501 while( !p_input->b_die )
503 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
506 msg_Dbg( p_input, "waiting decoder fifos to empty" );
508 msleep( INPUT_IDLE_SLEEP );
511 /* We have finished */
512 p_input->b_eof = VLC_TRUE;
513 playlist_Signal( pl_Get( p_input ) );
516 /* Wait until we are asked to die */
517 if( !p_input->b_die )
528 /*****************************************************************************
529 * RunAndDestroy: main thread loop
530 * This is the "just forget me" thread that spawns the input processing chain,
531 * reads the stream, cleans up and releases memory
532 *****************************************************************************/
533 static int RunAndDestroy( input_thread_t *p_input )
535 /* Signal that the thread is launched */
536 vlc_thread_ready( p_input );
538 if( Init( p_input ) )
543 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
545 /* We have finished demuxing data but not playing it */
546 while( !p_input->b_die )
548 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
551 msg_Dbg( p_input, "waiting decoder fifos to empty" );
553 msleep( INPUT_IDLE_SLEEP );
556 /* We have finished */
557 p_input->b_eof = VLC_TRUE;
565 Destroy( p_input, NULL );
569 /*****************************************************************************
570 * Main loop: Fill buffers from access, and demux
571 *****************************************************************************/
572 static void MainLoop( input_thread_t *p_input )
574 int64_t i_start_mdate = mdate();
575 int64_t i_intf_update = 0;
578 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
580 vlc_bool_t b_force_update = VLC_FALSE;
586 if( p_input->i_state != PAUSE_S )
588 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
589 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
592 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
597 if( p_input->p->input.b_title_demux &&
598 p_input->p->input.p_demux->info.i_update )
600 i_ret = UpdateFromDemux( p_input );
601 b_force_update = VLC_TRUE;
603 else if( !p_input->p->input.b_title_demux &&
604 p_input->p->input.p_access &&
605 p_input->p->input.p_access->info.i_update )
607 i_ret = UpdateFromAccess( p_input );
608 b_force_update = VLC_TRUE;
612 if( i_ret == 0 ) /* EOF */
616 var_Get( p_input, "input-repeat", &repeat );
617 if( repeat.i_int == 0 )
619 /* End of file - we do not set b_die because only the
620 * playlist is allowed to do so. */
621 input_ChangeState( p_input, END_S );
622 msg_Dbg( p_input, "EOF reached" );
623 p_input->p->input.b_eof = VLC_TRUE;
627 msg_Dbg( p_input, "repeating the same input (%d)",
629 if( repeat.i_int > 0 )
632 var_Set( p_input, "input-repeat", repeat );
635 /* Seek to start title/seekpoint */
636 val.i_int = p_input->p->input.i_title_start -
637 p_input->p->input.i_title_offset;
638 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
640 input_ControlPush( p_input,
641 INPUT_CONTROL_SET_TITLE, &val );
643 val.i_int = p_input->p->input.i_seekpoint_start -
644 p_input->p->input.i_seekpoint_offset;
645 if( val.i_int > 0 /* TODO: check upper boundary */ )
646 input_ControlPush( p_input,
647 INPUT_CONTROL_SET_SEEKPOINT, &val );
649 /* Seek to start position */
650 if( p_input->p->i_start > 0 )
652 val.i_time = p_input->p->i_start;
653 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
659 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
664 i_start_mdate = mdate();
669 p_input->b_error = VLC_TRUE;
672 if( i_ret > 0 && p_input->p->i_slave > 0 )
674 SlaveDemux( p_input );
684 vlc_mutex_lock( &p_input->p->lock_control );
685 ControlReduce( p_input );
686 while( !ControlPopNoLock( p_input, &i_type, &val ) )
688 msg_Dbg( p_input, "control type=%d", i_type );
689 if( Control( p_input, i_type, val ) )
690 b_force_update = VLC_TRUE;
692 vlc_mutex_unlock( &p_input->p->lock_control );
694 if( b_force_update || i_intf_update < mdate() )
698 int64_t i_time, i_length;
699 /* update input status variables */
700 if( !demux2_Control( p_input->p->input.p_demux,
701 DEMUX_GET_POSITION, &f_pos ) )
703 val.f_float = (float)f_pos;
704 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
706 if( !demux2_Control( p_input->p->input.p_demux,
707 DEMUX_GET_TIME, &i_time ) )
709 p_input->i_time = i_time;
711 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
713 if( !demux2_Control( p_input->p->input.p_demux,
714 DEMUX_GET_LENGTH, &i_length ) )
717 var_Get( p_input, "length", &old_val );
718 val.i_time = i_length;
719 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
721 if( old_val.i_time != val.i_time )
723 UpdateItemLength( p_input, i_length );
727 var_SetBool( p_input, "intf-change", VLC_TRUE );
728 i_intf_update = mdate() + I64C(150000);
730 /* 150ms * 8 = ~ 1 second */
731 if( ++i_updates % 8 == 0 )
733 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
734 /* Are we the thread responsible for computing global stats ? */
735 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
737 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
738 p_input->p_libvlc->p_playlist->p_stats );
744 static int Init( input_thread_t * p_input )
754 /* Initialize optional stream output. (before access/demuxer)
755 * XXX: we add a special case if the uri starts by vlc.
756 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
757 * be destroyed in case of a file).
758 * (this will break playing of file starting by 'vlc:' but I don't
759 * want to add more logic, just force file by file:// or code it ;)
761 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
762 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
764 if( !p_input->b_preparsing )
766 /* Prepare statistics */
767 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
768 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
769 if( p_input->p_libvlc->b_stats )
771 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
772 INIT_COUNTER( read_packets, INTEGER, COUNTER );
773 INIT_COUNTER( demux_read, INTEGER, COUNTER );
774 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
775 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
776 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
777 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
778 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
779 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
780 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
781 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
782 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
783 p_input->p->counters.p_sout_send_bitrate = NULL;
784 p_input->p->counters.p_sout_sent_packets = NULL;
785 p_input->p->counters.p_sout_sent_bytes = NULL;
786 if( p_input->p->counters.p_demux_bitrate )
787 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
788 if( p_input->p->counters.p_input_bitrate )
789 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
792 /* Find a usable sout and attach it to p_input */
793 psz = var_GetString( p_input, "sout" );
794 if( *psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
796 /* Check the validity of the provided sout */
797 if( p_input->p->p_sout )
799 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
801 msg_Dbg( p_input, "destroying unusable sout" );
803 sout_DeleteInstance( p_input->p->p_sout );
804 p_input->p->p_sout = NULL;
808 if( p_input->p->p_sout )
811 msg_Dbg( p_input, "sout keep: reusing sout" );
812 msg_Dbg( p_input, "sout keep: you probably want to use "
813 "gather stream_out" );
814 vlc_object_attach( p_input->p->p_sout, p_input );
818 /* Create a new one */
819 p_input->p->p_sout = sout_NewInstance( p_input, psz );
821 if( !p_input->p->p_sout )
823 input_ChangeState( p_input, ERROR_S );
824 msg_Err( p_input, "cannot start stream output instance, " \
831 if( p_input->p_libvlc->b_stats )
833 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
834 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
835 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
836 if( p_input->p->counters.p_sout_send_bitrate )
837 p_input->p->counters.p_sout_send_bitrate->update_interval =
841 else if( p_input->p->p_sout )
843 msg_Dbg( p_input, "destroying useless sout" );
845 sout_DeleteInstance( p_input->p->p_sout );
846 p_input->p->p_sout = NULL;
852 p_input->p->p_es_out = input_EsOutNew( p_input );
853 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
854 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
856 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
857 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
859 if( InputSourceInit( p_input, &p_input->p->input,
860 p_input->p->input.p_item->psz_uri, NULL ) )
865 /* Create global title (from master) */
866 if( !p_input->b_preparsing )
868 p_input->p->i_title = p_input->p->input.i_title;
869 p_input->p->title = p_input->p->input.title;
870 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
871 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
872 if( p_input->p->i_title > 0 )
874 /* Setup variables */
875 input_ControlVarNavigation( p_input );
876 input_ControlVarTitle( p_input, 0 );
880 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
881 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
884 if( p_input->i_pts_delay < 0 )
885 p_input->i_pts_delay = 0;
887 /* If the desynchronisation requested by the user is < 0, we need to
888 * cache more data. */
889 var_Get( p_input, "audio-desync", &val );
890 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
892 /* Update cr_average depending on the caching */
893 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
894 p_input->p->input.i_cr_average /= 10;
895 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
898 /* Load master infos */
900 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
901 &val.i_time ) && val.i_time > 0 )
903 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
904 UpdateItemLength( p_input, val.i_time );
906 else if( p_input->p->input.p_item->i_duration > 0 )
907 { /* fallback: gets length from metadata */
908 val.i_time = p_input->p->input.p_item->i_duration;
909 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
910 UpdateItemLength( p_input, val.i_time );
913 /* Start title/chapter */
914 if( !p_input->b_preparsing )
916 val.i_int = p_input->p->input.i_title_start -
917 p_input->p->input.i_title_offset;
918 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
919 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
920 val.i_int = p_input->p->input.i_seekpoint_start -
921 p_input->p->input.i_seekpoint_offset;
922 if( val.i_int > 0 /* TODO: check upper boundary */ )
923 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
927 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
928 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
929 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
930 if( p_input->p->i_run < 0 )
932 msg_Warn( p_input, "invalid run-time ignored" );
933 p_input->p->i_run = 0;
936 if( p_input->p->i_start > 0 )
938 if( p_input->p->i_start >= val.i_time )
940 msg_Warn( p_input, "invalid start-time ignored" );
946 msg_Dbg( p_input, "starting at time: %ds",
947 (int)( p_input->p->i_start / I64C(1000000) ) );
949 s.i_time = p_input->p->i_start;
950 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
953 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
955 msg_Warn( p_input, "invalid stop-time ignored" );
956 p_input->p->i_stop = 0;
960 /* Get fps and set it if not already set */
961 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
964 float f_requested_fps;
966 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
967 var_SetFloat( p_input, "sub-original-fps", f_fps );
969 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
970 if( f_requested_fps != f_fps )
972 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
974 var_SetFloat( p_input, "sub-fps", f_requested_fps );
978 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
981 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
984 /* Look for and add subtitle files */
985 psz_subtitle = var_GetString( p_input, "sub-file" );
988 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
989 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
992 var_Get( p_input, "sub-autodetect-file", &val );
995 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
996 char **subs = subtitles_Detect( p_input, psz_autopath,
997 p_input->p->input.p_item->psz_uri );
1001 /* Try to autoselect the first autodetected subtitles file
1002 * if no subtitles file was specified */
1003 if( *psz_subtitle == 0 && subs && subs[0] )
1005 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
1010 /* Then, just add the following subtitles files */
1011 for( ; subs && subs[i]; i++ )
1013 if( strcmp( psz_subtitle, subs[i] ) )
1015 sub = InputSourceNew( p_input );
1016 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1018 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1024 if( subs ) free( subs );
1025 if( psz_autopath ) free( psz_autopath );
1027 free( psz_subtitle );
1029 /* Look for slave */
1030 psz = var_GetString( p_input, "input-slave" );
1034 input_source_t *slave;
1035 while( psz && *psz )
1037 while( *psz == ' ' || *psz == '#' )
1041 if( ( psz_delim = strchr( psz, '#' ) ) )
1043 *psz_delim++ = '\0';
1050 msg_Dbg( p_input, "adding slave input '%s'", psz );
1051 slave = InputSourceNew( p_input );
1052 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1054 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1060 if( psz ) free( psz );
1064 p_input->p->i_start = 0;
1065 p_input->p->i_start = 0;
1069 if( !p_input->b_preparsing )
1071 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1072 i_es_out_mode = ES_OUT_MODE_AUTO;
1074 if( p_input->p->p_sout )
1076 var_Get( p_input, "sout-all", &val );
1079 i_es_out_mode = ES_OUT_MODE_ALL;
1084 var_Get( p_input, "programs", &val );
1085 if ( val.p_list && val.p_list->i_count )
1087 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1088 /* Note : we should remove the "program" callback. */
1091 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1095 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1097 /* Inform the demuxer about waited group (needed only for DVB) */
1098 if( i_es_out_mode == ES_OUT_MODE_ALL )
1100 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1102 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1104 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1109 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1110 (int) var_GetInteger( p_input, "program" ), NULL );
1113 if( p_input->p->p_sout )
1115 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1117 p_input->p->b_out_pace_control = VLC_FALSE;
1121 p_input->p->b_out_pace_control = VLC_TRUE;
1124 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1126 /* We don't want a high input priority here or we'll
1127 * end-up sucking up all the CPU time */
1128 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1131 msg_Dbg( p_input, "starting in %s mode",
1132 p_input->p->b_out_pace_control ? "async" : "sync" );
1136 p_meta = vlc_meta_New();
1137 /* Get meta data from users */
1138 InputMetaUser( p_input, p_meta );
1140 /* Get meta data from master input */
1141 demux2_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
1143 /* Access_file does not give any meta, and there are no slave */
1144 if( !p_input->b_preparsing )
1146 if( p_input->p->input.p_access )
1147 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1150 /* Get meta data from slave input */
1151 for( i = 0; i < p_input->p->i_slave; i++ )
1153 demux2_Control( p_input->p->slave[i]->p_demux,
1154 DEMUX_GET_META, p_meta );
1155 if( p_input->p->slave[i]->p_access )
1157 access2_Control( p_input->p->slave[i]->p_access,
1158 ACCESS_GET_META, p_meta );
1163 InputUpdateMeta( p_input, p_meta );
1165 if( !p_input->b_preparsing )
1167 msg_Dbg( p_input, "`%s' successfully opened",
1168 p_input->p->input.p_item->psz_uri );
1172 /* initialization is complete */
1173 input_ChangeState( p_input, PLAYING_S );
1178 input_ChangeState( p_input, ERROR_S );
1180 if( p_input->p->p_es_out )
1181 input_EsOutDelete( p_input->p->p_es_out );
1183 if( p_input->p->p_sout )
1185 vlc_object_detach( p_input->p->p_sout );
1186 sout_DeleteInstance( p_input->p->p_sout );
1190 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1192 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1193 stats_CounterClean( p_input->p->counters.p_##c );\
1194 p_input->p->counters.p_##c = NULL; } while(0)
1195 EXIT_COUNTER( read_bytes );
1196 EXIT_COUNTER( read_packets );
1197 EXIT_COUNTER( demux_read );
1198 EXIT_COUNTER( input_bitrate );
1199 EXIT_COUNTER( demux_bitrate );
1200 EXIT_COUNTER( played_abuffers );
1201 EXIT_COUNTER( lost_abuffers );
1202 EXIT_COUNTER( displayed_pictures );
1203 EXIT_COUNTER( lost_pictures );
1204 EXIT_COUNTER( decoded_audio );
1205 EXIT_COUNTER( decoded_video );
1206 EXIT_COUNTER( decoded_sub );
1208 if( p_input->p->p_sout )
1210 EXIT_COUNTER( sout_sent_packets );
1211 EXIT_COUNTER (sout_sent_bytes );
1212 EXIT_COUNTER( sout_send_bitrate );
1217 /* Mark them deleted */
1218 p_input->p->input.p_demux = NULL;
1219 p_input->p->input.p_stream = NULL;
1220 p_input->p->input.p_access = NULL;
1221 p_input->p->p_es_out = NULL;
1222 p_input->p->p_sout = NULL;
1224 return VLC_EGENERIC;
1227 /*****************************************************************************
1228 * Error: RunThread() error loop
1229 *****************************************************************************
1230 * This function is called when an error occurred during thread main's loop.
1231 *****************************************************************************/
1232 static void Error( input_thread_t *p_input )
1234 while( !p_input->b_die )
1237 input_ChangeState( p_input, ERROR_S );
1238 msleep( INPUT_IDLE_SLEEP );
1242 /*****************************************************************************
1243 * End: end the input thread
1244 *****************************************************************************/
1245 static void End( input_thread_t * p_input )
1249 /* We are at the end */
1250 input_ChangeState( p_input, END_S );
1252 /* Clean control variables */
1253 input_ControlVarClean( p_input );
1255 /* Clean up master */
1256 InputSourceClean( &p_input->p->input );
1259 for( i = 0; i < p_input->p->i_slave; i++ )
1261 InputSourceClean( p_input->p->slave[i] );
1262 free( p_input->p->slave[i] );
1264 if( p_input->p->slave ) free( p_input->p->slave );
1266 /* Unload all modules */
1267 if( p_input->p->p_es_out )
1268 input_EsOutDelete( p_input->p->p_es_out );
1270 if( !p_input->b_preparsing )
1272 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1273 if( p_input->p_libvlc->b_stats )
1275 /* make sure we are up to date */
1276 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1277 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1279 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1280 p_input->p_libvlc->p_playlist->p_stats );
1281 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1283 CL_CO( read_bytes );
1284 CL_CO( read_packets );
1285 CL_CO( demux_read );
1286 CL_CO( input_bitrate );
1287 CL_CO( demux_bitrate );
1288 CL_CO( played_abuffers );
1289 CL_CO( lost_abuffers );
1290 CL_CO( displayed_pictures );
1291 CL_CO( lost_pictures );
1292 CL_CO( decoded_audio) ;
1293 CL_CO( decoded_video );
1294 CL_CO( decoded_sub) ;
1297 /* Close optional stream output instance */
1298 if( p_input->p->p_sout )
1300 CL_CO( sout_sent_packets );
1301 CL_CO( sout_sent_bytes );
1302 CL_CO( sout_send_bitrate );
1304 vlc_object_detach( p_input->p->p_sout );
1309 if( p_input->p->i_attachment > 0 )
1311 for( i = 0; i < p_input->p->i_attachment; i++ )
1312 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1313 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1316 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1318 /* Tell we're dead */
1319 p_input->b_dead = VLC_TRUE;
1322 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t *pb_sout_keep )
1324 vlc_bool_t b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1325 sout_instance_t *p_sout = NULL;
1328 /* Search sout-keep options
1329 * XXX it has to be done here, but it is duplicated work :( */
1330 vlc_mutex_lock( &p_item->lock );
1331 for( i = 0; i < p_item->i_options; i++ )
1333 const char *psz_option = p_item->ppsz_options[i];
1336 if( *psz_option == ':' )
1339 if( !strcmp( psz_option, "sout-keep" ) )
1340 b_keep_sout = VLC_TRUE;
1341 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1342 b_keep_sout = VLC_FALSE;
1344 vlc_mutex_unlock( &p_item->lock );
1346 /* Find a potential sout to reuse
1347 * XXX it might be unusable but this will be checked later */
1350 /* Remove the sout from the playlist garbage collector */
1351 playlist_t *p_playlist = pl_Yield( p_parent );
1353 vlc_mutex_lock( &p_playlist->gc_lock );
1354 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1357 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1359 vlc_object_release( p_sout );
1364 vlc_object_detach( p_sout ); /* Remove it from the GC */
1366 vlc_object_release( p_sout );
1369 vlc_mutex_unlock( &p_playlist->gc_lock );
1371 pl_Release( p_parent );
1375 *pb_sout_keep = b_keep_sout;
1379 static void SoutKeep( sout_instance_t *p_sout )
1381 /* attach sout to the playlist */
1382 playlist_t *p_playlist = pl_Yield( p_sout );
1384 msg_Dbg( p_sout, "sout has been kept" );
1385 vlc_object_attach( p_sout, p_playlist );
1387 pl_Release( p_sout );
1390 /*****************************************************************************
1392 *****************************************************************************/
1393 static inline int ControlPopNoLock( input_thread_t *p_input,
1394 int *pi_type, vlc_value_t *p_val )
1396 if( p_input->p->i_control <= 0 )
1398 return VLC_EGENERIC;
1401 *pi_type = p_input->p->control[0].i_type;
1402 *p_val = p_input->p->control[0].val;
1404 p_input->p->i_control--;
1405 if( p_input->p->i_control > 0 )
1409 for( i = 0; i < p_input->p->i_control; i++ )
1411 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1412 p_input->p->control[i].val = p_input->p->control[i+1].val;
1419 static void ControlReduce( input_thread_t *p_input )
1426 for( i = 1; i < p_input->p->i_control; i++ )
1428 const int i_lt = p_input->p->control[i-1].i_type;
1429 const int i_ct = p_input->p->control[i].i_type;
1431 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1432 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1436 ( i_ct == INPUT_CONTROL_SET_STATE ||
1437 i_ct == INPUT_CONTROL_SET_RATE ||
1438 i_ct == INPUT_CONTROL_SET_POSITION ||
1439 i_ct == INPUT_CONTROL_SET_TIME ||
1440 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1441 i_ct == INPUT_CONTROL_SET_TITLE ||
1442 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1443 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1446 // msg_Dbg( p_input, "merged at %d", i );
1447 /* Remove the i-1 */
1448 for( j = i; j < p_input->p->i_control; j++ )
1449 p_input->p->control[j-1] = p_input->p->control[j];
1450 p_input->p->i_control--;
1454 /* TODO but that's not that important
1455 - merge SET_X with SET_X_CMD
1456 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1457 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1464 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1467 vlc_bool_t b_force_update = VLC_FALSE;
1469 if( !p_input ) return b_force_update;
1473 case INPUT_CONTROL_SET_DIE:
1474 msg_Dbg( p_input, "control: stopping input" );
1475 /* Mark all submodules to die */
1476 if( p_input->p->input.p_access )
1477 vlc_object_kill( p_input->p->input.p_access );
1478 if( p_input->p->input.p_stream )
1479 vlc_object_kill( p_input->p->input.p_stream );
1480 vlc_object_kill( p_input->p->input.p_demux );
1482 vlc_object_kill( p_input );
1485 case INPUT_CONTROL_SET_POSITION:
1486 case INPUT_CONTROL_SET_POSITION_OFFSET:
1489 if( i_type == INPUT_CONTROL_SET_POSITION )
1491 f_pos = val.f_float;
1495 /* Should not fail */
1496 demux2_Control( p_input->p->input.p_demux,
1497 DEMUX_GET_POSITION, &f_pos );
1498 f_pos += val.f_float;
1500 if( f_pos < 0.0 ) f_pos = 0.0;
1501 if( f_pos > 1.0 ) f_pos = 1.0;
1502 /* Reset the decoders states and clock sync (before calling the demuxer */
1503 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1504 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1505 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1508 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1509 "%2.1f%% failed", f_pos * 100 );
1513 if( p_input->p->i_slave > 0 )
1514 SlaveSeek( p_input );
1516 b_force_update = VLC_TRUE;
1521 case INPUT_CONTROL_SET_TIME:
1522 case INPUT_CONTROL_SET_TIME_OFFSET:
1527 if( i_type == INPUT_CONTROL_SET_TIME )
1529 i_time = val.i_time;
1533 /* Should not fail */
1534 demux2_Control( p_input->p->input.p_demux,
1535 DEMUX_GET_TIME, &i_time );
1536 i_time += val.i_time;
1538 if( i_time < 0 ) i_time = 0;
1540 /* Reset the decoders states and clock sync (before calling the demuxer */
1541 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1542 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1544 i_ret = demux2_Control( p_input->p->input.p_demux,
1545 DEMUX_SET_TIME, i_time );
1550 /* Emulate it with a SET_POS */
1551 demux2_Control( p_input->p->input.p_demux,
1552 DEMUX_GET_LENGTH, &i_length );
1555 double f_pos = (double)i_time / (double)i_length;
1556 i_ret = demux2_Control( p_input->p->input.p_demux,
1557 DEMUX_SET_POSITION, f_pos );
1562 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1563 " failed or not possible", i_time );
1567 if( p_input->p->i_slave > 0 )
1568 SlaveSeek( p_input );
1570 b_force_update = VLC_TRUE;
1575 case INPUT_CONTROL_SET_STATE:
1576 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1577 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1580 if( p_input->p->input.p_access )
1581 i_ret = access2_Control( p_input->p->input.p_access,
1582 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1584 i_ret = demux2_Control( p_input->p->input.p_demux,
1585 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1589 /* FIXME What to do ? */
1590 msg_Warn( p_input, "cannot unset pause -> EOF" );
1591 vlc_mutex_unlock( &p_input->p->lock_control );
1592 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1593 vlc_mutex_lock( &p_input->p->lock_control );
1596 b_force_update = VLC_TRUE;
1598 /* Switch to play */
1599 p_input->i_state = PLAYING_S;
1600 val.i_int = PLAYING_S;
1601 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1604 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1606 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1607 p_input->p->b_can_pause )
1610 if( p_input->p->input.p_access )
1611 i_ret = access2_Control( p_input->p->input.p_access,
1612 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1614 i_ret = demux2_Control( p_input->p->input.p_demux,
1615 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1617 b_force_update = VLC_TRUE;
1621 msg_Warn( p_input, "cannot set pause state" );
1622 val.i_int = p_input->i_state;
1626 val.i_int = PAUSE_S;
1629 /* Switch to new state */
1630 p_input->i_state = val.i_int;
1631 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1633 /* Send discontinuity to decoders (it will allow them to flush
1635 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE, VLC_FALSE );
1637 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1639 b_force_update = VLC_TRUE;
1641 /* Correct "state" value */
1642 val.i_int = p_input->i_state;
1643 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1645 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1647 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1651 case INPUT_CONTROL_SET_RATE:
1652 case INPUT_CONTROL_SET_RATE_SLOWER:
1653 case INPUT_CONTROL_SET_RATE_FASTER:
1657 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1658 i_rate = p_input->p->i_rate * 2;
1659 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1660 i_rate = p_input->p->i_rate / 2;
1664 if( i_rate < INPUT_RATE_MIN )
1666 msg_Dbg( p_input, "cannot set rate faster" );
1667 i_rate = INPUT_RATE_MIN;
1669 else if( i_rate > INPUT_RATE_MAX )
1671 msg_Dbg( p_input, "cannot set rate slower" );
1672 i_rate = INPUT_RATE_MAX;
1674 if( i_rate != INPUT_RATE_DEFAULT &&
1675 ( !p_input->b_can_pace_control ||
1676 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1678 msg_Dbg( p_input, "cannot change rate" );
1679 i_rate = INPUT_RATE_DEFAULT;
1681 if( i_rate != p_input->p->i_rate )
1684 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1686 input_EsOutDiscontinuity( p_input->p->p_es_out,
1687 VLC_FALSE, VLC_FALSE );
1689 p_input->p->i_rate = i_rate;
1691 input_EsOutSetRate( p_input->p->p_es_out );
1693 b_force_update = VLC_TRUE;
1698 case INPUT_CONTROL_SET_PROGRAM:
1699 /* No need to force update, es_out does it if needed */
1700 es_out_Control( p_input->p->p_es_out,
1701 ES_OUT_SET_GROUP, val.i_int );
1703 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1707 case INPUT_CONTROL_SET_ES:
1708 /* No need to force update, es_out does it if needed */
1709 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1710 input_EsOutGetFromID( p_input->p->p_es_out,
1714 case INPUT_CONTROL_SET_AUDIO_DELAY:
1715 input_EsOutSetDelay( p_input->p->p_es_out,
1716 AUDIO_ES, val.i_time );
1717 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1720 case INPUT_CONTROL_SET_SPU_DELAY:
1721 input_EsOutSetDelay( p_input->p->p_es_out,
1722 SPU_ES, val.i_time );
1723 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1726 case INPUT_CONTROL_SET_TITLE:
1727 case INPUT_CONTROL_SET_TITLE_NEXT:
1728 case INPUT_CONTROL_SET_TITLE_PREV:
1729 if( p_input->p->input.b_title_demux &&
1730 p_input->p->input.i_title > 0 )
1733 /* FIXME handle demux title */
1734 demux_t *p_demux = p_input->p->input.p_demux;
1737 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1738 i_title = p_demux->info.i_title - 1;
1739 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1740 i_title = p_demux->info.i_title + 1;
1742 i_title = val.i_int;
1744 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1746 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1747 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1749 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1750 input_ControlVarTitle( p_input, i_title );
1753 else if( p_input->p->input.i_title > 0 )
1755 access_t *p_access = p_input->p->input.p_access;
1758 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1759 i_title = p_access->info.i_title - 1;
1760 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1761 i_title = p_access->info.i_title + 1;
1763 i_title = val.i_int;
1765 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1767 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1768 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1770 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1771 stream_AccessReset( p_input->p->input.p_stream );
1775 case INPUT_CONTROL_SET_SEEKPOINT:
1776 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1777 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1778 if( p_input->p->input.b_title_demux &&
1779 p_input->p->input.i_title > 0 )
1781 demux_t *p_demux = p_input->p->input.p_demux;
1783 int64_t i_input_time;
1784 int64_t i_seekpoint_time;
1786 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1788 i_seekpoint = p_demux->info.i_seekpoint;
1789 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1790 if( i_seekpoint_time >= 0 &&
1791 !demux2_Control( p_demux,
1792 DEMUX_GET_TIME, &i_input_time ) )
1794 if ( i_input_time < i_seekpoint_time + 3000000 )
1800 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1801 i_seekpoint = p_demux->info.i_seekpoint + 1;
1803 i_seekpoint = val.i_int;
1805 if( i_seekpoint >= 0 && i_seekpoint <
1806 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1808 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1809 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1811 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1814 else if( p_input->p->input.i_title > 0 )
1816 demux_t *p_demux = p_input->p->input.p_demux;
1817 access_t *p_access = p_input->p->input.p_access;
1819 int64_t i_input_time;
1820 int64_t i_seekpoint_time;
1822 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1824 i_seekpoint = p_access->info.i_seekpoint;
1825 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1826 if( i_seekpoint_time >= 0 &&
1827 demux2_Control( p_demux,
1828 DEMUX_GET_TIME, &i_input_time ) )
1830 if ( i_input_time < i_seekpoint_time + 3000000 )
1836 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1837 i_seekpoint = p_access->info.i_seekpoint + 1;
1839 i_seekpoint = val.i_int;
1841 if( i_seekpoint >= 0 && i_seekpoint <
1842 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1844 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1845 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1847 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1849 stream_AccessReset( p_input->p->input.p_stream );
1854 case INPUT_CONTROL_ADD_SLAVE:
1855 if( val.psz_string )
1857 input_source_t *slave = InputSourceNew( p_input );
1859 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1865 msg_Dbg( p_input, "adding %s as slave on the fly",
1869 if( demux2_Control( p_input->p->input.p_demux,
1870 DEMUX_GET_TIME, &i_time ) )
1872 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1873 InputSourceClean( slave );
1877 if( demux2_Control( slave->p_demux,
1878 DEMUX_SET_TIME, i_time ) )
1880 msg_Err( p_input, "seek failed for new slave" );
1881 InputSourceClean( slave );
1886 /* Get meta (access and demux) */
1887 p_meta = vlc_meta_New();
1888 access2_Control( slave->p_access, ACCESS_GET_META,
1890 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1891 InputUpdateMeta( p_input, p_meta );
1893 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1898 msg_Warn( p_input, "failed to add %s as slave",
1902 free( val.psz_string );
1906 case INPUT_CONTROL_SET_BOOKMARK:
1908 msg_Err( p_input, "not yet implemented" );
1912 return b_force_update;
1915 /*****************************************************************************
1917 *****************************************************************************/
1918 static int UpdateFromDemux( input_thread_t *p_input )
1920 demux_t *p_demux = p_input->p->input.p_demux;
1923 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1925 v.i_int = p_demux->info.i_title;
1926 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1928 input_ControlVarTitle( p_input, p_demux->info.i_title );
1930 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1932 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1934 v.i_int = p_demux->info.i_seekpoint;
1935 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1937 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1939 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1941 /* Hmmm only works with master input */
1942 if( p_input->p->input.p_demux == p_demux )
1944 int i_title_end = p_input->p->input.i_title_end -
1945 p_input->p->input.i_title_offset;
1946 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1947 p_input->p->input.i_seekpoint_offset;
1949 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1951 if( p_demux->info.i_title > i_title_end ||
1952 ( p_demux->info.i_title == i_title_end &&
1953 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1955 else if( i_seekpoint_end >=0 )
1957 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1959 else if( i_title_end >= 0 )
1961 if( p_demux->info.i_title > i_title_end ) return 0;
1968 /*****************************************************************************
1970 *****************************************************************************/
1971 static int UpdateFromAccess( input_thread_t *p_input )
1973 access_t *p_access = p_input->p->input.p_access;
1976 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1978 v.i_int = p_access->info.i_title;
1979 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1981 input_ControlVarTitle( p_input, p_access->info.i_title );
1983 stream_AccessUpdate( p_input->p->input.p_stream );
1985 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1987 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1989 v.i_int = p_access->info.i_seekpoint;
1990 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1991 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1993 if( p_access->info.i_update & INPUT_UPDATE_META )
1995 /* TODO maybe multi - access ? */
1996 vlc_meta_t *p_meta = vlc_meta_New();
1997 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
1998 InputUpdateMeta( p_input, p_meta );
1999 var_SetBool( p_input, "item-change", p_input->p->input.p_item->i_id );
2000 p_access->info.i_update &= ~INPUT_UPDATE_META;
2003 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2005 /* Hmmm only works with master input */
2006 if( p_input->p->input.p_access == p_access )
2008 int i_title_end = p_input->p->input.i_title_end -
2009 p_input->p->input.i_title_offset;
2010 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2011 p_input->p->input.i_seekpoint_offset;
2013 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2015 if( p_access->info.i_title > i_title_end ||
2016 ( p_access->info.i_title == i_title_end &&
2017 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2019 else if( i_seekpoint_end >=0 )
2021 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2023 else if( i_title_end >= 0 )
2025 if( p_access->info.i_title > i_title_end ) return 0;
2032 /*****************************************************************************
2034 *****************************************************************************/
2035 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2037 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2038 p_input->p->input.p_item->i_duration = i_length;
2039 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2041 if( !p_input->b_preparsing )
2043 pl_Yield( p_input );
2044 var_SetInteger( pl_Get( p_input ), "item-change",
2045 p_input->p->input.p_item->i_id );
2046 pl_Release( p_input )
2050 /*****************************************************************************
2052 *****************************************************************************/
2053 static input_source_t *InputSourceNew( input_thread_t *p_input )
2055 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2059 msg_Err( p_input, "out of memory for new input source" );
2064 in->p_access = NULL;
2065 in->p_stream = NULL;
2067 in->b_title_demux = VLC_FALSE;
2068 TAB_INIT( in->i_title, in->title );
2069 in->b_can_pace_control = VLC_TRUE;
2070 in->b_eof = VLC_FALSE;
2071 in->i_cr_average = 0;
2076 /*****************************************************************************
2078 *****************************************************************************/
2079 static int InputSourceInit( input_thread_t *p_input,
2080 input_source_t *in, const char *psz_mrl,
2081 const char *psz_forced_demux )
2083 char psz_dup[strlen (psz_mrl) + 1];
2084 const char *psz_access;
2085 const char *psz_demux;
2091 strcpy( psz_dup, psz_mrl );
2093 if( !in ) return VLC_EGENERIC;
2094 if( !p_input ) return VLC_EGENERIC;
2097 if( !p_input->b_preparsing )
2099 MRLSplit( VLC_OBJECT(p_input), psz_dup,
2100 &psz_access, &psz_demux, &psz_path );
2102 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2103 psz_mrl, psz_access, psz_demux, psz_path );
2105 /* Hack to allow udp://@:port syntax */
2107 (strncmp( psz_access, "udp", 3 ) &&
2108 strncmp( psz_access, "rtp", 3 )) )
2110 /* Find optional titles and seekpoints */
2111 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2112 &in->i_seekpoint_start, &in->i_seekpoint_end );
2114 if( psz_forced_demux && *psz_forced_demux )
2116 psz_demux = psz_forced_demux;
2118 else if( !psz_demux || *psz_demux == '\0' )
2120 /* special hack for forcing a demuxer with --demux=module
2121 * (and do nothing with a list) */
2122 char *psz_var_demux = var_GetString( p_input, "demux" );
2124 if( *psz_var_demux != '\0' &&
2125 !strchr(psz_var_demux, ',' ) &&
2126 !strchr(psz_var_demux, ':' ) )
2128 psz_demux = psz_var_demux;
2130 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2132 else if( psz_var_demux )
2134 free( psz_var_demux );
2138 /* Try access_demux if no demux given */
2139 if( *psz_demux == '\0' )
2141 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2142 NULL, p_input->p->p_es_out, VLC_FALSE );
2148 if( !strncmp( psz_path, "file://", 7 ) )
2150 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2152 psz_access = "file";
2157 int64_t i_pts_delay;
2159 /* Get infos from access_demux */
2160 demux2_Control( in->p_demux,
2161 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2162 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2164 in->b_title_demux = VLC_TRUE;
2165 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2166 &in->title, &in->i_title,
2167 &in->i_title_offset, &in->i_seekpoint_offset ) )
2172 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2173 &in->b_can_pace_control );
2174 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2178 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2184 int64_t i_pts_delay;
2186 input_ChangeState( p_input, OPENING_S );
2188 /* Now try a real access */
2189 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2190 p_input->b_preparsing );
2192 /* Access failed, URL encoded ? */
2193 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2195 decode_URI( psz_path );
2197 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2198 psz_access, psz_demux, psz_path );
2200 in->p_access = access2_New( p_input,
2201 psz_access, psz_demux, psz_path,
2202 p_input->b_preparsing );
2204 /* access failed, maybe our access detection was wrong.
2205 * Retry with the full name */
2206 if( in->p_access == NULL && strchr( psz_mrl, ':' ) )
2208 msg_Dbg( p_input, "retrying with access `' demux `' path `%s'",
2210 in->p_access = access2_New( p_input,
2212 p_input->b_preparsing );
2214 if( in->p_access == NULL )
2216 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2218 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2219 _("Your input can't be opened"),
2220 _("VLC is unable to open the MRL '%s'."
2221 " Check the log for details."), psz_mrl );
2226 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2227 while( psz && *psz )
2229 access_t *p_access = in->p_access;
2230 char *end = strchr( psz, ':' );
2235 in->p_access = access2_FilterNew( in->p_access, psz );
2236 if( in->p_access == NULL )
2238 in->p_access = p_access;
2239 msg_Warn( p_input, "failed to insert access filter %s",
2245 if( psz_tmp ) free( psz_tmp );
2247 /* Get infos from access */
2248 if( !p_input->b_preparsing )
2250 access2_Control( in->p_access,
2251 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2252 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2254 in->b_title_demux = VLC_FALSE;
2255 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2256 &in->title, &in->i_title,
2257 &in->i_title_offset, &in->i_seekpoint_offset ) )
2263 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2264 &in->b_can_pace_control );
2265 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2267 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2269 var_Set( p_input, "seekable", val );
2272 input_ChangeState( p_input, BUFFERING_S );
2274 /* Create the stream_t */
2275 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2276 if( in->p_stream == NULL )
2278 msg_Warn( p_input, "cannot create a stream_t from access" );
2282 /* Open a demuxer */
2283 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2285 psz_demux = in->p_access->psz_demux;
2289 /* Take access redirections into account */
2290 char *psz_real_path;
2291 char *psz_buf = NULL;
2292 if( in->p_access->psz_path )
2294 const char *psz_a, *psz_d;
2295 psz_buf = strdup( in->p_access->psz_path );
2296 MRLSplit( VLC_OBJECT(p_input), psz_buf,
2297 &psz_a, &psz_d, &psz_real_path );
2301 psz_real_path = psz_path;
2303 in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2305 in->p_stream, p_input->p->p_es_out,
2306 p_input->b_preparsing );
2310 if( in->p_demux == NULL )
2312 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2313 psz_access, psz_demux, psz_path );
2314 intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2315 _("Can't recognize the input's format"),
2316 _("The format of '%s' can't be detected. "
2317 "Have a look the log for details."), psz_mrl );
2321 /* Get title from demux */
2322 if( !p_input->b_preparsing && in->i_title <= 0 )
2324 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2325 &in->title, &in->i_title,
2326 &in->i_title_offset, &in->i_seekpoint_offset ))
2328 TAB_INIT( in->i_title, in->title );
2332 in->b_title_demux = VLC_TRUE;
2336 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2337 if( 1 || !p_input->b_preparsing )
2340 input_attachment_t **attachment;
2341 if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2342 &attachment, &i_attachment ) )
2345 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2346 p_input->p->attachment = realloc( p_input->p->attachment,
2347 sizeof(input_attachment_t**) * ( p_input->p->i_attachment + i_attachment ) );
2348 for( i = 0; i < i_attachment; i++ )
2349 p_input->p->attachment[p_input->p->i_attachment++] = attachment[i];
2352 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2357 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2358 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2363 input_ChangeState( p_input, ERROR_S );
2366 demux2_Delete( in->p_demux );
2369 stream_Delete( in->p_stream );
2372 access2_Delete( in->p_access );
2374 return VLC_EGENERIC;
2377 /*****************************************************************************
2379 *****************************************************************************/
2380 static void InputSourceClean( input_source_t *in )
2385 demux2_Delete( in->p_demux );
2388 stream_Delete( in->p_stream );
2391 access2_Delete( in->p_access );
2393 if( in->i_title > 0 )
2395 for( i = 0; i < in->i_title; i++ )
2396 vlc_input_title_Delete( in->title[i] );
2397 TAB_CLEAN( in->i_title, in->title );
2401 static void SlaveDemux( input_thread_t *p_input )
2406 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2408 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2412 for( i = 0; i < p_input->p->i_slave; i++ )
2414 input_source_t *in = p_input->p->slave[i];
2420 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2425 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2427 msg_Err( p_input, "slave[%d] doesn't like "
2428 "DEMUX_GET_TIME -> EOF", i );
2433 if( i_stime >= i_time )
2436 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2442 i_ret = in->p_demux->pf_demux( in->p_demux );
2447 msg_Dbg( p_input, "slave %d EOF", i );
2448 in->b_eof = VLC_TRUE;
2453 static void SlaveSeek( input_thread_t *p_input )
2458 if( !p_input ) return;
2460 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2462 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2466 for( i = 0; i < p_input->p->i_slave; i++ )
2468 input_source_t *in = p_input->p->slave[i];
2470 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2472 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2473 in->b_eof = VLC_TRUE;
2478 /*****************************************************************************
2480 *****************************************************************************/
2481 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2485 if( !p_meta ) return;
2487 /* Get meta information from user */
2488 #define GET_META( field, s ) \
2489 var_Get( p_input, (s), &val ); \
2490 if( *val.psz_string ) \
2491 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2492 free( val.psz_string )
2494 GET_META( Title, "meta-title" );
2495 GET_META( Artist, "meta-artist" );
2496 GET_META( Genre, "meta-genre" );
2497 GET_META( Copyright, "meta-copyright" );
2498 GET_META( Description, "meta-description" );
2499 GET_META( Date, "meta-date" );
2500 GET_META( URL, "meta-url" );
2504 /*****************************************************************************
2505 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2506 * arturl and locking issue.
2507 *****************************************************************************/
2508 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2510 input_item_t *p_item = p_input->p->input.p_item;
2511 char * psz_arturl = NULL;
2512 char *psz_title = NULL;
2514 int i_arturl_event = VLC_FALSE;
2519 psz_arturl = input_item_GetArtURL( p_item );
2521 vlc_mutex_lock( &p_item->lock );
2522 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2523 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2525 vlc_meta_Merge( p_item->p_meta, p_meta );
2527 if( psz_arturl && *psz_arturl )
2529 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2530 i_arturl_event = VLC_TRUE;
2533 vlc_meta_Delete( p_meta );
2535 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2537 /* Don't look for art cover if sout
2538 * XXX It can change when sout has meta data support */
2539 if( p_input->p->p_sout && !p_input->b_preparsing )
2541 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2542 i_arturl_event = VLC_TRUE;
2546 input_ExtractAttachmentAndCacheArt( p_input );
2550 input_item_SetPreparsed( p_item, VLC_TRUE );
2554 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2556 p_meta = vlc_meta_New();
2557 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2559 vlc_mutex_unlock( &p_item->lock );
2561 if( i_arturl_event == VLC_TRUE )
2565 /* Notify interested third parties */
2566 event.type = vlc_InputItemMetaChanged;
2567 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2568 vlc_event_send( &p_item->event_manager, &event );
2573 input_Control( p_input, INPUT_SET_NAME, psz_title );
2579 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2580 for( i = 0; ppsz_all_keys[i]; i++ )
2582 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2583 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2584 free( ppsz_all_keys[i] );
2586 free( ppsz_all_keys );
2587 vlc_meta_Delete( p_meta );
2590 /** \todo handle sout meta */
2594 static inline vlc_bool_t IsValidAccess( const char *psz )
2598 while( ( c = *psz ) != '\0' )
2603 if( ( !isascii( c ) || !isalnum( c ) ) && c != '-' && ( c != '/' ) )
2607 /* should not happen though */
2612 /*****************************************************************************
2613 * MRLSplit: parse the access, demux and url part of the
2614 * Media Resource Locator.
2615 *****************************************************************************/
2616 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2617 const char **ppsz_access, const char **ppsz_demux,
2620 const char *psz_access = "";
2621 const char *psz_demux = "";
2625 psz = strchr( psz_dup, ':' );
2629 /* Guess whether ':' is part of a local filename, or separates
2630 * access/demux from path */
2631 if( !IsValidAccess( psz_dup ) )
2633 #if defined( WIN32 ) || defined( UNDER_CE )
2634 else if( ( psz - psz_dup == 1 ) && isalpha( psz_dup[0] ) )
2636 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2645 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2649 psz = strchr( psz_dup, '/' );
2656 psz_access = psz_dup;
2663 *ppsz_access = psz_access;
2664 *ppsz_demux = psz_demux;
2665 *ppsz_path = psz_path;
2668 /*****************************************************************************
2669 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2672 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2673 *****************************************************************************/
2674 static void MRLSections( input_thread_t *p_input, char *psz_source,
2675 int *pi_title_start, int *pi_title_end,
2676 int *pi_chapter_start, int *pi_chapter_end )
2678 char *psz, *psz_end, *psz_next, *psz_check;
2680 *pi_title_start = *pi_title_end = -1;
2681 *pi_chapter_start = *pi_chapter_end = -1;
2683 /* Start by parsing titles and chapters */
2684 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2686 /* Check we are really dealing with a title/chapter section */
2687 psz_check = psz + 1;
2688 if( !*psz_check ) return;
2689 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2690 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2691 if( *psz_check == ':' && ++psz_check )
2692 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2693 if( *psz_check != '-' && *psz_check ) return;
2694 if( *psz_check == '-' && ++psz_check )
2695 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2696 if( *psz_check != ':' && *psz_check ) return;
2697 if( *psz_check == ':' && ++psz_check )
2698 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2699 if( *psz_check ) return;
2701 /* Separate start and end */
2703 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2705 /* Look for the start title */
2706 *pi_title_start = strtol( psz, &psz_next, 0 );
2707 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2708 *pi_title_end = *pi_title_start;
2711 /* Look for the start chapter */
2713 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2714 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2715 *pi_chapter_end = *pi_chapter_start;
2719 /* Look for the end title */
2720 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2721 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2724 /* Look for the end chapter */
2725 if( *psz_end ) psz_end++;
2726 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2727 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2730 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2731 psz_source, *pi_title_start, *pi_chapter_start,
2732 *pi_title_end, *pi_chapter_end );
2735 /*****************************************************************************
2736 * input_AddSubtitles: add a subtitles file and enable it
2737 *****************************************************************************/
2738 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2739 vlc_bool_t b_check_extension )
2741 input_source_t *sub;
2744 char *psz_path, *psz_extension;
2746 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2751 /* if we are provided a subtitle.sub file,
2752 * see if we don't have a subtitle.idx and use it instead */
2753 psz_path = strdup( psz_subtitle );
2756 psz_extension = strrchr( psz_path, '.');
2757 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2761 strcpy( psz_extension, ".idx" );
2762 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2763 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2766 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2767 psz_path, psz_subtitle );
2768 strcpy( psz_subtitle, psz_path );
2774 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2776 sub = InputSourceNew( p_input );
2777 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2779 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2782 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2784 if( count.i_int == 0 )
2786 /* if it was first one, there is disable too */
2788 if( count.i_int < list.p_list->i_count )
2790 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2791 &list.p_list->p_values[count.i_int] );
2793 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );