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 vlc_mutex_lock( &p_item->lock );
258 input_item_SetNowPlaying( p_item, NULL );
259 vlc_mutex_unlock( &p_item->lock );
262 if( p_input->b_preparsing )
263 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
267 p_input->p->p_sout = p_sout;
269 /* Attach only once we are ready */
270 vlc_object_attach( p_input, p_parent );
275 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout )
277 vlc_object_detach( p_input );
281 if( p_input->p->p_sout )
284 *pp_sout = p_input->p->p_sout;
285 else if( p_input->p->b_sout_keep )
286 SoutKeep( p_input->p->p_sout );
288 sout_DeleteInstance( p_input->p->p_sout );
291 vlc_mutex_destroy( &p_input->p->lock_control );
294 vlc_object_destroy( p_input );
298 * Initialize an input thread and run it. You will need to monitor the
299 * thread to clean up after it is done
301 * \param p_parent a vlc_object
302 * \param p_item an input item
303 * \return a pointer to the spawned input thread
305 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
306 input_item_t *p_item )
308 vlc_bool_t b_sout_keep;
309 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
310 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
312 if( !p_input && p_sout )
315 p_input->p->b_sout_keep = b_sout_keep;
320 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
321 input_item_t *p_item,
322 const char *psz_log, sout_instance_t *p_sout )
324 input_thread_t *p_input;
326 p_input = Create( p_parent, p_item, psz_log, VLC_FALSE, p_sout );
330 /* Create thread and wait for its readiness. */
331 if( vlc_thread_create( p_input, "input", Run,
332 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
334 input_ChangeState( p_input, ERROR_S );
335 msg_Err( p_input, "cannot create input thread" );
336 Destroy( p_input, &p_sout );
344 * Initialize an input thread and run it. This thread will clean after himself,
345 * you can forget about it. It can work either in blocking or non-blocking mode
347 * \param p_parent a vlc_object
348 * \param p_item an input item
349 * \param b_block should we block until read is finished ?
350 * \return the input object id if non blocking, an error code else
352 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
355 vlc_bool_t b_sout_keep;
356 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
357 input_thread_t *p_input;
359 p_input = Create( p_parent, p_item, NULL, VLC_FALSE, p_sout );
360 if( !p_input && p_sout )
365 p_input->p->b_sout_keep = b_sout_keep;
369 RunAndDestroy( p_input );
374 if( vlc_thread_create( p_input, "input", RunAndDestroy,
375 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
377 input_ChangeState( p_input, ERROR_S );
378 msg_Err( p_input, "cannot create input thread" );
379 Destroy( p_input, NULL );
383 return p_input->i_object_id;
387 * Initialize an input and initialize it to preparse the item
388 * This function is blocking. It will only accept to parse files
390 * \param p_parent a vlc_object_t
391 * \param p_item an input item
392 * \return VLC_SUCCESS or an error
394 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
396 input_thread_t *p_input;
398 /* Allocate descriptor */
399 p_input = Create( p_parent, p_item, NULL, VLC_TRUE, NULL );
403 if( !Init( p_input ) )
406 Destroy( p_input, NULL );
412 * Request a running input thread to stop and die
414 * \param the input thread to stop
416 void input_StopThread( input_thread_t *p_input )
421 /* Set die for input */
422 vlc_object_kill( p_input );
423 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
425 /* We cannot touch p_input fields directly (we come from another thread),
426 * so use the vlc_object_find way, it's perfectly safe */
428 /* Set die for all access */
429 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
430 for( i = 0; i < p_list->i_count; i++ )
432 vlc_object_kill( p_list->p_values[i].p_object );
434 vlc_list_release( p_list );
436 /* Set die for all stream */
437 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
438 for( i = 0; i < p_list->i_count; i++ )
440 vlc_object_kill( p_list->p_values[i].p_object );
442 vlc_list_release( p_list );
444 /* Set die for all demux */
445 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
446 for( i = 0; i < p_list->i_count; i++ )
448 vlc_object_kill( p_list->p_values[i].p_object );
450 vlc_list_release( p_list );
452 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
456 * Clean up a dead input thread
457 * This function does not return until the thread is effectively cancelled.
459 * \param the input thread to kill
461 void input_DestroyThread( input_thread_t *p_input )
463 input_DestroyThreadExtended( p_input, NULL );
466 void input_DestroyThreadExtended( input_thread_t *p_input, sout_instance_t **pp_sout )
468 /* Join the thread */
469 vlc_thread_join( p_input );
472 Destroy( p_input, pp_sout );
475 /*****************************************************************************
476 * Run: main thread loop
477 * This is the "normal" thread that spawns the input processing chain,
478 * reads the stream, cleans up and waits
479 *****************************************************************************/
480 static int Run( input_thread_t *p_input )
482 /* Signal that the thread is launched */
483 vlc_thread_ready( p_input );
485 if( Init( p_input ) )
487 /* If we failed, wait before we are killed, and exit */
488 p_input->b_error = VLC_TRUE;
489 playlist_Signal( pl_Get( p_input ) );
493 /* Tell we're dead */
494 p_input->b_dead = VLC_TRUE;
501 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
503 /* We have finish to demux data but not to play them */
504 while( !p_input->b_die )
506 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
509 msg_Dbg( p_input, "waiting decoder fifos to empty" );
511 msleep( INPUT_IDLE_SLEEP );
514 /* We have finished */
515 p_input->b_eof = VLC_TRUE;
516 playlist_Signal( pl_Get( p_input ) );
519 /* Wait until we are asked to die */
520 if( !p_input->b_die )
531 /*****************************************************************************
532 * RunAndDestroy: main thread loop
533 * This is the "just forget me" thread that spawns the input processing chain,
534 * reads the stream, cleans up and releases memory
535 *****************************************************************************/
536 static int RunAndDestroy( input_thread_t *p_input )
538 /* Signal that the thread is launched */
539 vlc_thread_ready( p_input );
541 if( Init( p_input ) )
546 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
548 /* We have finished demuxing data but not playing it */
549 while( !p_input->b_die )
551 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
554 msg_Dbg( p_input, "waiting decoder fifos to empty" );
556 msleep( INPUT_IDLE_SLEEP );
559 /* We have finished */
560 p_input->b_eof = VLC_TRUE;
568 Destroy( p_input, NULL );
572 /*****************************************************************************
573 * Main loop: Fill buffers from access, and demux
574 *****************************************************************************/
575 static void MainLoop( input_thread_t *p_input )
577 int64_t i_start_mdate = mdate();
578 int64_t i_intf_update = 0;
581 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
583 vlc_bool_t b_force_update = VLC_FALSE;
589 if( p_input->i_state != PAUSE_S )
591 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
592 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
595 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
600 if( p_input->p->input.b_title_demux &&
601 p_input->p->input.p_demux->info.i_update )
603 i_ret = UpdateFromDemux( p_input );
604 b_force_update = VLC_TRUE;
606 else if( !p_input->p->input.b_title_demux &&
607 p_input->p->input.p_access &&
608 p_input->p->input.p_access->info.i_update )
610 i_ret = UpdateFromAccess( p_input );
611 b_force_update = VLC_TRUE;
615 if( i_ret == 0 ) /* EOF */
619 var_Get( p_input, "input-repeat", &repeat );
620 if( repeat.i_int == 0 )
622 /* End of file - we do not set b_die because only the
623 * playlist is allowed to do so. */
624 input_ChangeState( p_input, END_S );
625 msg_Dbg( p_input, "EOF reached" );
626 p_input->p->input.b_eof = VLC_TRUE;
630 msg_Dbg( p_input, "repeating the same input (%d)",
632 if( repeat.i_int > 0 )
635 var_Set( p_input, "input-repeat", repeat );
638 /* Seek to start title/seekpoint */
639 val.i_int = p_input->p->input.i_title_start -
640 p_input->p->input.i_title_offset;
641 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
643 input_ControlPush( p_input,
644 INPUT_CONTROL_SET_TITLE, &val );
646 val.i_int = p_input->p->input.i_seekpoint_start -
647 p_input->p->input.i_seekpoint_offset;
648 if( val.i_int > 0 /* TODO: check upper boundary */ )
649 input_ControlPush( p_input,
650 INPUT_CONTROL_SET_SEEKPOINT, &val );
652 /* Seek to start position */
653 if( p_input->p->i_start > 0 )
655 val.i_time = p_input->p->i_start;
656 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
662 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
667 i_start_mdate = mdate();
672 p_input->b_error = VLC_TRUE;
675 if( i_ret > 0 && p_input->p->i_slave > 0 )
677 SlaveDemux( p_input );
687 vlc_mutex_lock( &p_input->p->lock_control );
688 ControlReduce( p_input );
689 while( !ControlPopNoLock( p_input, &i_type, &val ) )
691 msg_Dbg( p_input, "control type=%d", i_type );
692 if( Control( p_input, i_type, val ) )
693 b_force_update = VLC_TRUE;
695 vlc_mutex_unlock( &p_input->p->lock_control );
697 if( b_force_update || i_intf_update < mdate() )
701 int64_t i_time, i_length;
702 /* update input status variables */
703 if( !demux2_Control( p_input->p->input.p_demux,
704 DEMUX_GET_POSITION, &f_pos ) )
706 val.f_float = (float)f_pos;
707 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
709 if( !demux2_Control( p_input->p->input.p_demux,
710 DEMUX_GET_TIME, &i_time ) )
712 p_input->i_time = i_time;
714 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
716 if( !demux2_Control( p_input->p->input.p_demux,
717 DEMUX_GET_LENGTH, &i_length ) )
720 var_Get( p_input, "length", &old_val );
721 val.i_time = i_length;
722 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
724 if( old_val.i_time != val.i_time )
726 UpdateItemLength( p_input, i_length );
730 var_SetBool( p_input, "intf-change", VLC_TRUE );
731 i_intf_update = mdate() + I64C(150000);
733 /* 150ms * 8 = ~ 1 second */
734 if( ++i_updates % 8 == 0 )
736 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
737 /* Are we the thread responsible for computing global stats ? */
738 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
740 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
741 p_input->p_libvlc->p_playlist->p_stats );
747 static int Init( input_thread_t * p_input )
757 /* Initialize optional stream output. (before access/demuxer)
758 * XXX: we add a special case if the uri starts by vlc.
759 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
760 * be destroyed in case of a file).
761 * (this will break playing of file starting by 'vlc:' but I don't
762 * want to add more logic, just force file by file:// or code it ;)
764 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
765 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
767 if( !p_input->b_preparsing )
769 /* Prepare statistics */
770 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
771 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
772 if( p_input->p_libvlc->b_stats )
774 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
775 INIT_COUNTER( read_packets, INTEGER, COUNTER );
776 INIT_COUNTER( demux_read, INTEGER, COUNTER );
777 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
778 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
779 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
780 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
781 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
782 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
783 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
784 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
785 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
786 p_input->p->counters.p_sout_send_bitrate = NULL;
787 p_input->p->counters.p_sout_sent_packets = NULL;
788 p_input->p->counters.p_sout_sent_bytes = NULL;
789 if( p_input->p->counters.p_demux_bitrate )
790 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
791 if( p_input->p->counters.p_input_bitrate )
792 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
795 /* Find a usable sout and attach it to p_input */
796 psz = var_GetString( p_input, "sout" );
797 if( *psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
799 /* Check the validity of the provided sout */
800 if( p_input->p->p_sout )
802 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
804 msg_Dbg( p_input, "destroying unusable sout" );
806 sout_DeleteInstance( p_input->p->p_sout );
807 p_input->p->p_sout = NULL;
811 if( p_input->p->p_sout )
814 msg_Dbg( p_input, "sout keep: reusing sout" );
815 msg_Dbg( p_input, "sout keep: you probably want to use "
816 "gather stream_out" );
817 vlc_object_attach( p_input->p->p_sout, p_input );
821 /* Create a new one */
822 p_input->p->p_sout = sout_NewInstance( p_input, psz );
824 if( !p_input->p->p_sout )
826 input_ChangeState( p_input, ERROR_S );
827 msg_Err( p_input, "cannot start stream output instance, " \
834 if( p_input->p_libvlc->b_stats )
836 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
837 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
838 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
839 if( p_input->p->counters.p_sout_send_bitrate )
840 p_input->p->counters.p_sout_send_bitrate->update_interval =
844 else if( p_input->p->p_sout )
846 msg_Dbg( p_input, "destroying useless sout" );
848 sout_DeleteInstance( p_input->p->p_sout );
849 p_input->p->p_sout = NULL;
855 p_input->p->p_es_out = input_EsOutNew( p_input );
856 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
857 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
859 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
860 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
862 if( InputSourceInit( p_input, &p_input->p->input,
863 p_input->p->input.p_item->psz_uri, NULL ) )
868 /* Create global title (from master) */
869 if( !p_input->b_preparsing )
871 p_input->p->i_title = p_input->p->input.i_title;
872 p_input->p->title = p_input->p->input.title;
873 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
874 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
875 if( p_input->p->i_title > 0 )
877 /* Setup variables */
878 input_ControlVarNavigation( p_input );
879 input_ControlVarTitle( p_input, 0 );
883 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
884 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
887 if( p_input->i_pts_delay < 0 )
888 p_input->i_pts_delay = 0;
890 /* If the desynchronisation requested by the user is < 0, we need to
891 * cache more data. */
892 var_Get( p_input, "audio-desync", &val );
893 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
895 /* Update cr_average depending on the caching */
896 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
897 p_input->p->input.i_cr_average /= 10;
898 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
901 /* Load master infos */
903 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
904 &val.i_time ) && val.i_time > 0 )
906 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
907 UpdateItemLength( p_input, val.i_time );
910 /* Start title/chapter */
911 if( !p_input->b_preparsing )
913 val.i_int = p_input->p->input.i_title_start -
914 p_input->p->input.i_title_offset;
915 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
916 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
917 val.i_int = p_input->p->input.i_seekpoint_start -
918 p_input->p->input.i_seekpoint_offset;
919 if( val.i_int > 0 /* TODO: check upper boundary */ )
920 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
924 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
925 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
926 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
927 if( p_input->p->i_run < 0 )
929 msg_Warn( p_input, "invalid run-time ignored" );
930 p_input->p->i_run = 0;
933 if( p_input->p->i_start > 0 )
935 if( p_input->p->i_start >= val.i_time )
937 msg_Warn( p_input, "invalid start-time ignored" );
943 msg_Dbg( p_input, "starting at time: %ds",
944 (int)( p_input->p->i_start / I64C(1000000) ) );
946 s.i_time = p_input->p->i_start;
947 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
950 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
952 msg_Warn( p_input, "invalid stop-time ignored" );
953 p_input->p->i_stop = 0;
957 /* Get fps and set it if not already set */
958 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
961 float f_requested_fps;
963 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
964 var_SetFloat( p_input, "sub-original-fps", f_fps );
966 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
967 if( f_requested_fps != f_fps )
969 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
971 var_SetFloat( p_input, "sub-fps", f_requested_fps );
975 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
978 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
981 /* Look for and add subtitle files */
982 psz_subtitle = var_GetString( p_input, "sub-file" );
985 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
986 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
989 var_Get( p_input, "sub-autodetect-file", &val );
992 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
993 char **subs = subtitles_Detect( p_input, psz_autopath,
994 p_input->p->input.p_item->psz_uri );
998 /* Try to autoselect the first autodetected subtitles file
999 * if no subtitles file was specified */
1000 if( *psz_subtitle == 0 && subs && subs[0] )
1002 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
1007 /* Then, just add the following subtitles files */
1008 for( ; subs && subs[i]; i++ )
1010 if( strcmp( psz_subtitle, subs[i] ) )
1012 sub = InputSourceNew( p_input );
1013 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1015 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1021 if( subs ) free( subs );
1022 if( psz_autopath ) free( psz_autopath );
1024 free( psz_subtitle );
1026 /* Look for slave */
1027 psz = var_GetString( p_input, "input-slave" );
1031 input_source_t *slave;
1032 while( psz && *psz )
1034 while( *psz == ' ' || *psz == '#' )
1038 if( ( psz_delim = strchr( psz, '#' ) ) )
1040 *psz_delim++ = '\0';
1047 msg_Dbg( p_input, "adding slave input '%s'", psz );
1048 slave = InputSourceNew( p_input );
1049 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1051 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1057 if( psz ) free( psz );
1061 p_input->p->i_start = 0;
1062 p_input->p->i_start = 0;
1066 if( !p_input->b_preparsing )
1068 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1069 i_es_out_mode = ES_OUT_MODE_AUTO;
1071 if( p_input->p->p_sout )
1073 var_Get( p_input, "sout-all", &val );
1076 i_es_out_mode = ES_OUT_MODE_ALL;
1081 var_Get( p_input, "programs", &val );
1082 if ( val.p_list && val.p_list->i_count )
1084 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1085 /* Note : we should remove the "program" callback. */
1088 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1092 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1094 /* Inform the demuxer about waited group (needed only for DVB) */
1095 if( i_es_out_mode == ES_OUT_MODE_ALL )
1097 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1099 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1101 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1106 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1107 (int) var_GetInteger( p_input, "program" ), NULL );
1110 if( p_input->p->p_sout )
1112 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1114 p_input->p->b_out_pace_control = VLC_FALSE;
1118 p_input->p->b_out_pace_control = VLC_TRUE;
1121 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1123 /* We don't want a high input priority here or we'll
1124 * end-up sucking up all the CPU time */
1125 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1128 msg_Dbg( p_input, "starting in %s mode",
1129 p_input->p->b_out_pace_control ? "async" : "sync" );
1133 p_meta = vlc_meta_New();
1134 /* Get meta data from users */
1135 InputMetaUser( p_input, p_meta );
1137 /* Get meta data from master input */
1138 demux2_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
1140 /* Access_file does not give any meta, and there are no slave */
1141 if( !p_input->b_preparsing )
1143 if( p_input->p->input.p_access )
1144 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1147 /* Get meta data from slave input */
1148 for( i = 0; i < p_input->p->i_slave; i++ )
1150 demux2_Control( p_input->p->slave[i]->p_demux,
1151 DEMUX_GET_META, p_meta );
1152 if( p_input->p->slave[i]->p_access )
1154 access2_Control( p_input->p->slave[i]->p_access,
1155 ACCESS_GET_META, p_meta );
1160 InputUpdateMeta( p_input, p_meta );
1162 if( !p_input->b_preparsing )
1164 msg_Dbg( p_input, "`%s' successfully opened",
1165 p_input->p->input.p_item->psz_uri );
1169 /* initialization is complete */
1170 input_ChangeState( p_input, PLAYING_S );
1175 input_ChangeState( p_input, ERROR_S );
1177 if( p_input->p->p_es_out )
1178 input_EsOutDelete( p_input->p->p_es_out );
1180 if( p_input->p->p_sout )
1182 vlc_object_detach( p_input->p->p_sout );
1183 sout_DeleteInstance( p_input->p->p_sout );
1187 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1189 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1190 stats_CounterClean( p_input->p->counters.p_##c );\
1191 p_input->p->counters.p_##c = NULL; } while(0)
1192 EXIT_COUNTER( read_bytes );
1193 EXIT_COUNTER( read_packets );
1194 EXIT_COUNTER( demux_read );
1195 EXIT_COUNTER( input_bitrate );
1196 EXIT_COUNTER( demux_bitrate );
1197 EXIT_COUNTER( played_abuffers );
1198 EXIT_COUNTER( lost_abuffers );
1199 EXIT_COUNTER( displayed_pictures );
1200 EXIT_COUNTER( lost_pictures );
1201 EXIT_COUNTER( decoded_audio );
1202 EXIT_COUNTER( decoded_video );
1203 EXIT_COUNTER( decoded_sub );
1205 if( p_input->p->p_sout )
1207 EXIT_COUNTER( sout_sent_packets );
1208 EXIT_COUNTER (sout_sent_bytes );
1209 EXIT_COUNTER( sout_send_bitrate );
1214 /* Mark them deleted */
1215 p_input->p->input.p_demux = NULL;
1216 p_input->p->input.p_stream = NULL;
1217 p_input->p->input.p_access = NULL;
1218 p_input->p->p_es_out = NULL;
1219 p_input->p->p_sout = NULL;
1221 return VLC_EGENERIC;
1224 /*****************************************************************************
1225 * Error: RunThread() error loop
1226 *****************************************************************************
1227 * This function is called when an error occurred during thread main's loop.
1228 *****************************************************************************/
1229 static void Error( input_thread_t *p_input )
1231 while( !p_input->b_die )
1234 input_ChangeState( p_input, ERROR_S );
1235 msleep( INPUT_IDLE_SLEEP );
1239 /*****************************************************************************
1240 * End: end the input thread
1241 *****************************************************************************/
1242 static void End( input_thread_t * p_input )
1246 /* We are at the end */
1247 input_ChangeState( p_input, END_S );
1249 /* Clean control variables */
1250 input_ControlVarClean( p_input );
1252 /* Clean up master */
1253 InputSourceClean( &p_input->p->input );
1256 for( i = 0; i < p_input->p->i_slave; i++ )
1258 InputSourceClean( p_input->p->slave[i] );
1259 free( p_input->p->slave[i] );
1261 if( p_input->p->slave ) free( p_input->p->slave );
1263 /* Unload all modules */
1264 if( p_input->p->p_es_out )
1265 input_EsOutDelete( p_input->p->p_es_out );
1267 if( !p_input->b_preparsing )
1269 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1270 if( p_input->p_libvlc->b_stats )
1272 /* make sure we are up to date */
1273 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1274 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1276 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1277 p_input->p_libvlc->p_playlist->p_stats );
1278 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1280 CL_CO( read_bytes );
1281 CL_CO( read_packets );
1282 CL_CO( demux_read );
1283 CL_CO( input_bitrate );
1284 CL_CO( demux_bitrate );
1285 CL_CO( played_abuffers );
1286 CL_CO( lost_abuffers );
1287 CL_CO( displayed_pictures );
1288 CL_CO( lost_pictures );
1289 CL_CO( decoded_audio) ;
1290 CL_CO( decoded_video );
1291 CL_CO( decoded_sub) ;
1294 /* Close optional stream output instance */
1295 if( p_input->p->p_sout )
1297 CL_CO( sout_sent_packets );
1298 CL_CO( sout_sent_bytes );
1299 CL_CO( sout_send_bitrate );
1301 vlc_object_detach( p_input->p->p_sout );
1306 if( p_input->p->i_attachment > 0 )
1308 for( i = 0; i < p_input->p->i_attachment; i++ )
1309 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1310 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1313 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1315 /* Tell we're dead */
1316 p_input->b_dead = VLC_TRUE;
1319 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t *pb_sout_keep )
1321 vlc_bool_t b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1322 sout_instance_t *p_sout = NULL;
1325 /* Search sout-keep options
1326 * XXX it has to be done here, but it is duplicated work :( */
1327 vlc_mutex_lock( &p_item->lock );
1328 for( i = 0; i < p_item->i_options; i++ )
1330 const char *psz_option = p_item->ppsz_options[i];
1333 if( *psz_option == ':' )
1336 if( !strcmp( psz_option, "sout-keep" ) )
1337 b_keep_sout = VLC_TRUE;
1338 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1339 b_keep_sout = VLC_FALSE;
1341 vlc_mutex_unlock( &p_item->lock );
1343 /* Find a potential sout to reuse
1344 * XXX it might be unusable but this will be checked later */
1347 /* Remove the sout from the playlist garbage collector */
1348 playlist_t *p_playlist = pl_Yield( p_parent );
1350 vlc_mutex_lock( &p_playlist->gc_lock );
1351 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1354 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1356 vlc_object_release( p_sout );
1361 vlc_object_detach( p_sout ); /* Remove it from the GC */
1363 vlc_object_release( p_sout );
1366 vlc_mutex_unlock( &p_playlist->gc_lock );
1368 pl_Release( p_parent );
1372 *pb_sout_keep = b_keep_sout;
1376 static void SoutKeep( sout_instance_t *p_sout )
1378 /* attach sout to the playlist */
1379 playlist_t *p_playlist = pl_Yield( p_sout );
1381 msg_Dbg( p_sout, "sout has been kept" );
1382 vlc_object_attach( p_sout, p_playlist );
1384 pl_Release( p_sout );
1387 /*****************************************************************************
1389 *****************************************************************************/
1390 static inline int ControlPopNoLock( input_thread_t *p_input,
1391 int *pi_type, vlc_value_t *p_val )
1393 if( p_input->p->i_control <= 0 )
1395 return VLC_EGENERIC;
1398 *pi_type = p_input->p->control[0].i_type;
1399 *p_val = p_input->p->control[0].val;
1401 p_input->p->i_control--;
1402 if( p_input->p->i_control > 0 )
1406 for( i = 0; i < p_input->p->i_control; i++ )
1408 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1409 p_input->p->control[i].val = p_input->p->control[i+1].val;
1416 static void ControlReduce( input_thread_t *p_input )
1423 for( i = 1; i < p_input->p->i_control; i++ )
1425 const int i_lt = p_input->p->control[i-1].i_type;
1426 const int i_ct = p_input->p->control[i].i_type;
1428 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1429 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1433 ( i_ct == INPUT_CONTROL_SET_STATE ||
1434 i_ct == INPUT_CONTROL_SET_RATE ||
1435 i_ct == INPUT_CONTROL_SET_POSITION ||
1436 i_ct == INPUT_CONTROL_SET_TIME ||
1437 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1438 i_ct == INPUT_CONTROL_SET_TITLE ||
1439 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1440 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1443 // msg_Dbg( p_input, "merged at %d", i );
1444 /* Remove the i-1 */
1445 for( j = i; j < p_input->p->i_control; j++ )
1446 p_input->p->control[j-1] = p_input->p->control[j];
1447 p_input->p->i_control--;
1451 /* TODO but that's not that important
1452 - merge SET_X with SET_X_CMD
1453 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1454 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1461 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1464 vlc_bool_t b_force_update = VLC_FALSE;
1466 if( !p_input ) return b_force_update;
1470 case INPUT_CONTROL_SET_DIE:
1471 msg_Dbg( p_input, "control: stopping input" );
1472 /* Mark all submodules to die */
1473 if( p_input->p->input.p_access )
1474 vlc_object_kill( p_input->p->input.p_access );
1475 if( p_input->p->input.p_stream )
1476 vlc_object_kill( p_input->p->input.p_stream );
1477 vlc_object_kill( p_input->p->input.p_demux );
1479 vlc_object_kill( p_input );
1482 case INPUT_CONTROL_SET_POSITION:
1483 case INPUT_CONTROL_SET_POSITION_OFFSET:
1486 if( i_type == INPUT_CONTROL_SET_POSITION )
1488 f_pos = val.f_float;
1492 /* Should not fail */
1493 demux2_Control( p_input->p->input.p_demux,
1494 DEMUX_GET_POSITION, &f_pos );
1495 f_pos += val.f_float;
1497 if( f_pos < 0.0 ) f_pos = 0.0;
1498 if( f_pos > 1.0 ) f_pos = 1.0;
1499 /* Reset the decoders states and clock sync (before calling the demuxer */
1500 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1501 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1502 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1505 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1506 "%2.1f%% failed", f_pos * 100 );
1510 if( p_input->p->i_slave > 0 )
1511 SlaveSeek( p_input );
1513 b_force_update = VLC_TRUE;
1518 case INPUT_CONTROL_SET_TIME:
1519 case INPUT_CONTROL_SET_TIME_OFFSET:
1524 if( i_type == INPUT_CONTROL_SET_TIME )
1526 i_time = val.i_time;
1530 /* Should not fail */
1531 demux2_Control( p_input->p->input.p_demux,
1532 DEMUX_GET_TIME, &i_time );
1533 i_time += val.i_time;
1535 if( i_time < 0 ) i_time = 0;
1537 /* Reset the decoders states and clock sync (before calling the demuxer */
1538 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1539 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1541 i_ret = demux2_Control( p_input->p->input.p_demux,
1542 DEMUX_SET_TIME, i_time );
1547 /* Emulate it with a SET_POS */
1548 demux2_Control( p_input->p->input.p_demux,
1549 DEMUX_GET_LENGTH, &i_length );
1552 double f_pos = (double)i_time / (double)i_length;
1553 i_ret = demux2_Control( p_input->p->input.p_demux,
1554 DEMUX_SET_POSITION, f_pos );
1559 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1560 " failed or not possible", i_time );
1564 if( p_input->p->i_slave > 0 )
1565 SlaveSeek( p_input );
1567 b_force_update = VLC_TRUE;
1572 case INPUT_CONTROL_SET_STATE:
1573 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1574 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1577 if( p_input->p->input.p_access )
1578 i_ret = access2_Control( p_input->p->input.p_access,
1579 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1581 i_ret = demux2_Control( p_input->p->input.p_demux,
1582 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1586 /* FIXME What to do ? */
1587 msg_Warn( p_input, "cannot unset pause -> EOF" );
1588 vlc_mutex_unlock( &p_input->p->lock_control );
1589 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1590 vlc_mutex_lock( &p_input->p->lock_control );
1593 b_force_update = VLC_TRUE;
1595 /* Switch to play */
1596 p_input->i_state = PLAYING_S;
1597 val.i_int = PLAYING_S;
1598 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1601 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1603 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1604 p_input->p->b_can_pause )
1607 if( p_input->p->input.p_access )
1608 i_ret = access2_Control( p_input->p->input.p_access,
1609 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1611 i_ret = demux2_Control( p_input->p->input.p_demux,
1612 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1614 b_force_update = VLC_TRUE;
1618 msg_Warn( p_input, "cannot set pause state" );
1619 val.i_int = p_input->i_state;
1623 val.i_int = PAUSE_S;
1626 /* Switch to new state */
1627 p_input->i_state = val.i_int;
1628 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1630 /* Send discontinuity to decoders (it will allow them to flush
1632 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE, VLC_FALSE );
1634 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1636 b_force_update = VLC_TRUE;
1638 /* Correct "state" value */
1639 val.i_int = p_input->i_state;
1640 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1642 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1644 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1648 case INPUT_CONTROL_SET_RATE:
1649 case INPUT_CONTROL_SET_RATE_SLOWER:
1650 case INPUT_CONTROL_SET_RATE_FASTER:
1654 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1655 i_rate = p_input->p->i_rate * 2;
1656 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1657 i_rate = p_input->p->i_rate / 2;
1661 if( i_rate < INPUT_RATE_MIN )
1663 msg_Dbg( p_input, "cannot set rate faster" );
1664 i_rate = INPUT_RATE_MIN;
1666 else if( i_rate > INPUT_RATE_MAX )
1668 msg_Dbg( p_input, "cannot set rate slower" );
1669 i_rate = INPUT_RATE_MAX;
1671 if( i_rate != INPUT_RATE_DEFAULT &&
1672 ( !p_input->b_can_pace_control ||
1673 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1675 msg_Dbg( p_input, "cannot change rate" );
1676 i_rate = INPUT_RATE_DEFAULT;
1678 if( i_rate != p_input->p->i_rate )
1681 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1683 input_EsOutDiscontinuity( p_input->p->p_es_out,
1684 VLC_FALSE, VLC_FALSE );
1686 p_input->p->i_rate = i_rate;
1688 input_EsOutSetRate( p_input->p->p_es_out );
1690 b_force_update = VLC_TRUE;
1695 case INPUT_CONTROL_SET_PROGRAM:
1696 /* No need to force update, es_out does it if needed */
1697 es_out_Control( p_input->p->p_es_out,
1698 ES_OUT_SET_GROUP, val.i_int );
1700 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1704 case INPUT_CONTROL_SET_ES:
1705 /* No need to force update, es_out does it if needed */
1706 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1707 input_EsOutGetFromID( p_input->p->p_es_out,
1711 case INPUT_CONTROL_SET_AUDIO_DELAY:
1712 input_EsOutSetDelay( p_input->p->p_es_out,
1713 AUDIO_ES, val.i_time );
1714 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1717 case INPUT_CONTROL_SET_SPU_DELAY:
1718 input_EsOutSetDelay( p_input->p->p_es_out,
1719 SPU_ES, val.i_time );
1720 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1723 case INPUT_CONTROL_SET_TITLE:
1724 case INPUT_CONTROL_SET_TITLE_NEXT:
1725 case INPUT_CONTROL_SET_TITLE_PREV:
1726 if( p_input->p->input.b_title_demux &&
1727 p_input->p->input.i_title > 0 )
1730 /* FIXME handle demux title */
1731 demux_t *p_demux = p_input->p->input.p_demux;
1734 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1735 i_title = p_demux->info.i_title - 1;
1736 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1737 i_title = p_demux->info.i_title + 1;
1739 i_title = val.i_int;
1741 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1743 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1744 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1746 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1747 input_ControlVarTitle( p_input, i_title );
1750 else if( p_input->p->input.i_title > 0 )
1752 access_t *p_access = p_input->p->input.p_access;
1755 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1756 i_title = p_access->info.i_title - 1;
1757 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1758 i_title = p_access->info.i_title + 1;
1760 i_title = val.i_int;
1762 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1764 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1765 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1767 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1768 stream_AccessReset( p_input->p->input.p_stream );
1772 case INPUT_CONTROL_SET_SEEKPOINT:
1773 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1774 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1775 if( p_input->p->input.b_title_demux &&
1776 p_input->p->input.i_title > 0 )
1778 demux_t *p_demux = p_input->p->input.p_demux;
1780 int64_t i_input_time;
1781 int64_t i_seekpoint_time;
1783 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1785 i_seekpoint = p_demux->info.i_seekpoint;
1786 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1787 if( i_seekpoint_time >= 0 &&
1788 !demux2_Control( p_demux,
1789 DEMUX_GET_TIME, &i_input_time ) )
1791 if ( i_input_time < i_seekpoint_time + 3000000 )
1797 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1798 i_seekpoint = p_demux->info.i_seekpoint + 1;
1800 i_seekpoint = val.i_int;
1802 if( i_seekpoint >= 0 && i_seekpoint <
1803 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1805 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1806 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1808 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1811 else if( p_input->p->input.i_title > 0 )
1813 demux_t *p_demux = p_input->p->input.p_demux;
1814 access_t *p_access = p_input->p->input.p_access;
1816 int64_t i_input_time;
1817 int64_t i_seekpoint_time;
1819 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1821 i_seekpoint = p_access->info.i_seekpoint;
1822 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1823 if( i_seekpoint_time >= 0 &&
1824 demux2_Control( p_demux,
1825 DEMUX_GET_TIME, &i_input_time ) )
1827 if ( i_input_time < i_seekpoint_time + 3000000 )
1833 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1834 i_seekpoint = p_access->info.i_seekpoint + 1;
1836 i_seekpoint = val.i_int;
1838 if( i_seekpoint >= 0 && i_seekpoint <
1839 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1841 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1842 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1844 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1846 stream_AccessReset( p_input->p->input.p_stream );
1851 case INPUT_CONTROL_ADD_SLAVE:
1852 if( val.psz_string )
1854 input_source_t *slave = InputSourceNew( p_input );
1856 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1862 msg_Dbg( p_input, "adding %s as slave on the fly",
1866 if( demux2_Control( p_input->p->input.p_demux,
1867 DEMUX_GET_TIME, &i_time ) )
1869 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1870 InputSourceClean( slave );
1874 if( demux2_Control( slave->p_demux,
1875 DEMUX_SET_TIME, i_time ) )
1877 msg_Err( p_input, "seek failed for new slave" );
1878 InputSourceClean( slave );
1883 /* Get meta (access and demux) */
1884 p_meta = vlc_meta_New();
1885 access2_Control( slave->p_access, ACCESS_GET_META,
1887 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1888 InputUpdateMeta( p_input, p_meta );
1890 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1895 msg_Warn( p_input, "failed to add %s as slave",
1899 free( val.psz_string );
1903 case INPUT_CONTROL_SET_BOOKMARK:
1905 msg_Err( p_input, "not yet implemented" );
1909 return b_force_update;
1912 /*****************************************************************************
1914 *****************************************************************************/
1915 static int UpdateFromDemux( input_thread_t *p_input )
1917 demux_t *p_demux = p_input->p->input.p_demux;
1920 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1922 v.i_int = p_demux->info.i_title;
1923 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1925 input_ControlVarTitle( p_input, p_demux->info.i_title );
1927 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1929 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1931 v.i_int = p_demux->info.i_seekpoint;
1932 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1934 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1936 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1938 /* Hmmm only works with master input */
1939 if( p_input->p->input.p_demux == p_demux )
1941 int i_title_end = p_input->p->input.i_title_end -
1942 p_input->p->input.i_title_offset;
1943 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1944 p_input->p->input.i_seekpoint_offset;
1946 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1948 if( p_demux->info.i_title > i_title_end ||
1949 ( p_demux->info.i_title == i_title_end &&
1950 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1952 else if( i_seekpoint_end >=0 )
1954 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1956 else if( i_title_end >= 0 )
1958 if( p_demux->info.i_title > i_title_end ) return 0;
1965 /*****************************************************************************
1967 *****************************************************************************/
1968 static int UpdateFromAccess( input_thread_t *p_input )
1970 access_t *p_access = p_input->p->input.p_access;
1973 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1975 v.i_int = p_access->info.i_title;
1976 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1978 input_ControlVarTitle( p_input, p_access->info.i_title );
1980 stream_AccessUpdate( p_input->p->input.p_stream );
1982 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1984 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1986 v.i_int = p_access->info.i_seekpoint;
1987 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1988 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1990 if( p_access->info.i_update & INPUT_UPDATE_META )
1992 /* TODO maybe multi - access ? */
1993 vlc_meta_t *p_meta = vlc_meta_New();
1994 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
1995 InputUpdateMeta( p_input, p_meta );
1996 var_SetBool( p_input, "item-change", p_input->p->input.p_item->i_id );
1997 p_access->info.i_update &= ~INPUT_UPDATE_META;
2000 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2002 /* Hmmm only works with master input */
2003 if( p_input->p->input.p_access == p_access )
2005 int i_title_end = p_input->p->input.i_title_end -
2006 p_input->p->input.i_title_offset;
2007 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2008 p_input->p->input.i_seekpoint_offset;
2010 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2012 if( p_access->info.i_title > i_title_end ||
2013 ( p_access->info.i_title == i_title_end &&
2014 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2016 else if( i_seekpoint_end >=0 )
2018 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2020 else if( i_title_end >= 0 )
2022 if( p_access->info.i_title > i_title_end ) return 0;
2029 /*****************************************************************************
2031 *****************************************************************************/
2032 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2034 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2035 p_input->p->input.p_item->i_duration = i_length;
2036 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2038 if( !p_input->b_preparsing )
2040 pl_Yield( p_input );
2041 var_SetInteger( pl_Get( p_input ), "item-change",
2042 p_input->p->input.p_item->i_id );
2043 pl_Release( p_input )
2047 /*****************************************************************************
2049 *****************************************************************************/
2050 static input_source_t *InputSourceNew( input_thread_t *p_input )
2052 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2056 msg_Err( p_input, "out of memory for new input source" );
2061 in->p_access = NULL;
2062 in->p_stream = NULL;
2064 in->b_title_demux = VLC_FALSE;
2065 TAB_INIT( in->i_title, in->title );
2066 in->b_can_pace_control = VLC_TRUE;
2067 in->b_eof = VLC_FALSE;
2068 in->i_cr_average = 0;
2073 /*****************************************************************************
2075 *****************************************************************************/
2076 static int InputSourceInit( input_thread_t *p_input,
2077 input_source_t *in, const char *psz_mrl,
2078 const char *psz_forced_demux )
2080 char psz_dup[strlen (psz_mrl) + 1];
2081 const char *psz_access;
2082 const char *psz_demux;
2088 strcpy( psz_dup, psz_mrl );
2090 if( !in ) return VLC_EGENERIC;
2091 if( !p_input ) return VLC_EGENERIC;
2094 if( !p_input->b_preparsing )
2096 MRLSplit( VLC_OBJECT(p_input), psz_dup,
2097 &psz_access, &psz_demux, &psz_path );
2099 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2100 psz_mrl, psz_access, psz_demux, psz_path );
2102 /* Hack to allow udp://@:port syntax */
2104 (strncmp( psz_access, "udp", 3 ) &&
2105 strncmp( psz_access, "rtp", 3 )) )
2107 /* Find optional titles and seekpoints */
2108 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2109 &in->i_seekpoint_start, &in->i_seekpoint_end );
2111 if( psz_forced_demux && *psz_forced_demux )
2113 psz_demux = psz_forced_demux;
2115 else if( !psz_demux || *psz_demux == '\0' )
2117 /* special hack for forcing a demuxer with --demux=module
2118 * (and do nothing with a list) */
2119 char *psz_var_demux = var_GetString( p_input, "demux" );
2121 if( *psz_var_demux != '\0' &&
2122 !strchr(psz_var_demux, ',' ) &&
2123 !strchr(psz_var_demux, ':' ) )
2125 psz_demux = psz_var_demux;
2127 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2129 else if( psz_var_demux )
2131 free( psz_var_demux );
2135 /* Try access_demux if no demux given */
2136 if( *psz_demux == '\0' )
2138 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2139 NULL, p_input->p->p_es_out, VLC_FALSE );
2145 if( !strncmp( psz_path, "file://", 7 ) )
2147 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2149 psz_access = "file";
2154 int64_t i_pts_delay;
2156 /* Get infos from access_demux */
2157 demux2_Control( in->p_demux,
2158 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2159 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2161 in->b_title_demux = VLC_TRUE;
2162 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2163 &in->title, &in->i_title,
2164 &in->i_title_offset, &in->i_seekpoint_offset ) )
2169 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2170 &in->b_can_pace_control );
2171 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2175 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2181 int64_t i_pts_delay;
2183 input_ChangeState( p_input, OPENING_S );
2185 /* Now try a real access */
2186 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2187 p_input->b_preparsing );
2189 /* Access failed, URL encoded ? */
2190 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2192 decode_URI( psz_path );
2194 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2195 psz_access, psz_demux, psz_path );
2197 in->p_access = access2_New( p_input,
2198 psz_access, psz_demux, psz_path,
2199 p_input->b_preparsing );
2201 /* access failed, maybe our access detection was wrong.
2202 * Retry with the full name */
2203 if( in->p_access == NULL && strchr( psz_mrl, ':' ) )
2205 msg_Dbg( p_input, "retrying with access `' demux `' path `%s'",
2207 in->p_access = access2_New( p_input,
2209 p_input->b_preparsing );
2211 if( in->p_access == NULL )
2213 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2215 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2216 _("Your input can't be opened"),
2217 _("VLC is unable to open the MRL '%s'."
2218 " Check the log for details."), psz_mrl );
2223 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2224 while( psz && *psz )
2226 access_t *p_access = in->p_access;
2227 char *end = strchr( psz, ':' );
2232 in->p_access = access2_FilterNew( in->p_access, psz );
2233 if( in->p_access == NULL )
2235 in->p_access = p_access;
2236 msg_Warn( p_input, "failed to insert access filter %s",
2242 if( psz_tmp ) free( psz_tmp );
2244 /* Get infos from access */
2245 if( !p_input->b_preparsing )
2247 access2_Control( in->p_access,
2248 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2249 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2251 in->b_title_demux = VLC_FALSE;
2252 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2253 &in->title, &in->i_title,
2254 &in->i_title_offset, &in->i_seekpoint_offset ) )
2260 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2261 &in->b_can_pace_control );
2262 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2264 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2266 var_Set( p_input, "seekable", val );
2269 input_ChangeState( p_input, BUFFERING_S );
2271 /* Create the stream_t */
2272 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2273 if( in->p_stream == NULL )
2275 msg_Warn( p_input, "cannot create a stream_t from access" );
2279 /* Open a demuxer */
2280 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2282 psz_demux = in->p_access->psz_demux;
2286 /* Take access redirections into account */
2287 char *psz_real_path;
2288 char *psz_buf = NULL;
2289 if( in->p_access->psz_path )
2291 const char *psz_a, *psz_d;
2292 psz_buf = strdup( in->p_access->psz_path );
2293 MRLSplit( VLC_OBJECT(p_input), psz_buf,
2294 &psz_a, &psz_d, &psz_real_path );
2298 psz_real_path = psz_path;
2300 in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2302 in->p_stream, p_input->p->p_es_out,
2303 p_input->b_preparsing );
2307 if( in->p_demux == NULL )
2309 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2310 psz_access, psz_demux, psz_path );
2311 intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2312 _("Can't recognize the input's format"),
2313 _("The format of '%s' can't be detected. "
2314 "Have a look the log for details."), psz_mrl );
2318 /* Get title from demux */
2319 if( !p_input->b_preparsing && in->i_title <= 0 )
2321 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2322 &in->title, &in->i_title,
2323 &in->i_title_offset, &in->i_seekpoint_offset ))
2325 TAB_INIT( in->i_title, in->title );
2329 in->b_title_demux = VLC_TRUE;
2333 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2334 if( 1 || !p_input->b_preparsing )
2337 input_attachment_t **attachment;
2338 if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2339 &attachment, &i_attachment ) )
2342 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2343 p_input->p->attachment = realloc( p_input->p->attachment,
2344 sizeof(input_attachment_t**) * ( p_input->p->i_attachment + i_attachment ) );
2345 for( i = 0; i < i_attachment; i++ )
2346 p_input->p->attachment[p_input->p->i_attachment++] = attachment[i];
2349 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2354 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2355 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2360 input_ChangeState( p_input, ERROR_S );
2363 demux2_Delete( in->p_demux );
2366 stream_Delete( in->p_stream );
2369 access2_Delete( in->p_access );
2371 return VLC_EGENERIC;
2374 /*****************************************************************************
2376 *****************************************************************************/
2377 static void InputSourceClean( input_source_t *in )
2382 demux2_Delete( in->p_demux );
2385 stream_Delete( in->p_stream );
2388 access2_Delete( in->p_access );
2390 if( in->i_title > 0 )
2392 for( i = 0; i < in->i_title; i++ )
2393 vlc_input_title_Delete( in->title[i] );
2394 TAB_CLEAN( in->i_title, in->title );
2398 static void SlaveDemux( input_thread_t *p_input )
2403 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2405 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2409 for( i = 0; i < p_input->p->i_slave; i++ )
2411 input_source_t *in = p_input->p->slave[i];
2417 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2422 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2424 msg_Err( p_input, "slave[%d] doesn't like "
2425 "DEMUX_GET_TIME -> EOF", i );
2430 if( i_stime >= i_time )
2433 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2439 i_ret = in->p_demux->pf_demux( in->p_demux );
2444 msg_Dbg( p_input, "slave %d EOF", i );
2445 in->b_eof = VLC_TRUE;
2450 static void SlaveSeek( input_thread_t *p_input )
2455 if( !p_input ) return;
2457 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2459 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2463 for( i = 0; i < p_input->p->i_slave; i++ )
2465 input_source_t *in = p_input->p->slave[i];
2467 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2469 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2470 in->b_eof = VLC_TRUE;
2475 /*****************************************************************************
2477 *****************************************************************************/
2478 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2482 if( !p_meta ) return;
2484 /* Get meta information from user */
2485 #define GET_META( field, s ) \
2486 var_Get( p_input, (s), &val ); \
2487 if( *val.psz_string ) \
2488 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2489 free( val.psz_string )
2491 GET_META( Title, "meta-title" );
2492 GET_META( Artist, "meta-artist" );
2493 GET_META( Genre, "meta-genre" );
2494 GET_META( Copyright, "meta-copyright" );
2495 GET_META( Description, "meta-description" );
2496 GET_META( Date, "meta-date" );
2497 GET_META( URL, "meta-url" );
2501 /*****************************************************************************
2502 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2503 * arturl and locking issue.
2504 *****************************************************************************/
2505 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2507 input_item_t *p_item = p_input->p->input.p_item;
2508 char * psz_saved_arturl = NULL;
2509 const char * psz_arturl = NULL;
2510 char *psz_title = NULL;
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 if( input_item_GetArtURL( p_item ) )
2521 psz_saved_arturl = strdup( input_item_GetArtURL( p_item ) );
2523 vlc_meta_Merge( p_item->p_meta, p_meta );
2525 if( psz_saved_arturl && *psz_saved_arturl )
2526 input_item_SetArtURL( p_item, psz_saved_arturl );
2528 free( psz_saved_arturl );
2529 vlc_meta_Delete( p_meta );
2531 psz_arturl = input_item_GetArtURL( p_item );
2532 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2534 /* Don't look for art cover if sout
2535 * XXX It can change when sout has meta data support */
2536 if( p_input->p->p_sout && !p_input->b_preparsing )
2537 input_item_SetArtURL( p_item, "" );
2539 input_ExtractAttachmentAndCacheArt( p_input );
2542 input_item_SetPreparsed( p_item, VLC_TRUE );
2546 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2548 p_meta = vlc_meta_New();
2549 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2551 vlc_mutex_unlock( &p_item->lock );
2555 input_Control( p_input, INPUT_SET_NAME, psz_title );
2561 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2562 for( i = 0; ppsz_all_keys[i]; i++ )
2564 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2565 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2566 free( ppsz_all_keys[i] );
2568 free( ppsz_all_keys );
2569 vlc_meta_Delete( p_meta );
2572 /** \todo handle sout meta */
2576 static inline vlc_bool_t IsValidAccess( const char *psz )
2580 while( ( c = *psz ) != '\0' )
2585 if( ( !isascii( c ) || !isalnum( c ) ) && ( c != '/' ) )
2589 /* should not happen though */
2594 /*****************************************************************************
2595 * MRLSplit: parse the access, demux and url part of the
2596 * Media Resource Locator.
2597 *****************************************************************************/
2598 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2599 const char **ppsz_access, const char **ppsz_demux,
2602 const char *psz_access = "";
2603 const char *psz_demux = "";
2607 psz = strchr( psz_dup, ':' );
2611 /* Guess whether ':' is part of a local filename, or separates
2612 * access/demux from path */
2613 if( !IsValidAccess( psz_dup ) )
2615 #if defined( WIN32 ) || defined( UNDER_CE )
2616 else if( ( psz - psz_dup == 1 ) && isalpha( psz_dup[0] ) )
2618 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2627 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2631 psz = strchr( psz_dup, '/' );
2638 psz_access = psz_dup;
2645 *ppsz_access = psz_access;
2646 *ppsz_demux = psz_demux;
2647 *ppsz_path = psz_path;
2650 /*****************************************************************************
2651 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2654 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2655 *****************************************************************************/
2656 static void MRLSections( input_thread_t *p_input, char *psz_source,
2657 int *pi_title_start, int *pi_title_end,
2658 int *pi_chapter_start, int *pi_chapter_end )
2660 char *psz, *psz_end, *psz_next, *psz_check;
2662 *pi_title_start = *pi_title_end = -1;
2663 *pi_chapter_start = *pi_chapter_end = -1;
2665 /* Start by parsing titles and chapters */
2666 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2668 /* Check we are really dealing with a title/chapter section */
2669 psz_check = psz + 1;
2670 if( !*psz_check ) return;
2671 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2672 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2673 if( *psz_check == ':' && ++psz_check )
2674 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2675 if( *psz_check != '-' && *psz_check ) return;
2676 if( *psz_check == '-' && ++psz_check )
2677 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2678 if( *psz_check != ':' && *psz_check ) return;
2679 if( *psz_check == ':' && ++psz_check )
2680 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2681 if( *psz_check ) return;
2683 /* Separate start and end */
2685 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2687 /* Look for the start title */
2688 *pi_title_start = strtol( psz, &psz_next, 0 );
2689 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2690 *pi_title_end = *pi_title_start;
2693 /* Look for the start chapter */
2695 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2696 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2697 *pi_chapter_end = *pi_chapter_start;
2701 /* Look for the end title */
2702 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2703 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2706 /* Look for the end chapter */
2707 if( *psz_end ) psz_end++;
2708 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2709 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2712 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2713 psz_source, *pi_title_start, *pi_chapter_start,
2714 *pi_title_end, *pi_chapter_end );
2717 /*****************************************************************************
2718 * input_AddSubtitles: add a subtitles file and enable it
2719 *****************************************************************************/
2720 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2721 vlc_bool_t b_check_extension )
2723 input_source_t *sub;
2726 char *psz_path, *psz_extension;
2728 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2733 /* if we are provided a subtitle.sub file,
2734 * see if we don't have a subtitle.idx and use it instead */
2735 psz_path = strdup( psz_subtitle );
2738 psz_extension = strrchr( psz_path, '.');
2739 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2743 strcpy( psz_extension, ".idx" );
2744 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2745 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2748 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2749 psz_path, psz_subtitle );
2750 strcpy( psz_subtitle, psz_path );
2756 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2758 sub = InputSourceNew( p_input );
2759 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2761 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2764 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2766 if( count.i_int == 0 )
2768 /* if it was first one, there is disable too */
2770 if( count.i_int < list.p_list->i_count )
2772 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2773 &list.p_list->p_values[count.i_int] );
2775 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );