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 *****************************************************************************/
33 #include "input_internal.h"
36 #include "../stream_output/stream_output.h"
38 #include <vlc_playlist.h>
39 #include <vlc_interface.h>
41 #include <vlc_demux.h>
42 #include <vlc_charset.h>
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Run ( input_thread_t *p_input );
52 static int RunAndDestroy ( input_thread_t *p_input );
54 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
55 const char *, vlc_bool_t, sout_instance_t * );
56 static int Init ( input_thread_t *p_input );
57 static void Error ( input_thread_t *p_input );
58 static void End ( input_thread_t *p_input );
59 static void MainLoop( input_thread_t *p_input );
60 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout );
62 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
63 static void ControlReduce( input_thread_t * );
64 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
66 static int UpdateFromAccess( input_thread_t * );
67 static int UpdateFromDemux( input_thread_t * );
69 static void UpdateItemLength( input_thread_t *, int64_t i_length );
71 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
73 static input_source_t *InputSourceNew( input_thread_t *);
74 static int InputSourceInit( input_thread_t *, input_source_t *,
75 const char *, const char *psz_forced_demux );
76 static void InputSourceClean( input_source_t * );
78 //static void InputGetAttachments( input_thread_t *, input_source_t * );
80 static void SlaveDemux( input_thread_t *p_input );
81 static void SlaveSeek( input_thread_t *p_input );
83 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
84 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
86 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t * );
87 static void SoutKeep( sout_instance_t * );
89 /*****************************************************************************
90 * This function creates a new input, and returns a pointer
91 * to its description. On error, it returns NULL.
93 * Variables for _public_ use:
96 * - rate,rate-slower, rate-faster
97 * - position, position-offset
99 * - title,title-next,title-prev
100 * - chapter,chapter-next, chapter-prev
101 * - program, audio-es, video-es, spu-es
102 * - audio-delay, spu-delay
107 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
108 * * For intf callback upon changes
110 * TODO explain when Callback is called
111 * TODO complete this list (?)
112 *****************************************************************************/
113 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
114 const char *psz_header, vlc_bool_t b_quick, sout_instance_t *p_sout )
116 input_thread_t *p_input = NULL; /* thread descriptor */
120 /* Allocate descriptor */
121 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
122 if( p_input == NULL )
124 msg_Err( p_parent, "out of memory" );
127 MALLOC_NULL( p_input->p, input_thread_private_t );
129 /* One "randomly" selected input thread is responsible for computing
130 * the global stats. Check if there is already someone doing this */
131 if( p_input->p_libvlc->p_playlist->p_stats && !b_quick )
133 vlc_mutex_lock( &p_input->p_libvlc->p_playlist->p_stats->lock );
134 if( p_input->p_libvlc->p_playlist->p_stats_computer == NULL )
136 p_input->p_libvlc->p_playlist->p_stats_computer = p_input;
138 vlc_mutex_unlock( &p_input->p_libvlc->p_playlist->p_stats->lock );
141 p_input->b_preparsing = b_quick;
142 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
144 /* Init Common fields */
145 p_input->b_eof = VLC_FALSE;
146 p_input->b_can_pace_control = VLC_TRUE;
147 p_input->p->i_start = 0;
149 p_input->p->i_stop = 0;
150 p_input->p->i_run = 0;
151 p_input->p->i_title = 0;
152 p_input->p->title = NULL;
153 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
154 p_input->i_state = INIT_S;
155 p_input->p->i_rate = INPUT_RATE_DEFAULT;
156 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
157 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
158 p_input->p->p_es_out = NULL;
159 p_input->p->p_sout = NULL;
160 p_input->p->b_sout_keep = VLC_FALSE;
161 p_input->p->b_out_pace_control = VLC_FALSE;
162 p_input->i_pts_delay = 0;
164 /* Init Input fields */
165 p_input->p->input.p_item = p_item;
166 p_input->p->input.p_access = NULL;
167 p_input->p->input.p_stream = NULL;
168 p_input->p->input.p_demux = NULL;
169 p_input->p->input.b_title_demux = VLC_FALSE;
170 p_input->p->input.i_title = 0;
171 p_input->p->input.title = NULL;
172 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
173 p_input->p->input.b_can_pace_control = VLC_TRUE;
174 p_input->p->input.b_eof = VLC_FALSE;
175 p_input->p->input.i_cr_average = 0;
177 vlc_mutex_lock( &p_item->lock );
179 if( !p_item->p_stats )
180 p_item->p_stats = stats_NewInputStats( p_input );
181 vlc_mutex_unlock( &p_item->lock );
184 p_input->p->i_slave = 0;
185 p_input->p->slave = NULL;
187 /* Init control buffer */
188 vlc_mutex_init( p_input, &p_input->p->lock_control );
189 p_input->p->i_control = 0;
191 /* Parse input options */
192 vlc_mutex_lock( &p_item->lock );
193 for( i = 0; i < p_item->i_options; i++ )
194 var_OptionParse( p_input, p_item->ppsz_options[i] );
195 vlc_mutex_unlock( &p_item->lock );
197 /* Create Object Variables for private use only */
198 input_ConfigVarInit( p_input );
200 /* Create Objects variables for public Get and Set */
201 input_ControlVarInit( p_input );
203 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
205 if( !p_input->b_preparsing )
207 var_Get( p_input, "bookmarks", &val );
210 /* FIXME: have a common cfg parsing routine used by sout and others */
211 char *psz_parser, *psz_start, *psz_end;
212 psz_parser = val.psz_string;
213 while( (psz_start = strchr( psz_parser, '{' ) ) )
215 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
218 psz_end = strchr( psz_start, '}' );
219 if( !psz_end ) break;
220 psz_parser = psz_end + 1;
221 backup = *psz_parser;
224 while( (psz_end = strchr( psz_start, ',' ) ) )
227 if( !strncmp( psz_start, "name=", 5 ) )
229 p_seekpoint->psz_name = strdup(psz_start + 5);
231 else if( !strncmp( psz_start, "bytes=", 6 ) )
233 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
235 else if( !strncmp( psz_start, "time=", 5 ) )
237 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
240 psz_start = psz_end + 1;
242 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
243 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
244 p_seekpoint->i_time_offset );
245 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
246 vlc_seekpoint_Delete( p_seekpoint );
247 *psz_parser = backup;
249 free( val.psz_string );
253 /* Remove 'Now playing' info as it is probably outdated */
254 input_Control( p_input, INPUT_DEL_INFO,
255 _(VLC_META_INFO_CAT),
256 _(VLC_META_NOW_PLAYING) );
257 input_item_SetNowPlaying( p_item, NULL );
260 if( p_input->b_preparsing )
261 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
265 p_input->p->p_sout = p_sout;
267 /* Attach only once we are ready */
268 vlc_object_attach( p_input, p_parent );
273 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout )
275 vlc_object_detach( p_input );
279 if( p_input->p->p_sout )
282 *pp_sout = p_input->p->p_sout;
283 else if( p_input->p->b_sout_keep )
284 SoutKeep( p_input->p->p_sout );
286 sout_DeleteInstance( p_input->p->p_sout );
289 vlc_mutex_destroy( &p_input->p->lock_control );
292 vlc_object_destroy( p_input );
296 * Initialize an input thread and run it. You will need to monitor the
297 * thread to clean up after it is done
299 * \param p_parent a vlc_object
300 * \param p_item an input item
301 * \return a pointer to the spawned input thread
303 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
304 input_item_t *p_item )
306 vlc_bool_t b_sout_keep;
307 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
308 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
310 if( !p_input && p_sout )
313 p_input->p->b_sout_keep = b_sout_keep;
318 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
319 input_item_t *p_item,
320 const char *psz_log, sout_instance_t *p_sout )
322 input_thread_t *p_input;
324 p_input = Create( p_parent, p_item, psz_log, VLC_FALSE, p_sout );
328 /* Create thread and wait for its readiness. */
329 if( vlc_thread_create( p_input, "input", Run,
330 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
332 input_ChangeState( p_input, ERROR_S );
333 msg_Err( p_input, "cannot create input thread" );
334 Destroy( p_input, &p_sout );
342 * Initialize an input thread and run it. This thread will clean after himself,
343 * you can forget about it. It can work either in blocking or non-blocking mode
345 * \param p_parent a vlc_object
346 * \param p_item an input item
347 * \param b_block should we block until read is finished ?
348 * \return the input object id if non blocking, an error code else
350 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
353 vlc_bool_t b_sout_keep;
354 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
355 input_thread_t *p_input;
357 p_input = Create( p_parent, p_item, NULL, VLC_FALSE, p_sout );
358 if( !p_input && p_sout )
363 p_input->p->b_sout_keep = b_sout_keep;
367 RunAndDestroy( p_input );
372 if( vlc_thread_create( p_input, "input", RunAndDestroy,
373 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
375 input_ChangeState( p_input, ERROR_S );
376 msg_Err( p_input, "cannot create input thread" );
377 Destroy( p_input, NULL );
381 return p_input->i_object_id;
385 * Initialize an input and initialize it to preparse the item
386 * This function is blocking. It will only accept to parse files
388 * \param p_parent a vlc_object_t
389 * \param p_item an input item
390 * \return VLC_SUCCESS or an error
392 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
394 input_thread_t *p_input;
396 /* Allocate descriptor */
397 p_input = Create( p_parent, p_item, NULL, VLC_TRUE, NULL );
401 if( !Init( p_input ) )
404 Destroy( p_input, NULL );
410 * Request a running input thread to stop and die
412 * \param the input thread to stop
414 void input_StopThread( input_thread_t *p_input )
419 /* Set die for input */
420 vlc_object_kill( p_input );
421 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
423 /* We cannot touch p_input fields directly (we come from another thread),
424 * so use the vlc_object_find way, it's perfectly safe */
426 /* Set die for all access */
427 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
428 for( i = 0; i < p_list->i_count; i++ )
430 vlc_object_kill( p_list->p_values[i].p_object );
432 vlc_list_release( p_list );
434 /* Set die for all stream */
435 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
436 for( i = 0; i < p_list->i_count; i++ )
438 vlc_object_kill( p_list->p_values[i].p_object );
440 vlc_list_release( p_list );
442 /* Set die for all demux */
443 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
444 for( i = 0; i < p_list->i_count; i++ )
446 vlc_object_kill( p_list->p_values[i].p_object );
448 vlc_list_release( p_list );
450 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
454 * Clean up a dead input thread
455 * This function does not return until the thread is effectively cancelled.
457 * \param the input thread to kill
459 void input_DestroyThread( input_thread_t *p_input )
461 input_DestroyThreadExtended( p_input, NULL );
464 void input_DestroyThreadExtended( input_thread_t *p_input, sout_instance_t **pp_sout )
466 /* Join the thread */
467 vlc_thread_join( p_input );
470 Destroy( p_input, pp_sout );
473 /*****************************************************************************
474 * Run: main thread loop
475 * This is the "normal" thread that spawns the input processing chain,
476 * reads the stream, cleans up and waits
477 *****************************************************************************/
478 static int Run( input_thread_t *p_input )
480 /* Signal that the thread is launched */
481 vlc_thread_ready( p_input );
483 if( Init( p_input ) )
485 /* If we failed, wait before we are killed, and exit */
486 p_input->b_error = VLC_TRUE;
487 playlist_Signal( pl_Get( p_input ) );
491 /* Tell we're dead */
492 p_input->b_dead = VLC_TRUE;
499 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
501 /* We have finish to demux data but not to play them */
502 while( !p_input->b_die )
504 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
507 msg_Dbg( p_input, "waiting decoder fifos to empty" );
509 msleep( INPUT_IDLE_SLEEP );
512 /* We have finished */
513 p_input->b_eof = VLC_TRUE;
514 playlist_Signal( pl_Get( p_input ) );
517 /* Wait until we are asked to die */
518 if( !p_input->b_die )
529 /*****************************************************************************
530 * RunAndDestroy: main thread loop
531 * This is the "just forget me" thread that spawns the input processing chain,
532 * reads the stream, cleans up and releases memory
533 *****************************************************************************/
534 static int RunAndDestroy( input_thread_t *p_input )
536 /* Signal that the thread is launched */
537 vlc_thread_ready( p_input );
539 if( Init( p_input ) )
544 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
546 /* We have finished demuxing data but not playing it */
547 while( !p_input->b_die )
549 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
552 msg_Dbg( p_input, "waiting decoder fifos to empty" );
554 msleep( INPUT_IDLE_SLEEP );
557 /* We have finished */
558 p_input->b_eof = VLC_TRUE;
566 Destroy( p_input, NULL );
570 /*****************************************************************************
571 * Main loop: Fill buffers from access, and demux
572 *****************************************************************************/
573 static void MainLoop( input_thread_t *p_input )
575 int64_t i_start_mdate = mdate();
576 int64_t i_intf_update = 0;
579 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
581 vlc_bool_t b_force_update = VLC_FALSE;
587 if( p_input->i_state != PAUSE_S )
589 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
590 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
593 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
598 if( p_input->p->input.b_title_demux &&
599 p_input->p->input.p_demux->info.i_update )
601 i_ret = UpdateFromDemux( p_input );
602 b_force_update = VLC_TRUE;
604 else if( !p_input->p->input.b_title_demux &&
605 p_input->p->input.p_access &&
606 p_input->p->input.p_access->info.i_update )
608 i_ret = UpdateFromAccess( p_input );
609 b_force_update = VLC_TRUE;
613 if( i_ret == 0 ) /* EOF */
617 var_Get( p_input, "input-repeat", &repeat );
618 if( repeat.i_int == 0 )
620 /* End of file - we do not set b_die because only the
621 * playlist is allowed to do so. */
622 input_ChangeState( p_input, END_S );
623 msg_Dbg( p_input, "EOF reached" );
624 p_input->p->input.b_eof = VLC_TRUE;
628 msg_Dbg( p_input, "repeating the same input (%d)",
630 if( repeat.i_int > 0 )
633 var_Set( p_input, "input-repeat", repeat );
636 /* Seek to start title/seekpoint */
637 val.i_int = p_input->p->input.i_title_start -
638 p_input->p->input.i_title_offset;
639 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
641 input_ControlPush( p_input,
642 INPUT_CONTROL_SET_TITLE, &val );
644 val.i_int = p_input->p->input.i_seekpoint_start -
645 p_input->p->input.i_seekpoint_offset;
646 if( val.i_int > 0 /* TODO: check upper boundary */ )
647 input_ControlPush( p_input,
648 INPUT_CONTROL_SET_SEEKPOINT, &val );
650 /* Seek to start position */
651 if( p_input->p->i_start > 0 )
653 val.i_time = p_input->p->i_start;
654 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
660 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
665 i_start_mdate = mdate();
670 p_input->b_error = VLC_TRUE;
673 if( i_ret > 0 && p_input->p->i_slave > 0 )
675 SlaveDemux( p_input );
685 vlc_mutex_lock( &p_input->p->lock_control );
686 ControlReduce( p_input );
687 while( !ControlPopNoLock( p_input, &i_type, &val ) )
689 msg_Dbg( p_input, "control type=%d", i_type );
690 if( Control( p_input, i_type, val ) )
691 b_force_update = VLC_TRUE;
693 vlc_mutex_unlock( &p_input->p->lock_control );
695 if( b_force_update || i_intf_update < mdate() )
699 int64_t i_time, i_length;
700 /* update input status variables */
701 if( !demux2_Control( p_input->p->input.p_demux,
702 DEMUX_GET_POSITION, &f_pos ) )
704 val.f_float = (float)f_pos;
705 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
707 if( !demux2_Control( p_input->p->input.p_demux,
708 DEMUX_GET_TIME, &i_time ) )
710 p_input->i_time = i_time;
712 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
714 if( !demux2_Control( p_input->p->input.p_demux,
715 DEMUX_GET_LENGTH, &i_length ) )
718 var_Get( p_input, "length", &old_val );
719 val.i_time = i_length;
720 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
722 if( old_val.i_time != val.i_time )
724 UpdateItemLength( p_input, i_length );
728 var_SetBool( p_input, "intf-change", VLC_TRUE );
729 i_intf_update = mdate() + I64C(150000);
731 /* 150ms * 8 = ~ 1 second */
732 if( ++i_updates % 8 == 0 )
734 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
735 /* Are we the thread responsible for computing global stats ? */
736 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
738 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
739 p_input->p_libvlc->p_playlist->p_stats );
745 static int Init( input_thread_t * p_input )
755 /* Initialize optional stream output. (before access/demuxer)
756 * XXX: we add a special case if the uri starts by vlc.
757 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
758 * be destroyed in case of a file).
759 * (this will break playing of file starting by 'vlc:' but I don't
760 * want to add more logic, just force file by file:// or code it ;)
762 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
763 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
765 if( !p_input->b_preparsing )
767 /* Prepare statistics */
768 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
769 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
770 if( p_input->p_libvlc->b_stats )
772 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
773 INIT_COUNTER( read_packets, INTEGER, COUNTER );
774 INIT_COUNTER( demux_read, INTEGER, COUNTER );
775 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
776 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
777 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
778 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
779 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
780 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
781 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
782 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
783 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
784 p_input->p->counters.p_sout_send_bitrate = NULL;
785 p_input->p->counters.p_sout_sent_packets = NULL;
786 p_input->p->counters.p_sout_sent_bytes = NULL;
787 if( p_input->p->counters.p_demux_bitrate )
788 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
789 if( p_input->p->counters.p_input_bitrate )
790 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
793 /* Find a usable sout and attach it to p_input */
794 psz = var_GetString( p_input, "sout" );
795 if( *psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
797 /* Check the validity of the provided sout */
798 if( p_input->p->p_sout )
800 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
802 msg_Dbg( p_input, "destroying unusable sout" );
804 sout_DeleteInstance( p_input->p->p_sout );
805 p_input->p->p_sout = NULL;
809 if( p_input->p->p_sout )
812 msg_Dbg( p_input, "sout keep: reusing sout" );
813 msg_Dbg( p_input, "sout keep: you probably want to use "
814 "gather stream_out" );
815 vlc_object_attach( p_input->p->p_sout, p_input );
819 /* Create a new one */
820 p_input->p->p_sout = sout_NewInstance( p_input, psz );
822 if( !p_input->p->p_sout )
824 input_ChangeState( p_input, ERROR_S );
825 msg_Err( p_input, "cannot start stream output instance, " \
832 if( p_input->p_libvlc->b_stats )
834 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
835 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
836 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
837 if( p_input->p->counters.p_sout_send_bitrate )
838 p_input->p->counters.p_sout_send_bitrate->update_interval =
842 else if( p_input->p->p_sout )
844 msg_Dbg( p_input, "destroying useless sout" );
846 sout_DeleteInstance( p_input->p->p_sout );
847 p_input->p->p_sout = NULL;
853 p_input->p->p_es_out = input_EsOutNew( p_input );
854 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
855 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
857 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
858 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
860 if( InputSourceInit( p_input, &p_input->p->input,
861 p_input->p->input.p_item->psz_uri, NULL ) )
866 /* Create global title (from master) */
867 if( !p_input->b_preparsing )
869 p_input->p->i_title = p_input->p->input.i_title;
870 p_input->p->title = p_input->p->input.title;
871 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
872 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
873 if( p_input->p->i_title > 0 )
875 /* Setup variables */
876 input_ControlVarNavigation( p_input );
877 input_ControlVarTitle( p_input, 0 );
881 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
882 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
885 if( p_input->i_pts_delay < 0 )
886 p_input->i_pts_delay = 0;
888 /* If the desynchronisation requested by the user is < 0, we need to
889 * cache more data. */
890 var_Get( p_input, "audio-desync", &val );
891 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
893 /* Update cr_average depending on the caching */
894 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
895 p_input->p->input.i_cr_average /= 10;
896 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
899 /* Load master infos */
901 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
902 &val.i_time ) && val.i_time > 0 )
904 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
905 UpdateItemLength( p_input, val.i_time );
908 /* Start title/chapter */
909 if( !p_input->b_preparsing )
911 val.i_int = p_input->p->input.i_title_start -
912 p_input->p->input.i_title_offset;
913 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
914 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
915 val.i_int = p_input->p->input.i_seekpoint_start -
916 p_input->p->input.i_seekpoint_offset;
917 if( val.i_int > 0 /* TODO: check upper boundary */ )
918 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
922 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
923 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
924 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
925 if( p_input->p->i_run < 0 )
927 msg_Warn( p_input, "invalid run-time ignored" );
928 p_input->p->i_run = 0;
931 if( p_input->p->i_start > 0 )
933 if( p_input->p->i_start >= val.i_time )
935 msg_Warn( p_input, "invalid start-time ignored" );
941 msg_Dbg( p_input, "starting at time: %ds",
942 (int)( p_input->p->i_start / I64C(1000000) ) );
944 s.i_time = p_input->p->i_start;
945 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
948 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
950 msg_Warn( p_input, "invalid stop-time ignored" );
951 p_input->p->i_stop = 0;
955 /* Get fps and set it if not already set */
956 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
959 float f_requested_fps;
961 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
962 var_SetFloat( p_input, "sub-original-fps", f_fps );
964 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
965 if( f_requested_fps != f_fps )
967 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
969 var_SetFloat( p_input, "sub-fps", f_requested_fps );
973 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
976 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
979 /* Look for and add subtitle files */
980 psz_subtitle = var_GetString( p_input, "sub-file" );
983 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
984 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
987 var_Get( p_input, "sub-autodetect-file", &val );
990 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
991 char **subs = subtitles_Detect( p_input, psz_autopath,
992 p_input->p->input.p_item->psz_uri );
996 /* Try to autoselect the first autodetected subtitles file
997 * if no subtitles file was specified */
998 if( *psz_subtitle == 0 && subs && subs[0] )
1000 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
1005 /* Then, just add the following subtitles files */
1006 for( ; subs && subs[i]; i++ )
1008 if( strcmp( psz_subtitle, subs[i] ) )
1010 sub = InputSourceNew( p_input );
1011 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1013 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1019 if( subs ) free( subs );
1020 if( psz_autopath ) free( psz_autopath );
1022 free( psz_subtitle );
1024 /* Look for slave */
1025 psz = var_GetString( p_input, "input-slave" );
1029 input_source_t *slave;
1030 while( psz && *psz )
1032 while( *psz == ' ' || *psz == '#' )
1036 if( ( psz_delim = strchr( psz, '#' ) ) )
1038 *psz_delim++ = '\0';
1045 msg_Dbg( p_input, "adding slave input '%s'", psz );
1046 slave = InputSourceNew( p_input );
1047 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1049 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1055 if( psz ) free( psz );
1059 p_input->p->i_start = 0;
1060 p_input->p->i_start = 0;
1064 if( !p_input->b_preparsing )
1066 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1067 i_es_out_mode = ES_OUT_MODE_AUTO;
1069 if( p_input->p->p_sout )
1071 var_Get( p_input, "sout-all", &val );
1074 i_es_out_mode = ES_OUT_MODE_ALL;
1079 var_Get( p_input, "programs", &val );
1080 if ( val.p_list && val.p_list->i_count )
1082 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1083 /* Note : we should remove the "program" callback. */
1086 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1090 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1092 /* Inform the demuxer about waited group (needed only for DVB) */
1093 if( i_es_out_mode == ES_OUT_MODE_ALL )
1095 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1097 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1099 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1104 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1105 (int) var_GetInteger( p_input, "program" ), NULL );
1108 if( p_input->p->p_sout )
1110 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1112 p_input->p->b_out_pace_control = VLC_FALSE;
1116 p_input->p->b_out_pace_control = VLC_TRUE;
1119 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1121 /* We don't want a high input priority here or we'll
1122 * end-up sucking up all the CPU time */
1123 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1126 msg_Dbg( p_input, "starting in %s mode",
1127 p_input->p->b_out_pace_control ? "async" : "sync" );
1131 p_meta = vlc_meta_New();
1132 /* Get meta data from users */
1133 InputMetaUser( p_input, p_meta );
1135 /* Get meta data from master input */
1136 demux2_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
1138 /* Access_file does not give any meta, and there are no slave */
1139 if( !p_input->b_preparsing )
1141 if( p_input->p->input.p_access )
1142 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1145 /* Get meta data from slave input */
1146 for( i = 0; i < p_input->p->i_slave; i++ )
1148 demux2_Control( p_input->p->slave[i]->p_demux,
1149 DEMUX_GET_META, p_meta );
1150 if( p_input->p->slave[i]->p_access )
1152 access2_Control( p_input->p->slave[i]->p_access,
1153 ACCESS_GET_META, p_meta );
1158 InputUpdateMeta( p_input, p_meta );
1160 if( !p_input->b_preparsing )
1162 msg_Dbg( p_input, "`%s' successfully opened",
1163 p_input->p->input.p_item->psz_uri );
1167 /* initialization is complete */
1168 input_ChangeState( p_input, PLAYING_S );
1173 input_ChangeState( p_input, ERROR_S );
1175 if( p_input->p->p_es_out )
1176 input_EsOutDelete( p_input->p->p_es_out );
1178 if( p_input->p->p_sout )
1180 vlc_object_detach( p_input->p->p_sout );
1181 sout_DeleteInstance( p_input->p->p_sout );
1185 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1187 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1188 stats_CounterClean( p_input->p->counters.p_##c );\
1189 p_input->p->counters.p_##c = NULL; } while(0)
1190 EXIT_COUNTER( read_bytes );
1191 EXIT_COUNTER( read_packets );
1192 EXIT_COUNTER( demux_read );
1193 EXIT_COUNTER( input_bitrate );
1194 EXIT_COUNTER( demux_bitrate );
1195 EXIT_COUNTER( played_abuffers );
1196 EXIT_COUNTER( lost_abuffers );
1197 EXIT_COUNTER( displayed_pictures );
1198 EXIT_COUNTER( lost_pictures );
1199 EXIT_COUNTER( decoded_audio );
1200 EXIT_COUNTER( decoded_video );
1201 EXIT_COUNTER( decoded_sub );
1203 if( p_input->p->p_sout )
1205 EXIT_COUNTER( sout_sent_packets );
1206 EXIT_COUNTER (sout_sent_bytes );
1207 EXIT_COUNTER( sout_send_bitrate );
1212 /* Mark them deleted */
1213 p_input->p->input.p_demux = NULL;
1214 p_input->p->input.p_stream = NULL;
1215 p_input->p->input.p_access = NULL;
1216 p_input->p->p_es_out = NULL;
1217 p_input->p->p_sout = NULL;
1219 return VLC_EGENERIC;
1222 /*****************************************************************************
1223 * Error: RunThread() error loop
1224 *****************************************************************************
1225 * This function is called when an error occurred during thread main's loop.
1226 *****************************************************************************/
1227 static void Error( input_thread_t *p_input )
1229 while( !p_input->b_die )
1232 input_ChangeState( p_input, ERROR_S );
1233 msleep( INPUT_IDLE_SLEEP );
1237 /*****************************************************************************
1238 * End: end the input thread
1239 *****************************************************************************/
1240 static void End( input_thread_t * p_input )
1244 /* We are at the end */
1245 input_ChangeState( p_input, END_S );
1247 /* Clean control variables */
1248 input_ControlVarClean( p_input );
1250 /* Clean up master */
1251 InputSourceClean( &p_input->p->input );
1254 for( i = 0; i < p_input->p->i_slave; i++ )
1256 InputSourceClean( p_input->p->slave[i] );
1257 free( p_input->p->slave[i] );
1259 if( p_input->p->slave ) free( p_input->p->slave );
1261 /* Unload all modules */
1262 if( p_input->p->p_es_out )
1263 input_EsOutDelete( p_input->p->p_es_out );
1265 if( !p_input->b_preparsing )
1267 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1268 if( p_input->p_libvlc->b_stats )
1270 /* make sure we are up to date */
1271 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1272 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1274 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1275 p_input->p_libvlc->p_playlist->p_stats );
1276 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1278 CL_CO( read_bytes );
1279 CL_CO( read_packets );
1280 CL_CO( demux_read );
1281 CL_CO( input_bitrate );
1282 CL_CO( demux_bitrate );
1283 CL_CO( played_abuffers );
1284 CL_CO( lost_abuffers );
1285 CL_CO( displayed_pictures );
1286 CL_CO( lost_pictures );
1287 CL_CO( decoded_audio) ;
1288 CL_CO( decoded_video );
1289 CL_CO( decoded_sub) ;
1292 /* Close optional stream output instance */
1293 if( p_input->p->p_sout )
1295 CL_CO( sout_sent_packets );
1296 CL_CO( sout_sent_bytes );
1297 CL_CO( sout_send_bitrate );
1299 vlc_object_detach( p_input->p->p_sout );
1304 if( p_input->p->i_attachment > 0 )
1306 for( i = 0; i < p_input->p->i_attachment; i++ )
1307 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1308 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1311 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1313 /* Tell we're dead */
1314 p_input->b_dead = VLC_TRUE;
1317 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t *pb_sout_keep )
1319 vlc_bool_t b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1320 sout_instance_t *p_sout = NULL;
1323 /* Search sout-keep options
1324 * XXX it has to be done here, but it is duplicated work :( */
1325 vlc_mutex_lock( &p_item->lock );
1326 for( i = 0; i < p_item->i_options; i++ )
1328 const char *psz_option = p_item->ppsz_options[i];
1331 if( *psz_option == ':' )
1334 if( !strcmp( psz_option, "sout-keep" ) )
1335 b_keep_sout = VLC_TRUE;
1336 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1337 b_keep_sout = VLC_FALSE;
1339 vlc_mutex_unlock( &p_item->lock );
1341 /* Find a potential sout to reuse
1342 * XXX it might be unusable but this will be checked later */
1345 /* Remove the sout from the playlist garbage collector */
1346 playlist_t *p_playlist = pl_Yield( p_parent );
1348 vlc_mutex_lock( &p_playlist->gc_lock );
1349 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1352 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1354 vlc_object_release( p_sout );
1359 vlc_object_detach( p_sout ); /* Remove it from the GC */
1361 vlc_object_release( p_sout );
1364 vlc_mutex_unlock( &p_playlist->gc_lock );
1366 pl_Release( p_parent );
1370 *pb_sout_keep = b_keep_sout;
1374 static void SoutKeep( sout_instance_t *p_sout )
1376 /* attach sout to the playlist */
1377 playlist_t *p_playlist = pl_Yield( p_sout );
1379 msg_Dbg( p_sout, "sout has been kept" );
1380 vlc_object_attach( p_sout, p_playlist );
1382 pl_Release( p_sout );
1385 /*****************************************************************************
1387 *****************************************************************************/
1388 static inline int ControlPopNoLock( input_thread_t *p_input,
1389 int *pi_type, vlc_value_t *p_val )
1391 if( p_input->p->i_control <= 0 )
1393 return VLC_EGENERIC;
1396 *pi_type = p_input->p->control[0].i_type;
1397 *p_val = p_input->p->control[0].val;
1399 p_input->p->i_control--;
1400 if( p_input->p->i_control > 0 )
1404 for( i = 0; i < p_input->p->i_control; i++ )
1406 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1407 p_input->p->control[i].val = p_input->p->control[i+1].val;
1414 static void ControlReduce( input_thread_t *p_input )
1421 for( i = 1; i < p_input->p->i_control; i++ )
1423 const int i_lt = p_input->p->control[i-1].i_type;
1424 const int i_ct = p_input->p->control[i].i_type;
1426 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1427 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1431 ( i_ct == INPUT_CONTROL_SET_STATE ||
1432 i_ct == INPUT_CONTROL_SET_RATE ||
1433 i_ct == INPUT_CONTROL_SET_POSITION ||
1434 i_ct == INPUT_CONTROL_SET_TIME ||
1435 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1436 i_ct == INPUT_CONTROL_SET_TITLE ||
1437 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1438 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1441 // msg_Dbg( p_input, "merged at %d", i );
1442 /* Remove the i-1 */
1443 for( j = i; j < p_input->p->i_control; j++ )
1444 p_input->p->control[j-1] = p_input->p->control[j];
1445 p_input->p->i_control--;
1449 /* TODO but that's not that important
1450 - merge SET_X with SET_X_CMD
1451 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1452 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1459 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1462 vlc_bool_t b_force_update = VLC_FALSE;
1464 if( !p_input ) return b_force_update;
1468 case INPUT_CONTROL_SET_DIE:
1469 msg_Dbg( p_input, "control: stopping input" );
1470 /* Mark all submodules to die */
1471 if( p_input->p->input.p_access )
1472 vlc_object_kill( p_input->p->input.p_access );
1473 if( p_input->p->input.p_stream )
1474 vlc_object_kill( p_input->p->input.p_stream );
1475 vlc_object_kill( p_input->p->input.p_demux );
1477 vlc_object_kill( p_input );
1480 case INPUT_CONTROL_SET_POSITION:
1481 case INPUT_CONTROL_SET_POSITION_OFFSET:
1484 if( i_type == INPUT_CONTROL_SET_POSITION )
1486 f_pos = val.f_float;
1490 /* Should not fail */
1491 demux2_Control( p_input->p->input.p_demux,
1492 DEMUX_GET_POSITION, &f_pos );
1493 f_pos += val.f_float;
1495 if( f_pos < 0.0 ) f_pos = 0.0;
1496 if( f_pos > 1.0 ) f_pos = 1.0;
1497 /* Reset the decoders states and clock sync (before calling the demuxer */
1498 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1499 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1500 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1503 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1504 "%2.1f%% failed", f_pos * 100 );
1508 if( p_input->p->i_slave > 0 )
1509 SlaveSeek( p_input );
1511 b_force_update = VLC_TRUE;
1516 case INPUT_CONTROL_SET_TIME:
1517 case INPUT_CONTROL_SET_TIME_OFFSET:
1522 if( i_type == INPUT_CONTROL_SET_TIME )
1524 i_time = val.i_time;
1528 /* Should not fail */
1529 demux2_Control( p_input->p->input.p_demux,
1530 DEMUX_GET_TIME, &i_time );
1531 i_time += val.i_time;
1533 if( i_time < 0 ) i_time = 0;
1535 /* Reset the decoders states and clock sync (before calling the demuxer */
1536 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1537 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1539 i_ret = demux2_Control( p_input->p->input.p_demux,
1540 DEMUX_SET_TIME, i_time );
1545 /* Emulate it with a SET_POS */
1546 demux2_Control( p_input->p->input.p_demux,
1547 DEMUX_GET_LENGTH, &i_length );
1550 double f_pos = (double)i_time / (double)i_length;
1551 i_ret = demux2_Control( p_input->p->input.p_demux,
1552 DEMUX_SET_POSITION, f_pos );
1557 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1558 " failed or not possible", i_time );
1562 if( p_input->p->i_slave > 0 )
1563 SlaveSeek( p_input );
1565 b_force_update = VLC_TRUE;
1570 case INPUT_CONTROL_SET_STATE:
1571 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1572 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1575 if( p_input->p->input.p_access )
1576 i_ret = access2_Control( p_input->p->input.p_access,
1577 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1579 i_ret = demux2_Control( p_input->p->input.p_demux,
1580 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1584 /* FIXME What to do ? */
1585 msg_Warn( p_input, "cannot unset pause -> EOF" );
1586 vlc_mutex_unlock( &p_input->p->lock_control );
1587 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1588 vlc_mutex_lock( &p_input->p->lock_control );
1591 b_force_update = VLC_TRUE;
1593 /* Switch to play */
1594 p_input->i_state = PLAYING_S;
1595 val.i_int = PLAYING_S;
1596 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1599 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1601 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1602 p_input->p->b_can_pause )
1605 if( p_input->p->input.p_access )
1606 i_ret = access2_Control( p_input->p->input.p_access,
1607 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1609 i_ret = demux2_Control( p_input->p->input.p_demux,
1610 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1612 b_force_update = VLC_TRUE;
1616 msg_Warn( p_input, "cannot set pause state" );
1617 val.i_int = p_input->i_state;
1621 val.i_int = PAUSE_S;
1624 /* Switch to new state */
1625 p_input->i_state = val.i_int;
1626 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1628 /* Send discontinuity to decoders (it will allow them to flush
1630 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE, VLC_FALSE );
1632 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1634 b_force_update = VLC_TRUE;
1636 /* Correct "state" value */
1637 val.i_int = p_input->i_state;
1638 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1640 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1642 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1646 case INPUT_CONTROL_SET_RATE:
1647 case INPUT_CONTROL_SET_RATE_SLOWER:
1648 case INPUT_CONTROL_SET_RATE_FASTER:
1652 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1653 i_rate = p_input->p->i_rate * 2;
1654 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1655 i_rate = p_input->p->i_rate / 2;
1659 if( i_rate < INPUT_RATE_MIN )
1661 msg_Dbg( p_input, "cannot set rate faster" );
1662 i_rate = INPUT_RATE_MIN;
1664 else if( i_rate > INPUT_RATE_MAX )
1666 msg_Dbg( p_input, "cannot set rate slower" );
1667 i_rate = INPUT_RATE_MAX;
1669 if( i_rate != INPUT_RATE_DEFAULT &&
1670 ( !p_input->b_can_pace_control ||
1671 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1673 msg_Dbg( p_input, "cannot change rate" );
1674 i_rate = INPUT_RATE_DEFAULT;
1676 if( i_rate != p_input->p->i_rate )
1679 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1681 input_EsOutDiscontinuity( p_input->p->p_es_out,
1682 VLC_FALSE, VLC_FALSE );
1684 p_input->p->i_rate = i_rate;
1686 input_EsOutSetRate( p_input->p->p_es_out );
1688 b_force_update = VLC_TRUE;
1693 case INPUT_CONTROL_SET_PROGRAM:
1694 /* No need to force update, es_out does it if needed */
1695 es_out_Control( p_input->p->p_es_out,
1696 ES_OUT_SET_GROUP, val.i_int );
1698 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1702 case INPUT_CONTROL_SET_ES:
1703 /* No need to force update, es_out does it if needed */
1704 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1705 input_EsOutGetFromID( p_input->p->p_es_out,
1709 case INPUT_CONTROL_SET_AUDIO_DELAY:
1710 input_EsOutSetDelay( p_input->p->p_es_out,
1711 AUDIO_ES, val.i_time );
1712 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1715 case INPUT_CONTROL_SET_SPU_DELAY:
1716 input_EsOutSetDelay( p_input->p->p_es_out,
1717 SPU_ES, val.i_time );
1718 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1721 case INPUT_CONTROL_SET_TITLE:
1722 case INPUT_CONTROL_SET_TITLE_NEXT:
1723 case INPUT_CONTROL_SET_TITLE_PREV:
1724 if( p_input->p->input.b_title_demux &&
1725 p_input->p->input.i_title > 0 )
1728 /* FIXME handle demux title */
1729 demux_t *p_demux = p_input->p->input.p_demux;
1732 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1733 i_title = p_demux->info.i_title - 1;
1734 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1735 i_title = p_demux->info.i_title + 1;
1737 i_title = val.i_int;
1739 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1741 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1742 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1744 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1745 input_ControlVarTitle( p_input, i_title );
1748 else if( p_input->p->input.i_title > 0 )
1750 access_t *p_access = p_input->p->input.p_access;
1753 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1754 i_title = p_access->info.i_title - 1;
1755 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1756 i_title = p_access->info.i_title + 1;
1758 i_title = val.i_int;
1760 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1762 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1763 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1765 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1766 stream_AccessReset( p_input->p->input.p_stream );
1770 case INPUT_CONTROL_SET_SEEKPOINT:
1771 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1772 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1773 if( p_input->p->input.b_title_demux &&
1774 p_input->p->input.i_title > 0 )
1776 demux_t *p_demux = p_input->p->input.p_demux;
1778 int64_t i_input_time;
1779 int64_t i_seekpoint_time;
1781 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1783 i_seekpoint = p_demux->info.i_seekpoint;
1784 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1785 if( i_seekpoint_time >= 0 &&
1786 !demux2_Control( p_demux,
1787 DEMUX_GET_TIME, &i_input_time ) )
1789 if ( i_input_time < i_seekpoint_time + 3000000 )
1795 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1796 i_seekpoint = p_demux->info.i_seekpoint + 1;
1798 i_seekpoint = val.i_int;
1800 if( i_seekpoint >= 0 && i_seekpoint <
1801 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1803 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1804 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1806 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1809 else if( p_input->p->input.i_title > 0 )
1811 demux_t *p_demux = p_input->p->input.p_demux;
1812 access_t *p_access = p_input->p->input.p_access;
1814 int64_t i_input_time;
1815 int64_t i_seekpoint_time;
1817 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1819 i_seekpoint = p_access->info.i_seekpoint;
1820 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1821 if( i_seekpoint_time >= 0 &&
1822 demux2_Control( p_demux,
1823 DEMUX_GET_TIME, &i_input_time ) )
1825 if ( i_input_time < i_seekpoint_time + 3000000 )
1831 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1832 i_seekpoint = p_access->info.i_seekpoint + 1;
1834 i_seekpoint = val.i_int;
1836 if( i_seekpoint >= 0 && i_seekpoint <
1837 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1839 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1840 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1842 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1844 stream_AccessReset( p_input->p->input.p_stream );
1849 case INPUT_CONTROL_ADD_SLAVE:
1850 if( val.psz_string )
1852 input_source_t *slave = InputSourceNew( p_input );
1854 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1860 msg_Dbg( p_input, "adding %s as slave on the fly",
1864 if( demux2_Control( p_input->p->input.p_demux,
1865 DEMUX_GET_TIME, &i_time ) )
1867 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1868 InputSourceClean( slave );
1872 if( demux2_Control( slave->p_demux,
1873 DEMUX_SET_TIME, i_time ) )
1875 msg_Err( p_input, "seek failed for new slave" );
1876 InputSourceClean( slave );
1881 /* Get meta (access and demux) */
1882 p_meta = vlc_meta_New();
1883 access2_Control( slave->p_access, ACCESS_GET_META,
1885 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1886 InputUpdateMeta( p_input, p_meta );
1888 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1893 msg_Warn( p_input, "failed to add %s as slave",
1897 free( val.psz_string );
1901 case INPUT_CONTROL_SET_BOOKMARK:
1903 msg_Err( p_input, "not yet implemented" );
1907 return b_force_update;
1910 /*****************************************************************************
1912 *****************************************************************************/
1913 static int UpdateFromDemux( input_thread_t *p_input )
1915 demux_t *p_demux = p_input->p->input.p_demux;
1918 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1920 v.i_int = p_demux->info.i_title;
1921 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1923 input_ControlVarTitle( p_input, p_demux->info.i_title );
1925 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1927 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1929 v.i_int = p_demux->info.i_seekpoint;
1930 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1932 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1934 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1936 /* Hmmm only works with master input */
1937 if( p_input->p->input.p_demux == p_demux )
1939 int i_title_end = p_input->p->input.i_title_end -
1940 p_input->p->input.i_title_offset;
1941 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1942 p_input->p->input.i_seekpoint_offset;
1944 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1946 if( p_demux->info.i_title > i_title_end ||
1947 ( p_demux->info.i_title == i_title_end &&
1948 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1950 else if( i_seekpoint_end >=0 )
1952 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1954 else if( i_title_end >= 0 )
1956 if( p_demux->info.i_title > i_title_end ) return 0;
1963 /*****************************************************************************
1965 *****************************************************************************/
1966 static int UpdateFromAccess( input_thread_t *p_input )
1968 access_t *p_access = p_input->p->input.p_access;
1971 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1973 v.i_int = p_access->info.i_title;
1974 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1976 input_ControlVarTitle( p_input, p_access->info.i_title );
1978 stream_AccessUpdate( p_input->p->input.p_stream );
1980 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1982 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1984 v.i_int = p_access->info.i_seekpoint;
1985 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1986 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1988 if( p_access->info.i_update & INPUT_UPDATE_META )
1990 /* TODO maybe multi - access ? */
1991 vlc_meta_t *p_meta = vlc_meta_New();
1992 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
1993 InputUpdateMeta( p_input, p_meta );
1994 var_SetBool( p_input, "item-change", p_input->p->input.p_item->i_id );
1995 p_access->info.i_update &= ~INPUT_UPDATE_META;
1998 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2000 /* Hmmm only works with master input */
2001 if( p_input->p->input.p_access == p_access )
2003 int i_title_end = p_input->p->input.i_title_end -
2004 p_input->p->input.i_title_offset;
2005 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2006 p_input->p->input.i_seekpoint_offset;
2008 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2010 if( p_access->info.i_title > i_title_end ||
2011 ( p_access->info.i_title == i_title_end &&
2012 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2014 else if( i_seekpoint_end >=0 )
2016 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2018 else if( i_title_end >= 0 )
2020 if( p_access->info.i_title > i_title_end ) return 0;
2027 /*****************************************************************************
2029 *****************************************************************************/
2030 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2032 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2033 p_input->p->input.p_item->i_duration = i_length;
2034 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2036 if( !p_input->b_preparsing )
2038 pl_Yield( p_input );
2039 var_SetInteger( pl_Get( p_input ), "item-change",
2040 p_input->p->input.p_item->i_id );
2041 pl_Release( p_input )
2045 /*****************************************************************************
2047 *****************************************************************************/
2048 static input_source_t *InputSourceNew( input_thread_t *p_input )
2050 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2054 msg_Err( p_input, "out of memory for new input source" );
2059 in->p_access = NULL;
2060 in->p_stream = NULL;
2062 in->b_title_demux = VLC_FALSE;
2063 TAB_INIT( in->i_title, in->title );
2064 in->b_can_pace_control = VLC_TRUE;
2065 in->b_eof = VLC_FALSE;
2066 in->i_cr_average = 0;
2071 /*****************************************************************************
2073 *****************************************************************************/
2074 static int InputSourceInit( input_thread_t *p_input,
2075 input_source_t *in, const char *psz_mrl,
2076 const char *psz_forced_demux )
2078 char psz_dup[strlen (psz_mrl) + 1];
2079 const char *psz_access;
2080 const char *psz_demux;
2086 strcpy( psz_dup, psz_mrl );
2088 if( !in ) return VLC_EGENERIC;
2089 if( !p_input ) return VLC_EGENERIC;
2092 if( !p_input->b_preparsing )
2094 MRLSplit( VLC_OBJECT(p_input), psz_dup,
2095 &psz_access, &psz_demux, &psz_path );
2097 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2098 psz_mrl, psz_access, psz_demux, psz_path );
2100 /* Hack to allow udp://@:port syntax */
2102 (strncmp( psz_access, "udp", 3 ) &&
2103 strncmp( psz_access, "rtp", 3 )) )
2105 /* Find optional titles and seekpoints */
2106 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2107 &in->i_seekpoint_start, &in->i_seekpoint_end );
2109 if( psz_forced_demux && *psz_forced_demux )
2111 psz_demux = psz_forced_demux;
2113 else if( !psz_demux || *psz_demux == '\0' )
2115 /* special hack for forcing a demuxer with --demux=module
2116 * (and do nothing with a list) */
2117 char *psz_var_demux = var_GetString( p_input, "demux" );
2119 if( *psz_var_demux != '\0' &&
2120 !strchr(psz_var_demux, ',' ) &&
2121 !strchr(psz_var_demux, ':' ) )
2123 psz_demux = psz_var_demux;
2125 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2127 else if( psz_var_demux )
2129 free( psz_var_demux );
2133 /* Try access_demux if no demux given */
2134 if( *psz_demux == '\0' )
2136 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2137 NULL, p_input->p->p_es_out, VLC_FALSE );
2143 if( !strncmp( psz_path, "file://", 7 ) )
2145 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2147 psz_access = "file";
2152 int64_t i_pts_delay;
2154 /* Get infos from access_demux */
2155 demux2_Control( in->p_demux,
2156 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2157 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2159 in->b_title_demux = VLC_TRUE;
2160 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2161 &in->title, &in->i_title,
2162 &in->i_title_offset, &in->i_seekpoint_offset ) )
2167 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2168 &in->b_can_pace_control );
2169 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2173 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2179 int64_t i_pts_delay;
2181 input_ChangeState( p_input, OPENING_S );
2183 /* Now try a real access */
2184 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2185 p_input->b_preparsing );
2187 /* Access failed, URL encoded ? */
2188 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2190 decode_URI( psz_path );
2192 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2193 psz_access, psz_demux, psz_path );
2195 in->p_access = access2_New( p_input,
2196 psz_access, psz_demux, psz_path,
2197 p_input->b_preparsing );
2199 /* access failed, maybe our access detection was wrong.
2200 * Retry with the full name */
2201 if( in->p_access == NULL && strchr( psz_mrl, ':' ) )
2203 msg_Dbg( p_input, "retrying with access `' demux `' path `%s'",
2205 in->p_access = access2_New( p_input,
2207 p_input->b_preparsing );
2209 if( in->p_access == NULL )
2211 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2213 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2214 _("Your input can't be opened"),
2215 _("VLC is unable to open the MRL '%s'."
2216 " Check the log for details."), psz_mrl );
2221 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2222 while( psz && *psz )
2224 access_t *p_access = in->p_access;
2225 char *end = strchr( psz, ':' );
2230 in->p_access = access2_FilterNew( in->p_access, psz );
2231 if( in->p_access == NULL )
2233 in->p_access = p_access;
2234 msg_Warn( p_input, "failed to insert access filter %s",
2240 if( psz_tmp ) free( psz_tmp );
2242 /* Get infos from access */
2243 if( !p_input->b_preparsing )
2245 access2_Control( in->p_access,
2246 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2247 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2249 in->b_title_demux = VLC_FALSE;
2250 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2251 &in->title, &in->i_title,
2252 &in->i_title_offset, &in->i_seekpoint_offset ) )
2258 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2259 &in->b_can_pace_control );
2260 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2262 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2264 var_Set( p_input, "seekable", val );
2267 input_ChangeState( p_input, BUFFERING_S );
2269 /* Create the stream_t */
2270 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2271 if( in->p_stream == NULL )
2273 msg_Warn( p_input, "cannot create a stream_t from access" );
2277 /* Open a demuxer */
2278 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2280 psz_demux = in->p_access->psz_demux;
2284 /* Take access redirections into account */
2285 char *psz_real_path;
2286 char *psz_buf = NULL;
2287 if( in->p_access->psz_path )
2289 const char *psz_a, *psz_d;
2290 psz_buf = strdup( in->p_access->psz_path );
2291 MRLSplit( VLC_OBJECT(p_input), psz_buf,
2292 &psz_a, &psz_d, &psz_real_path );
2296 psz_real_path = psz_path;
2298 in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2300 in->p_stream, p_input->p->p_es_out,
2301 p_input->b_preparsing );
2305 if( in->p_demux == NULL )
2307 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2308 psz_access, psz_demux, psz_path );
2309 intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2310 _("Can't recognize the input's format"),
2311 _("The format of '%s' can't be detected. "
2312 "Have a look the log for details."), psz_mrl );
2316 /* Get title from demux */
2317 if( !p_input->b_preparsing && in->i_title <= 0 )
2319 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2320 &in->title, &in->i_title,
2321 &in->i_title_offset, &in->i_seekpoint_offset ))
2323 TAB_INIT( in->i_title, in->title );
2327 in->b_title_demux = VLC_TRUE;
2331 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2332 if( 1 || !p_input->b_preparsing )
2335 input_attachment_t **attachment;
2336 if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2337 &attachment, &i_attachment ) )
2340 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2341 p_input->p->attachment = realloc( p_input->p->attachment,
2342 sizeof(input_attachment_t**) * ( p_input->p->i_attachment + i_attachment ) );
2343 for( i = 0; i < i_attachment; i++ )
2344 p_input->p->attachment[p_input->p->i_attachment++] = attachment[i];
2347 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2352 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2353 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2358 input_ChangeState( p_input, ERROR_S );
2361 demux2_Delete( in->p_demux );
2364 stream_Delete( in->p_stream );
2367 access2_Delete( in->p_access );
2369 return VLC_EGENERIC;
2372 /*****************************************************************************
2374 *****************************************************************************/
2375 static void InputSourceClean( input_source_t *in )
2380 demux2_Delete( in->p_demux );
2383 stream_Delete( in->p_stream );
2386 access2_Delete( in->p_access );
2388 if( in->i_title > 0 )
2390 for( i = 0; i < in->i_title; i++ )
2391 vlc_input_title_Delete( in->title[i] );
2392 TAB_CLEAN( in->i_title, in->title );
2396 static void SlaveDemux( input_thread_t *p_input )
2401 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2403 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2407 for( i = 0; i < p_input->p->i_slave; i++ )
2409 input_source_t *in = p_input->p->slave[i];
2415 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2420 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2422 msg_Err( p_input, "slave[%d] doesn't like "
2423 "DEMUX_GET_TIME -> EOF", i );
2428 if( i_stime >= i_time )
2431 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2437 i_ret = in->p_demux->pf_demux( in->p_demux );
2442 msg_Dbg( p_input, "slave %d EOF", i );
2443 in->b_eof = VLC_TRUE;
2448 static void SlaveSeek( input_thread_t *p_input )
2453 if( !p_input ) return;
2455 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2457 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2461 for( i = 0; i < p_input->p->i_slave; i++ )
2463 input_source_t *in = p_input->p->slave[i];
2465 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2467 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2468 in->b_eof = VLC_TRUE;
2473 /*****************************************************************************
2475 *****************************************************************************/
2476 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2480 if( !p_meta ) return;
2482 /* Get meta information from user */
2483 #define GET_META( field, s ) \
2484 var_Get( p_input, (s), &val ); \
2485 if( *val.psz_string ) \
2486 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2487 free( val.psz_string )
2489 GET_META( Title, "meta-title" );
2490 GET_META( Artist, "meta-artist" );
2491 GET_META( Genre, "meta-genre" );
2492 GET_META( Copyright, "meta-copyright" );
2493 GET_META( Description, "meta-description" );
2494 GET_META( Date, "meta-date" );
2495 GET_META( URL, "meta-url" );
2499 /*****************************************************************************
2500 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2501 * arturl and locking issue.
2502 *****************************************************************************/
2503 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2505 input_item_t *p_item = p_input->p->input.p_item;
2506 char * psz_arturl = NULL;
2507 char *psz_title = NULL;
2509 int i_arturl_event = VLC_FALSE;
2514 psz_arturl = input_item_GetArtURL( p_item );
2516 vlc_mutex_lock( &p_item->lock );
2517 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2518 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2520 vlc_meta_Merge( p_item->p_meta, p_meta );
2522 if( psz_arturl && *psz_arturl )
2524 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2525 i_arturl_event = VLC_TRUE;
2528 vlc_meta_Delete( p_meta );
2530 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2532 /* Don't look for art cover if sout
2533 * XXX It can change when sout has meta data support */
2534 if( p_input->p->p_sout && !p_input->b_preparsing )
2536 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2537 i_arturl_event = VLC_TRUE;
2541 input_ExtractAttachmentAndCacheArt( p_input );
2545 input_item_SetPreparsed( p_item, VLC_TRUE );
2549 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2551 p_meta = vlc_meta_New();
2552 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2554 vlc_mutex_unlock( &p_item->lock );
2556 if( i_arturl_event == VLC_TRUE )
2560 /* Notify interested third parties */
2561 event.type = vlc_InputItemMetaChanged;
2562 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2563 vlc_event_send( &p_item->event_manager, &event );
2568 input_Control( p_input, INPUT_SET_NAME, psz_title );
2574 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2575 for( i = 0; ppsz_all_keys[i]; i++ )
2577 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2578 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2579 free( ppsz_all_keys[i] );
2581 free( ppsz_all_keys );
2582 vlc_meta_Delete( p_meta );
2585 /** \todo handle sout meta */
2589 static inline vlc_bool_t IsValidAccess( const char *psz )
2593 while( ( c = *psz ) != '\0' )
2598 if( ( !isascii( c ) || !isalnum( c ) ) && c != '-' && ( c != '/' ) )
2602 /* should not happen though */
2607 /*****************************************************************************
2608 * MRLSplit: parse the access, demux and url part of the
2609 * Media Resource Locator.
2610 *****************************************************************************/
2611 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2612 const char **ppsz_access, const char **ppsz_demux,
2615 const char *psz_access = "";
2616 const char *psz_demux = "";
2620 psz = strchr( psz_dup, ':' );
2624 /* Guess whether ':' is part of a local filename, or separates
2625 * access/demux from path */
2626 if( !IsValidAccess( psz_dup ) )
2628 #if defined( WIN32 ) || defined( UNDER_CE )
2629 else if( ( psz - psz_dup == 1 ) && isalpha( psz_dup[0] ) )
2631 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2640 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2644 psz = strchr( psz_dup, '/' );
2651 psz_access = psz_dup;
2658 *ppsz_access = psz_access;
2659 *ppsz_demux = psz_demux;
2660 *ppsz_path = psz_path;
2663 /*****************************************************************************
2664 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2667 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2668 *****************************************************************************/
2669 static void MRLSections( input_thread_t *p_input, char *psz_source,
2670 int *pi_title_start, int *pi_title_end,
2671 int *pi_chapter_start, int *pi_chapter_end )
2673 char *psz, *psz_end, *psz_next, *psz_check;
2675 *pi_title_start = *pi_title_end = -1;
2676 *pi_chapter_start = *pi_chapter_end = -1;
2678 /* Start by parsing titles and chapters */
2679 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2681 /* Check we are really dealing with a title/chapter section */
2682 psz_check = psz + 1;
2683 if( !*psz_check ) return;
2684 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2685 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2686 if( *psz_check == ':' && ++psz_check )
2687 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2688 if( *psz_check != '-' && *psz_check ) return;
2689 if( *psz_check == '-' && ++psz_check )
2690 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2691 if( *psz_check != ':' && *psz_check ) return;
2692 if( *psz_check == ':' && ++psz_check )
2693 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2694 if( *psz_check ) return;
2696 /* Separate start and end */
2698 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2700 /* Look for the start title */
2701 *pi_title_start = strtol( psz, &psz_next, 0 );
2702 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2703 *pi_title_end = *pi_title_start;
2706 /* Look for the start chapter */
2708 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2709 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2710 *pi_chapter_end = *pi_chapter_start;
2714 /* Look for the end title */
2715 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2716 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2719 /* Look for the end chapter */
2720 if( *psz_end ) psz_end++;
2721 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2722 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2725 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2726 psz_source, *pi_title_start, *pi_chapter_start,
2727 *pi_title_end, *pi_chapter_end );
2730 /*****************************************************************************
2731 * input_AddSubtitles: add a subtitles file and enable it
2732 *****************************************************************************/
2733 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2734 vlc_bool_t b_check_extension )
2736 input_source_t *sub;
2739 char *psz_path, *psz_extension;
2741 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2746 /* if we are provided a subtitle.sub file,
2747 * see if we don't have a subtitle.idx and use it instead */
2748 psz_path = strdup( psz_subtitle );
2751 psz_extension = strrchr( psz_path, '.');
2752 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2756 strcpy( psz_extension, ".idx" );
2757 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2758 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2761 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2762 psz_path, psz_subtitle );
2763 strcpy( psz_subtitle, psz_path );
2769 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2771 sub = InputSourceNew( p_input );
2772 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2774 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2777 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2779 if( count.i_int == 0 )
2781 /* if it was first one, there is disable too */
2783 if( count.i_int < list.p_list->i_count )
2785 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2786 &list.p_list->p_values[count.i_int] );
2788 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );