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 );
66 static void *RunAndDestroy ( 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 );
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 );
77 static void ControlReduce( input_thread_t * );
78 static void ControlRelease( int i_type, vlc_value_t val );
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 );
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 */
113 /* Do not let a pts_delay from access/demux go beyong 60s */
114 #define INPUT_PTS_DELAY_MAX INT64_C(60000000)
116 /*****************************************************************************
117 * This function creates a new input, and returns a pointer
118 * to its description. On error, it returns NULL.
120 * XXX Do not forget to update vlc_input.h if you add new variables.
121 *****************************************************************************/
122 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
123 const char *psz_header, bool b_quick,
124 input_resource_t *p_resource )
126 static const char input_name[] = "input";
127 input_thread_t *p_input = NULL; /* thread descriptor */
130 /* Allocate descriptor */
131 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
132 VLC_OBJECT_INPUT, input_name );
133 if( p_input == NULL )
136 /* Construct a nice name for the input timer */
137 char psz_timer_name[255];
138 char * psz_name = input_item_GetName( p_item );
139 snprintf( psz_timer_name, sizeof(psz_timer_name),
140 "input launching for '%s'", psz_name );
142 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
146 /* Start a timer to mesure how long it takes
147 * to launch an input */
148 stats_TimerStart( p_input, psz_timer_name,
149 STATS_TIMER_INPUT_LAUNCHING );
151 p_input->p = calloc( 1, sizeof( input_thread_private_t ) );
155 p_input->b_preparsing = b_quick;
156 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
158 /* Init Common fields */
159 p_input->b_eof = false;
160 p_input->p->b_can_pace_control = true;
161 p_input->p->i_start = 0;
162 p_input->p->i_time = 0;
163 p_input->p->i_stop = 0;
164 p_input->p->i_run = 0;
165 p_input->p->i_title = 0;
166 p_input->p->title = NULL;
167 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
168 p_input->p->i_state = INIT_S;
169 p_input->p->i_rate = INPUT_RATE_DEFAULT;
170 p_input->p->b_recording = false;
171 memset( &p_input->p->bookmark, 0, sizeof(p_input->p->bookmark) );
172 TAB_INIT( p_input->p->i_bookmark, p_input->p->pp_bookmark );
173 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
174 p_input->p->p_es_out_display = NULL;
175 p_input->p->p_es_out = NULL;
176 p_input->p->p_sout = NULL;
177 p_input->p->b_out_pace_control = false;
179 vlc_gc_incref( p_item ); /* Released in Destructor() */
180 p_input->p->p_item = p_item;
182 /* Init Input fields */
183 p_input->p->input.p_access = NULL;
184 p_input->p->input.p_stream = NULL;
185 p_input->p->input.p_demux = NULL;
186 p_input->p->input.b_title_demux = false;
187 p_input->p->input.i_title = 0;
188 p_input->p->input.title = NULL;
189 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
190 p_input->p->input.b_can_pace_control = true;
191 p_input->p->input.b_can_rate_control = true;
192 p_input->p->input.b_rescale_ts = true;
193 p_input->p->input.b_eof = false;
195 vlc_mutex_lock( &p_item->lock );
197 if( !p_item->p_stats )
198 p_item->p_stats = stats_NewInputStats( p_input );
199 vlc_mutex_unlock( &p_item->lock );
202 p_input->p->i_slave = 0;
203 p_input->p->slave = NULL;
207 p_input->p->p_resource = p_resource;
209 p_input->p->p_resource = input_resource_New();
210 input_resource_SetInput( p_input->p->p_resource, p_input );
212 /* Init control buffer */
213 vlc_mutex_init( &p_input->p->lock_control );
214 vlc_cond_init( &p_input->p->wait_control );
215 p_input->p->i_control = 0;
216 p_input->p->b_abort = false;
218 /* Parse input options */
219 vlc_mutex_lock( &p_item->lock );
220 assert( (int)p_item->optflagc == p_item->i_options );
221 for( i = 0; i < p_item->i_options; i++ )
222 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
223 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
224 vlc_mutex_unlock( &p_item->lock );
226 /* Create Object Variables for private use only */
227 input_ConfigVarInit( p_input );
229 /* Create Objects variables for public Get and Set */
230 input_ControlVarInit( p_input );
233 if( !p_input->b_preparsing )
235 char *psz_bookmarks = var_GetNonEmptyString( p_input, "bookmarks" );
238 /* FIXME: have a common cfg parsing routine used by sout and others */
239 char *psz_parser, *psz_start, *psz_end;
240 psz_parser = psz_bookmarks;
241 while( (psz_start = strchr( psz_parser, '{' ) ) )
243 seekpoint_t *p_seekpoint;
246 psz_end = strchr( psz_start, '}' );
247 if( !psz_end ) break;
248 psz_parser = psz_end + 1;
249 backup = *psz_parser;
253 p_seekpoint = vlc_seekpoint_New();
254 while( (psz_end = strchr( psz_start, ',' ) ) )
257 if( !strncmp( psz_start, "name=", 5 ) )
259 p_seekpoint->psz_name = strdup(psz_start + 5);
261 else if( !strncmp( psz_start, "bytes=", 6 ) )
263 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
265 else if( !strncmp( psz_start, "time=", 5 ) )
267 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
270 psz_start = psz_end + 1;
272 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
273 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
274 p_seekpoint->i_time_offset );
275 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
276 vlc_seekpoint_Delete( p_seekpoint );
277 *psz_parser = backup;
279 free( psz_bookmarks );
283 /* Remove 'Now playing' info as it is probably outdated */
284 input_item_SetNowPlaying( p_item, NULL );
285 input_SendEventMeta( p_input );
288 if( p_input->b_preparsing )
289 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
292 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
293 vlc_mutex_init( &p_input->p->counters.counters_lock );
295 /* Set the destructor when we are sure we are initialized */
296 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
298 /* Attach only once we are ready */
299 vlc_object_attach( p_input, p_parent );
305 * Input destructor (called when the object's refcount reaches 0).
307 static void Destructor( input_thread_t * p_input )
310 char * psz_name = input_item_GetName( p_input->p->p_item );
311 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
315 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
316 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
318 if( p_input->p->p_resource )
319 input_resource_Delete( p_input->p->p_resource );
321 vlc_gc_decref( p_input->p->p_item );
323 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
325 for( int i = 0; i < p_input->p->i_control; i++ )
327 input_control_t *p_ctrl = &p_input->p->control[i];
328 ControlRelease( p_ctrl->i_type, p_ctrl->val );
331 vlc_cond_destroy( &p_input->p->wait_control );
332 vlc_mutex_destroy( &p_input->p->lock_control );
337 * Initialize an input thread and run it. You will need to monitor the
338 * thread to clean up after it is done
340 * \param p_parent a vlc_object
341 * \param p_item an input item
342 * \return a pointer to the spawned input thread
344 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
345 input_item_t *p_item )
347 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
351 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
352 input_item_t *p_item,
353 const char *psz_log, input_resource_t *p_resource )
355 input_thread_t *p_input;
357 p_input = Create( p_parent, p_item, psz_log, false, p_resource );
361 /* Create thread and wait for its readiness. */
362 if( vlc_thread_create( p_input, "input", Run,
363 VLC_THREAD_PRIORITY_INPUT ) )
365 input_ChangeState( p_input, ERROR_S );
366 msg_Err( p_input, "cannot create input thread" );
367 vlc_object_detach( p_input );
368 vlc_object_release( p_input );
376 * Initialize an input thread and run it. This thread will clean after itself,
377 * you can forget about it. It can work either in blocking or non-blocking mode
379 * \param p_parent a vlc_object
380 * \param p_item an input item
381 * \param b_block should we block until read is finished ?
382 * \return an error code, VLC_SUCCESS on success
384 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
387 input_thread_t *p_input;
389 p_input = Create( p_parent, p_item, NULL, false, NULL );
395 RunAndDestroy( VLC_OBJECT(p_input) );
400 if( vlc_thread_create( p_input, "input", RunAndDestroy,
401 VLC_THREAD_PRIORITY_INPUT ) )
403 input_ChangeState( p_input, ERROR_S );
404 msg_Err( p_input, "cannot create input thread" );
405 vlc_object_release( p_input );
413 * Initialize an input and initialize it to preparse the item
414 * This function is blocking. It will only accept parsing regular files.
416 * \param p_parent a vlc_object_t
417 * \param p_item an input item
418 * \return VLC_SUCCESS or an error
420 int input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
422 input_thread_t *p_input;
424 /* Allocate descriptor */
425 p_input = Create( p_parent, p_item, NULL, true, NULL );
429 if( !Init( p_input ) )
432 vlc_object_detach( p_input );
433 vlc_object_release( p_input );
439 * Request a running input thread to stop and die
441 * \param the input thread to stop
443 void input_StopThread( input_thread_t *p_input, bool b_abort )
445 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
446 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
447 * unlock the control loop */
448 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
450 vlc_mutex_lock( &p_input->p->lock_control );
451 p_input->p->b_abort |= b_abort;
452 vlc_mutex_unlock( &p_input->p->lock_control );
454 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
457 input_resource_t *input_DetachResource( input_thread_t *p_input )
459 assert( p_input->b_dead );
461 input_resource_SetInput( p_input->p->p_resource, NULL );
463 input_resource_t *p_resource = input_resource_Detach( p_input->p->p_resource );
464 p_input->p->p_sout = NULL;
470 * Get the item from an input thread
471 * FIXME it does not increase ref count of the item.
472 * if it is used after p_input is destroyed nothing prevent it from
475 input_item_t *input_GetItem( input_thread_t *p_input )
477 assert( p_input && p_input->p );
478 return p_input->p->p_item;
481 /*****************************************************************************
482 * ObjectKillChildrens
483 *****************************************************************************/
484 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
489 /* FIXME ObjectKillChildrens seems a very bad idea in fact */
490 i = vlc_internals( p_obj )->i_object_type;
491 if( i == VLC_OBJECT_VOUT ||i == VLC_OBJECT_AOUT ||
492 p_obj == VLC_OBJECT(p_input->p->p_sout) ||
493 i == VLC_OBJECT_DECODER || i == VLC_OBJECT_PACKETIZER )
496 vlc_object_kill( p_obj );
498 p_list = vlc_list_children( p_obj );
499 for( i = 0; i < p_list->i_count; i++ )
500 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
501 vlc_list_release( p_list );
504 /*****************************************************************************
505 * Run: main thread loop
506 * This is the "normal" thread that spawns the input processing chain,
507 * reads the stream, cleans up and waits
508 *****************************************************************************/
509 static void *Run( vlc_object_t *p_this )
511 input_thread_t *p_input = (input_thread_t *)p_this;
512 const int canc = vlc_savecancel();
514 if( Init( p_input ) )
523 /* Tell we're dead */
524 vlc_mutex_lock( &p_input->p->lock_control );
525 const bool b_abort = p_input->p->b_abort;
526 vlc_mutex_unlock( &p_input->p->lock_control );
529 input_SendEventAbort( p_input );
530 input_SendEventDead( p_input );
532 vlc_restorecancel( canc );
536 /*****************************************************************************
537 * RunAndDestroy: main thread loop
538 * This is the "just forget me" thread that spawns the input processing chain,
539 * reads the stream, cleans up and releases memory
540 *****************************************************************************/
541 static void *RunAndDestroy( vlc_object_t *p_this )
543 input_thread_t *p_input = (input_thread_t *)p_this;
544 const int canc = vlc_savecancel();
546 if( Init( p_input ) )
556 vlc_object_release( p_input );
557 vlc_restorecancel( canc );
561 /*****************************************************************************
562 * Main loop: Fill buffers from access, and demux
563 *****************************************************************************/
567 * It asks the demuxer to demux some data
569 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *pi_start_mdate )
575 if( ( p_input->p->i_stop > 0 && p_input->p->i_time >= p_input->p->i_stop ) ||
576 ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
579 i_ret = demux_Demux( p_input->p->input.p_demux );
583 if( p_input->p->input.p_demux->info.i_update )
585 if( p_input->p->input.b_title_demux )
587 i_ret = UpdateTitleSeekpointFromDemux( p_input );
590 UpdateGenericFromDemux( p_input );
592 else if( p_input->p->input.p_access &&
593 p_input->p->input.p_access->info.i_update )
595 if( !p_input->p->input.b_title_demux )
597 i_ret = UpdateTitleSeekpointFromAccess( p_input );
600 UpdateGenericFromAccess( p_input );
604 if( i_ret == 0 ) /* EOF */
606 int i_repeat = var_GetInteger( p_input, "input-repeat" );
609 /* End of file - we do not set b_die because only the
610 * playlist is allowed to do so. */
611 msg_Dbg( p_input, "EOF reached" );
612 p_input->p->input.b_eof = true;
618 msg_Dbg( p_input, "repeating the same input (%d)", i_repeat );
622 var_SetInteger( p_input, "input-repeat", i_repeat );
625 /* Seek to start title/seekpoint */
626 val.i_int = p_input->p->input.i_title_start -
627 p_input->p->input.i_title_offset;
628 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
630 input_ControlPush( p_input,
631 INPUT_CONTROL_SET_TITLE, &val );
633 val.i_int = p_input->p->input.i_seekpoint_start -
634 p_input->p->input.i_seekpoint_offset;
635 if( val.i_int > 0 /* TODO: check upper boundary */ )
636 input_ControlPush( p_input,
637 INPUT_CONTROL_SET_SEEKPOINT, &val );
639 /* Seek to start position */
640 if( p_input->p->i_start > 0 )
642 val.i_time = p_input->p->i_start;
643 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
649 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
654 *pi_start_mdate = mdate();
659 input_ChangeState( p_input, ERROR_S );
662 if( i_ret > 0 && p_input->p->i_slave > 0 )
664 SlaveDemux( p_input );
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 )
718 mtime_t i_start_mdate = mdate();
719 mtime_t i_intf_update = 0;
720 mtime_t i_statistic_update = 0;
722 /* Start the timer */
723 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
725 while( vlc_object_alive( p_input ) && !p_input->b_error )
736 b_force_update = false;
738 /* FIXME if p_input->p->i_state == PAUSE_S the access/access_demux
739 * is paused -> this may cause problem with some of them
740 * The same problem can be seen when seeking while paused */
741 b_paused = p_input->p->i_state == PAUSE_S &&
742 !es_out_GetBuffering( p_input->p->p_es_out );
746 if( !p_input->p->input.b_eof )
748 MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
750 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
752 else if( !p_input->b_eof && !es_out_GetEmpty( p_input->p->p_es_out ) )
754 msg_Dbg( p_input, "waiting decoder fifos to empty" );
755 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
765 i_deadline = i_wakeup;
767 i_deadline = __MIN( i_intf_update, i_statistic_update );
770 ControlReduce( p_input );
771 while( !ControlPop( p_input, &i_type, &val, i_deadline ) )
774 msg_Dbg( p_input, "control type=%d", i_type );
776 if( Control( p_input, i_type, val ) )
777 b_force_update = true;
780 /* Update interface and statistics */
782 if( i_intf_update < i_current || b_force_update )
784 MainLoopInterface( p_input );
785 i_intf_update = i_current + INT64_C(250000);
786 b_force_update = false;
788 if( i_statistic_update < i_current )
790 MainLoopStatistic( p_input );
791 i_statistic_update = i_current + INT64_C(1000000);
794 /* Check if i_wakeup is still valid */
797 mtime_t i_new_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
801 } while( i_current < i_wakeup );
804 if( !p_input->b_error )
805 input_ChangeState( p_input, END_S );
808 static void InitStatistics( input_thread_t * p_input )
810 if( p_input->b_preparsing ) return;
812 /* Prepare statistics */
813 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
814 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
815 if( libvlc_stats( p_input ) )
817 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
818 INIT_COUNTER( read_packets, INTEGER, COUNTER );
819 INIT_COUNTER( demux_read, INTEGER, COUNTER );
820 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
821 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
822 INIT_COUNTER( demux_corrupted, INTEGER, COUNTER );
823 INIT_COUNTER( demux_discontinuity, INTEGER, COUNTER );
824 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
825 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
826 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
827 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
828 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
829 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
830 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
831 p_input->p->counters.p_sout_send_bitrate = NULL;
832 p_input->p->counters.p_sout_sent_packets = NULL;
833 p_input->p->counters.p_sout_sent_bytes = NULL;
834 if( p_input->p->counters.p_demux_bitrate )
835 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
836 if( p_input->p->counters.p_input_bitrate )
837 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
842 static int InitSout( input_thread_t * p_input )
844 if( p_input->b_preparsing )
847 /* Find a usable sout and attach it to p_input */
848 char *psz = var_GetNonEmptyString( p_input, "sout" );
849 if( psz && strncasecmp( p_input->p->p_item->psz_uri, "vlc:", 4 ) )
851 p_input->p->p_sout = input_resource_RequestSout( p_input->p->p_resource, NULL, psz );
852 if( !p_input->p->p_sout )
854 input_ChangeState( p_input, ERROR_S );
855 msg_Err( p_input, "cannot start stream output instance, " \
860 if( libvlc_stats( p_input ) )
862 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
863 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
864 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
865 if( p_input->p->counters.p_sout_send_bitrate )
866 p_input->p->counters.p_sout_send_bitrate->update_interval =
872 input_resource_RequestSout( p_input->p->p_resource, NULL, NULL );
880 static void InitTitle( input_thread_t * p_input )
882 input_source_t *p_master = &p_input->p->input;
884 if( p_input->b_preparsing )
887 /* Create global title (from master) */
888 p_input->p->i_title = p_master->i_title;
889 p_input->p->title = p_master->title;
890 p_input->p->i_title_offset = p_master->i_title_offset;
891 p_input->p->i_seekpoint_offset = p_master->i_seekpoint_offset;
892 if( p_input->p->i_title > 0 )
894 /* Setup variables */
895 input_ControlVarNavigation( p_input );
896 input_SendEventTitle( p_input, 0 );
900 p_input->p->b_can_pace_control = p_master->b_can_pace_control;
901 p_input->p->b_can_pause = p_master->b_can_pause;
902 p_input->p->b_can_rate_control = p_master->b_can_rate_control;
905 static void StartTitle( input_thread_t * p_input )
909 /* Start title/chapter */
910 val.i_int = p_input->p->input.i_title_start -
911 p_input->p->input.i_title_offset;
912 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
913 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
915 val.i_int = p_input->p->input.i_seekpoint_start -
916 p_input->p->input.i_seekpoint_offset;
917 if( val.i_int > 0 /* TODO: check upper boundary */ )
918 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
920 /* Start/stop/run time */
921 p_input->p->i_start = (int64_t)(1000000.0
922 * var_GetFloat( p_input, "start-time" ));
923 p_input->p->i_stop = (int64_t)(1000000.0
924 * var_GetFloat( p_input, "stop-time" ));
925 p_input->p->i_run = (int64_t)(1000000.0
926 * var_GetFloat( p_input, "run-time" ));
927 if( p_input->p->i_run < 0 )
929 msg_Warn( p_input, "invalid run-time ignored" );
930 p_input->p->i_run = 0;
933 const mtime_t i_length = var_GetTime( p_input, "length" );
934 if( p_input->p->i_start > 0 )
936 if( p_input->p->i_start >= i_length )
938 msg_Warn( p_input, "invalid start-time ignored" );
944 msg_Dbg( p_input, "starting at time: %ds",
945 (int)( p_input->p->i_start / INT64_C(1000000) ) );
947 s.i_time = p_input->p->i_start;
948 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
951 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
953 msg_Warn( p_input, "invalid stop-time ignored" );
954 p_input->p->i_stop = 0;
956 p_input->p->b_fast_seek = var_GetBool( p_input, "input-fast-seek" );
959 static void LoadSubtitles( input_thread_t *p_input )
962 /* Get fps and set it if not already set */
963 const double f_fps = p_input->p->f_fps;
966 float f_requested_fps;
968 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
969 var_SetFloat( p_input, "sub-original-fps", f_fps );
971 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
972 if( f_requested_fps != f_fps )
974 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
976 var_SetFloat( p_input, "sub-fps", f_requested_fps );
980 const int i_delay = var_CreateGetInteger( p_input, "sub-delay" );
982 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
984 /* Look for and add subtitle files */
985 char *psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
986 if( psz_subtitle != NULL )
988 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
989 SubtitleAdd( p_input, psz_subtitle, true );
992 if( var_GetBool( p_input, "sub-autodetect-file" ) )
994 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
995 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
996 p_input->p->p_item->psz_uri );
997 free( psz_autopath );
999 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1001 /* Try to autoselect the first autodetected subtitles file
1002 * if no subtitles file was specified */
1003 bool b_forced = i == 0 && !psz_subtitle;
1005 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1006 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1008 free( ppsz_subs[i] );
1012 free( psz_subtitle );
1015 static void LoadSlaves( input_thread_t *p_input )
1017 char *psz = var_GetNonEmptyString( p_input, "input-slave" );
1021 char *psz_org = psz;
1022 while( psz && *psz )
1024 while( *psz == ' ' || *psz == '#' )
1027 char *psz_delim = strchr( psz, '#' );
1029 *psz_delim++ = '\0';
1034 msg_Dbg( p_input, "adding slave input '%s'", psz );
1036 input_source_t *p_slave = InputSourceNew( p_input );
1037 if( p_slave && !InputSourceInit( p_input, p_slave, psz, NULL ) )
1038 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, p_slave );
1047 static void UpdatePtsDelay( input_thread_t *p_input )
1049 input_thread_private_t *p_sys = p_input->p;
1051 /* Get max pts delay from input source */
1052 mtime_t i_pts_delay = p_sys->input.i_pts_delay;
1053 for( int i = 0; i < p_sys->i_slave; i++ )
1054 i_pts_delay = __MAX( i_pts_delay, p_sys->slave[i]->i_pts_delay );
1056 if( i_pts_delay < 0 )
1059 /* Take care of audio/spu delay */
1060 const mtime_t i_audio_delay = var_GetTime( p_input, "audio-delay" );
1061 const mtime_t i_spu_delay = var_GetTime( p_input, "spu-delay" );
1062 const mtime_t i_extra_delay = __MIN( i_audio_delay, i_spu_delay );
1063 if( i_extra_delay < 0 )
1064 i_pts_delay -= i_extra_delay;
1066 /* Update cr_average depending on the caching */
1067 const int i_cr_average = var_GetInteger( p_input, "cr-average" ) * i_pts_delay / DEFAULT_PTS_DELAY;
1070 es_out_SetJitter( p_input->p->p_es_out, i_pts_delay, i_cr_average );
1073 static void InitPrograms( input_thread_t * p_input )
1078 /* Compute correct pts_delay */
1079 UpdatePtsDelay( p_input );
1082 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1083 i_es_out_mode = ES_OUT_MODE_AUTO;
1085 if( p_input->p->p_sout )
1087 if( var_GetBool( p_input, "sout-all" ) )
1089 i_es_out_mode = ES_OUT_MODE_ALL;
1094 var_Get( p_input, "programs", &val );
1095 if( val.p_list && val.p_list->i_count )
1097 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1098 /* Note : we should remove the "program" callback. */
1102 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1107 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1109 /* Inform the demuxer about waited group (needed only for DVB) */
1110 if( i_es_out_mode == ES_OUT_MODE_ALL )
1112 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1114 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1116 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1121 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1122 (int) var_GetInteger( p_input, "program" ), NULL );
1126 static int Init( input_thread_t * p_input )
1131 for( i = 0; i < p_input->p->p_item->i_options; i++ )
1133 if( !strncmp( p_input->p->p_item->ppsz_options[i], "meta-file", 9 ) )
1135 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1136 var_SetString( p_input, "sout", "" );
1137 var_SetBool( p_input, "sout-all", false );
1138 var_SetString( p_input, "input-slave", "" );
1139 var_SetInteger( p_input, "input-repeat", 0 );
1140 var_SetString( p_input, "sub-file", "" );
1141 var_SetBool( p_input, "sub-autodetect-file", false );
1145 InitStatistics( p_input );
1147 ret = InitSout( p_input );
1148 if( ret != VLC_SUCCESS )
1153 p_input->p->p_es_out_display = input_EsOutNew( p_input, p_input->p->i_rate );
1154 p_input->p->p_es_out = input_EsOutTimeshiftNew( p_input, p_input->p->p_es_out_display, p_input->p->i_rate );
1155 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1156 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1158 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1159 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1162 input_ChangeState( p_input, OPENING_S );
1163 input_SendEventCache( p_input, 0.0 );
1166 if( InputSourceInit( p_input, &p_input->p->input,
1167 p_input->p->p_item->psz_uri, NULL ) )
1172 InitTitle( p_input );
1174 /* Load master infos */
1177 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1181 i_length = input_item_GetDuration( p_input->p->p_item );
1182 input_SendEventTimes( p_input, 0.0, 0, i_length );
1184 if( !p_input->b_preparsing )
1186 StartTitle( p_input );
1187 LoadSubtitles( p_input );
1188 LoadSlaves( p_input );
1189 InitPrograms( p_input );
1192 if( !p_input->b_preparsing && p_input->p->p_sout )
1194 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1196 if( p_input->p->b_can_pace_control && p_input->p->b_out_pace_control )
1198 /* We don't want a high input priority here or we'll
1199 * end-up sucking up all the CPU time */
1200 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1203 msg_Dbg( p_input, "starting in %s mode",
1204 p_input->p->b_out_pace_control ? "async" : "sync" );
1207 p_meta = vlc_meta_New();
1210 /* Get meta data from users */
1211 InputMetaUser( p_input, p_meta );
1213 /* Get meta data from master input */
1214 InputSourceMeta( p_input, &p_input->p->input, p_meta );
1216 /* And from slave */
1217 for( int i = 0; i < p_input->p->i_slave; i++ )
1218 InputSourceMeta( p_input, p_input->p->slave[i], p_meta );
1221 InputUpdateMeta( p_input, p_meta );
1224 msg_Dbg( p_input, "`%s' successfully opened",
1225 p_input->p->p_item->psz_uri );
1227 /* initialization is complete */
1228 input_ChangeState( p_input, PLAYING_S );
1233 input_ChangeState( p_input, ERROR_S );
1235 if( p_input->p->p_es_out )
1236 es_out_Delete( p_input->p->p_es_out );
1237 if( p_input->p->p_es_out_display )
1238 es_out_Delete( p_input->p->p_es_out_display );
1239 if( p_input->p->p_resource )
1241 if( p_input->p->p_sout )
1242 input_resource_RequestSout( p_input->p->p_resource,
1243 p_input->p->p_sout, NULL );
1244 input_resource_SetInput( p_input->p->p_resource, NULL );
1250 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1252 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1253 stats_CounterClean( p_input->p->counters.p_##c );\
1254 p_input->p->counters.p_##c = NULL; } while(0)
1255 EXIT_COUNTER( read_bytes );
1256 EXIT_COUNTER( read_packets );
1257 EXIT_COUNTER( demux_read );
1258 EXIT_COUNTER( input_bitrate );
1259 EXIT_COUNTER( demux_bitrate );
1260 EXIT_COUNTER( demux_corrupted );
1261 EXIT_COUNTER( demux_discontinuity );
1262 EXIT_COUNTER( played_abuffers );
1263 EXIT_COUNTER( lost_abuffers );
1264 EXIT_COUNTER( displayed_pictures );
1265 EXIT_COUNTER( lost_pictures );
1266 EXIT_COUNTER( decoded_audio );
1267 EXIT_COUNTER( decoded_video );
1268 EXIT_COUNTER( decoded_sub );
1270 if( p_input->p->p_sout )
1272 EXIT_COUNTER( sout_sent_packets );
1273 EXIT_COUNTER( sout_sent_bytes );
1274 EXIT_COUNTER( sout_send_bitrate );
1279 /* Mark them deleted */
1280 p_input->p->input.p_demux = NULL;
1281 p_input->p->input.p_stream = NULL;
1282 p_input->p->input.p_access = NULL;
1283 p_input->p->p_es_out = NULL;
1284 p_input->p->p_es_out_display = NULL;
1285 p_input->p->p_sout = NULL;
1287 return VLC_EGENERIC;
1290 /*****************************************************************************
1291 * End: end the input thread
1292 *****************************************************************************/
1293 static void End( input_thread_t * p_input )
1297 /* We are at the end */
1298 input_ChangeState( p_input, END_S );
1300 /* Clean control variables */
1301 input_ControlVarStop( p_input );
1303 /* Stop es out activity */
1304 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1305 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1307 /* Clean up master */
1308 InputSourceClean( &p_input->p->input );
1311 for( i = 0; i < p_input->p->i_slave; i++ )
1313 InputSourceClean( p_input->p->slave[i] );
1314 free( p_input->p->slave[i] );
1316 free( p_input->p->slave );
1318 /* Unload all modules */
1319 if( p_input->p->p_es_out )
1320 es_out_Delete( p_input->p->p_es_out );
1321 if( p_input->p->p_es_out_display )
1322 es_out_Delete( p_input->p->p_es_out_display );
1324 if( !p_input->b_preparsing )
1326 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1327 if( libvlc_stats( p_input ) )
1329 /* make sure we are up to date */
1330 stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
1331 CL_CO( read_bytes );
1332 CL_CO( read_packets );
1333 CL_CO( demux_read );
1334 CL_CO( input_bitrate );
1335 CL_CO( demux_bitrate );
1336 CL_CO( demux_corrupted );
1337 CL_CO( demux_discontinuity );
1338 CL_CO( played_abuffers );
1339 CL_CO( lost_abuffers );
1340 CL_CO( displayed_pictures );
1341 CL_CO( lost_pictures );
1342 CL_CO( decoded_audio) ;
1343 CL_CO( decoded_video );
1344 CL_CO( decoded_sub) ;
1347 /* Close optional stream output instance */
1348 if( p_input->p->p_sout )
1350 CL_CO( sout_sent_packets );
1351 CL_CO( sout_sent_bytes );
1352 CL_CO( sout_send_bitrate );
1357 vlc_mutex_lock( &p_input->p->p_item->lock );
1358 if( p_input->p->i_attachment > 0 )
1360 for( i = 0; i < p_input->p->i_attachment; i++ )
1361 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1362 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1364 vlc_mutex_unlock( &p_input->p->p_item->lock );
1367 input_resource_RequestSout( p_input->p->p_resource,
1368 p_input->p->p_sout, NULL );
1369 input_resource_SetInput( p_input->p->p_resource, NULL );
1372 /*****************************************************************************
1374 *****************************************************************************/
1375 void input_ControlPush( input_thread_t *p_input,
1376 int i_type, vlc_value_t *p_val )
1378 vlc_mutex_lock( &p_input->p->lock_control );
1379 if( i_type == INPUT_CONTROL_SET_DIE )
1381 /* Special case, empty the control */
1382 for( int i = 0; i < p_input->p->i_control; i++ )
1384 input_control_t *p_ctrl = &p_input->p->control[i];
1385 ControlRelease( p_ctrl->i_type, p_ctrl->val );
1387 p_input->p->i_control = 0;
1390 if( p_input->p->i_control >= INPUT_CONTROL_FIFO_SIZE )
1392 msg_Err( p_input, "input control fifo overflow, trashing type=%d",
1395 ControlRelease( i_type, *p_val );
1404 memset( &c.val, 0, sizeof(c.val) );
1406 p_input->p->control[p_input->p->i_control++] = c;
1408 vlc_cond_signal( &p_input->p->wait_control );
1409 vlc_mutex_unlock( &p_input->p->lock_control );
1412 static inline int ControlPop( input_thread_t *p_input,
1413 int *pi_type, vlc_value_t *p_val,
1414 mtime_t i_deadline )
1416 input_thread_private_t *p_sys = p_input->p;
1418 vlc_mutex_lock( &p_sys->lock_control );
1419 while( p_sys->i_control <= 0 )
1421 if( !vlc_object_alive( p_input ) || i_deadline < 0 )
1423 vlc_mutex_unlock( &p_sys->lock_control );
1424 return VLC_EGENERIC;
1427 if( vlc_cond_timedwait( &p_sys->wait_control, &p_sys->lock_control,
1430 vlc_mutex_unlock( &p_sys->lock_control );
1431 return VLC_EGENERIC;
1436 *pi_type = p_sys->control[0].i_type;
1437 *p_val = p_sys->control[0].val;
1440 if( p_sys->i_control > 0 )
1441 memmove( &p_sys->control[0], &p_sys->control[1],
1442 sizeof(*p_sys->control) * p_sys->i_control );
1443 vlc_mutex_unlock( &p_sys->lock_control );
1448 static void ControlReduce( input_thread_t *p_input )
1450 vlc_mutex_lock( &p_input->p->lock_control );
1452 for( int i = 1; i < p_input->p->i_control; i++ )
1454 const int i_lt = p_input->p->control[i-1].i_type;
1455 const int i_ct = p_input->p->control[i].i_type;
1457 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1458 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1462 ( i_ct == INPUT_CONTROL_SET_STATE ||
1463 i_ct == INPUT_CONTROL_SET_RATE ||
1464 i_ct == INPUT_CONTROL_SET_POSITION ||
1465 i_ct == INPUT_CONTROL_SET_TIME ||
1466 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1467 i_ct == INPUT_CONTROL_SET_TITLE ||
1468 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1469 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1472 // msg_Dbg( p_input, "merged at %d", i );
1473 /* Remove the i-1 */
1474 for( j = i; j < p_input->p->i_control; j++ )
1475 p_input->p->control[j-1] = p_input->p->control[j];
1476 p_input->p->i_control--;
1480 /* TODO but that's not that important
1481 - merge SET_X with SET_X_CMD
1482 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1483 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1488 vlc_mutex_unlock( &p_input->p->lock_control );
1491 static void ControlRelease( int i_type, vlc_value_t val )
1495 case INPUT_CONTROL_ADD_SUBTITLE:
1496 case INPUT_CONTROL_ADD_SLAVE:
1497 free( val.psz_string );
1506 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1508 int i_ret = VLC_SUCCESS;
1509 int i_state = PAUSE_S;
1511 if( p_input->p->b_can_pause )
1513 if( p_input->p->input.p_access )
1514 i_ret = access_Control( p_input->p->input.p_access,
1515 ACCESS_SET_PAUSE_STATE, true );
1517 i_ret = demux_Control( p_input->p->input.p_demux,
1518 DEMUX_SET_PAUSE_STATE, true );
1522 msg_Warn( p_input, "cannot set pause state" );
1523 i_state = p_input->p->i_state;
1530 i_ret = es_out_SetPauseState( p_input->p->p_es_out, p_input->p->b_can_pause, true, i_control_date );
1533 msg_Warn( p_input, "cannot set pause state at es_out level" );
1534 i_state = p_input->p->i_state;
1538 /* Switch to new state */
1539 input_ChangeState( p_input, i_state );
1543 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1545 int i_ret = VLC_SUCCESS;
1547 if( p_input->p->b_can_pause )
1549 if( p_input->p->input.p_access )
1550 i_ret = access_Control( p_input->p->input.p_access,
1551 ACCESS_SET_PAUSE_STATE, false );
1553 i_ret = demux_Control( p_input->p->input.p_demux,
1554 DEMUX_SET_PAUSE_STATE, false );
1557 /* FIXME What to do ? */
1558 msg_Warn( p_input, "cannot unset pause -> EOF" );
1559 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1563 /* Switch to play */
1564 input_ChangeState( p_input, PLAYING_S );
1568 es_out_SetPauseState( p_input->p->p_es_out, false, false, i_control_date );
1571 static bool Control( input_thread_t *p_input,
1572 int i_type, vlc_value_t val )
1574 const mtime_t i_control_date = mdate();
1575 /* FIXME b_force_update is abused, it should be carefully checked */
1576 bool b_force_update = false;
1579 return b_force_update;
1583 case INPUT_CONTROL_SET_DIE:
1584 msg_Dbg( p_input, "control: stopping input" );
1586 /* Mark all submodules to die */
1587 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1590 case INPUT_CONTROL_SET_POSITION:
1591 case INPUT_CONTROL_SET_POSITION_OFFSET:
1595 if( p_input->p->b_recording )
1597 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1600 f_pos = val.f_float;
1601 if( i_type != INPUT_CONTROL_SET_POSITION )
1602 f_pos += var_GetFloat( p_input, "position" );
1605 else if( f_pos > 1.0 )
1607 /* Reset the decoders states and clock sync (before calling the demuxer */
1608 es_out_SetTime( p_input->p->p_es_out, -1 );
1609 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1610 f_pos, !p_input->p->b_fast_seek ) )
1612 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1613 "%2.1f%% failed", f_pos * 100 );
1617 if( p_input->p->i_slave > 0 )
1618 SlaveSeek( p_input );
1619 p_input->p->input.b_eof = false;
1621 b_force_update = true;
1626 case INPUT_CONTROL_SET_TIME:
1627 case INPUT_CONTROL_SET_TIME_OFFSET:
1632 if( p_input->p->b_recording )
1634 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1638 i_time = val.i_time;
1639 if( i_type != INPUT_CONTROL_SET_TIME )
1640 i_time += var_GetTime( p_input, "time" );
1645 /* Reset the decoders states and clock sync (before calling the demuxer */
1646 es_out_SetTime( p_input->p->p_es_out, -1 );
1648 i_ret = demux_Control( p_input->p->input.p_demux,
1649 DEMUX_SET_TIME, i_time,
1650 !p_input->p->b_fast_seek );
1655 /* Emulate it with a SET_POS */
1656 demux_Control( p_input->p->input.p_demux,
1657 DEMUX_GET_LENGTH, &i_length );
1660 double f_pos = (double)i_time / (double)i_length;
1661 i_ret = demux_Control( p_input->p->input.p_demux,
1662 DEMUX_SET_POSITION, f_pos,
1663 !p_input->p->b_fast_seek );
1668 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1669 " failed or not possible", i_time );
1673 if( p_input->p->i_slave > 0 )
1674 SlaveSeek( p_input );
1675 p_input->p->input.b_eof = false;
1677 b_force_update = true;
1682 case INPUT_CONTROL_SET_STATE:
1683 if( ( val.i_int == PLAYING_S && p_input->p->i_state == PAUSE_S ) ||
1684 ( val.i_int == PAUSE_S && p_input->p->i_state == PAUSE_S ) )
1686 ControlUnpause( p_input, i_control_date );
1688 b_force_update = true;
1690 else if( val.i_int == PAUSE_S && p_input->p->i_state == PLAYING_S /* &&
1691 p_input->p->b_can_pause */ )
1693 ControlPause( p_input, i_control_date );
1695 b_force_update = true;
1697 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause && 0 )
1699 b_force_update = true;
1701 /* Correct "state" value */
1702 input_ChangeState( p_input, p_input->p->i_state );
1704 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1706 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1710 case INPUT_CONTROL_SET_RATE:
1711 case INPUT_CONTROL_SET_RATE_SLOWER:
1712 case INPUT_CONTROL_SET_RATE_FASTER:
1717 /* Get rate and direction */
1718 if( i_type == INPUT_CONTROL_SET_RATE )
1720 i_rate = abs( val.i_int );
1721 i_rate_sign = val.i_int < 0 ? -1 : 1;
1725 static const int ppi_factor[][2] = {
1726 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1728 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1735 i_rate_sign = p_input->p->i_rate < 0 ? -1 : 1;
1739 for( i = 0; ppi_factor[i][0] != 0; i++ )
1741 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1742 const int i_test_e = abs( abs( p_input->p->i_rate ) - i_test_r );
1743 if( i_test_e < i_error )
1750 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1752 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1754 if( ppi_factor[i_idx+1][0] > 0 )
1755 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1757 i_rate = INPUT_RATE_MAX+1;
1761 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1763 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1765 i_rate = INPUT_RATE_MIN-1;
1769 /* Check rate bound */
1770 if( i_rate < INPUT_RATE_MIN )
1772 msg_Dbg( p_input, "cannot set rate faster" );
1773 i_rate = INPUT_RATE_MIN;
1775 else if( i_rate > INPUT_RATE_MAX )
1777 msg_Dbg( p_input, "cannot set rate slower" );
1778 i_rate = INPUT_RATE_MAX;
1781 /* Apply direction */
1782 if( i_rate_sign < 0 )
1784 if( p_input->p->input.b_rescale_ts )
1786 msg_Dbg( p_input, "cannot set negative rate" );
1787 i_rate = p_input->p->i_rate;
1788 assert( i_rate > 0 );
1792 i_rate *= i_rate_sign;
1796 if( i_rate != INPUT_RATE_DEFAULT &&
1797 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1798 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1800 msg_Dbg( p_input, "cannot change rate" );
1801 i_rate = INPUT_RATE_DEFAULT;
1803 if( i_rate != p_input->p->i_rate &&
1804 !p_input->p->b_can_pace_control && p_input->p->b_can_rate_control )
1807 if( p_input->p->input.p_access )
1809 i_ret = VLC_EGENERIC;
1813 if( !p_input->p->input.b_rescale_ts )
1814 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1816 i_ret = demux_Control( p_input->p->input.p_demux,
1817 DEMUX_SET_RATE, &i_rate );
1821 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1822 i_rate = p_input->p->i_rate;
1827 if( i_rate != p_input->p->i_rate )
1829 p_input->p->i_rate = i_rate;
1830 input_SendEventRate( p_input, i_rate );
1832 if( p_input->p->input.b_rescale_ts )
1834 const int i_rate_source = (p_input->p->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1835 es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1838 b_force_update = true;
1843 case INPUT_CONTROL_SET_PROGRAM:
1844 /* No need to force update, es_out does it if needed */
1845 es_out_Control( p_input->p->p_es_out,
1846 ES_OUT_SET_GROUP, val.i_int );
1848 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1852 case INPUT_CONTROL_SET_ES:
1853 /* No need to force update, es_out does it if needed */
1854 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, val.i_int );
1857 case INPUT_CONTROL_RESTART_ES:
1858 es_out_Control( p_input->p->p_es_out_display, ES_OUT_RESTART_ES_BY_ID, val.i_int );
1861 case INPUT_CONTROL_SET_AUDIO_DELAY:
1862 if( !es_out_SetDelay( p_input->p->p_es_out_display, AUDIO_ES, val.i_time ) )
1864 input_SendEventAudioDelay( p_input, val.i_time );
1865 UpdatePtsDelay( p_input );
1869 case INPUT_CONTROL_SET_SPU_DELAY:
1870 if( !es_out_SetDelay( p_input->p->p_es_out_display, SPU_ES, val.i_time ) )
1872 input_SendEventSubtitleDelay( p_input, val.i_time );
1873 UpdatePtsDelay( p_input );
1877 case INPUT_CONTROL_SET_TITLE:
1878 case INPUT_CONTROL_SET_TITLE_NEXT:
1879 case INPUT_CONTROL_SET_TITLE_PREV:
1880 if( p_input->p->b_recording )
1882 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1885 if( p_input->p->input.b_title_demux &&
1886 p_input->p->input.i_title > 0 )
1889 /* FIXME handle demux title */
1890 demux_t *p_demux = p_input->p->input.p_demux;
1893 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1894 i_title = p_demux->info.i_title - 1;
1895 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1896 i_title = p_demux->info.i_title + 1;
1898 i_title = val.i_int;
1900 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1902 es_out_SetTime( p_input->p->p_es_out, -1 );
1904 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1905 input_SendEventTitle( p_input, i_title );
1908 else if( p_input->p->input.i_title > 0 )
1910 access_t *p_access = p_input->p->input.p_access;
1913 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1914 i_title = p_access->info.i_title - 1;
1915 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1916 i_title = p_access->info.i_title + 1;
1918 i_title = val.i_int;
1920 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1922 es_out_SetTime( p_input->p->p_es_out, -1 );
1924 stream_Control( p_input->p->input.p_stream, STREAM_CONTROL_ACCESS,
1925 ACCESS_SET_TITLE, i_title );
1926 input_SendEventTitle( p_input, i_title );
1930 case INPUT_CONTROL_SET_SEEKPOINT:
1931 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1932 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1933 if( p_input->p->b_recording )
1935 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1939 if( p_input->p->input.b_title_demux &&
1940 p_input->p->input.i_title > 0 )
1942 demux_t *p_demux = p_input->p->input.p_demux;
1944 int64_t i_input_time;
1945 int64_t i_seekpoint_time;
1947 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1949 i_seekpoint = p_demux->info.i_seekpoint;
1950 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1951 i_input_time = var_GetTime( p_input, "time" );
1952 if( i_seekpoint_time >= 0 && i_input_time >= 0 )
1954 if( i_input_time < i_seekpoint_time + 3000000 )
1960 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1961 i_seekpoint = p_demux->info.i_seekpoint + 1;
1963 i_seekpoint = val.i_int;
1965 if( i_seekpoint >= 0 && i_seekpoint <
1966 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1969 es_out_SetTime( p_input->p->p_es_out, -1 );
1971 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1972 input_SendEventSeekpoint( p_input, p_demux->info.i_title, i_seekpoint );
1975 else if( p_input->p->input.i_title > 0 )
1977 access_t *p_access = p_input->p->input.p_access;
1979 int64_t i_input_time;
1980 int64_t i_seekpoint_time;
1982 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1984 i_seekpoint = p_access->info.i_seekpoint;
1985 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1986 i_input_time = var_GetTime( p_input, "time" );
1987 if( i_seekpoint_time >= 0 && i_input_time >= 0 )
1989 if( i_input_time < i_seekpoint_time + 3000000 )
1995 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1996 i_seekpoint = p_access->info.i_seekpoint + 1;
1998 i_seekpoint = val.i_int;
2000 if( i_seekpoint >= 0 && i_seekpoint <
2001 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
2003 es_out_SetTime( p_input->p->p_es_out, -1 );
2005 stream_Control( p_input->p->input.p_stream, STREAM_CONTROL_ACCESS,
2006 ACCESS_SET_SEEKPOINT, i_seekpoint );
2007 input_SendEventSeekpoint( p_input, p_access->info.i_title, i_seekpoint );
2012 case INPUT_CONTROL_ADD_SUBTITLE:
2013 if( val.psz_string )
2014 SubtitleAdd( p_input, val.psz_string, true );
2017 case INPUT_CONTROL_ADD_SLAVE:
2018 if( val.psz_string )
2020 input_source_t *slave = InputSourceNew( p_input );
2022 if( slave && !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
2028 msg_Dbg( p_input, "adding %s as slave on the fly",
2032 if( demux_Control( p_input->p->input.p_demux,
2033 DEMUX_GET_TIME, &i_time ) )
2035 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2036 InputSourceClean( slave );
2040 if( demux_Control( slave->p_demux,
2041 DEMUX_SET_TIME, i_time, true ) )
2043 msg_Err( p_input, "seek failed for new slave" );
2044 InputSourceClean( slave );
2049 /* Get meta (access and demux) */
2050 p_meta = vlc_meta_New();
2053 access_Control( slave->p_access, ACCESS_GET_META, p_meta );
2054 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
2055 InputUpdateMeta( p_input, p_meta );
2058 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2063 msg_Warn( p_input, "failed to add %s as slave",
2069 case INPUT_CONTROL_SET_RECORD_STATE:
2070 if( !!p_input->p->b_recording != !!val.b_bool )
2072 if( p_input->p->input.b_can_stream_record )
2074 if( demux_Control( p_input->p->input.p_demux,
2075 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2080 if( es_out_SetRecordState( p_input->p->p_es_out_display, val.b_bool ) )
2083 p_input->p->b_recording = val.b_bool;
2085 input_SendEventRecord( p_input, val.b_bool );
2087 b_force_update = true;
2091 case INPUT_CONTROL_SET_FRAME_NEXT:
2092 if( p_input->p->i_state == PAUSE_S )
2094 es_out_SetFrameNext( p_input->p->p_es_out );
2096 else if( p_input->p->i_state == PLAYING_S )
2098 ControlPause( p_input, i_control_date );
2102 msg_Err( p_input, "invalid state for frame next" );
2104 b_force_update = true;
2107 case INPUT_CONTROL_SET_BOOKMARK:
2109 seekpoint_t bookmark;
2111 bookmark.i_time_offset = -1;
2112 bookmark.i_byte_offset = -1;
2114 vlc_mutex_lock( &p_input->p->p_item->lock );
2115 if( val.i_int >= 0 && val.i_int < p_input->p->i_bookmark )
2117 const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[val.i_int];
2118 bookmark.i_time_offset = p_bookmark->i_time_offset;
2119 bookmark.i_byte_offset = p_bookmark->i_byte_offset;
2121 vlc_mutex_unlock( &p_input->p->p_item->lock );
2123 if( bookmark.i_time_offset < 0 && bookmark.i_byte_offset < 0 )
2125 msg_Err( p_input, "invalid bookmark %d", val.i_int );
2129 if( bookmark.i_time_offset >= 0 )
2131 val.i_time = bookmark.i_time_offset;
2132 b_force_update = Control( p_input, INPUT_CONTROL_SET_TIME, val );
2134 else if( bookmark.i_byte_offset >= 0 &&
2135 p_input->p->input.p_stream )
2137 const int64_t i_size = stream_Size( p_input->p->input.p_stream );
2138 if( i_size > 0 && bookmark.i_byte_offset <= i_size )
2140 val.f_float = (double)bookmark.i_byte_offset / i_size;
2141 b_force_update = Control( p_input, INPUT_CONTROL_SET_POSITION, val );
2148 msg_Err( p_input, "not yet implemented" );
2152 ControlRelease( i_type, val );
2153 return b_force_update;
2156 /*****************************************************************************
2157 * UpdateTitleSeekpoint
2158 *****************************************************************************/
2159 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2160 int i_title, int i_seekpoint )
2162 int i_title_end = p_input->p->input.i_title_end -
2163 p_input->p->input.i_title_offset;
2164 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2165 p_input->p->input.i_seekpoint_offset;
2167 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2169 if( i_title > i_title_end ||
2170 ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2173 else if( i_seekpoint_end >= 0 )
2175 if( i_seekpoint > i_seekpoint_end )
2178 else if( i_title_end >= 0 )
2180 if( i_title > i_title_end )
2185 /*****************************************************************************
2187 *****************************************************************************/
2188 static int UpdateTitleSeekpointFromDemux( input_thread_t *p_input )
2190 demux_t *p_demux = p_input->p->input.p_demux;
2192 /* TODO event-like */
2193 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2195 input_SendEventTitle( p_input, p_demux->info.i_title );
2197 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2199 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2201 input_SendEventSeekpoint( p_input,
2202 p_demux->info.i_title, p_demux->info.i_seekpoint );
2204 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2207 /* Hmmm only works with master input */
2208 if( p_input->p->input.p_demux == p_demux )
2209 return UpdateTitleSeekpoint( p_input,
2210 p_demux->info.i_title,
2211 p_demux->info.i_seekpoint );
2215 static void UpdateGenericFromDemux( input_thread_t *p_input )
2217 demux_t *p_demux = p_input->p->input.p_demux;
2219 if( p_demux->info.i_update & INPUT_UPDATE_META )
2221 vlc_meta_t *p_meta = vlc_meta_New();
2224 demux_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
2225 InputUpdateMeta( p_input, p_meta );
2227 p_demux->info.i_update &= ~INPUT_UPDATE_META;
2230 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2234 /*****************************************************************************
2235 * Update*FromAccess:
2236 *****************************************************************************/
2237 static int UpdateTitleSeekpointFromAccess( input_thread_t *p_input )
2239 access_t *p_access = p_input->p->input.p_access;
2241 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2243 input_SendEventTitle( p_input, p_access->info.i_title );
2245 stream_Control( p_input->p->input.p_stream, STREAM_UPDATE_SIZE );
2247 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2249 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2251 input_SendEventSeekpoint( p_input,
2252 p_access->info.i_title, p_access->info.i_seekpoint );
2254 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2256 /* Hmmm only works with master input */
2257 if( p_input->p->input.p_access == p_access )
2258 return UpdateTitleSeekpoint( p_input,
2259 p_access->info.i_title,
2260 p_access->info.i_seekpoint );
2263 static void UpdateGenericFromAccess( input_thread_t *p_input )
2265 access_t *p_access = p_input->p->input.p_access;
2267 if( p_access->info.i_update & INPUT_UPDATE_META )
2269 /* TODO maybe multi - access ? */
2270 vlc_meta_t *p_meta = vlc_meta_New();
2273 access_Control( p_input->p->input.p_access, ACCESS_GET_META, p_meta );
2274 InputUpdateMeta( p_input, p_meta );
2276 p_access->info.i_update &= ~INPUT_UPDATE_META;
2278 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2283 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2284 f_quality = f_strength = -1;
2286 input_SendEventSignal( p_input, f_quality, f_strength );
2288 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2291 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2294 /*****************************************************************************
2296 *****************************************************************************/
2297 static input_source_t *InputSourceNew( input_thread_t *p_input )
2299 VLC_UNUSED(p_input);
2301 return calloc( 1, sizeof( input_source_t ) );
2304 /*****************************************************************************
2306 *****************************************************************************/
2307 static int InputSourceInit( input_thread_t *p_input,
2308 input_source_t *in, const char *psz_mrl,
2309 const char *psz_forced_demux )
2311 const char *psz_access;
2312 const char *psz_demux;
2316 char *psz_dup = strdup( psz_mrl );
2318 if( psz_dup == NULL )
2322 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2324 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2325 psz_mrl, psz_access, psz_demux, psz_path );
2326 if( !p_input->b_preparsing )
2328 /* Hack to allow udp://@:port syntax */
2330 (strncmp( psz_access, "udp", 3 ) &&
2331 strncmp( psz_access, "rtp", 3 )) )
2333 /* Find optional titles and seekpoints */
2334 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2335 &in->i_seekpoint_start, &in->i_seekpoint_end );
2338 if( psz_forced_demux && *psz_forced_demux )
2340 psz_demux = psz_forced_demux;
2342 else if( *psz_demux == '\0' )
2344 /* special hack for forcing a demuxer with --demux=module
2345 * (and do nothing with a list) */
2346 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2348 if( psz_var_demux != NULL &&
2349 !strchr(psz_var_demux, ',' ) &&
2350 !strchr(psz_var_demux, ':' ) )
2352 psz_demux = psz_var_demux;
2354 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2358 /* Try access_demux first */
2359 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2360 NULL, p_input->p->p_es_out, false );
2364 /* Preparsing is only for file:// */
2367 if( !*psz_access ) /* path without scheme:// */
2368 psz_access = "file";
2369 if( strcmp( psz_access, "file" ) )
2371 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2376 /* Get infos from access_demux */
2377 demux_Control( in->p_demux,
2378 DEMUX_GET_PTS_DELAY, &in->i_pts_delay );
2379 in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2382 in->b_title_demux = true;
2383 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2384 &in->title, &in->i_title,
2385 &in->i_title_offset, &in->i_seekpoint_offset ) )
2387 TAB_INIT( in->i_title, in->title );
2389 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2390 &in->b_can_pace_control ) )
2391 in->b_can_pace_control = false;
2393 if( !in->b_can_pace_control )
2395 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2396 &in->b_can_rate_control, &in->b_rescale_ts ) )
2398 in->b_can_rate_control = false;
2399 in->b_rescale_ts = true; /* not used */
2404 in->b_can_rate_control = true;
2405 in->b_rescale_ts = true;
2407 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2408 &in->b_can_pause ) )
2409 in->b_can_pause = false;
2410 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2411 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2412 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2415 if( demux_Control( in->p_demux, DEMUX_CAN_SEEK, &b_can_seek ) )
2417 var_SetBool( p_input, "can-seek", b_can_seek );
2421 /* Now try a real access */
2422 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2424 /* Access failed, URL encoded ? */
2425 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2427 decode_URI( psz_path );
2429 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2430 psz_access, psz_demux, psz_path );
2432 in->p_access = access_New( p_input,
2433 psz_access, psz_demux, psz_path );
2435 if( in->p_access == NULL )
2437 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2439 dialog_Fatal( p_input, _("Your input can't be opened"),
2440 _("VLC is unable to open the MRL '%s'."
2441 " Check the log for details."), psz_mrl );
2445 /* Get infos from access */
2446 if( !p_input->b_preparsing )
2449 access_Control( in->p_access,
2450 ACCESS_GET_PTS_DELAY, &in->i_pts_delay );
2451 in->i_pts_delay = __MAX( 0, __MIN( in->i_pts_delay, INPUT_PTS_DELAY_MAX ) );
2453 in->b_title_demux = false;
2454 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2455 &in->title, &in->i_title,
2456 &in->i_title_offset, &in->i_seekpoint_offset ) )
2459 TAB_INIT( in->i_title, in->title );
2461 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2462 &in->b_can_pace_control );
2463 in->b_can_rate_control = in->b_can_pace_control;
2464 in->b_rescale_ts = true;
2466 access_Control( in->p_access, ACCESS_CAN_PAUSE, &in->b_can_pause );
2467 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2468 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2469 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2471 access_Control( in->p_access, ACCESS_CAN_SEEK, &b_can_seek );
2472 var_SetBool( p_input, "can-seek", b_can_seek );
2477 char **ppsz_input_list;
2479 TAB_INIT( i_input_list, ppsz_input_list );
2481 /* On master stream only, use input-list */
2482 if( &p_input->p->input == in )
2488 psz_parser = var_CreateGetNonEmptyString( p_input, "input-list" );
2490 while( psz_parser && *psz_parser )
2492 char *p = strchr( psz_parser, ',' );
2498 char *psz_name = strdup( psz_parser );
2500 TAB_APPEND( i_input_list, ppsz_input_list, psz_name );
2507 /* Autodetect extra files if none specified */
2508 if( i_input_list <= 0 )
2510 InputGetExtraFiles( p_input, &i_input_list, &ppsz_input_list,
2511 psz_access, psz_path );
2513 if( i_input_list > 0 )
2514 TAB_APPEND( i_input_list, ppsz_input_list, NULL );
2516 /* Create the stream_t */
2517 in->p_stream = stream_AccessNew( in->p_access, ppsz_input_list );
2518 if( ppsz_input_list )
2520 for( int i = 0; ppsz_input_list[i] != NULL; i++ )
2521 free( ppsz_input_list[i] );
2522 TAB_CLEAN( i_input_list, ppsz_input_list );
2525 if( in->p_stream == NULL )
2527 msg_Warn( p_input, "cannot create a stream_t from access" );
2531 /* Add stream filters */
2532 char *psz_stream_filter = var_GetNonEmptyString( p_input,
2534 in->p_stream = stream_FilterChainNew( in->p_stream,
2536 var_GetBool( p_input, "input-record-native" ) );
2537 free( psz_stream_filter );
2539 /* Open a demuxer */
2540 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2542 psz_demux = in->p_access->psz_demux;
2546 /* Take access/stream redirections into account */
2547 char *psz_real_path;
2548 char *psz_buf = NULL;
2549 if( in->p_stream->psz_path )
2551 const char *psz_a, *psz_d;
2552 psz_buf = strdup( in->p_stream->psz_path );
2553 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2557 psz_real_path = psz_path;
2559 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2561 in->p_stream, p_input->p->p_es_out,
2562 p_input->b_preparsing );
2566 if( in->p_demux == NULL )
2568 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2569 psz_access, psz_demux, psz_path );
2570 dialog_Fatal( VLC_OBJECT( p_input ),
2571 _("VLC can't recognize the input's format"),
2572 _("The format of '%s' cannot be detected. "
2573 "Have a look at the log for details."), psz_mrl );
2577 /* Get title from demux */
2578 if( !p_input->b_preparsing && in->i_title <= 0 )
2580 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2581 &in->title, &in->i_title,
2582 &in->i_title_offset, &in->i_seekpoint_offset ))
2584 TAB_INIT( in->i_title, in->title );
2588 in->b_title_demux = true;
2595 /* Set record capabilities */
2596 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2597 in->b_can_stream_record = false;
2599 if( !var_GetBool( p_input, "input-record-native" ) )
2600 in->b_can_stream_record = false;
2601 var_SetBool( p_input, "can-record", true );
2603 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2607 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2608 if( 1 || !p_input->b_preparsing )
2611 input_attachment_t **attachment;
2612 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2613 &attachment, &i_attachment ) )
2615 vlc_mutex_lock( &p_input->p->p_item->lock );
2616 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2617 i_attachment, attachment );
2618 vlc_mutex_unlock( &p_input->p->p_item->lock );
2621 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) && f_fps > 0.0 )
2623 vlc_mutex_lock( &p_input->p->p_item->lock );
2624 p_input->p->f_fps = f_fps;
2625 vlc_mutex_unlock( &p_input->p->p_item->lock );
2628 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2629 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2635 demux_Delete( in->p_demux );
2638 stream_Delete( in->p_stream );
2641 access_Delete( in->p_access );
2644 return VLC_EGENERIC;
2647 /*****************************************************************************
2649 *****************************************************************************/
2650 static void InputSourceClean( input_source_t *in )
2655 demux_Delete( in->p_demux );
2658 stream_Delete( in->p_stream );
2661 access_Delete( in->p_access );
2663 if( in->i_title > 0 )
2665 for( i = 0; i < in->i_title; i++ )
2666 vlc_input_title_Delete( in->title[i] );
2667 TAB_CLEAN( in->i_title, in->title );
2671 /*****************************************************************************
2673 *****************************************************************************/
2674 static void InputSourceMeta( input_thread_t *p_input,
2675 input_source_t *p_source, vlc_meta_t *p_meta )
2677 access_t *p_access = p_source->p_access;
2678 demux_t *p_demux = p_source->p_demux;
2680 /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
2683 /* Read access meta */
2685 access_Control( p_access, ACCESS_GET_META, p_meta );
2687 /* Read demux meta */
2688 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2690 /* If the demux report unsupported meta data, try an external "meta reader" */
2692 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2697 demux_meta_t *p_demux_meta = p_demux->p_private = calloc( 1, sizeof(*p_demux_meta) );
2701 module_t *p_id3 = module_need( p_demux, "meta reader", NULL, false );
2704 if( p_demux_meta->p_meta )
2706 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2707 vlc_meta_Delete( p_demux_meta->p_meta );
2710 if( p_demux_meta->i_attachments > 0 )
2712 vlc_mutex_lock( &p_input->p->p_item->lock );
2713 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2714 p_demux_meta->i_attachments, p_demux_meta->attachments );
2715 vlc_mutex_unlock( &p_input->p->p_item->lock );
2717 module_unneed( p_demux, p_id3 );
2719 free( p_demux_meta );
2723 static void SlaveDemux( input_thread_t *p_input )
2728 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2730 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2734 for( i = 0; i < p_input->p->i_slave; i++ )
2736 input_source_t *in = p_input->p->slave[i];
2742 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2747 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2749 msg_Err( p_input, "slave[%d] doesn't like "
2750 "DEMUX_GET_TIME -> EOF", i );
2755 if( i_stime >= i_time )
2758 if( ( i_ret = demux_Demux( in->p_demux ) ) <= 0 )
2764 i_ret = demux_Demux( in->p_demux );
2769 msg_Dbg( p_input, "slave %d EOF", i );
2775 static void SlaveSeek( input_thread_t *p_input )
2780 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2782 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2786 for( i = 0; i < p_input->p->i_slave; i++ )
2788 input_source_t *in = p_input->p->slave[i];
2790 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time, true ) )
2793 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2803 /*****************************************************************************
2805 *****************************************************************************/
2806 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2808 static const struct { int i_meta; const char *psz_name; } p_list[] = {
2809 { vlc_meta_Title, "meta-title" },
2810 { vlc_meta_Artist, "meta-artist" },
2811 { vlc_meta_Genre, "meta-genre" },
2812 { vlc_meta_Copyright, "meta-copyright" },
2813 { vlc_meta_Description, "meta-description" },
2814 { vlc_meta_Date, "meta-date" },
2815 { vlc_meta_URL, "meta-url" },
2819 /* Get meta information from user */
2820 for( int i = 0; p_list[i].psz_name; i++ )
2822 char *psz_string = var_GetNonEmptyString( p_input, p_list[i].psz_name );
2826 EnsureUTF8( psz_string );
2827 vlc_meta_Set( p_meta, p_list[i].i_meta, psz_string );
2832 /*****************************************************************************
2833 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2834 * arturl and locking issue.
2835 *****************************************************************************/
2836 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2838 input_item_t *p_item = p_input->p->p_item;
2840 char *psz_title = NULL;
2841 char *psz_arturl = input_item_GetArtURL( p_item );
2843 vlc_mutex_lock( &p_item->lock );
2845 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2846 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2848 vlc_meta_Merge( p_item->p_meta, p_meta );
2850 vlc_meta_Delete( p_meta );
2852 if( !psz_arturl || *psz_arturl == '\0' )
2854 const char *psz_tmp = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
2856 psz_arturl = strdup( psz_tmp );
2858 vlc_mutex_unlock( &p_item->lock );
2860 if( psz_arturl && *psz_arturl )
2862 input_item_SetArtURL( p_item, psz_arturl );
2864 if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2866 /* Don't look for art cover if sout
2867 * XXX It can change when sout has meta data support */
2868 if( p_input->p->p_sout && !p_input->b_preparsing )
2869 input_item_SetArtURL( p_item, "" );
2871 input_ExtractAttachmentAndCacheArt( p_input );
2878 input_item_SetName( p_item, psz_title );
2881 input_item_SetPreparsed( p_item, true );
2883 input_SendEventMeta( p_input );
2885 /** \todo handle sout meta */
2888 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2889 int i_new, input_attachment_t **pp_new )
2891 int i_attachment = *pi_attachment;
2892 input_attachment_t **attachment = *ppp_attachment;
2895 attachment = realloc( attachment,
2896 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2897 for( i = 0; i < i_new; i++ )
2898 attachment[i_attachment++] = pp_new[i];
2902 *pi_attachment = i_attachment;
2903 *ppp_attachment = attachment;
2905 /*****************************************************************************
2906 * InputGetExtraFiles
2907 * Autodetect extra input list
2908 *****************************************************************************/
2909 static void InputGetExtraFilesPattern( input_thread_t *p_input,
2910 int *pi_list, char ***pppsz_list,
2911 const char *psz_path,
2912 const char *psz_match,
2913 const char *psz_format,
2914 int i_start, int i_stop )
2919 TAB_INIT( i_list, ppsz_list );
2921 char *psz_base = strdup( psz_path );
2925 /* Remove the extension */
2926 char *psz_end = &psz_base[strlen(psz_base)-strlen(psz_match)];
2927 assert( psz_end >= psz_base);
2930 /* Try to list files */
2931 for( int i = i_start; i <= i_stop; i++ )
2936 if( asprintf( &psz_file, psz_format, psz_base, i ) < 0 )
2939 if( utf8_stat( psz_file, &st ) || !S_ISREG( st.st_mode ) || !st.st_size )
2945 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2946 TAB_APPEND( i_list, ppsz_list, psz_file );
2951 *pppsz_list = ppsz_list;
2954 static void InputGetExtraFiles( input_thread_t *p_input,
2955 int *pi_list, char ***pppsz_list,
2956 const char *psz_access, const char *psz_path )
2960 const char *psz_match;
2961 const char *psz_format;
2965 /* XXX the order is important */
2966 { ".001", "%s.%.3d", 2, 999 },
2967 { ".part1.rar", "%s.part%.1d.rar",2, 9 },
2968 { ".part01.rar", "%s.part%.2d.rar",2, 99, },
2969 { ".part001.rar", "%s.part%.3d.rar",2, 999 },
2970 { ".rar", "%s.r%.2d", 1, 99 },
2971 { NULL, NULL, 0, 0 }
2974 TAB_INIT( *pi_list, *pppsz_list );
2976 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2979 const size_t i_path = strlen(psz_path);
2981 for( int i = 0; p_pattern[i].psz_match != NULL; i++ )
2983 const size_t i_ext = strlen(p_pattern[i].psz_match );
2985 if( i_path < i_ext )
2987 if( !strcmp( &psz_path[i_path-i_ext], p_pattern[i].psz_match ) )
2989 InputGetExtraFilesPattern( p_input, pi_list, pppsz_list,
2991 p_pattern[i].psz_match, p_pattern[i].psz_format,
2992 p_pattern[i].i_start, p_pattern[i].i_stop );
2999 static void input_ChangeState( input_thread_t *p_input, int i_state )
3001 const bool b_changed = p_input->p->i_state != i_state;
3003 p_input->p->i_state = i_state;
3004 if( i_state == ERROR_S )
3005 p_input->b_error = true;
3006 else if( i_state == END_S )
3007 p_input->b_eof = true;
3011 input_item_SetErrorWhenReading( p_input->p->p_item, p_input->b_error );
3012 input_SendEventState( p_input, i_state );
3017 /*****************************************************************************
3018 * MRLSplit: parse the access, demux and url part of the
3019 * Media Resource Locator.
3020 *****************************************************************************/
3021 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
3024 char *psz_access = NULL;
3025 char *psz_demux = NULL;
3028 /* Either there is an access/demux specification before ://
3029 * or we have a plain local file path. */
3030 psz_path = strstr( psz_dup, "://" );
3031 if( psz_path != NULL )
3034 psz_path += 3; /* skips "://" */
3036 /* Separate access from demux (<access>/<demux>://<path>) */
3037 psz_access = psz_dup;
3038 psz_demux = strchr( psz_access, '/' );
3040 *psz_demux++ = '\0';
3042 /* We really don't want module name substitution here! */
3043 if( psz_access[0] == '$' )
3045 if( psz_demux && psz_demux[0] == '$' )
3052 *ppsz_access = psz_access ? psz_access : (char*)"";
3053 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
3054 *ppsz_path = psz_path;
3057 static inline bool next(char ** src)
3061 long result = strtol( *src, &end, 0 );
3062 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
3071 /*****************************************************************************
3072 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3075 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
3076 *****************************************************************************/
3077 static void MRLSections( input_thread_t *p_input, char *psz_source,
3078 int *pi_title_start, int *pi_title_end,
3079 int *pi_chapter_start, int *pi_chapter_end )
3081 char *psz, *psz_end, *psz_next, *psz_check;
3083 *pi_title_start = *pi_title_end = -1;
3084 *pi_chapter_start = *pi_chapter_end = -1;
3086 /* Start by parsing titles and chapters */
3087 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
3090 /* Check we are really dealing with a title/chapter section */
3091 psz_check = psz + 1;
3092 if( !*psz_check ) return;
3093 if( isdigit(*psz_check) )
3094 if(!next(&psz_check)) return;
3095 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
3096 if( *psz_check == ':' && ++psz_check )
3098 if( isdigit(*psz_check) )
3099 if(!next(&psz_check)) return;
3101 if( *psz_check != '-' && *psz_check ) return;
3102 if( *psz_check == '-' && ++psz_check )
3104 if( isdigit(*psz_check) )
3105 if(!next(&psz_check)) return;
3107 if( *psz_check != ':' && *psz_check ) return;
3108 if( *psz_check == ':' && ++psz_check )
3110 if( isdigit(*psz_check) )
3111 if(!next(&psz_check)) return;
3113 if( *psz_check ) return;
3115 /* Separate start and end */
3117 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
3119 /* Look for the start title */
3120 *pi_title_start = strtol( psz, &psz_next, 0 );
3121 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
3122 *pi_title_end = *pi_title_start;
3125 /* Look for the start chapter */
3127 *pi_chapter_start = strtol( psz, &psz_next, 0 );
3128 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
3129 *pi_chapter_end = *pi_chapter_start;
3133 /* Look for the end title */
3134 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3135 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3138 /* Look for the end chapter */
3139 if( *psz_end ) psz_end++;
3140 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3141 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3144 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3145 psz_source, *pi_title_start, *pi_chapter_start,
3146 *pi_title_end, *pi_chapter_end );
3149 /*****************************************************************************
3150 * input_AddSubtitles: add a subtitles file and enable it
3151 *****************************************************************************/
3152 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3154 input_source_t *sub;
3157 char *psz_path, *psz_extension;
3159 /* if we are provided a subtitle.sub file,
3160 * see if we don't have a subtitle.idx and use it instead */
3161 psz_path = strdup( psz_subtitle );
3164 psz_extension = strrchr( psz_path, '.');
3165 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3169 strcpy( psz_extension, ".idx" );
3171 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3173 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3174 psz_path, psz_subtitle );
3175 strcpy( psz_subtitle, psz_path );
3181 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3183 sub = InputSourceNew( p_input );
3184 if( !sub || InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3189 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3192 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3194 if( count.i_int == 0 )
3196 /* if it was first one, there is disable too */
3198 if( count.i_int < list.p_list->i_count )
3200 const int i_id = list.p_list->p_values[count.i_int].i_int;
3202 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3203 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3205 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3209 /*****************************************************************************
3211 *****************************************************************************/
3212 void input_UpdateStatistic( input_thread_t *p_input,
3213 input_statistic_t i_type, int i_delta )
3215 assert( p_input->p->i_state != INIT_S );
3217 vlc_mutex_lock( &p_input->p->counters.counters_lock);
3220 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
3221 case INPUT_STATISTIC_DECODED_VIDEO:
3224 case INPUT_STATISTIC_DECODED_AUDIO:
3227 case INPUT_STATISTIC_DECODED_SUBTITLE:
3230 case INPUT_STATISTIC_SENT_PACKET:
3231 I(p_sout_sent_packets);
3234 case INPUT_STATISTIC_SENT_BYTE:
3236 int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
3238 if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
3239 stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
3243 msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3246 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
3250 /* TODO FIXME nearly the same logic that snapshot code */
3251 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3256 path = utf8_opendir( psz_path );
3261 char *psz_tmp = str_format( p_obj, psz_prefix );
3265 char *psz_tmp2 = filename_sanitize( psz_tmp );
3269 asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3271 psz_extension ? "." : "",
3272 psz_extension ? psz_extension : "" ) < 0 )
3279 psz_file = str_format( p_obj, psz_path );
3280 path_sanitize( psz_file );