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:
1773 case INPUT_CONTROL_SET_RATE_SLOWER:
1774 case INPUT_CONTROL_SET_RATE_FASTER:
1779 /* Get rate and direction */
1780 if( i_type == INPUT_CONTROL_SET_RATE )
1782 i_rate = abs( val.i_int );
1783 i_rate_sign = val.i_int < 0 ? -1 : 1;
1787 static const int ppi_factor[][2] = {
1788 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1790 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1797 i_rate_sign = p_input->p->i_rate < 0 ? -1 : 1;
1801 for( i = 0; ppi_factor[i][0] != 0; i++ )
1803 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1804 const int i_test_e = abs( abs( p_input->p->i_rate ) - i_test_r );
1805 if( i_test_e < i_error )
1812 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1814 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1816 if( ppi_factor[i_idx+1][0] > 0 )
1817 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1819 i_rate = INPUT_RATE_MAX+1;
1823 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1825 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1827 i_rate = INPUT_RATE_MIN-1;
1831 /* Check rate bound */
1832 if( i_rate < INPUT_RATE_MIN )
1834 msg_Dbg( p_input, "cannot set rate faster" );
1835 i_rate = INPUT_RATE_MIN;
1837 else if( i_rate > INPUT_RATE_MAX )
1839 msg_Dbg( p_input, "cannot set rate slower" );
1840 i_rate = INPUT_RATE_MAX;
1843 /* Apply direction */
1844 if( i_rate_sign < 0 )
1846 if( p_input->p->input.b_rescale_ts )
1848 msg_Dbg( p_input, "cannot set negative rate" );
1849 i_rate = p_input->p->i_rate;
1850 assert( i_rate > 0 );
1854 i_rate *= i_rate_sign;
1858 if( i_rate != INPUT_RATE_DEFAULT &&
1859 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1860 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1862 msg_Dbg( p_input, "cannot change rate" );
1863 i_rate = INPUT_RATE_DEFAULT;
1865 if( i_rate != p_input->p->i_rate &&
1866 !p_input->p->b_can_pace_control && p_input->p->b_can_rate_control )
1869 if( p_input->p->input.p_access )
1871 i_ret = VLC_EGENERIC;
1875 if( !p_input->p->input.b_rescale_ts )
1876 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1878 i_ret = demux_Control( p_input->p->input.p_demux,
1879 DEMUX_SET_RATE, &i_rate );
1883 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1884 i_rate = p_input->p->i_rate;
1889 if( i_rate != p_input->p->i_rate )
1891 p_input->p->i_rate = i_rate;
1892 input_SendEventRate( p_input, i_rate );
1894 if( p_input->p->input.b_rescale_ts )
1896 const int i_rate_source = (p_input->p->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1897 es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1900 b_force_update = true;
1905 case INPUT_CONTROL_SET_PROGRAM:
1906 /* No need to force update, es_out does it if needed */
1907 es_out_Control( p_input->p->p_es_out,
1908 ES_OUT_SET_GROUP, val.i_int );
1910 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1914 case INPUT_CONTROL_SET_ES:
1915 /* No need to force update, es_out does it if needed */
1916 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, val.i_int );
1919 case INPUT_CONTROL_RESTART_ES:
1920 es_out_Control( p_input->p->p_es_out_display, ES_OUT_RESTART_ES_BY_ID, val.i_int );
1923 case INPUT_CONTROL_SET_AUDIO_DELAY:
1924 input_SendEventAudioDelay( p_input, val.i_time );
1925 UpdatePtsDelay( p_input );
1928 case INPUT_CONTROL_SET_SPU_DELAY:
1929 input_SendEventSubtitleDelay( p_input, val.i_time );
1930 UpdatePtsDelay( p_input );
1933 case INPUT_CONTROL_SET_TITLE:
1934 case INPUT_CONTROL_SET_TITLE_NEXT:
1935 case INPUT_CONTROL_SET_TITLE_PREV:
1936 if( p_input->p->b_recording )
1938 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1941 if( p_input->p->input.b_title_demux &&
1942 p_input->p->input.i_title > 0 )
1945 /* FIXME handle demux title */
1946 demux_t *p_demux = p_input->p->input.p_demux;
1949 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1950 i_title = p_demux->info.i_title - 1;
1951 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1952 i_title = p_demux->info.i_title + 1;
1954 i_title = val.i_int;
1956 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1958 es_out_SetTime( p_input->p->p_es_out, -1 );
1960 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1961 input_SendEventTitle( p_input, i_title );
1964 else if( p_input->p->input.i_title > 0 )
1966 access_t *p_access = p_input->p->input.p_access;
1969 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1970 i_title = p_access->info.i_title - 1;
1971 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1972 i_title = p_access->info.i_title + 1;
1974 i_title = val.i_int;
1976 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1978 es_out_SetTime( p_input->p->p_es_out, -1 );
1980 stream_Control( p_input->p->input.p_stream, STREAM_CONTROL_ACCESS,
1981 ACCESS_SET_TITLE, i_title );
1982 input_SendEventTitle( p_input, i_title );
1986 case INPUT_CONTROL_SET_SEEKPOINT:
1987 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1988 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1989 if( p_input->p->b_recording )
1991 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1995 if( p_input->p->input.b_title_demux &&
1996 p_input->p->input.i_title > 0 )
1998 demux_t *p_demux = p_input->p->input.p_demux;
2000 int64_t i_input_time;
2001 int64_t i_seekpoint_time;
2003 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
2005 i_seekpoint = p_demux->info.i_seekpoint;
2006 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
2007 i_input_time = var_GetTime( p_input, "time" );
2008 if( i_seekpoint_time >= 0 && i_input_time >= 0 )
2010 if( i_input_time < i_seekpoint_time + 3000000 )
2016 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
2017 i_seekpoint = p_demux->info.i_seekpoint + 1;
2019 i_seekpoint = val.i_int;
2021 if( i_seekpoint >= 0 && i_seekpoint <
2022 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
2025 es_out_SetTime( p_input->p->p_es_out, -1 );
2027 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
2028 input_SendEventSeekpoint( p_input, p_demux->info.i_title, i_seekpoint );
2031 else if( p_input->p->input.i_title > 0 )
2033 access_t *p_access = p_input->p->input.p_access;
2035 int64_t i_input_time;
2036 int64_t i_seekpoint_time;
2038 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
2040 i_seekpoint = p_access->info.i_seekpoint;
2041 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
2042 i_input_time = var_GetTime( p_input, "time" );
2043 if( i_seekpoint_time >= 0 && i_input_time >= 0 )
2045 if( i_input_time < i_seekpoint_time + 3000000 )
2051 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
2052 i_seekpoint = p_access->info.i_seekpoint + 1;
2054 i_seekpoint = val.i_int;
2056 if( i_seekpoint >= 0 && i_seekpoint <
2057 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
2059 es_out_SetTime( p_input->p->p_es_out, -1 );
2061 stream_Control( p_input->p->input.p_stream, STREAM_CONTROL_ACCESS,
2062 ACCESS_SET_SEEKPOINT, i_seekpoint );
2063 input_SendEventSeekpoint( p_input, p_access->info.i_title, i_seekpoint );
2068 case INPUT_CONTROL_ADD_SUBTITLE:
2069 if( val.psz_string )
2070 SubtitleAdd( p_input, val.psz_string, true );
2073 case INPUT_CONTROL_ADD_SLAVE:
2074 if( val.psz_string )
2076 input_source_t *slave = InputSourceNew( p_input );
2078 if( slave && !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
2084 msg_Dbg( p_input, "adding %s as slave on the fly",
2088 if( demux_Control( p_input->p->input.p_demux,
2089 DEMUX_GET_TIME, &i_time ) )
2091 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2092 InputSourceClean( slave );
2096 if( demux_Control( slave->p_demux,
2097 DEMUX_SET_TIME, i_time, true ) )
2099 msg_Err( p_input, "seek failed for new slave" );
2100 InputSourceClean( slave );
2105 /* Get meta (access and demux) */
2106 p_meta = vlc_meta_New();
2109 access_Control( slave->p_access, ACCESS_GET_META, p_meta );
2110 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
2111 InputUpdateMeta( p_input, p_meta );
2114 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2119 msg_Warn( p_input, "failed to add %s as slave",
2125 case INPUT_CONTROL_SET_RECORD_STATE:
2126 if( !!p_input->p->b_recording != !!val.b_bool )
2128 if( p_input->p->input.b_can_stream_record )
2130 if( demux_Control( p_input->p->input.p_demux,
2131 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2136 if( es_out_SetRecordState( p_input->p->p_es_out_display, val.b_bool ) )
2139 p_input->p->b_recording = val.b_bool;
2141 input_SendEventRecord( p_input, val.b_bool );
2143 b_force_update = true;
2147 case INPUT_CONTROL_SET_FRAME_NEXT:
2148 if( p_input->p->i_state == PAUSE_S )
2150 es_out_SetFrameNext( p_input->p->p_es_out );
2152 else if( p_input->p->i_state == PLAYING_S )
2154 ControlPause( p_input, i_control_date );
2158 msg_Err( p_input, "invalid state for frame next" );
2160 b_force_update = true;
2163 case INPUT_CONTROL_SET_BOOKMARK:
2165 seekpoint_t bookmark;
2167 bookmark.i_time_offset = -1;
2168 bookmark.i_byte_offset = -1;
2170 vlc_mutex_lock( &p_input->p->p_item->lock );
2171 if( val.i_int >= 0 && val.i_int < p_input->p->i_bookmark )
2173 const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[val.i_int];
2174 bookmark.i_time_offset = p_bookmark->i_time_offset;
2175 bookmark.i_byte_offset = p_bookmark->i_byte_offset;
2177 vlc_mutex_unlock( &p_input->p->p_item->lock );
2179 if( bookmark.i_time_offset < 0 && bookmark.i_byte_offset < 0 )
2181 msg_Err( p_input, "invalid bookmark %d", val.i_int );
2185 if( bookmark.i_time_offset >= 0 )
2187 val.i_time = bookmark.i_time_offset;
2188 b_force_update = Control( p_input, INPUT_CONTROL_SET_TIME, val );
2190 else if( bookmark.i_byte_offset >= 0 &&
2191 p_input->p->input.p_stream )
2193 const uint64_t i_size = stream_Size( p_input->p->input.p_stream );
2194 if( i_size > 0 && bookmark.i_byte_offset <= i_size )
2196 val.f_float = (double)bookmark.i_byte_offset / i_size;
2197 b_force_update = Control( p_input, INPUT_CONTROL_SET_POSITION, val );
2204 msg_Err( p_input, "not yet implemented" );
2208 ControlRelease( i_type, val );
2209 return b_force_update;
2212 /*****************************************************************************
2213 * UpdateTitleSeekpoint
2214 *****************************************************************************/
2215 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2216 int i_title, int i_seekpoint )
2218 int i_title_end = p_input->p->input.i_title_end -
2219 p_input->p->input.i_title_offset;
2220 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2221 p_input->p->input.i_seekpoint_offset;
2223 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2225 if( i_title > i_title_end ||
2226 ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2229 else if( i_seekpoint_end >= 0 )
2231 if( i_seekpoint > i_seekpoint_end )
2234 else if( i_title_end >= 0 )
2236 if( i_title > i_title_end )
2241 /*****************************************************************************
2243 *****************************************************************************/
2244 static int UpdateTitleSeekpointFromDemux( input_thread_t *p_input )
2246 demux_t *p_demux = p_input->p->input.p_demux;
2248 /* TODO event-like */
2249 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2251 input_SendEventTitle( p_input, p_demux->info.i_title );
2253 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2255 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2257 input_SendEventSeekpoint( p_input,
2258 p_demux->info.i_title, p_demux->info.i_seekpoint );
2260 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2263 /* Hmmm only works with master input */
2264 if( p_input->p->input.p_demux == p_demux )
2265 return UpdateTitleSeekpoint( p_input,
2266 p_demux->info.i_title,
2267 p_demux->info.i_seekpoint );
2271 static void UpdateGenericFromDemux( input_thread_t *p_input )
2273 demux_t *p_demux = p_input->p->input.p_demux;
2275 if( p_demux->info.i_update & INPUT_UPDATE_META )
2277 vlc_meta_t *p_meta = vlc_meta_New();
2280 demux_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
2281 InputUpdateMeta( p_input, p_meta );
2283 p_demux->info.i_update &= ~INPUT_UPDATE_META;
2286 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2290 /*****************************************************************************
2291 * Update*FromAccess:
2292 *****************************************************************************/
2293 static int UpdateTitleSeekpointFromAccess( input_thread_t *p_input )
2295 access_t *p_access = p_input->p->input.p_access;
2297 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2299 input_SendEventTitle( p_input, p_access->info.i_title );
2301 stream_Control( p_input->p->input.p_stream, STREAM_UPDATE_SIZE );
2303 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2305 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2307 input_SendEventSeekpoint( p_input,
2308 p_access->info.i_title, p_access->info.i_seekpoint );
2310 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2312 /* Hmmm only works with master input */
2313 if( p_input->p->input.p_access == p_access )
2314 return UpdateTitleSeekpoint( p_input,
2315 p_access->info.i_title,
2316 p_access->info.i_seekpoint );
2319 static void UpdateGenericFromAccess( input_thread_t *p_input )
2321 access_t *p_access = p_input->p->input.p_access;
2323 if( p_access->info.i_update & INPUT_UPDATE_META )
2325 /* TODO maybe multi - access ? */
2326 vlc_meta_t *p_meta = vlc_meta_New();
2329 access_Control( p_input->p->input.p_access, ACCESS_GET_META, p_meta );
2330 InputUpdateMeta( p_input, p_meta );
2332 p_access->info.i_update &= ~INPUT_UPDATE_META;
2334 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2339 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2340 f_quality = f_strength = -1;
2342 input_SendEventSignal( p_input, f_quality, f_strength );
2344 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2347 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2350 /*****************************************************************************
2352 *****************************************************************************/
2353 static input_source_t *InputSourceNew( input_thread_t *p_input )
2355 VLC_UNUSED(p_input);
2357 return calloc( 1, sizeof( input_source_t ) );
2360 /*****************************************************************************
2362 *****************************************************************************/
2363 static int InputSourceInit( input_thread_t *p_input,
2364 input_source_t *in, const char *psz_mrl,
2365 const char *psz_forced_demux )
2367 const char *psz_access;
2368 const char *psz_demux;
2370 char *psz_var_demux = NULL;
2374 char *psz_dup = strdup( psz_mrl );
2376 if( psz_dup == NULL )
2380 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2382 /* FIXME: file:// handling plugins do not support URIs properly...
2383 * So we pre-decode the URI to a path for them. Note that we do not do it
2384 * for non-standard VLC-specific schemes. */
2385 if( !strcmp( psz_access, "file" ) )
2387 if( psz_path[0] != '/'
2388 #if (DIR_SEP_CHAR != '/')
2389 /* We accept invalid URIs too. */
2390 && psz_path[0] != DIR_SEP_CHAR
2393 { /* host specified -> only localhost is supported */
2394 static const size_t i_localhost = sizeof("localhost")-1;
2395 if( strncmp( psz_path, "localhost/", i_localhost + 1) != 0 )
2397 msg_Err( p_input, "cannot open remote file `%s://%s'",
2398 psz_access, psz_path );
2399 msg_Info( p_input, "Did you mean `%s:///%s'?",
2400 psz_access, psz_path );
2403 psz_path += i_localhost;
2405 /* Then URI-decode the path. */
2406 decode_URI( psz_path );
2407 #if defined( WIN32 ) && !defined( UNDER_CE )
2408 /* Strip leading slash in front of the drive letter */
2411 #if (DIR_SEP_CHAR != '/')
2412 /* Turn slashes into anti-slashes */
2413 for( char *s = strchr( psz_path, '/' ); s; s = strchr( s + 1, '/' ) )
2418 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2419 psz_mrl, psz_access, psz_demux, psz_path );
2420 if( !p_input->b_preparsing )
2422 /* Hack to allow udp://@:port syntax */
2424 (strncmp( psz_access, "udp", 3 ) &&
2425 strncmp( psz_access, "rtp", 3 )) )
2427 /* Find optional titles and seekpoints */
2428 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2429 &in->i_seekpoint_start, &in->i_seekpoint_end );
2432 if( psz_forced_demux && *psz_forced_demux )
2434 psz_demux = psz_forced_demux;
2436 else if( *psz_demux == '\0' )
2438 /* special hack for forcing a demuxer with --demux=module
2439 * (and do nothing with a list) */
2440 psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2442 if( psz_var_demux != NULL &&
2443 !strchr(psz_var_demux, ',' ) &&
2444 !strchr(psz_var_demux, ':' ) )
2446 psz_demux = psz_var_demux;
2448 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2452 /* Try access_demux first */
2453 in->p_demux = demux_New( p_input, p_input, psz_access, psz_demux, psz_path,
2454 NULL, p_input->p->p_es_out, false );
2458 /* Preparsing is only for file:// */
2461 if( !*psz_access ) /* path without scheme:// */
2462 psz_access = "file";
2463 if( strcmp( psz_access, "file" ) )
2465 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2470 /* Get infos from access_demux */
2471 int i_ret = demux_Control( in->p_demux,
2472 DEMUX_GET_PTS_DELAY, &in->i_pts_delay );
2474 in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2477 in->b_title_demux = true;
2478 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2479 &in->title, &in->i_title,
2480 &in->i_title_offset, &in->i_seekpoint_offset ) )
2482 TAB_INIT( in->i_title, in->title );
2484 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2485 &in->b_can_pace_control ) )
2486 in->b_can_pace_control = false;
2488 assert( in->p_demux->pf_demux != NULL || !in->b_can_pace_control );
2490 if( !in->b_can_pace_control )
2492 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2493 &in->b_can_rate_control, &in->b_rescale_ts ) )
2495 in->b_can_rate_control = false;
2496 in->b_rescale_ts = true; /* not used */
2501 in->b_can_rate_control = true;
2502 in->b_rescale_ts = true;
2504 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2505 &in->b_can_pause ) )
2506 in->b_can_pause = false;
2507 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2508 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2509 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control && in->b_can_rate_control );
2512 if( demux_Control( in->p_demux, DEMUX_CAN_SEEK, &b_can_seek ) )
2514 var_SetBool( p_input, "can-seek", b_can_seek );
2518 /* Now try a real access */
2519 in->p_access = access_New( p_input, p_input, psz_access, psz_demux, psz_path );
2520 if( in->p_access == NULL )
2522 if( vlc_object_alive( p_input ) )
2524 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2526 dialog_Fatal( p_input, _("Your input can't be opened"),
2527 _("VLC is unable to open the MRL '%s'."
2528 " Check the log for details."), psz_mrl );
2533 /* Get infos from access */
2534 if( !p_input->b_preparsing )
2537 access_Control( in->p_access,
2538 ACCESS_GET_PTS_DELAY, &in->i_pts_delay );
2539 in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2541 in->b_title_demux = false;
2542 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2543 &in->title, &in->i_title,
2544 &in->i_title_offset, &in->i_seekpoint_offset ) )
2547 TAB_INIT( in->i_title, in->title );
2549 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2550 &in->b_can_pace_control );
2551 in->b_can_rate_control = in->b_can_pace_control;
2552 in->b_rescale_ts = true;
2554 access_Control( in->p_access, ACCESS_CAN_PAUSE, &in->b_can_pause );
2555 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2556 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2557 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2559 access_Control( in->p_access, ACCESS_CAN_SEEK, &b_can_seek );
2560 var_SetBool( p_input, "can-seek", b_can_seek );
2565 char **ppsz_input_list;
2567 TAB_INIT( i_input_list, ppsz_input_list );
2569 /* On master stream only, use input-list */
2570 if( &p_input->p->input == in )
2576 psz_parser = var_CreateGetNonEmptyString( p_input, "input-list" );
2578 while( psz_parser && *psz_parser )
2580 char *p = strchr( psz_parser, ',' );
2586 char *psz_name = strdup( psz_parser );
2588 TAB_APPEND( i_input_list, ppsz_input_list, psz_name );
2595 /* Autodetect extra files if none specified */
2596 if( i_input_list <= 0 )
2598 InputGetExtraFiles( p_input, &i_input_list, &ppsz_input_list,
2599 psz_access, psz_path );
2601 if( i_input_list > 0 )
2602 TAB_APPEND( i_input_list, ppsz_input_list, NULL );
2604 /* Create the stream_t */
2605 in->p_stream = stream_AccessNew( in->p_access, ppsz_input_list );
2606 if( ppsz_input_list )
2608 for( int i = 0; ppsz_input_list[i] != NULL; i++ )
2609 free( ppsz_input_list[i] );
2610 TAB_CLEAN( i_input_list, ppsz_input_list );
2613 if( in->p_stream == NULL )
2615 msg_Warn( p_input, "cannot create a stream_t from access" );
2619 /* Add stream filters */
2620 char *psz_stream_filter = var_GetNonEmptyString( p_input,
2622 in->p_stream = stream_FilterChainNew( in->p_stream,
2624 var_GetBool( p_input, "input-record-native" ) );
2625 free( psz_stream_filter );
2627 /* Open a demuxer */
2628 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2630 psz_demux = in->p_access->psz_demux;
2633 in->p_demux = demux_New( p_input, p_input, psz_access, psz_demux,
2634 /* Take access/stream redirections into account: */
2635 in->p_stream->psz_path ? in->p_stream->psz_path : psz_path,
2636 in->p_stream, p_input->p->p_es_out,
2637 p_input->b_preparsing );
2639 if( in->p_demux == NULL )
2641 if( vlc_object_alive( p_input ) )
2643 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2644 psz_access, psz_demux, psz_path );
2645 dialog_Fatal( VLC_OBJECT( p_input ),
2646 _("VLC can't recognize the input's format"),
2647 _("The format of '%s' cannot be detected. "
2648 "Have a look at the log for details."), psz_mrl );
2652 assert( in->p_demux->pf_demux != NULL );
2654 /* Get title from demux */
2655 if( !p_input->b_preparsing && in->i_title <= 0 )
2657 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2658 &in->title, &in->i_title,
2659 &in->i_title_offset, &in->i_seekpoint_offset ))
2661 TAB_INIT( in->i_title, in->title );
2665 in->b_title_demux = true;
2670 free( psz_var_demux );
2673 /* Set record capabilities */
2674 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2675 in->b_can_stream_record = false;
2677 if( !var_GetBool( p_input, "input-record-native" ) )
2678 in->b_can_stream_record = false;
2679 var_SetBool( p_input, "can-record", true );
2681 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2685 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2686 if( 1 || !p_input->b_preparsing )
2689 input_attachment_t **attachment;
2690 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2691 &attachment, &i_attachment ) )
2693 vlc_mutex_lock( &p_input->p->p_item->lock );
2694 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2695 i_attachment, attachment );
2696 vlc_mutex_unlock( &p_input->p->p_item->lock );
2699 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) && f_fps > 0.0 )
2701 vlc_mutex_lock( &p_input->p->p_item->lock );
2702 p_input->p->f_fps = f_fps;
2703 vlc_mutex_unlock( &p_input->p->p_item->lock );
2706 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2707 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2713 demux_Delete( in->p_demux );
2716 stream_Delete( in->p_stream );
2719 access_Delete( in->p_access );
2721 free( psz_var_demux );
2724 return VLC_EGENERIC;
2727 /*****************************************************************************
2729 *****************************************************************************/
2730 static void InputSourceClean( input_source_t *in )
2735 demux_Delete( in->p_demux );
2738 stream_Delete( in->p_stream );
2741 access_Delete( in->p_access );
2743 if( in->i_title > 0 )
2745 for( i = 0; i < in->i_title; i++ )
2746 vlc_input_title_Delete( in->title[i] );
2747 TAB_CLEAN( in->i_title, in->title );
2751 /*****************************************************************************
2753 *****************************************************************************/
2754 static void InputSourceMeta( input_thread_t *p_input,
2755 input_source_t *p_source, vlc_meta_t *p_meta )
2757 access_t *p_access = p_source->p_access;
2758 demux_t *p_demux = p_source->p_demux;
2760 /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
2765 /* Read access meta */
2766 has_meta = p_access && !access_Control( p_access, ACCESS_GET_META, p_meta );
2768 /* Read demux meta */
2769 has_meta |= !demux_Control( p_demux, DEMUX_GET_META, p_meta );
2771 bool has_unsupported;
2772 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &has_unsupported ) )
2773 has_unsupported = true;
2775 /* If the demux report unsupported meta data, or if we don't have meta data
2776 * try an external "meta reader" */
2777 if( has_meta && !has_unsupported )
2780 demux_meta_t *p_demux_meta =
2781 vlc_custom_create( p_demux, sizeof( *p_demux_meta ),
2782 VLC_OBJECT_GENERIC, "demux meta" );
2785 vlc_object_attach( p_demux_meta, p_demux );
2786 p_demux_meta->p_demux = p_demux;
2787 p_demux_meta->p_item = p_input->p->p_item;
2789 module_t *p_id3 = module_need( p_demux_meta, "meta reader", NULL, false );
2792 if( p_demux_meta->p_meta )
2794 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2795 vlc_meta_Delete( p_demux_meta->p_meta );
2798 if( p_demux_meta->i_attachments > 0 )
2800 vlc_mutex_lock( &p_input->p->p_item->lock );
2801 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2802 p_demux_meta->i_attachments, p_demux_meta->attachments );
2803 vlc_mutex_unlock( &p_input->p->p_item->lock );
2805 module_unneed( p_demux, p_id3 );
2807 vlc_object_release( p_demux_meta );
2811 static void SlaveDemux( input_thread_t *p_input, bool *pb_demux_polled )
2816 *pb_demux_polled = false;
2817 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2819 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2823 for( i = 0; i < p_input->p->i_slave; i++ )
2825 input_source_t *in = p_input->p->slave[i];
2831 const bool b_demux_polled = in->p_demux->pf_demux != NULL;
2832 if( !b_demux_polled )
2835 *pb_demux_polled = true;
2837 /* Call demux_Demux until we have read enough data */
2838 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2843 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2845 msg_Err( p_input, "slave[%d] doesn't like "
2846 "DEMUX_GET_TIME -> EOF", i );
2851 if( i_stime >= i_time )
2857 if( ( i_ret = demux_Demux( in->p_demux ) ) <= 0 )
2863 i_ret = demux_Demux( in->p_demux );
2868 msg_Dbg( p_input, "slave %d EOF", i );
2874 static void SlaveSeek( input_thread_t *p_input )
2879 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2881 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2885 for( i = 0; i < p_input->p->i_slave; i++ )
2887 input_source_t *in = p_input->p->slave[i];
2889 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time, true ) )
2892 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2902 /*****************************************************************************
2904 *****************************************************************************/
2905 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2907 static const struct { int i_meta; const char *psz_name; } p_list[] = {
2908 { vlc_meta_Title, "meta-title" },
2909 { vlc_meta_Artist, "meta-artist" },
2910 { vlc_meta_Genre, "meta-genre" },
2911 { vlc_meta_Copyright, "meta-copyright" },
2912 { vlc_meta_Description, "meta-description" },
2913 { vlc_meta_Date, "meta-date" },
2914 { vlc_meta_URL, "meta-url" },
2918 /* Get meta information from user */
2919 for( int i = 0; p_list[i].psz_name; i++ )
2921 char *psz_string = var_GetNonEmptyString( p_input, p_list[i].psz_name );
2925 EnsureUTF8( psz_string );
2926 vlc_meta_Set( p_meta, p_list[i].i_meta, psz_string );
2931 /*****************************************************************************
2932 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2933 * arturl and locking issue.
2934 *****************************************************************************/
2935 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2937 es_out_ControlSetMeta( p_input->p->p_es_out, p_meta );
2938 vlc_meta_Delete( p_meta );
2941 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2942 int i_new, input_attachment_t **pp_new )
2944 int i_attachment = *pi_attachment;
2945 input_attachment_t **attachment = *ppp_attachment;
2948 attachment = xrealloc( attachment,
2949 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2950 for( i = 0; i < i_new; i++ )
2951 attachment[i_attachment++] = pp_new[i];
2955 *pi_attachment = i_attachment;
2956 *ppp_attachment = attachment;
2958 /*****************************************************************************
2959 * InputGetExtraFiles
2960 * Autodetect extra input list
2961 *****************************************************************************/
2962 static void InputGetExtraFilesPattern( input_thread_t *p_input,
2963 int *pi_list, char ***pppsz_list,
2964 const char *psz_path,
2965 const char *psz_match,
2966 const char *psz_format,
2967 int i_start, int i_stop )
2972 TAB_INIT( i_list, ppsz_list );
2974 char *psz_base = strdup( psz_path );
2978 /* Remove the extension */
2979 char *psz_end = &psz_base[strlen(psz_base)-strlen(psz_match)];
2980 assert( psz_end >= psz_base);
2983 /* Try to list files */
2984 for( int i = i_start; i <= i_stop; i++ )
2989 if( asprintf( &psz_file, psz_format, psz_base, i ) < 0 )
2992 if( vlc_stat( psz_file, &st ) || !S_ISREG( st.st_mode ) || !st.st_size )
2998 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2999 TAB_APPEND( i_list, ppsz_list, psz_file );
3004 *pppsz_list = ppsz_list;
3007 static void InputGetExtraFiles( input_thread_t *p_input,
3008 int *pi_list, char ***pppsz_list,
3009 const char *psz_access, const char *psz_path )
3013 const char *psz_match;
3014 const char *psz_format;
3018 /* XXX the order is important */
3019 { ".001", "%s.%.3d", 2, 999 },
3020 { ".part1.rar", "%s.part%.1d.rar",2, 9 },
3021 { ".part01.rar", "%s.part%.2d.rar",2, 99, },
3022 { ".part001.rar", "%s.part%.3d.rar",2, 999 },
3023 { ".rar", "%s.r%.2d", 0, 99 },
3024 { NULL, NULL, 0, 0 }
3027 TAB_INIT( *pi_list, *pppsz_list );
3029 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
3032 const size_t i_path = strlen(psz_path);
3034 for( int i = 0; p_pattern[i].psz_match != NULL; i++ )
3036 const size_t i_ext = strlen(p_pattern[i].psz_match );
3038 if( i_path < i_ext )
3040 if( !strcmp( &psz_path[i_path-i_ext], p_pattern[i].psz_match ) )
3042 InputGetExtraFilesPattern( p_input, pi_list, pppsz_list,
3044 p_pattern[i].psz_match, p_pattern[i].psz_format,
3045 p_pattern[i].i_start, p_pattern[i].i_stop );
3052 static void input_ChangeState( input_thread_t *p_input, int i_state )
3054 const bool b_changed = p_input->p->i_state != i_state;
3056 p_input->p->i_state = i_state;
3057 if( i_state == ERROR_S )
3058 p_input->b_error = true;
3059 else if( i_state == END_S )
3060 p_input->b_eof = true;
3064 input_item_SetErrorWhenReading( p_input->p->p_item, p_input->b_error );
3065 input_SendEventState( p_input, i_state );
3070 /*****************************************************************************
3071 * MRLSplit: parse the access, demux and url part of the
3072 * Media Resource Locator.
3073 *****************************************************************************/
3074 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux,
3075 char **ppsz_path, char *psz_dup )
3077 const char *psz_access;
3078 const char *psz_demux = "";
3081 /* Either there is an access/demux specification before ://
3082 * or we have a plain local file path. */
3083 psz_path = strstr( psz_dup, "://" );
3084 if( psz_path != NULL )
3087 psz_path += 3; /* skips "://" */
3089 psz_access = psz_dup;
3090 /* We really don't want module name substitution here! */
3091 if( psz_access[0] == '$' )
3094 /* Separate access from demux (<access>/<demux>://<path>) */
3095 char *p = strchr( psz_access, '/' );
3100 if( psz_demux[0] == '$' )
3104 /* Remove HTML anchor if present (not supported).
3105 * The hash symbol itself should be URI-encoded. */
3106 p = strchr( psz_path, '#' );
3113 fprintf( stderr, "%s(\"%s\"): not a valid URI!\n", __func__,
3120 *ppsz_access = psz_access;
3121 *ppsz_demux = psz_demux;
3122 *ppsz_path = psz_path;
3125 static inline bool next(char ** src)
3129 long result = strtol( *src, &end, 0 );
3130 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
3139 /*****************************************************************************
3140 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3143 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
3144 *****************************************************************************/
3145 static void MRLSections( input_thread_t *p_input, char *psz_source,
3146 int *pi_title_start, int *pi_title_end,
3147 int *pi_chapter_start, int *pi_chapter_end )
3149 char *psz, *psz_end, *psz_next, *psz_check;
3151 *pi_title_start = *pi_title_end = -1;
3152 *pi_chapter_start = *pi_chapter_end = -1;
3154 /* Start by parsing titles and chapters */
3155 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
3158 /* Check we are really dealing with a title/chapter section */
3159 psz_check = psz + 1;
3160 if( !*psz_check ) return;
3161 if( isdigit(*psz_check) )
3162 if(!next(&psz_check)) return;
3163 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
3164 if( *psz_check == ':' && ++psz_check )
3166 if( isdigit(*psz_check) )
3167 if(!next(&psz_check)) return;
3169 if( *psz_check != '-' && *psz_check ) return;
3170 if( *psz_check == '-' && ++psz_check )
3172 if( isdigit(*psz_check) )
3173 if(!next(&psz_check)) return;
3175 if( *psz_check != ':' && *psz_check ) return;
3176 if( *psz_check == ':' && ++psz_check )
3178 if( isdigit(*psz_check) )
3179 if(!next(&psz_check)) return;
3181 if( *psz_check ) return;
3183 /* Separate start and end */
3185 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
3187 /* Look for the start title */
3188 *pi_title_start = strtol( psz, &psz_next, 0 );
3189 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
3190 *pi_title_end = *pi_title_start;
3193 /* Look for the start chapter */
3195 *pi_chapter_start = strtol( psz, &psz_next, 0 );
3196 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
3197 *pi_chapter_end = *pi_chapter_start;
3201 /* Look for the end title */
3202 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3203 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3206 /* Look for the end chapter */
3207 if( *psz_end ) psz_end++;
3208 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3209 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3212 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3213 psz_source, *pi_title_start, *pi_chapter_start,
3214 *pi_title_end, *pi_chapter_end );
3217 /*****************************************************************************
3218 * input_AddSubtitles: add a subtitles file and enable it
3219 *****************************************************************************/
3220 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3222 input_source_t *sub;
3225 char *psz_path, *psz_extension;
3227 /* if we are provided a subtitle.sub file,
3228 * see if we don't have a subtitle.idx and use it instead */
3229 psz_path = strdup( psz_subtitle );
3232 psz_extension = strrchr( psz_path, '.');
3233 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3237 strcpy( psz_extension, ".idx" );
3239 if( !vlc_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3241 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3242 psz_path, psz_subtitle );
3243 strcpy( psz_subtitle, psz_path );
3249 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3251 sub = InputSourceNew( p_input );
3252 if( !sub || InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3257 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3260 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3262 if( count.i_int == 0 )
3264 /* if it was first one, there is disable too */
3266 if( count.i_int < list.p_list->i_count )
3268 const int i_id = list.p_list->p_values[count.i_int].i_int;
3270 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3271 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3273 var_FreeList( &list, NULL );
3277 /*****************************************************************************
3279 *****************************************************************************/
3280 void input_UpdateStatistic( input_thread_t *p_input,
3281 input_statistic_t i_type, int i_delta )
3283 assert( p_input->p->i_state != INIT_S );
3285 vlc_mutex_lock( &p_input->p->counters.counters_lock);
3288 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
3289 case INPUT_STATISTIC_DECODED_VIDEO:
3292 case INPUT_STATISTIC_DECODED_AUDIO:
3295 case INPUT_STATISTIC_DECODED_SUBTITLE:
3298 case INPUT_STATISTIC_SENT_PACKET:
3299 I(p_sout_sent_packets);
3302 case INPUT_STATISTIC_SENT_BYTE:
3304 int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
3306 if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
3307 stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
3311 msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3314 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
3318 /* TODO FIXME nearly the same logic that snapshot code */
3319 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3324 path = vlc_opendir( psz_path );
3329 char *psz_tmp = str_format( p_obj, psz_prefix );
3333 filename_sanitize( psz_tmp );
3335 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3337 psz_extension ? "." : "",
3338 psz_extension ? psz_extension : "" ) < 0 )
3345 psz_file = str_format( p_obj, psz_path );
3346 path_sanitize( psz_file );