1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
39 #include "input_internal.h"
42 #include "es_out_timeshift.h"
49 #include <vlc_dialog.h>
51 #include <vlc_charset.h>
53 #include <vlc_strings.h>
54 #include <vlc_modules.h>
55 #include <vlc_stream.h>
56 #include <vlc_stream_extractor.h>
58 /*****************************************************************************
60 *****************************************************************************/
61 static void *Run( void * );
62 static void *Preparse( void * );
64 static input_thread_t
* Create ( vlc_object_t
*, input_item_t
*,
65 const char *, bool, input_resource_t
* );
66 static int Init ( input_thread_t
*p_input
);
67 static void End ( input_thread_t
*p_input
);
68 static void MainLoop( input_thread_t
*p_input
, bool b_interactive
);
70 static inline int ControlPop( input_thread_t
*, int *, vlc_value_t
*, mtime_t i_deadline
, bool b_postpone_seek
);
71 static void ControlRelease( int i_type
, vlc_value_t val
);
72 static bool ControlIsSeekRequest( int i_type
);
73 static bool Control( input_thread_t
*, int, vlc_value_t
);
74 static void ControlPause( input_thread_t
*, mtime_t
);
76 static int UpdateTitleSeekpointFromDemux( input_thread_t
* );
77 static void UpdateGenericFromDemux( input_thread_t
* );
78 static void UpdateTitleListfromDemux( input_thread_t
* );
80 static void MRLSections( const char *, int *, int *, int *, int *);
82 static input_source_t
*InputSourceNew( input_thread_t
*, const char *,
83 const char *psz_forced_demux
,
85 static void InputSourceDestroy( input_source_t
* );
86 static void InputSourceMeta( input_thread_t
*, input_source_t
*, vlc_meta_t
* );
89 //static void InputGetAttachments( input_thread_t *, input_source_t * );
90 static void SlaveDemux( input_thread_t
*p_input
);
91 static void SlaveSeek( input_thread_t
*p_input
);
93 static void InputMetaUser( input_thread_t
*p_input
, vlc_meta_t
*p_meta
);
94 static void InputUpdateMeta( input_thread_t
*p_input
, demux_t
*p_demux
);
95 static void InputGetExtraFiles( input_thread_t
*p_input
,
96 int *pi_list
, char ***pppsz_list
,
97 const char **psz_access
, const char *psz_path
);
99 static void AppendAttachment( int *pi_attachment
, input_attachment_t
***ppp_attachment
,
100 const demux_t
***ppp_attachment_demux
,
101 int i_new
, input_attachment_t
**pp_new
, const demux_t
*p_demux
);
103 #define SLAVE_ADD_NOFLAG 0
104 #define SLAVE_ADD_FORCED (1<<0)
105 #define SLAVE_ADD_CANFAIL (1<<1)
106 #define SLAVE_ADD_SET_TIME (1<<2)
108 static int input_SlaveSourceAdd( input_thread_t
*, enum slave_type
,
109 const char *, unsigned );
110 static char *input_SubtitleFile2Uri( input_thread_t
*, const char * );
111 static void input_ChangeState( input_thread_t
*p_input
, int i_state
); /* TODO fix name */
115 * Create a new input_thread_t.
117 * You need to call input_Start on it when you are done
118 * adding callback on the variables/events you want to monitor.
120 * \param p_parent a vlc_object
121 * \param p_item an input item
122 * \param psz_log an optional prefix for this input logs
123 * \param p_resource an optional input ressource
124 * \return a pointer to the spawned input thread
126 input_thread_t
*input_Create( vlc_object_t
*p_parent
,
127 input_item_t
*p_item
,
128 const char *psz_log
, input_resource_t
*p_resource
)
130 return Create( p_parent
, p_item
, psz_log
, false, p_resource
);
135 * Initialize an input thread and run it until it stops by itself.
137 * \param p_parent a vlc_object
138 * \param p_item an input item
139 * \return an error code, VLC_SUCCESS on success
141 int input_Read( vlc_object_t
*p_parent
, input_item_t
*p_item
)
143 input_thread_t
*p_input
= Create( p_parent
, p_item
, NULL
, false, NULL
);
147 if( !Init( p_input
) )
149 MainLoop( p_input
, false );
153 vlc_object_release( p_input
);
157 input_thread_t
*input_CreatePreparser( vlc_object_t
*parent
,
160 return Create( parent
, item
, NULL
, true, NULL
);
164 * Start a input_thread_t created by input_Create.
166 * You must not start an already running input_thread_t.
168 * \param the input thread to start
170 int input_Start( input_thread_t
*p_input
)
172 input_thread_private_t
*priv
= input_priv(p_input
);
173 void *(*func
)(void *) = Run
;
175 if( priv
->b_preparsing
)
178 assert( !priv
->is_running
);
179 /* Create thread and wait for its readiness. */
180 priv
->is_running
= !vlc_clone( &priv
->thread
, func
, priv
,
181 VLC_THREAD_PRIORITY_INPUT
);
182 if( !priv
->is_running
)
184 input_ChangeState( p_input
, ERROR_S
);
185 msg_Err( p_input
, "cannot create input thread" );
192 * Request a running input thread to stop and die
194 * \param p_input the input thread to stop
196 void input_Stop( input_thread_t
*p_input
)
198 input_thread_private_t
*sys
= input_priv(p_input
);
200 vlc_mutex_lock( &sys
->lock_control
);
201 /* Discard all pending controls */
202 for( int i
= 0; i
< sys
->i_control
; i
++ )
204 input_control_t
*ctrl
= &sys
->control
[i
];
205 ControlRelease( ctrl
->i_type
, ctrl
->val
);
208 sys
->is_stopped
= true;
209 vlc_cond_signal( &sys
->wait_control
);
210 vlc_mutex_unlock( &sys
->lock_control
);
211 vlc_interrupt_kill( &sys
->interrupt
);
217 * It does not call input_Stop itself.
219 void input_Close( input_thread_t
*p_input
)
221 if( input_priv(p_input
)->is_running
)
222 vlc_join( input_priv(p_input
)->thread
, NULL
);
223 vlc_interrupt_deinit( &input_priv(p_input
)->interrupt
);
224 vlc_object_release( p_input
);
228 * Input destructor (called when the object's refcount reaches 0).
230 static void input_Destructor( vlc_object_t
*obj
)
232 input_thread_t
*p_input
= (input_thread_t
*)obj
;
233 input_thread_private_t
*priv
= input_priv(p_input
);
235 char * psz_name
= input_item_GetName( priv
->p_item
);
236 msg_Dbg( p_input
, "Destroying the input for '%s'", psz_name
);
240 if( priv
->p_es_out_display
)
241 es_out_Delete( priv
->p_es_out_display
);
243 if( priv
->p_resource
)
244 input_resource_Release( priv
->p_resource
);
245 if( priv
->p_resource_private
)
246 input_resource_Release( priv
->p_resource_private
);
248 input_item_Release( priv
->p_item
);
250 vlc_mutex_destroy( &priv
->counters
.counters_lock
);
252 for( int i
= 0; i
< priv
->i_control
; i
++ )
254 input_control_t
*p_ctrl
= &priv
->control
[i
];
255 ControlRelease( p_ctrl
->i_type
, p_ctrl
->val
);
258 vlc_cond_destroy( &priv
->wait_control
);
259 vlc_mutex_destroy( &priv
->lock_control
);
263 * Get the item from an input thread
264 * FIXME it does not increase ref count of the item.
265 * if it is used after p_input is destroyed nothing prevent it from
268 input_item_t
*input_GetItem( input_thread_t
*p_input
)
270 assert( p_input
!= NULL
);
271 return input_priv(p_input
)->p_item
;
274 /*****************************************************************************
275 * This function creates a new input, and returns a pointer
276 * to its description. On error, it returns NULL.
278 * XXX Do not forget to update vlc_input.h if you add new variables.
279 *****************************************************************************/
280 static input_thread_t
*Create( vlc_object_t
*p_parent
, input_item_t
*p_item
,
281 const char *psz_header
, bool b_preparsing
,
282 input_resource_t
*p_resource
)
284 /* Allocate descriptor */
285 input_thread_private_t
*priv
;
287 priv
= vlc_custom_create( p_parent
, sizeof( *priv
), "input" );
288 if( unlikely(priv
== NULL
) )
291 input_thread_t
*p_input
= &priv
->input
;
293 char * psz_name
= input_item_GetName( p_item
);
294 msg_Dbg( p_input
, "Creating an input for %s'%s'",
295 b_preparsing
? "preparsing " : "", psz_name
);
298 /* Parse input options */
299 input_item_ApplyOptions( VLC_OBJECT(p_input
), p_item
);
301 p_input
->obj
.header
= psz_header
? strdup( psz_header
) : NULL
;
303 /* Init Common fields */
304 priv
->b_preparsing
= b_preparsing
;
305 priv
->b_can_pace_control
= true;
311 priv
->i_title_offset
= input_priv(p_input
)->i_seekpoint_offset
= 0;
312 priv
->i_state
= INIT_S
;
313 priv
->is_running
= false;
314 priv
->is_stopped
= false;
315 priv
->b_recording
= false;
316 priv
->i_rate
= INPUT_RATE_DEFAULT
;
317 memset( &priv
->bookmark
, 0, sizeof(priv
->bookmark
) );
318 TAB_INIT( priv
->i_bookmark
, priv
->pp_bookmark
);
319 TAB_INIT( priv
->i_attachment
, priv
->attachment
);
320 priv
->attachment_demux
= NULL
;
322 priv
->b_out_pace_control
= false;
324 vlc_viewpoint_t
*p_viewpoint
= var_InheritAddress( p_input
, "viewpoint" );
325 if (likely(p_viewpoint
!= NULL
))
326 priv
->viewpoint
= *p_viewpoint
;
328 vlc_viewpoint_init( &priv
->viewpoint
);
330 input_item_Hold( p_item
); /* Released in Destructor() */
331 priv
->p_item
= p_item
;
333 /* Init Input fields */
335 vlc_mutex_lock( &p_item
->lock
);
337 if( !p_item
->p_stats
)
338 p_item
->p_stats
= stats_NewInputStats( p_input
);
340 /* setup the preparse depth of the item
341 * if we are preparsing, use the i_preparse_depth of the parent item */
342 if( !priv
->b_preparsing
)
344 char *psz_rec
= var_InheritString( p_parent
, "recursive" );
346 if( psz_rec
!= NULL
)
348 if ( !strcasecmp( psz_rec
, "none" ) )
349 p_item
->i_preparse_depth
= 0;
350 else if ( !strcasecmp( psz_rec
, "collapse" ) )
351 p_item
->i_preparse_depth
= 1;
353 p_item
->i_preparse_depth
= -1; /* default is expand */
356 p_item
->i_preparse_depth
= -1;
359 p_input
->obj
.flags
|= OBJECT_FLAGS_QUIET
| OBJECT_FLAGS_NOINTERACT
;
361 /* Make sure the interaction option is honored */
362 if( !var_InheritBool( p_input
, "interact" ) )
363 p_input
->obj
.flags
|= OBJECT_FLAGS_NOINTERACT
;
364 else if( p_item
->b_preparse_interact
)
366 /* If true, this item was asked explicitly to interact with the user
367 * (via libvlc_MetadataRequest). Sub items created from this input won't
368 * have this flag and won't interact with the user */
369 p_input
->obj
.flags
&= ~OBJECT_FLAGS_NOINTERACT
;
372 vlc_mutex_unlock( &p_item
->lock
);
381 priv
->p_resource_private
= NULL
;
382 priv
->p_resource
= input_resource_Hold( p_resource
);
386 priv
->p_resource_private
= input_resource_New( VLC_OBJECT( p_input
) );
387 priv
->p_resource
= input_resource_Hold( priv
->p_resource_private
);
389 input_resource_SetInput( priv
->p_resource
, p_input
);
391 /* Init control buffer */
392 vlc_mutex_init( &priv
->lock_control
);
393 vlc_cond_init( &priv
->wait_control
);
395 vlc_interrupt_init(&priv
->interrupt
);
397 /* Create Object Variables for private use only */
398 input_ConfigVarInit( p_input
);
400 /* Create Objects variables for public Get and Set */
401 input_ControlVarInit( p_input
);
404 if( !priv
->b_preparsing
)
406 char *psz_bookmarks
= var_GetNonEmptyString( p_input
, "bookmarks" );
409 /* FIXME: have a common cfg parsing routine used by sout and others */
410 char *psz_parser
, *psz_start
, *psz_end
;
411 psz_parser
= psz_bookmarks
;
412 while( (psz_start
= strchr( psz_parser
, '{' ) ) )
414 seekpoint_t
*p_seekpoint
;
417 psz_end
= strchr( psz_start
, '}' );
418 if( !psz_end
) break;
419 psz_parser
= psz_end
+ 1;
420 backup
= *psz_parser
;
424 p_seekpoint
= vlc_seekpoint_New();
426 if( unlikely( p_seekpoint
== NULL
) )
429 while( (psz_end
= strchr( psz_start
, ',' ) ) )
432 if( !strncmp( psz_start
, "name=", 5 ) )
434 free( p_seekpoint
->psz_name
);
436 p_seekpoint
->psz_name
= strdup(psz_start
+ 5);
438 else if( !strncmp( psz_start
, "time=", 5 ) )
440 p_seekpoint
->i_time_offset
= atof(psz_start
+ 5) *
443 psz_start
= psz_end
+ 1;
445 msg_Dbg( p_input
, "adding bookmark: %s, time=%"PRId64
,
446 p_seekpoint
->psz_name
,
447 p_seekpoint
->i_time_offset
);
448 input_Control( p_input
, INPUT_ADD_BOOKMARK
, p_seekpoint
);
449 vlc_seekpoint_Delete( p_seekpoint
);
450 *psz_parser
= backup
;
452 free( psz_bookmarks
);
456 /* Remove 'Now playing' info as it is probably outdated */
457 input_item_SetNowPlaying( p_item
, NULL
);
458 input_item_SetESNowPlaying( p_item
, NULL
);
459 input_SendEventMeta( p_input
);
462 memset( &priv
->counters
, 0, sizeof( priv
->counters
) );
463 vlc_mutex_init( &priv
->counters
.counters_lock
);
465 priv
->p_es_out_display
= input_EsOutNew( p_input
, priv
->i_rate
);
466 priv
->p_es_out
= NULL
;
468 /* Set the destructor when we are sure we are initialized */
469 vlc_object_set_destructor( p_input
, input_Destructor
);
474 /*****************************************************************************
475 * Run: main thread loop
476 * This is the "normal" thread that spawns the input processing chain,
477 * reads the stream, cleans up and waits
478 *****************************************************************************/
479 static void *Run( void *data
)
481 input_thread_private_t
*priv
= data
;
482 input_thread_t
*p_input
= &priv
->input
;
484 vlc_interrupt_set(&priv
->interrupt
);
486 if( !Init( p_input
) )
488 if( priv
->b_can_pace_control
&& priv
->b_out_pace_control
)
490 /* We don't want a high input priority here or we'll
491 * end-up sucking up all the CPU time */
492 vlc_set_priority( priv
->thread
, VLC_THREAD_PRIORITY_LOW
);
495 MainLoop( p_input
, true ); /* FIXME it can be wrong (like with VLM) */
501 input_SendEventDead( p_input
);
505 static void *Preparse( void *data
)
507 input_thread_private_t
*priv
= data
;
508 input_thread_t
*p_input
= &priv
->input
;
510 vlc_interrupt_set(&priv
->interrupt
);
512 if( !Init( p_input
) )
513 { /* if the demux is a playlist, call Mainloop that will call
514 * demux_Demux in order to fetch sub items */
515 bool b_is_playlist
= false;
517 if ( input_item_ShouldPreparseSubItems( priv
->p_item
)
518 && demux_Control( priv
->master
->p_demux
, DEMUX_IS_PLAYLIST
,
520 b_is_playlist
= false;
522 MainLoop( p_input
, false );
526 input_SendEventDead( p_input
);
530 bool input_Stopped( input_thread_t
*input
)
532 input_thread_private_t
*sys
= input_priv(input
);
535 vlc_mutex_lock( &sys
->lock_control
);
536 ret
= sys
->is_stopped
;
537 vlc_mutex_unlock( &sys
->lock_control
);
541 /*****************************************************************************
542 * Main loop: Fill buffers from access, and demux
543 *****************************************************************************/
547 * It asks the demuxer to demux some data
549 static void MainLoopDemux( input_thread_t
*p_input
, bool *pb_changed
)
552 demux_t
*p_demux
= input_priv(p_input
)->master
->p_demux
;
556 if( input_priv(p_input
)->i_stop
> 0 && input_priv(p_input
)->i_time
>= input_priv(p_input
)->i_stop
)
557 i_ret
= VLC_DEMUXER_EOF
;
559 i_ret
= demux_Demux( p_demux
);
561 i_ret
= i_ret
> 0 ? VLC_DEMUXER_SUCCESS
: ( i_ret
< 0 ? VLC_DEMUXER_EGENERIC
: VLC_DEMUXER_EOF
);
563 if( i_ret
== VLC_DEMUXER_SUCCESS
)
565 if( demux_TestAndClearFlags( p_demux
, INPUT_UPDATE_TITLE_LIST
) )
566 UpdateTitleListfromDemux( p_input
);
568 if( input_priv(p_input
)->master
->b_title_demux
)
570 i_ret
= UpdateTitleSeekpointFromDemux( p_input
);
574 UpdateGenericFromDemux( p_input
);
577 if( i_ret
== VLC_DEMUXER_EOF
)
579 msg_Dbg( p_input
, "EOF reached" );
580 input_priv(p_input
)->master
->b_eof
= true;
581 es_out_Eos(input_priv(p_input
)->p_es_out
);
583 else if( i_ret
== VLC_DEMUXER_EGENERIC
)
585 input_ChangeState( p_input
, ERROR_S
);
587 else if( input_priv(p_input
)->i_slave
> 0 )
588 SlaveDemux( p_input
);
591 static int MainLoopTryRepeat( input_thread_t
*p_input
)
593 int i_repeat
= var_GetInteger( p_input
, "input-repeat" );
599 msg_Dbg( p_input
, "repeating the same input (%d)", i_repeat
);
603 var_SetInteger( p_input
, "input-repeat", i_repeat
);
606 /* Seek to start title/seekpoint */
607 val
.i_int
= input_priv(p_input
)->master
->i_title_start
-
608 input_priv(p_input
)->master
->i_title_offset
;
609 if( val
.i_int
< 0 || val
.i_int
>= input_priv(p_input
)->master
->i_title
)
611 input_ControlPush( p_input
,
612 INPUT_CONTROL_SET_TITLE
, &val
);
614 val
.i_int
= input_priv(p_input
)->master
->i_seekpoint_start
-
615 input_priv(p_input
)->master
->i_seekpoint_offset
;
616 if( val
.i_int
> 0 /* TODO: check upper boundary */ )
617 input_ControlPush( p_input
,
618 INPUT_CONTROL_SET_SEEKPOINT
, &val
);
620 /* Seek to start position */
621 if( input_priv(p_input
)->i_start
> 0 )
623 val
.i_int
= input_priv(p_input
)->i_start
;
624 input_ControlPush( p_input
, INPUT_CONTROL_SET_TIME
, &val
);
629 input_ControlPush( p_input
, INPUT_CONTROL_SET_POSITION
, &val
);
636 * Update timing infos and statistics.
638 static void MainLoopStatistics( input_thread_t
*p_input
)
640 double f_position
= 0.0;
642 mtime_t i_length
= 0;
644 /* update input status variables */
645 if( demux_Control( input_priv(p_input
)->master
->p_demux
,
646 DEMUX_GET_POSITION
, &f_position
) )
649 if( demux_Control( input_priv(p_input
)->master
->p_demux
,
650 DEMUX_GET_TIME
, &i_time
) )
652 input_priv(p_input
)->i_time
= i_time
;
654 if( demux_Control( input_priv(p_input
)->master
->p_demux
,
655 DEMUX_GET_LENGTH
, &i_length
) )
658 es_out_SetTimes( input_priv(p_input
)->p_es_out
, f_position
, i_time
, i_length
);
660 /* update current bookmark */
661 vlc_mutex_lock( &input_priv(p_input
)->p_item
->lock
);
662 input_priv(p_input
)->bookmark
.i_time_offset
= i_time
;
663 vlc_mutex_unlock( &input_priv(p_input
)->p_item
->lock
);
665 stats_ComputeInputStats( p_input
, input_priv(p_input
)->p_item
->p_stats
);
666 input_SendEventStatistics( p_input
);
671 * The main input loop.
673 static void MainLoop( input_thread_t
*p_input
, bool b_interactive
)
675 mtime_t i_intf_update
= 0;
676 mtime_t i_last_seek_mdate
= 0;
678 if( b_interactive
&& var_InheritBool( p_input
, "start-paused" ) )
679 ControlPause( p_input
, mdate() );
681 bool b_pause_after_eof
= b_interactive
&&
682 var_InheritBool( p_input
, "play-and-pause" );
683 bool b_paused_at_eof
= false;
685 demux_t
*p_demux
= input_priv(p_input
)->master
->p_demux
;
686 const bool b_can_demux
= p_demux
->pf_demux
!= NULL
;
688 while( !input_Stopped( p_input
) && input_priv(p_input
)->i_state
!= ERROR_S
)
690 mtime_t i_wakeup
= -1;
691 bool b_paused
= input_priv(p_input
)->i_state
== PAUSE_S
;
692 /* FIXME if input_priv(p_input)->i_state == PAUSE_S the access/access_demux
693 * is paused -> this may cause problem with some of them
694 * The same problem can be seen when seeking while paused */
696 b_paused
= !es_out_GetBuffering( input_priv(p_input
)->p_es_out
)
697 || input_priv(p_input
)->master
->b_eof
;
701 if( !input_priv(p_input
)->master
->b_eof
)
703 bool b_force_update
= false;
705 MainLoopDemux( p_input
, &b_force_update
);
708 i_wakeup
= es_out_GetWakeup( input_priv(p_input
)->p_es_out
);
712 b_paused_at_eof
= false;
714 else if( !es_out_GetEmpty( input_priv(p_input
)->p_es_out
) )
716 msg_Dbg( p_input
, "waiting decoder fifos to empty" );
717 i_wakeup
= mdate() + INPUT_IDLE_SLEEP
;
719 /* Pause after eof only if the input is pausable.
720 * This way we won't trigger timeshifting for nothing */
721 else if( b_pause_after_eof
&& input_priv(p_input
)->b_can_pause
)
723 if( b_paused_at_eof
)
726 vlc_value_t val
= { .i_int
= PAUSE_S
};
728 msg_Dbg( p_input
, "pausing at EOF (pause after each)");
729 Control( p_input
, INPUT_CONTROL_SET_STATE
, val
);
732 b_paused_at_eof
= true;
736 if( MainLoopTryRepeat( p_input
) )
740 /* Update interface and statistics */
741 mtime_t now
= mdate();
742 if( now
>= i_intf_update
)
744 MainLoopStatistics( p_input
);
745 i_intf_update
= now
+ INT64_C(250000);
752 mtime_t i_deadline
= i_wakeup
;
754 /* Postpone seeking until ES buffering is complete or at most
756 bool b_postpone
= es_out_GetBuffering( input_priv(p_input
)->p_es_out
)
757 && !input_priv(p_input
)->master
->b_eof
;
760 mtime_t now
= mdate();
762 /* Recheck ES buffer level every 20 ms when seeking */
763 if( now
< i_last_seek_mdate
+ INT64_C(125000)
764 && (i_deadline
< 0 || i_deadline
> now
+ INT64_C(20000)) )
765 i_deadline
= now
+ INT64_C(20000);
773 if( ControlPop( p_input
, &i_type
, &val
, i_deadline
, b_postpone
) )
777 break; /* Wake-up time reached */
781 msg_Dbg( p_input
, "control type=%d", i_type
);
783 if( Control( p_input
, i_type
, val
) )
785 if( ControlIsSeekRequest( i_type
) )
786 i_last_seek_mdate
= mdate();
790 /* Update the wakeup time */
792 i_wakeup
= es_out_GetWakeup( input_priv(p_input
)->p_es_out
);
797 static void InitStatistics( input_thread_t
*p_input
)
799 input_thread_private_t
*priv
= input_priv(p_input
);
801 if( priv
->b_preparsing
) return;
803 /* Prepare statistics */
804 #define INIT_COUNTER( c, compute ) priv->counters.p_##c = \
805 stats_CounterCreate( STATS_##compute);
806 if( libvlc_stats( p_input
) )
808 INIT_COUNTER( read_bytes
, COUNTER
);
809 INIT_COUNTER( read_packets
, COUNTER
);
810 INIT_COUNTER( demux_read
, COUNTER
);
811 INIT_COUNTER( input_bitrate
, DERIVATIVE
);
812 INIT_COUNTER( demux_bitrate
, DERIVATIVE
);
813 INIT_COUNTER( demux_corrupted
, COUNTER
);
814 INIT_COUNTER( demux_discontinuity
, COUNTER
);
815 INIT_COUNTER( played_abuffers
, COUNTER
);
816 INIT_COUNTER( lost_abuffers
, COUNTER
);
817 INIT_COUNTER( displayed_pictures
, COUNTER
);
818 INIT_COUNTER( lost_pictures
, COUNTER
);
819 INIT_COUNTER( decoded_audio
, COUNTER
);
820 INIT_COUNTER( decoded_video
, COUNTER
);
821 INIT_COUNTER( decoded_sub
, COUNTER
);
822 priv
->counters
.p_sout_send_bitrate
= NULL
;
823 priv
->counters
.p_sout_sent_packets
= NULL
;
824 priv
->counters
.p_sout_sent_bytes
= NULL
;
829 static int InitSout( input_thread_t
* p_input
)
831 input_thread_private_t
*priv
= input_priv(p_input
);
833 if( priv
->b_preparsing
)
836 /* Find a usable sout and attach it to p_input */
837 char *psz
= var_GetNonEmptyString( p_input
, "sout" );
838 if( psz
&& strncasecmp( priv
->p_item
->psz_uri
, "vlc:", 4 ) )
840 priv
->p_sout
= input_resource_RequestSout( priv
->p_resource
, NULL
, psz
);
841 if( priv
->p_sout
== NULL
)
843 input_ChangeState( p_input
, ERROR_S
);
844 msg_Err( p_input
, "cannot start stream output instance, " \
849 if( libvlc_stats( p_input
) )
851 INIT_COUNTER( sout_sent_packets
, COUNTER
);
852 INIT_COUNTER( sout_sent_bytes
, COUNTER
);
853 INIT_COUNTER( sout_send_bitrate
, DERIVATIVE
);
858 input_resource_RequestSout( priv
->p_resource
, NULL
, NULL
);
866 static void InitTitle( input_thread_t
* p_input
)
868 input_thread_private_t
*priv
= input_priv(p_input
);
869 input_source_t
*p_master
= priv
->master
;
871 if( priv
->b_preparsing
)
874 vlc_mutex_lock( &priv
->p_item
->lock
);
875 /* Create global title (from master) */
876 priv
->i_title
= p_master
->i_title
;
877 priv
->title
= p_master
->title
;
878 priv
->i_title_offset
= p_master
->i_title_offset
;
879 priv
->i_seekpoint_offset
= p_master
->i_seekpoint_offset
;
880 if( priv
->i_title
> 0 )
882 /* Setup variables */
883 input_ControlVarNavigation( p_input
);
884 input_SendEventTitle( p_input
, 0 );
888 priv
->b_can_pace_control
= p_master
->b_can_pace_control
;
889 priv
->b_can_pause
= p_master
->b_can_pause
;
890 priv
->b_can_rate_control
= p_master
->b_can_rate_control
;
891 vlc_mutex_unlock( &priv
->p_item
->lock
);
894 static void StartTitle( input_thread_t
* p_input
)
896 input_thread_private_t
*priv
= input_priv(p_input
);
899 /* Start title/chapter */
900 val
.i_int
= priv
->master
->i_title_start
- priv
->master
->i_title_offset
;
901 if( val
.i_int
> 0 && val
.i_int
< priv
->master
->i_title
)
902 input_ControlPush( p_input
, INPUT_CONTROL_SET_TITLE
, &val
);
904 val
.i_int
= priv
->master
->i_seekpoint_start
-
905 priv
->master
->i_seekpoint_offset
;
906 if( val
.i_int
> 0 /* TODO: check upper boundary */ )
907 input_ControlPush( p_input
, INPUT_CONTROL_SET_SEEKPOINT
, &val
);
909 /* Start/stop/run time */
910 priv
->i_start
= llroundf(1000000.f
911 * var_GetFloat( p_input
, "start-time" ));
912 priv
->i_stop
= llroundf(1000000.f
913 * var_GetFloat( p_input
, "stop-time" ));
914 if( priv
->i_stop
<= 0 )
916 priv
->i_stop
= llroundf(1000000.f
917 * var_GetFloat( p_input
, "run-time" ));
918 if( priv
->i_stop
< 0 )
920 msg_Warn( p_input
, "invalid run-time ignored" );
924 priv
->i_stop
+= priv
->i_start
;
927 if( priv
->i_start
> 0 )
931 msg_Dbg( p_input
, "starting at time: %"PRId64
"s",
932 priv
->i_start
/ CLOCK_FREQ
);
934 s
.i_int
= priv
->i_start
;
935 input_ControlPush( p_input
, INPUT_CONTROL_SET_TIME
, &s
);
937 if( priv
->i_stop
> 0 && priv
->i_stop
<= priv
->i_start
)
939 msg_Warn( p_input
, "invalid stop-time ignored" );
942 priv
->b_fast_seek
= var_GetBool( p_input
, "input-fast-seek" );
945 static int SlaveCompare(const void *a
, const void *b
)
947 const input_item_slave_t
*p_slave0
= *((const input_item_slave_t
**) a
);
948 const input_item_slave_t
*p_slave1
= *((const input_item_slave_t
**) b
);
950 if( p_slave0
== NULL
|| p_slave1
== NULL
)
952 /* Put NULL (or rejected) subs at the end */
953 return p_slave0
== NULL
? 1 : p_slave1
== NULL
? -1 : 0;
956 if( p_slave0
->i_priority
> p_slave1
->i_priority
)
959 if( p_slave0
->i_priority
< p_slave1
->i_priority
)
965 static bool SlaveExists( input_item_slave_t
**pp_slaves
, int i_slaves
,
968 for( int i
= 0; i
< i_slaves
; i
++ )
970 if( pp_slaves
[i
] != NULL
971 && !strcmp( pp_slaves
[i
]->psz_uri
, psz_uri
) )
977 static void SetSubtitlesOptions( input_thread_t
*p_input
)
979 /* Get fps and set it if not already set */
980 const float f_fps
= input_priv(p_input
)->master
->f_fps
;
983 var_Create( p_input
, "sub-original-fps", VLC_VAR_FLOAT
);
984 var_SetFloat( p_input
, "sub-original-fps", f_fps
);
986 float f_requested_fps
= var_CreateGetFloat( p_input
, "sub-fps" );
987 if( f_requested_fps
!= f_fps
)
989 var_Create( p_input
, "sub-fps", VLC_VAR_FLOAT
|
991 var_SetFloat( p_input
, "sub-fps", f_requested_fps
);
995 const int i_delay
= var_CreateGetInteger( p_input
, "sub-delay" );
997 var_SetInteger( p_input
, "spu-delay", (mtime_t
)i_delay
* 100000 );
1000 static void GetVarSlaves( input_thread_t
*p_input
,
1001 input_item_slave_t
***ppp_slaves
, int *p_slaves
)
1003 char *psz
= var_GetNonEmptyString( p_input
, "input-slave" );
1007 input_item_slave_t
**pp_slaves
= *ppp_slaves
;
1008 int i_slaves
= *p_slaves
;
1010 char *psz_org
= psz
;
1011 while( psz
&& *psz
)
1013 while( *psz
== ' ' || *psz
== '#' )
1016 char *psz_delim
= strchr( psz
, '#' );
1018 *psz_delim
++ = '\0';
1023 char *uri
= strstr(psz
, "://")
1024 ? strdup( psz
) : vlc_path2uri( psz
, NULL
);
1029 input_item_slave_t
*p_slave
=
1030 input_item_slave_New( uri
, SLAVE_TYPE_AUDIO
, SLAVE_PRIORITY_USER
);
1033 if( unlikely( p_slave
== NULL
) )
1035 TAB_APPEND(i_slaves
, pp_slaves
, p_slave
);
1039 *ppp_slaves
= pp_slaves
; /* in case of realloc */
1040 *p_slaves
= i_slaves
;
1043 static void LoadSlaves( input_thread_t
*p_input
)
1045 input_item_slave_t
**pp_slaves
;
1047 TAB_INIT( i_slaves
, pp_slaves
);
1049 /* Look for and add slaves */
1051 char *psz_subtitle
= var_GetNonEmptyString( p_input
, "sub-file" );
1052 if( psz_subtitle
!= NULL
)
1054 msg_Dbg( p_input
, "forced subtitle: %s", psz_subtitle
);
1055 char *psz_uri
= input_SubtitleFile2Uri( p_input
, psz_subtitle
);
1056 free( psz_subtitle
);
1057 psz_subtitle
= NULL
;
1058 if( psz_uri
!= NULL
)
1060 input_item_slave_t
*p_slave
=
1061 input_item_slave_New( psz_uri
, SLAVE_TYPE_SPU
,
1062 SLAVE_PRIORITY_USER
);
1066 TAB_APPEND(i_slaves
, pp_slaves
, p_slave
);
1067 psz_subtitle
= p_slave
->psz_uri
;
1072 if( var_GetBool( p_input
, "sub-autodetect-file" ) )
1074 /* Add local subtitles */
1075 char *psz_autopath
= var_GetNonEmptyString( p_input
, "sub-autodetect-path" );
1077 if( subtitles_Detect( p_input
, psz_autopath
, input_priv(p_input
)->p_item
->psz_uri
,
1078 &pp_slaves
, &i_slaves
) == VLC_SUCCESS
)
1080 /* check that we did not add the subtitle through sub-file */
1081 if( psz_subtitle
!= NULL
)
1083 for( int i
= 1; i
< i_slaves
; i
++ )
1085 input_item_slave_t
*p_curr
= pp_slaves
[i
];
1087 && !strcmp( psz_subtitle
, p_curr
->psz_uri
) )
1089 /* reject current sub */
1090 input_item_slave_Delete( p_curr
);
1091 pp_slaves
[i
] = NULL
;
1096 free( psz_autopath
);
1099 /* Add slaves found by the directory demuxer or via libvlc */
1100 input_item_t
*p_item
= input_priv(p_input
)->p_item
;
1101 vlc_mutex_lock( &p_item
->lock
);
1103 /* Move item slaves to local pp_slaves */
1104 for( int i
= 0; i
< p_item
->i_slaves
; i
++ )
1106 input_item_slave_t
*p_slave
= p_item
->pp_slaves
[i
];
1107 if( !SlaveExists( pp_slaves
, i_slaves
, p_slave
->psz_uri
) )
1108 TAB_APPEND(i_slaves
, pp_slaves
, p_slave
);
1110 input_item_slave_Delete( p_slave
);
1112 /* Slaves that are successfully loaded will be added back to the item */
1113 TAB_CLEAN( p_item
->i_slaves
, p_item
->pp_slaves
);
1114 vlc_mutex_unlock( &p_item
->lock
);
1116 /* Add slaves from the "input-slave" option */
1117 GetVarSlaves( p_input
, &pp_slaves
, &i_slaves
);
1120 qsort( pp_slaves
, i_slaves
, sizeof (input_item_slave_t
*),
1123 /* add all detected slaves */
1124 bool p_forced
[2] = {};
1125 static_assert( SLAVE_TYPE_AUDIO
<= 1 && SLAVE_TYPE_SPU
<= 1,
1126 "slave type size mismatch");
1127 for( int i
= 0; i
< i_slaves
&& pp_slaves
[i
] != NULL
; i
++ )
1129 input_item_slave_t
*p_slave
= pp_slaves
[i
];
1130 /* Slaves added via options should not fail */
1131 unsigned i_flags
= p_slave
->i_priority
!= SLAVE_PRIORITY_USER
1132 ? SLAVE_ADD_CANFAIL
: SLAVE_ADD_NOFLAG
;
1134 if( !p_forced
[p_slave
->i_type
]
1135 && ( p_slave
->b_forced
|| p_slave
->i_priority
== SLAVE_PRIORITY_USER
) )
1136 i_flags
|= SLAVE_ADD_FORCED
;
1138 if( input_SlaveSourceAdd( p_input
, p_slave
->i_type
, p_slave
->psz_uri
,
1139 i_flags
) == VLC_SUCCESS
)
1141 input_item_AddSlave( input_priv(p_input
)->p_item
, p_slave
);
1142 p_forced
[p_slave
->i_type
] = true;
1145 input_item_slave_Delete( p_slave
);
1147 TAB_CLEAN( i_slaves
, pp_slaves
);
1149 /* Load subtitles from attachments */
1150 int i_attachment
= 0;
1151 input_attachment_t
**pp_attachment
= NULL
;
1153 vlc_mutex_lock( &input_priv(p_input
)->p_item
->lock
);
1154 for( int i
= 0; i
< input_priv(p_input
)->i_attachment
; i
++ )
1156 const input_attachment_t
*a
= input_priv(p_input
)->attachment
[i
];
1157 if( !strcmp( a
->psz_mime
, "application/x-srt" ) )
1158 TAB_APPEND( i_attachment
, pp_attachment
,
1159 vlc_input_attachment_New( a
->psz_name
, NULL
,
1160 a
->psz_description
, NULL
, 0 ) );
1162 vlc_mutex_unlock( &input_priv(p_input
)->p_item
->lock
);
1164 if( i_attachment
> 0 )
1165 var_Create( p_input
, "sub-description", VLC_VAR_STRING
);
1166 for( int i
= 0; i
< i_attachment
; i
++ )
1168 input_attachment_t
*a
= pp_attachment
[i
];
1172 if( a
->psz_name
[0] &&
1173 asprintf( &psz_mrl
, "attachment://%s", a
->psz_name
) >= 0 )
1175 var_SetString( p_input
, "sub-description", a
->psz_description
? a
->psz_description
: "");
1177 input_SlaveSourceAdd( p_input
, SLAVE_TYPE_SPU
, psz_mrl
,
1180 /* Don't update item slaves for attachements */
1182 vlc_input_attachment_Delete( a
);
1184 free( pp_attachment
);
1185 if( i_attachment
> 0 )
1186 var_Destroy( p_input
, "sub-description" );
1189 static void UpdatePtsDelay( input_thread_t
*p_input
)
1191 input_thread_private_t
*p_sys
= input_priv(p_input
);
1193 /* Get max pts delay from input source */
1194 mtime_t i_pts_delay
= p_sys
->master
->i_pts_delay
;
1195 for( int i
= 0; i
< p_sys
->i_slave
; i
++ )
1196 i_pts_delay
= __MAX( i_pts_delay
, p_sys
->slave
[i
]->i_pts_delay
);
1198 if( i_pts_delay
< 0 )
1201 /* Take care of audio/spu delay */
1202 const mtime_t i_audio_delay
= var_GetInteger( p_input
, "audio-delay" );
1203 const mtime_t i_spu_delay
= var_GetInteger( p_input
, "spu-delay" );
1204 const mtime_t i_extra_delay
= __MIN( i_audio_delay
, i_spu_delay
);
1205 if( i_extra_delay
< 0 )
1206 i_pts_delay
-= i_extra_delay
;
1208 /* Update cr_average depending on the caching */
1209 const int i_cr_average
= var_GetInteger( p_input
, "cr-average" ) * i_pts_delay
/ DEFAULT_PTS_DELAY
;
1212 es_out_SetDelay( input_priv(p_input
)->p_es_out_display
, AUDIO_ES
, i_audio_delay
);
1213 es_out_SetDelay( input_priv(p_input
)->p_es_out_display
, SPU_ES
, i_spu_delay
);
1214 es_out_SetJitter( input_priv(p_input
)->p_es_out
, i_pts_delay
, 0, i_cr_average
);
1217 static void InitPrograms( input_thread_t
* p_input
)
1222 /* Compute correct pts_delay */
1223 UpdatePtsDelay( p_input
);
1226 i_es_out_mode
= ES_OUT_MODE_AUTO
;
1227 if( input_priv(p_input
)->p_sout
)
1231 if( (prgms
= var_GetNonEmptyString( p_input
, "programs" )) != NULL
)
1235 TAB_INIT( list
.i_count
, list
.p_values
);
1236 for( const char *prgm
= strtok_r( prgms
, ",", &buf
);
1238 prgm
= strtok_r( NULL
, ",", &buf
) )
1240 vlc_value_t val
= { .i_int
= atoi( prgm
) };
1241 TAB_APPEND(list
.i_count
, list
.p_values
, val
);
1244 if( list
.i_count
> 0 )
1245 i_es_out_mode
= ES_OUT_MODE_PARTIAL
;
1246 /* Note : we should remove the "program" callback. */
1250 else if( var_GetBool( p_input
, "sout-all" ) )
1252 i_es_out_mode
= ES_OUT_MODE_ALL
;
1255 es_out_SetMode( input_priv(p_input
)->p_es_out
, i_es_out_mode
);
1257 /* Inform the demuxer about waited group (needed only for DVB) */
1258 if( i_es_out_mode
== ES_OUT_MODE_ALL
)
1260 demux_Control( input_priv(p_input
)->master
->p_demux
, DEMUX_SET_GROUP
, -1, NULL
);
1262 else if( i_es_out_mode
== ES_OUT_MODE_PARTIAL
)
1264 demux_Control( input_priv(p_input
)->master
->p_demux
, DEMUX_SET_GROUP
, -1,
1266 TAB_CLEAN( list
.i_count
, list
.p_values
);
1270 demux_Control( input_priv(p_input
)->master
->p_demux
, DEMUX_SET_GROUP
,
1271 es_out_GetGroupForced( input_priv(p_input
)->p_es_out
), NULL
);
1275 static int Init( input_thread_t
* p_input
)
1277 input_thread_private_t
*priv
= input_priv(p_input
);
1278 input_source_t
*master
;
1280 if( var_Type( p_input
->obj
.parent
, "meta-file" ) )
1282 msg_Dbg( p_input
, "Input is a meta file: disabling unneeded options" );
1283 var_SetString( p_input
, "sout", "" );
1284 var_SetBool( p_input
, "sout-all", false );
1285 var_SetString( p_input
, "input-slave", "" );
1286 var_SetInteger( p_input
, "input-repeat", 0 );
1287 var_SetString( p_input
, "sub-file", "" );
1288 var_SetBool( p_input
, "sub-autodetect-file", false );
1291 InitStatistics( p_input
);
1293 if( InitSout( p_input
) )
1298 priv
->p_es_out
= input_EsOutTimeshiftNew( p_input
, priv
->p_es_out_display
,
1300 if( priv
->p_es_out
== NULL
)
1304 input_ChangeState( p_input
, OPENING_S
);
1305 input_SendEventCache( p_input
, 0.0 );
1308 master
= InputSourceNew( p_input
, priv
->p_item
->psz_uri
, NULL
, false );
1309 if( master
== NULL
)
1311 priv
->master
= master
;
1313 InitTitle( p_input
);
1315 /* Load master infos */
1318 if( demux_Control( master
->p_demux
, DEMUX_GET_LENGTH
, &i_length
) )
1321 i_length
= input_item_GetDuration( priv
->p_item
);
1322 input_SendEventLength( p_input
, i_length
);
1324 input_SendEventPosition( p_input
, 0.0, 0 );
1326 if( !priv
->b_preparsing
)
1328 StartTitle( p_input
);
1329 SetSubtitlesOptions( p_input
);
1330 LoadSlaves( p_input
);
1331 InitPrograms( p_input
);
1333 double f_rate
= var_InheritFloat( p_input
, "rate" );
1334 if( f_rate
!= 0.0 && f_rate
!= 1.0 )
1336 vlc_value_t val
= { .i_int
= INPUT_RATE_DEFAULT
/ f_rate
};
1337 input_ControlPush( p_input
, INPUT_CONTROL_SET_RATE
, &val
);
1341 if( !priv
->b_preparsing
&& priv
->p_sout
)
1343 priv
->b_out_pace_control
= priv
->p_sout
->i_out_pace_nocontrol
> 0;
1345 msg_Dbg( p_input
, "starting in %ssync mode",
1346 priv
->b_out_pace_control
? "a" : "" );
1349 vlc_meta_t
*p_meta
= vlc_meta_New();
1350 if( p_meta
!= NULL
)
1352 /* Get meta data from users */
1353 InputMetaUser( p_input
, p_meta
);
1355 /* Get meta data from master input */
1356 InputSourceMeta( p_input
, master
, p_meta
);
1358 /* And from slave */
1359 for( int i
= 0; i
< priv
->i_slave
; i
++ )
1360 InputSourceMeta( p_input
, priv
->slave
[i
], p_meta
);
1362 es_out_ControlSetMeta( priv
->p_es_out
, p_meta
);
1363 vlc_meta_Delete( p_meta
);
1366 msg_Dbg( p_input
, "`%s' successfully opened",
1367 input_priv(p_input
)->p_item
->psz_uri
);
1369 /* initialization is complete */
1370 input_ChangeState( p_input
, PLAYING_S
);
1375 input_ChangeState( p_input
, ERROR_S
);
1377 if( input_priv(p_input
)->p_es_out
)
1378 es_out_Delete( input_priv(p_input
)->p_es_out
);
1379 es_out_SetMode( input_priv(p_input
)->p_es_out_display
, ES_OUT_MODE_END
);
1380 if( input_priv(p_input
)->p_resource
)
1382 if( input_priv(p_input
)->p_sout
)
1383 input_resource_RequestSout( input_priv(p_input
)->p_resource
,
1384 input_priv(p_input
)->p_sout
, NULL
);
1385 input_resource_SetInput( input_priv(p_input
)->p_resource
, NULL
);
1386 if( input_priv(p_input
)->p_resource_private
)
1387 input_resource_Terminate( input_priv(p_input
)->p_resource_private
);
1390 if( !priv
->b_preparsing
&& libvlc_stats( p_input
) )
1392 #define EXIT_COUNTER( c ) do { if( input_priv(p_input)->counters.p_##c ) \
1393 stats_CounterClean( input_priv(p_input)->counters.p_##c );\
1394 input_priv(p_input)->counters.p_##c = NULL; } while(0)
1395 EXIT_COUNTER( read_bytes
);
1396 EXIT_COUNTER( read_packets
);
1397 EXIT_COUNTER( demux_read
);
1398 EXIT_COUNTER( input_bitrate
);
1399 EXIT_COUNTER( demux_bitrate
);
1400 EXIT_COUNTER( demux_corrupted
);
1401 EXIT_COUNTER( demux_discontinuity
);
1402 EXIT_COUNTER( played_abuffers
);
1403 EXIT_COUNTER( lost_abuffers
);
1404 EXIT_COUNTER( displayed_pictures
);
1405 EXIT_COUNTER( lost_pictures
);
1406 EXIT_COUNTER( decoded_audio
);
1407 EXIT_COUNTER( decoded_video
);
1408 EXIT_COUNTER( decoded_sub
);
1410 if( input_priv(p_input
)->p_sout
)
1412 EXIT_COUNTER( sout_sent_packets
);
1413 EXIT_COUNTER( sout_sent_bytes
);
1414 EXIT_COUNTER( sout_send_bitrate
);
1419 /* Mark them deleted */
1420 input_priv(p_input
)->p_es_out
= NULL
;
1421 input_priv(p_input
)->p_sout
= NULL
;
1423 return VLC_EGENERIC
;
1426 /*****************************************************************************
1427 * End: end the input thread
1428 *****************************************************************************/
1429 static void End( input_thread_t
* p_input
)
1431 input_thread_private_t
*priv
= input_priv(p_input
);
1433 /* We are at the end */
1434 input_ChangeState( p_input
, END_S
);
1436 /* Clean control variables */
1437 input_ControlVarStop( p_input
);
1439 /* Stop es out activity */
1440 es_out_SetMode( priv
->p_es_out
, ES_OUT_MODE_NONE
);
1443 for( int i
= 0; i
< priv
->i_slave
; i
++ )
1444 InputSourceDestroy( priv
->slave
[i
] );
1445 free( priv
->slave
);
1447 /* Clean up master */
1448 InputSourceDestroy( priv
->master
);
1451 priv
->i_title_offset
= 0;
1452 priv
->i_seekpoint_offset
= 0;
1454 /* Unload all modules */
1455 if( priv
->p_es_out
)
1456 es_out_Delete( priv
->p_es_out
);
1457 es_out_SetMode( priv
->p_es_out_display
, ES_OUT_MODE_END
);
1459 if( !priv
->b_preparsing
)
1461 #define CL_CO( c ) \
1463 stats_CounterClean( priv->counters.p_##c ); \
1464 priv->counters.p_##c = NULL; \
1467 if( libvlc_stats( p_input
) )
1469 /* make sure we are up to date */
1470 stats_ComputeInputStats( p_input
, priv
->p_item
->p_stats
);
1471 CL_CO( read_bytes
);
1472 CL_CO( read_packets
);
1473 CL_CO( demux_read
);
1474 CL_CO( input_bitrate
);
1475 CL_CO( demux_bitrate
);
1476 CL_CO( demux_corrupted
);
1477 CL_CO( demux_discontinuity
);
1478 CL_CO( played_abuffers
);
1479 CL_CO( lost_abuffers
);
1480 CL_CO( displayed_pictures
);
1481 CL_CO( lost_pictures
);
1482 CL_CO( decoded_audio
) ;
1483 CL_CO( decoded_video
);
1484 CL_CO( decoded_sub
) ;
1487 /* Close optional stream output instance */
1490 CL_CO( sout_sent_packets
);
1491 CL_CO( sout_sent_bytes
);
1492 CL_CO( sout_send_bitrate
);
1497 vlc_mutex_lock( &priv
->p_item
->lock
);
1498 if( priv
->i_attachment
> 0 )
1500 for( int i
= 0; i
< priv
->i_attachment
; i
++ )
1501 vlc_input_attachment_Delete( priv
->attachment
[i
] );
1502 TAB_CLEAN( priv
->i_attachment
, priv
->attachment
);
1503 free( priv
->attachment_demux
);
1504 priv
->attachment_demux
= NULL
;
1507 /* clean bookmarks */
1508 for( int i
= 0; i
< priv
->i_bookmark
; ++i
)
1509 vlc_seekpoint_Delete( priv
->pp_bookmark
[i
] );
1510 TAB_CLEAN( priv
->i_bookmark
, priv
->pp_bookmark
);
1512 vlc_mutex_unlock( &input_priv(p_input
)->p_item
->lock
);
1515 input_resource_RequestSout( input_priv(p_input
)->p_resource
,
1516 input_priv(p_input
)->p_sout
, NULL
);
1517 input_resource_SetInput( input_priv(p_input
)->p_resource
, NULL
);
1518 if( input_priv(p_input
)->p_resource_private
)
1519 input_resource_Terminate( input_priv(p_input
)->p_resource_private
);
1522 /*****************************************************************************
1524 *****************************************************************************/
1525 void input_ControlPush( input_thread_t
*p_input
,
1526 int i_type
, vlc_value_t
*p_val
)
1528 input_thread_private_t
*sys
= input_priv(p_input
);
1530 vlc_mutex_lock( &sys
->lock_control
);
1531 if( sys
->is_stopped
|| sys
->i_control
>= INPUT_CONTROL_FIFO_SIZE
)
1533 if( sys
->is_stopped
)
1534 msg_Dbg( p_input
, "input control stopped, trashing type=%d",
1537 msg_Err( p_input
, "input control fifo overflow, trashing type=%d",
1540 ControlRelease( i_type
, *p_val
);
1549 memset( &c
.val
, 0, sizeof(c
.val
) );
1551 sys
->control
[sys
->i_control
++] = c
;
1553 vlc_cond_signal( &sys
->wait_control
);
1555 vlc_mutex_unlock( &sys
->lock_control
);
1558 static int ControlGetReducedIndexLocked( input_thread_t
*p_input
)
1560 const int i_lt
= input_priv(p_input
)->control
[0].i_type
;
1562 for( i
= 1; i
< input_priv(p_input
)->i_control
; i
++ )
1564 const int i_ct
= input_priv(p_input
)->control
[i
].i_type
;
1567 ( i_ct
== INPUT_CONTROL_SET_STATE
||
1568 i_ct
== INPUT_CONTROL_SET_RATE
||
1569 i_ct
== INPUT_CONTROL_SET_POSITION
||
1570 i_ct
== INPUT_CONTROL_SET_TIME
||
1571 i_ct
== INPUT_CONTROL_SET_PROGRAM
||
1572 i_ct
== INPUT_CONTROL_SET_TITLE
||
1573 i_ct
== INPUT_CONTROL_SET_SEEKPOINT
||
1574 i_ct
== INPUT_CONTROL_SET_BOOKMARK
) )
1580 /* TODO but that's not that important
1581 - merge SET_X with SET_X_CMD
1582 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1583 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1593 static inline int ControlPop( input_thread_t
*p_input
,
1594 int *pi_type
, vlc_value_t
*p_val
,
1595 mtime_t i_deadline
, bool b_postpone_seek
)
1597 input_thread_private_t
*p_sys
= input_priv(p_input
);
1599 vlc_mutex_lock( &p_sys
->lock_control
);
1600 while( p_sys
->i_control
<= 0 ||
1601 ( b_postpone_seek
&& ControlIsSeekRequest( p_sys
->control
[0].i_type
) ) )
1603 if( p_sys
->is_stopped
)
1605 vlc_mutex_unlock( &p_sys
->lock_control
);
1606 return VLC_EGENERIC
;
1609 if( i_deadline
>= 0 )
1611 if( vlc_cond_timedwait( &p_sys
->wait_control
, &p_sys
->lock_control
,
1614 vlc_mutex_unlock( &p_sys
->lock_control
);
1615 return VLC_EGENERIC
;
1619 vlc_cond_wait( &p_sys
->wait_control
, &p_sys
->lock_control
);
1623 const int i_index
= ControlGetReducedIndexLocked( p_input
);
1625 for( int i
= 0; i
< i_index
; ++i
)
1627 /* Release Reduced controls */
1628 ControlRelease( p_sys
->control
[i
].i_type
, p_sys
->control
[i
].val
);
1632 *pi_type
= p_sys
->control
[i_index
].i_type
;
1633 *p_val
= p_sys
->control
[i_index
].val
;
1635 p_sys
->i_control
-= i_index
+ 1;
1636 if( p_sys
->i_control
> 0 )
1637 memmove( &p_sys
->control
[0], &p_sys
->control
[i_index
+1],
1638 sizeof(*p_sys
->control
) * p_sys
->i_control
);
1639 vlc_mutex_unlock( &p_sys
->lock_control
);
1643 static bool ControlIsSeekRequest( int i_type
)
1647 case INPUT_CONTROL_SET_POSITION
:
1648 case INPUT_CONTROL_SET_TIME
:
1649 case INPUT_CONTROL_SET_TITLE
:
1650 case INPUT_CONTROL_SET_TITLE_NEXT
:
1651 case INPUT_CONTROL_SET_TITLE_PREV
:
1652 case INPUT_CONTROL_SET_SEEKPOINT
:
1653 case INPUT_CONTROL_SET_SEEKPOINT_NEXT
:
1654 case INPUT_CONTROL_SET_SEEKPOINT_PREV
:
1655 case INPUT_CONTROL_SET_BOOKMARK
:
1656 case INPUT_CONTROL_NAV_ACTIVATE
:
1657 case INPUT_CONTROL_NAV_UP
:
1658 case INPUT_CONTROL_NAV_DOWN
:
1659 case INPUT_CONTROL_NAV_LEFT
:
1660 case INPUT_CONTROL_NAV_RIGHT
:
1661 case INPUT_CONTROL_NAV_POPUP
:
1662 case INPUT_CONTROL_NAV_MENU
:
1669 static void ControlRelease( int i_type
, vlc_value_t val
)
1673 case INPUT_CONTROL_ADD_SUBTITLE
:
1674 free( val
.psz_string
);
1676 case INPUT_CONTROL_ADD_SLAVE
:
1678 input_item_slave_Delete( val
.p_address
);
1680 case INPUT_CONTROL_SET_VIEWPOINT
:
1681 case INPUT_CONTROL_UPDATE_VIEWPOINT
:
1682 free( val
.p_address
);
1691 static void ControlPause( input_thread_t
*p_input
, mtime_t i_control_date
)
1693 int i_state
= PAUSE_S
;
1695 if( input_priv(p_input
)->b_can_pause
)
1697 demux_t
*p_demux
= input_priv(p_input
)->master
->p_demux
;
1699 if( demux_Control( p_demux
, DEMUX_SET_PAUSE_STATE
, true ) )
1701 msg_Warn( p_input
, "cannot set pause state" );
1707 if( es_out_SetPauseState( input_priv(p_input
)->p_es_out
, input_priv(p_input
)->b_can_pause
,
1708 true, i_control_date
) )
1710 msg_Warn( p_input
, "cannot set pause state at es_out level" );
1714 /* Switch to new state */
1715 input_ChangeState( p_input
, i_state
);
1718 static void ControlUnpause( input_thread_t
*p_input
, mtime_t i_control_date
)
1720 if( input_priv(p_input
)->b_can_pause
)
1722 demux_t
*p_demux
= input_priv(p_input
)->master
->p_demux
;
1724 if( demux_Control( p_demux
, DEMUX_SET_PAUSE_STATE
, false ) )
1726 msg_Err( p_input
, "cannot resume" );
1727 input_ChangeState( p_input
, ERROR_S
);
1732 /* Switch to play */
1733 input_ChangeState( p_input
, PLAYING_S
);
1734 es_out_SetPauseState( input_priv(p_input
)->p_es_out
, false, false, i_control_date
);
1737 static bool Control( input_thread_t
*p_input
,
1738 int i_type
, vlc_value_t val
)
1740 const mtime_t i_control_date
= mdate();
1741 /* FIXME b_force_update is abused, it should be carefully checked */
1742 bool b_force_update
= false;
1745 return b_force_update
;
1749 case INPUT_CONTROL_SET_POSITION
:
1751 if( input_priv(p_input
)->b_recording
)
1753 msg_Err( p_input
, "INPUT_CONTROL_SET_POSITION ignored while recording" );
1757 float f_pos
= val
.f_float
;
1760 else if( f_pos
> 1.f
)
1762 /* Reset the decoders states and clock sync (before calling the demuxer */
1763 es_out_SetTime( input_priv(p_input
)->p_es_out
, -1 );
1764 if( demux_Control( input_priv(p_input
)->master
->p_demux
, DEMUX_SET_POSITION
,
1765 (double) f_pos
, !input_priv(p_input
)->b_fast_seek
) )
1767 msg_Err( p_input
, "INPUT_CONTROL_SET_POSITION "
1768 "%2.1f%% failed", (double)(f_pos
* 100.f
) );
1772 if( input_priv(p_input
)->i_slave
> 0 )
1773 SlaveSeek( p_input
);
1774 input_priv(p_input
)->master
->b_eof
= false;
1776 b_force_update
= true;
1781 case INPUT_CONTROL_SET_TIME
:
1786 if( input_priv(p_input
)->b_recording
)
1788 msg_Err( p_input
, "INPUT_CONTROL_SET_TIME ignored while recording" );
1796 /* Reset the decoders states and clock sync (before calling the demuxer */
1797 es_out_SetTime( input_priv(p_input
)->p_es_out
, -1 );
1799 i_ret
= demux_Control( input_priv(p_input
)->master
->p_demux
,
1800 DEMUX_SET_TIME
, i_time
,
1801 !input_priv(p_input
)->b_fast_seek
);
1806 /* Emulate it with a SET_POS */
1807 if( !demux_Control( input_priv(p_input
)->master
->p_demux
,
1808 DEMUX_GET_LENGTH
, &i_length
) && i_length
> 0 )
1810 double f_pos
= (double)i_time
/ (double)i_length
;
1811 i_ret
= demux_Control( input_priv(p_input
)->master
->p_demux
,
1812 DEMUX_SET_POSITION
, f_pos
,
1813 !input_priv(p_input
)->b_fast_seek
);
1818 msg_Warn( p_input
, "INPUT_CONTROL_SET_TIME %"PRId64
1819 " failed or not possible", i_time
);
1823 if( input_priv(p_input
)->i_slave
> 0 )
1824 SlaveSeek( p_input
);
1825 input_priv(p_input
)->master
->b_eof
= false;
1827 b_force_update
= true;
1832 case INPUT_CONTROL_SET_STATE
:
1836 if( input_priv(p_input
)->i_state
== PAUSE_S
)
1838 ControlUnpause( p_input
, i_control_date
);
1839 b_force_update
= true;
1843 if( input_priv(p_input
)->i_state
== PLAYING_S
)
1845 ControlPause( p_input
, i_control_date
);
1846 b_force_update
= true;
1850 msg_Err( p_input
, "invalid INPUT_CONTROL_SET_STATE" );
1854 case INPUT_CONTROL_SET_RATE
:
1856 /* Get rate and direction */
1857 int i_rate
= abs( val
.i_int
);
1858 int i_rate_sign
= val
.i_int
< 0 ? -1 : 1;
1860 /* Check rate bound */
1861 if( i_rate
< INPUT_RATE_MIN
)
1863 msg_Dbg( p_input
, "cannot set rate faster" );
1864 i_rate
= INPUT_RATE_MIN
;
1866 else if( i_rate
> INPUT_RATE_MAX
)
1868 msg_Dbg( p_input
, "cannot set rate slower" );
1869 i_rate
= INPUT_RATE_MAX
;
1872 /* Apply direction */
1873 if( i_rate_sign
< 0 )
1875 if( input_priv(p_input
)->master
->b_rescale_ts
)
1877 msg_Dbg( p_input
, "cannot set negative rate" );
1878 i_rate
= input_priv(p_input
)->i_rate
;
1879 assert( i_rate
> 0 );
1883 i_rate
*= i_rate_sign
;
1887 if( i_rate
!= INPUT_RATE_DEFAULT
&&
1888 ( ( !input_priv(p_input
)->b_can_rate_control
&& !input_priv(p_input
)->master
->b_rescale_ts
) ||
1889 ( input_priv(p_input
)->p_sout
&& !input_priv(p_input
)->b_out_pace_control
) ) )
1891 msg_Dbg( p_input
, "cannot change rate" );
1892 i_rate
= INPUT_RATE_DEFAULT
;
1894 if( i_rate
!= input_priv(p_input
)->i_rate
&&
1895 !input_priv(p_input
)->b_can_pace_control
&& input_priv(p_input
)->b_can_rate_control
)
1897 if( !input_priv(p_input
)->master
->b_rescale_ts
)
1898 es_out_Control( input_priv(p_input
)->p_es_out
, ES_OUT_RESET_PCR
);
1900 if( demux_Control( input_priv(p_input
)->master
->p_demux
, DEMUX_SET_RATE
,
1903 msg_Warn( p_input
, "ACCESS/DEMUX_SET_RATE failed" );
1904 i_rate
= input_priv(p_input
)->i_rate
;
1909 if( i_rate
!= input_priv(p_input
)->i_rate
)
1911 input_priv(p_input
)->i_rate
= i_rate
;
1912 input_SendEventRate( p_input
, i_rate
);
1914 if( input_priv(p_input
)->master
->b_rescale_ts
)
1916 const int i_rate_source
= (input_priv(p_input
)->b_can_pace_control
|| input_priv(p_input
)->b_can_rate_control
) ? i_rate
: INPUT_RATE_DEFAULT
;
1917 es_out_SetRate( input_priv(p_input
)->p_es_out
, i_rate_source
, i_rate
);
1920 b_force_update
= true;
1925 case INPUT_CONTROL_SET_PROGRAM
:
1926 /* No need to force update, es_out does it if needed */
1927 es_out_Control( input_priv(p_input
)->p_es_out
,
1928 ES_OUT_SET_GROUP
, val
.i_int
);
1930 demux_Control( input_priv(p_input
)->master
->p_demux
, DEMUX_SET_GROUP
, val
.i_int
,
1934 case INPUT_CONTROL_SET_ES
:
1935 /* No need to force update, es_out does it if needed */
1936 es_out_Control( input_priv(p_input
)->p_es_out_display
,
1937 ES_OUT_SET_ES_BY_ID
, (int)val
.i_int
);
1939 demux_Control( input_priv(p_input
)->master
->p_demux
, DEMUX_SET_ES
, (int)val
.i_int
);
1942 case INPUT_CONTROL_RESTART_ES
:
1943 es_out_Control( input_priv(p_input
)->p_es_out_display
,
1944 ES_OUT_RESTART_ES_BY_ID
, (int)val
.i_int
);
1947 case INPUT_CONTROL_SET_VIEWPOINT
:
1948 case INPUT_CONTROL_UPDATE_VIEWPOINT
:
1950 input_thread_private_t
*priv
= input_priv(p_input
);
1951 const vlc_viewpoint_t
*p_vp
= val
.p_address
;
1952 if ( i_type
== INPUT_CONTROL_SET_VIEWPOINT
)
1953 priv
->viewpoint
= *p_vp
;
1956 priv
->viewpoint
.yaw
+= p_vp
->yaw
;
1957 priv
->viewpoint
.pitch
+= p_vp
->pitch
;
1958 priv
->viewpoint
.roll
+= p_vp
->roll
;
1959 priv
->viewpoint
.fov
+= p_vp
->fov
;
1962 priv
->viewpoint
.yaw
= fmodf( priv
->viewpoint
.yaw
, 360.f
);
1963 priv
->viewpoint
.pitch
= fmodf( priv
->viewpoint
.pitch
, 360.f
);
1964 priv
->viewpoint
.roll
= fmodf( priv
->viewpoint
.roll
, 360.f
);
1965 priv
->viewpoint
.fov
= VLC_CLIP( priv
->viewpoint
.fov
,
1966 FIELD_OF_VIEW_DEGREES_MIN
,
1967 FIELD_OF_VIEW_DEGREES_MAX
);
1969 vout_thread_t
**pp_vout
;
1971 input_resource_HoldVouts( priv
->p_resource
, &pp_vout
, &i_vout
);
1973 for( size_t i
= 0; i
< i_vout
; ++i
)
1975 var_SetAddress( pp_vout
[i
], "viewpoint", &priv
->viewpoint
);
1976 vlc_object_release( pp_vout
[i
] );
1982 case INPUT_CONTROL_SET_AUDIO_DELAY
:
1983 input_SendEventAudioDelay( p_input
, val
.i_int
);
1984 UpdatePtsDelay( p_input
);
1987 case INPUT_CONTROL_SET_SPU_DELAY
:
1988 input_SendEventSubtitleDelay( p_input
, val
.i_int
);
1989 UpdatePtsDelay( p_input
);
1992 case INPUT_CONTROL_SET_TITLE
:
1993 case INPUT_CONTROL_SET_TITLE_NEXT
:
1994 case INPUT_CONTROL_SET_TITLE_PREV
:
1996 if( input_priv(p_input
)->b_recording
)
1998 msg_Err( p_input
, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
2001 if( input_priv(p_input
)->master
->i_title
<= 0 )
2004 int i_title
= demux_GetTitle( input_priv(p_input
)->master
->p_demux
);
2005 if( i_type
== INPUT_CONTROL_SET_TITLE_PREV
)
2007 else if( i_type
== INPUT_CONTROL_SET_TITLE_NEXT
)
2010 i_title
= val
.i_int
;
2011 if( i_title
< 0 || i_title
>= input_priv(p_input
)->master
->i_title
)
2014 es_out_SetTime( input_priv(p_input
)->p_es_out
, -1 );
2015 demux_Control( input_priv(p_input
)->master
->p_demux
,
2016 DEMUX_SET_TITLE
, i_title
);
2017 input_SendEventTitle( p_input
, i_title
);
2020 case INPUT_CONTROL_SET_SEEKPOINT
:
2021 case INPUT_CONTROL_SET_SEEKPOINT_NEXT
:
2022 case INPUT_CONTROL_SET_SEEKPOINT_PREV
:
2024 if( input_priv(p_input
)->b_recording
)
2026 msg_Err( p_input
, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
2029 if( input_priv(p_input
)->master
->i_title
<= 0 )
2032 demux_t
*p_demux
= input_priv(p_input
)->master
->p_demux
;
2034 int i_title
= demux_GetTitle( p_demux
);
2035 int i_seekpoint
= demux_GetSeekpoint( p_demux
);
2037 if( i_type
== INPUT_CONTROL_SET_SEEKPOINT_PREV
)
2039 int64_t i_seekpoint_time
= input_priv(p_input
)->master
->title
[i_title
]->seekpoint
[i_seekpoint
]->i_time_offset
;
2040 int64_t i_input_time
= var_GetInteger( p_input
, "time" );
2041 if( i_seekpoint_time
>= 0 && i_input_time
>= 0 )
2043 if( i_input_time
< i_seekpoint_time
+ 3000000 )
2049 else if( i_type
== INPUT_CONTROL_SET_SEEKPOINT_NEXT
)
2052 i_seekpoint
= val
.i_int
;
2054 || i_seekpoint
>= input_priv(p_input
)->master
->title
[i_title
]->i_seekpoint
)
2057 es_out_SetTime( input_priv(p_input
)->p_es_out
, -1 );
2058 demux_Control( input_priv(p_input
)->master
->p_demux
,
2059 DEMUX_SET_SEEKPOINT
, i_seekpoint
);
2060 input_SendEventSeekpoint( p_input
, i_title
, i_seekpoint
);
2064 case INPUT_CONTROL_ADD_SUBTITLE
:
2065 if( val
.psz_string
)
2067 char *psz_uri
= input_SubtitleFile2Uri( p_input
, val
.psz_string
);
2068 if( psz_uri
!= NULL
)
2070 input_SlaveSourceAdd( p_input
, SLAVE_TYPE_SPU
, psz_uri
,
2077 case INPUT_CONTROL_ADD_SLAVE
:
2080 input_item_slave_t
*p_item_slave
= val
.p_address
;
2081 unsigned i_flags
= SLAVE_ADD_CANFAIL
| SLAVE_ADD_SET_TIME
;
2082 if( p_item_slave
->b_forced
)
2083 i_flags
|= SLAVE_ADD_FORCED
;
2085 if( input_SlaveSourceAdd( p_input
, p_item_slave
->i_type
,
2086 p_item_slave
->psz_uri
, i_flags
)
2089 /* Update item slaves */
2090 input_item_AddSlave( input_priv(p_input
)->p_item
, p_item_slave
);
2091 /* The slave is now owned by the item */
2092 val
.p_address
= NULL
;
2097 case INPUT_CONTROL_SET_RECORD_STATE
:
2098 if( !!input_priv(p_input
)->b_recording
!= !!val
.b_bool
)
2100 if( input_priv(p_input
)->master
->b_can_stream_record
)
2102 if( demux_Control( input_priv(p_input
)->master
->p_demux
,
2103 DEMUX_SET_RECORD_STATE
, val
.b_bool
) )
2108 if( es_out_SetRecordState( input_priv(p_input
)->p_es_out_display
, val
.b_bool
) )
2111 input_priv(p_input
)->b_recording
= val
.b_bool
;
2113 input_SendEventRecord( p_input
, val
.b_bool
);
2115 b_force_update
= true;
2119 case INPUT_CONTROL_SET_FRAME_NEXT
:
2120 if( input_priv(p_input
)->i_state
== PAUSE_S
)
2122 es_out_SetFrameNext( input_priv(p_input
)->p_es_out
);
2124 else if( input_priv(p_input
)->i_state
== PLAYING_S
)
2126 ControlPause( p_input
, i_control_date
);
2130 msg_Err( p_input
, "invalid state for frame next" );
2132 b_force_update
= true;
2135 case INPUT_CONTROL_SET_BOOKMARK
:
2137 mtime_t time_offset
= -1;
2139 vlc_mutex_lock( &input_priv(p_input
)->p_item
->lock
);
2140 if( val
.i_int
>= 0 && val
.i_int
< input_priv(p_input
)->i_bookmark
)
2142 const seekpoint_t
*p_bookmark
= input_priv(p_input
)->pp_bookmark
[val
.i_int
];
2143 time_offset
= p_bookmark
->i_time_offset
;
2145 vlc_mutex_unlock( &input_priv(p_input
)->p_item
->lock
);
2147 if( time_offset
< 0 )
2149 msg_Err( p_input
, "invalid bookmark %"PRId64
, val
.i_int
);
2153 val
.i_int
= time_offset
;
2154 b_force_update
= Control( p_input
, INPUT_CONTROL_SET_TIME
, val
);
2158 case INPUT_CONTROL_NAV_ACTIVATE
:
2159 case INPUT_CONTROL_NAV_UP
:
2160 case INPUT_CONTROL_NAV_DOWN
:
2161 case INPUT_CONTROL_NAV_LEFT
:
2162 case INPUT_CONTROL_NAV_RIGHT
:
2163 case INPUT_CONTROL_NAV_POPUP
:
2164 case INPUT_CONTROL_NAV_MENU
:
2165 demux_Control( input_priv(p_input
)->master
->p_demux
, i_type
2166 - INPUT_CONTROL_NAV_ACTIVATE
+ DEMUX_NAV_ACTIVATE
);
2170 msg_Err( p_input
, "not yet implemented" );
2174 ControlRelease( i_type
, val
);
2175 return b_force_update
;
2178 /*****************************************************************************
2179 * UpdateTitleSeekpoint
2180 *****************************************************************************/
2181 static int UpdateTitleSeekpoint( input_thread_t
*p_input
,
2182 int i_title
, int i_seekpoint
)
2184 int i_title_end
= input_priv(p_input
)->master
->i_title_end
-
2185 input_priv(p_input
)->master
->i_title_offset
;
2186 int i_seekpoint_end
= input_priv(p_input
)->master
->i_seekpoint_end
-
2187 input_priv(p_input
)->master
->i_seekpoint_offset
;
2189 if( i_title_end
>= 0 && i_seekpoint_end
>= 0 )
2191 if( i_title
> i_title_end
||
2192 ( i_title
== i_title_end
&& i_seekpoint
> i_seekpoint_end
) )
2193 return VLC_DEMUXER_EOF
;
2195 else if( i_seekpoint_end
>= 0 )
2197 if( i_seekpoint
> i_seekpoint_end
)
2198 return VLC_DEMUXER_EOF
;
2200 else if( i_title_end
>= 0 )
2202 if( i_title
> i_title_end
)
2203 return VLC_DEMUXER_EOF
;
2205 return VLC_DEMUXER_SUCCESS
;
2207 /*****************************************************************************
2209 *****************************************************************************/
2210 static int UpdateTitleSeekpointFromDemux( input_thread_t
*p_input
)
2212 demux_t
*p_demux
= input_priv(p_input
)->master
->p_demux
;
2214 /* TODO event-like */
2215 if( demux_TestAndClearFlags( p_demux
, INPUT_UPDATE_TITLE
) )
2216 input_SendEventTitle( p_input
, demux_GetTitle( p_demux
) );
2218 if( demux_TestAndClearFlags( p_demux
, INPUT_UPDATE_SEEKPOINT
) )
2219 input_SendEventSeekpoint( p_input
, demux_GetTitle( p_demux
),
2220 demux_GetSeekpoint( p_demux
) );
2222 return UpdateTitleSeekpoint( p_input
,
2223 demux_GetTitle( p_demux
),
2224 demux_GetSeekpoint( p_demux
) );
2227 static void UpdateGenericFromDemux( input_thread_t
*p_input
)
2229 demux_t
*p_demux
= input_priv(p_input
)->master
->p_demux
;
2231 if( demux_TestAndClearFlags( p_demux
, INPUT_UPDATE_META
) )
2232 InputUpdateMeta( p_input
, p_demux
);
2238 if( !demux_Control( p_demux
, DEMUX_GET_SIGNAL
, &quality
, &strength
) )
2239 input_SendEventSignal( p_input
, quality
, strength
);
2243 static void UpdateTitleListfromDemux( input_thread_t
*p_input
)
2245 input_source_t
*in
= input_priv(p_input
)->master
;
2247 /* Delete the preexisting titles */
2248 if( in
->i_title
> 0 )
2250 for( int i
= 0; i
< in
->i_title
; i
++ )
2251 vlc_input_title_Delete( in
->title
[i
] );
2252 TAB_CLEAN( in
->i_title
, in
->title
);
2253 in
->b_title_demux
= false;
2256 /* Get the new title list */
2257 if( demux_Control( in
->p_demux
, DEMUX_GET_TITLE_INFO
,
2258 &in
->title
, &in
->i_title
,
2259 &in
->i_title_offset
, &in
->i_seekpoint_offset
) )
2260 TAB_INIT( in
->i_title
, in
->title
);
2262 in
->b_title_demux
= true;
2264 InitTitle( p_input
);
2268 InputStreamHandleAnchor( input_source_t
*source
, stream_t
**stream
,
2269 char const *anchor
)
2272 if( stream_extractor_AttachParsed( stream
, anchor
, &extra
) )
2274 msg_Err( source
, "unable to attach stream-extractors for %s",
2275 (*stream
)->psz_url
);
2277 return VLC_EGENERIC
;
2280 if( vlc_stream_directory_Attach( stream
, NULL
) )
2281 msg_Dbg( source
, "attachment of directory-extractor failed for %s",
2282 (*stream
)->psz_url
);
2284 MRLSections( extra
? extra
: "",
2285 &source
->i_title_start
, &source
->i_title_end
,
2286 &source
->i_seekpoint_start
, &source
->i_seekpoint_end
);
2291 static demux_t
*InputDemuxNew( input_thread_t
*p_input
, input_source_t
*p_source
,
2292 const char *psz_access
, const char *psz_demux
,
2293 const char *psz_path
, const char *psz_anchor
)
2295 input_thread_private_t
*priv
= input_priv(p_input
);
2296 demux_t
*p_demux
= NULL
;
2298 /* first, try to create an access demux */
2299 p_demux
= demux_NewAdvanced( VLC_OBJECT( p_source
), p_input
,
2300 psz_access
, psz_demux
, psz_path
,
2301 NULL
, priv
->p_es_out
, priv
->b_preparsing
);
2304 MRLSections( psz_anchor
,
2305 &p_source
->i_title_start
, &p_source
->i_title_end
,
2306 &p_source
->i_seekpoint_start
, &p_source
->i_seekpoint_end
);
2311 /* not an access-demux: create the underlying access stream */
2314 if( asprintf( &psz_base_mrl
, "%s://%s", psz_access
, psz_path
) < 0 )
2317 char *psz_filters
= var_InheritString( p_source
, "stream-filter" );
2318 stream_t
* p_stream
= stream_AccessNew( VLC_OBJECT( p_source
), p_input
,
2321 FREENULL( psz_base_mrl
);
2323 if( p_stream
== NULL
)
2326 p_stream
= stream_FilterAutoNew( p_stream
);
2328 /* attach explicit stream filters to stream */
2330 p_stream
= stream_FilterChainNew( p_stream
, psz_filters
);
2332 FREENULL( psz_filters
);
2334 /* handle anchors */
2335 if( InputStreamHandleAnchor( p_source
, &p_stream
, psz_anchor
) )
2338 /* attach conditional record stream-filter */
2339 if( var_InheritBool( p_source
, "input-record-native" ) )
2340 p_stream
= stream_FilterChainNew( p_stream
, "record" );
2342 /* create a regular demux with the access stream created */
2343 p_demux
= demux_NewAdvanced( VLC_OBJECT( p_source
), p_input
,
2344 psz_access
, psz_demux
, psz_path
,
2345 p_stream
, priv
->p_es_out
,
2346 priv
->b_preparsing
);
2351 free( psz_base_mrl
);
2352 free( psz_filters
);
2355 vlc_stream_Delete( p_stream
);
2360 /*****************************************************************************
2362 *****************************************************************************/
2363 static input_source_t
*InputSourceNew( input_thread_t
*p_input
,
2364 const char *psz_mrl
,
2365 const char *psz_forced_demux
,
2366 bool b_in_can_fail
)
2368 input_source_t
*in
= vlc_custom_create( p_input
, sizeof( *in
),
2370 if( unlikely(in
== NULL
) )
2373 const char *psz_access
, *psz_demux
, *psz_path
, *psz_anchor
= NULL
;
2376 char *psz_dup
= strdup( psz_mrl
);
2377 char *psz_demux_var
= NULL
;
2379 if( psz_dup
== NULL
)
2381 vlc_object_release( in
);
2386 input_SplitMRL( &psz_access
, &psz_demux
, &psz_path
, &psz_anchor
, psz_dup
);
2388 if( psz_demux
== NULL
|| psz_demux
[0] == '\0' )
2389 psz_demux
= psz_demux_var
= var_InheritString( in
, "demux" );
2391 if( psz_forced_demux
!= NULL
)
2392 psz_demux
= psz_forced_demux
;
2394 if( psz_demux
== NULL
)
2397 msg_Dbg( p_input
, "`%s' gives access `%s' demux `%s' path `%s'",
2398 psz_mrl
, psz_access
, psz_demux
, psz_path
);
2400 if( input_priv(p_input
)->master
== NULL
/* XXX ugly */)
2401 { /* On master stream only, use input-list */
2402 char *str
= var_InheritString( p_input
, "input-list" );
2407 var_Create( p_input
, "concat-list", VLC_VAR_STRING
);
2408 if( likely(asprintf( &list
, "%s://%s,%s", psz_access
, psz_path
,
2411 var_SetString( p_input
, "concat-list", list
);
2415 psz_access
= "concat";
2419 if( strcasecmp( psz_access
, "concat" ) )
2420 { /* Autodetect extra files if none specified */
2424 TAB_INIT( count
, tab
);
2425 InputGetExtraFiles( p_input
, &count
, &tab
, &psz_access
, psz_path
);
2430 for( int i
= 0; i
< count
; i
++ )
2433 if( asprintf( &str
, "%s,%s", list
? list
: psz_mrl
,
2442 var_Create( p_input
, "concat-list", VLC_VAR_STRING
);
2443 if( likely(list
!= NULL
) )
2445 var_SetString( p_input
, "concat-list", list
);
2449 TAB_CLEAN( count
, tab
);
2452 in
->p_demux
= InputDemuxNew( p_input
, in
, psz_access
, psz_demux
,
2453 psz_path
, psz_anchor
);
2455 free( psz_demux_var
);
2458 if( in
->p_demux
== NULL
)
2460 if( !b_in_can_fail
&& !input_Stopped( p_input
) )
2461 vlc_dialog_display_error( p_input
, _("Your input can't be opened"),
2462 _("VLC is unable to open the MRL '%s'."
2463 " Check the log for details."), psz_mrl
);
2464 vlc_object_release( in
);
2468 char *psz_demux_chain
= var_GetNonEmptyString(p_input
, "demux-filter");
2469 if( psz_demux_chain
!= NULL
) /* add the chain of demux filters */
2471 in
->p_demux
= demux_FilterChainNew( in
->p_demux
, psz_demux_chain
);
2472 free( psz_demux_chain
);
2474 if( in
->p_demux
== NULL
)
2476 msg_Err(p_input
, "Failed to create demux filter");
2477 vlc_object_release( in
);
2482 /* Get infos from (access_)demux */
2484 if( demux_Control( in
->p_demux
, DEMUX_CAN_SEEK
, &b_can_seek
) )
2486 var_SetBool( p_input
, "can-seek", b_can_seek
);
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
) )
2499 in
->b_can_rate_control
= false;
2500 in
->b_rescale_ts
= true;
2503 in
->b_rescale_ts
= !in
->b_can_rate_control
;
2507 in
->b_can_rate_control
= true;
2508 in
->b_rescale_ts
= true;
2511 demux_Control( in
->p_demux
, DEMUX_CAN_PAUSE
, &in
->b_can_pause
);
2513 var_SetBool( p_input
, "can-pause", in
->b_can_pause
|| !in
->b_can_pace_control
); /* XXX temporary because of es_out_timeshift*/
2514 var_SetBool( p_input
, "can-rate", !in
->b_can_pace_control
|| in
->b_can_rate_control
); /* XXX temporary because of es_out_timeshift*/
2515 var_SetBool( p_input
, "can-rewind", !in
->b_rescale_ts
&& !in
->b_can_pace_control
&& in
->b_can_rate_control
);
2517 /* Set record capabilities */
2518 if( demux_Control( in
->p_demux
, DEMUX_CAN_RECORD
, &in
->b_can_stream_record
) )
2519 in
->b_can_stream_record
= false;
2521 if( !var_GetBool( p_input
, "input-record-native" ) )
2522 in
->b_can_stream_record
= false;
2523 var_SetBool( p_input
, "can-record", true );
2525 var_SetBool( p_input
, "can-record", in
->b_can_stream_record
);
2529 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2530 if( !input_priv(p_input
)->b_preparsing
)
2532 if( demux_Control( in
->p_demux
, DEMUX_GET_TITLE_INFO
,
2533 &in
->title
, &in
->i_title
,
2534 &in
->i_title_offset
, &in
->i_seekpoint_offset
))
2536 TAB_INIT( in
->i_title
, in
->title
);
2540 in
->b_title_demux
= true;
2544 input_attachment_t
**attachment
;
2545 if( !demux_Control( in
->p_demux
, DEMUX_GET_ATTACHMENTS
,
2546 &attachment
, &i_attachment
) )
2548 vlc_mutex_lock( &input_priv(p_input
)->p_item
->lock
);
2549 AppendAttachment( &input_priv(p_input
)->i_attachment
, &input_priv(p_input
)->attachment
, &input_priv(p_input
)->attachment_demux
,
2550 i_attachment
, attachment
, in
->p_demux
);
2551 vlc_mutex_unlock( &input_priv(p_input
)->p_item
->lock
);
2554 demux_Control( in
->p_demux
, DEMUX_GET_PTS_DELAY
, &in
->i_pts_delay
);
2555 if( in
->i_pts_delay
> INPUT_PTS_DELAY_MAX
)
2556 in
->i_pts_delay
= INPUT_PTS_DELAY_MAX
;
2557 else if( in
->i_pts_delay
< 0 )
2558 in
->i_pts_delay
= 0;
2561 if( demux_Control( in
->p_demux
, DEMUX_GET_FPS
, &in
->f_fps
) )
2564 if( var_GetInteger( p_input
, "clock-synchro" ) != -1 )
2565 in
->b_can_pace_control
= !var_GetInteger( p_input
, "clock-synchro" );
2570 /*****************************************************************************
2571 * InputSourceDestroy:
2572 *****************************************************************************/
2573 static void InputSourceDestroy( input_source_t
*in
)
2578 demux_Delete( in
->p_demux
);
2580 if( in
->i_title
> 0 )
2582 for( i
= 0; i
< in
->i_title
; i
++ )
2583 vlc_input_title_Delete( in
->title
[i
] );
2584 TAB_CLEAN( in
->i_title
, in
->title
);
2587 vlc_object_release( in
);
2590 /*****************************************************************************
2592 *****************************************************************************/
2593 static void InputSourceMeta( input_thread_t
*p_input
,
2594 input_source_t
*p_source
, vlc_meta_t
*p_meta
)
2596 demux_t
*p_demux
= p_source
->p_demux
;
2598 /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
2601 bool has_meta
= false;
2603 /* Read demux meta */
2604 if( !demux_Control( p_demux
, DEMUX_GET_META
, p_meta
) )
2607 bool has_unsupported
;
2608 if( demux_Control( p_demux
, DEMUX_HAS_UNSUPPORTED_META
, &has_unsupported
) )
2609 has_unsupported
= true;
2611 /* If the demux report unsupported meta data, or if we don't have meta data
2612 * try an external "meta reader" */
2613 if( has_meta
&& !has_unsupported
)
2616 demux_meta_t
*p_demux_meta
=
2617 vlc_custom_create( p_source
, sizeof( *p_demux_meta
), "demux meta" );
2618 if( unlikely(p_demux_meta
== NULL
) )
2620 p_demux_meta
->p_item
= input_priv(p_input
)->p_item
;
2622 module_t
*p_id3
= module_need( p_demux_meta
, "meta reader", NULL
, false );
2625 if( p_demux_meta
->p_meta
)
2627 vlc_meta_Merge( p_meta
, p_demux_meta
->p_meta
);
2628 vlc_meta_Delete( p_demux_meta
->p_meta
);
2631 if( p_demux_meta
->i_attachments
> 0 )
2633 vlc_mutex_lock( &input_priv(p_input
)->p_item
->lock
);
2634 AppendAttachment( &input_priv(p_input
)->i_attachment
, &input_priv(p_input
)->attachment
, &input_priv(p_input
)->attachment_demux
,
2635 p_demux_meta
->i_attachments
, p_demux_meta
->attachments
, p_demux
);
2636 vlc_mutex_unlock( &input_priv(p_input
)->p_item
->lock
);
2638 module_unneed( p_demux
, p_id3
);
2640 vlc_object_release( p_demux_meta
);
2644 static void SlaveDemux( input_thread_t
*p_input
)
2649 if( demux_Control( input_priv(p_input
)->master
->p_demux
, DEMUX_GET_TIME
, &i_time
) )
2651 msg_Err( p_input
, "demux doesn't like DEMUX_GET_TIME" );
2655 for( i
= 0; i
< input_priv(p_input
)->i_slave
; i
++ )
2657 input_source_t
*in
= input_priv(p_input
)->slave
[i
];
2663 /* Call demux_Demux until we have read enough data */
2664 if( demux_Control( in
->p_demux
, DEMUX_SET_NEXT_DEMUX_TIME
, i_time
) )
2669 if( demux_Control( in
->p_demux
, DEMUX_GET_TIME
, &i_stime
) )
2671 msg_Err( p_input
, "slave[%d] doesn't like "
2672 "DEMUX_GET_TIME -> EOF", i
);
2677 if( i_stime
>= i_time
)
2683 if( ( i_ret
= demux_Demux( in
->p_demux
) ) <= 0 )
2689 i_ret
= demux_Demux( in
->p_demux
);
2694 msg_Dbg( p_input
, "slave %d EOF", i
);
2700 static void SlaveSeek( input_thread_t
*p_input
)
2705 if( demux_Control( input_priv(p_input
)->master
->p_demux
, DEMUX_GET_TIME
, &i_time
) )
2707 msg_Err( p_input
, "demux doesn't like DEMUX_GET_TIME" );
2711 for( i
= 0; i
< input_priv(p_input
)->i_slave
; i
++ )
2713 input_source_t
*in
= input_priv(p_input
)->slave
[i
];
2715 if( demux_Control( in
->p_demux
, DEMUX_SET_TIME
, i_time
, true ) )
2718 msg_Err( p_input
, "seek failed for slave %d -> EOF", i
);
2728 /*****************************************************************************
2730 *****************************************************************************/
2731 static void InputMetaUser( input_thread_t
*p_input
, vlc_meta_t
*p_meta
)
2733 static const struct { int i_meta
; const char *psz_name
; } p_list
[] = {
2734 { vlc_meta_Title
, "meta-title" },
2735 { vlc_meta_Artist
, "meta-artist" },
2736 { vlc_meta_Genre
, "meta-genre" },
2737 { vlc_meta_Copyright
, "meta-copyright" },
2738 { vlc_meta_Description
, "meta-description" },
2739 { vlc_meta_Date
, "meta-date" },
2740 { vlc_meta_URL
, "meta-url" },
2744 /* Get meta information from user */
2745 for( int i
= 0; p_list
[i
].psz_name
; i
++ )
2747 char *psz_string
= var_GetNonEmptyString( p_input
, p_list
[i
].psz_name
);
2751 EnsureUTF8( psz_string
);
2752 vlc_meta_Set( p_meta
, p_list
[i
].i_meta
, psz_string
);
2757 static void AppendAttachment( int *pi_attachment
, input_attachment_t
***ppp_attachment
,
2758 const demux_t
***ppp_attachment_demux
,
2759 int i_new
, input_attachment_t
**pp_new
, const demux_t
*p_demux
)
2761 int i_attachment
= *pi_attachment
;
2764 input_attachment_t
**pp_att
= realloc( *ppp_attachment
,
2765 sizeof(*pp_att
) * ( i_attachment
+ i_new
) );
2766 if( likely(pp_att
) )
2768 *ppp_attachment
= pp_att
;
2769 const demux_t
**pp_attdmx
= realloc( *ppp_attachment_demux
,
2770 sizeof(*pp_attdmx
) * ( i_attachment
+ i_new
) );
2771 if( likely(pp_attdmx
) )
2773 *ppp_attachment_demux
= pp_attdmx
;
2775 for( i
= 0; i
< i_new
; i
++ )
2777 pp_att
[i_attachment
] = pp_new
[i
];
2778 pp_attdmx
[i_attachment
++] = p_demux
;
2781 *pi_attachment
= i_attachment
;
2787 /* on alloc errors */
2788 for( i
= 0; i
< i_new
; i
++ )
2789 vlc_input_attachment_Delete( pp_new
[i
] );
2793 /*****************************************************************************
2794 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2795 * arturl and locking issue.
2796 *****************************************************************************/
2797 static void InputUpdateMeta( input_thread_t
*p_input
, demux_t
*p_demux
)
2799 vlc_meta_t
*p_meta
= vlc_meta_New();
2800 if( unlikely(p_meta
== NULL
) )
2803 demux_Control( p_demux
, DEMUX_GET_META
, p_meta
);
2805 /* If metadata changed, then the attachments might have changed.
2806 We need to update them in case they contain album art. */
2807 input_attachment_t
**attachment
;
2810 if( !demux_Control( p_demux
, DEMUX_GET_ATTACHMENTS
,
2811 &attachment
, &i_attachment
) )
2813 vlc_mutex_lock( &input_priv(p_input
)->p_item
->lock
);
2814 if( input_priv(p_input
)->i_attachment
> 0 )
2817 for( int i
= 0; i
< input_priv(p_input
)->i_attachment
; i
++ )
2819 if( input_priv(p_input
)->attachment_demux
[i
] == p_demux
)
2820 vlc_input_attachment_Delete( input_priv(p_input
)->attachment
[i
] );
2823 input_priv(p_input
)->attachment
[j
] = input_priv(p_input
)->attachment
[i
];
2824 input_priv(p_input
)->attachment_demux
[j
] = input_priv(p_input
)->attachment_demux
[i
];
2828 input_priv(p_input
)->i_attachment
= j
;
2830 AppendAttachment( &input_priv(p_input
)->i_attachment
, &input_priv(p_input
)->attachment
, &input_priv(p_input
)->attachment_demux
,
2831 i_attachment
, attachment
, p_demux
);
2832 vlc_mutex_unlock( &input_priv(p_input
)->p_item
->lock
);
2835 es_out_ControlSetMeta( input_priv(p_input
)->p_es_out
, p_meta
);
2836 vlc_meta_Delete( p_meta
);
2839 /*****************************************************************************
2840 * InputGetExtraFiles
2841 * Autodetect extra input list
2842 *****************************************************************************/
2843 static void InputGetExtraFilesPattern( input_thread_t
*p_input
,
2844 int *pi_list
, char ***pppsz_list
,
2845 const char *psz_path
,
2846 const char *psz_match
,
2847 const char *psz_format
,
2848 int i_start
, int i_stop
)
2852 TAB_INIT( i_list
, ppsz_list
);
2854 char *psz_base
= strdup( psz_path
);
2858 /* Remove the extension */
2859 char *psz_end
= &psz_base
[strlen(psz_base
)-strlen(psz_match
)];
2860 assert( psz_end
>= psz_base
);
2863 /* Try to list files */
2864 for( int i
= i_start
; i
<= i_stop
; i
++ )
2867 if( asprintf( &psz_probe
, psz_format
, psz_base
, i
) < 0 )
2870 char *filepath
= get_path( psz_probe
);
2873 if( filepath
== NULL
||
2874 vlc_stat( filepath
, &st
) || !S_ISREG( st
.st_mode
) || !st
.st_size
)
2881 msg_Dbg( p_input
, "Detected extra file `%s'", filepath
);
2883 char* psz_uri
= vlc_path2uri( filepath
, NULL
);
2885 TAB_APPEND( i_list
, ppsz_list
, psz_uri
);
2893 *pppsz_list
= ppsz_list
;
2896 static void InputGetExtraFiles( input_thread_t
*p_input
,
2897 int *pi_list
, char ***pppsz_list
,
2898 const char **ppsz_access
, const char *psz_path
)
2900 static const struct pattern
2902 const char *psz_access_force
;
2903 const char *psz_match
;
2904 const char *psz_format
;
2908 /* XXX the order is important */
2909 { "concat", ".001", "%s.%.3d", 2, 999 },
2910 { NULL
, ".part1.rar","%s.part%.1d.rar", 2, 9 },
2911 { NULL
, ".part01.rar","%s.part%.2d.rar", 2, 99, },
2912 { NULL
, ".part001.rar", "%s.part%.3d.rar", 2, 999 },
2913 { NULL
, ".rar", "%s.r%.2d", 0, 99 },
2916 TAB_INIT( *pi_list
, *pppsz_list
);
2918 if( ( **ppsz_access
&& strcmp( *ppsz_access
, "file" ) ) || !psz_path
)
2921 const size_t i_path
= strlen(psz_path
);
2923 for( size_t i
= 0; i
< ARRAY_SIZE( patterns
); ++i
)
2925 const struct pattern
* pat
= &patterns
[i
];
2926 const size_t i_ext
= strlen( pat
->psz_match
);
2928 if( i_path
< i_ext
)
2931 if( !strcmp( &psz_path
[i_path
-i_ext
], pat
->psz_match
) )
2933 InputGetExtraFilesPattern( p_input
, pi_list
, pppsz_list
, psz_path
,
2934 pat
->psz_match
, pat
->psz_format
, pat
->i_start
, pat
->i_stop
);
2936 if( *pi_list
> 0 && pat
->psz_access_force
)
2937 *ppsz_access
= pat
->psz_access_force
;
2944 static void input_ChangeState( input_thread_t
*p_input
, int i_state
)
2946 if( input_priv(p_input
)->i_state
== i_state
)
2949 input_priv(p_input
)->i_state
= i_state
;
2950 if( i_state
== ERROR_S
)
2951 input_item_SetErrorWhenReading( input_priv(p_input
)->p_item
, true );
2952 input_SendEventState( p_input
, i_state
);
2956 /*****************************************************************************
2957 * MRLSplit: parse the access, demux and url part of the
2958 * Media Resource Locator.
2959 *****************************************************************************/
2960 void input_SplitMRL( const char **access
, const char **demux
,
2961 const char **path
, const char **anchor
, char *buf
)
2965 /* Separate <path> from <access>[/<demux>]:// */
2966 p
= strstr( buf
, "://" );
2970 p
+= 3; /* skips "://" */
2973 /* Remove HTML anchor if present (not supported).
2974 * The hash symbol itself should be URI-encoded. */
2975 p
= strchr( p
, '#' );
2987 fprintf( stderr
, "%s(\"%s\") probably not a valid URI!\n", __func__
,
2990 /* Note: this is a valid non const pointer to "": */
2991 *path
= buf
+ strlen( buf
);
2994 /* Separate access from demux */
2995 p
= strchr( buf
, '/' );
3006 /* We really don't want module name substitution here! */
3013 static const char *MRLSeekPoint( const char *str
, int *title
, int *chapter
)
3018 /* Look for the title */
3019 u
= strtoul( str
, &end
, 0 );
3020 *title
= (str
== end
|| u
> (unsigned long)INT_MAX
) ? -1 : (int)u
;
3023 /* Look for the chapter */
3027 u
= strtoul( str
, &end
, 0 );
3028 *chapter
= (str
== end
|| u
> (unsigned long)INT_MAX
) ? -1 : (int)u
;
3038 /*****************************************************************************
3039 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3042 * [url][@[title_start][:chapter_start][-[title_end][:chapter_end]]]
3043 *****************************************************************************/
3044 static void MRLSections( const char *p
,
3045 int *pi_title_start
, int *pi_title_end
,
3046 int *pi_chapter_start
, int *pi_chapter_end
)
3048 *pi_title_start
= *pi_title_end
= *pi_chapter_start
= *pi_chapter_end
= -1;
3050 int title_start
, chapter_start
, title_end
, chapter_end
;
3056 p
= MRLSeekPoint( p
, &title_start
, &chapter_start
);
3058 title_start
= chapter_start
= -1;
3061 p
= MRLSeekPoint( p
+ 1, &title_end
, &chapter_end
);
3063 title_end
= chapter_end
= -1;
3065 if( *p
) /* syntax error */
3068 *pi_title_start
= title_start
;
3069 *pi_title_end
= title_end
;
3070 *pi_chapter_start
= chapter_start
;
3071 *pi_chapter_end
= chapter_end
;
3074 static int input_SlaveSourceAdd( input_thread_t
*p_input
,
3075 enum slave_type i_type
, const char *psz_uri
,
3080 const char *psz_forced_demux
;
3081 const bool b_can_fail
= i_flags
& SLAVE_ADD_CANFAIL
;
3082 const bool b_forced
= i_flags
& SLAVE_ADD_FORCED
;
3083 const bool b_set_time
= i_flags
& SLAVE_ADD_SET_TIME
;
3087 case SLAVE_TYPE_SPU
:
3089 psz_forced_demux
= "subtitle";
3091 case SLAVE_TYPE_AUDIO
:
3092 psz_es
= "audio-es";
3093 psz_forced_demux
= NULL
;
3096 vlc_assert_unreachable();
3100 var_Change( p_input
, psz_es
, VLC_VAR_CHOICESCOUNT
, &count
, NULL
);
3102 msg_Dbg( p_input
, "loading %s slave: %s (forced: %d)", psz_es
, psz_uri
,
3105 input_source_t
*p_source
= InputSourceNew( p_input
, psz_uri
,
3106 psz_forced_demux
, b_can_fail
);
3107 if( p_source
== NULL
)
3109 msg_Warn( p_input
, "failed to add %s as slave", psz_uri
);
3110 return VLC_EGENERIC
;
3113 if( i_type
== SLAVE_TYPE_AUDIO
)
3120 if( demux_Control( input_priv(p_input
)->master
->p_demux
,
3121 DEMUX_GET_TIME
, &i_time
) )
3123 msg_Err( p_input
, "demux doesn't like DEMUX_GET_TIME" );
3124 InputSourceDestroy( p_source
);
3125 return VLC_EGENERIC
;
3128 if( demux_Control( p_source
->p_demux
,
3129 DEMUX_SET_TIME
, i_time
, true ) )
3131 msg_Err( p_input
, "seek failed for new slave" );
3132 InputSourceDestroy( p_source
);
3133 return VLC_EGENERIC
;
3137 /* Get meta (access and demux) */
3138 InputUpdateMeta( p_input
, p_source
->p_demux
);
3141 TAB_APPEND( input_priv(p_input
)->i_slave
, input_priv(p_input
)->slave
, p_source
);
3149 if( var_Change( p_input
, psz_es
, VLC_VAR_GETCHOICES
, &list
, NULL
) )
3152 if( count
.i_int
== 0 )
3154 /* if it was first one, there is disable too */
3156 if( count
.i_int
< list
.p_list
->i_count
)
3158 const int i_id
= list
.p_list
->p_values
[count
.i_int
].i_int
;
3160 es_out_Control( input_priv(p_input
)->p_es_out_display
, ES_OUT_SET_ES_DEFAULT_BY_ID
, i_id
);
3161 es_out_Control( input_priv(p_input
)->p_es_out_display
, ES_OUT_SET_ES_BY_ID
, i_id
);
3163 var_FreeList( &list
, NULL
);
3168 static char *input_SubtitleFile2Uri( input_thread_t
*p_input
,
3169 const char *psz_subtitle
)
3171 /* if we are provided a subtitle.sub file,
3172 * see if we don't have a subtitle.idx and use it instead */
3173 char *psz_idxpath
= NULL
;
3174 char *psz_extension
= strrchr( psz_subtitle
, '.');
3175 if( psz_extension
&& strcmp( psz_extension
, ".sub" ) == 0 )
3177 psz_idxpath
= strdup( psz_subtitle
);
3182 psz_extension
= psz_extension
- psz_subtitle
+ psz_idxpath
;
3183 strcpy( psz_extension
, ".idx" );
3185 if( !vlc_stat( psz_idxpath
, &st
) && S_ISREG( st
.st_mode
) )
3187 msg_Dbg( p_input
, "using %s as subtitle file instead of %s",
3188 psz_idxpath
, psz_subtitle
);
3189 psz_subtitle
= psz_idxpath
;
3194 char *psz_uri
= vlc_path2uri( psz_subtitle
, NULL
);
3195 free( psz_idxpath
);
3199 /*****************************************************************************
3201 *****************************************************************************/
3202 void input_UpdateStatistic( input_thread_t
*p_input
,
3203 input_statistic_t i_type
, int i_delta
)
3205 assert( input_priv(p_input
)->i_state
!= INIT_S
);
3207 vlc_mutex_lock( &input_priv(p_input
)->counters
.counters_lock
);
3210 #define I(c) stats_Update( input_priv(p_input)->counters.c, i_delta, NULL )
3211 case INPUT_STATISTIC_DECODED_VIDEO
:
3214 case INPUT_STATISTIC_DECODED_AUDIO
:
3217 case INPUT_STATISTIC_DECODED_SUBTITLE
:
3220 case INPUT_STATISTIC_SENT_PACKET
:
3221 I(p_sout_sent_packets
);
3224 case INPUT_STATISTIC_SENT_BYTE
:
3228 stats_Update( input_priv(p_input
)->counters
.p_sout_sent_bytes
, i_delta
, &bytes
);
3229 stats_Update( input_priv(p_input
)->counters
.p_sout_send_bitrate
, bytes
, NULL
);
3233 msg_Err( p_input
, "Invalid statistic type %d (internal error)", i_type
);
3236 vlc_mutex_unlock( &input_priv(p_input
)->counters
.counters_lock
);
3240 /* TODO FIXME nearly the same logic that snapshot code */
3241 char *input_CreateFilename(input_thread_t
*input
, const char *dir
,
3242 const char *filenamefmt
, const char *ext
)
3245 char *filename
= str_format(input
, filenamefmt
);
3246 if (unlikely(filename
== NULL
))
3249 filename_sanitize(filename
);
3252 ? asprintf(&path
, "%s"DIR_SEP
"%s.%s", dir
, filename
, ext
)
3253 : asprintf(&path
, "%s"DIR_SEP
"%s", dir
, filename
)) < 0)