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>
54 #include <vlc_strings.h>
56 #ifdef HAVE_SYS_STAT_H
57 # include <sys/stat.h>
60 /*****************************************************************************
62 *****************************************************************************/
63 static void Destructor( input_thread_t * p_input );
65 static void *Run ( vlc_object_t *p_this );
67 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
68 const char *, bool, input_resource_t * );
69 static int Init ( input_thread_t *p_input );
70 static void End ( input_thread_t *p_input );
71 static void MainLoop( input_thread_t *p_input, bool b_interactive );
73 static void ObjectKillChildrens( input_thread_t *, vlc_object_t * );
75 static inline int ControlPop( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline, bool b_postpone_seek );
76 static void ControlRelease( int i_type, vlc_value_t val );
77 static bool ControlIsSeekRequest( int i_type );
78 static bool Control( input_thread_t *, int, vlc_value_t );
80 static int UpdateTitleSeekpointFromAccess( input_thread_t * );
81 static void UpdateGenericFromAccess( input_thread_t * );
83 static int UpdateTitleSeekpointFromDemux( input_thread_t * );
84 static void UpdateGenericFromDemux( input_thread_t * );
86 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
88 static input_source_t *InputSourceNew( input_thread_t *);
89 static int InputSourceInit( input_thread_t *, input_source_t *,
90 const char *, const char *psz_forced_demux );
91 static void InputSourceClean( input_source_t * );
92 static void InputSourceMeta( input_thread_t *, input_source_t *, vlc_meta_t * );
95 //static void InputGetAttachments( input_thread_t *, input_source_t * );
96 static void SlaveDemux( input_thread_t *p_input, bool *pb_demux_polled );
97 static void SlaveSeek( input_thread_t *p_input );
99 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
100 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
101 static void InputGetExtraFiles( input_thread_t *p_input,
102 int *pi_list, char ***pppsz_list,
103 const char *psz_access, const char *psz_path );
105 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
106 int i_new, input_attachment_t **pp_new );
108 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced );
110 static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */
112 /* Do not let a pts_delay from access/demux go beyong 60s */
113 #define INPUT_PTS_DELAY_MAX INT64_C(60000000)
117 * Create a new input_thread_t.
119 * You need to call input_Start on it when you are done
120 * adding callback on the variables/events you want to monitor.
122 * \param p_parent a vlc_object
123 * \param p_item an input item
124 * \param psz_log an optional prefix for this input logs
125 * \param p_resource an optional input ressource
126 * \return a pointer to the spawned input thread
128 input_thread_t *input_Create( vlc_object_t *p_parent,
129 input_item_t *p_item,
130 const char *psz_log, input_resource_t *p_resource )
132 return Create( p_parent, p_item, psz_log, false, p_resource );
135 #undef input_CreateAndStart
137 * Create a new input_thread_t and start it.
139 * Provided for convenience.
143 input_thread_t *input_CreateAndStart( vlc_object_t *p_parent,
144 input_item_t *p_item, const char *psz_log )
146 input_thread_t *p_input = input_Create( p_parent, p_item, psz_log, NULL );
148 if( input_Start( p_input ) )
150 vlc_object_release( p_input );
158 * Initialize an input thread and run it until it stops by itself.
160 * \param p_parent a vlc_object
161 * \param p_item an input item
162 * \return an error code, VLC_SUCCESS on success
164 int input_Read( vlc_object_t *p_parent, input_item_t *p_item )
166 input_thread_t *p_input = Create( p_parent, p_item, NULL, false, NULL );
170 if( !Init( p_input ) )
172 MainLoop( p_input, false );
176 vlc_object_release( p_input );
181 * Initialize an input and initialize it to preparse the item
182 * This function is blocking. It will only accept parsing regular files.
184 * \param p_parent a vlc_object_t
185 * \param p_item an input item
186 * \return VLC_SUCCESS or an error
188 int input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
190 input_thread_t *p_input;
192 /* Allocate descriptor */
193 p_input = Create( p_parent, p_item, NULL, true, NULL );
197 if( !Init( p_input ) )
200 vlc_object_release( p_input );
206 * Start a input_thread_t created by input_Create.
208 * You must not start an already running input_thread_t.
210 * \param the input thread to start
212 int input_Start( input_thread_t *p_input )
214 /* Create thread and wait for its readiness. */
215 if( vlc_thread_create( p_input, "input", Run,
216 VLC_THREAD_PRIORITY_INPUT ) )
218 input_ChangeState( p_input, ERROR_S );
219 msg_Err( p_input, "cannot create input thread" );
226 * Request a running input thread to stop and die
228 * b_abort must be true when a user stop is requested and not because you have
229 * detected an error or an eof. It will be used to properly send the
230 * INPUT_EVENT_ABORT event.
232 * \param p_input the input thread to stop
233 * \param b_abort true if the input has been aborted by a user request
235 void input_Stop( input_thread_t *p_input, bool b_abort )
237 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
238 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
239 * unlock the control loop */
240 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
242 vlc_mutex_lock( &p_input->p->lock_control );
243 p_input->p->b_abort |= b_abort;
244 vlc_mutex_unlock( &p_input->p->lock_control );
246 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
249 input_resource_t *input_DetachResource( input_thread_t *p_input )
251 assert( p_input->b_dead );
253 input_resource_SetInput( p_input->p->p_resource, NULL );
255 input_resource_t *p_resource = input_resource_Detach( p_input->p->p_resource );
256 p_input->p->p_sout = NULL;
262 * Get the item from an input thread
263 * FIXME it does not increase ref count of the item.
264 * if it is used after p_input is destroyed nothing prevent it from
267 input_item_t *input_GetItem( input_thread_t *p_input )
269 assert( p_input && p_input->p );
270 return p_input->p->p_item;
273 /*****************************************************************************
274 * ObjectKillChildrens
275 *****************************************************************************/
276 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
281 /* FIXME ObjectKillChildrens seems a very bad idea in fact */
282 i = vlc_internals( p_obj )->i_object_type;
283 if( i == VLC_OBJECT_VOUT ||i == VLC_OBJECT_AOUT ||
284 p_obj == VLC_OBJECT(p_input->p->p_sout) ||
285 i == VLC_OBJECT_DECODER )
288 vlc_object_kill( p_obj );
290 p_list = vlc_list_children( p_obj );
291 for( i = 0; i < p_list->i_count; i++ )
292 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
293 vlc_list_release( p_list );
296 /*****************************************************************************
297 * This function creates a new input, and returns a pointer
298 * to its description. On error, it returns NULL.
300 * XXX Do not forget to update vlc_input.h if you add new variables.
301 *****************************************************************************/
302 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
303 const char *psz_header, bool b_quick,
304 input_resource_t *p_resource )
306 static const char input_name[] = "input";
307 input_thread_t *p_input = NULL; /* thread descriptor */
310 /* Allocate descriptor */
311 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
312 VLC_OBJECT_INPUT, input_name );
313 if( p_input == NULL )
316 vlc_object_attach( p_input, p_parent );
318 /* Construct a nice name for the input timer */
319 char psz_timer_name[255];
320 char * psz_name = input_item_GetName( p_item );
321 snprintf( psz_timer_name, sizeof(psz_timer_name),
322 "input launching for '%s'", psz_name );
324 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
328 /* Start a timer to mesure how long it takes
329 * to launch an input */
330 stats_TimerStart( p_input, psz_timer_name,
331 STATS_TIMER_INPUT_LAUNCHING );
333 p_input->p = calloc( 1, sizeof( input_thread_private_t ) );
337 p_input->b_preparsing = b_quick;
338 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
340 /* Init Common fields */
341 p_input->b_eof = false;
342 p_input->p->b_can_pace_control = true;
343 p_input->p->i_start = 0;
344 p_input->p->i_time = 0;
345 p_input->p->i_stop = 0;
346 p_input->p->i_run = 0;
347 p_input->p->i_title = 0;
348 p_input->p->title = NULL;
349 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
350 p_input->p->i_state = INIT_S;
351 p_input->p->i_rate = INPUT_RATE_DEFAULT
352 / var_CreateGetFloat( p_input, "rate" );
353 /* Currently, the input rate variable is an integer. So we need to destroy
354 * the float variable inherited from the configuration. */
355 var_Destroy( p_input, "rate" );
356 p_input->p->b_recording = false;
357 memset( &p_input->p->bookmark, 0, sizeof(p_input->p->bookmark) );
358 TAB_INIT( p_input->p->i_bookmark, p_input->p->pp_bookmark );
359 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
360 p_input->p->p_sout = NULL;
361 p_input->p->b_out_pace_control = false;
363 vlc_gc_incref( p_item ); /* Released in Destructor() */
364 p_input->p->p_item = p_item;
366 /* Init Input fields */
367 p_input->p->input.p_access = NULL;
368 p_input->p->input.p_stream = NULL;
369 p_input->p->input.p_demux = NULL;
370 p_input->p->input.b_title_demux = false;
371 p_input->p->input.i_title = 0;
372 p_input->p->input.title = NULL;
373 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
374 p_input->p->input.b_can_pace_control = true;
375 p_input->p->input.b_can_rate_control = true;
376 p_input->p->input.b_rescale_ts = true;
377 p_input->p->input.b_eof = false;
379 vlc_mutex_lock( &p_item->lock );
381 if( !p_item->p_stats )
382 p_item->p_stats = stats_NewInputStats( p_input );
383 vlc_mutex_unlock( &p_item->lock );
386 p_input->p->i_slave = 0;
387 p_input->p->slave = NULL;
391 p_input->p->p_resource = p_resource;
393 p_input->p->p_resource = input_resource_New();
394 input_resource_SetInput( p_input->p->p_resource, p_input );
396 /* Init control buffer */
397 vlc_mutex_init( &p_input->p->lock_control );
398 vlc_cond_init( &p_input->p->wait_control );
399 p_input->p->i_control = 0;
400 p_input->p->b_abort = false;
402 /* Parse input options */
403 vlc_mutex_lock( &p_item->lock );
404 assert( (int)p_item->optflagc == p_item->i_options );
405 for( i = 0; i < p_item->i_options; i++ )
406 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
407 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
408 vlc_mutex_unlock( &p_item->lock );
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;
476 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
477 vlc_mutex_init( &p_input->p->counters.counters_lock );
479 p_input->p->p_es_out_display = input_EsOutNew( p_input, p_input->p->i_rate );
480 p_input->p->p_es_out = NULL;
482 /* Set the destructor when we are sure we are initialized */
483 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
489 * Input destructor (called when the object's refcount reaches 0).
491 static void Destructor( input_thread_t * p_input )
494 char * psz_name = input_item_GetName( p_input->p->p_item );
495 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
499 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
500 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
502 if( p_input->p->p_es_out_display )
503 es_out_Delete( p_input->p->p_es_out_display );
505 if( p_input->p->p_resource )
506 input_resource_Delete( p_input->p->p_resource );
508 vlc_gc_decref( p_input->p->p_item );
510 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
512 for( int i = 0; i < p_input->p->i_control; i++ )
514 input_control_t *p_ctrl = &p_input->p->control[i];
515 ControlRelease( p_ctrl->i_type, p_ctrl->val );
518 vlc_cond_destroy( &p_input->p->wait_control );
519 vlc_mutex_destroy( &p_input->p->lock_control );
523 /*****************************************************************************
524 * Run: main thread loop
525 * This is the "normal" thread that spawns the input processing chain,
526 * reads the stream, cleans up and waits
527 *****************************************************************************/
528 static void *Run( vlc_object_t *p_this )
530 input_thread_t *p_input = (input_thread_t *)p_this;
531 const int canc = vlc_savecancel();
533 if( Init( p_input ) )
536 MainLoop( p_input, true ); /* FIXME it can be wrong (like with VLM) */
542 /* Tell we're dead */
543 vlc_mutex_lock( &p_input->p->lock_control );
544 const bool b_abort = p_input->p->b_abort;
545 vlc_mutex_unlock( &p_input->p->lock_control );
548 input_SendEventAbort( p_input );
549 input_SendEventDead( p_input );
551 vlc_restorecancel( canc );
555 /*****************************************************************************
556 * Main loop: Fill buffers from access, and demux
557 *****************************************************************************/
561 * It asks the demuxer to demux some data
563 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, bool *pb_demux_polled, mtime_t i_start_mdate )
568 *pb_demux_polled = p_input->p->input.p_demux->pf_demux != NULL;
570 if( ( p_input->p->i_stop > 0 && p_input->p->i_time >= p_input->p->i_stop ) ||
571 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
574 i_ret = demux_Demux( p_input->p->input.p_demux );
578 if( p_input->p->input.p_demux->info.i_update )
580 if( p_input->p->input.b_title_demux )
582 i_ret = UpdateTitleSeekpointFromDemux( p_input );
585 UpdateGenericFromDemux( p_input );
587 else if( p_input->p->input.p_access &&
588 p_input->p->input.p_access->info.i_update )
590 if( !p_input->p->input.b_title_demux )
592 i_ret = UpdateTitleSeekpointFromAccess( p_input );
595 UpdateGenericFromAccess( p_input );
599 if( i_ret == 0 ) /* EOF */
601 msg_Dbg( p_input, "EOF reached" );
602 p_input->p->input.b_eof = true;
606 input_ChangeState( p_input, ERROR_S );
609 if( i_ret > 0 && p_input->p->i_slave > 0 )
612 SlaveDemux( p_input, &b_demux_polled );
614 *pb_demux_polled |= b_demux_polled;
618 static int MainLoopTryRepeat( input_thread_t *p_input, mtime_t *pi_start_mdate )
620 int i_repeat = var_GetInteger( p_input, "input-repeat" );
626 msg_Dbg( p_input, "repeating the same input (%d)", i_repeat );
630 var_SetInteger( p_input, "input-repeat", i_repeat );
633 /* Seek to start title/seekpoint */
634 val.i_int = p_input->p->input.i_title_start -
635 p_input->p->input.i_title_offset;
636 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
638 input_ControlPush( p_input,
639 INPUT_CONTROL_SET_TITLE, &val );
641 val.i_int = p_input->p->input.i_seekpoint_start -
642 p_input->p->input.i_seekpoint_offset;
643 if( val.i_int > 0 /* TODO: check upper boundary */ )
644 input_ControlPush( p_input,
645 INPUT_CONTROL_SET_SEEKPOINT, &val );
647 /* Seek to start position */
648 if( p_input->p->i_start > 0 )
650 val.i_time = p_input->p->i_start;
651 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &val );
656 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION, &val );
660 *pi_start_mdate = mdate();
666 * It update the variables used by the interfaces
668 static void MainLoopInterface( input_thread_t *p_input )
670 double f_position = 0.0;
672 mtime_t i_length = 0;
674 /* update input status variables */
675 if( demux_Control( p_input->p->input.p_demux,
676 DEMUX_GET_POSITION, &f_position ) )
679 if( demux_Control( p_input->p->input.p_demux,
680 DEMUX_GET_TIME, &i_time ) )
682 p_input->p->i_time = i_time;
684 if( demux_Control( p_input->p->input.p_demux,
685 DEMUX_GET_LENGTH, &i_length ) )
688 es_out_SetTimes( p_input->p->p_es_out, f_position, i_time, i_length );
690 /* update current bookmark */
691 vlc_mutex_lock( &p_input->p->p_item->lock );
692 p_input->p->bookmark.i_time_offset = i_time;
693 if( p_input->p->input.p_stream )
694 p_input->p->bookmark.i_byte_offset = stream_Tell( p_input->p->input.p_stream );
695 vlc_mutex_unlock( &p_input->p->p_item->lock );
700 * It updates the globals statics
702 static void MainLoopStatistic( input_thread_t *p_input )
704 stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
705 input_SendEventStatistics( p_input );
710 * The main input loop.
712 static void MainLoop( input_thread_t *p_input, bool b_interactive )
714 mtime_t i_start_mdate = mdate();
715 mtime_t i_intf_update = 0;
716 mtime_t i_statistic_update = 0;
717 mtime_t i_last_seek_mdate = 0;
718 bool b_pause_after_eof = b_interactive &&
719 var_CreateGetBool( p_input, "play-and-pause" );
721 /* Start the timer */
722 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
724 while( vlc_object_alive( p_input ) && !p_input->b_error )
734 b_force_update = false;
736 /* FIXME if p_input->p->i_state == PAUSE_S the access/access_demux
737 * is paused -> this may cause problem with some of them
738 * The same problem can be seen when seeking while paused */
739 b_paused = p_input->p->i_state == PAUSE_S &&
740 ( !es_out_GetBuffering( p_input->p->p_es_out ) || p_input->p->input.b_eof );
742 b_demux_polled = true;
745 if( !p_input->p->input.b_eof )
747 MainLoopDemux( p_input, &b_force_update, &b_demux_polled, i_start_mdate );
749 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
751 else if( !es_out_GetEmpty( p_input->p->p_es_out ) )
753 msg_Dbg( p_input, "waiting decoder fifos to empty" );
754 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
756 /* Pause after eof only if the input is pausable.
757 * This way we won't trigger timeshifting for nothing */
758 else if( b_pause_after_eof && p_input->p->b_can_pause )
760 msg_Dbg( p_input, "pausing at EOF (pause after each)");
762 Control( p_input, INPUT_CONTROL_SET_STATE, val );
764 b_pause_after_eof = false;
769 if( MainLoopTryRepeat( p_input, &i_start_mdate ) )
771 b_pause_after_eof = var_GetBool( p_input, "play-and-pause" );
777 mtime_t i_deadline = i_wakeup;
778 if( b_paused || !b_demux_polled )
779 i_deadline = __MIN( i_intf_update, i_statistic_update );
784 mtime_t i_limit = i_deadline;
786 /* We will postpone the execution of a seek until we have
787 * finished the ES bufferisation (postpone is limited to
789 bool b_buffering = es_out_GetBuffering( p_input->p->p_es_out ) &&
790 !p_input->p->input.b_eof;
793 /* When postpone is in order, check the ES level every 20ms */
794 mtime_t i_current = mdate();
795 if( i_last_seek_mdate + INT64_C(125000) >= i_current )
796 i_limit = __MIN( i_deadline, i_current + INT64_C(20000) );
800 if( ControlPop( p_input, &i_type, &val, i_limit, b_buffering ) )
802 if( b_buffering && i_limit < i_deadline )
807 msg_Dbg( p_input, "control type=%d", i_type );
809 if( Control( p_input, i_type, val ) )
811 if( ControlIsSeekRequest( i_type ) )
812 i_last_seek_mdate = mdate();
813 b_force_update = true;
817 /* Update interface and statistics */
819 if( i_intf_update < i_current || b_force_update )
821 MainLoopInterface( p_input );
822 i_intf_update = i_current + INT64_C(250000);
823 b_force_update = false;
825 if( i_statistic_update < i_current )
827 MainLoopStatistic( p_input );
828 i_statistic_update = i_current + INT64_C(1000000);
831 /* Update the wakeup time */
833 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
834 } while( i_current < i_wakeup );
837 if( !p_input->b_error )
838 input_ChangeState( p_input, END_S );
841 static void InitStatistics( input_thread_t * p_input )
843 if( p_input->b_preparsing ) return;
845 /* Prepare statistics */
846 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
847 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
848 if( libvlc_stats( p_input ) )
850 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
851 INIT_COUNTER( read_packets, INTEGER, COUNTER );
852 INIT_COUNTER( demux_read, INTEGER, COUNTER );
853 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
854 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
855 INIT_COUNTER( demux_corrupted, INTEGER, COUNTER );
856 INIT_COUNTER( demux_discontinuity, INTEGER, COUNTER );
857 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
858 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
859 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
860 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
861 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
862 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
863 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
864 p_input->p->counters.p_sout_send_bitrate = NULL;
865 p_input->p->counters.p_sout_sent_packets = NULL;
866 p_input->p->counters.p_sout_sent_bytes = NULL;
867 if( p_input->p->counters.p_demux_bitrate )
868 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
869 if( p_input->p->counters.p_input_bitrate )
870 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
875 static int InitSout( input_thread_t * p_input )
877 if( p_input->b_preparsing )
880 /* Find a usable sout and attach it to p_input */
881 char *psz = var_GetNonEmptyString( p_input, "sout" );
882 if( psz && strncasecmp( p_input->p->p_item->psz_uri, "vlc:", 4 ) )
884 p_input->p->p_sout = input_resource_RequestSout( p_input->p->p_resource, NULL, psz );
885 if( !p_input->p->p_sout )
887 input_ChangeState( p_input, ERROR_S );
888 msg_Err( p_input, "cannot start stream output instance, " \
893 if( libvlc_stats( p_input ) )
895 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
896 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
897 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
898 if( p_input->p->counters.p_sout_send_bitrate )
899 p_input->p->counters.p_sout_send_bitrate->update_interval =
905 input_resource_RequestSout( p_input->p->p_resource, NULL, NULL );
913 static void InitTitle( input_thread_t * p_input )
915 input_source_t *p_master = &p_input->p->input;
917 if( p_input->b_preparsing )
920 /* Create global title (from master) */
921 p_input->p->i_title = p_master->i_title;
922 p_input->p->title = p_master->title;
923 p_input->p->i_title_offset = p_master->i_title_offset;
924 p_input->p->i_seekpoint_offset = p_master->i_seekpoint_offset;
925 if( p_input->p->i_title > 0 )
927 /* Setup variables */
928 input_ControlVarNavigation( p_input );
929 input_SendEventTitle( p_input, 0 );
933 p_input->p->b_can_pace_control = p_master->b_can_pace_control;
934 p_input->p->b_can_pause = p_master->b_can_pause;
935 p_input->p->b_can_rate_control = p_master->b_can_rate_control;
938 static void StartTitle( input_thread_t * p_input )
942 /* Start title/chapter */
943 val.i_int = p_input->p->input.i_title_start -
944 p_input->p->input.i_title_offset;
945 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
946 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
948 val.i_int = p_input->p->input.i_seekpoint_start -
949 p_input->p->input.i_seekpoint_offset;
950 if( val.i_int > 0 /* TODO: check upper boundary */ )
951 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
953 /* Start/stop/run time */
954 p_input->p->i_start = (int64_t)(1000000.0
955 * var_GetFloat( p_input, "start-time" ));
956 p_input->p->i_stop = (int64_t)(1000000.0
957 * var_GetFloat( p_input, "stop-time" ));
958 p_input->p->i_run = (int64_t)(1000000.0
959 * var_GetFloat( p_input, "run-time" ));
960 if( p_input->p->i_run < 0 )
962 msg_Warn( p_input, "invalid run-time ignored" );
963 p_input->p->i_run = 0;
966 if( p_input->p->i_start > 0 )
970 msg_Dbg( p_input, "starting at time: %ds",
971 (int)( p_input->p->i_start / INT64_C(1000000) ) );
973 s.i_time = p_input->p->i_start;
974 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
976 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
978 msg_Warn( p_input, "invalid stop-time ignored" );
979 p_input->p->i_stop = 0;
981 p_input->p->b_fast_seek = var_GetBool( p_input, "input-fast-seek" );
984 static void LoadSubtitles( input_thread_t *p_input )
987 /* Get fps and set it if not already set */
988 const double f_fps = p_input->p->f_fps;
991 float f_requested_fps;
993 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
994 var_SetFloat( p_input, "sub-original-fps", f_fps );
996 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
997 if( f_requested_fps != f_fps )
999 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1000 VLC_VAR_DOINHERIT );
1001 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1005 const int i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1007 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1009 /* Look for and add subtitle files */
1010 bool b_forced = true;
1012 char *psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1013 if( psz_subtitle != NULL )
1015 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1016 SubtitleAdd( p_input, psz_subtitle, b_forced );
1020 if( var_GetBool( p_input, "sub-autodetect-file" ) )
1022 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1023 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1024 p_input->p->p_item->psz_uri );
1025 free( psz_autopath );
1027 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1029 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1031 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1035 free( ppsz_subs[i] );
1039 free( psz_subtitle );
1041 /* Load subtitles from attachments */
1042 int i_attachment = 0;
1043 input_attachment_t **pp_attachment = NULL;
1045 vlc_mutex_lock( &p_input->p->p_item->lock );
1046 for( int i = 0; i < p_input->p->i_attachment; i++ )
1048 const input_attachment_t *a = p_input->p->attachment[i];
1049 if( !strcmp( a->psz_mime, "application/x-srt" ) )
1050 TAB_APPEND( i_attachment, pp_attachment,
1051 vlc_input_attachment_New( a->psz_name, NULL,
1052 a->psz_description, NULL, 0 ) );
1054 vlc_mutex_unlock( &p_input->p->p_item->lock );
1056 if( i_attachment > 0 )
1057 var_Create( p_input, "sub-description", VLC_VAR_STRING );
1058 for( int i = 0; i < i_attachment; i++ )
1060 input_attachment_t *a = pp_attachment[i];
1064 if( a->psz_name[i] &&
1065 asprintf( &psz_mrl, "attachment://%s", a->psz_name ) >= 0 )
1067 var_SetString( p_input, "sub-description", a->psz_description ? a->psz_description : "");
1069 SubtitleAdd( p_input, psz_mrl, b_forced );
1074 vlc_input_attachment_Delete( a );
1076 free( pp_attachment );
1077 if( i_attachment > 0 )
1078 var_Destroy( p_input, "sub-description" );
1081 static void LoadSlaves( input_thread_t *p_input )
1083 char *psz = var_GetNonEmptyString( p_input, "input-slave" );
1087 char *psz_org = psz;
1088 while( psz && *psz )
1090 while( *psz == ' ' || *psz == '#' )
1093 char *psz_delim = strchr( psz, '#' );
1095 *psz_delim++ = '\0';
1100 msg_Dbg( p_input, "adding slave input '%s'", psz );
1102 input_source_t *p_slave = InputSourceNew( p_input );
1103 if( p_slave && !InputSourceInit( p_input, p_slave, psz, NULL ) )
1104 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, p_slave );
1113 static void UpdatePtsDelay( input_thread_t *p_input )
1115 input_thread_private_t *p_sys = p_input->p;
1117 /* Get max pts delay from input source */
1118 mtime_t i_pts_delay = p_sys->input.i_pts_delay;
1119 for( int i = 0; i < p_sys->i_slave; i++ )
1120 i_pts_delay = __MAX( i_pts_delay, p_sys->slave[i]->i_pts_delay );
1122 if( i_pts_delay < 0 )
1125 /* Take care of audio/spu delay */
1126 const mtime_t i_audio_delay = var_GetTime( p_input, "audio-delay" );
1127 const mtime_t i_spu_delay = var_GetTime( p_input, "spu-delay" );
1128 const mtime_t i_extra_delay = __MIN( i_audio_delay, i_spu_delay );
1129 if( i_extra_delay < 0 )
1130 i_pts_delay -= i_extra_delay;
1132 /* Update cr_average depending on the caching */
1133 const int i_cr_average = var_GetInteger( p_input, "cr-average" ) * i_pts_delay / DEFAULT_PTS_DELAY;
1136 es_out_SetJitter( p_input->p->p_es_out, i_pts_delay, i_cr_average );
1139 static void InitPrograms( input_thread_t * p_input )
1144 /* Compute correct pts_delay */
1145 UpdatePtsDelay( p_input );
1148 i_es_out_mode = ES_OUT_MODE_AUTO;
1149 if( p_input->p->p_sout )
1151 if( var_GetBool( p_input, "sout-all" ) )
1153 i_es_out_mode = ES_OUT_MODE_ALL;
1157 var_Get( p_input, "programs", &val );
1158 if( val.p_list && val.p_list->i_count )
1160 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1161 /* Note : we should remove the "program" callback. */
1165 var_FreeList( &val, NULL );
1169 es_out_SetMode( p_input->p->p_es_out, i_es_out_mode );
1171 /* Inform the demuxer about waited group (needed only for DVB) */
1172 if( i_es_out_mode == ES_OUT_MODE_ALL )
1174 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1176 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1178 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1183 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1184 (int) var_GetInteger( p_input, "program" ), NULL );
1188 static int Init( input_thread_t * p_input )
1193 for( i = 0; i < p_input->p->p_item->i_options; i++ )
1195 if( !strncmp( p_input->p->p_item->ppsz_options[i], "meta-file", 9 ) )
1197 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1198 var_SetString( p_input, "sout", "" );
1199 var_SetBool( p_input, "sout-all", false );
1200 var_SetString( p_input, "input-slave", "" );
1201 var_SetInteger( p_input, "input-repeat", 0 );
1202 var_SetString( p_input, "sub-file", "" );
1203 var_SetBool( p_input, "sub-autodetect-file", false );
1207 InitStatistics( p_input );
1209 if( InitSout( p_input ) )
1214 p_input->p->p_es_out = input_EsOutTimeshiftNew( p_input, p_input->p->p_es_out_display, p_input->p->i_rate );
1217 input_ChangeState( p_input, OPENING_S );
1218 input_SendEventCache( p_input, 0.0 );
1221 if( InputSourceInit( p_input, &p_input->p->input,
1222 p_input->p->p_item->psz_uri, NULL ) )
1227 InitTitle( p_input );
1229 /* Load master infos */
1232 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1236 i_length = input_item_GetDuration( p_input->p->p_item );
1237 input_SendEventLength( p_input, i_length );
1239 input_SendEventPosition( p_input, 0.0, 0 );
1241 if( !p_input->b_preparsing )
1243 StartTitle( p_input );
1244 LoadSubtitles( p_input );
1245 LoadSlaves( p_input );
1246 InitPrograms( p_input );
1249 if( !p_input->b_preparsing && p_input->p->p_sout )
1251 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1253 if( p_input->p->b_can_pace_control && p_input->p->b_out_pace_control )
1255 /* We don't want a high input priority here or we'll
1256 * end-up sucking up all the CPU time */
1257 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1260 msg_Dbg( p_input, "starting in %s mode",
1261 p_input->p->b_out_pace_control ? "async" : "sync" );
1264 p_meta = vlc_meta_New();
1267 /* Get meta data from users */
1268 InputMetaUser( p_input, p_meta );
1270 /* Get meta data from master input */
1271 InputSourceMeta( p_input, &p_input->p->input, p_meta );
1273 /* And from slave */
1274 for( int i = 0; i < p_input->p->i_slave; i++ )
1275 InputSourceMeta( p_input, p_input->p->slave[i], p_meta );
1278 InputUpdateMeta( p_input, p_meta );
1281 msg_Dbg( p_input, "`%s' successfully opened",
1282 p_input->p->p_item->psz_uri );
1284 /* initialization is complete */
1285 input_ChangeState( p_input, PLAYING_S );
1290 input_ChangeState( p_input, ERROR_S );
1292 if( p_input->p->p_es_out )
1293 es_out_Delete( p_input->p->p_es_out );
1294 es_out_SetMode( p_input->p->p_es_out_display, ES_OUT_MODE_END );
1295 if( p_input->p->p_resource )
1297 if( p_input->p->p_sout )
1298 input_resource_RequestSout( p_input->p->p_resource,
1299 p_input->p->p_sout, NULL );
1300 input_resource_SetInput( p_input->p->p_resource, NULL );
1303 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1305 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1306 stats_CounterClean( p_input->p->counters.p_##c );\
1307 p_input->p->counters.p_##c = NULL; } while(0)
1308 EXIT_COUNTER( read_bytes );
1309 EXIT_COUNTER( read_packets );
1310 EXIT_COUNTER( demux_read );
1311 EXIT_COUNTER( input_bitrate );
1312 EXIT_COUNTER( demux_bitrate );
1313 EXIT_COUNTER( demux_corrupted );
1314 EXIT_COUNTER( demux_discontinuity );
1315 EXIT_COUNTER( played_abuffers );
1316 EXIT_COUNTER( lost_abuffers );
1317 EXIT_COUNTER( displayed_pictures );
1318 EXIT_COUNTER( lost_pictures );
1319 EXIT_COUNTER( decoded_audio );
1320 EXIT_COUNTER( decoded_video );
1321 EXIT_COUNTER( decoded_sub );
1323 if( p_input->p->p_sout )
1325 EXIT_COUNTER( sout_sent_packets );
1326 EXIT_COUNTER( sout_sent_bytes );
1327 EXIT_COUNTER( sout_send_bitrate );
1332 /* Mark them deleted */
1333 p_input->p->input.p_demux = NULL;
1334 p_input->p->input.p_stream = NULL;
1335 p_input->p->input.p_access = NULL;
1336 p_input->p->p_es_out = NULL;
1337 p_input->p->p_sout = NULL;
1339 return VLC_EGENERIC;
1342 /*****************************************************************************
1343 * End: end the input thread
1344 *****************************************************************************/
1345 static void End( input_thread_t * p_input )
1349 /* We are at the end */
1350 input_ChangeState( p_input, END_S );
1352 /* Clean control variables */
1353 input_ControlVarStop( p_input );
1355 /* Stop es out activity */
1356 es_out_SetMode( p_input->p->p_es_out, ES_OUT_MODE_NONE );
1358 /* Clean up master */
1359 InputSourceClean( &p_input->p->input );
1362 for( i = 0; i < p_input->p->i_slave; i++ )
1364 InputSourceClean( p_input->p->slave[i] );
1365 free( p_input->p->slave[i] );
1367 free( p_input->p->slave );
1369 /* Unload all modules */
1370 if( p_input->p->p_es_out )
1371 es_out_Delete( p_input->p->p_es_out );
1372 es_out_SetMode( p_input->p->p_es_out_display, ES_OUT_MODE_END );
1374 if( !p_input->b_preparsing )
1376 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1377 if( libvlc_stats( p_input ) )
1379 /* make sure we are up to date */
1380 stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
1381 CL_CO( read_bytes );
1382 CL_CO( read_packets );
1383 CL_CO( demux_read );
1384 CL_CO( input_bitrate );
1385 CL_CO( demux_bitrate );
1386 CL_CO( demux_corrupted );
1387 CL_CO( demux_discontinuity );
1388 CL_CO( played_abuffers );
1389 CL_CO( lost_abuffers );
1390 CL_CO( displayed_pictures );
1391 CL_CO( lost_pictures );
1392 CL_CO( decoded_audio) ;
1393 CL_CO( decoded_video );
1394 CL_CO( decoded_sub) ;
1397 /* Close optional stream output instance */
1398 if( p_input->p->p_sout )
1400 CL_CO( sout_sent_packets );
1401 CL_CO( sout_sent_bytes );
1402 CL_CO( sout_send_bitrate );
1407 vlc_mutex_lock( &p_input->p->p_item->lock );
1408 if( p_input->p->i_attachment > 0 )
1410 for( i = 0; i < p_input->p->i_attachment; i++ )
1411 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1412 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1414 vlc_mutex_unlock( &p_input->p->p_item->lock );
1417 input_resource_RequestSout( p_input->p->p_resource,
1418 p_input->p->p_sout, NULL );
1419 input_resource_SetInput( p_input->p->p_resource, NULL );
1422 /*****************************************************************************
1424 *****************************************************************************/
1425 void input_ControlPush( input_thread_t *p_input,
1426 int i_type, vlc_value_t *p_val )
1428 vlc_mutex_lock( &p_input->p->lock_control );
1429 if( i_type == INPUT_CONTROL_SET_DIE )
1431 /* Special case, empty the control */
1432 for( int i = 0; i < p_input->p->i_control; i++ )
1434 input_control_t *p_ctrl = &p_input->p->control[i];
1435 ControlRelease( p_ctrl->i_type, p_ctrl->val );
1437 p_input->p->i_control = 0;
1440 if( p_input->p->i_control >= INPUT_CONTROL_FIFO_SIZE )
1442 msg_Err( p_input, "input control fifo overflow, trashing type=%d",
1445 ControlRelease( i_type, *p_val );
1454 memset( &c.val, 0, sizeof(c.val) );
1456 p_input->p->control[p_input->p->i_control++] = c;
1458 vlc_cond_signal( &p_input->p->wait_control );
1459 vlc_mutex_unlock( &p_input->p->lock_control );
1462 static int ControlGetReducedIndexLocked( input_thread_t *p_input )
1464 const int i_lt = p_input->p->control[0].i_type;
1466 for( i = 1; i < p_input->p->i_control; i++ )
1468 const int i_ct = p_input->p->control[i].i_type;
1471 ( i_ct == INPUT_CONTROL_SET_STATE ||
1472 i_ct == INPUT_CONTROL_SET_RATE ||
1473 i_ct == INPUT_CONTROL_SET_POSITION ||
1474 i_ct == INPUT_CONTROL_SET_TIME ||
1475 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1476 i_ct == INPUT_CONTROL_SET_TITLE ||
1477 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1478 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1484 /* TODO but that's not that important
1485 - merge SET_X with SET_X_CMD
1486 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1487 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1497 static inline int ControlPop( input_thread_t *p_input,
1498 int *pi_type, vlc_value_t *p_val,
1499 mtime_t i_deadline, bool b_postpone_seek )
1501 input_thread_private_t *p_sys = p_input->p;
1503 vlc_mutex_lock( &p_sys->lock_control );
1504 while( p_sys->i_control <= 0 ||
1505 ( b_postpone_seek && ControlIsSeekRequest( p_sys->control[0].i_type ) ) )
1507 if( !vlc_object_alive( p_input ) || i_deadline < 0 )
1509 vlc_mutex_unlock( &p_sys->lock_control );
1510 return VLC_EGENERIC;
1513 if( vlc_cond_timedwait( &p_sys->wait_control, &p_sys->lock_control,
1516 vlc_mutex_unlock( &p_sys->lock_control );
1517 return VLC_EGENERIC;
1522 const int i_index = ControlGetReducedIndexLocked( p_input );
1525 *pi_type = p_sys->control[i_index].i_type;
1526 *p_val = p_sys->control[i_index].val;
1528 p_sys->i_control -= i_index + 1;
1529 if( p_sys->i_control > 0 )
1530 memmove( &p_sys->control[0], &p_sys->control[i_index+1],
1531 sizeof(*p_sys->control) * p_sys->i_control );
1532 vlc_mutex_unlock( &p_sys->lock_control );
1536 static bool ControlIsSeekRequest( int i_type )
1540 case INPUT_CONTROL_SET_POSITION:
1541 case INPUT_CONTROL_SET_TIME:
1542 case INPUT_CONTROL_SET_TITLE:
1543 case INPUT_CONTROL_SET_TITLE_NEXT:
1544 case INPUT_CONTROL_SET_TITLE_PREV:
1545 case INPUT_CONTROL_SET_SEEKPOINT:
1546 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1547 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1548 case INPUT_CONTROL_SET_BOOKMARK:
1555 static void ControlRelease( int i_type, vlc_value_t val )
1559 case INPUT_CONTROL_ADD_SUBTITLE:
1560 case INPUT_CONTROL_ADD_SLAVE:
1561 free( val.psz_string );
1570 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1572 int i_ret = VLC_SUCCESS;
1573 int i_state = PAUSE_S;
1575 if( p_input->p->b_can_pause )
1577 if( p_input->p->input.p_access )
1578 i_ret = access_Control( p_input->p->input.p_access,
1579 ACCESS_SET_PAUSE_STATE, true );
1581 i_ret = demux_Control( p_input->p->input.p_demux,
1582 DEMUX_SET_PAUSE_STATE, true );
1586 msg_Warn( p_input, "cannot set pause state" );
1592 i_ret = es_out_SetPauseState( p_input->p->p_es_out,
1593 p_input->p->b_can_pause, true,
1597 msg_Warn( p_input, "cannot set pause state at es_out level" );
1601 /* Switch to new state */
1602 input_ChangeState( p_input, i_state );
1605 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1607 int i_ret = VLC_SUCCESS;
1609 if( p_input->p->b_can_pause )
1611 if( p_input->p->input.p_access )
1612 i_ret = access_Control( p_input->p->input.p_access,
1613 ACCESS_SET_PAUSE_STATE, false );
1615 i_ret = demux_Control( p_input->p->input.p_demux,
1616 DEMUX_SET_PAUSE_STATE, false );
1619 /* FIXME What to do ? */
1620 msg_Warn( p_input, "cannot unset pause -> EOF" );
1621 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1625 /* Switch to play */
1626 input_ChangeState( p_input, PLAYING_S );
1630 es_out_SetPauseState( p_input->p->p_es_out, false, false, i_control_date );
1633 static bool Control( input_thread_t *p_input,
1634 int i_type, vlc_value_t val )
1636 const mtime_t i_control_date = mdate();
1637 /* FIXME b_force_update is abused, it should be carefully checked */
1638 bool b_force_update = false;
1641 return b_force_update;
1645 case INPUT_CONTROL_SET_DIE:
1646 msg_Dbg( p_input, "control: stopping input" );
1648 /* Mark all submodules to die */
1649 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1652 case INPUT_CONTROL_SET_POSITION:
1656 if( p_input->p->b_recording )
1658 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1661 f_pos = val.f_float;
1662 if( i_type != INPUT_CONTROL_SET_POSITION )
1663 f_pos += var_GetFloat( p_input, "position" );
1666 else if( f_pos > 1.0 )
1668 /* Reset the decoders states and clock sync (before calling the demuxer */
1669 es_out_SetTime( p_input->p->p_es_out, -1 );
1670 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1671 f_pos, !p_input->p->b_fast_seek ) )
1673 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1674 "%2.1f%% failed", f_pos * 100 );
1678 if( p_input->p->i_slave > 0 )
1679 SlaveSeek( p_input );
1680 p_input->p->input.b_eof = false;
1682 b_force_update = true;
1687 case INPUT_CONTROL_SET_TIME:
1692 if( p_input->p->b_recording )
1694 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1698 i_time = val.i_time;
1699 if( i_type != INPUT_CONTROL_SET_TIME )
1700 i_time += var_GetTime( p_input, "time" );
1705 /* Reset the decoders states and clock sync (before calling the demuxer */
1706 es_out_SetTime( p_input->p->p_es_out, -1 );
1708 i_ret = demux_Control( p_input->p->input.p_demux,
1709 DEMUX_SET_TIME, i_time,
1710 !p_input->p->b_fast_seek );
1715 /* Emulate it with a SET_POS */
1716 if( !demux_Control( p_input->p->input.p_demux,
1717 DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
1719 double f_pos = (double)i_time / (double)i_length;
1720 i_ret = demux_Control( p_input->p->input.p_demux,
1721 DEMUX_SET_POSITION, f_pos,
1722 !p_input->p->b_fast_seek );
1727 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1728 " failed or not possible", i_time );
1732 if( p_input->p->i_slave > 0 )
1733 SlaveSeek( p_input );
1734 p_input->p->input.b_eof = false;
1736 b_force_update = true;
1741 case INPUT_CONTROL_SET_STATE:
1742 if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1743 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1744 else if( p_input->p->i_state == PAUSE_S )
1746 ControlUnpause( p_input, i_control_date );
1748 b_force_update = true;
1750 else if( val.i_int == PAUSE_S && p_input->p->i_state == PLAYING_S /* &&
1751 p_input->p->b_can_pause */ )
1753 ControlPause( p_input, i_control_date );
1755 b_force_update = true;
1757 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause && 0 )
1759 b_force_update = true;
1761 /* Correct "state" value */
1762 input_ChangeState( p_input, p_input->p->i_state );
1766 case INPUT_CONTROL_SET_RATE:
1767 case INPUT_CONTROL_SET_RATE_SLOWER:
1768 case INPUT_CONTROL_SET_RATE_FASTER:
1773 /* Get rate and direction */
1774 if( i_type == INPUT_CONTROL_SET_RATE )
1776 i_rate = abs( val.i_int );
1777 i_rate_sign = val.i_int < 0 ? -1 : 1;
1781 static const int ppi_factor[][2] = {
1782 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1784 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1791 i_rate_sign = p_input->p->i_rate < 0 ? -1 : 1;
1795 for( i = 0; ppi_factor[i][0] != 0; i++ )
1797 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1798 const int i_test_e = abs( abs( p_input->p->i_rate ) - i_test_r );
1799 if( i_test_e < i_error )
1806 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1808 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1810 if( ppi_factor[i_idx+1][0] > 0 )
1811 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1813 i_rate = INPUT_RATE_MAX+1;
1817 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1819 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1821 i_rate = INPUT_RATE_MIN-1;
1825 /* Check rate bound */
1826 if( i_rate < INPUT_RATE_MIN )
1828 msg_Dbg( p_input, "cannot set rate faster" );
1829 i_rate = INPUT_RATE_MIN;
1831 else if( i_rate > INPUT_RATE_MAX )
1833 msg_Dbg( p_input, "cannot set rate slower" );
1834 i_rate = INPUT_RATE_MAX;
1837 /* Apply direction */
1838 if( i_rate_sign < 0 )
1840 if( p_input->p->input.b_rescale_ts )
1842 msg_Dbg( p_input, "cannot set negative rate" );
1843 i_rate = p_input->p->i_rate;
1844 assert( i_rate > 0 );
1848 i_rate *= i_rate_sign;
1852 if( i_rate != INPUT_RATE_DEFAULT &&
1853 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1854 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1856 msg_Dbg( p_input, "cannot change rate" );
1857 i_rate = INPUT_RATE_DEFAULT;
1859 if( i_rate != p_input->p->i_rate &&
1860 !p_input->p->b_can_pace_control && p_input->p->b_can_rate_control )
1863 if( p_input->p->input.p_access )
1865 i_ret = VLC_EGENERIC;
1869 if( !p_input->p->input.b_rescale_ts )
1870 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1872 i_ret = demux_Control( p_input->p->input.p_demux,
1873 DEMUX_SET_RATE, &i_rate );
1877 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1878 i_rate = p_input->p->i_rate;
1883 if( i_rate != p_input->p->i_rate )
1885 p_input->p->i_rate = i_rate;
1886 input_SendEventRate( p_input, i_rate );
1888 if( p_input->p->input.b_rescale_ts )
1890 const int i_rate_source = (p_input->p->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1891 es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1894 b_force_update = true;
1899 case INPUT_CONTROL_SET_PROGRAM:
1900 /* No need to force update, es_out does it if needed */
1901 es_out_Control( p_input->p->p_es_out,
1902 ES_OUT_SET_GROUP, val.i_int );
1904 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1908 case INPUT_CONTROL_SET_ES:
1909 /* No need to force update, es_out does it if needed */
1910 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, val.i_int );
1913 case INPUT_CONTROL_RESTART_ES:
1914 es_out_Control( p_input->p->p_es_out_display, ES_OUT_RESTART_ES_BY_ID, val.i_int );
1917 case INPUT_CONTROL_SET_AUDIO_DELAY:
1918 if( !es_out_SetDelay( p_input->p->p_es_out_display, AUDIO_ES, val.i_time ) )
1920 input_SendEventAudioDelay( p_input, val.i_time );
1921 UpdatePtsDelay( p_input );
1925 case INPUT_CONTROL_SET_SPU_DELAY:
1926 if( !es_out_SetDelay( p_input->p->p_es_out_display, SPU_ES, val.i_time ) )
1928 input_SendEventSubtitleDelay( p_input, val.i_time );
1929 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 /* Remove HTML anchor if present (not supported). */
2406 char *p = strchr( psz_path, '#' );
2409 /* Then URI-decode the path. */
2410 decode_URI( psz_path );
2411 #if defined( WIN32 ) && !defined( UNDER_CE )
2412 /* Strip leading slash in front of the drive letter */
2415 #if (DIR_SEP_CHAR != '/')
2416 /* Turn slashes into anti-slashes */
2417 for( char *s = strchr( psz_path, '/' ); s; s = strchr( s + 1, '/' ) )
2422 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2423 psz_mrl, psz_access, psz_demux, psz_path );
2424 if( !p_input->b_preparsing )
2426 /* Hack to allow udp://@:port syntax */
2428 (strncmp( psz_access, "udp", 3 ) &&
2429 strncmp( psz_access, "rtp", 3 )) )
2431 /* Find optional titles and seekpoints */
2432 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2433 &in->i_seekpoint_start, &in->i_seekpoint_end );
2436 if( psz_forced_demux && *psz_forced_demux )
2438 psz_demux = psz_forced_demux;
2440 else if( *psz_demux == '\0' )
2442 /* special hack for forcing a demuxer with --demux=module
2443 * (and do nothing with a list) */
2444 psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2446 if( psz_var_demux != NULL &&
2447 !strchr(psz_var_demux, ',' ) &&
2448 !strchr(psz_var_demux, ':' ) )
2450 psz_demux = psz_var_demux;
2452 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2456 /* Try access_demux first */
2457 in->p_demux = demux_New( p_input, p_input, psz_access, psz_demux, psz_path,
2458 NULL, p_input->p->p_es_out, false );
2462 /* Preparsing is only for file:// */
2465 if( !*psz_access ) /* path without scheme:// */
2466 psz_access = "file";
2467 if( strcmp( psz_access, "file" ) )
2469 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2474 /* Get infos from access_demux */
2475 int i_ret = demux_Control( in->p_demux,
2476 DEMUX_GET_PTS_DELAY, &in->i_pts_delay );
2478 in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2481 in->b_title_demux = true;
2482 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2483 &in->title, &in->i_title,
2484 &in->i_title_offset, &in->i_seekpoint_offset ) )
2486 TAB_INIT( in->i_title, in->title );
2488 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2489 &in->b_can_pace_control ) )
2490 in->b_can_pace_control = false;
2492 assert( in->p_demux->pf_demux != NULL || !in->b_can_pace_control );
2494 if( !in->b_can_pace_control )
2496 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2497 &in->b_can_rate_control, &in->b_rescale_ts ) )
2499 in->b_can_rate_control = false;
2500 in->b_rescale_ts = true; /* not used */
2505 in->b_can_rate_control = true;
2506 in->b_rescale_ts = true;
2508 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2509 &in->b_can_pause ) )
2510 in->b_can_pause = false;
2511 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2512 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2513 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control && in->b_can_rate_control );
2516 if( demux_Control( in->p_demux, DEMUX_CAN_SEEK, &b_can_seek ) )
2518 var_SetBool( p_input, "can-seek", b_can_seek );
2522 /* Now try a real access */
2523 in->p_access = access_New( p_input, p_input, psz_access, psz_demux, psz_path );
2524 if( in->p_access == NULL )
2526 if( vlc_object_alive( p_input ) )
2528 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2530 dialog_Fatal( p_input, _("Your input can't be opened"),
2531 _("VLC is unable to open the MRL '%s'."
2532 " Check the log for details."), psz_mrl );
2537 /* Get infos from access */
2538 if( !p_input->b_preparsing )
2541 access_Control( in->p_access,
2542 ACCESS_GET_PTS_DELAY, &in->i_pts_delay );
2543 in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2545 in->b_title_demux = false;
2546 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2547 &in->title, &in->i_title,
2548 &in->i_title_offset, &in->i_seekpoint_offset ) )
2551 TAB_INIT( in->i_title, in->title );
2553 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2554 &in->b_can_pace_control );
2555 in->b_can_rate_control = in->b_can_pace_control;
2556 in->b_rescale_ts = true;
2558 access_Control( in->p_access, ACCESS_CAN_PAUSE, &in->b_can_pause );
2559 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2560 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2561 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2563 access_Control( in->p_access, ACCESS_CAN_SEEK, &b_can_seek );
2564 var_SetBool( p_input, "can-seek", b_can_seek );
2569 char **ppsz_input_list;
2571 TAB_INIT( i_input_list, ppsz_input_list );
2573 /* On master stream only, use input-list */
2574 if( &p_input->p->input == in )
2580 psz_parser = var_CreateGetNonEmptyString( p_input, "input-list" );
2582 while( psz_parser && *psz_parser )
2584 char *p = strchr( psz_parser, ',' );
2590 char *psz_name = strdup( psz_parser );
2592 TAB_APPEND( i_input_list, ppsz_input_list, psz_name );
2599 /* Autodetect extra files if none specified */
2600 if( i_input_list <= 0 )
2602 InputGetExtraFiles( p_input, &i_input_list, &ppsz_input_list,
2603 psz_access, psz_path );
2605 if( i_input_list > 0 )
2606 TAB_APPEND( i_input_list, ppsz_input_list, NULL );
2608 /* Create the stream_t */
2609 in->p_stream = stream_AccessNew( in->p_access, ppsz_input_list );
2610 if( ppsz_input_list )
2612 for( int i = 0; ppsz_input_list[i] != NULL; i++ )
2613 free( ppsz_input_list[i] );
2614 TAB_CLEAN( i_input_list, ppsz_input_list );
2617 if( in->p_stream == NULL )
2619 msg_Warn( p_input, "cannot create a stream_t from access" );
2623 /* Add stream filters */
2624 char *psz_stream_filter = var_GetNonEmptyString( p_input,
2626 in->p_stream = stream_FilterChainNew( in->p_stream,
2628 var_GetBool( p_input, "input-record-native" ) );
2629 free( psz_stream_filter );
2631 /* Open a demuxer */
2632 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2634 psz_demux = in->p_access->psz_demux;
2638 /* Take access/stream redirections into account */
2639 char *psz_real_path;
2640 char *psz_buf = NULL;
2641 if( in->p_stream->psz_path )
2643 const char *psz_a, *psz_d;
2644 psz_buf = strdup( in->p_stream->psz_path );
2645 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2649 psz_real_path = psz_path;
2651 in->p_demux = demux_New( p_input, p_input, psz_access, psz_demux,
2653 in->p_stream, p_input->p->p_es_out,
2654 p_input->b_preparsing );
2658 if( in->p_demux == NULL )
2660 if( vlc_object_alive( p_input ) )
2662 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2663 psz_access, psz_demux, psz_path );
2664 dialog_Fatal( VLC_OBJECT( p_input ),
2665 _("VLC can't recognize the input's format"),
2666 _("The format of '%s' cannot be detected. "
2667 "Have a look at the log for details."), psz_mrl );
2671 assert( in->p_demux->pf_demux != NULL );
2673 /* Get title from demux */
2674 if( !p_input->b_preparsing && in->i_title <= 0 )
2676 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2677 &in->title, &in->i_title,
2678 &in->i_title_offset, &in->i_seekpoint_offset ))
2680 TAB_INIT( in->i_title, in->title );
2684 in->b_title_demux = true;
2689 free( psz_var_demux );
2692 /* Set record capabilities */
2693 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2694 in->b_can_stream_record = false;
2696 if( !var_GetBool( p_input, "input-record-native" ) )
2697 in->b_can_stream_record = false;
2698 var_SetBool( p_input, "can-record", true );
2700 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2704 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2705 if( 1 || !p_input->b_preparsing )
2708 input_attachment_t **attachment;
2709 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2710 &attachment, &i_attachment ) )
2712 vlc_mutex_lock( &p_input->p->p_item->lock );
2713 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2714 i_attachment, attachment );
2715 vlc_mutex_unlock( &p_input->p->p_item->lock );
2718 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) && f_fps > 0.0 )
2720 vlc_mutex_lock( &p_input->p->p_item->lock );
2721 p_input->p->f_fps = f_fps;
2722 vlc_mutex_unlock( &p_input->p->p_item->lock );
2725 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2726 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2732 demux_Delete( in->p_demux );
2735 stream_Delete( in->p_stream );
2738 access_Delete( in->p_access );
2740 free( psz_var_demux );
2743 return VLC_EGENERIC;
2746 /*****************************************************************************
2748 *****************************************************************************/
2749 static void InputSourceClean( input_source_t *in )
2754 demux_Delete( in->p_demux );
2757 stream_Delete( in->p_stream );
2760 access_Delete( in->p_access );
2762 if( in->i_title > 0 )
2764 for( i = 0; i < in->i_title; i++ )
2765 vlc_input_title_Delete( in->title[i] );
2766 TAB_CLEAN( in->i_title, in->title );
2770 /*****************************************************************************
2772 *****************************************************************************/
2773 static void InputSourceMeta( input_thread_t *p_input,
2774 input_source_t *p_source, vlc_meta_t *p_meta )
2776 access_t *p_access = p_source->p_access;
2777 demux_t *p_demux = p_source->p_demux;
2779 /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
2784 /* Read access meta */
2785 has_meta = p_access && !access_Control( p_access, ACCESS_GET_META, p_meta );
2787 /* Read demux meta */
2788 has_meta |= !demux_Control( p_demux, DEMUX_GET_META, p_meta );
2790 bool has_unsupported;
2791 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &has_unsupported ) )
2792 has_unsupported = true;
2794 /* If the demux report unsupported meta data, or if we don't have meta data
2795 * try an external "meta reader" */
2796 if( has_meta && !has_unsupported )
2799 demux_meta_t *p_demux_meta =
2800 vlc_custom_create( p_demux, sizeof( *p_demux_meta ),
2801 VLC_OBJECT_GENERIC, "demux meta" );
2804 vlc_object_attach( p_demux_meta, p_demux );
2805 p_demux_meta->p_demux = p_demux;
2806 p_demux_meta->p_item = p_input->p->p_item;
2808 module_t *p_id3 = module_need( p_demux_meta, "meta reader", NULL, false );
2811 if( p_demux_meta->p_meta )
2813 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2814 vlc_meta_Delete( p_demux_meta->p_meta );
2817 if( p_demux_meta->i_attachments > 0 )
2819 vlc_mutex_lock( &p_input->p->p_item->lock );
2820 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2821 p_demux_meta->i_attachments, p_demux_meta->attachments );
2822 vlc_mutex_unlock( &p_input->p->p_item->lock );
2824 module_unneed( p_demux, p_id3 );
2826 vlc_object_release( p_demux_meta );
2830 static void SlaveDemux( input_thread_t *p_input, bool *pb_demux_polled )
2835 *pb_demux_polled = false;
2836 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2838 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2842 for( i = 0; i < p_input->p->i_slave; i++ )
2844 input_source_t *in = p_input->p->slave[i];
2850 const bool b_demux_polled = in->p_demux->pf_demux != NULL;
2851 if( !b_demux_polled )
2854 *pb_demux_polled = true;
2856 /* Call demux_Demux until we have read enough data */
2857 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2862 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2864 msg_Err( p_input, "slave[%d] doesn't like "
2865 "DEMUX_GET_TIME -> EOF", i );
2870 if( i_stime >= i_time )
2876 if( ( i_ret = demux_Demux( in->p_demux ) ) <= 0 )
2882 i_ret = demux_Demux( in->p_demux );
2887 msg_Dbg( p_input, "slave %d EOF", i );
2893 static void SlaveSeek( input_thread_t *p_input )
2898 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2900 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2904 for( i = 0; i < p_input->p->i_slave; i++ )
2906 input_source_t *in = p_input->p->slave[i];
2908 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time, true ) )
2911 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2921 /*****************************************************************************
2923 *****************************************************************************/
2924 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2926 static const struct { int i_meta; const char *psz_name; } p_list[] = {
2927 { vlc_meta_Title, "meta-title" },
2928 { vlc_meta_Artist, "meta-artist" },
2929 { vlc_meta_Genre, "meta-genre" },
2930 { vlc_meta_Copyright, "meta-copyright" },
2931 { vlc_meta_Description, "meta-description" },
2932 { vlc_meta_Date, "meta-date" },
2933 { vlc_meta_URL, "meta-url" },
2937 /* Get meta information from user */
2938 for( int i = 0; p_list[i].psz_name; i++ )
2940 char *psz_string = var_GetNonEmptyString( p_input, p_list[i].psz_name );
2944 EnsureUTF8( psz_string );
2945 vlc_meta_Set( p_meta, p_list[i].i_meta, psz_string );
2950 /*****************************************************************************
2951 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2952 * arturl and locking issue.
2953 *****************************************************************************/
2954 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2956 es_out_ControlSetMeta( p_input->p->p_es_out, p_meta );
2957 vlc_meta_Delete( p_meta );
2960 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2961 int i_new, input_attachment_t **pp_new )
2963 int i_attachment = *pi_attachment;
2964 input_attachment_t **attachment = *ppp_attachment;
2967 attachment = xrealloc( attachment,
2968 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2969 for( i = 0; i < i_new; i++ )
2970 attachment[i_attachment++] = pp_new[i];
2974 *pi_attachment = i_attachment;
2975 *ppp_attachment = attachment;
2977 /*****************************************************************************
2978 * InputGetExtraFiles
2979 * Autodetect extra input list
2980 *****************************************************************************/
2981 static void InputGetExtraFilesPattern( input_thread_t *p_input,
2982 int *pi_list, char ***pppsz_list,
2983 const char *psz_path,
2984 const char *psz_match,
2985 const char *psz_format,
2986 int i_start, int i_stop )
2991 TAB_INIT( i_list, ppsz_list );
2993 char *psz_base = strdup( psz_path );
2997 /* Remove the extension */
2998 char *psz_end = &psz_base[strlen(psz_base)-strlen(psz_match)];
2999 assert( psz_end >= psz_base);
3002 /* Try to list files */
3003 for( int i = i_start; i <= i_stop; i++ )
3008 if( asprintf( &psz_file, psz_format, psz_base, i ) < 0 )
3011 if( utf8_stat( psz_file, &st ) || !S_ISREG( st.st_mode ) || !st.st_size )
3017 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
3018 TAB_APPEND( i_list, ppsz_list, psz_file );
3023 *pppsz_list = ppsz_list;
3026 static void InputGetExtraFiles( input_thread_t *p_input,
3027 int *pi_list, char ***pppsz_list,
3028 const char *psz_access, const char *psz_path )
3032 const char *psz_match;
3033 const char *psz_format;
3037 /* XXX the order is important */
3038 { ".001", "%s.%.3d", 2, 999 },
3039 { ".part1.rar", "%s.part%.1d.rar",2, 9 },
3040 { ".part01.rar", "%s.part%.2d.rar",2, 99, },
3041 { ".part001.rar", "%s.part%.3d.rar",2, 999 },
3042 { ".rar", "%s.r%.2d", 0, 99 },
3043 { NULL, NULL, 0, 0 }
3046 TAB_INIT( *pi_list, *pppsz_list );
3048 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
3051 const size_t i_path = strlen(psz_path);
3053 for( int i = 0; p_pattern[i].psz_match != NULL; i++ )
3055 const size_t i_ext = strlen(p_pattern[i].psz_match );
3057 if( i_path < i_ext )
3059 if( !strcmp( &psz_path[i_path-i_ext], p_pattern[i].psz_match ) )
3061 InputGetExtraFilesPattern( p_input, pi_list, pppsz_list,
3063 p_pattern[i].psz_match, p_pattern[i].psz_format,
3064 p_pattern[i].i_start, p_pattern[i].i_stop );
3071 static void input_ChangeState( input_thread_t *p_input, int i_state )
3073 const bool b_changed = p_input->p->i_state != i_state;
3075 p_input->p->i_state = i_state;
3076 if( i_state == ERROR_S )
3077 p_input->b_error = true;
3078 else if( i_state == END_S )
3079 p_input->b_eof = true;
3083 input_item_SetErrorWhenReading( p_input->p->p_item, p_input->b_error );
3084 input_SendEventState( p_input, i_state );
3089 /*****************************************************************************
3090 * MRLSplit: parse the access, demux and url part of the
3091 * Media Resource Locator.
3092 *****************************************************************************/
3093 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
3096 char *psz_access = NULL;
3097 char *psz_demux = NULL;
3100 /* Either there is an access/demux specification before ://
3101 * or we have a plain local file path. */
3102 psz_path = strstr( psz_dup, "://" );
3103 if( psz_path != NULL )
3106 psz_path += 3; /* skips "://" */
3108 /* Separate access from demux (<access>/<demux>://<path>) */
3109 psz_access = psz_dup;
3110 psz_demux = strchr( psz_access, '/' );
3112 *psz_demux++ = '\0';
3114 /* We really don't want module name substitution here! */
3115 if( psz_access[0] == '$' )
3117 if( psz_demux && psz_demux[0] == '$' )
3124 *ppsz_access = psz_access ? psz_access : "";
3125 *ppsz_demux = psz_demux ? psz_demux : "";
3126 *ppsz_path = psz_path;
3129 static inline bool next(char ** src)
3133 long result = strtol( *src, &end, 0 );
3134 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
3143 /*****************************************************************************
3144 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3147 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
3148 *****************************************************************************/
3149 static void MRLSections( input_thread_t *p_input, char *psz_source,
3150 int *pi_title_start, int *pi_title_end,
3151 int *pi_chapter_start, int *pi_chapter_end )
3153 char *psz, *psz_end, *psz_next, *psz_check;
3155 *pi_title_start = *pi_title_end = -1;
3156 *pi_chapter_start = *pi_chapter_end = -1;
3158 /* Start by parsing titles and chapters */
3159 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
3162 /* Check we are really dealing with a title/chapter section */
3163 psz_check = psz + 1;
3164 if( !*psz_check ) return;
3165 if( isdigit(*psz_check) )
3166 if(!next(&psz_check)) return;
3167 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
3168 if( *psz_check == ':' && ++psz_check )
3170 if( isdigit(*psz_check) )
3171 if(!next(&psz_check)) return;
3173 if( *psz_check != '-' && *psz_check ) return;
3174 if( *psz_check == '-' && ++psz_check )
3176 if( isdigit(*psz_check) )
3177 if(!next(&psz_check)) return;
3179 if( *psz_check != ':' && *psz_check ) return;
3180 if( *psz_check == ':' && ++psz_check )
3182 if( isdigit(*psz_check) )
3183 if(!next(&psz_check)) return;
3185 if( *psz_check ) return;
3187 /* Separate start and end */
3189 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
3191 /* Look for the start title */
3192 *pi_title_start = strtol( psz, &psz_next, 0 );
3193 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
3194 *pi_title_end = *pi_title_start;
3197 /* Look for the start chapter */
3199 *pi_chapter_start = strtol( psz, &psz_next, 0 );
3200 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
3201 *pi_chapter_end = *pi_chapter_start;
3205 /* Look for the end title */
3206 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3207 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3210 /* Look for the end chapter */
3211 if( *psz_end ) psz_end++;
3212 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3213 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3216 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3217 psz_source, *pi_title_start, *pi_chapter_start,
3218 *pi_title_end, *pi_chapter_end );
3221 /*****************************************************************************
3222 * input_AddSubtitles: add a subtitles file and enable it
3223 *****************************************************************************/
3224 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3226 input_source_t *sub;
3229 char *psz_path, *psz_extension;
3231 /* if we are provided a subtitle.sub file,
3232 * see if we don't have a subtitle.idx and use it instead */
3233 psz_path = strdup( psz_subtitle );
3236 psz_extension = strrchr( psz_path, '.');
3237 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3241 strcpy( psz_extension, ".idx" );
3243 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3245 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3246 psz_path, psz_subtitle );
3247 strcpy( psz_subtitle, psz_path );
3253 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3255 sub = InputSourceNew( p_input );
3256 if( !sub || InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3261 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3264 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3266 if( count.i_int == 0 )
3268 /* if it was first one, there is disable too */
3270 if( count.i_int < list.p_list->i_count )
3272 const int i_id = list.p_list->p_values[count.i_int].i_int;
3274 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3275 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3277 var_FreeList( &list, NULL );
3281 /*****************************************************************************
3283 *****************************************************************************/
3284 void input_UpdateStatistic( input_thread_t *p_input,
3285 input_statistic_t i_type, int i_delta )
3287 assert( p_input->p->i_state != INIT_S );
3289 vlc_mutex_lock( &p_input->p->counters.counters_lock);
3292 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
3293 case INPUT_STATISTIC_DECODED_VIDEO:
3296 case INPUT_STATISTIC_DECODED_AUDIO:
3299 case INPUT_STATISTIC_DECODED_SUBTITLE:
3302 case INPUT_STATISTIC_SENT_PACKET:
3303 I(p_sout_sent_packets);
3306 case INPUT_STATISTIC_SENT_BYTE:
3308 int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
3310 if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
3311 stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
3315 msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3318 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
3322 /* TODO FIXME nearly the same logic that snapshot code */
3323 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3328 path = utf8_opendir( psz_path );
3333 char *psz_tmp = str_format( p_obj, psz_prefix );
3337 char *psz_tmp2 = filename_sanitize( psz_tmp );
3341 asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3343 psz_extension ? "." : "",
3344 psz_extension ? psz_extension : "" ) < 0 )
3351 psz_file = str_format( p_obj, psz_path );
3352 path_sanitize( psz_file );