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"
49 #include "../stream_output/stream_output.h"
51 #include <vlc_dialog.h>
53 #include <vlc_charset.h>
55 #include <vlc_strings.h>
57 #ifdef HAVE_SYS_STAT_H
58 # include <sys/stat.h>
61 /*****************************************************************************
63 *****************************************************************************/
64 static void Destructor( input_thread_t * p_input );
66 static void *Run ( vlc_object_t *p_this );
68 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
69 const char *, bool, input_resource_t * );
70 static int Init ( input_thread_t *p_input );
71 static void End ( input_thread_t *p_input );
72 static void MainLoop( input_thread_t *p_input, bool b_interactive );
74 static void ObjectKillChildrens( input_thread_t *, vlc_object_t * );
76 static inline int ControlPop( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline, bool b_postpone_seek );
77 static void ControlRelease( int i_type, vlc_value_t val );
78 static bool ControlIsSeekRequest( int i_type );
79 static bool Control( input_thread_t *, int, vlc_value_t );
81 static int UpdateTitleSeekpointFromAccess( input_thread_t * );
82 static void UpdateGenericFromAccess( input_thread_t * );
84 static int UpdateTitleSeekpointFromDemux( input_thread_t * );
85 static void UpdateGenericFromDemux( input_thread_t * );
87 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
89 static input_source_t *InputSourceNew( input_thread_t *);
90 static int InputSourceInit( input_thread_t *, input_source_t *,
91 const char *, const char *psz_forced_demux );
92 static void InputSourceClean( input_source_t * );
93 static void InputSourceMeta( input_thread_t *, input_source_t *, vlc_meta_t * );
96 //static void InputGetAttachments( input_thread_t *, input_source_t * );
97 static void SlaveDemux( input_thread_t *p_input, bool *pb_demux_polled );
98 static void SlaveSeek( input_thread_t *p_input );
100 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
101 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
102 static void InputGetExtraFiles( input_thread_t *p_input,
103 int *pi_list, char ***pppsz_list,
104 const char *psz_access, const char *psz_path );
106 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
107 int i_new, input_attachment_t **pp_new );
109 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced );
111 static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */
115 * Create a new input_thread_t.
117 * You need to call input_Start on it when you are done
118 * adding callback on the variables/events you want to monitor.
120 * \param p_parent a vlc_object
121 * \param p_item an input item
122 * \param psz_log an optional prefix for this input logs
123 * \param p_resource an optional input ressource
124 * \return a pointer to the spawned input thread
126 input_thread_t *input_Create( vlc_object_t *p_parent,
127 input_item_t *p_item,
128 const char *psz_log, input_resource_t *p_resource )
130 return Create( p_parent, p_item, psz_log, false, p_resource );
133 #undef input_CreateAndStart
135 * Create a new input_thread_t and start it.
137 * Provided for convenience.
141 input_thread_t *input_CreateAndStart( vlc_object_t *p_parent,
142 input_item_t *p_item, const char *psz_log )
144 input_thread_t *p_input = input_Create( p_parent, p_item, psz_log, NULL );
146 if( input_Start( p_input ) )
148 vlc_object_release( p_input );
156 * Initialize an input thread and run it until it stops by itself.
158 * \param p_parent a vlc_object
159 * \param p_item an input item
160 * \return an error code, VLC_SUCCESS on success
162 int input_Read( vlc_object_t *p_parent, input_item_t *p_item )
164 input_thread_t *p_input = Create( p_parent, p_item, NULL, false, NULL );
168 if( !Init( p_input ) )
170 MainLoop( p_input, false );
174 vlc_object_release( p_input );
179 * Initialize an input and initialize it to preparse the item
180 * This function is blocking. It will only accept parsing regular files.
182 * \param p_parent a vlc_object_t
183 * \param p_item an input item
184 * \return VLC_SUCCESS or an error
186 int input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
188 input_thread_t *p_input;
190 /* Allocate descriptor */
191 p_input = Create( p_parent, p_item, NULL, true, NULL );
195 if( !Init( p_input ) )
198 vlc_object_release( p_input );
204 * Start a input_thread_t created by input_Create.
206 * You must not start an already running input_thread_t.
208 * \param the input thread to start
210 int input_Start( input_thread_t *p_input )
212 /* Create thread and wait for its readiness. */
213 if( vlc_thread_create( p_input, "input", Run,
214 VLC_THREAD_PRIORITY_INPUT ) )
216 input_ChangeState( p_input, ERROR_S );
217 msg_Err( p_input, "cannot create input thread" );
224 * Request a running input thread to stop and die
226 * b_abort must be true when a user stop is requested and not because you have
227 * detected an error or an eof. It will be used to properly send the
228 * INPUT_EVENT_ABORT event.
230 * \param p_input the input thread to stop
231 * \param b_abort true if the input has been aborted by a user request
233 void input_Stop( input_thread_t *p_input, bool b_abort )
235 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
236 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
237 * unlock the control loop */
238 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
240 vlc_mutex_lock( &p_input->p->lock_control );
241 p_input->p->b_abort |= b_abort;
242 vlc_mutex_unlock( &p_input->p->lock_control );
244 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
247 input_resource_t *input_DetachResource( input_thread_t *p_input )
249 assert( p_input->b_dead );
251 input_resource_SetInput( p_input->p->p_resource, NULL );
253 input_resource_t *p_resource = input_resource_Detach( p_input->p->p_resource );
254 p_input->p->p_sout = NULL;
260 * Get the item from an input thread
261 * FIXME it does not increase ref count of the item.
262 * if it is used after p_input is destroyed nothing prevent it from
265 input_item_t *input_GetItem( input_thread_t *p_input )
267 assert( p_input && p_input->p );
268 return p_input->p->p_item;
271 /*****************************************************************************
272 * ObjectKillChildrens
273 *****************************************************************************/
274 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
279 /* FIXME ObjectKillChildrens seems a very bad idea in fact */
280 i = vlc_internals( p_obj )->i_object_type;
281 if( i == VLC_OBJECT_VOUT ||i == VLC_OBJECT_AOUT ||
282 p_obj == VLC_OBJECT(p_input->p->p_sout) ||
283 i == VLC_OBJECT_DECODER )
286 vlc_object_kill( p_obj );
288 p_list = vlc_list_children( p_obj );
289 for( i = 0; i < p_list->i_count; i++ )
290 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
291 vlc_list_release( p_list );
294 /*****************************************************************************
295 * This function creates a new input, and returns a pointer
296 * to its description. On error, it returns NULL.
298 * XXX Do not forget to update vlc_input.h if you add new variables.
299 *****************************************************************************/
300 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
301 const char *psz_header, bool b_quick,
302 input_resource_t *p_resource )
304 static const char input_name[] = "input";
305 input_thread_t *p_input = NULL; /* thread descriptor */
308 /* Allocate descriptor */
309 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
310 VLC_OBJECT_INPUT, input_name );
311 if( p_input == NULL )
314 vlc_object_attach( p_input, p_parent );
316 /* Construct a nice name for the input timer */
317 char psz_timer_name[255];
318 char * psz_name = input_item_GetName( p_item );
319 snprintf( psz_timer_name, sizeof(psz_timer_name),
320 "input launching for '%s'", psz_name );
322 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
326 /* Start a timer to mesure how long it takes
327 * to launch an input */
328 stats_TimerStart( p_input, psz_timer_name,
329 STATS_TIMER_INPUT_LAUNCHING );
331 p_input->p = calloc( 1, sizeof( input_thread_private_t ) );
335 /* Parse input options */
336 vlc_mutex_lock( &p_item->lock );
337 assert( (int)p_item->optflagc == p_item->i_options );
338 for( i = 0; i < p_item->i_options; i++ )
339 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
340 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
341 vlc_mutex_unlock( &p_item->lock );
343 p_input->b_preparsing = b_quick;
344 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
346 /* Init Common fields */
347 p_input->b_eof = false;
348 p_input->p->b_can_pace_control = true;
349 p_input->p->i_start = 0;
350 p_input->p->i_time = 0;
351 p_input->p->i_stop = 0;
352 p_input->p->i_run = 0;
353 p_input->p->i_title = 0;
354 p_input->p->title = NULL;
355 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
356 p_input->p->i_state = INIT_S;
357 double f_rate = var_InheritFloat( p_input, "rate" );
360 msg_Warn( p_input, "Negative or zero rate values are forbidden" );
363 p_input->p->i_rate = INPUT_RATE_DEFAULT / f_rate;
364 p_input->p->b_recording = false;
365 memset( &p_input->p->bookmark, 0, sizeof(p_input->p->bookmark) );
366 TAB_INIT( p_input->p->i_bookmark, p_input->p->pp_bookmark );
367 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
368 p_input->p->p_sout = NULL;
369 p_input->p->b_out_pace_control = false;
371 vlc_gc_incref( p_item ); /* Released in Destructor() */
372 p_input->p->p_item = p_item;
374 /* Init Input fields */
375 p_input->p->input.p_access = NULL;
376 p_input->p->input.p_stream = NULL;
377 p_input->p->input.p_demux = NULL;
378 p_input->p->input.b_title_demux = false;
379 p_input->p->input.i_title = 0;
380 p_input->p->input.title = NULL;
381 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
382 p_input->p->input.b_can_pace_control = true;
383 p_input->p->input.b_can_rate_control = true;
384 p_input->p->input.b_rescale_ts = true;
385 p_input->p->input.b_eof = false;
387 vlc_mutex_lock( &p_item->lock );
389 if( !p_item->p_stats )
390 p_item->p_stats = stats_NewInputStats( p_input );
391 vlc_mutex_unlock( &p_item->lock );
394 p_input->p->i_slave = 0;
395 p_input->p->slave = NULL;
399 p_input->p->p_resource = p_resource;
401 p_input->p->p_resource = input_resource_New();
402 input_resource_SetInput( p_input->p->p_resource, p_input );
404 /* Init control buffer */
405 vlc_mutex_init( &p_input->p->lock_control );
406 vlc_cond_init( &p_input->p->wait_control );
407 p_input->p->i_control = 0;
408 p_input->p->b_abort = false;
410 /* Create Object Variables for private use only */
411 input_ConfigVarInit( p_input );
413 /* Create Objects variables for public Get and Set */
414 input_ControlVarInit( p_input );
417 if( !p_input->b_preparsing )
419 char *psz_bookmarks = var_GetNonEmptyString( p_input, "bookmarks" );
422 /* FIXME: have a common cfg parsing routine used by sout and others */
423 char *psz_parser, *psz_start, *psz_end;
424 psz_parser = psz_bookmarks;
425 while( (psz_start = strchr( psz_parser, '{' ) ) )
427 seekpoint_t *p_seekpoint;
430 psz_end = strchr( psz_start, '}' );
431 if( !psz_end ) break;
432 psz_parser = psz_end + 1;
433 backup = *psz_parser;
437 p_seekpoint = vlc_seekpoint_New();
438 while( (psz_end = strchr( psz_start, ',' ) ) )
441 if( !strncmp( psz_start, "name=", 5 ) )
443 p_seekpoint->psz_name = strdup(psz_start + 5);
445 else if( !strncmp( psz_start, "bytes=", 6 ) )
447 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
449 else if( !strncmp( psz_start, "time=", 5 ) )
451 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
454 psz_start = psz_end + 1;
456 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
457 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
458 p_seekpoint->i_time_offset );
459 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
460 vlc_seekpoint_Delete( p_seekpoint );
461 *psz_parser = backup;
463 free( psz_bookmarks );
467 /* Remove 'Now playing' info as it is probably outdated */
468 input_item_SetNowPlaying( p_item, NULL );
469 input_SendEventMeta( p_input );
472 if( p_input->b_preparsing )
473 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
475 /* Make sure the interaction option is honored */
476 if( !var_InheritBool( p_input, "interact" ) )
477 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
480 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
481 vlc_mutex_init( &p_input->p->counters.counters_lock );
483 p_input->p->p_es_out_display = input_EsOutNew( p_input, p_input->p->i_rate );
484 p_input->p->p_es_out = NULL;
486 /* Set the destructor when we are sure we are initialized */
487 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
493 * Input destructor (called when the object's refcount reaches 0).
495 static void Destructor( input_thread_t * p_input )
498 char * psz_name = input_item_GetName( p_input->p->p_item );
499 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
503 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
504 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
506 if( p_input->p->p_es_out_display )
507 es_out_Delete( p_input->p->p_es_out_display );
509 if( p_input->p->p_resource )
510 input_resource_Delete( p_input->p->p_resource );
512 vlc_gc_decref( p_input->p->p_item );
514 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
516 for( int i = 0; i < p_input->p->i_control; i++ )
518 input_control_t *p_ctrl = &p_input->p->control[i];
519 ControlRelease( p_ctrl->i_type, p_ctrl->val );
522 vlc_cond_destroy( &p_input->p->wait_control );
523 vlc_mutex_destroy( &p_input->p->lock_control );
527 /*****************************************************************************
528 * Run: main thread loop
529 * This is the "normal" thread that spawns the input processing chain,
530 * reads the stream, cleans up and waits
531 *****************************************************************************/
532 static void *Run( vlc_object_t *p_this )
534 input_thread_t *p_input = (input_thread_t *)p_this;
535 const int canc = vlc_savecancel();
537 if( Init( p_input ) )
540 MainLoop( p_input, true ); /* FIXME it can be wrong (like with VLM) */
546 /* Tell we're dead */
547 vlc_mutex_lock( &p_input->p->lock_control );
548 const bool b_abort = p_input->p->b_abort;
549 vlc_mutex_unlock( &p_input->p->lock_control );
552 input_SendEventAbort( p_input );
553 input_SendEventDead( p_input );
555 vlc_restorecancel( canc );
559 /*****************************************************************************
560 * Main loop: Fill buffers from access, and demux
561 *****************************************************************************/
565 * It asks the demuxer to demux some data
567 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, bool *pb_demux_polled, mtime_t i_start_mdate )
572 *pb_demux_polled = p_input->p->input.p_demux->pf_demux != NULL;
574 if( ( p_input->p->i_stop > 0 && p_input->p->i_time >= p_input->p->i_stop ) ||
575 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
578 i_ret = demux_Demux( p_input->p->input.p_demux );
582 if( p_input->p->input.p_demux->info.i_update )
584 if( p_input->p->input.b_title_demux )
586 i_ret = UpdateTitleSeekpointFromDemux( p_input );
589 UpdateGenericFromDemux( p_input );
591 else if( p_input->p->input.p_access &&
592 p_input->p->input.p_access->info.i_update )
594 if( !p_input->p->input.b_title_demux )
596 i_ret = UpdateTitleSeekpointFromAccess( p_input );
599 UpdateGenericFromAccess( p_input );
603 if( i_ret == 0 ) /* EOF */
605 msg_Dbg( p_input, "EOF reached" );
606 p_input->p->input.b_eof = true;
610 input_ChangeState( p_input, ERROR_S );
613 if( i_ret > 0 && p_input->p->i_slave > 0 )
616 SlaveDemux( p_input, &b_demux_polled );
618 *pb_demux_polled |= b_demux_polled;
622 static int MainLoopTryRepeat( input_thread_t *p_input, mtime_t *pi_start_mdate )
624 int i_repeat = var_GetInteger( p_input, "input-repeat" );
630 msg_Dbg( p_input, "repeating the same input (%d)", i_repeat );
634 var_SetInteger( p_input, "input-repeat", i_repeat );
637 /* Seek to start title/seekpoint */
638 val.i_int = p_input->p->input.i_title_start -
639 p_input->p->input.i_title_offset;
640 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
642 input_ControlPush( p_input,
643 INPUT_CONTROL_SET_TITLE, &val );
645 val.i_int = p_input->p->input.i_seekpoint_start -
646 p_input->p->input.i_seekpoint_offset;
647 if( val.i_int > 0 /* TODO: check upper boundary */ )
648 input_ControlPush( p_input,
649 INPUT_CONTROL_SET_SEEKPOINT, &val );
651 /* Seek to start position */
652 if( p_input->p->i_start > 0 )
654 val.i_time = p_input->p->i_start;
655 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &val );
660 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION, &val );
664 *pi_start_mdate = mdate();
670 * It update the variables used by the interfaces
672 static void MainLoopInterface( input_thread_t *p_input )
674 double f_position = 0.0;
676 mtime_t i_length = 0;
678 /* update input status variables */
679 if( demux_Control( p_input->p->input.p_demux,
680 DEMUX_GET_POSITION, &f_position ) )
683 if( demux_Control( p_input->p->input.p_demux,
684 DEMUX_GET_TIME, &i_time ) )
686 p_input->p->i_time = i_time;
688 if( demux_Control( p_input->p->input.p_demux,
689 DEMUX_GET_LENGTH, &i_length ) )
692 es_out_SetTimes( p_input->p->p_es_out, f_position, i_time, i_length );
694 /* update current bookmark */
695 vlc_mutex_lock( &p_input->p->p_item->lock );
696 p_input->p->bookmark.i_time_offset = i_time;
697 if( p_input->p->input.p_stream )
698 p_input->p->bookmark.i_byte_offset = stream_Tell( p_input->p->input.p_stream );
699 vlc_mutex_unlock( &p_input->p->p_item->lock );
704 * It updates the globals statics
706 static void MainLoopStatistic( input_thread_t *p_input )
708 stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
709 input_SendEventStatistics( p_input );
714 * The main input loop.
716 static void MainLoop( input_thread_t *p_input, bool b_interactive )
718 mtime_t i_start_mdate = mdate();
719 mtime_t i_intf_update = 0;
720 mtime_t i_statistic_update = 0;
721 mtime_t i_last_seek_mdate = 0;
722 bool b_pause_after_eof = b_interactive &&
723 var_CreateGetBool( p_input, "play-and-pause" );
725 /* Start the timer */
726 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
728 while( vlc_object_alive( p_input ) && !p_input->b_error )
738 b_force_update = false;
740 /* FIXME if p_input->p->i_state == PAUSE_S the access/access_demux
741 * is paused -> this may cause problem with some of them
742 * The same problem can be seen when seeking while paused */
743 b_paused = p_input->p->i_state == PAUSE_S &&
744 ( !es_out_GetBuffering( p_input->p->p_es_out ) || p_input->p->input.b_eof );
746 b_demux_polled = true;
749 if( !p_input->p->input.b_eof )
751 MainLoopDemux( p_input, &b_force_update, &b_demux_polled, i_start_mdate );
753 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
755 else if( !es_out_GetEmpty( p_input->p->p_es_out ) )
757 msg_Dbg( p_input, "waiting decoder fifos to empty" );
758 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
760 /* Pause after eof only if the input is pausable.
761 * This way we won't trigger timeshifting for nothing */
762 else if( b_pause_after_eof && p_input->p->b_can_pause )
764 msg_Dbg( p_input, "pausing at EOF (pause after each)");
766 Control( p_input, INPUT_CONTROL_SET_STATE, val );
768 b_pause_after_eof = false;
773 if( MainLoopTryRepeat( p_input, &i_start_mdate ) )
775 b_pause_after_eof = var_GetBool( p_input, "play-and-pause" );
781 mtime_t i_deadline = i_wakeup;
782 if( b_paused || !b_demux_polled )
783 i_deadline = __MIN( i_intf_update, i_statistic_update );
788 mtime_t i_limit = i_deadline;
790 /* We will postpone the execution of a seek until we have
791 * finished the ES bufferisation (postpone is limited to
793 bool b_buffering = es_out_GetBuffering( p_input->p->p_es_out ) &&
794 !p_input->p->input.b_eof;
797 /* When postpone is in order, check the ES level every 20ms */
798 mtime_t i_current = mdate();
799 if( i_last_seek_mdate + INT64_C(125000) >= i_current )
800 i_limit = __MIN( i_deadline, i_current + INT64_C(20000) );
804 if( ControlPop( p_input, &i_type, &val, i_limit, b_buffering ) )
806 if( b_buffering && i_limit < i_deadline )
811 msg_Dbg( p_input, "control type=%d", i_type );
813 if( Control( p_input, i_type, val ) )
815 if( ControlIsSeekRequest( i_type ) )
816 i_last_seek_mdate = mdate();
817 b_force_update = true;
821 /* Update interface and statistics */
823 if( i_intf_update < i_current || b_force_update )
825 MainLoopInterface( p_input );
826 i_intf_update = i_current + INT64_C(250000);
827 b_force_update = false;
829 if( i_statistic_update < i_current )
831 MainLoopStatistic( p_input );
832 i_statistic_update = i_current + INT64_C(1000000);
835 /* Update the wakeup time */
837 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
838 } while( i_current < i_wakeup );
841 if( !p_input->b_error )
842 input_ChangeState( p_input, END_S );
845 static void InitStatistics( input_thread_t * p_input )
847 if( p_input->b_preparsing ) return;
849 /* Prepare statistics */
850 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
851 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
852 if( libvlc_stats( p_input ) )
854 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
855 INIT_COUNTER( read_packets, INTEGER, COUNTER );
856 INIT_COUNTER( demux_read, INTEGER, COUNTER );
857 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
858 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
859 INIT_COUNTER( demux_corrupted, INTEGER, COUNTER );
860 INIT_COUNTER( demux_discontinuity, INTEGER, COUNTER );
861 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
862 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
863 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
864 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
865 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
866 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
867 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
868 p_input->p->counters.p_sout_send_bitrate = NULL;
869 p_input->p->counters.p_sout_sent_packets = NULL;
870 p_input->p->counters.p_sout_sent_bytes = NULL;
871 if( p_input->p->counters.p_demux_bitrate )
872 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
873 if( p_input->p->counters.p_input_bitrate )
874 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
879 static int InitSout( input_thread_t * p_input )
881 if( p_input->b_preparsing )
884 /* Find a usable sout and attach it to p_input */
885 char *psz = var_GetNonEmptyString( p_input, "sout" );
886 if( psz && strncasecmp( p_input->p->p_item->psz_uri, "vlc:", 4 ) )
888 p_input->p->p_sout = input_resource_RequestSout( p_input->p->p_resource, NULL, psz );
889 if( !p_input->p->p_sout )
891 input_ChangeState( p_input, ERROR_S );
892 msg_Err( p_input, "cannot start stream output instance, " \
897 if( libvlc_stats( p_input ) )
899 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
900 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
901 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
902 if( p_input->p->counters.p_sout_send_bitrate )
903 p_input->p->counters.p_sout_send_bitrate->update_interval =
909 input_resource_RequestSout( p_input->p->p_resource, NULL, NULL );
917 static void InitTitle( input_thread_t * p_input )
919 input_source_t *p_master = &p_input->p->input;
921 if( p_input->b_preparsing )
924 /* Create global title (from master) */
925 p_input->p->i_title = p_master->i_title;
926 p_input->p->title = p_master->title;
927 p_input->p->i_title_offset = p_master->i_title_offset;
928 p_input->p->i_seekpoint_offset = p_master->i_seekpoint_offset;
929 if( p_input->p->i_title > 0 )
931 /* Setup variables */
932 input_ControlVarNavigation( p_input );
933 input_SendEventTitle( p_input, 0 );
937 p_input->p->b_can_pace_control = p_master->b_can_pace_control;
938 p_input->p->b_can_pause = p_master->b_can_pause;
939 p_input->p->b_can_rate_control = p_master->b_can_rate_control;
942 static void StartTitle( input_thread_t * p_input )
946 /* Start title/chapter */
947 val.i_int = p_input->p->input.i_title_start -
948 p_input->p->input.i_title_offset;
949 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
950 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
952 val.i_int = p_input->p->input.i_seekpoint_start -
953 p_input->p->input.i_seekpoint_offset;
954 if( val.i_int > 0 /* TODO: check upper boundary */ )
955 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
957 /* Start/stop/run time */
958 p_input->p->i_start = (int64_t)(1000000.0
959 * var_GetFloat( p_input, "start-time" ));
960 p_input->p->i_stop = (int64_t)(1000000.0
961 * var_GetFloat( p_input, "stop-time" ));
962 p_input->p->i_run = (int64_t)(1000000.0
963 * var_GetFloat( p_input, "run-time" ));
964 if( p_input->p->i_run < 0 )
966 msg_Warn( p_input, "invalid run-time ignored" );
967 p_input->p->i_run = 0;
970 if( p_input->p->i_start > 0 )
974 msg_Dbg( p_input, "starting at time: %ds",
975 (int)( p_input->p->i_start / INT64_C(1000000) ) );
977 s.i_time = p_input->p->i_start;
978 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
980 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
982 msg_Warn( p_input, "invalid stop-time ignored" );
983 p_input->p->i_stop = 0;
985 p_input->p->b_fast_seek = var_GetBool( p_input, "input-fast-seek" );
988 static void LoadSubtitles( input_thread_t *p_input )
991 /* Get fps and set it if not already set */
992 const double f_fps = p_input->p->f_fps;
995 float f_requested_fps;
997 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
998 var_SetFloat( p_input, "sub-original-fps", f_fps );
1000 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1001 if( f_requested_fps != f_fps )
1003 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1004 VLC_VAR_DOINHERIT );
1005 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1009 const int i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1011 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1013 /* Look for and add subtitle files */
1014 bool b_forced = true;
1016 char *psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1017 if( psz_subtitle != NULL )
1019 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1020 SubtitleAdd( p_input, psz_subtitle, b_forced );
1024 if( var_GetBool( p_input, "sub-autodetect-file" ) )
1026 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1027 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1028 p_input->p->p_item->psz_uri );
1029 free( psz_autopath );
1031 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1033 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1035 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1039 free( ppsz_subs[i] );
1043 free( psz_subtitle );
1045 /* Load subtitles from attachments */
1046 int i_attachment = 0;
1047 input_attachment_t **pp_attachment = NULL;
1049 vlc_mutex_lock( &p_input->p->p_item->lock );
1050 for( int i = 0; i < p_input->p->i_attachment; i++ )
1052 const input_attachment_t *a = p_input->p->attachment[i];
1053 if( !strcmp( a->psz_mime, "application/x-srt" ) )
1054 TAB_APPEND( i_attachment, pp_attachment,
1055 vlc_input_attachment_New( a->psz_name, NULL,
1056 a->psz_description, NULL, 0 ) );
1058 vlc_mutex_unlock( &p_input->p->p_item->lock );
1060 if( i_attachment > 0 )
1061 var_Create( p_input, "sub-description", VLC_VAR_STRING );
1062 for( int i = 0; i < i_attachment; i++ )
1064 input_attachment_t *a = pp_attachment[i];
1068 if( a->psz_name[i] &&
1069 asprintf( &psz_mrl, "attachment://%s", a->psz_name ) >= 0 )
1071 var_SetString( p_input, "sub-description", a->psz_description ? a->psz_description : "");
1073 SubtitleAdd( p_input, psz_mrl, b_forced );
1078 vlc_input_attachment_Delete( a );
1080 free( pp_attachment );
1081 if( i_attachment > 0 )
1082 var_Destroy( p_input, "sub-description" );
1085 static void LoadSlaves( input_thread_t *p_input )
1087 char *psz = var_GetNonEmptyString( p_input, "input-slave" );
1091 char *psz_org = psz;
1092 while( psz && *psz )
1094 while( *psz == ' ' || *psz == '#' )
1097 char *psz_delim = strchr( psz, '#' );
1099 *psz_delim++ = '\0';
1104 msg_Dbg( p_input, "adding slave input '%s'", psz );
1106 input_source_t *p_slave = InputSourceNew( p_input );
1107 if( p_slave && !InputSourceInit( p_input, p_slave, psz, NULL ) )
1108 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, p_slave );
1117 static void UpdatePtsDelay( input_thread_t *p_input )
1119 input_thread_private_t *p_sys = p_input->p;
1121 /* Get max pts delay from input source */
1122 mtime_t i_pts_delay = p_sys->input.i_pts_delay;
1123 for( int i = 0; i < p_sys->i_slave; i++ )
1124 i_pts_delay = __MAX( i_pts_delay, p_sys->slave[i]->i_pts_delay );
1126 if( i_pts_delay < 0 )
1129 /* Take care of audio/spu delay */
1130 const mtime_t i_audio_delay = var_GetTime( p_input, "audio-delay" );
1131 const mtime_t i_spu_delay = var_GetTime( p_input, "spu-delay" );
1132 const mtime_t i_extra_delay = __MIN( i_audio_delay, i_spu_delay );
1133 if( i_extra_delay < 0 )
1134 i_pts_delay -= i_extra_delay;
1136 /* Update cr_average depending on the caching */
1137 const int i_cr_average = var_GetInteger( p_input, "cr-average" ) * i_pts_delay / DEFAULT_PTS_DELAY;
1140 es_out_SetDelay( p_input->p->p_es_out_display, AUDIO_ES, i_audio_delay );
1141 es_out_SetDelay( p_input->p->p_es_out_display, SPU_ES, i_spu_delay );
1142 es_out_SetJitter( p_input->p->p_es_out, i_pts_delay, 0, i_cr_average );
1145 static void InitPrograms( input_thread_t * p_input )
1150 /* Compute correct pts_delay */
1151 UpdatePtsDelay( p_input );
1154 i_es_out_mode = ES_OUT_MODE_AUTO;
1155 if( p_input->p->p_sout )
1157 if( var_GetBool( p_input, "sout-all" ) )
1159 i_es_out_mode = ES_OUT_MODE_ALL;
1163 var_Get( p_input, "programs", &val );
1164 if( val.p_list && val.p_list->i_count )
1166 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1167 /* Note : we should remove the "program" callback. */
1171 var_FreeList( &val, NULL );
1175 es_out_SetMode( p_input->p->p_es_out, i_es_out_mode );
1177 /* Inform the demuxer about waited group (needed only for DVB) */
1178 if( i_es_out_mode == ES_OUT_MODE_ALL )
1180 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1182 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1184 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1189 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1190 (int) var_GetInteger( p_input, "program" ), NULL );
1194 static int Init( input_thread_t * p_input )
1199 for( i = 0; i < p_input->p->p_item->i_options; i++ )
1201 if( !strncmp( p_input->p->p_item->ppsz_options[i], "meta-file", 9 ) )
1203 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1204 var_SetString( p_input, "sout", "" );
1205 var_SetBool( p_input, "sout-all", false );
1206 var_SetString( p_input, "input-slave", "" );
1207 var_SetInteger( p_input, "input-repeat", 0 );
1208 var_SetString( p_input, "sub-file", "" );
1209 var_SetBool( p_input, "sub-autodetect-file", false );
1213 InitStatistics( p_input );
1215 if( InitSout( p_input ) )
1220 p_input->p->p_es_out = input_EsOutTimeshiftNew( p_input, p_input->p->p_es_out_display, p_input->p->i_rate );
1223 input_ChangeState( p_input, OPENING_S );
1224 input_SendEventCache( p_input, 0.0 );
1227 if( InputSourceInit( p_input, &p_input->p->input,
1228 p_input->p->p_item->psz_uri, NULL ) )
1233 InitTitle( p_input );
1235 /* Load master infos */
1238 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1242 i_length = input_item_GetDuration( p_input->p->p_item );
1243 input_SendEventLength( p_input, i_length );
1245 input_SendEventPosition( p_input, 0.0, 0 );
1247 if( !p_input->b_preparsing )
1249 StartTitle( p_input );
1250 LoadSubtitles( p_input );
1251 LoadSlaves( p_input );
1252 InitPrograms( p_input );
1255 if( !p_input->b_preparsing && p_input->p->p_sout )
1257 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1259 if( p_input->p->b_can_pace_control && p_input->p->b_out_pace_control )
1261 /* We don't want a high input priority here or we'll
1262 * end-up sucking up all the CPU time */
1263 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1266 msg_Dbg( p_input, "starting in %s mode",
1267 p_input->p->b_out_pace_control ? "async" : "sync" );
1270 p_meta = vlc_meta_New();
1273 /* Get meta data from users */
1274 InputMetaUser( p_input, p_meta );
1276 /* Get meta data from master input */
1277 InputSourceMeta( p_input, &p_input->p->input, p_meta );
1279 /* And from slave */
1280 for( int i = 0; i < p_input->p->i_slave; i++ )
1281 InputSourceMeta( p_input, p_input->p->slave[i], p_meta );
1284 InputUpdateMeta( p_input, p_meta );
1287 msg_Dbg( p_input, "`%s' successfully opened",
1288 p_input->p->p_item->psz_uri );
1290 /* initialization is complete */
1291 input_ChangeState( p_input, PLAYING_S );
1296 input_ChangeState( p_input, ERROR_S );
1298 if( p_input->p->p_es_out )
1299 es_out_Delete( p_input->p->p_es_out );
1300 es_out_SetMode( p_input->p->p_es_out_display, ES_OUT_MODE_END );
1301 if( p_input->p->p_resource )
1303 if( p_input->p->p_sout )
1304 input_resource_RequestSout( p_input->p->p_resource,
1305 p_input->p->p_sout, NULL );
1306 input_resource_SetInput( p_input->p->p_resource, NULL );
1309 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1311 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1312 stats_CounterClean( p_input->p->counters.p_##c );\
1313 p_input->p->counters.p_##c = NULL; } while(0)
1314 EXIT_COUNTER( read_bytes );
1315 EXIT_COUNTER( read_packets );
1316 EXIT_COUNTER( demux_read );
1317 EXIT_COUNTER( input_bitrate );
1318 EXIT_COUNTER( demux_bitrate );
1319 EXIT_COUNTER( demux_corrupted );
1320 EXIT_COUNTER( demux_discontinuity );
1321 EXIT_COUNTER( played_abuffers );
1322 EXIT_COUNTER( lost_abuffers );
1323 EXIT_COUNTER( displayed_pictures );
1324 EXIT_COUNTER( lost_pictures );
1325 EXIT_COUNTER( decoded_audio );
1326 EXIT_COUNTER( decoded_video );
1327 EXIT_COUNTER( decoded_sub );
1329 if( p_input->p->p_sout )
1331 EXIT_COUNTER( sout_sent_packets );
1332 EXIT_COUNTER( sout_sent_bytes );
1333 EXIT_COUNTER( sout_send_bitrate );
1338 /* Mark them deleted */
1339 p_input->p->input.p_demux = NULL;
1340 p_input->p->input.p_stream = NULL;
1341 p_input->p->input.p_access = NULL;
1342 p_input->p->p_es_out = NULL;
1343 p_input->p->p_sout = NULL;
1345 return VLC_EGENERIC;
1348 /*****************************************************************************
1349 * End: end the input thread
1350 *****************************************************************************/
1351 static void End( input_thread_t * p_input )
1355 /* We are at the end */
1356 input_ChangeState( p_input, END_S );
1358 /* Clean control variables */
1359 input_ControlVarStop( p_input );
1361 /* Stop es out activity */
1362 es_out_SetMode( p_input->p->p_es_out, ES_OUT_MODE_NONE );
1364 /* Clean up master */
1365 InputSourceClean( &p_input->p->input );
1368 for( i = 0; i < p_input->p->i_slave; i++ )
1370 InputSourceClean( p_input->p->slave[i] );
1371 free( p_input->p->slave[i] );
1373 free( p_input->p->slave );
1375 /* Unload all modules */
1376 if( p_input->p->p_es_out )
1377 es_out_Delete( p_input->p->p_es_out );
1378 es_out_SetMode( p_input->p->p_es_out_display, ES_OUT_MODE_END );
1380 if( !p_input->b_preparsing )
1382 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1383 if( libvlc_stats( p_input ) )
1385 /* make sure we are up to date */
1386 stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
1387 CL_CO( read_bytes );
1388 CL_CO( read_packets );
1389 CL_CO( demux_read );
1390 CL_CO( input_bitrate );
1391 CL_CO( demux_bitrate );
1392 CL_CO( demux_corrupted );
1393 CL_CO( demux_discontinuity );
1394 CL_CO( played_abuffers );
1395 CL_CO( lost_abuffers );
1396 CL_CO( displayed_pictures );
1397 CL_CO( lost_pictures );
1398 CL_CO( decoded_audio) ;
1399 CL_CO( decoded_video );
1400 CL_CO( decoded_sub) ;
1403 /* Close optional stream output instance */
1404 if( p_input->p->p_sout )
1406 CL_CO( sout_sent_packets );
1407 CL_CO( sout_sent_bytes );
1408 CL_CO( sout_send_bitrate );
1413 vlc_mutex_lock( &p_input->p->p_item->lock );
1414 if( p_input->p->i_attachment > 0 )
1416 for( i = 0; i < p_input->p->i_attachment; i++ )
1417 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1418 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1420 vlc_mutex_unlock( &p_input->p->p_item->lock );
1423 input_resource_RequestSout( p_input->p->p_resource,
1424 p_input->p->p_sout, NULL );
1425 input_resource_SetInput( p_input->p->p_resource, NULL );
1428 /*****************************************************************************
1430 *****************************************************************************/
1431 void input_ControlPush( input_thread_t *p_input,
1432 int i_type, vlc_value_t *p_val )
1434 vlc_mutex_lock( &p_input->p->lock_control );
1435 if( i_type == INPUT_CONTROL_SET_DIE )
1437 /* Special case, empty the control */
1438 for( int i = 0; i < p_input->p->i_control; i++ )
1440 input_control_t *p_ctrl = &p_input->p->control[i];
1441 ControlRelease( p_ctrl->i_type, p_ctrl->val );
1443 p_input->p->i_control = 0;
1446 if( p_input->p->i_control >= INPUT_CONTROL_FIFO_SIZE )
1448 msg_Err( p_input, "input control fifo overflow, trashing type=%d",
1451 ControlRelease( i_type, *p_val );
1460 memset( &c.val, 0, sizeof(c.val) );
1462 p_input->p->control[p_input->p->i_control++] = c;
1464 vlc_cond_signal( &p_input->p->wait_control );
1465 vlc_mutex_unlock( &p_input->p->lock_control );
1468 static int ControlGetReducedIndexLocked( input_thread_t *p_input )
1470 const int i_lt = p_input->p->control[0].i_type;
1472 for( i = 1; i < p_input->p->i_control; i++ )
1474 const int i_ct = p_input->p->control[i].i_type;
1477 ( i_ct == INPUT_CONTROL_SET_STATE ||
1478 i_ct == INPUT_CONTROL_SET_RATE ||
1479 i_ct == INPUT_CONTROL_SET_POSITION ||
1480 i_ct == INPUT_CONTROL_SET_TIME ||
1481 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1482 i_ct == INPUT_CONTROL_SET_TITLE ||
1483 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1484 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1490 /* TODO but that's not that important
1491 - merge SET_X with SET_X_CMD
1492 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1493 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1503 static inline int ControlPop( input_thread_t *p_input,
1504 int *pi_type, vlc_value_t *p_val,
1505 mtime_t i_deadline, bool b_postpone_seek )
1507 input_thread_private_t *p_sys = p_input->p;
1509 vlc_mutex_lock( &p_sys->lock_control );
1510 while( p_sys->i_control <= 0 ||
1511 ( b_postpone_seek && ControlIsSeekRequest( p_sys->control[0].i_type ) ) )
1513 if( !vlc_object_alive( p_input ) || i_deadline < 0 )
1515 vlc_mutex_unlock( &p_sys->lock_control );
1516 return VLC_EGENERIC;
1519 if( vlc_cond_timedwait( &p_sys->wait_control, &p_sys->lock_control,
1522 vlc_mutex_unlock( &p_sys->lock_control );
1523 return VLC_EGENERIC;
1528 const int i_index = ControlGetReducedIndexLocked( p_input );
1531 *pi_type = p_sys->control[i_index].i_type;
1532 *p_val = p_sys->control[i_index].val;
1534 p_sys->i_control -= i_index + 1;
1535 if( p_sys->i_control > 0 )
1536 memmove( &p_sys->control[0], &p_sys->control[i_index+1],
1537 sizeof(*p_sys->control) * p_sys->i_control );
1538 vlc_mutex_unlock( &p_sys->lock_control );
1542 static bool ControlIsSeekRequest( int i_type )
1546 case INPUT_CONTROL_SET_POSITION:
1547 case INPUT_CONTROL_SET_TIME:
1548 case INPUT_CONTROL_SET_TITLE:
1549 case INPUT_CONTROL_SET_TITLE_NEXT:
1550 case INPUT_CONTROL_SET_TITLE_PREV:
1551 case INPUT_CONTROL_SET_SEEKPOINT:
1552 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1553 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1554 case INPUT_CONTROL_SET_BOOKMARK:
1561 static void ControlRelease( int i_type, vlc_value_t val )
1565 case INPUT_CONTROL_ADD_SUBTITLE:
1566 case INPUT_CONTROL_ADD_SLAVE:
1567 free( val.psz_string );
1576 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1578 int i_ret = VLC_SUCCESS;
1579 int i_state = PAUSE_S;
1581 if( p_input->p->b_can_pause )
1583 if( p_input->p->input.p_access )
1584 i_ret = access_Control( p_input->p->input.p_access,
1585 ACCESS_SET_PAUSE_STATE, true );
1587 i_ret = demux_Control( p_input->p->input.p_demux,
1588 DEMUX_SET_PAUSE_STATE, true );
1592 msg_Warn( p_input, "cannot set pause state" );
1598 i_ret = es_out_SetPauseState( p_input->p->p_es_out,
1599 p_input->p->b_can_pause, true,
1603 msg_Warn( p_input, "cannot set pause state at es_out level" );
1607 /* Switch to new state */
1608 input_ChangeState( p_input, i_state );
1611 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1613 int i_ret = VLC_SUCCESS;
1615 if( p_input->p->b_can_pause )
1617 if( p_input->p->input.p_access )
1618 i_ret = access_Control( p_input->p->input.p_access,
1619 ACCESS_SET_PAUSE_STATE, false );
1621 i_ret = demux_Control( p_input->p->input.p_demux,
1622 DEMUX_SET_PAUSE_STATE, false );
1625 /* FIXME What to do ? */
1626 msg_Warn( p_input, "cannot unset pause -> EOF" );
1627 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1631 /* Switch to play */
1632 input_ChangeState( p_input, PLAYING_S );
1636 es_out_SetPauseState( p_input->p->p_es_out, false, false, i_control_date );
1639 static bool Control( input_thread_t *p_input,
1640 int i_type, vlc_value_t val )
1642 const mtime_t i_control_date = mdate();
1643 /* FIXME b_force_update is abused, it should be carefully checked */
1644 bool b_force_update = false;
1647 return b_force_update;
1651 case INPUT_CONTROL_SET_DIE:
1652 msg_Dbg( p_input, "control: stopping input" );
1654 /* Mark all submodules to die */
1655 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1658 case INPUT_CONTROL_SET_POSITION:
1662 if( p_input->p->b_recording )
1664 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1667 f_pos = val.f_float;
1668 if( i_type != INPUT_CONTROL_SET_POSITION )
1669 f_pos += var_GetFloat( p_input, "position" );
1672 else if( f_pos > 1.0 )
1674 /* Reset the decoders states and clock sync (before calling the demuxer */
1675 es_out_SetTime( p_input->p->p_es_out, -1 );
1676 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1677 f_pos, !p_input->p->b_fast_seek ) )
1679 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1680 "%2.1f%% failed", f_pos * 100 );
1684 if( p_input->p->i_slave > 0 )
1685 SlaveSeek( p_input );
1686 p_input->p->input.b_eof = false;
1688 b_force_update = true;
1693 case INPUT_CONTROL_SET_TIME:
1698 if( p_input->p->b_recording )
1700 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1704 i_time = val.i_time;
1705 if( i_type != INPUT_CONTROL_SET_TIME )
1706 i_time += var_GetTime( p_input, "time" );
1711 /* Reset the decoders states and clock sync (before calling the demuxer */
1712 es_out_SetTime( p_input->p->p_es_out, -1 );
1714 i_ret = demux_Control( p_input->p->input.p_demux,
1715 DEMUX_SET_TIME, i_time,
1716 !p_input->p->b_fast_seek );
1721 /* Emulate it with a SET_POS */
1722 if( !demux_Control( p_input->p->input.p_demux,
1723 DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
1725 double f_pos = (double)i_time / (double)i_length;
1726 i_ret = demux_Control( p_input->p->input.p_demux,
1727 DEMUX_SET_POSITION, f_pos,
1728 !p_input->p->b_fast_seek );
1733 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1734 " failed or not possible", i_time );
1738 if( p_input->p->i_slave > 0 )
1739 SlaveSeek( p_input );
1740 p_input->p->input.b_eof = false;
1742 b_force_update = true;
1747 case INPUT_CONTROL_SET_STATE:
1748 if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1749 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1750 else if( p_input->p->i_state == PAUSE_S )
1752 ControlUnpause( p_input, i_control_date );
1754 b_force_update = true;
1756 else if( val.i_int == PAUSE_S && p_input->p->i_state == PLAYING_S /* &&
1757 p_input->p->b_can_pause */ )
1759 ControlPause( p_input, i_control_date );
1761 b_force_update = true;
1763 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause && 0 )
1765 b_force_update = true;
1767 /* Correct "state" value */
1768 input_ChangeState( p_input, p_input->p->i_state );
1772 case INPUT_CONTROL_SET_RATE:
1774 /* Get rate and direction */
1775 int i_rate = abs( val.i_int );
1776 int i_rate_sign = val.i_int < 0 ? -1 : 1;
1778 /* Check rate bound */
1779 if( i_rate < INPUT_RATE_MIN )
1781 msg_Dbg( p_input, "cannot set rate faster" );
1782 i_rate = INPUT_RATE_MIN;
1784 else if( i_rate > INPUT_RATE_MAX )
1786 msg_Dbg( p_input, "cannot set rate slower" );
1787 i_rate = INPUT_RATE_MAX;
1790 /* Apply direction */
1791 if( i_rate_sign < 0 )
1793 if( p_input->p->input.b_rescale_ts )
1795 msg_Dbg( p_input, "cannot set negative rate" );
1796 i_rate = p_input->p->i_rate;
1797 assert( i_rate > 0 );
1801 i_rate *= i_rate_sign;
1805 if( i_rate != INPUT_RATE_DEFAULT &&
1806 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1807 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1809 msg_Dbg( p_input, "cannot change rate" );
1810 i_rate = INPUT_RATE_DEFAULT;
1812 if( i_rate != p_input->p->i_rate &&
1813 !p_input->p->b_can_pace_control && p_input->p->b_can_rate_control )
1816 if( p_input->p->input.p_access )
1818 i_ret = VLC_EGENERIC;
1822 if( !p_input->p->input.b_rescale_ts )
1823 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1825 i_ret = demux_Control( p_input->p->input.p_demux,
1826 DEMUX_SET_RATE, &i_rate );
1830 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1831 i_rate = p_input->p->i_rate;
1836 if( i_rate != p_input->p->i_rate )
1838 p_input->p->i_rate = i_rate;
1839 input_SendEventRate( p_input, i_rate );
1841 if( p_input->p->input.b_rescale_ts )
1843 const int i_rate_source = (p_input->p->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1844 es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1847 b_force_update = true;
1852 case INPUT_CONTROL_SET_PROGRAM:
1853 /* No need to force update, es_out does it if needed */
1854 es_out_Control( p_input->p->p_es_out,
1855 ES_OUT_SET_GROUP, val.i_int );
1857 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1861 case INPUT_CONTROL_SET_ES:
1862 /* No need to force update, es_out does it if needed */
1863 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, val.i_int );
1866 case INPUT_CONTROL_RESTART_ES:
1867 es_out_Control( p_input->p->p_es_out_display, ES_OUT_RESTART_ES_BY_ID, val.i_int );
1870 case INPUT_CONTROL_SET_AUDIO_DELAY:
1871 input_SendEventAudioDelay( p_input, val.i_time );
1872 UpdatePtsDelay( p_input );
1875 case INPUT_CONTROL_SET_SPU_DELAY:
1876 input_SendEventSubtitleDelay( p_input, val.i_time );
1877 UpdatePtsDelay( p_input );
1880 case INPUT_CONTROL_SET_TITLE:
1881 case INPUT_CONTROL_SET_TITLE_NEXT:
1882 case INPUT_CONTROL_SET_TITLE_PREV:
1883 if( p_input->p->b_recording )
1885 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1888 if( p_input->p->input.b_title_demux &&
1889 p_input->p->input.i_title > 0 )
1892 /* FIXME handle demux title */
1893 demux_t *p_demux = p_input->p->input.p_demux;
1896 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1897 i_title = p_demux->info.i_title - 1;
1898 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1899 i_title = p_demux->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 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1908 input_SendEventTitle( p_input, i_title );
1911 else if( p_input->p->input.i_title > 0 )
1913 access_t *p_access = p_input->p->input.p_access;
1916 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1917 i_title = p_access->info.i_title - 1;
1918 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1919 i_title = p_access->info.i_title + 1;
1921 i_title = val.i_int;
1923 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1925 es_out_SetTime( p_input->p->p_es_out, -1 );
1927 stream_Control( p_input->p->input.p_stream, STREAM_CONTROL_ACCESS,
1928 ACCESS_SET_TITLE, i_title );
1929 input_SendEventTitle( p_input, i_title );
1933 case INPUT_CONTROL_SET_SEEKPOINT:
1934 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1935 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1936 if( p_input->p->b_recording )
1938 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1942 if( p_input->p->input.b_title_demux &&
1943 p_input->p->input.i_title > 0 )
1945 demux_t *p_demux = p_input->p->input.p_demux;
1947 int64_t i_input_time;
1948 int64_t i_seekpoint_time;
1950 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1952 i_seekpoint = p_demux->info.i_seekpoint;
1953 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1954 i_input_time = var_GetTime( p_input, "time" );
1955 if( i_seekpoint_time >= 0 && i_input_time >= 0 )
1957 if( i_input_time < i_seekpoint_time + 3000000 )
1963 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1964 i_seekpoint = p_demux->info.i_seekpoint + 1;
1966 i_seekpoint = val.i_int;
1968 if( i_seekpoint >= 0 && i_seekpoint <
1969 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1972 es_out_SetTime( p_input->p->p_es_out, -1 );
1974 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1975 input_SendEventSeekpoint( p_input, p_demux->info.i_title, i_seekpoint );
1978 else if( p_input->p->input.i_title > 0 )
1980 access_t *p_access = p_input->p->input.p_access;
1982 int64_t i_input_time;
1983 int64_t i_seekpoint_time;
1985 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1987 i_seekpoint = p_access->info.i_seekpoint;
1988 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1989 i_input_time = var_GetTime( p_input, "time" );
1990 if( i_seekpoint_time >= 0 && i_input_time >= 0 )
1992 if( i_input_time < i_seekpoint_time + 3000000 )
1998 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1999 i_seekpoint = p_access->info.i_seekpoint + 1;
2001 i_seekpoint = val.i_int;
2003 if( i_seekpoint >= 0 && i_seekpoint <
2004 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
2006 es_out_SetTime( p_input->p->p_es_out, -1 );
2008 stream_Control( p_input->p->input.p_stream, STREAM_CONTROL_ACCESS,
2009 ACCESS_SET_SEEKPOINT, i_seekpoint );
2010 input_SendEventSeekpoint( p_input, p_access->info.i_title, i_seekpoint );
2015 case INPUT_CONTROL_ADD_SUBTITLE:
2016 if( val.psz_string )
2017 SubtitleAdd( p_input, val.psz_string, true );
2020 case INPUT_CONTROL_ADD_SLAVE:
2021 if( val.psz_string )
2023 input_source_t *slave = InputSourceNew( p_input );
2025 if( slave && !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
2031 msg_Dbg( p_input, "adding %s as slave on the fly",
2035 if( demux_Control( p_input->p->input.p_demux,
2036 DEMUX_GET_TIME, &i_time ) )
2038 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2039 InputSourceClean( slave );
2043 if( demux_Control( slave->p_demux,
2044 DEMUX_SET_TIME, i_time, true ) )
2046 msg_Err( p_input, "seek failed for new slave" );
2047 InputSourceClean( slave );
2052 /* Get meta (access and demux) */
2053 p_meta = vlc_meta_New();
2056 access_Control( slave->p_access, ACCESS_GET_META, p_meta );
2057 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
2058 InputUpdateMeta( p_input, p_meta );
2061 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2066 msg_Warn( p_input, "failed to add %s as slave",
2072 case INPUT_CONTROL_SET_RECORD_STATE:
2073 if( !!p_input->p->b_recording != !!val.b_bool )
2075 if( p_input->p->input.b_can_stream_record )
2077 if( demux_Control( p_input->p->input.p_demux,
2078 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2083 if( es_out_SetRecordState( p_input->p->p_es_out_display, val.b_bool ) )
2086 p_input->p->b_recording = val.b_bool;
2088 input_SendEventRecord( p_input, val.b_bool );
2090 b_force_update = true;
2094 case INPUT_CONTROL_SET_FRAME_NEXT:
2095 if( p_input->p->i_state == PAUSE_S )
2097 es_out_SetFrameNext( p_input->p->p_es_out );
2099 else if( p_input->p->i_state == PLAYING_S )
2101 ControlPause( p_input, i_control_date );
2105 msg_Err( p_input, "invalid state for frame next" );
2107 b_force_update = true;
2110 case INPUT_CONTROL_SET_BOOKMARK:
2112 seekpoint_t bookmark;
2114 bookmark.i_time_offset = -1;
2115 bookmark.i_byte_offset = -1;
2117 vlc_mutex_lock( &p_input->p->p_item->lock );
2118 if( val.i_int >= 0 && val.i_int < p_input->p->i_bookmark )
2120 const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[val.i_int];
2121 bookmark.i_time_offset = p_bookmark->i_time_offset;
2122 bookmark.i_byte_offset = p_bookmark->i_byte_offset;
2124 vlc_mutex_unlock( &p_input->p->p_item->lock );
2126 if( bookmark.i_time_offset < 0 && bookmark.i_byte_offset < 0 )
2128 msg_Err( p_input, "invalid bookmark %d", val.i_int );
2132 if( bookmark.i_time_offset >= 0 )
2134 val.i_time = bookmark.i_time_offset;
2135 b_force_update = Control( p_input, INPUT_CONTROL_SET_TIME, val );
2137 else if( bookmark.i_byte_offset >= 0 &&
2138 p_input->p->input.p_stream )
2140 const uint64_t i_size = stream_Size( p_input->p->input.p_stream );
2141 if( i_size > 0 && bookmark.i_byte_offset <= i_size )
2143 val.f_float = (double)bookmark.i_byte_offset / i_size;
2144 b_force_update = Control( p_input, INPUT_CONTROL_SET_POSITION, val );
2151 msg_Err( p_input, "not yet implemented" );
2155 ControlRelease( i_type, val );
2156 return b_force_update;
2159 /*****************************************************************************
2160 * UpdateTitleSeekpoint
2161 *****************************************************************************/
2162 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2163 int i_title, int i_seekpoint )
2165 int i_title_end = p_input->p->input.i_title_end -
2166 p_input->p->input.i_title_offset;
2167 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2168 p_input->p->input.i_seekpoint_offset;
2170 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2172 if( i_title > i_title_end ||
2173 ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2176 else if( i_seekpoint_end >= 0 )
2178 if( i_seekpoint > i_seekpoint_end )
2181 else if( i_title_end >= 0 )
2183 if( i_title > i_title_end )
2188 /*****************************************************************************
2190 *****************************************************************************/
2191 static int UpdateTitleSeekpointFromDemux( input_thread_t *p_input )
2193 demux_t *p_demux = p_input->p->input.p_demux;
2195 /* TODO event-like */
2196 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2198 input_SendEventTitle( p_input, p_demux->info.i_title );
2200 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2202 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2204 input_SendEventSeekpoint( p_input,
2205 p_demux->info.i_title, p_demux->info.i_seekpoint );
2207 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2210 /* Hmmm only works with master input */
2211 if( p_input->p->input.p_demux == p_demux )
2212 return UpdateTitleSeekpoint( p_input,
2213 p_demux->info.i_title,
2214 p_demux->info.i_seekpoint );
2218 static void UpdateGenericFromDemux( input_thread_t *p_input )
2220 demux_t *p_demux = p_input->p->input.p_demux;
2222 if( p_demux->info.i_update & INPUT_UPDATE_META )
2224 vlc_meta_t *p_meta = vlc_meta_New();
2227 demux_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
2228 InputUpdateMeta( p_input, p_meta );
2230 p_demux->info.i_update &= ~INPUT_UPDATE_META;
2233 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2237 /*****************************************************************************
2238 * Update*FromAccess:
2239 *****************************************************************************/
2240 static int UpdateTitleSeekpointFromAccess( input_thread_t *p_input )
2242 access_t *p_access = p_input->p->input.p_access;
2244 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2246 input_SendEventTitle( p_input, p_access->info.i_title );
2248 stream_Control( p_input->p->input.p_stream, STREAM_UPDATE_SIZE );
2250 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2252 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2254 input_SendEventSeekpoint( p_input,
2255 p_access->info.i_title, p_access->info.i_seekpoint );
2257 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2259 /* Hmmm only works with master input */
2260 if( p_input->p->input.p_access == p_access )
2261 return UpdateTitleSeekpoint( p_input,
2262 p_access->info.i_title,
2263 p_access->info.i_seekpoint );
2266 static void UpdateGenericFromAccess( input_thread_t *p_input )
2268 access_t *p_access = p_input->p->input.p_access;
2270 if( p_access->info.i_update & INPUT_UPDATE_META )
2272 /* TODO maybe multi - access ? */
2273 vlc_meta_t *p_meta = vlc_meta_New();
2276 access_Control( p_input->p->input.p_access, ACCESS_GET_META, p_meta );
2277 InputUpdateMeta( p_input, p_meta );
2279 p_access->info.i_update &= ~INPUT_UPDATE_META;
2281 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2286 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2287 f_quality = f_strength = -1;
2289 input_SendEventSignal( p_input, f_quality, f_strength );
2291 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2294 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2297 /*****************************************************************************
2299 *****************************************************************************/
2300 static input_source_t *InputSourceNew( input_thread_t *p_input )
2302 VLC_UNUSED(p_input);
2304 return calloc( 1, sizeof( input_source_t ) );
2307 /*****************************************************************************
2309 *****************************************************************************/
2310 static int InputSourceInit( input_thread_t *p_input,
2311 input_source_t *in, const char *psz_mrl,
2312 const char *psz_forced_demux )
2314 const char *psz_access;
2315 const char *psz_demux;
2317 char *psz_var_demux = NULL;
2321 char *psz_dup = strdup( psz_mrl );
2323 if( psz_dup == NULL )
2327 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2329 /* FIXME: file:// handling plugins do not support URIs properly...
2330 * So we pre-decode the URI to a path for them. Note that we do not do it
2331 * for non-standard VLC-specific schemes. */
2332 if( !strcmp( psz_access, "file" ) )
2334 if( psz_path[0] != '/'
2335 #if (DIR_SEP_CHAR != '/')
2336 /* We accept invalid URIs too. */
2337 && psz_path[0] != DIR_SEP_CHAR
2340 { /* host specified -> only localhost is supported */
2341 static const size_t i_localhost = sizeof("localhost")-1;
2342 if( strncmp( psz_path, "localhost/", i_localhost + 1) != 0 )
2344 msg_Err( p_input, "cannot open remote file `%s://%s'",
2345 psz_access, psz_path );
2346 msg_Info( p_input, "Did you mean `%s:///%s'?",
2347 psz_access, psz_path );
2350 psz_path += i_localhost;
2352 /* Then URI-decode the path. */
2353 decode_URI( psz_path );
2354 #if defined( WIN32 ) && !defined( UNDER_CE )
2355 /* Strip leading slash in front of the drive letter */
2358 #if (DIR_SEP_CHAR != '/')
2359 /* Turn slashes into anti-slashes */
2360 for( char *s = strchr( psz_path, '/' ); s; s = strchr( s + 1, '/' ) )
2365 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2366 psz_mrl, psz_access, psz_demux, psz_path );
2367 if( !p_input->b_preparsing )
2369 /* Hack to allow udp://@:port syntax */
2371 (strncmp( psz_access, "udp", 3 ) &&
2372 strncmp( psz_access, "rtp", 3 )) )
2374 /* Find optional titles and seekpoints */
2375 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2376 &in->i_seekpoint_start, &in->i_seekpoint_end );
2379 if( psz_forced_demux && *psz_forced_demux )
2381 psz_demux = psz_forced_demux;
2383 else if( *psz_demux == '\0' )
2385 /* special hack for forcing a demuxer with --demux=module
2386 * (and do nothing with a list) */
2387 psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2389 if( psz_var_demux != NULL &&
2390 !strchr(psz_var_demux, ',' ) &&
2391 !strchr(psz_var_demux, ':' ) )
2393 psz_demux = psz_var_demux;
2395 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2399 /* Try access_demux first */
2400 in->p_demux = demux_New( p_input, p_input, psz_access, psz_demux, psz_path,
2401 NULL, p_input->p->p_es_out, false );
2405 /* Preparsing is only for file:// */
2408 if( !*psz_access ) /* path without scheme:// */
2409 psz_access = "file";
2410 if( strcmp( psz_access, "file" ) )
2412 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2417 /* Get infos from access_demux */
2418 int i_ret = demux_Control( in->p_demux,
2419 DEMUX_GET_PTS_DELAY, &in->i_pts_delay );
2421 in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2424 in->b_title_demux = true;
2425 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2426 &in->title, &in->i_title,
2427 &in->i_title_offset, &in->i_seekpoint_offset ) )
2429 TAB_INIT( in->i_title, in->title );
2431 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2432 &in->b_can_pace_control ) )
2433 in->b_can_pace_control = false;
2435 assert( in->p_demux->pf_demux != NULL || !in->b_can_pace_control );
2437 if( !in->b_can_pace_control )
2439 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2440 &in->b_can_rate_control, &in->b_rescale_ts ) )
2442 in->b_can_rate_control = false;
2443 in->b_rescale_ts = true; /* not used */
2448 in->b_can_rate_control = true;
2449 in->b_rescale_ts = true;
2451 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2452 &in->b_can_pause ) )
2453 in->b_can_pause = false;
2454 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2455 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2456 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control && in->b_can_rate_control );
2459 if( demux_Control( in->p_demux, DEMUX_CAN_SEEK, &b_can_seek ) )
2461 var_SetBool( p_input, "can-seek", b_can_seek );
2465 /* Now try a real access */
2466 in->p_access = access_New( p_input, p_input, psz_access, psz_demux, psz_path );
2467 if( in->p_access == NULL )
2469 if( vlc_object_alive( p_input ) )
2471 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2473 dialog_Fatal( p_input, _("Your input can't be opened"),
2474 _("VLC is unable to open the MRL '%s'."
2475 " Check the log for details."), psz_mrl );
2480 /* Get infos from access */
2481 if( !p_input->b_preparsing )
2484 access_Control( in->p_access,
2485 ACCESS_GET_PTS_DELAY, &in->i_pts_delay );
2486 in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2488 in->b_title_demux = false;
2489 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2490 &in->title, &in->i_title,
2491 &in->i_title_offset, &in->i_seekpoint_offset ) )
2494 TAB_INIT( in->i_title, in->title );
2496 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2497 &in->b_can_pace_control );
2498 in->b_can_rate_control = in->b_can_pace_control;
2499 in->b_rescale_ts = true;
2501 access_Control( in->p_access, ACCESS_CAN_PAUSE, &in->b_can_pause );
2502 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2503 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2504 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2506 access_Control( in->p_access, ACCESS_CAN_SEEK, &b_can_seek );
2507 var_SetBool( p_input, "can-seek", b_can_seek );
2512 char **ppsz_input_list;
2514 TAB_INIT( i_input_list, ppsz_input_list );
2516 /* On master stream only, use input-list */
2517 if( &p_input->p->input == in )
2523 psz_parser = var_CreateGetNonEmptyString( p_input, "input-list" );
2525 while( psz_parser && *psz_parser )
2527 char *p = strchr( psz_parser, ',' );
2533 char *psz_name = strdup( psz_parser );
2535 TAB_APPEND( i_input_list, ppsz_input_list, psz_name );
2542 /* Autodetect extra files if none specified */
2543 if( i_input_list <= 0 )
2545 InputGetExtraFiles( p_input, &i_input_list, &ppsz_input_list,
2546 psz_access, psz_path );
2548 if( i_input_list > 0 )
2549 TAB_APPEND( i_input_list, ppsz_input_list, NULL );
2551 /* Create the stream_t */
2552 in->p_stream = stream_AccessNew( in->p_access, ppsz_input_list );
2553 if( ppsz_input_list )
2555 for( int i = 0; ppsz_input_list[i] != NULL; i++ )
2556 free( ppsz_input_list[i] );
2557 TAB_CLEAN( i_input_list, ppsz_input_list );
2560 if( in->p_stream == NULL )
2562 msg_Warn( p_input, "cannot create a stream_t from access" );
2566 /* Add stream filters */
2567 char *psz_stream_filter = var_GetNonEmptyString( p_input,
2569 in->p_stream = stream_FilterChainNew( in->p_stream,
2571 var_GetBool( p_input, "input-record-native" ) );
2572 free( psz_stream_filter );
2574 /* Open a demuxer */
2575 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2577 psz_demux = in->p_access->psz_demux;
2580 in->p_demux = demux_New( p_input, p_input, psz_access, psz_demux,
2581 /* Take access/stream redirections into account: */
2582 in->p_stream->psz_path ? in->p_stream->psz_path : psz_path,
2583 in->p_stream, p_input->p->p_es_out,
2584 p_input->b_preparsing );
2586 if( in->p_demux == NULL )
2588 if( vlc_object_alive( p_input ) )
2590 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2591 psz_access, psz_demux, psz_path );
2592 dialog_Fatal( VLC_OBJECT( p_input ),
2593 _("VLC can't recognize the input's format"),
2594 _("The format of '%s' cannot be detected. "
2595 "Have a look at the log for details."), psz_mrl );
2599 assert( in->p_demux->pf_demux != NULL );
2601 /* Get title from demux */
2602 if( !p_input->b_preparsing && in->i_title <= 0 )
2604 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2605 &in->title, &in->i_title,
2606 &in->i_title_offset, &in->i_seekpoint_offset ))
2608 TAB_INIT( in->i_title, in->title );
2612 in->b_title_demux = true;
2617 free( psz_var_demux );
2620 /* Set record capabilities */
2621 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2622 in->b_can_stream_record = false;
2624 if( !var_GetBool( p_input, "input-record-native" ) )
2625 in->b_can_stream_record = false;
2626 var_SetBool( p_input, "can-record", true );
2628 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2632 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2633 if( 1 || !p_input->b_preparsing )
2636 input_attachment_t **attachment;
2637 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2638 &attachment, &i_attachment ) )
2640 vlc_mutex_lock( &p_input->p->p_item->lock );
2641 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2642 i_attachment, attachment );
2643 vlc_mutex_unlock( &p_input->p->p_item->lock );
2646 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) && f_fps > 0.0 )
2648 vlc_mutex_lock( &p_input->p->p_item->lock );
2649 p_input->p->f_fps = f_fps;
2650 vlc_mutex_unlock( &p_input->p->p_item->lock );
2653 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2654 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2660 demux_Delete( in->p_demux );
2663 stream_Delete( in->p_stream );
2666 access_Delete( in->p_access );
2668 free( psz_var_demux );
2671 return VLC_EGENERIC;
2674 /*****************************************************************************
2676 *****************************************************************************/
2677 static void InputSourceClean( input_source_t *in )
2682 demux_Delete( in->p_demux );
2685 stream_Delete( in->p_stream );
2688 access_Delete( in->p_access );
2690 if( in->i_title > 0 )
2692 for( i = 0; i < in->i_title; i++ )
2693 vlc_input_title_Delete( in->title[i] );
2694 TAB_CLEAN( in->i_title, in->title );
2698 /*****************************************************************************
2700 *****************************************************************************/
2701 static void InputSourceMeta( input_thread_t *p_input,
2702 input_source_t *p_source, vlc_meta_t *p_meta )
2704 access_t *p_access = p_source->p_access;
2705 demux_t *p_demux = p_source->p_demux;
2707 /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
2712 /* Read access meta */
2713 has_meta = p_access && !access_Control( p_access, ACCESS_GET_META, p_meta );
2715 /* Read demux meta */
2716 has_meta |= !demux_Control( p_demux, DEMUX_GET_META, p_meta );
2718 bool has_unsupported;
2719 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &has_unsupported ) )
2720 has_unsupported = true;
2722 /* If the demux report unsupported meta data, or if we don't have meta data
2723 * try an external "meta reader" */
2724 if( has_meta && !has_unsupported )
2727 demux_meta_t *p_demux_meta =
2728 vlc_custom_create( p_demux, sizeof( *p_demux_meta ),
2729 VLC_OBJECT_GENERIC, "demux meta" );
2732 vlc_object_attach( p_demux_meta, p_demux );
2733 p_demux_meta->p_demux = p_demux;
2734 p_demux_meta->p_item = p_input->p->p_item;
2736 module_t *p_id3 = module_need( p_demux_meta, "meta reader", NULL, false );
2739 if( p_demux_meta->p_meta )
2741 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2742 vlc_meta_Delete( p_demux_meta->p_meta );
2745 if( p_demux_meta->i_attachments > 0 )
2747 vlc_mutex_lock( &p_input->p->p_item->lock );
2748 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2749 p_demux_meta->i_attachments, p_demux_meta->attachments );
2750 vlc_mutex_unlock( &p_input->p->p_item->lock );
2752 module_unneed( p_demux, p_id3 );
2754 vlc_object_release( p_demux_meta );
2758 static void SlaveDemux( input_thread_t *p_input, bool *pb_demux_polled )
2763 *pb_demux_polled = false;
2764 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2766 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2770 for( i = 0; i < p_input->p->i_slave; i++ )
2772 input_source_t *in = p_input->p->slave[i];
2778 const bool b_demux_polled = in->p_demux->pf_demux != NULL;
2779 if( !b_demux_polled )
2782 *pb_demux_polled = true;
2784 /* Call demux_Demux until we have read enough data */
2785 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2790 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2792 msg_Err( p_input, "slave[%d] doesn't like "
2793 "DEMUX_GET_TIME -> EOF", i );
2798 if( i_stime >= i_time )
2804 if( ( i_ret = demux_Demux( in->p_demux ) ) <= 0 )
2810 i_ret = demux_Demux( in->p_demux );
2815 msg_Dbg( p_input, "slave %d EOF", i );
2821 static void SlaveSeek( input_thread_t *p_input )
2826 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2828 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2832 for( i = 0; i < p_input->p->i_slave; i++ )
2834 input_source_t *in = p_input->p->slave[i];
2836 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time, true ) )
2839 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2849 /*****************************************************************************
2851 *****************************************************************************/
2852 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2854 static const struct { int i_meta; const char *psz_name; } p_list[] = {
2855 { vlc_meta_Title, "meta-title" },
2856 { vlc_meta_Artist, "meta-artist" },
2857 { vlc_meta_Genre, "meta-genre" },
2858 { vlc_meta_Copyright, "meta-copyright" },
2859 { vlc_meta_Description, "meta-description" },
2860 { vlc_meta_Date, "meta-date" },
2861 { vlc_meta_URL, "meta-url" },
2865 /* Get meta information from user */
2866 for( int i = 0; p_list[i].psz_name; i++ )
2868 char *psz_string = var_GetNonEmptyString( p_input, p_list[i].psz_name );
2872 EnsureUTF8( psz_string );
2873 vlc_meta_Set( p_meta, p_list[i].i_meta, psz_string );
2878 /*****************************************************************************
2879 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2880 * arturl and locking issue.
2881 *****************************************************************************/
2882 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2884 es_out_ControlSetMeta( p_input->p->p_es_out, p_meta );
2885 vlc_meta_Delete( p_meta );
2888 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2889 int i_new, input_attachment_t **pp_new )
2891 int i_attachment = *pi_attachment;
2892 input_attachment_t **attachment = *ppp_attachment;
2895 attachment = xrealloc( attachment,
2896 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2897 for( i = 0; i < i_new; i++ )
2898 attachment[i_attachment++] = pp_new[i];
2902 *pi_attachment = i_attachment;
2903 *ppp_attachment = attachment;
2905 /*****************************************************************************
2906 * InputGetExtraFiles
2907 * Autodetect extra input list
2908 *****************************************************************************/
2909 static void InputGetExtraFilesPattern( input_thread_t *p_input,
2910 int *pi_list, char ***pppsz_list,
2911 const char *psz_path,
2912 const char *psz_match,
2913 const char *psz_format,
2914 int i_start, int i_stop )
2919 TAB_INIT( i_list, ppsz_list );
2921 char *psz_base = strdup( psz_path );
2925 /* Remove the extension */
2926 char *psz_end = &psz_base[strlen(psz_base)-strlen(psz_match)];
2927 assert( psz_end >= psz_base);
2930 /* Try to list files */
2931 for( int i = i_start; i <= i_stop; i++ )
2936 if( asprintf( &psz_file, psz_format, psz_base, i ) < 0 )
2939 if( vlc_stat( psz_file, &st ) || !S_ISREG( st.st_mode ) || !st.st_size )
2945 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2946 TAB_APPEND( i_list, ppsz_list, psz_file );
2951 *pppsz_list = ppsz_list;
2954 static void InputGetExtraFiles( input_thread_t *p_input,
2955 int *pi_list, char ***pppsz_list,
2956 const char *psz_access, const char *psz_path )
2960 const char *psz_match;
2961 const char *psz_format;
2965 /* XXX the order is important */
2966 { ".001", "%s.%.3d", 2, 999 },
2967 { ".part1.rar", "%s.part%.1d.rar",2, 9 },
2968 { ".part01.rar", "%s.part%.2d.rar",2, 99, },
2969 { ".part001.rar", "%s.part%.3d.rar",2, 999 },
2970 { ".rar", "%s.r%.2d", 0, 99 },
2971 { NULL, NULL, 0, 0 }
2974 TAB_INIT( *pi_list, *pppsz_list );
2976 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2979 const size_t i_path = strlen(psz_path);
2981 for( int i = 0; p_pattern[i].psz_match != NULL; i++ )
2983 const size_t i_ext = strlen(p_pattern[i].psz_match );
2985 if( i_path < i_ext )
2987 if( !strcmp( &psz_path[i_path-i_ext], p_pattern[i].psz_match ) )
2989 InputGetExtraFilesPattern( p_input, pi_list, pppsz_list,
2991 p_pattern[i].psz_match, p_pattern[i].psz_format,
2992 p_pattern[i].i_start, p_pattern[i].i_stop );
2999 static void input_ChangeState( input_thread_t *p_input, int i_state )
3001 const bool b_changed = p_input->p->i_state != i_state;
3003 p_input->p->i_state = i_state;
3004 if( i_state == ERROR_S )
3005 p_input->b_error = true;
3006 else if( i_state == END_S )
3007 p_input->b_eof = true;
3011 input_item_SetErrorWhenReading( p_input->p->p_item, p_input->b_error );
3012 input_SendEventState( p_input, i_state );
3017 /*****************************************************************************
3018 * MRLSplit: parse the access, demux and url part of the
3019 * Media Resource Locator.
3020 *****************************************************************************/
3021 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux,
3022 char **ppsz_path, char *psz_dup )
3024 const char *psz_access;
3025 const char *psz_demux = "";
3028 /* Either there is an access/demux specification before ://
3029 * or we have a plain local file path. */
3030 psz_path = strstr( psz_dup, "://" );
3031 if( psz_path != NULL )
3034 psz_path += 3; /* skips "://" */
3036 psz_access = psz_dup;
3037 /* We really don't want module name substitution here! */
3038 if( psz_access[0] == '$' )
3041 /* Separate access from demux (<access>/<demux>://<path>) */
3042 char *p = strchr( psz_access, '/' );
3047 if( psz_demux[0] == '$' )
3051 /* Remove HTML anchor if present (not supported).
3052 * The hash symbol itself should be URI-encoded. */
3053 p = strchr( psz_path, '#' );
3060 fprintf( stderr, "%s(\"%s\"): not a valid URI!\n", __func__,
3067 *ppsz_access = psz_access;
3068 *ppsz_demux = psz_demux;
3069 *ppsz_path = psz_path;
3072 static inline bool next(char ** src)
3076 long result = strtol( *src, &end, 0 );
3077 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
3086 /*****************************************************************************
3087 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3090 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
3091 *****************************************************************************/
3092 static void MRLSections( input_thread_t *p_input, char *psz_source,
3093 int *pi_title_start, int *pi_title_end,
3094 int *pi_chapter_start, int *pi_chapter_end )
3096 char *psz, *psz_end, *psz_next, *psz_check;
3098 *pi_title_start = *pi_title_end = -1;
3099 *pi_chapter_start = *pi_chapter_end = -1;
3101 /* Start by parsing titles and chapters */
3102 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
3105 /* Check we are really dealing with a title/chapter section */
3106 psz_check = psz + 1;
3107 if( !*psz_check ) return;
3108 if( isdigit(*psz_check) )
3109 if(!next(&psz_check)) return;
3110 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
3111 if( *psz_check == ':' && ++psz_check )
3113 if( isdigit(*psz_check) )
3114 if(!next(&psz_check)) return;
3116 if( *psz_check != '-' && *psz_check ) return;
3117 if( *psz_check == '-' && ++psz_check )
3119 if( isdigit(*psz_check) )
3120 if(!next(&psz_check)) return;
3122 if( *psz_check != ':' && *psz_check ) return;
3123 if( *psz_check == ':' && ++psz_check )
3125 if( isdigit(*psz_check) )
3126 if(!next(&psz_check)) return;
3128 if( *psz_check ) return;
3130 /* Separate start and end */
3132 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
3134 /* Look for the start title */
3135 *pi_title_start = strtol( psz, &psz_next, 0 );
3136 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
3137 *pi_title_end = *pi_title_start;
3140 /* Look for the start chapter */
3142 *pi_chapter_start = strtol( psz, &psz_next, 0 );
3143 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
3144 *pi_chapter_end = *pi_chapter_start;
3148 /* Look for the end title */
3149 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3150 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3153 /* Look for the end chapter */
3154 if( *psz_end ) psz_end++;
3155 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3156 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3159 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3160 psz_source, *pi_title_start, *pi_chapter_start,
3161 *pi_title_end, *pi_chapter_end );
3164 /*****************************************************************************
3165 * input_AddSubtitles: add a subtitles file and enable it
3166 *****************************************************************************/
3167 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3169 input_source_t *sub;
3172 char *psz_path, *psz_extension;
3174 /* if we are provided a subtitle.sub file,
3175 * see if we don't have a subtitle.idx and use it instead */
3176 psz_path = strdup( psz_subtitle );
3179 psz_extension = strrchr( psz_path, '.');
3180 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3184 strcpy( psz_extension, ".idx" );
3186 if( !vlc_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3188 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3189 psz_path, psz_subtitle );
3190 strcpy( psz_subtitle, psz_path );
3196 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3198 sub = InputSourceNew( p_input );
3199 if( !sub || InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3204 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3207 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3209 if( count.i_int == 0 )
3211 /* if it was first one, there is disable too */
3213 if( count.i_int < list.p_list->i_count )
3215 const int i_id = list.p_list->p_values[count.i_int].i_int;
3217 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3218 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3220 var_FreeList( &list, NULL );
3224 /*****************************************************************************
3226 *****************************************************************************/
3227 void input_UpdateStatistic( input_thread_t *p_input,
3228 input_statistic_t i_type, int i_delta )
3230 assert( p_input->p->i_state != INIT_S );
3232 vlc_mutex_lock( &p_input->p->counters.counters_lock);
3235 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
3236 case INPUT_STATISTIC_DECODED_VIDEO:
3239 case INPUT_STATISTIC_DECODED_AUDIO:
3242 case INPUT_STATISTIC_DECODED_SUBTITLE:
3245 case INPUT_STATISTIC_SENT_PACKET:
3246 I(p_sout_sent_packets);
3249 case INPUT_STATISTIC_SENT_BYTE:
3251 int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
3253 if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
3254 stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
3258 msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3261 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
3265 /* TODO FIXME nearly the same logic that snapshot code */
3266 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3271 path = vlc_opendir( psz_path );
3276 char *psz_tmp = str_format( p_obj, psz_prefix );
3280 filename_sanitize( psz_tmp );
3282 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3284 psz_extension ? "." : "",
3285 psz_extension ? psz_extension : "" ) < 0 )
3292 psz_file = str_format( p_obj, psz_path );
3293 path_sanitize( psz_file );