1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
38 #include "input_internal.h"
41 #include "es_out_timeshift.h"
47 #include "../stream_output/stream_output.h"
49 #include <vlc_interface.h>
51 #include <vlc_charset.h>
52 #include <vlc_strings.h>
54 #ifdef HAVE_SYS_STAT_H
55 # include <sys/stat.h>
58 /*****************************************************************************
60 *****************************************************************************/
61 static void Destructor( input_thread_t * p_input );
63 static void *Run ( vlc_object_t *p_this );
64 static void *RunAndDestroy ( vlc_object_t *p_this );
66 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
67 const char *, bool, sout_instance_t * );
68 static int Init ( input_thread_t *p_input );
69 static void WaitDie ( input_thread_t *p_input );
70 static void End ( input_thread_t *p_input );
71 static void MainLoop( input_thread_t *p_input );
73 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline );
74 static void ControlReduce( input_thread_t * );
75 static bool Control( input_thread_t *, int, vlc_value_t );
77 static int UpdateFromAccess( input_thread_t * );
78 static int UpdateFromDemux( input_thread_t * );
80 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
82 static input_source_t *InputSourceNew( input_thread_t *);
83 static int InputSourceInit( input_thread_t *, input_source_t *,
84 const char *, const char *psz_forced_demux );
85 static void InputSourceClean( input_source_t * );
87 //static void InputGetAttachments( input_thread_t *, input_source_t * );
88 static void SlaveDemux( input_thread_t *p_input );
89 static void SlaveSeek( input_thread_t *p_input );
91 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
92 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
93 static char *InputGetExtraFiles( input_thread_t *p_input,
94 const char *psz_access, const char *psz_path );
96 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
97 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
98 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
99 int i_new, input_attachment_t **pp_new );
101 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced );
103 static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */
105 /*****************************************************************************
106 * This function creates a new input, and returns a pointer
107 * to its description. On error, it returns NULL.
109 * XXX Do not forget to update vlc_input.h if you add new variables.
110 *****************************************************************************/
111 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
112 const char *psz_header, bool b_quick,
113 sout_instance_t *p_sout )
115 static const char input_name[] = "input";
116 input_thread_t *p_input = NULL; /* thread descriptor */
120 /* Allocate descriptor */
121 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
122 VLC_OBJECT_INPUT, input_name );
123 if( p_input == NULL )
126 /* Construct a nice name for the input timer */
127 char psz_timer_name[255];
128 char * psz_name = input_item_GetName( p_item );
129 snprintf( psz_timer_name, sizeof(psz_timer_name),
130 "input launching for '%s'", psz_name );
132 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
136 /* Start a timer to mesure how long it takes
137 * to launch an input */
138 stats_TimerStart( p_input, psz_timer_name,
139 STATS_TIMER_INPUT_LAUNCHING );
141 p_input->p = calloc( 1, sizeof( input_thread_private_t ) );
145 /* One "randomly" selected input thread is responsible for computing
146 * the global stats. Check if there is already someone doing this */
147 if( p_input->p_libvlc->p_stats && !b_quick )
149 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
150 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
151 if( p_private->p_stats_computer == NULL )
152 p_private->p_stats_computer = p_input;
153 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
156 p_input->b_preparsing = b_quick;
157 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
160 vlc_event_manager_t * p_em = &p_input->p->event_manager;
161 vlc_event_manager_init_with_vlc_object( p_em, p_input );
162 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
163 vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
165 /* Init Common fields */
166 p_input->b_eof = false;
167 p_input->b_can_pace_control = true;
168 p_input->p->i_start = 0;
170 p_input->p->i_stop = 0;
171 p_input->p->i_run = 0;
172 p_input->p->i_title = 0;
173 p_input->p->title = NULL;
174 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
175 p_input->i_state = INIT_S;
176 p_input->p->i_rate = INPUT_RATE_DEFAULT;
177 p_input->p->b_recording = false;
178 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
179 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
180 p_input->p->p_es_out_display = NULL;
181 p_input->p->p_es_out = NULL;
182 p_input->p->p_sout = NULL;
183 p_input->p->b_out_pace_control = false;
184 p_input->i_pts_delay = 0;
186 /* Init Input fields */
187 vlc_gc_incref( p_item ); /* Released in Destructor() */
188 p_input->p->input.p_item = p_item;
189 p_input->p->input.p_access = NULL;
190 p_input->p->input.p_stream = NULL;
191 p_input->p->input.p_demux = NULL;
192 p_input->p->input.b_title_demux = false;
193 p_input->p->input.i_title = 0;
194 p_input->p->input.title = NULL;
195 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
196 p_input->p->input.b_can_pace_control = true;
197 p_input->p->input.b_can_rate_control = true;
198 p_input->p->input.b_rescale_ts = true;
199 p_input->p->input.b_eof = false;
200 p_input->p->input.i_cr_average = 0;
202 vlc_mutex_lock( &p_item->lock );
204 if( !p_item->p_stats )
205 p_item->p_stats = stats_NewInputStats( p_input );
206 vlc_mutex_unlock( &p_item->lock );
209 p_input->p->i_slave = 0;
210 p_input->p->slave = NULL;
212 /* Init control buffer */
213 vlc_mutex_init( &p_input->p->lock_control );
214 vlc_cond_init( &p_input->p->wait_control );
215 p_input->p->i_control = 0;
217 /* Parse input options */
218 vlc_mutex_lock( &p_item->lock );
219 assert( (int)p_item->optflagc == p_item->i_options );
220 for( i = 0; i < p_item->i_options; i++ )
221 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
222 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
223 vlc_mutex_unlock( &p_item->lock );
225 /* Create Object Variables for private use only */
226 input_ConfigVarInit( p_input );
228 /* Create Objects variables for public Get and Set */
229 input_ControlVarInit( p_input );
232 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
234 if( !p_input->b_preparsing )
236 var_Get( p_input, "bookmarks", &val );
239 /* FIXME: have a common cfg parsing routine used by sout and others */
240 char *psz_parser, *psz_start, *psz_end;
241 psz_parser = val.psz_string;
242 while( (psz_start = strchr( psz_parser, '{' ) ) )
244 seekpoint_t *p_seekpoint;
247 psz_end = strchr( psz_start, '}' );
248 if( !psz_end ) break;
249 psz_parser = psz_end + 1;
250 backup = *psz_parser;
254 p_seekpoint = vlc_seekpoint_New();
255 while( (psz_end = strchr( psz_start, ',' ) ) )
258 if( !strncmp( psz_start, "name=", 5 ) )
260 p_seekpoint->psz_name = strdup(psz_start + 5);
262 else if( !strncmp( psz_start, "bytes=", 6 ) )
264 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
266 else if( !strncmp( psz_start, "time=", 5 ) )
268 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
271 psz_start = psz_end + 1;
273 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
274 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
275 p_seekpoint->i_time_offset );
276 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
277 vlc_seekpoint_Delete( p_seekpoint );
278 *psz_parser = backup;
280 free( val.psz_string );
284 /* Remove 'Now playing' info as it is probably outdated */
285 input_item_SetNowPlaying( p_item, NULL );
286 input_SendEventMeta( p_input );
289 if( p_input->b_preparsing )
290 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
294 p_input->p->p_sout = p_sout;
296 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
297 vlc_mutex_init( &p_input->p->counters.counters_lock );
299 /* Set the destructor when we are sure we are initialized */
300 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
302 /* Attach only once we are ready */
303 vlc_object_attach( p_input, p_parent );
309 * Input destructor (called when the object's refcount reaches 0).
311 static void Destructor( input_thread_t * p_input )
314 char * psz_name = input_item_GetName( p_input->p->input.p_item );
315 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
319 vlc_event_manager_fini( &p_input->p->event_manager );
321 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
322 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
324 if( p_input->p->p_sout )
325 sout_DeleteInstance( p_input->p->p_sout );
327 vlc_gc_decref( p_input->p->input.p_item );
329 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
331 vlc_cond_destroy( &p_input->p->wait_control );
332 vlc_mutex_destroy( &p_input->p->lock_control );
337 * Initialize an input thread and run it. You will need to monitor the
338 * thread to clean up after it is done
340 * \param p_parent a vlc_object
341 * \param p_item an input item
342 * \return a pointer to the spawned input thread
344 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
345 input_item_t *p_item )
347 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
351 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
352 input_item_t *p_item,
353 const char *psz_log, sout_instance_t *p_sout )
355 input_thread_t *p_input;
357 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
361 /* Create thread and wait for its readiness. */
362 if( vlc_thread_create( p_input, "input", Run,
363 VLC_THREAD_PRIORITY_INPUT, false ) )
365 input_ChangeState( p_input, ERROR_S );
366 msg_Err( p_input, "cannot create input thread" );
367 vlc_object_detach( p_input );
368 vlc_object_release( p_input );
376 * Initialize an input thread and run it. This thread will clean after itself,
377 * you can forget about it. It can work either in blocking or non-blocking mode
379 * \param p_parent a vlc_object
380 * \param p_item an input item
381 * \param b_block should we block until read is finished ?
382 * \return an error code, VLC_SUCCESS on success
384 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
387 input_thread_t *p_input;
389 p_input = Create( p_parent, p_item, NULL, false, NULL );
395 RunAndDestroy( VLC_OBJECT(p_input) );
400 if( vlc_thread_create( p_input, "input", RunAndDestroy,
401 VLC_THREAD_PRIORITY_INPUT, false ) )
403 input_ChangeState( p_input, ERROR_S );
404 msg_Err( p_input, "cannot create input thread" );
405 vlc_object_release( p_input );
413 * Initialize an input and initialize it to preparse the item
414 * This function is blocking. It will only accept to parse files
416 * \param p_parent a vlc_object_t
417 * \param p_item an input item
418 * \return VLC_SUCCESS or an error
420 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
422 input_thread_t *p_input;
424 /* Allocate descriptor */
425 p_input = Create( p_parent, p_item, NULL, true, NULL );
429 if( !Init( p_input ) )
432 vlc_object_detach( p_input );
433 vlc_object_release( p_input );
439 * Request a running input thread to stop and die
441 * \param the input thread to stop
443 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
448 if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
449 p_obj->i_object_type == VLC_OBJECT_AOUT ||
450 p_obj == VLC_OBJECT(p_input->p->p_sout) )
453 vlc_object_kill( p_obj );
455 p_list = vlc_list_children( p_obj );
456 for( i = 0; i < p_list->i_count; i++ )
457 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
458 vlc_list_release( p_list );
460 void input_StopThread( input_thread_t *p_input )
462 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
463 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
464 * unlock the control loop */
465 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
467 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
470 sout_instance_t *input_DetachSout( input_thread_t *p_input )
472 assert( p_input->b_dead );
473 sout_instance_t *p_sout = p_input->p->p_sout;
474 vlc_object_detach( p_sout );
475 p_input->p->p_sout = NULL;
479 /*****************************************************************************
480 * Run: main thread loop
481 * This is the "normal" thread that spawns the input processing chain,
482 * reads the stream, cleans up and waits
483 *****************************************************************************/
484 static void *Run( vlc_object_t *p_this )
486 input_thread_t *p_input = (input_thread_t *)p_this;
487 const int canc = vlc_savecancel();
489 if( Init( p_input ) )
491 /* If we failed, wait before we are killed, and exit */
498 /* Wait until we are asked to die */
499 if( !p_input->b_die )
506 p_input->b_dead = true;
507 vlc_restorecancel( canc );
511 /*****************************************************************************
512 * RunAndDestroy: main thread loop
513 * This is the "just forget me" thread that spawns the input processing chain,
514 * reads the stream, cleans up and releases memory
515 *****************************************************************************/
516 static void *RunAndDestroy( vlc_object_t *p_this )
518 input_thread_t *p_input = (input_thread_t *)p_this;
519 const int canc = vlc_savecancel();
521 if( Init( p_input ) )
531 vlc_object_release( p_input );
532 vlc_restorecancel( canc );
536 /*****************************************************************************
537 * Main loop: Fill buffers from access, and demux
538 *****************************************************************************/
542 * It asks the demuxer to demux some data
544 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *pi_start_mdate )
550 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
551 ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
554 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
559 if( p_input->p->input.b_title_demux &&
560 p_input->p->input.p_demux->info.i_update )
562 i_ret = UpdateFromDemux( p_input );
565 else if( !p_input->p->input.b_title_demux &&
566 p_input->p->input.p_access &&
567 p_input->p->input.p_access->info.i_update )
569 i_ret = UpdateFromAccess( p_input );
574 if( i_ret == 0 ) /* EOF */
578 var_Get( p_input, "input-repeat", &repeat );
579 if( repeat.i_int == 0 )
581 /* End of file - we do not set b_die because only the
582 * playlist is allowed to do so. */
583 msg_Dbg( p_input, "EOF reached" );
584 p_input->p->input.b_eof = true;
590 msg_Dbg( p_input, "repeating the same input (%d)",
592 if( repeat.i_int > 0 )
595 var_Set( p_input, "input-repeat", repeat );
598 /* Seek to start title/seekpoint */
599 val.i_int = p_input->p->input.i_title_start -
600 p_input->p->input.i_title_offset;
601 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
603 input_ControlPush( p_input,
604 INPUT_CONTROL_SET_TITLE, &val );
606 val.i_int = p_input->p->input.i_seekpoint_start -
607 p_input->p->input.i_seekpoint_offset;
608 if( val.i_int > 0 /* TODO: check upper boundary */ )
609 input_ControlPush( p_input,
610 INPUT_CONTROL_SET_SEEKPOINT, &val );
612 /* Seek to start position */
613 if( p_input->p->i_start > 0 )
615 val.i_time = p_input->p->i_start;
616 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
622 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
627 *pi_start_mdate = mdate();
632 input_ChangeState( p_input, ERROR_S );
635 if( i_ret > 0 && p_input->p->i_slave > 0 )
637 SlaveDemux( p_input );
643 * It update the variables used by the interfaces
645 static void MainLoopInterface( input_thread_t *p_input )
647 double f_position = 0.0;
649 mtime_t i_length = 0;
651 /* update input status variables */
652 if( demux_Control( p_input->p->input.p_demux,
653 DEMUX_GET_POSITION, &f_position ) )
656 if( demux_Control( p_input->p->input.p_demux,
657 DEMUX_GET_TIME, &i_time ) )
660 if( demux_Control( p_input->p->input.p_demux,
661 DEMUX_GET_LENGTH, &i_length ) )
664 es_out_SetTimes( p_input->p->p_es_out, f_position, i_time, i_length );
669 * It updates the globals statics
671 static void MainLoopStatistic( input_thread_t *p_input )
673 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
674 /* Are we the thread responsible for computing global stats ? */
675 if( libvlc_priv( p_input->p_libvlc )->p_stats_computer == p_input )
677 stats_ComputeGlobalStats( p_input->p_libvlc,
678 p_input->p_libvlc->p_stats );
680 input_SendEventStatistics( p_input );
685 * The main input loop.
687 static void MainLoop( input_thread_t *p_input )
689 mtime_t i_start_mdate = mdate();
690 mtime_t i_intf_update = 0;
691 mtime_t i_statistic_update = 0;
693 /* Start the timer */
694 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
696 while( vlc_object_alive( p_input ) && !p_input->b_error )
707 b_force_update = false;
709 /* FIXME if p_input->i_state == PAUSE_S the access/access_demux
710 * is paused -> this may cause problem with some of them
711 * The same problem can be seen when seeking while paused */
712 b_paused = p_input->i_state == PAUSE_S &&
713 !es_out_GetBuffering( p_input->p->p_es_out );
717 if( !p_input->p->input.b_eof )
719 MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
721 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
723 else if( !p_input->b_eof && !es_out_GetEmpty( p_input->p->p_es_out ) )
725 msg_Dbg( p_input, "waiting decoder fifos to empty" );
726 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
736 i_deadline = i_wakeup;
738 i_deadline = __MIN( i_intf_update, i_statistic_update );
741 vlc_mutex_lock( &p_input->p->lock_control );
742 ControlReduce( p_input );
743 while( !ControlPopNoLock( p_input, &i_type, &val, i_deadline ) )
745 msg_Dbg( p_input, "control type=%d", i_type );
747 if( Control( p_input, i_type, val ) )
748 b_force_update = true;
750 vlc_mutex_unlock( &p_input->p->lock_control );
752 /* Update interface and statistics */
754 if( i_intf_update < i_current || b_force_update )
756 MainLoopInterface( p_input );
757 i_intf_update = i_current + INT64_C(250000);
758 b_force_update = false;
760 if( i_statistic_update < i_current )
762 MainLoopStatistic( p_input );
763 i_statistic_update = i_current + INT64_C(1000000);
766 /* Check if i_wakeup is still valid */
769 mtime_t i_new_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
773 } while( i_current < i_wakeup );
776 if( !p_input->b_error )
777 input_ChangeState( p_input, END_S );
780 static void InitStatistics( input_thread_t * p_input )
782 if( p_input->b_preparsing ) return;
784 /* Prepare statistics */
785 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
786 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
787 if( libvlc_stats( p_input ) )
789 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
790 INIT_COUNTER( read_packets, INTEGER, COUNTER );
791 INIT_COUNTER( demux_read, INTEGER, COUNTER );
792 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
793 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
794 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
795 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
796 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
797 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
798 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
799 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
800 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
801 p_input->p->counters.p_sout_send_bitrate = NULL;
802 p_input->p->counters.p_sout_sent_packets = NULL;
803 p_input->p->counters.p_sout_sent_bytes = NULL;
804 if( p_input->p->counters.p_demux_bitrate )
805 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
806 if( p_input->p->counters.p_input_bitrate )
807 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
812 static int InitSout( input_thread_t * p_input )
816 if( p_input->b_preparsing ) return VLC_SUCCESS;
818 /* Find a usable sout and attach it to p_input */
819 psz = var_GetNonEmptyString( p_input, "sout" );
820 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
822 /* Check the validity of the provided sout */
823 if( p_input->p->p_sout )
825 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
827 msg_Dbg( p_input, "destroying unusable sout" );
829 sout_DeleteInstance( p_input->p->p_sout );
830 p_input->p->p_sout = NULL;
834 if( p_input->p->p_sout )
837 msg_Dbg( p_input, "sout keep: reusing sout" );
838 msg_Dbg( p_input, "sout keep: you probably want to use "
839 "gather stream_out" );
840 vlc_object_attach( p_input->p->p_sout, p_input );
844 /* Create a new one */
845 p_input->p->p_sout = sout_NewInstance( p_input, psz );
846 if( !p_input->p->p_sout )
848 input_ChangeState( p_input, ERROR_S );
849 msg_Err( p_input, "cannot start stream output instance, " \
855 if( libvlc_stats( p_input ) )
857 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
858 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
859 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
860 if( p_input->p->counters.p_sout_send_bitrate )
861 p_input->p->counters.p_sout_send_bitrate->update_interval =
865 else if( p_input->p->p_sout )
867 msg_Dbg( p_input, "destroying useless sout" );
869 sout_DeleteInstance( p_input->p->p_sout );
870 p_input->p->p_sout = NULL;
878 static void InitTitle( input_thread_t * p_input )
882 if( p_input->b_preparsing ) return;
884 /* Create global title (from master) */
885 p_input->p->i_title = p_input->p->input.i_title;
886 p_input->p->title = p_input->p->input.title;
887 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
888 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
889 if( p_input->p->i_title > 0 )
891 /* Setup variables */
892 input_ControlVarNavigation( p_input );
893 input_ControlVarTitle( p_input, 0 );
897 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
898 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
899 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
902 if( p_input->i_pts_delay < 0 )
903 p_input->i_pts_delay = 0;
905 /* If the desynchronisation requested by the user is < 0, we need to
906 * cache more data. */
907 var_Get( p_input, "audio-desync", &val );
909 p_input->i_pts_delay -= (val.i_int * 1000);
911 /* Update cr_average depending on the caching */
912 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
913 p_input->p->input.i_cr_average /= 10;
914 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
917 static void StartTitle( input_thread_t * p_input )
926 /* Start title/chapter */
928 if( p_input->b_preparsing )
930 p_input->p->i_start = 0;
934 val.i_int = p_input->p->input.i_title_start -
935 p_input->p->input.i_title_offset;
936 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
937 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
938 val.i_int = p_input->p->input.i_seekpoint_start -
939 p_input->p->input.i_seekpoint_offset;
940 if( val.i_int > 0 /* TODO: check upper boundary */ )
941 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
945 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
946 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
947 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
948 i_length = var_GetTime( p_input, "length" );
949 if( p_input->p->i_run < 0 )
951 msg_Warn( p_input, "invalid run-time ignored" );
952 p_input->p->i_run = 0;
955 if( p_input->p->i_start > 0 )
957 if( p_input->p->i_start >= i_length )
959 msg_Warn( p_input, "invalid start-time ignored" );
965 msg_Dbg( p_input, "starting at time: %ds",
966 (int)( p_input->p->i_start / INT64_C(1000000) ) );
968 s.i_time = p_input->p->i_start;
969 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
972 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
974 msg_Warn( p_input, "invalid stop-time ignored" );
975 p_input->p->i_stop = 0;
979 /* Get fps and set it if not already set */
980 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
983 float f_requested_fps;
985 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
986 var_SetFloat( p_input, "sub-original-fps", f_fps );
988 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
989 if( f_requested_fps != f_fps )
991 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
993 var_SetFloat( p_input, "sub-fps", f_requested_fps );
997 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1000 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1003 /* Look for and add subtitle files */
1004 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1005 if( psz_subtitle != NULL )
1007 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1008 SubtitleAdd( p_input, psz_subtitle, true );
1011 var_Get( p_input, "sub-autodetect-file", &val );
1014 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1015 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1016 p_input->p->input.p_item->psz_uri );
1017 free( psz_autopath );
1019 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1021 /* Try to autoselect the first autodetected subtitles file
1022 * if no subtitles file was specified */
1023 bool b_forced = i == 0 && !psz_subtitle;
1025 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1026 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1028 free( ppsz_subs[i] );
1032 free( psz_subtitle );
1034 /* Look for slave */
1035 psz = var_GetNonEmptyString( p_input, "input-slave" );
1039 input_source_t *slave;
1040 while( psz && *psz )
1042 while( *psz == ' ' || *psz == '#' )
1046 if( ( psz_delim = strchr( psz, '#' ) ) )
1048 *psz_delim++ = '\0';
1055 msg_Dbg( p_input, "adding slave input '%s'", psz );
1056 slave = InputSourceNew( p_input );
1057 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1059 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1068 static void InitPrograms( input_thread_t * p_input )
1073 if( p_input->b_preparsing ) return;
1076 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1077 i_es_out_mode = ES_OUT_MODE_AUTO;
1079 if( p_input->p->p_sout )
1081 var_Get( p_input, "sout-all", &val );
1084 i_es_out_mode = ES_OUT_MODE_ALL;
1089 var_Get( p_input, "programs", &val );
1090 if( val.p_list && val.p_list->i_count )
1092 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1093 /* Note : we should remove the "program" callback. */
1097 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1102 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1104 /* Inform the demuxer about waited group (needed only for DVB) */
1105 if( i_es_out_mode == ES_OUT_MODE_ALL )
1107 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1109 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1111 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1116 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1117 (int) var_GetInteger( p_input, "program" ), NULL );
1121 static int Init( input_thread_t * p_input )
1126 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1128 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1130 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1131 var_SetString( p_input, "sout", "" );
1132 var_SetBool( p_input, "sout-all", false );
1133 var_SetString( p_input, "input-slave", "" );
1134 var_SetInteger( p_input, "input-repeat", 0 );
1135 var_SetString( p_input, "sub-file", "" );
1136 var_SetBool( p_input, "sub-autodetect-file", false );
1140 InitStatistics( p_input );
1142 ret = InitSout( p_input );
1143 if( ret != VLC_SUCCESS )
1144 return ret; /* FIXME: goto error; should be better here */
1148 p_input->p->p_es_out_display = input_EsOutNew( p_input, p_input->p->i_rate );
1149 p_input->p->p_es_out = input_EsOutTimeshiftNew( p_input, p_input->p->p_es_out_display, p_input->p->i_rate );
1150 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1151 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1153 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1154 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1156 if( InputSourceInit( p_input, &p_input->p->input,
1157 p_input->p->input.p_item->psz_uri, NULL ) )
1162 InitTitle( p_input );
1164 /* Load master infos */
1167 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1171 i_length = input_item_GetDuration( p_input->p->input.p_item );
1172 input_SendEventTimes( p_input, 0.0, 0, i_length );
1174 StartTitle( p_input );
1176 InitPrograms( p_input );
1178 if( !p_input->b_preparsing && p_input->p->p_sout )
1180 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1182 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1184 /* We don't want a high input priority here or we'll
1185 * end-up sucking up all the CPU time */
1186 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1189 msg_Dbg( p_input, "starting in %s mode",
1190 p_input->p->b_out_pace_control ? "async" : "sync" );
1193 p_meta = vlc_meta_New();
1195 /* Get meta data from users */
1196 InputMetaUser( p_input, p_meta );
1198 /* Get meta data from master input */
1199 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1201 /* Access_file does not give any meta, and there are no slave */
1202 AccessMeta( p_input, p_meta );
1204 InputUpdateMeta( p_input, p_meta );
1206 if( !p_input->b_preparsing )
1208 msg_Dbg( p_input, "`%s' successfully opened",
1209 p_input->p->input.p_item->psz_uri );
1213 /* initialization is complete */
1214 input_ChangeState( p_input, PLAYING_S );
1219 input_ChangeState( p_input, ERROR_S );
1221 if( p_input->p->p_es_out )
1222 es_out_Delete( p_input->p->p_es_out );
1223 if( p_input->p->p_es_out_display )
1224 es_out_Delete( p_input->p->p_es_out_display );
1226 if( p_input->p->p_sout )
1228 vlc_object_detach( p_input->p->p_sout );
1229 sout_DeleteInstance( p_input->p->p_sout );
1233 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1235 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1236 stats_CounterClean( p_input->p->counters.p_##c );\
1237 p_input->p->counters.p_##c = NULL; } while(0)
1238 EXIT_COUNTER( read_bytes );
1239 EXIT_COUNTER( read_packets );
1240 EXIT_COUNTER( demux_read );
1241 EXIT_COUNTER( input_bitrate );
1242 EXIT_COUNTER( demux_bitrate );
1243 EXIT_COUNTER( played_abuffers );
1244 EXIT_COUNTER( lost_abuffers );
1245 EXIT_COUNTER( displayed_pictures );
1246 EXIT_COUNTER( lost_pictures );
1247 EXIT_COUNTER( decoded_audio );
1248 EXIT_COUNTER( decoded_video );
1249 EXIT_COUNTER( decoded_sub );
1251 if( p_input->p->p_sout )
1253 EXIT_COUNTER( sout_sent_packets );
1254 EXIT_COUNTER( sout_sent_bytes );
1255 EXIT_COUNTER( sout_send_bitrate );
1260 /* Mark them deleted */
1261 p_input->p->input.p_demux = NULL;
1262 p_input->p->input.p_stream = NULL;
1263 p_input->p->input.p_access = NULL;
1264 p_input->p->p_es_out = NULL;
1265 p_input->p->p_es_out_display = NULL;
1266 p_input->p->p_sout = NULL;
1268 return VLC_EGENERIC;
1271 /*****************************************************************************
1272 * WaitDie: Wait until we are asked to die.
1273 *****************************************************************************
1274 * This function is called when an error occurred during thread main's loop.
1275 *****************************************************************************/
1276 static void WaitDie( input_thread_t *p_input )
1278 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1280 /* Wait a die order */
1281 vlc_object_lock( p_input );
1282 while( vlc_object_alive( p_input ) )
1283 vlc_object_wait( p_input );
1284 vlc_object_unlock( p_input );
1287 /*****************************************************************************
1288 * End: end the input thread
1289 *****************************************************************************/
1290 static void End( input_thread_t * p_input )
1294 /* We are at the end */
1295 input_ChangeState( p_input, END_S );
1297 /* Clean control variables */
1298 input_ControlVarStop( p_input );
1300 /* Clean up master */
1301 InputSourceClean( &p_input->p->input );
1304 for( i = 0; i < p_input->p->i_slave; i++ )
1306 InputSourceClean( p_input->p->slave[i] );
1307 free( p_input->p->slave[i] );
1309 free( p_input->p->slave );
1311 /* Unload all modules */
1312 if( p_input->p->p_es_out )
1313 es_out_Delete( p_input->p->p_es_out );
1314 if( p_input->p->p_es_out_display )
1315 es_out_Delete( p_input->p->p_es_out_display );
1317 if( !p_input->b_preparsing )
1319 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1320 if( libvlc_stats( p_input ) )
1322 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1324 /* make sure we are up to date */
1325 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1326 if( p_private->p_stats_computer == p_input )
1328 stats_ComputeGlobalStats( p_input->p_libvlc,
1329 p_input->p_libvlc->p_stats );
1330 p_private->p_stats_computer = NULL;
1332 CL_CO( read_bytes );
1333 CL_CO( read_packets );
1334 CL_CO( demux_read );
1335 CL_CO( input_bitrate );
1336 CL_CO( demux_bitrate );
1337 CL_CO( played_abuffers );
1338 CL_CO( lost_abuffers );
1339 CL_CO( displayed_pictures );
1340 CL_CO( lost_pictures );
1341 CL_CO( decoded_audio) ;
1342 CL_CO( decoded_video );
1343 CL_CO( decoded_sub) ;
1346 /* Close optional stream output instance */
1347 if( p_input->p->p_sout )
1349 CL_CO( sout_sent_packets );
1350 CL_CO( sout_sent_bytes );
1351 CL_CO( sout_send_bitrate );
1353 vlc_object_detach( p_input->p->p_sout );
1358 if( p_input->p->i_attachment > 0 )
1360 for( i = 0; i < p_input->p->i_attachment; i++ )
1361 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1362 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1365 /* Tell we're dead */
1366 p_input->b_dead = true;
1369 /*****************************************************************************
1371 *****************************************************************************/
1372 void input_ControlPush( input_thread_t *p_input,
1373 int i_type, vlc_value_t *p_val )
1375 vlc_mutex_lock( &p_input->p->lock_control );
1376 if( i_type == INPUT_CONTROL_SET_DIE )
1378 /* Special case, empty the control */
1379 p_input->p->i_control = 1;
1380 p_input->p->control[0].i_type = i_type;
1381 memset( &p_input->p->control[0].val, 0, sizeof( vlc_value_t ) );
1383 else if( p_input->p->i_control >= INPUT_CONTROL_FIFO_SIZE )
1385 msg_Err( p_input, "input control fifo overflow, trashing type=%d",
1390 p_input->p->control[p_input->p->i_control].i_type = i_type;
1392 p_input->p->control[p_input->p->i_control].val = *p_val;
1394 memset( &p_input->p->control[p_input->p->i_control].val, 0,
1395 sizeof( vlc_value_t ) );
1397 p_input->p->i_control++;
1399 vlc_cond_signal( &p_input->p->wait_control );
1400 vlc_mutex_unlock( &p_input->p->lock_control );
1403 static inline int ControlPopNoLock( input_thread_t *p_input,
1404 int *pi_type, vlc_value_t *p_val,
1405 mtime_t i_deadline )
1408 while( p_input->p->i_control <= 0 )
1410 if( !vlc_object_alive( p_input ) )
1411 return VLC_EGENERIC;
1413 if( i_deadline < 0 )
1414 return VLC_EGENERIC;
1416 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1417 return VLC_EGENERIC;
1420 *pi_type = p_input->p->control[0].i_type;
1421 *p_val = p_input->p->control[0].val;
1423 p_input->p->i_control--;
1424 if( p_input->p->i_control > 0 )
1428 for( i = 0; i < p_input->p->i_control; i++ )
1430 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1431 p_input->p->control[i].val = p_input->p->control[i+1].val;
1438 static void ControlReduce( input_thread_t *p_input )
1445 for( i = 1; i < p_input->p->i_control; i++ )
1447 const int i_lt = p_input->p->control[i-1].i_type;
1448 const int i_ct = p_input->p->control[i].i_type;
1450 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1451 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1455 ( i_ct == INPUT_CONTROL_SET_STATE ||
1456 i_ct == INPUT_CONTROL_SET_RATE ||
1457 i_ct == INPUT_CONTROL_SET_POSITION ||
1458 i_ct == INPUT_CONTROL_SET_TIME ||
1459 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1460 i_ct == INPUT_CONTROL_SET_TITLE ||
1461 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1462 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1465 // msg_Dbg( p_input, "merged at %d", i );
1466 /* Remove the i-1 */
1467 for( j = i; j < p_input->p->i_control; j++ )
1468 p_input->p->control[j-1] = p_input->p->control[j];
1469 p_input->p->i_control--;
1473 /* TODO but that's not that important
1474 - merge SET_X with SET_X_CMD
1475 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1476 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1483 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1485 int i_ret = VLC_SUCCESS;
1486 int i_state = PAUSE_S;
1488 if( p_input->p->b_can_pause )
1490 if( p_input->p->input.p_access )
1491 i_ret = access_Control( p_input->p->input.p_access,
1492 ACCESS_SET_PAUSE_STATE, true );
1494 i_ret = demux_Control( p_input->p->input.p_demux,
1495 DEMUX_SET_PAUSE_STATE, true );
1499 msg_Warn( p_input, "cannot set pause state" );
1500 i_state = p_input->i_state;
1507 i_ret = es_out_SetPauseState( p_input->p->p_es_out, p_input->p->b_can_pause, true, i_control_date );
1510 msg_Warn( p_input, "cannot set pause state at es_out level" );
1511 i_state = p_input->i_state;
1515 /* Switch to new state */
1516 input_ChangeState( p_input, i_state );
1520 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1522 int i_ret = VLC_SUCCESS;
1524 if( p_input->p->b_can_pause )
1526 if( p_input->p->input.p_access )
1527 i_ret = access_Control( p_input->p->input.p_access,
1528 ACCESS_SET_PAUSE_STATE, false );
1530 i_ret = demux_Control( p_input->p->input.p_demux,
1531 DEMUX_SET_PAUSE_STATE, false );
1534 /* FIXME What to do ? */
1535 msg_Warn( p_input, "cannot unset pause -> EOF" );
1536 vlc_mutex_unlock( &p_input->p->lock_control );
1537 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1538 vlc_mutex_lock( &p_input->p->lock_control );
1542 /* Switch to play */
1543 input_ChangeState( p_input, PLAYING_S );
1547 es_out_SetPauseState( p_input->p->p_es_out, false, false, i_control_date );
1550 static bool Control( input_thread_t *p_input, int i_type,
1553 const mtime_t i_control_date = mdate();
1554 /* FIXME b_force_update is abused, it should be carefully checked */
1555 bool b_force_update = false;
1558 return b_force_update;
1562 case INPUT_CONTROL_SET_DIE:
1563 msg_Dbg( p_input, "control: stopping input" );
1565 /* Mark all submodules to die */
1566 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1569 case INPUT_CONTROL_SET_POSITION:
1570 case INPUT_CONTROL_SET_POSITION_OFFSET:
1574 if( p_input->p->b_recording )
1576 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1579 if( i_type == INPUT_CONTROL_SET_POSITION )
1581 f_pos = val.f_float;
1585 /* Should not fail */
1586 demux_Control( p_input->p->input.p_demux,
1587 DEMUX_GET_POSITION, &f_pos );
1588 f_pos += val.f_float;
1590 if( f_pos < 0.0 ) f_pos = 0.0;
1591 if( f_pos > 1.0 ) f_pos = 1.0;
1592 /* Reset the decoders states and clock sync (before calling the demuxer */
1593 es_out_SetTime( p_input->p->p_es_out, -1 );
1594 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1597 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1598 "%2.1f%% failed", f_pos * 100 );
1602 if( p_input->p->i_slave > 0 )
1603 SlaveSeek( p_input );
1604 p_input->p->input.b_eof = false;
1606 b_force_update = true;
1611 case INPUT_CONTROL_SET_TIME:
1612 case INPUT_CONTROL_SET_TIME_OFFSET:
1617 if( p_input->p->b_recording )
1619 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1623 if( i_type == INPUT_CONTROL_SET_TIME )
1625 i_time = val.i_time;
1629 /* Should not fail */
1630 demux_Control( p_input->p->input.p_demux,
1631 DEMUX_GET_TIME, &i_time );
1632 i_time += val.i_time;
1634 if( i_time < 0 ) i_time = 0;
1636 /* Reset the decoders states and clock sync (before calling the demuxer */
1637 es_out_SetTime( p_input->p->p_es_out, -1 );
1639 i_ret = demux_Control( p_input->p->input.p_demux,
1640 DEMUX_SET_TIME, i_time );
1645 /* Emulate it with a SET_POS */
1646 demux_Control( p_input->p->input.p_demux,
1647 DEMUX_GET_LENGTH, &i_length );
1650 double f_pos = (double)i_time / (double)i_length;
1651 i_ret = demux_Control( p_input->p->input.p_demux,
1652 DEMUX_SET_POSITION, f_pos );
1657 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1658 " failed or not possible", i_time );
1662 if( p_input->p->i_slave > 0 )
1663 SlaveSeek( p_input );
1664 p_input->p->input.b_eof = false;
1666 b_force_update = true;
1671 case INPUT_CONTROL_SET_STATE:
1672 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1673 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1675 ControlUnpause( p_input, i_control_date );
1677 b_force_update = true;
1679 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S /* &&
1680 p_input->p->b_can_pause */ )
1682 ControlPause( p_input, i_control_date );
1684 b_force_update = true;
1686 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause && 0 )
1688 b_force_update = true;
1690 /* Correct "state" value */
1691 input_ChangeState( p_input, p_input->i_state );
1693 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1695 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1699 case INPUT_CONTROL_SET_RATE:
1700 case INPUT_CONTROL_SET_RATE_SLOWER:
1701 case INPUT_CONTROL_SET_RATE_FASTER:
1706 /* Get rate and direction */
1707 if( i_type == INPUT_CONTROL_SET_RATE )
1709 i_rate = abs( val.i_int );
1710 i_rate_sign = val.i_int < 0 ? -1 : 1;
1714 static const int ppi_factor[][2] = {
1715 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1717 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1724 i_rate_sign = p_input->p->i_rate < 0 ? -1 : 1;
1728 for( i = 0; ppi_factor[i][0] != 0; i++ )
1730 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1731 const int i_test_e = abs( abs( p_input->p->i_rate ) - i_test_r );
1732 if( i_test_e < i_error )
1739 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1741 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1743 if( ppi_factor[i_idx+1][0] > 0 )
1744 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1746 i_rate = INPUT_RATE_MAX+1;
1750 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1752 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1754 i_rate = INPUT_RATE_MIN-1;
1758 /* Check rate bound */
1759 if( i_rate < INPUT_RATE_MIN )
1761 msg_Dbg( p_input, "cannot set rate faster" );
1762 i_rate = INPUT_RATE_MIN;
1764 else if( i_rate > INPUT_RATE_MAX )
1766 msg_Dbg( p_input, "cannot set rate slower" );
1767 i_rate = INPUT_RATE_MAX;
1770 /* Apply direction */
1771 if( i_rate_sign < 0 )
1773 if( p_input->p->input.b_rescale_ts )
1775 msg_Dbg( p_input, "cannot set negative rate" );
1776 i_rate = p_input->p->i_rate;
1777 assert( i_rate > 0 );
1781 i_rate *= i_rate_sign;
1785 if( i_rate != INPUT_RATE_DEFAULT &&
1786 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1787 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1789 msg_Dbg( p_input, "cannot change rate" );
1790 i_rate = INPUT_RATE_DEFAULT;
1792 if( i_rate != p_input->p->i_rate &&
1793 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1796 if( p_input->p->input.p_access )
1798 i_ret = VLC_EGENERIC;
1802 if( !p_input->p->input.b_rescale_ts )
1803 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1805 i_ret = demux_Control( p_input->p->input.p_demux,
1806 DEMUX_SET_RATE, &i_rate );
1810 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1811 i_rate = p_input->p->i_rate;
1816 if( i_rate != p_input->p->i_rate )
1818 p_input->p->i_rate = i_rate;
1819 input_SendEventRate( p_input, i_rate );
1821 if( p_input->p->input.b_rescale_ts )
1823 const int i_rate_source = (p_input->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1824 es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1827 b_force_update = true;
1832 case INPUT_CONTROL_SET_PROGRAM:
1833 /* No need to force update, es_out does it if needed */
1834 es_out_Control( p_input->p->p_es_out,
1835 ES_OUT_SET_GROUP, val.i_int );
1837 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1841 case INPUT_CONTROL_SET_ES:
1842 /* No need to force update, es_out does it if needed */
1843 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, val.i_int );
1846 case INPUT_CONTROL_RESTART_ES:
1847 es_out_Control( p_input->p->p_es_out_display, ES_OUT_RESTART_ES_BY_ID, val.i_int );
1850 case INPUT_CONTROL_SET_AUDIO_DELAY:
1851 if( !es_out_SetDelay( p_input->p->p_es_out_display, AUDIO_ES, val.i_time ) )
1852 input_SendEventAudioDelay( p_input, val.i_time );
1855 case INPUT_CONTROL_SET_SPU_DELAY:
1856 if( !es_out_SetDelay( p_input->p->p_es_out_display, SPU_ES, val.i_time ) )
1857 input_SendEventSubtitleDelay( p_input, val.i_time );
1860 case INPUT_CONTROL_SET_TITLE:
1861 case INPUT_CONTROL_SET_TITLE_NEXT:
1862 case INPUT_CONTROL_SET_TITLE_PREV:
1863 if( p_input->p->b_recording )
1865 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1868 if( p_input->p->input.b_title_demux &&
1869 p_input->p->input.i_title > 0 )
1872 /* FIXME handle demux title */
1873 demux_t *p_demux = p_input->p->input.p_demux;
1876 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1877 i_title = p_demux->info.i_title - 1;
1878 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1879 i_title = p_demux->info.i_title + 1;
1881 i_title = val.i_int;
1883 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1885 es_out_SetTime( p_input->p->p_es_out, -1 );
1887 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1888 input_ControlVarTitle( p_input, i_title );
1891 else if( p_input->p->input.i_title > 0 )
1893 access_t *p_access = p_input->p->input.p_access;
1896 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1897 i_title = p_access->info.i_title - 1;
1898 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1899 i_title = p_access->info.i_title + 1;
1901 i_title = val.i_int;
1903 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1905 es_out_SetTime( p_input->p->p_es_out, -1 );
1907 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1908 stream_AccessReset( p_input->p->input.p_stream );
1912 case INPUT_CONTROL_SET_SEEKPOINT:
1913 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1914 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1915 if( p_input->p->b_recording )
1917 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1921 if( p_input->p->input.b_title_demux &&
1922 p_input->p->input.i_title > 0 )
1924 demux_t *p_demux = p_input->p->input.p_demux;
1926 int64_t i_input_time;
1927 int64_t i_seekpoint_time;
1929 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1931 i_seekpoint = p_demux->info.i_seekpoint;
1932 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1933 if( i_seekpoint_time >= 0 &&
1934 !demux_Control( p_demux,
1935 DEMUX_GET_TIME, &i_input_time ) )
1937 if( i_input_time < i_seekpoint_time + 3000000 )
1943 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1944 i_seekpoint = p_demux->info.i_seekpoint + 1;
1946 i_seekpoint = val.i_int;
1948 if( i_seekpoint >= 0 && i_seekpoint <
1949 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1952 es_out_SetTime( p_input->p->p_es_out, -1 );
1954 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1957 else if( p_input->p->input.i_title > 0 )
1959 demux_t *p_demux = p_input->p->input.p_demux;
1960 access_t *p_access = p_input->p->input.p_access;
1962 int64_t i_input_time;
1963 int64_t i_seekpoint_time;
1965 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1967 i_seekpoint = p_access->info.i_seekpoint;
1968 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1969 if( i_seekpoint_time >= 0 &&
1970 demux_Control( p_demux,
1971 DEMUX_GET_TIME, &i_input_time ) )
1973 if( i_input_time < i_seekpoint_time + 3000000 )
1979 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1980 i_seekpoint = p_access->info.i_seekpoint + 1;
1982 i_seekpoint = val.i_int;
1984 if( i_seekpoint >= 0 && i_seekpoint <
1985 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1987 es_out_SetTime( p_input->p->p_es_out, -1 );
1989 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1991 stream_AccessReset( p_input->p->input.p_stream );
1996 case INPUT_CONTROL_ADD_SUBTITLE:
1997 if( val.psz_string )
1999 SubtitleAdd( p_input, val.psz_string, true );
2000 free( val.psz_string );
2004 case INPUT_CONTROL_ADD_SLAVE:
2005 if( val.psz_string )
2007 input_source_t *slave = InputSourceNew( p_input );
2009 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
2015 msg_Dbg( p_input, "adding %s as slave on the fly",
2019 if( demux_Control( p_input->p->input.p_demux,
2020 DEMUX_GET_TIME, &i_time ) )
2022 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2023 InputSourceClean( slave );
2027 if( demux_Control( slave->p_demux,
2028 DEMUX_SET_TIME, i_time ) )
2030 msg_Err( p_input, "seek failed for new slave" );
2031 InputSourceClean( slave );
2036 /* Get meta (access and demux) */
2037 p_meta = vlc_meta_New();
2038 access_Control( slave->p_access, ACCESS_GET_META,
2040 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
2041 InputUpdateMeta( p_input, p_meta );
2043 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2048 msg_Warn( p_input, "failed to add %s as slave",
2052 free( val.psz_string );
2056 case INPUT_CONTROL_SET_RECORD_STATE:
2057 if( !!p_input->p->b_recording != !!val.b_bool )
2059 if( p_input->p->input.b_can_stream_record )
2061 if( demux_Control( p_input->p->input.p_demux,
2062 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2067 if( es_out_SetRecordState( p_input->p->p_es_out_display, val.b_bool ) )
2070 p_input->p->b_recording = val.b_bool;
2072 input_SendEventRecord( p_input, val.b_bool );
2074 b_force_update = true;
2078 case INPUT_CONTROL_SET_FRAME_NEXT:
2079 if( p_input->i_state == PAUSE_S )
2081 es_out_SetFrameNext( p_input->p->p_es_out );
2083 else if( p_input->i_state == PLAYING_S )
2085 ControlPause( p_input, i_control_date );
2089 msg_Err( p_input, "invalid state for frame next" );
2091 b_force_update = true;
2094 case INPUT_CONTROL_SET_BOOKMARK:
2096 msg_Err( p_input, "not yet implemented" );
2100 return b_force_update;
2103 /*****************************************************************************
2105 *****************************************************************************/
2106 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2107 int i_title, int i_seekpoint )
2109 int i_title_end = p_input->p->input.i_title_end -
2110 p_input->p->input.i_title_offset;
2111 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2112 p_input->p->input.i_seekpoint_offset;
2114 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2116 if( i_title > i_title_end ||
2117 ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2120 else if( i_seekpoint_end >= 0 )
2122 if( i_seekpoint > i_seekpoint_end )
2125 else if( i_title_end >= 0 )
2127 if( i_title > i_title_end )
2132 static int UpdateFromDemux( input_thread_t *p_input )
2134 demux_t *p_demux = p_input->p->input.p_demux;
2136 /* TODO event-like */
2137 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2139 input_SendEventTitle( p_input, p_demux->info.i_title );
2141 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2143 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2145 input_SendEventSeekpoint( p_input,
2146 p_demux->info.i_title, p_demux->info.i_seekpoint );
2148 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2150 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2152 /* Hmmm only works with master input */
2153 if( p_input->p->input.p_demux == p_demux )
2154 return UpdateTitleSeekpoint( p_input,
2155 p_demux->info.i_title,
2156 p_demux->info.i_seekpoint );
2160 /*****************************************************************************
2162 *****************************************************************************/
2163 static int UpdateFromAccess( input_thread_t *p_input )
2165 access_t *p_access = p_input->p->input.p_access;
2167 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2169 input_SendEventTitle( p_input, p_access->info.i_title );
2171 stream_AccessUpdate( p_input->p->input.p_stream );
2173 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2175 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2177 input_SendEventSeekpoint( p_input,
2178 p_access->info.i_title, p_access->info.i_seekpoint );
2180 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2182 if( p_access->info.i_update & INPUT_UPDATE_META )
2184 /* TODO maybe multi - access ? */
2185 vlc_meta_t *p_meta = vlc_meta_New();
2186 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2187 InputUpdateMeta( p_input, p_meta );
2188 p_access->info.i_update &= ~INPUT_UPDATE_META;
2190 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2195 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2196 f_quality = f_strength = -1;
2198 input_SendEventSignal( p_input, f_quality, f_strength );
2200 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2203 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2205 /* Hmmm only works with master input */
2206 if( p_input->p->input.p_access == p_access )
2207 return UpdateTitleSeekpoint( p_input,
2208 p_access->info.i_title,
2209 p_access->info.i_seekpoint );
2213 /*****************************************************************************
2215 *****************************************************************************/
2216 static input_source_t *InputSourceNew( input_thread_t *p_input )
2218 VLC_UNUSED(p_input);
2219 input_source_t *in = malloc( sizeof( input_source_t ) );
2221 memset( in, 0, sizeof( input_source_t ) );
2225 /*****************************************************************************
2227 *****************************************************************************/
2228 static int InputSourceInit( input_thread_t *p_input,
2229 input_source_t *in, const char *psz_mrl,
2230 const char *psz_forced_demux )
2232 const bool b_master = in == &p_input->p->input;
2234 char psz_dup[strlen(psz_mrl) + 1];
2235 const char *psz_access;
2236 const char *psz_demux;
2243 strcpy( psz_dup, psz_mrl );
2245 if( !in ) return VLC_EGENERIC;
2246 if( !p_input ) return VLC_EGENERIC;
2249 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2251 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2252 psz_mrl, psz_access, psz_demux, psz_path );
2253 if( !p_input->b_preparsing )
2255 /* Hack to allow udp://@:port syntax */
2257 (strncmp( psz_access, "udp", 3 ) &&
2258 strncmp( psz_access, "rtp", 3 )) )
2260 /* Find optional titles and seekpoints */
2261 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2262 &in->i_seekpoint_start, &in->i_seekpoint_end );
2265 if( psz_forced_demux && *psz_forced_demux )
2267 psz_demux = psz_forced_demux;
2269 else if( *psz_demux == '\0' )
2271 /* special hack for forcing a demuxer with --demux=module
2272 * (and do nothing with a list) */
2273 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2275 if( psz_var_demux != NULL &&
2276 !strchr(psz_var_demux, ',' ) &&
2277 !strchr(psz_var_demux, ':' ) )
2279 psz_demux = psz_var_demux;
2281 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2285 /* Try access_demux first */
2286 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2287 NULL, p_input->p->p_es_out, false );
2291 /* Preparsing is only for file:// */
2294 if( !*psz_access ) /* path without scheme:// */
2295 psz_access = "file";
2296 if( strcmp( psz_access, "file" ) )
2298 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2303 int64_t i_pts_delay;
2305 /* Get infos from access_demux */
2306 demux_Control( in->p_demux,
2307 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2308 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2310 in->b_title_demux = true;
2311 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2312 &in->title, &in->i_title,
2313 &in->i_title_offset, &in->i_seekpoint_offset ) )
2315 TAB_INIT( in->i_title, in->title );
2317 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2318 &in->b_can_pace_control ) )
2319 in->b_can_pace_control = false;
2321 if( !in->b_can_pace_control )
2323 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2324 &in->b_can_rate_control, &in->b_rescale_ts ) )
2326 in->b_can_rate_control = false;
2327 in->b_rescale_ts = true; /* not used */
2332 in->b_can_rate_control = true;
2333 in->b_rescale_ts = true;
2335 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2336 &in->b_can_pause ) )
2337 in->b_can_pause = false;
2338 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2339 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2340 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2342 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2344 if( ret != VLC_SUCCESS )
2346 var_Set( p_input, "can-seek", val );
2350 int64_t i_pts_delay;
2353 input_ChangeState( p_input, OPENING_S );
2355 /* Now try a real access */
2356 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2358 /* Access failed, URL encoded ? */
2359 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2361 decode_URI( psz_path );
2363 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2364 psz_access, psz_demux, psz_path );
2366 in->p_access = access_New( p_input,
2367 psz_access, psz_demux, psz_path );
2369 if( in->p_access == NULL )
2371 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2373 intf_UserFatal( VLC_OBJECT( p_input), false,
2374 _("Your input can't be opened"),
2375 _("VLC is unable to open the MRL '%s'."
2376 " Check the log for details."), psz_mrl );
2381 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2382 while( psz && *psz )
2384 access_t *p_access = in->p_access;
2385 char *end = strchr( psz, ':' );
2390 in->p_access = access_FilterNew( in->p_access, psz );
2391 if( in->p_access == NULL )
2393 in->p_access = p_access;
2394 msg_Warn( p_input, "failed to insert access filter %s",
2402 /* Get infos from access */
2403 if( !p_input->b_preparsing )
2405 access_Control( in->p_access,
2406 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2407 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2409 in->b_title_demux = false;
2410 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2411 &in->title, &in->i_title,
2412 &in->i_title_offset, &in->i_seekpoint_offset ) )
2415 TAB_INIT( in->i_title, in->title );
2417 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2418 &in->b_can_pace_control );
2419 in->b_can_rate_control = in->b_can_pace_control;
2420 in->b_rescale_ts = true;
2422 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2424 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2425 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2426 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2428 access_Control( in->p_access, ACCESS_CAN_SEEK,
2430 var_Set( p_input, "can-seek", val );
2434 input_ChangeState( p_input, BUFFERING_S );
2436 /* Autodetect extra files if none specified */
2437 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2438 if( !psz_input_list )
2440 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2441 if( psz_extra_files )
2442 var_SetString( p_input, "input-list", psz_extra_files );
2443 free( psz_extra_files );
2446 /* Create the stream_t */
2447 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2449 /* Restor old value */
2450 if( !psz_input_list )
2451 var_SetString( p_input, "input-list", "" );
2452 free( psz_input_list );
2454 if( in->p_stream == NULL )
2456 msg_Warn( p_input, "cannot create a stream_t from access" );
2460 /* Open a demuxer */
2461 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2463 psz_demux = in->p_access->psz_demux;
2467 /* Take access redirections into account */
2468 char *psz_real_path;
2469 char *psz_buf = NULL;
2470 if( in->p_access->psz_path )
2472 const char *psz_a, *psz_d;
2473 psz_buf = strdup( in->p_access->psz_path );
2474 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2478 psz_real_path = psz_path;
2480 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2482 in->p_stream, p_input->p->p_es_out,
2483 p_input->b_preparsing );
2487 if( in->p_demux == NULL )
2489 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2490 psz_access, psz_demux, psz_path );
2491 intf_UserFatal( VLC_OBJECT( p_input ), false,
2492 _("VLC can't recognize the input's format"),
2493 _("The format of '%s' cannot be detected. "
2494 "Have a look at the log for details."), psz_mrl );
2498 /* Get title from demux */
2499 if( !p_input->b_preparsing && in->i_title <= 0 )
2501 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2502 &in->title, &in->i_title,
2503 &in->i_title_offset, &in->i_seekpoint_offset ))
2505 TAB_INIT( in->i_title, in->title );
2509 in->b_title_demux = true;
2514 /* Set record capabilities */
2515 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2516 in->b_can_stream_record = false;
2518 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2519 in->b_can_stream_record = false;
2520 var_SetBool( p_input, "can-record", true );
2522 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2526 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2527 if( 1 || !p_input->b_preparsing )
2530 input_attachment_t **attachment;
2531 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2532 &attachment, &i_attachment ) )
2534 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2535 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2536 i_attachment, attachment );
2537 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2540 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2542 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2544 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2547 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2548 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2554 input_ChangeState( p_input, ERROR_S );
2557 demux_Delete( in->p_demux );
2560 stream_Delete( in->p_stream );
2563 access_Delete( in->p_access );
2565 return VLC_EGENERIC;
2568 /*****************************************************************************
2570 *****************************************************************************/
2571 static void InputSourceClean( input_source_t *in )
2576 demux_Delete( in->p_demux );
2579 stream_Delete( in->p_stream );
2582 access_Delete( in->p_access );
2584 if( in->i_title > 0 )
2586 for( i = 0; i < in->i_title; i++ )
2587 vlc_input_title_Delete( in->title[i] );
2588 TAB_CLEAN( in->i_title, in->title );
2592 static void SlaveDemux( input_thread_t *p_input )
2596 bool b_set_time = true;
2598 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2600 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2604 for( i = 0; i < p_input->p->i_slave; i++ )
2606 input_source_t *in = p_input->p->slave[i];
2612 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2617 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2619 msg_Err( p_input, "slave[%d] doesn't like "
2620 "DEMUX_GET_TIME -> EOF", i );
2625 if( i_stime >= i_time )
2628 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2634 i_ret = in->p_demux->pf_demux( in->p_demux );
2639 msg_Dbg( p_input, "slave %d EOF", i );
2645 static void SlaveSeek( input_thread_t *p_input )
2650 if( !p_input ) return;
2652 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2654 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2658 for( i = 0; i < p_input->p->i_slave; i++ )
2660 input_source_t *in = p_input->p->slave[i];
2662 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2665 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2675 /*****************************************************************************
2677 *****************************************************************************/
2678 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2682 if( !p_meta ) return;
2684 /* Get meta information from user */
2685 #define GET_META( field, s ) \
2686 var_Get( p_input, (s), &val ); \
2687 if( *val.psz_string ) \
2688 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2689 free( val.psz_string )
2691 GET_META( Title, "meta-title" );
2692 GET_META( Artist, "meta-artist" );
2693 GET_META( Genre, "meta-genre" );
2694 GET_META( Copyright, "meta-copyright" );
2695 GET_META( Description, "meta-description" );
2696 GET_META( Date, "meta-date" );
2697 GET_META( URL, "meta-url" );
2701 /*****************************************************************************
2702 * InputGetExtraFiles
2703 * Autodetect extra input list
2704 *****************************************************************************/
2705 static char *InputGetExtraFiles( input_thread_t *p_input,
2706 const char *psz_access, const char *psz_path )
2708 char *psz_list = NULL;
2710 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2714 const char *psz_ext = strrchr( psz_path, '.' );
2715 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2718 char *psz_file = strdup( psz_path );
2722 /* Try to list .xyz files */
2723 for( int i = 2; i < 999; i++ )
2725 char *psz_ext = strrchr( psz_file, '.' );
2728 snprintf( psz_ext, 5, ".%.3d", i );
2730 if( utf8_stat( psz_file, &st )
2731 || !S_ISREG( st.st_mode ) || !st.st_size )
2734 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2738 char *psz_old = psz_list;
2739 /* FIXME how to handle file with ',' ?*/
2740 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2748 psz_list = strdup( psz_file );
2756 /*****************************************************************************
2757 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2758 * arturl and locking issue.
2759 *****************************************************************************/
2760 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2762 input_item_t *p_item = p_input->p->input.p_item;
2763 char * psz_arturl = NULL;
2764 char *psz_title = NULL;
2769 psz_arturl = input_item_GetArtURL( p_item );
2771 vlc_mutex_lock( &p_item->lock );
2773 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2774 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2776 vlc_meta_Merge( p_item->p_meta, p_meta );
2778 vlc_meta_Delete( p_meta );
2780 if( psz_arturl && *psz_arturl )
2782 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2784 if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2786 /* Don't look for art cover if sout
2787 * XXX It can change when sout has meta data support */
2788 if( p_input->p->p_sout && !p_input->b_preparsing )
2789 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2791 input_ExtractAttachmentAndCacheArt( p_input );
2796 vlc_mutex_unlock( &p_item->lock );
2800 input_item_SetName( p_item, psz_title );
2803 input_item_SetPreparsed( p_item, true );
2805 input_SendEventMeta( p_input );
2807 /** \todo handle sout meta */
2811 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2812 int i_new, input_attachment_t **pp_new )
2814 int i_attachment = *pi_attachment;
2815 input_attachment_t **attachment = *ppp_attachment;
2818 attachment = realloc( attachment,
2819 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2820 for( i = 0; i < i_new; i++ )
2821 attachment[i_attachment++] = pp_new[i];
2825 *pi_attachment = i_attachment;
2826 *ppp_attachment = attachment;
2829 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2833 if( p_input->b_preparsing )
2836 if( p_input->p->input.p_access )
2837 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2840 /* Get meta data from slave input */
2841 for( i = 0; i < p_input->p->i_slave; i++ )
2843 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2844 if( p_input->p->slave[i]->p_access )
2846 access_Control( p_input->p->slave[i]->p_access,
2847 ACCESS_GET_META, p_meta );
2852 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2858 /* XXX I am not sure it is a great idea, besides, there is more than that
2859 * if we want to do it right */
2860 vlc_mutex_lock( &p_item->lock );
2861 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2863 vlc_mutex_unlock( &p_item->lock );
2866 vlc_mutex_unlock( &p_item->lock );
2869 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2870 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2875 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2876 if(! p_demux->p_private )
2879 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2882 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2884 if( p_demux_meta->p_meta )
2886 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2887 vlc_meta_Delete( p_demux_meta->p_meta );
2890 if( p_demux_meta->i_attachments > 0 )
2892 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2893 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2894 p_demux_meta->i_attachments, p_demux_meta->attachments );
2895 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2897 module_unneed( p_demux, p_id3 );
2899 free( p_demux->p_private );
2902 static void input_ChangeState( input_thread_t *p_input, int i_state )
2904 const bool b_changed = p_input->i_state != i_state;
2906 p_input->i_state = i_state;
2907 if( i_state == ERROR_S )
2908 p_input->b_error = true;
2909 else if( i_state == END_S )
2910 p_input->b_eof = true;
2914 input_item_SetErrorWhenReading( p_input->p->input.p_item, p_input->b_error );
2915 input_SendEventState( p_input, i_state );
2920 /*****************************************************************************
2921 * MRLSplit: parse the access, demux and url part of the
2922 * Media Resource Locator.
2923 *****************************************************************************/
2924 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2927 char *psz_access = NULL;
2928 char *psz_demux = NULL;
2931 /* Either there is an access/demux specification before ://
2932 * or we have a plain local file path. */
2933 psz_path = strstr( psz_dup, "://" );
2934 if( psz_path != NULL )
2937 psz_path += 3; /* skips "://" */
2939 /* Separate access from demux (<access>/<demux>://<path>) */
2940 psz_access = psz_dup;
2941 psz_demux = strchr( psz_access, '/' );
2943 *psz_demux++ = '\0';
2945 /* We really don't want module name substitution here! */
2946 if( psz_access[0] == '$' )
2948 if( psz_demux && psz_demux[0] == '$' )
2955 *ppsz_access = psz_access ? psz_access : (char*)"";
2956 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2957 *ppsz_path = psz_path;
2960 static inline bool next(char ** src)
2964 long result = strtol( *src, &end, 0 );
2965 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2974 /*****************************************************************************
2975 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2978 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2979 *****************************************************************************/
2980 static void MRLSections( input_thread_t *p_input, char *psz_source,
2981 int *pi_title_start, int *pi_title_end,
2982 int *pi_chapter_start, int *pi_chapter_end )
2984 char *psz, *psz_end, *psz_next, *psz_check;
2986 *pi_title_start = *pi_title_end = -1;
2987 *pi_chapter_start = *pi_chapter_end = -1;
2989 /* Start by parsing titles and chapters */
2990 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2993 /* Check we are really dealing with a title/chapter section */
2994 psz_check = psz + 1;
2995 if( !*psz_check ) return;
2996 if( isdigit(*psz_check) )
2997 if(!next(&psz_check)) return;
2998 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2999 if( *psz_check == ':' && ++psz_check )
3001 if( isdigit(*psz_check) )
3002 if(!next(&psz_check)) return;
3004 if( *psz_check != '-' && *psz_check ) return;
3005 if( *psz_check == '-' && ++psz_check )
3007 if( isdigit(*psz_check) )
3008 if(!next(&psz_check)) return;
3010 if( *psz_check != ':' && *psz_check ) return;
3011 if( *psz_check == ':' && ++psz_check )
3013 if( isdigit(*psz_check) )
3014 if(!next(&psz_check)) return;
3016 if( *psz_check ) return;
3018 /* Separate start and end */
3020 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
3022 /* Look for the start title */
3023 *pi_title_start = strtol( psz, &psz_next, 0 );
3024 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
3025 *pi_title_end = *pi_title_start;
3028 /* Look for the start chapter */
3030 *pi_chapter_start = strtol( psz, &psz_next, 0 );
3031 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
3032 *pi_chapter_end = *pi_chapter_start;
3036 /* Look for the end title */
3037 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3038 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3041 /* Look for the end chapter */
3042 if( *psz_end ) psz_end++;
3043 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3044 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3047 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3048 psz_source, *pi_title_start, *pi_chapter_start,
3049 *pi_title_end, *pi_chapter_end );
3052 /*****************************************************************************
3053 * input_AddSubtitles: add a subtitles file and enable it
3054 *****************************************************************************/
3055 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3057 input_source_t *sub;
3060 char *psz_path, *psz_extension;
3062 /* if we are provided a subtitle.sub file,
3063 * see if we don't have a subtitle.idx and use it instead */
3064 psz_path = strdup( psz_subtitle );
3067 psz_extension = strrchr( psz_path, '.');
3068 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3072 strcpy( psz_extension, ".idx" );
3074 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3076 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3077 psz_path, psz_subtitle );
3078 strcpy( psz_subtitle, psz_path );
3084 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3086 sub = InputSourceNew( p_input );
3087 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3092 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3095 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3097 if( count.i_int == 0 )
3099 /* if it was first one, there is disable too */
3101 if( count.i_int < list.p_list->i_count )
3103 const int i_id = list.p_list->p_values[count.i_int].i_int;
3105 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3106 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3108 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3112 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3113 bool b_check_extension )
3117 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3120 assert( psz_subtitle != NULL );
3122 val.psz_string = strdup( psz_subtitle );
3123 if( val.psz_string )
3124 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3128 /*****************************************************************************
3130 *****************************************************************************/
3131 void input_UpdateStatistic( input_thread_t *p_input,
3132 input_statistic_t i_type, int i_delta )
3134 assert( p_input->i_state != INIT_S );
3136 vlc_mutex_lock( &p_input->p->counters.counters_lock);
3139 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
3140 case INPUT_STATISTIC_DECODED_VIDEO:
3143 case INPUT_STATISTIC_DECODED_AUDIO:
3146 case INPUT_STATISTIC_DECODED_SUBTITLE:
3149 case INPUT_STATISTIC_SENT_PACKET:
3150 I(p_sout_sent_packets);
3153 case INPUT_STATISTIC_SENT_BYTE:
3155 int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
3157 if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
3158 stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
3162 msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3165 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
3167 /*****************************************************************************
3168 * input_get_event_manager
3169 *****************************************************************************/
3170 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3172 return &p_input->p->event_manager;
3176 /* TODO FIXME nearly the same logic that snapshot code */
3177 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3182 path = utf8_opendir( psz_path );
3187 char *psz_tmp = str_format( p_obj, psz_prefix );
3191 filename_sanitize( psz_tmp );
3192 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3194 psz_extension ? "." : "",
3195 psz_extension ? psz_extension : "" ) < 0 )
3202 psz_file = str_format( p_obj, psz_path );
3203 path_sanitize( psz_file );