input: provide module name in psz_demux for access_demux
[vlc.git] / src / input / input.c
blobf751b41299f173fd019ef1623e081fedb3941bfe
1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 VLC authors and VideoLAN
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
34 #include <limits.h>
35 #include <assert.h>
36 #include <sys/stat.h>
38 #include "input_internal.h"
39 #include "event.h"
40 #include "es_out.h"
41 #include "es_out_timeshift.h"
42 #include "demux.h"
43 #include "item.h"
44 #include "resource.h"
45 #include "stream.h"
47 #include <vlc_aout.h>
48 #include <vlc_sout.h>
49 #include <vlc_dialog.h>
50 #include <vlc_url.h>
51 #include <vlc_charset.h>
52 #include <vlc_fs.h>
53 #include <vlc_strings.h>
54 #include <vlc_modules.h>
55 #include <vlc_stream.h>
56 #include <vlc_stream_extractor.h>
57 #include <vlc_renderer_discovery.h>
59 /*****************************************************************************
60 * Local prototypes
61 *****************************************************************************/
62 static void *Run( void * );
63 static void *Preparse( void * );
65 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
66 const char *, bool, input_resource_t *,
67 vlc_renderer_item_t * );
68 static int Init ( input_thread_t *p_input );
69 static void End ( input_thread_t *p_input );
70 static void MainLoop( input_thread_t *p_input, bool b_interactive );
72 static inline int ControlPop( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline, bool b_postpone_seek );
73 static void ControlRelease( int i_type, vlc_value_t val );
74 static bool ControlIsSeekRequest( int i_type );
75 static bool Control( input_thread_t *, int, vlc_value_t );
76 static void ControlPause( input_thread_t *, mtime_t );
78 static int UpdateTitleSeekpointFromDemux( input_thread_t * );
79 static void UpdateGenericFromDemux( input_thread_t * );
80 static void UpdateTitleListfromDemux( input_thread_t * );
82 static void MRLSections( const char *, int *, int *, int *, int *);
84 static input_source_t *InputSourceNew( input_thread_t *, const char *,
85 const char *psz_forced_demux,
86 bool b_in_can_fail );
87 static void InputSourceDestroy( input_source_t * );
88 static void InputSourceMeta( input_thread_t *, input_source_t *, vlc_meta_t * );
90 /* TODO */
91 //static void InputGetAttachments( input_thread_t *, input_source_t * );
92 static void SlaveDemux( input_thread_t *p_input );
93 static void SlaveSeek( input_thread_t *p_input );
95 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
96 static void InputUpdateMeta( input_thread_t *p_input, demux_t *p_demux );
97 static void InputGetExtraFiles( input_thread_t *p_input,
98 int *pi_list, char ***pppsz_list,
99 const char **psz_access, const char *psz_path );
101 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
102 const demux_t ***ppp_attachment_demux,
103 int i_new, input_attachment_t **pp_new, const demux_t *p_demux );
105 #define SLAVE_ADD_NOFLAG 0
106 #define SLAVE_ADD_FORCED (1<<0)
107 #define SLAVE_ADD_CANFAIL (1<<1)
108 #define SLAVE_ADD_SET_TIME (1<<2)
110 static int input_SlaveSourceAdd( input_thread_t *, enum slave_type,
111 const char *, unsigned );
112 static char *input_SubtitleFile2Uri( input_thread_t *, const char * );
113 static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */
115 #undef input_Create
117 * Create a new input_thread_t.
119 * You need to call input_Start on it when you are done
120 * adding callback on the variables/events you want to monitor.
122 * \param p_parent a vlc_object
123 * \param p_item an input item
124 * \param psz_log an optional prefix for this input logs
125 * \param p_resource an optional input ressource
126 * \return a pointer to the spawned input thread
128 input_thread_t *input_Create( vlc_object_t *p_parent,
129 input_item_t *p_item,
130 const char *psz_log, input_resource_t *p_resource,
131 vlc_renderer_item_t *p_renderer )
133 return Create( p_parent, p_item, psz_log, false, p_resource, p_renderer );
136 #undef input_Read
138 * Initialize an input thread and run it until it stops by itself.
140 * \param p_parent a vlc_object
141 * \param p_item an input item
142 * \return an error code, VLC_SUCCESS on success
144 int input_Read( vlc_object_t *p_parent, input_item_t *p_item )
146 input_thread_t *p_input = Create( p_parent, p_item, NULL, false, NULL, NULL );
147 if( !p_input )
148 return VLC_EGENERIC;
150 if( !Init( p_input ) )
152 MainLoop( p_input, false );
153 End( p_input );
156 vlc_object_release( p_input );
157 return VLC_SUCCESS;
160 input_thread_t *input_CreatePreparser( vlc_object_t *parent,
161 input_item_t *item )
163 return Create( parent, item, NULL, true, NULL, NULL );
167 * Start a input_thread_t created by input_Create.
169 * You must not start an already running input_thread_t.
171 * \param the input thread to start
173 int input_Start( input_thread_t *p_input )
175 input_thread_private_t *priv = input_priv(p_input);
176 void *(*func)(void *) = Run;
178 if( priv->b_preparsing )
179 func = Preparse;
181 assert( !priv->is_running );
182 /* Create thread and wait for its readiness. */
183 priv->is_running = !vlc_clone( &priv->thread, func, priv,
184 VLC_THREAD_PRIORITY_INPUT );
185 if( !priv->is_running )
187 input_ChangeState( p_input, ERROR_S );
188 msg_Err( p_input, "cannot create input thread" );
189 return VLC_EGENERIC;
191 return VLC_SUCCESS;
195 * Request a running input thread to stop and die
197 * \param p_input the input thread to stop
199 void input_Stop( input_thread_t *p_input )
201 input_thread_private_t *sys = input_priv(p_input);
203 vlc_mutex_lock( &sys->lock_control );
204 /* Discard all pending controls */
205 for( int i = 0; i < sys->i_control; i++ )
207 input_control_t *ctrl = &sys->control[i];
208 ControlRelease( ctrl->i_type, ctrl->val );
210 sys->i_control = 0;
211 sys->is_stopped = true;
212 vlc_cond_signal( &sys->wait_control );
213 vlc_mutex_unlock( &sys->lock_control );
214 vlc_interrupt_kill( &sys->interrupt );
218 * Close an input
220 * It does not call input_Stop itself.
222 void input_Close( input_thread_t *p_input )
224 if( input_priv(p_input)->is_running )
225 vlc_join( input_priv(p_input)->thread, NULL );
226 vlc_interrupt_deinit( &input_priv(p_input)->interrupt );
227 vlc_object_release( p_input );
231 * Input destructor (called when the object's refcount reaches 0).
233 static void input_Destructor( vlc_object_t *obj )
235 input_thread_t *p_input = (input_thread_t *)obj;
236 input_thread_private_t *priv = input_priv(p_input);
237 #ifndef NDEBUG
238 char * psz_name = input_item_GetName( priv->p_item );
239 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
240 free( psz_name );
241 #endif
243 if( priv->p_renderer )
244 vlc_renderer_item_release( priv->p_renderer );
245 if( priv->p_es_out_display )
246 es_out_Delete( priv->p_es_out_display );
248 if( priv->p_resource )
249 input_resource_Release( priv->p_resource );
250 if( priv->p_resource_private )
251 input_resource_Release( priv->p_resource_private );
253 input_item_Release( priv->p_item );
255 vlc_mutex_destroy( &priv->counters.counters_lock );
257 for( int i = 0; i < priv->i_control; i++ )
259 input_control_t *p_ctrl = &priv->control[i];
260 ControlRelease( p_ctrl->i_type, p_ctrl->val );
263 vlc_cond_destroy( &priv->wait_control );
264 vlc_mutex_destroy( &priv->lock_control );
268 * Get the item from an input thread
269 * FIXME it does not increase ref count of the item.
270 * if it is used after p_input is destroyed nothing prevent it from
271 * being freed.
273 input_item_t *input_GetItem( input_thread_t *p_input )
275 assert( p_input != NULL );
276 return input_priv(p_input)->p_item;
279 /*****************************************************************************
280 * This function creates a new input, and returns a pointer
281 * to its description. On error, it returns NULL.
283 * XXX Do not forget to update vlc_input.h if you add new variables.
284 *****************************************************************************/
285 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
286 const char *psz_header, bool b_preparsing,
287 input_resource_t *p_resource,
288 vlc_renderer_item_t *p_renderer )
290 /* Allocate descriptor */
291 input_thread_private_t *priv;
293 priv = vlc_custom_create( p_parent, sizeof( *priv ), "input" );
294 if( unlikely(priv == NULL) )
295 return NULL;
297 input_thread_t *p_input = &priv->input;
299 char * psz_name = input_item_GetName( p_item );
300 msg_Dbg( p_input, "Creating an input for %s'%s'",
301 b_preparsing ? "preparsing " : "", psz_name);
302 free( psz_name );
304 /* Parse input options */
305 input_item_ApplyOptions( VLC_OBJECT(p_input), p_item );
307 p_input->obj.header = psz_header ? strdup( psz_header ) : NULL;
309 /* Init Common fields */
310 priv->b_preparsing = b_preparsing;
311 priv->b_can_pace_control = true;
312 priv->i_start = 0;
313 priv->i_time = 0;
314 priv->i_stop = 0;
315 priv->i_title = 0;
316 priv->title = NULL;
317 priv->i_title_offset = input_priv(p_input)->i_seekpoint_offset = 0;
318 priv->i_state = INIT_S;
319 priv->is_running = false;
320 priv->is_stopped = false;
321 priv->b_recording = false;
322 priv->i_rate = INPUT_RATE_DEFAULT;
323 memset( &priv->bookmark, 0, sizeof(priv->bookmark) );
324 TAB_INIT( priv->i_bookmark, priv->pp_bookmark );
325 TAB_INIT( priv->i_attachment, priv->attachment );
326 priv->attachment_demux = NULL;
327 priv->p_sout = NULL;
328 priv->b_out_pace_control = false;
329 priv->p_renderer = p_renderer ? vlc_renderer_item_hold( p_renderer ) : NULL;
331 priv->viewpoint_changed = false;
332 /* Fetch the viewpoint from the mediaplayer or the playlist if any */
333 vlc_viewpoint_t *p_viewpoint = var_InheritAddress( p_input, "viewpoint" );
334 if (p_viewpoint != NULL)
335 priv->viewpoint = *p_viewpoint;
336 else
337 vlc_viewpoint_init( &priv->viewpoint );
339 input_item_Hold( p_item ); /* Released in Destructor() */
340 priv->p_item = p_item;
342 /* Init Input fields */
343 priv->master = NULL;
344 vlc_mutex_lock( &p_item->lock );
346 if( !p_item->p_stats )
347 p_item->p_stats = stats_NewInputStats( p_input );
349 /* setup the preparse depth of the item
350 * if we are preparsing, use the i_preparse_depth of the parent item */
351 if( !priv->b_preparsing )
353 char *psz_rec = var_InheritString( p_parent, "recursive" );
355 if( psz_rec != NULL )
357 if ( !strcasecmp( psz_rec, "none" ) )
358 p_item->i_preparse_depth = 0;
359 else if ( !strcasecmp( psz_rec, "collapse" ) )
360 p_item->i_preparse_depth = 1;
361 else
362 p_item->i_preparse_depth = -1; /* default is expand */
363 free (psz_rec);
364 } else
365 p_item->i_preparse_depth = -1;
367 else
368 p_input->obj.flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
370 /* Make sure the interaction option is honored */
371 if( !var_InheritBool( p_input, "interact" ) )
372 p_input->obj.flags |= OBJECT_FLAGS_NOINTERACT;
373 else if( p_item->b_preparse_interact )
375 /* If true, this item was asked explicitly to interact with the user
376 * (via libvlc_MetadataRequest). Sub items created from this input won't
377 * have this flag and won't interact with the user */
378 p_input->obj.flags &= ~OBJECT_FLAGS_NOINTERACT;
381 vlc_mutex_unlock( &p_item->lock );
383 /* No slave */
384 priv->i_slave = 0;
385 priv->slave = NULL;
387 /* */
388 if( p_resource )
390 priv->p_resource_private = NULL;
391 priv->p_resource = input_resource_Hold( p_resource );
393 else
395 priv->p_resource_private = input_resource_New( VLC_OBJECT( p_input ) );
396 priv->p_resource = input_resource_Hold( priv->p_resource_private );
398 input_resource_SetInput( priv->p_resource, p_input );
400 /* Init control buffer */
401 vlc_mutex_init( &priv->lock_control );
402 vlc_cond_init( &priv->wait_control );
403 priv->i_control = 0;
404 vlc_interrupt_init(&priv->interrupt);
406 /* Create Object Variables for private use only */
407 input_ConfigVarInit( p_input );
409 /* Create Objects variables for public Get and Set */
410 input_ControlVarInit( p_input );
412 /* */
413 if( !priv->b_preparsing )
415 char *psz_bookmarks = var_GetNonEmptyString( p_input, "bookmarks" );
416 if( psz_bookmarks )
418 /* FIXME: have a common cfg parsing routine used by sout and others */
419 char *psz_parser, *psz_start, *psz_end;
420 psz_parser = psz_bookmarks;
421 while( (psz_start = strchr( psz_parser, '{' ) ) )
423 seekpoint_t *p_seekpoint;
424 char backup;
425 psz_start++;
426 psz_end = strchr( psz_start, '}' );
427 if( !psz_end ) break;
428 psz_parser = psz_end + 1;
429 backup = *psz_parser;
430 *psz_parser = 0;
431 *psz_end = ',';
433 p_seekpoint = vlc_seekpoint_New();
435 if( unlikely( p_seekpoint == NULL ) )
436 break;
438 while( (psz_end = strchr( psz_start, ',' ) ) )
440 *psz_end = 0;
441 if( !strncmp( psz_start, "name=", 5 ) )
443 free( p_seekpoint->psz_name );
445 p_seekpoint->psz_name = strdup(psz_start + 5);
447 else if( !strncmp( psz_start, "time=", 5 ) )
449 p_seekpoint->i_time_offset = atof(psz_start + 5) *
450 CLOCK_FREQ;
452 psz_start = psz_end + 1;
454 msg_Dbg( p_input, "adding bookmark: %s, time=%"PRId64,
455 p_seekpoint->psz_name,
456 p_seekpoint->i_time_offset );
457 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
458 vlc_seekpoint_Delete( p_seekpoint );
459 *psz_parser = backup;
461 free( psz_bookmarks );
465 /* Remove 'Now playing' info as it is probably outdated */
466 input_item_SetNowPlaying( p_item, NULL );
467 input_item_SetESNowPlaying( p_item, NULL );
468 input_SendEventMeta( p_input );
470 /* */
471 memset( &priv->counters, 0, sizeof( priv->counters ) );
472 vlc_mutex_init( &priv->counters.counters_lock );
474 priv->p_es_out_display = input_EsOutNew( p_input, priv->i_rate );
475 priv->p_es_out = NULL;
477 /* Set the destructor when we are sure we are initialized */
478 vlc_object_set_destructor( p_input, input_Destructor );
480 return p_input;
483 /*****************************************************************************
484 * Run: main thread loop
485 * This is the "normal" thread that spawns the input processing chain,
486 * reads the stream, cleans up and waits
487 *****************************************************************************/
488 static void *Run( void *data )
490 input_thread_private_t *priv = data;
491 input_thread_t *p_input = &priv->input;
493 vlc_interrupt_set(&priv->interrupt);
495 if( !Init( p_input ) )
497 if( priv->b_can_pace_control && priv->b_out_pace_control )
499 /* We don't want a high input priority here or we'll
500 * end-up sucking up all the CPU time */
501 vlc_set_priority( priv->thread, VLC_THREAD_PRIORITY_LOW );
504 MainLoop( p_input, true ); /* FIXME it can be wrong (like with VLM) */
506 /* Clean up */
507 End( p_input );
510 input_SendEventDead( p_input );
511 return NULL;
514 static void *Preparse( void *data )
516 input_thread_private_t *priv = data;
517 input_thread_t *p_input = &priv->input;
519 vlc_interrupt_set(&priv->interrupt);
521 if( !Init( p_input ) )
522 { /* if the demux is a playlist, call Mainloop that will call
523 * demux_Demux in order to fetch sub items */
524 bool b_is_playlist = false;
526 if ( input_item_ShouldPreparseSubItems( priv->p_item )
527 && demux_Control( priv->master->p_demux, DEMUX_IS_PLAYLIST,
528 &b_is_playlist ) )
529 b_is_playlist = false;
530 if( b_is_playlist )
531 MainLoop( p_input, false );
532 End( p_input );
535 input_SendEventDead( p_input );
536 return NULL;
539 bool input_Stopped( input_thread_t *input )
541 input_thread_private_t *sys = input_priv(input);
542 bool ret;
544 vlc_mutex_lock( &sys->lock_control );
545 ret = sys->is_stopped;
546 vlc_mutex_unlock( &sys->lock_control );
547 return ret;
550 /*****************************************************************************
551 * Main loop: Fill buffers from access, and demux
552 *****************************************************************************/
555 * MainLoopDemux
556 * It asks the demuxer to demux some data
558 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed )
560 int i_ret;
561 demux_t *p_demux = input_priv(p_input)->master->p_demux;
563 *pb_changed = false;
565 if( input_priv(p_input)->i_stop > 0 && input_priv(p_input)->i_time >= input_priv(p_input)->i_stop )
566 i_ret = VLC_DEMUXER_EOF;
567 else
568 i_ret = demux_Demux( p_demux );
570 i_ret = i_ret > 0 ? VLC_DEMUXER_SUCCESS : ( i_ret < 0 ? VLC_DEMUXER_EGENERIC : VLC_DEMUXER_EOF);
572 if( i_ret == VLC_DEMUXER_SUCCESS )
574 if( demux_TestAndClearFlags( p_demux, INPUT_UPDATE_TITLE_LIST ) )
575 UpdateTitleListfromDemux( p_input );
577 if( input_priv(p_input)->master->b_title_demux )
579 i_ret = UpdateTitleSeekpointFromDemux( p_input );
580 *pb_changed = true;
583 UpdateGenericFromDemux( p_input );
586 if( i_ret == VLC_DEMUXER_EOF )
588 msg_Dbg( p_input, "EOF reached" );
589 input_priv(p_input)->master->b_eof = true;
590 es_out_Eos(input_priv(p_input)->p_es_out);
592 else if( i_ret == VLC_DEMUXER_EGENERIC )
594 input_ChangeState( p_input, ERROR_S );
596 else if( input_priv(p_input)->i_slave > 0 )
597 SlaveDemux( p_input );
600 static int MainLoopTryRepeat( input_thread_t *p_input )
602 int i_repeat = var_GetInteger( p_input, "input-repeat" );
603 if( i_repeat <= 0 )
604 return VLC_EGENERIC;
606 vlc_value_t val;
608 msg_Dbg( p_input, "repeating the same input (%d)", i_repeat );
609 if( i_repeat > 0 )
611 i_repeat--;
612 var_SetInteger( p_input, "input-repeat", i_repeat );
615 /* Seek to start title/seekpoint */
616 val.i_int = input_priv(p_input)->master->i_title_start -
617 input_priv(p_input)->master->i_title_offset;
618 if( val.i_int < 0 || val.i_int >= input_priv(p_input)->master->i_title )
619 val.i_int = 0;
620 input_ControlPush( p_input,
621 INPUT_CONTROL_SET_TITLE, &val );
623 val.i_int = input_priv(p_input)->master->i_seekpoint_start -
624 input_priv(p_input)->master->i_seekpoint_offset;
625 if( val.i_int > 0 /* TODO: check upper boundary */ )
626 input_ControlPush( p_input,
627 INPUT_CONTROL_SET_SEEKPOINT, &val );
629 /* Seek to start position */
630 if( input_priv(p_input)->i_start > 0 )
632 val.i_int = input_priv(p_input)->i_start;
633 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &val );
635 else
637 val.f_float = 0.f;
638 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION, &val );
641 return VLC_SUCCESS;
645 * Update timing infos and statistics.
647 static void MainLoopStatistics( input_thread_t *p_input )
649 double f_position = 0.0;
650 mtime_t i_time = 0;
651 mtime_t i_length = 0;
653 /* update input status variables */
654 if( demux_Control( input_priv(p_input)->master->p_demux,
655 DEMUX_GET_POSITION, &f_position ) )
656 f_position = 0.0;
658 if( demux_Control( input_priv(p_input)->master->p_demux,
659 DEMUX_GET_TIME, &i_time ) )
660 i_time = 0;
661 input_priv(p_input)->i_time = i_time;
663 if( demux_Control( input_priv(p_input)->master->p_demux,
664 DEMUX_GET_LENGTH, &i_length ) )
665 i_length = 0;
667 es_out_SetTimes( input_priv(p_input)->p_es_out, f_position, i_time, i_length );
669 /* update current bookmark */
670 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
671 input_priv(p_input)->bookmark.i_time_offset = i_time;
672 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
674 stats_ComputeInputStats( p_input, input_priv(p_input)->p_item->p_stats );
675 input_SendEventStatistics( p_input );
679 * MainLoop
680 * The main input loop.
682 static void MainLoop( input_thread_t *p_input, bool b_interactive )
684 mtime_t i_intf_update = 0;
685 mtime_t i_last_seek_mdate = 0;
687 if( b_interactive && var_InheritBool( p_input, "start-paused" ) )
688 ControlPause( p_input, mdate() );
690 bool b_pause_after_eof = b_interactive &&
691 var_InheritBool( p_input, "play-and-pause" );
692 bool b_paused_at_eof = false;
694 demux_t *p_demux = input_priv(p_input)->master->p_demux;
695 const bool b_can_demux = p_demux->pf_demux != NULL;
697 while( !input_Stopped( p_input ) && input_priv(p_input)->i_state != ERROR_S )
699 mtime_t i_wakeup = -1;
700 bool b_paused = input_priv(p_input)->i_state == PAUSE_S;
701 /* FIXME if input_priv(p_input)->i_state == PAUSE_S the access/access_demux
702 * is paused -> this may cause problem with some of them
703 * The same problem can be seen when seeking while paused */
704 if( b_paused )
705 b_paused = !es_out_GetBuffering( input_priv(p_input)->p_es_out )
706 || input_priv(p_input)->master->b_eof;
708 if( !b_paused )
710 if( !input_priv(p_input)->master->b_eof )
712 bool b_force_update = false;
714 MainLoopDemux( p_input, &b_force_update );
716 if( b_can_demux )
717 i_wakeup = es_out_GetWakeup( input_priv(p_input)->p_es_out );
718 if( b_force_update )
719 i_intf_update = 0;
721 b_paused_at_eof = false;
723 else if( !es_out_GetEmpty( input_priv(p_input)->p_es_out ) )
725 msg_Dbg( p_input, "waiting decoder fifos to empty" );
726 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
728 /* Pause after eof only if the input is pausable.
729 * This way we won't trigger timeshifting for nothing */
730 else if( b_pause_after_eof && input_priv(p_input)->b_can_pause )
732 if( b_paused_at_eof )
733 break;
735 vlc_value_t val = { .i_int = PAUSE_S };
737 msg_Dbg( p_input, "pausing at EOF (pause after each)");
738 Control( p_input, INPUT_CONTROL_SET_STATE, val );
740 b_paused = true;
741 b_paused_at_eof = true;
743 else
745 if( MainLoopTryRepeat( p_input ) )
746 break;
749 /* Update interface and statistics */
750 mtime_t now = mdate();
751 if( now >= i_intf_update )
753 MainLoopStatistics( p_input );
754 i_intf_update = now + INT64_C(250000);
758 /* Handle control */
759 for( ;; )
761 mtime_t i_deadline = i_wakeup;
763 /* Postpone seeking until ES buffering is complete or at most
764 * 125 ms. */
765 bool b_postpone = es_out_GetBuffering( input_priv(p_input)->p_es_out )
766 && !input_priv(p_input)->master->b_eof;
767 if( b_postpone )
769 mtime_t now = mdate();
771 /* Recheck ES buffer level every 20 ms when seeking */
772 if( now < i_last_seek_mdate + INT64_C(125000)
773 && (i_deadline < 0 || i_deadline > now + INT64_C(20000)) )
774 i_deadline = now + INT64_C(20000);
775 else
776 b_postpone = false;
779 int i_type;
780 vlc_value_t val;
782 if( ControlPop( p_input, &i_type, &val, i_deadline, b_postpone ) )
784 if( b_postpone )
785 continue;
786 break; /* Wake-up time reached */
789 #ifndef NDEBUG
790 msg_Dbg( p_input, "control type=%d", i_type );
791 #endif
792 if( Control( p_input, i_type, val ) )
794 if( ControlIsSeekRequest( i_type ) )
795 i_last_seek_mdate = mdate();
796 i_intf_update = 0;
799 /* Update the wakeup time */
800 if( i_wakeup != 0 )
801 i_wakeup = es_out_GetWakeup( input_priv(p_input)->p_es_out );
806 static void InitStatistics( input_thread_t *p_input )
808 input_thread_private_t *priv = input_priv(p_input);
810 if( priv->b_preparsing ) return;
812 /* Prepare statistics */
813 #define INIT_COUNTER( c, compute ) free( priv->counters.p_##c ); \
814 priv->counters.p_##c = \
815 stats_CounterCreate( STATS_##compute);
816 if( libvlc_stats( p_input ) )
818 INIT_COUNTER( read_bytes, COUNTER );
819 INIT_COUNTER( read_packets, COUNTER );
820 INIT_COUNTER( demux_read, COUNTER );
821 INIT_COUNTER( input_bitrate, DERIVATIVE );
822 INIT_COUNTER( demux_bitrate, DERIVATIVE );
823 INIT_COUNTER( demux_corrupted, COUNTER );
824 INIT_COUNTER( demux_discontinuity, COUNTER );
825 INIT_COUNTER( played_abuffers, COUNTER );
826 INIT_COUNTER( lost_abuffers, COUNTER );
827 INIT_COUNTER( displayed_pictures, COUNTER );
828 INIT_COUNTER( lost_pictures, COUNTER );
829 INIT_COUNTER( decoded_audio, COUNTER );
830 INIT_COUNTER( decoded_video, COUNTER );
831 INIT_COUNTER( decoded_sub, COUNTER );
832 priv->counters.p_sout_send_bitrate = NULL;
833 priv->counters.p_sout_sent_packets = NULL;
834 priv->counters.p_sout_sent_bytes = NULL;
838 #ifdef ENABLE_SOUT
839 static int InitSout( input_thread_t * p_input )
841 input_thread_private_t *priv = input_priv(p_input);
843 if( priv->b_preparsing )
844 return VLC_SUCCESS;
846 /* Find a usable sout and attach it to p_input */
847 char *psz = NULL;
848 if( priv->p_renderer )
850 const char *psz_renderer_sout = vlc_renderer_item_sout( priv->p_renderer );
851 if( asprintf( &psz, "#%s", psz_renderer_sout ) < 0 )
852 return VLC_ENOMEM;
854 if( !psz )
855 psz = var_GetNonEmptyString( p_input, "sout" );
856 if( psz && strncasecmp( priv->p_item->psz_uri, "vlc:", 4 ) )
858 priv->p_sout = input_resource_RequestSout( priv->p_resource, NULL, psz );
859 if( priv->p_sout == NULL )
861 input_ChangeState( p_input, ERROR_S );
862 msg_Err( p_input, "cannot start stream output instance, " \
863 "aborting" );
864 free( psz );
865 return VLC_EGENERIC;
867 if( libvlc_stats( p_input ) )
869 INIT_COUNTER( sout_sent_packets, COUNTER );
870 INIT_COUNTER( sout_sent_bytes, COUNTER );
871 INIT_COUNTER( sout_send_bitrate, DERIVATIVE );
874 else
876 input_resource_RequestSout( priv->p_resource, NULL, NULL );
878 free( psz );
880 return VLC_SUCCESS;
882 #endif
884 static void InitTitle( input_thread_t * p_input )
886 input_thread_private_t *priv = input_priv(p_input);
887 input_source_t *p_master = priv->master;
889 if( priv->b_preparsing )
890 return;
892 vlc_mutex_lock( &priv->p_item->lock );
893 /* Create global title (from master) */
894 priv->i_title = p_master->i_title;
895 priv->title = p_master->title;
896 priv->i_title_offset = p_master->i_title_offset;
897 priv->i_seekpoint_offset = p_master->i_seekpoint_offset;
898 if( priv->i_title > 0 )
900 /* Setup variables */
901 input_ControlVarNavigation( p_input );
902 input_SendEventTitle( p_input, 0 );
905 /* Global flag */
906 priv->b_can_pace_control = p_master->b_can_pace_control;
907 priv->b_can_pause = p_master->b_can_pause;
908 priv->b_can_rate_control = p_master->b_can_rate_control;
909 vlc_mutex_unlock( &priv->p_item->lock );
912 static void StartTitle( input_thread_t * p_input )
914 input_thread_private_t *priv = input_priv(p_input);
915 vlc_value_t val;
917 /* Start title/chapter */
918 val.i_int = priv->master->i_title_start - priv->master->i_title_offset;
919 if( val.i_int > 0 && val.i_int < priv->master->i_title )
920 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
922 val.i_int = priv->master->i_seekpoint_start -
923 priv->master->i_seekpoint_offset;
924 if( val.i_int > 0 /* TODO: check upper boundary */ )
925 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
927 /* Start/stop/run time */
928 priv->i_start = llroundf(1000000.f
929 * var_GetFloat( p_input, "start-time" ));
930 priv->i_stop = llroundf(1000000.f
931 * var_GetFloat( p_input, "stop-time" ));
932 if( priv->i_stop <= 0 )
934 priv->i_stop = llroundf(1000000.f
935 * var_GetFloat( p_input, "run-time" ));
936 if( priv->i_stop < 0 )
938 msg_Warn( p_input, "invalid run-time ignored" );
939 priv->i_stop = 0;
941 else
942 priv->i_stop += priv->i_start;
945 if( priv->i_start > 0 )
947 vlc_value_t s;
949 msg_Dbg( p_input, "starting at time: %"PRId64"s",
950 priv->i_start / CLOCK_FREQ );
952 s.i_int = priv->i_start;
953 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
955 if( priv->i_stop > 0 && priv->i_stop <= priv->i_start )
957 msg_Warn( p_input, "invalid stop-time ignored" );
958 priv->i_stop = 0;
960 priv->b_fast_seek = var_GetBool( p_input, "input-fast-seek" );
963 static int SlaveCompare(const void *a, const void *b)
965 const input_item_slave_t *p_slave0 = *((const input_item_slave_t **) a);
966 const input_item_slave_t *p_slave1 = *((const input_item_slave_t **) b);
968 if( p_slave0 == NULL || p_slave1 == NULL )
970 /* Put NULL (or rejected) subs at the end */
971 return p_slave0 == NULL ? 1 : p_slave1 == NULL ? -1 : 0;
974 if( p_slave0->i_priority > p_slave1->i_priority )
975 return -1;
977 if( p_slave0->i_priority < p_slave1->i_priority )
978 return 1;
980 return 0;
983 static bool SlaveExists( input_item_slave_t **pp_slaves, int i_slaves,
984 const char *psz_uri)
986 for( int i = 0; i < i_slaves; i++ )
988 if( pp_slaves[i] != NULL
989 && !strcmp( pp_slaves[i]->psz_uri, psz_uri ) )
990 return true;
992 return false;
995 static void SetSubtitlesOptions( input_thread_t *p_input )
997 /* Get fps and set it if not already set */
998 const float f_fps = input_priv(p_input)->master->f_fps;
999 if( f_fps > 1.f )
1001 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
1002 var_SetFloat( p_input, "sub-original-fps", f_fps );
1004 float f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1005 if( f_requested_fps != f_fps )
1007 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1008 VLC_VAR_DOINHERIT );
1009 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1013 const int i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1014 if( i_delay != 0 )
1015 var_SetInteger( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1018 static void GetVarSlaves( input_thread_t *p_input,
1019 input_item_slave_t ***ppp_slaves, int *p_slaves )
1021 char *psz = var_GetNonEmptyString( p_input, "input-slave" );
1022 if( !psz )
1023 return;
1025 input_item_slave_t **pp_slaves = *ppp_slaves;
1026 int i_slaves = *p_slaves;
1028 char *psz_org = psz;
1029 while( psz && *psz )
1031 while( *psz == ' ' || *psz == '#' )
1032 psz++;
1034 char *psz_delim = strchr( psz, '#' );
1035 if( psz_delim )
1036 *psz_delim++ = '\0';
1038 if( *psz == 0 )
1039 break;
1041 char *uri = strstr(psz, "://")
1042 ? strdup( psz ) : vlc_path2uri( psz, NULL );
1043 psz = psz_delim;
1044 if( uri == NULL )
1045 continue;
1047 input_item_slave_t *p_slave =
1048 input_item_slave_New( uri, SLAVE_TYPE_AUDIO, SLAVE_PRIORITY_USER );
1049 free( uri );
1051 if( unlikely( p_slave == NULL ) )
1052 break;
1053 TAB_APPEND(i_slaves, pp_slaves, p_slave);
1055 free( psz_org );
1057 *ppp_slaves = pp_slaves; /* in case of realloc */
1058 *p_slaves = i_slaves;
1061 static void LoadSlaves( input_thread_t *p_input )
1063 input_item_slave_t **pp_slaves;
1064 int i_slaves;
1065 TAB_INIT( i_slaves, pp_slaves );
1067 /* Look for and add slaves */
1069 char *psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1070 if( psz_subtitle != NULL )
1072 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1073 char *psz_uri = input_SubtitleFile2Uri( p_input, psz_subtitle );
1074 free( psz_subtitle );
1075 psz_subtitle = NULL;
1076 if( psz_uri != NULL )
1078 input_item_slave_t *p_slave =
1079 input_item_slave_New( psz_uri, SLAVE_TYPE_SPU,
1080 SLAVE_PRIORITY_USER );
1081 free( psz_uri );
1082 if( p_slave )
1084 TAB_APPEND(i_slaves, pp_slaves, p_slave);
1085 psz_subtitle = p_slave->psz_uri;
1090 if( var_GetBool( p_input, "sub-autodetect-file" ) )
1092 /* Add local subtitles */
1093 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1095 if( subtitles_Detect( p_input, psz_autopath, input_priv(p_input)->p_item->psz_uri,
1096 &pp_slaves, &i_slaves ) == VLC_SUCCESS )
1098 /* check that we did not add the subtitle through sub-file */
1099 if( psz_subtitle != NULL )
1101 for( int i = 1; i < i_slaves; i++ )
1103 input_item_slave_t *p_curr = pp_slaves[i];
1104 if( p_curr != NULL
1105 && !strcmp( psz_subtitle, p_curr->psz_uri ) )
1107 /* reject current sub */
1108 input_item_slave_Delete( p_curr );
1109 pp_slaves[i] = NULL;
1114 free( psz_autopath );
1117 /* Add slaves found by the directory demuxer or via libvlc */
1118 input_item_t *p_item = input_priv(p_input)->p_item;
1119 vlc_mutex_lock( &p_item->lock );
1121 /* Move item slaves to local pp_slaves */
1122 for( int i = 0; i < p_item->i_slaves; i++ )
1124 input_item_slave_t *p_slave = p_item->pp_slaves[i];
1125 if( !SlaveExists( pp_slaves, i_slaves, p_slave->psz_uri ) )
1126 TAB_APPEND(i_slaves, pp_slaves, p_slave);
1127 else
1128 input_item_slave_Delete( p_slave );
1130 /* Slaves that are successfully loaded will be added back to the item */
1131 TAB_CLEAN( p_item->i_slaves, p_item->pp_slaves );
1132 vlc_mutex_unlock( &p_item->lock );
1134 /* Add slaves from the "input-slave" option */
1135 GetVarSlaves( p_input, &pp_slaves, &i_slaves );
1137 if( i_slaves > 0 )
1138 qsort( pp_slaves, i_slaves, sizeof (input_item_slave_t*),
1139 SlaveCompare );
1141 /* add all detected slaves */
1142 bool p_forced[2] = { false, false };
1143 static_assert( SLAVE_TYPE_AUDIO <= 1 && SLAVE_TYPE_SPU <= 1,
1144 "slave type size mismatch");
1145 for( int i = 0; i < i_slaves && pp_slaves[i] != NULL; i++ )
1147 input_item_slave_t *p_slave = pp_slaves[i];
1148 /* Slaves added via options should not fail */
1149 unsigned i_flags = p_slave->i_priority != SLAVE_PRIORITY_USER
1150 ? SLAVE_ADD_CANFAIL : SLAVE_ADD_NOFLAG;
1151 bool b_forced = false;
1153 /* Force the first subtitle with the highest priority or with the
1154 * forced flag */
1155 if( !p_forced[p_slave->i_type]
1156 && ( p_slave->b_forced || p_slave->i_priority == SLAVE_PRIORITY_USER ) )
1158 i_flags |= SLAVE_ADD_FORCED;
1159 b_forced = true;
1162 if( input_SlaveSourceAdd( p_input, p_slave->i_type, p_slave->psz_uri,
1163 i_flags ) == VLC_SUCCESS )
1165 input_item_AddSlave( input_priv(p_input)->p_item, p_slave );
1166 if( b_forced )
1167 p_forced[p_slave->i_type] = true;
1169 else
1170 input_item_slave_Delete( p_slave );
1172 TAB_CLEAN( i_slaves, pp_slaves );
1174 /* Load subtitles from attachments */
1175 int i_attachment = 0;
1176 input_attachment_t **pp_attachment = NULL;
1178 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
1179 for( int i = 0; i < input_priv(p_input)->i_attachment; i++ )
1181 const input_attachment_t *a = input_priv(p_input)->attachment[i];
1182 if( !strcmp( a->psz_mime, "application/x-srt" ) )
1183 TAB_APPEND( i_attachment, pp_attachment,
1184 vlc_input_attachment_New( a->psz_name, NULL,
1185 a->psz_description, NULL, 0 ) );
1187 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
1189 if( i_attachment > 0 )
1190 var_Create( p_input, "sub-description", VLC_VAR_STRING );
1191 for( int i = 0; i < i_attachment; i++ )
1193 input_attachment_t *a = pp_attachment[i];
1194 if( !a )
1195 continue;
1196 char *psz_mrl;
1197 if( a->psz_name[0] &&
1198 asprintf( &psz_mrl, "attachment://%s", a->psz_name ) >= 0 )
1200 var_SetString( p_input, "sub-description", a->psz_description ? a->psz_description : "");
1202 /* Force the first subtitle from attachment if there is no
1203 * subtitles already forced */
1204 if( input_SlaveSourceAdd( p_input, SLAVE_TYPE_SPU, psz_mrl,
1205 p_forced[ SLAVE_TYPE_SPU ] ?
1206 SLAVE_ADD_NOFLAG : SLAVE_ADD_FORCED ) == VLC_SUCCESS )
1207 p_forced[ SLAVE_TYPE_SPU ] = true;
1209 free( psz_mrl );
1210 /* Don't update item slaves for attachements */
1212 vlc_input_attachment_Delete( a );
1214 free( pp_attachment );
1215 if( i_attachment > 0 )
1216 var_Destroy( p_input, "sub-description" );
1219 static void UpdatePtsDelay( input_thread_t *p_input )
1221 input_thread_private_t *p_sys = input_priv(p_input);
1223 /* Get max pts delay from input source */
1224 mtime_t i_pts_delay = p_sys->master->i_pts_delay;
1225 for( int i = 0; i < p_sys->i_slave; i++ )
1226 i_pts_delay = __MAX( i_pts_delay, p_sys->slave[i]->i_pts_delay );
1228 if( i_pts_delay < 0 )
1229 i_pts_delay = 0;
1231 /* Take care of audio/spu delay */
1232 const mtime_t i_audio_delay = var_GetInteger( p_input, "audio-delay" );
1233 const mtime_t i_spu_delay = var_GetInteger( p_input, "spu-delay" );
1234 const mtime_t i_extra_delay = __MIN( i_audio_delay, i_spu_delay );
1235 if( i_extra_delay < 0 )
1236 i_pts_delay -= i_extra_delay;
1238 /* Update cr_average depending on the caching */
1239 const int i_cr_average = var_GetInteger( p_input, "cr-average" ) * i_pts_delay / DEFAULT_PTS_DELAY;
1241 /* */
1242 es_out_SetDelay( input_priv(p_input)->p_es_out_display, AUDIO_ES, i_audio_delay );
1243 es_out_SetDelay( input_priv(p_input)->p_es_out_display, SPU_ES, i_spu_delay );
1244 es_out_SetJitter( input_priv(p_input)->p_es_out, i_pts_delay, 0, i_cr_average );
1247 static void InitPrograms( input_thread_t * p_input )
1249 int i_es_out_mode;
1250 vlc_list_t list;
1252 /* Compute correct pts_delay */
1253 UpdatePtsDelay( p_input );
1255 /* Set up es_out */
1256 i_es_out_mode = ES_OUT_MODE_AUTO;
1257 if( input_priv(p_input)->p_sout )
1259 char *prgms;
1261 if( (prgms = var_GetNonEmptyString( p_input, "programs" )) != NULL )
1263 char *buf;
1265 TAB_INIT( list.i_count, list.p_values );
1266 for( const char *prgm = strtok_r( prgms, ",", &buf );
1267 prgm != NULL;
1268 prgm = strtok_r( NULL, ",", &buf ) )
1270 vlc_value_t val = { .i_int = atoi( prgm ) };
1271 TAB_APPEND(list.i_count, list.p_values, val);
1274 if( list.i_count > 0 )
1275 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1276 /* Note : we should remove the "program" callback. */
1278 free( prgms );
1280 else if( var_GetBool( p_input, "sout-all" ) )
1282 i_es_out_mode = ES_OUT_MODE_ALL;
1285 es_out_SetMode( input_priv(p_input)->p_es_out, i_es_out_mode );
1287 /* Inform the demuxer about waited group (needed only for DVB) */
1288 if( i_es_out_mode == ES_OUT_MODE_ALL )
1290 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_GROUP, -1, NULL );
1292 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1294 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_GROUP, -1,
1295 &list );
1296 TAB_CLEAN( list.i_count, list.p_values );
1298 else
1300 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_GROUP,
1301 es_out_GetGroupForced( input_priv(p_input)->p_es_out ), NULL );
1305 static int Init( input_thread_t * p_input )
1307 input_thread_private_t *priv = input_priv(p_input);
1308 input_source_t *master;
1310 if( var_Type( p_input->obj.parent, "meta-file" ) )
1312 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1313 var_SetString( p_input, "sout", "" );
1314 var_SetBool( p_input, "sout-all", false );
1315 var_SetString( p_input, "input-slave", "" );
1316 var_SetInteger( p_input, "input-repeat", 0 );
1317 var_SetString( p_input, "sub-file", "" );
1318 var_SetBool( p_input, "sub-autodetect-file", false );
1321 InitStatistics( p_input );
1322 #ifdef ENABLE_SOUT
1323 if( InitSout( p_input ) )
1324 goto error;
1325 #endif
1327 /* Create es out */
1328 priv->p_es_out = input_EsOutTimeshiftNew( p_input, priv->p_es_out_display,
1329 priv->i_rate );
1330 if( priv->p_es_out == NULL )
1331 goto error;
1333 /* */
1334 input_ChangeState( p_input, OPENING_S );
1335 input_SendEventCache( p_input, 0.0 );
1337 /* */
1338 master = InputSourceNew( p_input, priv->p_item->psz_uri, NULL, false );
1339 if( master == NULL )
1340 goto error;
1341 priv->master = master;
1343 InitTitle( p_input );
1345 /* Load master infos */
1346 /* Init length */
1347 mtime_t i_length;
1348 if( demux_Control( master->p_demux, DEMUX_GET_LENGTH, &i_length ) )
1349 i_length = 0;
1350 if( i_length <= 0 )
1351 i_length = input_item_GetDuration( priv->p_item );
1352 input_SendEventLength( p_input, i_length );
1354 input_SendEventPosition( p_input, 0.0, 0 );
1356 if( !priv->b_preparsing )
1358 StartTitle( p_input );
1359 SetSubtitlesOptions( p_input );
1360 LoadSlaves( p_input );
1361 InitPrograms( p_input );
1363 double f_rate = var_InheritFloat( p_input, "rate" );
1364 if( f_rate != 0.0 && f_rate != 1.0 )
1366 vlc_value_t val = { .i_int = INPUT_RATE_DEFAULT / f_rate };
1367 input_ControlPush( p_input, INPUT_CONTROL_SET_RATE, &val );
1371 if( !priv->b_preparsing && priv->p_sout )
1373 priv->b_out_pace_control = priv->p_sout->i_out_pace_nocontrol > 0;
1375 msg_Dbg( p_input, "starting in %ssync mode",
1376 priv->b_out_pace_control ? "a" : "" );
1379 vlc_meta_t *p_meta = vlc_meta_New();
1380 if( p_meta != NULL )
1382 /* Get meta data from users */
1383 InputMetaUser( p_input, p_meta );
1385 /* Get meta data from master input */
1386 InputSourceMeta( p_input, master, p_meta );
1388 /* And from slave */
1389 for( int i = 0; i < priv->i_slave; i++ )
1390 InputSourceMeta( p_input, priv->slave[i], p_meta );
1392 es_out_ControlSetMeta( priv->p_es_out, p_meta );
1393 vlc_meta_Delete( p_meta );
1396 msg_Dbg( p_input, "`%s' successfully opened",
1397 input_priv(p_input)->p_item->psz_uri );
1399 /* initialization is complete */
1400 input_ChangeState( p_input, PLAYING_S );
1402 return VLC_SUCCESS;
1404 error:
1405 input_ChangeState( p_input, ERROR_S );
1407 if( input_priv(p_input)->p_es_out )
1408 es_out_Delete( input_priv(p_input)->p_es_out );
1409 es_out_SetMode( input_priv(p_input)->p_es_out_display, ES_OUT_MODE_END );
1410 if( input_priv(p_input)->p_resource )
1412 if( input_priv(p_input)->p_sout )
1413 input_resource_RequestSout( input_priv(p_input)->p_resource,
1414 input_priv(p_input)->p_sout, NULL );
1415 input_resource_SetInput( input_priv(p_input)->p_resource, NULL );
1416 if( input_priv(p_input)->p_resource_private )
1417 input_resource_Terminate( input_priv(p_input)->p_resource_private );
1420 if( !priv->b_preparsing && libvlc_stats( p_input ) )
1422 #define EXIT_COUNTER( c ) do { if( input_priv(p_input)->counters.p_##c ) \
1423 stats_CounterClean( input_priv(p_input)->counters.p_##c );\
1424 input_priv(p_input)->counters.p_##c = NULL; } while(0)
1425 EXIT_COUNTER( read_bytes );
1426 EXIT_COUNTER( read_packets );
1427 EXIT_COUNTER( demux_read );
1428 EXIT_COUNTER( input_bitrate );
1429 EXIT_COUNTER( demux_bitrate );
1430 EXIT_COUNTER( demux_corrupted );
1431 EXIT_COUNTER( demux_discontinuity );
1432 EXIT_COUNTER( played_abuffers );
1433 EXIT_COUNTER( lost_abuffers );
1434 EXIT_COUNTER( displayed_pictures );
1435 EXIT_COUNTER( lost_pictures );
1436 EXIT_COUNTER( decoded_audio );
1437 EXIT_COUNTER( decoded_video );
1438 EXIT_COUNTER( decoded_sub );
1440 if( input_priv(p_input)->p_sout )
1442 EXIT_COUNTER( sout_sent_packets );
1443 EXIT_COUNTER( sout_sent_bytes );
1444 EXIT_COUNTER( sout_send_bitrate );
1446 #undef EXIT_COUNTER
1449 /* Mark them deleted */
1450 input_priv(p_input)->p_es_out = NULL;
1451 input_priv(p_input)->p_sout = NULL;
1453 return VLC_EGENERIC;
1456 /*****************************************************************************
1457 * End: end the input thread
1458 *****************************************************************************/
1459 static void End( input_thread_t * p_input )
1461 input_thread_private_t *priv = input_priv(p_input);
1463 /* We are at the end */
1464 input_ChangeState( p_input, END_S );
1466 /* Clean control variables */
1467 input_ControlVarStop( p_input );
1469 /* Stop es out activity */
1470 es_out_SetMode( priv->p_es_out, ES_OUT_MODE_NONE );
1472 /* Delete slave */
1473 for( int i = 0; i < priv->i_slave; i++ )
1474 InputSourceDestroy( priv->slave[i] );
1475 free( priv->slave );
1477 /* Clean up master */
1478 InputSourceDestroy( priv->master );
1479 priv->i_title = 0;
1480 priv->title = NULL;
1481 priv->i_title_offset = 0;
1482 priv->i_seekpoint_offset = 0;
1484 /* Unload all modules */
1485 if( priv->p_es_out )
1486 es_out_Delete( priv->p_es_out );
1487 es_out_SetMode( priv->p_es_out_display, ES_OUT_MODE_END );
1489 if( !priv->b_preparsing )
1491 #define CL_CO( c ) \
1492 do { \
1493 stats_CounterClean( priv->counters.p_##c ); \
1494 priv->counters.p_##c = NULL; \
1495 } while (0)
1497 if( libvlc_stats( p_input ) )
1499 /* make sure we are up to date */
1500 stats_ComputeInputStats( p_input, priv->p_item->p_stats );
1501 CL_CO( read_bytes );
1502 CL_CO( read_packets );
1503 CL_CO( demux_read );
1504 CL_CO( input_bitrate );
1505 CL_CO( demux_bitrate );
1506 CL_CO( demux_corrupted );
1507 CL_CO( demux_discontinuity );
1508 CL_CO( played_abuffers );
1509 CL_CO( lost_abuffers );
1510 CL_CO( displayed_pictures );
1511 CL_CO( lost_pictures );
1512 CL_CO( decoded_audio) ;
1513 CL_CO( decoded_video );
1514 CL_CO( decoded_sub) ;
1517 /* Close optional stream output instance */
1518 if( priv->p_sout )
1520 CL_CO( sout_sent_packets );
1521 CL_CO( sout_sent_bytes );
1522 CL_CO( sout_send_bitrate );
1524 #undef CL_CO
1527 vlc_mutex_lock( &priv->p_item->lock );
1528 if( priv->i_attachment > 0 )
1530 for( int i = 0; i < priv->i_attachment; i++ )
1531 vlc_input_attachment_Delete( priv->attachment[i] );
1532 TAB_CLEAN( priv->i_attachment, priv->attachment );
1533 free( priv->attachment_demux);
1534 priv->attachment_demux = NULL;
1537 /* clean bookmarks */
1538 for( int i = 0; i < priv->i_bookmark; ++i )
1539 vlc_seekpoint_Delete( priv->pp_bookmark[i] );
1540 TAB_CLEAN( priv->i_bookmark, priv->pp_bookmark );
1542 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
1544 /* */
1545 input_resource_RequestSout( input_priv(p_input)->p_resource,
1546 input_priv(p_input)->p_sout, NULL );
1547 input_resource_SetInput( input_priv(p_input)->p_resource, NULL );
1548 if( input_priv(p_input)->p_resource_private )
1549 input_resource_Terminate( input_priv(p_input)->p_resource_private );
1552 /*****************************************************************************
1553 * Control
1554 *****************************************************************************/
1555 void input_ControlPush( input_thread_t *p_input,
1556 int i_type, vlc_value_t *p_val )
1558 input_thread_private_t *sys = input_priv(p_input);
1560 vlc_mutex_lock( &sys->lock_control );
1561 if( sys->is_stopped || sys->i_control >= INPUT_CONTROL_FIFO_SIZE )
1563 if( sys->is_stopped )
1564 msg_Dbg( p_input, "input control stopped, trashing type=%d",
1565 i_type );
1566 else
1567 msg_Err( p_input, "input control fifo overflow, trashing type=%d",
1568 i_type );
1569 if( p_val )
1570 ControlRelease( i_type, *p_val );
1572 else
1574 input_control_t c;
1575 c.i_type = i_type;
1576 if( p_val )
1577 c.val = *p_val;
1578 else
1579 memset( &c.val, 0, sizeof(c.val) );
1581 sys->control[sys->i_control++] = c;
1583 vlc_cond_signal( &sys->wait_control );
1585 vlc_mutex_unlock( &sys->lock_control );
1588 static int ControlGetReducedIndexLocked( input_thread_t *p_input )
1590 const int i_lt = input_priv(p_input)->control[0].i_type;
1591 int i;
1592 for( i = 1; i < input_priv(p_input)->i_control; i++ )
1594 const int i_ct = input_priv(p_input)->control[i].i_type;
1596 if( i_lt == i_ct &&
1597 ( i_ct == INPUT_CONTROL_SET_STATE ||
1598 i_ct == INPUT_CONTROL_SET_RATE ||
1599 i_ct == INPUT_CONTROL_SET_POSITION ||
1600 i_ct == INPUT_CONTROL_SET_TIME ||
1601 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1602 i_ct == INPUT_CONTROL_SET_TITLE ||
1603 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1604 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1606 continue;
1608 else
1610 /* TODO but that's not that important
1611 - merge SET_X with SET_X_CMD
1612 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1613 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1616 break;
1619 return i - 1;
1623 static inline int ControlPop( input_thread_t *p_input,
1624 int *pi_type, vlc_value_t *p_val,
1625 mtime_t i_deadline, bool b_postpone_seek )
1627 input_thread_private_t *p_sys = input_priv(p_input);
1629 vlc_mutex_lock( &p_sys->lock_control );
1630 while( p_sys->i_control <= 0 ||
1631 ( b_postpone_seek && ControlIsSeekRequest( p_sys->control[0].i_type ) ) )
1633 if( p_sys->is_stopped )
1635 vlc_mutex_unlock( &p_sys->lock_control );
1636 return VLC_EGENERIC;
1639 if( i_deadline >= 0 )
1641 if( vlc_cond_timedwait( &p_sys->wait_control, &p_sys->lock_control,
1642 i_deadline ) )
1644 vlc_mutex_unlock( &p_sys->lock_control );
1645 return VLC_EGENERIC;
1648 else
1649 vlc_cond_wait( &p_sys->wait_control, &p_sys->lock_control );
1652 /* */
1653 const int i_index = ControlGetReducedIndexLocked( p_input );
1655 for( int i = 0; i < i_index; ++i )
1657 /* Release Reduced controls */
1658 ControlRelease( p_sys->control[i].i_type, p_sys->control[i].val );
1661 /* */
1662 *pi_type = p_sys->control[i_index].i_type;
1663 *p_val = p_sys->control[i_index].val;
1665 p_sys->i_control -= i_index + 1;
1666 if( p_sys->i_control > 0 )
1667 memmove( &p_sys->control[0], &p_sys->control[i_index+1],
1668 sizeof(*p_sys->control) * p_sys->i_control );
1669 vlc_mutex_unlock( &p_sys->lock_control );
1671 return VLC_SUCCESS;
1673 static bool ControlIsSeekRequest( int i_type )
1675 switch( i_type )
1677 case INPUT_CONTROL_SET_POSITION:
1678 case INPUT_CONTROL_SET_TIME:
1679 case INPUT_CONTROL_SET_TITLE:
1680 case INPUT_CONTROL_SET_TITLE_NEXT:
1681 case INPUT_CONTROL_SET_TITLE_PREV:
1682 case INPUT_CONTROL_SET_SEEKPOINT:
1683 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1684 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1685 case INPUT_CONTROL_SET_BOOKMARK:
1686 case INPUT_CONTROL_NAV_ACTIVATE:
1687 case INPUT_CONTROL_NAV_UP:
1688 case INPUT_CONTROL_NAV_DOWN:
1689 case INPUT_CONTROL_NAV_LEFT:
1690 case INPUT_CONTROL_NAV_RIGHT:
1691 case INPUT_CONTROL_NAV_POPUP:
1692 case INPUT_CONTROL_NAV_MENU:
1693 return true;
1694 default:
1695 return false;
1699 static void ControlRelease( int i_type, vlc_value_t val )
1701 switch( i_type )
1703 case INPUT_CONTROL_ADD_SLAVE:
1704 if( val.p_address )
1705 input_item_slave_Delete( val.p_address );
1706 break;
1707 case INPUT_CONTROL_SET_VIEWPOINT:
1708 case INPUT_CONTROL_SET_INITIAL_VIEWPOINT:
1709 case INPUT_CONTROL_UPDATE_VIEWPOINT:
1710 free( val.p_address );
1711 break;
1712 case INPUT_CONTROL_SET_RENDERER:
1713 if( val.p_address )
1714 vlc_renderer_item_release( val.p_address );
1715 break;
1717 default:
1718 break;
1722 /* Pause input */
1723 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1725 int i_state = PAUSE_S;
1727 if( input_priv(p_input)->b_can_pause )
1729 demux_t *p_demux = input_priv(p_input)->master->p_demux;
1731 if( demux_Control( p_demux, DEMUX_SET_PAUSE_STATE, true ) )
1733 msg_Warn( p_input, "cannot set pause state" );
1734 return;
1738 /* */
1739 if( es_out_SetPauseState( input_priv(p_input)->p_es_out, input_priv(p_input)->b_can_pause,
1740 true, i_control_date ) )
1742 msg_Warn( p_input, "cannot set pause state at es_out level" );
1743 return;
1746 /* Switch to new state */
1747 input_ChangeState( p_input, i_state );
1750 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1752 if( input_priv(p_input)->b_can_pause )
1754 demux_t *p_demux = input_priv(p_input)->master->p_demux;
1756 if( demux_Control( p_demux, DEMUX_SET_PAUSE_STATE, false ) )
1758 msg_Err( p_input, "cannot resume" );
1759 input_ChangeState( p_input, ERROR_S );
1760 return;
1764 /* Switch to play */
1765 input_ChangeState( p_input, PLAYING_S );
1766 es_out_SetPauseState( input_priv(p_input)->p_es_out, false, false, i_control_date );
1769 static void ViewpointApply( input_thread_t *p_input )
1771 input_thread_private_t *priv = input_priv(p_input);
1773 vlc_viewpoint_clip( &priv->viewpoint );
1775 vout_thread_t **pp_vout;
1776 size_t i_vout;
1777 input_resource_HoldVouts( priv->p_resource, &pp_vout, &i_vout );
1779 for( size_t i = 0; i < i_vout; ++i )
1781 var_SetAddress( pp_vout[i], "viewpoint", &priv->viewpoint );
1782 /* This variable can only be read from callbacks */
1783 var_Change( pp_vout[i], "viewpoint", VLC_VAR_SETVALUE,
1784 &(vlc_value_t) { .p_address = NULL }, NULL );
1785 vlc_object_release( pp_vout[i] );
1787 free( pp_vout );
1789 audio_output_t *p_aout = input_resource_HoldAout( priv->p_resource );
1790 if( p_aout )
1793 var_SetAddress( p_aout, "viewpoint", &priv->viewpoint );
1794 /* This variable can only be read from callbacks */
1795 var_Change( p_aout, "viewpoint", VLC_VAR_SETVALUE,
1796 &(vlc_value_t) { .p_address = NULL }, NULL );
1797 vlc_object_release( p_aout );
1801 static void ControlNav( input_thread_t *p_input, int i_type )
1803 input_thread_private_t *priv = input_priv(p_input);
1805 if( !demux_Control( priv->master->p_demux, i_type
1806 - INPUT_CONTROL_NAV_ACTIVATE + DEMUX_NAV_ACTIVATE ) )
1807 return; /* The demux handled the navigation control */
1809 /* Handle Up/Down/Left/Right if the demux can't navigate */
1810 vlc_viewpoint_t vp = {};
1811 int vol_direction = 0;
1812 int seek_direction = 0;
1813 switch( i_type )
1815 case INPUT_CONTROL_NAV_UP:
1816 vol_direction = 1;
1817 vp.pitch = -1.f;
1818 break;
1819 case INPUT_CONTROL_NAV_DOWN:
1820 vol_direction = -1;
1821 vp.pitch = 1.f;
1822 break;
1823 case INPUT_CONTROL_NAV_LEFT:
1824 seek_direction = -1;
1825 vp.yaw = -1.f;
1826 break;
1827 case INPUT_CONTROL_NAV_RIGHT:
1828 seek_direction = 1;
1829 vp.yaw = 1.f;
1830 break;
1831 case INPUT_CONTROL_NAV_ACTIVATE:
1832 case INPUT_CONTROL_NAV_POPUP:
1833 case INPUT_CONTROL_NAV_MENU:
1834 return;
1835 default:
1836 vlc_assert_unreachable();
1839 /* Try to change the viewpoint if possible */
1840 vout_thread_t **pp_vout;
1841 size_t i_vout;
1842 bool b_viewpoint_ch = false;
1843 input_resource_HoldVouts( priv->p_resource, &pp_vout, &i_vout );
1844 for( size_t i = 0; i < i_vout; ++i )
1846 if( !b_viewpoint_ch
1847 && var_GetBool( pp_vout[i], "viewpoint-changeable" ) )
1848 b_viewpoint_ch = true;
1849 vlc_object_release( pp_vout[i] );
1851 free( pp_vout );
1853 if( b_viewpoint_ch )
1855 priv->viewpoint_changed = true;
1856 priv->viewpoint.yaw += vp.yaw;
1857 priv->viewpoint.pitch += vp.pitch;
1858 priv->viewpoint.roll += vp.roll;
1859 priv->viewpoint.fov += vp.fov;
1860 ViewpointApply( p_input );
1861 return;
1864 /* Seek or change volume if the input doesn't have navigation or viewpoint */
1865 if( seek_direction != 0 )
1867 mtime_t it = var_InheritInteger( p_input, "short-jump-size" );
1868 var_SetInteger( p_input, "time-offset", it * seek_direction * CLOCK_FREQ );
1870 else
1872 assert( vol_direction != 0 );
1873 audio_output_t *p_aout = input_resource_HoldAout( priv->p_resource );
1874 if( p_aout )
1876 aout_VolumeUpdate( p_aout, vol_direction, NULL );
1877 vlc_object_release( p_aout );
1882 #ifdef ENABLE_SOUT
1883 static void ControlUpdateSout( input_thread_t *p_input, const char* psz_chain )
1885 var_SetString( p_input, "sout", psz_chain );
1886 if( psz_chain && *psz_chain )
1888 if( InitSout( p_input ) != VLC_SUCCESS )
1890 msg_Err( p_input, "Failed to start sout" );
1891 return;
1894 else
1896 input_resource_RequestSout( input_priv(p_input)->p_resource,
1897 input_priv(p_input)->p_sout, NULL );
1898 input_priv(p_input)->p_sout = NULL;
1900 es_out_Control( input_priv(p_input)->p_es_out, ES_OUT_RESTART_ALL_ES );
1902 #endif
1904 static void ControlInsertDemuxFilter( input_thread_t* p_input, const char* psz_demux_chain )
1906 input_source_t *p_inputSource = input_priv(p_input)->master;
1907 demux_t *p_filtered_demux = demux_FilterChainNew( p_inputSource->p_demux, psz_demux_chain );
1908 if ( p_filtered_demux != NULL )
1909 p_inputSource->p_demux = p_filtered_demux;
1910 else if ( psz_demux_chain != NULL )
1911 msg_Dbg(p_input, "Failed to create demux filter %s", psz_demux_chain);
1914 static bool Control( input_thread_t *p_input,
1915 int i_type, vlc_value_t val )
1917 const mtime_t i_control_date = mdate();
1918 /* FIXME b_force_update is abused, it should be carefully checked */
1919 bool b_force_update = false;
1921 if( !p_input )
1922 return b_force_update;
1924 switch( i_type )
1926 case INPUT_CONTROL_SET_POSITION:
1928 if( input_priv(p_input)->b_recording )
1930 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION ignored while recording" );
1931 break;
1934 float f_pos = val.f_float;
1935 if( f_pos < 0.f )
1936 f_pos = 0.f;
1937 else if( f_pos > 1.f )
1938 f_pos = 1.f;
1939 /* Reset the decoders states and clock sync (before calling the demuxer */
1940 es_out_SetTime( input_priv(p_input)->p_es_out, -1 );
1941 if( demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_POSITION,
1942 (double) f_pos, !input_priv(p_input)->b_fast_seek ) )
1944 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION "
1945 "%2.1f%% failed", (double)(f_pos * 100.f) );
1947 else
1949 if( input_priv(p_input)->i_slave > 0 )
1950 SlaveSeek( p_input );
1951 input_priv(p_input)->master->b_eof = false;
1953 b_force_update = true;
1955 break;
1958 case INPUT_CONTROL_SET_TIME:
1960 int64_t i_time;
1961 int i_ret;
1963 if( input_priv(p_input)->b_recording )
1965 msg_Err( p_input, "INPUT_CONTROL_SET_TIME ignored while recording" );
1966 break;
1969 i_time = val.i_int;
1970 if( i_time < 0 )
1971 i_time = 0;
1973 /* Reset the decoders states and clock sync (before calling the demuxer */
1974 es_out_SetTime( input_priv(p_input)->p_es_out, -1 );
1976 i_ret = demux_Control( input_priv(p_input)->master->p_demux,
1977 DEMUX_SET_TIME, i_time,
1978 !input_priv(p_input)->b_fast_seek );
1979 if( i_ret )
1981 int64_t i_length;
1983 /* Emulate it with a SET_POS */
1984 if( !demux_Control( input_priv(p_input)->master->p_demux,
1985 DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
1987 double f_pos = (double)i_time / (double)i_length;
1988 i_ret = demux_Control( input_priv(p_input)->master->p_demux,
1989 DEMUX_SET_POSITION, f_pos,
1990 !input_priv(p_input)->b_fast_seek );
1993 if( i_ret )
1995 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME %"PRId64
1996 " failed or not possible", i_time );
1998 else
2000 if( input_priv(p_input)->i_slave > 0 )
2001 SlaveSeek( p_input );
2002 input_priv(p_input)->master->b_eof = false;
2004 b_force_update = true;
2006 break;
2009 case INPUT_CONTROL_SET_STATE:
2010 switch( val.i_int )
2012 case PLAYING_S:
2013 if( input_priv(p_input)->i_state == PAUSE_S )
2015 ControlUnpause( p_input, i_control_date );
2016 b_force_update = true;
2018 break;
2019 case PAUSE_S:
2020 if( input_priv(p_input)->i_state == PLAYING_S )
2022 ControlPause( p_input, i_control_date );
2023 b_force_update = true;
2025 break;
2026 default:
2027 msg_Err( p_input, "invalid INPUT_CONTROL_SET_STATE" );
2029 break;
2031 case INPUT_CONTROL_SET_RATE:
2033 /* Get rate and direction */
2034 long long i_rate = llabs( val.i_int );
2035 int i_rate_sign = val.i_int < 0 ? -1 : 1;
2037 /* Check rate bound */
2038 if( i_rate < INPUT_RATE_MIN )
2040 msg_Dbg( p_input, "cannot set rate faster" );
2041 i_rate = INPUT_RATE_MIN;
2043 else if( i_rate > INPUT_RATE_MAX )
2045 msg_Dbg( p_input, "cannot set rate slower" );
2046 i_rate = INPUT_RATE_MAX;
2049 /* Apply direction */
2050 if( i_rate_sign < 0 )
2052 if( input_priv(p_input)->master->b_rescale_ts )
2054 msg_Dbg( p_input, "cannot set negative rate" );
2055 i_rate = input_priv(p_input)->i_rate;
2056 assert( i_rate > 0 );
2058 else
2060 i_rate *= i_rate_sign;
2064 if( i_rate != INPUT_RATE_DEFAULT &&
2065 ( ( !input_priv(p_input)->b_can_rate_control && !input_priv(p_input)->master->b_rescale_ts ) ||
2066 ( input_priv(p_input)->p_sout && !input_priv(p_input)->b_out_pace_control ) ) )
2068 msg_Dbg( p_input, "cannot change rate" );
2069 i_rate = INPUT_RATE_DEFAULT;
2071 if( i_rate != input_priv(p_input)->i_rate &&
2072 !input_priv(p_input)->b_can_pace_control && input_priv(p_input)->b_can_rate_control )
2074 if( !input_priv(p_input)->master->b_rescale_ts )
2075 es_out_Control( input_priv(p_input)->p_es_out, ES_OUT_RESET_PCR );
2077 if( demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_RATE,
2078 &i_rate ) )
2080 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
2081 i_rate = input_priv(p_input)->i_rate;
2085 /* */
2086 if( i_rate != input_priv(p_input)->i_rate )
2088 input_priv(p_input)->i_rate = i_rate;
2089 input_SendEventRate( p_input, i_rate );
2091 if( input_priv(p_input)->master->b_rescale_ts )
2093 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;
2094 es_out_SetRate( input_priv(p_input)->p_es_out, i_rate_source, i_rate );
2097 b_force_update = true;
2099 break;
2102 case INPUT_CONTROL_SET_PROGRAM:
2103 /* No need to force update, es_out does it if needed */
2104 es_out_Control( input_priv(p_input)->p_es_out,
2105 ES_OUT_SET_GROUP, val.i_int );
2107 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_GROUP, val.i_int,
2108 NULL );
2109 break;
2111 case INPUT_CONTROL_SET_ES:
2112 /* No need to force update, es_out does it if needed */
2113 es_out_Control( input_priv(p_input)->p_es_out_display,
2114 ES_OUT_SET_ES_BY_ID, (int)val.i_int );
2116 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_ES, (int)val.i_int );
2117 break;
2119 case INPUT_CONTROL_RESTART_ES:
2120 es_out_Control( input_priv(p_input)->p_es_out_display,
2121 ES_OUT_RESTART_ES_BY_ID, (int)val.i_int );
2122 break;
2124 case INPUT_CONTROL_SET_VIEWPOINT:
2125 case INPUT_CONTROL_SET_INITIAL_VIEWPOINT:
2126 case INPUT_CONTROL_UPDATE_VIEWPOINT:
2128 input_thread_private_t *priv = input_priv(p_input);
2129 const vlc_viewpoint_t *p_vp = val.p_address;
2131 if ( i_type == INPUT_CONTROL_SET_INITIAL_VIEWPOINT )
2134 /* Set the initial viewpoint if it had not been changed by the
2135 * user. */
2136 if( !priv->viewpoint_changed )
2137 priv->viewpoint = *p_vp;
2138 /* Update viewpoints of aout and every vouts in all cases. */
2140 else if ( i_type == INPUT_CONTROL_SET_VIEWPOINT)
2142 priv->viewpoint_changed = true;
2143 priv->viewpoint = *p_vp;
2145 else
2147 priv->viewpoint_changed = true;
2148 priv->viewpoint.yaw += p_vp->yaw;
2149 priv->viewpoint.pitch += p_vp->pitch;
2150 priv->viewpoint.roll += p_vp->roll;
2151 priv->viewpoint.fov += p_vp->fov;
2154 ViewpointApply( p_input );
2155 break;
2158 case INPUT_CONTROL_SET_AUDIO_DELAY:
2159 input_SendEventAudioDelay( p_input, val.i_int );
2160 UpdatePtsDelay( p_input );
2161 break;
2163 case INPUT_CONTROL_SET_SPU_DELAY:
2164 input_SendEventSubtitleDelay( p_input, val.i_int );
2165 UpdatePtsDelay( p_input );
2166 break;
2168 case INPUT_CONTROL_SET_TITLE:
2169 case INPUT_CONTROL_SET_TITLE_NEXT:
2170 case INPUT_CONTROL_SET_TITLE_PREV:
2172 if( input_priv(p_input)->b_recording )
2174 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
2175 break;
2177 if( input_priv(p_input)->master->i_title <= 0 )
2178 break;
2180 int i_title = demux_GetTitle( input_priv(p_input)->master->p_demux );
2181 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
2182 i_title--;
2183 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
2184 i_title++;
2185 else
2186 i_title = val.i_int;
2187 if( i_title < 0 || i_title >= input_priv(p_input)->master->i_title )
2188 break;
2190 es_out_SetTime( input_priv(p_input)->p_es_out, -1 );
2191 demux_Control( input_priv(p_input)->master->p_demux,
2192 DEMUX_SET_TITLE, i_title );
2193 input_SendEventTitle( p_input, i_title );
2194 break;
2196 case INPUT_CONTROL_SET_SEEKPOINT:
2197 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
2198 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
2200 if( input_priv(p_input)->b_recording )
2202 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
2203 break;
2205 if( input_priv(p_input)->master->i_title <= 0 )
2206 break;
2208 demux_t *p_demux = input_priv(p_input)->master->p_demux;
2210 int i_title = demux_GetTitle( p_demux );
2211 int i_seekpoint = demux_GetSeekpoint( p_demux );
2213 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
2215 int64_t i_seekpoint_time = input_priv(p_input)->master->title[i_title]->seekpoint[i_seekpoint]->i_time_offset;
2216 int64_t i_input_time = var_GetInteger( p_input, "time" );
2217 if( i_seekpoint_time >= 0 && i_input_time >= 0 )
2219 if( i_input_time < i_seekpoint_time + 3000000 )
2220 i_seekpoint--;
2222 else
2223 i_seekpoint--;
2225 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
2226 i_seekpoint++;
2227 else
2228 i_seekpoint = val.i_int;
2229 if( i_seekpoint < 0
2230 || i_seekpoint >= input_priv(p_input)->master->title[i_title]->i_seekpoint )
2231 break;
2233 es_out_SetTime( input_priv(p_input)->p_es_out, -1 );
2234 demux_Control( input_priv(p_input)->master->p_demux,
2235 DEMUX_SET_SEEKPOINT, i_seekpoint );
2236 input_SendEventSeekpoint( p_input, i_title, i_seekpoint );
2237 break;
2240 case INPUT_CONTROL_ADD_SLAVE:
2241 if( val.p_address )
2243 input_item_slave_t *p_item_slave = val.p_address;
2244 unsigned i_flags = SLAVE_ADD_CANFAIL | SLAVE_ADD_SET_TIME;
2245 if( p_item_slave->b_forced )
2246 i_flags |= SLAVE_ADD_FORCED;
2248 if( input_SlaveSourceAdd( p_input, p_item_slave->i_type,
2249 p_item_slave->psz_uri, i_flags )
2250 == VLC_SUCCESS )
2252 /* Update item slaves */
2253 input_item_AddSlave( input_priv(p_input)->p_item, p_item_slave );
2254 /* The slave is now owned by the item */
2255 val.p_address = NULL;
2258 break;
2260 case INPUT_CONTROL_SET_RECORD_STATE:
2261 if( !!input_priv(p_input)->b_recording != !!val.b_bool )
2263 if( input_priv(p_input)->master->b_can_stream_record )
2265 if( demux_Control( input_priv(p_input)->master->p_demux,
2266 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2267 val.b_bool = false;
2269 else
2271 if( es_out_SetRecordState( input_priv(p_input)->p_es_out_display, val.b_bool ) )
2272 val.b_bool = false;
2274 input_priv(p_input)->b_recording = val.b_bool;
2276 input_SendEventRecord( p_input, val.b_bool );
2278 b_force_update = true;
2280 break;
2282 case INPUT_CONTROL_SET_FRAME_NEXT:
2283 if( input_priv(p_input)->i_state == PAUSE_S )
2285 es_out_SetFrameNext( input_priv(p_input)->p_es_out );
2287 else if( input_priv(p_input)->i_state == PLAYING_S )
2289 ControlPause( p_input, i_control_date );
2291 else
2293 msg_Err( p_input, "invalid state for frame next" );
2295 b_force_update = true;
2296 break;
2298 case INPUT_CONTROL_SET_BOOKMARK:
2300 mtime_t time_offset = -1;
2302 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
2303 if( val.i_int >= 0 && val.i_int < input_priv(p_input)->i_bookmark )
2305 const seekpoint_t *p_bookmark = input_priv(p_input)->pp_bookmark[val.i_int];
2306 time_offset = p_bookmark->i_time_offset;
2308 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
2310 if( time_offset < 0 )
2312 msg_Err( p_input, "invalid bookmark %"PRId64, val.i_int );
2313 break;
2316 val.i_int = time_offset;
2317 b_force_update = Control( p_input, INPUT_CONTROL_SET_TIME, val );
2318 break;
2320 case INPUT_CONTROL_SET_RENDERER:
2322 #ifdef ENABLE_SOUT
2323 vlc_renderer_item_t *p_item = val.p_address;
2324 input_thread_private_t *p_priv = input_priv( p_input );
2325 // We do not support switching from a renderer to another for now
2326 if ( p_item == NULL && p_priv->p_renderer == NULL )
2327 break;
2329 if ( p_priv->p_renderer )
2331 ControlUpdateSout( p_input, NULL );
2332 demux_FilterDisable( p_priv->master->p_demux,
2333 vlc_renderer_item_demux_filter( p_priv->p_renderer ) );
2334 vlc_renderer_item_release( p_priv->p_renderer );
2335 p_priv->p_renderer = NULL;
2337 if( p_item != NULL )
2339 p_priv->p_renderer = vlc_renderer_item_hold( p_item );
2340 ControlUpdateSout( p_input, vlc_renderer_item_sout( p_item ) );
2341 if( !demux_FilterEnable( p_priv->master->p_demux,
2342 vlc_renderer_item_demux_filter( p_priv->p_renderer ) ) )
2344 ControlInsertDemuxFilter( p_input,
2345 vlc_renderer_item_demux_filter( p_item ) );
2347 input_resource_TerminateVout( p_priv->p_resource );
2349 #endif
2350 break;
2353 case INPUT_CONTROL_NAV_ACTIVATE:
2354 case INPUT_CONTROL_NAV_UP:
2355 case INPUT_CONTROL_NAV_DOWN:
2356 case INPUT_CONTROL_NAV_LEFT:
2357 case INPUT_CONTROL_NAV_RIGHT:
2358 case INPUT_CONTROL_NAV_POPUP:
2359 case INPUT_CONTROL_NAV_MENU:
2360 ControlNav( p_input, i_type );
2361 break;
2363 default:
2364 msg_Err( p_input, "not yet implemented" );
2365 break;
2368 ControlRelease( i_type, val );
2369 return b_force_update;
2372 /*****************************************************************************
2373 * UpdateTitleSeekpoint
2374 *****************************************************************************/
2375 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2376 int i_title, int i_seekpoint )
2378 int i_title_end = input_priv(p_input)->master->i_title_end -
2379 input_priv(p_input)->master->i_title_offset;
2380 int i_seekpoint_end = input_priv(p_input)->master->i_seekpoint_end -
2381 input_priv(p_input)->master->i_seekpoint_offset;
2383 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2385 if( i_title > i_title_end ||
2386 ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2387 return VLC_DEMUXER_EOF;
2389 else if( i_seekpoint_end >= 0 )
2391 if( i_seekpoint > i_seekpoint_end )
2392 return VLC_DEMUXER_EOF;
2394 else if( i_title_end >= 0 )
2396 if( i_title > i_title_end )
2397 return VLC_DEMUXER_EOF;
2399 return VLC_DEMUXER_SUCCESS;
2401 /*****************************************************************************
2402 * Update*FromDemux:
2403 *****************************************************************************/
2404 static int UpdateTitleSeekpointFromDemux( input_thread_t *p_input )
2406 demux_t *p_demux = input_priv(p_input)->master->p_demux;
2408 /* TODO event-like */
2409 if( demux_TestAndClearFlags( p_demux, INPUT_UPDATE_TITLE ) )
2410 input_SendEventTitle( p_input, demux_GetTitle( p_demux ) );
2412 if( demux_TestAndClearFlags( p_demux, INPUT_UPDATE_SEEKPOINT ) )
2413 input_SendEventSeekpoint( p_input, demux_GetTitle( p_demux ),
2414 demux_GetSeekpoint( p_demux ) );
2416 return UpdateTitleSeekpoint( p_input,
2417 demux_GetTitle( p_demux ),
2418 demux_GetSeekpoint( p_demux ) );
2421 static void UpdateGenericFromDemux( input_thread_t *p_input )
2423 demux_t *p_demux = input_priv(p_input)->master->p_demux;
2425 if( demux_TestAndClearFlags( p_demux, INPUT_UPDATE_META ) )
2426 InputUpdateMeta( p_input, p_demux );
2429 double quality;
2430 double strength;
2432 if( !demux_Control( p_demux, DEMUX_GET_SIGNAL, &quality, &strength ) )
2433 input_SendEventSignal( p_input, quality, strength );
2437 static void UpdateTitleListfromDemux( input_thread_t *p_input )
2439 input_thread_private_t *priv = input_priv(p_input);
2440 input_source_t *in = priv->master;
2442 /* Delete the preexisting titles */
2443 if( in->i_title > 0 )
2445 for( int i = 0; i < in->i_title; i++ )
2446 vlc_input_title_Delete( in->title[i] );
2447 TAB_CLEAN( in->i_title, in->title );
2448 priv->i_title = 0;
2449 priv->title = NULL;
2450 in->b_title_demux = false;
2453 /* Get the new title list */
2454 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2455 &in->title, &in->i_title,
2456 &in->i_title_offset, &in->i_seekpoint_offset ) )
2457 TAB_INIT( in->i_title, in->title );
2458 else
2459 in->b_title_demux = true;
2461 InitTitle( p_input );
2464 static int
2465 InputStreamHandleAnchor( input_source_t *source, stream_t **stream,
2466 char const *anchor )
2468 char const* extra;
2469 if( stream_extractor_AttachParsed( stream, anchor, &extra ) )
2471 msg_Err( source, "unable to attach stream-extractors for %s",
2472 (*stream)->psz_url );
2474 return VLC_EGENERIC;
2477 if( vlc_stream_directory_Attach( stream, NULL ) )
2478 msg_Dbg( source, "attachment of directory-extractor failed for %s",
2479 (*stream)->psz_url );
2481 MRLSections( extra ? extra : "",
2482 &source->i_title_start, &source->i_title_end,
2483 &source->i_seekpoint_start, &source->i_seekpoint_end );
2485 return VLC_SUCCESS;
2488 static demux_t *InputDemuxNew( input_thread_t *p_input, input_source_t *p_source,
2489 const char *psz_access, const char *psz_demux,
2490 const char *psz_path, const char *psz_anchor )
2492 input_thread_private_t *priv = input_priv(p_input );
2493 demux_t *p_demux = NULL;
2495 /* first, try to create an access demux */
2496 p_demux = demux_NewAdvanced( VLC_OBJECT( p_source ), p_input,
2497 psz_access, psz_access, psz_path,
2498 NULL, priv->p_es_out, priv->b_preparsing );
2499 if( p_demux )
2501 MRLSections( psz_anchor,
2502 &p_source->i_title_start, &p_source->i_title_end,
2503 &p_source->i_seekpoint_start, &p_source->i_seekpoint_end );
2505 return p_demux;
2508 /* not an access-demux: create the underlying access stream */
2509 char *psz_base_mrl;
2511 if( asprintf( &psz_base_mrl, "%s://%s", psz_access, psz_path ) < 0 )
2512 return NULL;
2514 char *psz_filters = var_InheritString( p_source, "stream-filter" );
2515 stream_t* p_stream = stream_AccessNew( VLC_OBJECT( p_source ), p_input,
2516 priv->b_preparsing,
2517 psz_base_mrl );
2518 FREENULL( psz_base_mrl );
2520 if( p_stream == NULL )
2521 goto error;
2523 /* attach explicit stream filters to stream */
2524 if( psz_filters )
2525 p_stream = stream_FilterChainNew( p_stream, psz_filters );
2527 FREENULL( psz_filters );
2529 /* handle anchors */
2530 if( InputStreamHandleAnchor( p_source, &p_stream, psz_anchor ) )
2531 goto error;
2533 /* attach conditional record stream-filter */
2534 if( var_InheritBool( p_source, "input-record-native" ) )
2535 p_stream = stream_FilterChainNew( p_stream, "record" );
2537 /* create a regular demux with the access stream created */
2538 p_demux = demux_NewAdvanced( VLC_OBJECT( p_source ), p_input,
2539 psz_access, psz_demux, psz_path,
2540 p_stream, priv->p_es_out,
2541 priv->b_preparsing );
2542 if( p_demux )
2543 return p_demux;
2545 error:
2546 free( psz_base_mrl );
2547 free( psz_filters );
2549 if( p_stream )
2550 vlc_stream_Delete( p_stream );
2552 return NULL;
2555 /*****************************************************************************
2556 * InputSourceNew:
2557 *****************************************************************************/
2558 static input_source_t *InputSourceNew( input_thread_t *p_input,
2559 const char *psz_mrl,
2560 const char *psz_forced_demux,
2561 bool b_in_can_fail )
2563 input_thread_private_t *priv = input_priv(p_input);
2564 input_source_t *in = vlc_custom_create( p_input, sizeof( *in ),
2565 "input source" );
2566 if( unlikely(in == NULL) )
2567 return NULL;
2569 const char *psz_access, *psz_demux, *psz_path, *psz_anchor = NULL;
2571 assert( psz_mrl );
2572 char *psz_dup = strdup( psz_mrl );
2573 char *psz_demux_var = NULL;
2575 if( psz_dup == NULL )
2577 vlc_object_release( in );
2578 return NULL;
2581 /* Split uri */
2582 input_SplitMRL( &psz_access, &psz_demux, &psz_path, &psz_anchor, psz_dup );
2584 if( psz_demux == NULL || psz_demux[0] == '\0' )
2585 psz_demux = psz_demux_var = var_InheritString( in, "demux" );
2587 if( psz_forced_demux != NULL )
2588 psz_demux = psz_forced_demux;
2590 if( psz_demux == NULL )
2591 psz_demux = "any";
2593 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2594 psz_mrl, psz_access, psz_demux, psz_path );
2596 if( input_priv(p_input)->master == NULL /* XXX ugly */)
2597 { /* On master stream only, use input-list */
2598 char *str = var_InheritString( p_input, "input-list" );
2599 if( str != NULL )
2601 char *list;
2603 var_Create( p_input, "concat-list", VLC_VAR_STRING );
2604 if( likely(asprintf( &list, "%s://%s,%s", psz_access, psz_path,
2605 str ) >= 0) )
2607 var_SetString( p_input, "concat-list", list );
2608 free( list );
2610 free( str );
2611 psz_access = "concat";
2615 if( strcasecmp( psz_access, "concat" ) )
2616 { /* Autodetect extra files if none specified */
2617 int count;
2618 char **tab;
2620 TAB_INIT( count, tab );
2621 InputGetExtraFiles( p_input, &count, &tab, &psz_access, psz_path );
2622 if( count > 0 )
2624 char *list = NULL;
2626 for( int i = 0; i < count; i++ )
2628 char *str;
2629 if( asprintf( &str, "%s,%s", list ? list : psz_mrl,
2630 tab[i] ) < 0 )
2631 break;
2633 free( tab[i] );
2634 free( list );
2635 list = str;
2638 var_Create( p_input, "concat-list", VLC_VAR_STRING );
2639 if( likely(list != NULL) )
2641 var_SetString( p_input, "concat-list", list );
2642 free( list );
2645 TAB_CLEAN( count, tab );
2648 in->p_demux = InputDemuxNew( p_input, in, psz_access, psz_demux,
2649 psz_path, psz_anchor );
2651 free( psz_demux_var );
2652 free( psz_dup );
2654 if( in->p_demux == NULL )
2656 if( !b_in_can_fail && !input_Stopped( p_input ) )
2657 vlc_dialog_display_error( p_input, _("Your input can't be opened"),
2658 _("VLC is unable to open the MRL '%s'."
2659 " Check the log for details."), psz_mrl );
2660 vlc_object_release( in );
2661 return NULL;
2664 char *psz_demux_chain = NULL;
2665 if( priv->p_renderer )
2667 const char* psz_renderer_demux = vlc_renderer_item_demux_filter(
2668 priv->p_renderer );
2669 if( psz_renderer_demux )
2670 psz_demux_chain = strdup( psz_renderer_demux );
2672 if( !psz_demux_chain )
2673 psz_demux_chain = var_GetNonEmptyString(p_input, "demux-filter");
2674 if( psz_demux_chain != NULL ) /* add the chain of demux filters */
2676 in->p_demux = demux_FilterChainNew( in->p_demux, psz_demux_chain );
2677 free( psz_demux_chain );
2679 if( in->p_demux == NULL )
2681 msg_Err(p_input, "Failed to create demux filter");
2682 vlc_object_release( in );
2683 return NULL;
2687 /* Get infos from (access_)demux */
2688 bool b_can_seek;
2689 if( demux_Control( in->p_demux, DEMUX_CAN_SEEK, &b_can_seek ) )
2690 b_can_seek = false;
2691 var_SetBool( p_input, "can-seek", b_can_seek );
2693 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2694 &in->b_can_pace_control ) )
2695 in->b_can_pace_control = false;
2697 assert( in->p_demux->pf_demux != NULL || !in->b_can_pace_control );
2699 if( !in->b_can_pace_control )
2701 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2702 &in->b_can_rate_control ) )
2704 in->b_can_rate_control = false;
2705 in->b_rescale_ts = true;
2707 else
2708 in->b_rescale_ts = !in->b_can_rate_control;
2710 else
2712 in->b_can_rate_control = true;
2713 in->b_rescale_ts = true;
2716 demux_Control( in->p_demux, DEMUX_CAN_PAUSE, &in->b_can_pause );
2718 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2719 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2720 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control && in->b_can_rate_control );
2722 /* Set record capabilities */
2723 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2724 in->b_can_stream_record = false;
2725 #ifdef ENABLE_SOUT
2726 if( !var_GetBool( p_input, "input-record-native" ) )
2727 in->b_can_stream_record = false;
2728 var_SetBool( p_input, "can-record", true );
2729 #else
2730 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2731 #endif
2733 /* get attachment
2734 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2735 if( !input_priv(p_input)->b_preparsing )
2737 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2738 &in->title, &in->i_title,
2739 &in->i_title_offset, &in->i_seekpoint_offset ))
2741 TAB_INIT( in->i_title, in->title );
2743 else
2745 in->b_title_demux = true;
2748 int i_attachment;
2749 input_attachment_t **attachment;
2750 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2751 &attachment, &i_attachment ) )
2753 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
2754 AppendAttachment( &input_priv(p_input)->i_attachment, &input_priv(p_input)->attachment, &input_priv(p_input)->attachment_demux,
2755 i_attachment, attachment, in->p_demux );
2756 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
2759 demux_Control( in->p_demux, DEMUX_GET_PTS_DELAY, &in->i_pts_delay );
2760 if( in->i_pts_delay > INPUT_PTS_DELAY_MAX )
2761 in->i_pts_delay = INPUT_PTS_DELAY_MAX;
2762 else if( in->i_pts_delay < 0 )
2763 in->i_pts_delay = 0;
2766 if( demux_Control( in->p_demux, DEMUX_GET_FPS, &in->f_fps ) )
2767 in->f_fps = 0.f;
2769 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2770 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2772 return in;
2775 /*****************************************************************************
2776 * InputSourceDestroy:
2777 *****************************************************************************/
2778 static void InputSourceDestroy( input_source_t *in )
2780 int i;
2782 if( in->p_demux )
2783 demux_Delete( in->p_demux );
2785 if( in->i_title > 0 )
2787 for( i = 0; i < in->i_title; i++ )
2788 vlc_input_title_Delete( in->title[i] );
2789 TAB_CLEAN( in->i_title, in->title );
2792 vlc_object_release( in );
2795 /*****************************************************************************
2796 * InputSourceMeta:
2797 *****************************************************************************/
2798 static void InputSourceMeta( input_thread_t *p_input,
2799 input_source_t *p_source, vlc_meta_t *p_meta )
2801 demux_t *p_demux = p_source->p_demux;
2803 /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
2804 * is a bad idea */
2806 bool has_meta = false;
2808 /* Read demux meta */
2809 if( !demux_Control( p_demux, DEMUX_GET_META, p_meta ) )
2810 has_meta = true;
2812 bool has_unsupported;
2813 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &has_unsupported ) )
2814 has_unsupported = true;
2816 /* If the demux report unsupported meta data, or if we don't have meta data
2817 * try an external "meta reader" */
2818 if( has_meta && !has_unsupported )
2819 return;
2821 demux_meta_t *p_demux_meta =
2822 vlc_custom_create( p_source, sizeof( *p_demux_meta ), "demux meta" );
2823 if( unlikely(p_demux_meta == NULL) )
2824 return;
2825 p_demux_meta->p_item = input_priv(p_input)->p_item;
2827 module_t *p_id3 = module_need( p_demux_meta, "meta reader", NULL, false );
2828 if( p_id3 )
2830 if( p_demux_meta->p_meta )
2832 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2833 vlc_meta_Delete( p_demux_meta->p_meta );
2836 if( p_demux_meta->i_attachments > 0 )
2838 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
2839 AppendAttachment( &input_priv(p_input)->i_attachment, &input_priv(p_input)->attachment, &input_priv(p_input)->attachment_demux,
2840 p_demux_meta->i_attachments, p_demux_meta->attachments, p_demux);
2841 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
2843 module_unneed( p_demux, p_id3 );
2845 vlc_object_release( p_demux_meta );
2849 static void SlaveDemux( input_thread_t *p_input )
2851 int64_t i_time;
2852 int i;
2854 if( demux_Control( input_priv(p_input)->master->p_demux, DEMUX_GET_TIME, &i_time ) )
2856 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2857 return;
2860 for( i = 0; i < input_priv(p_input)->i_slave; i++ )
2862 input_source_t *in = input_priv(p_input)->slave[i];
2863 int i_ret;
2865 if( in->b_eof )
2866 continue;
2868 /* Call demux_Demux until we have read enough data */
2869 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2871 for( ;; )
2873 int64_t i_stime;
2874 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2876 msg_Err( p_input, "slave[%d] doesn't like "
2877 "DEMUX_GET_TIME -> EOF", i );
2878 i_ret = 0;
2879 break;
2882 if( i_stime >= i_time )
2884 i_ret = 1;
2885 break;
2888 if( ( i_ret = demux_Demux( in->p_demux ) ) <= 0 )
2889 break;
2892 else
2894 i_ret = demux_Demux( in->p_demux );
2897 if( i_ret <= 0 )
2899 msg_Dbg( p_input, "slave %d EOF", i );
2900 in->b_eof = true;
2905 static void SlaveSeek( input_thread_t *p_input )
2907 int64_t i_time;
2908 int i;
2910 if( demux_Control( input_priv(p_input)->master->p_demux, DEMUX_GET_TIME, &i_time ) )
2912 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2913 return;
2916 for( i = 0; i < input_priv(p_input)->i_slave; i++ )
2918 input_source_t *in = input_priv(p_input)->slave[i];
2920 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time, true ) )
2922 if( !in->b_eof )
2923 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2924 in->b_eof = true;
2926 else
2928 in->b_eof = false;
2933 /*****************************************************************************
2934 * InputMetaUser:
2935 *****************************************************************************/
2936 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2938 static const struct { int i_meta; const char *psz_name; } p_list[] = {
2939 { vlc_meta_Title, "meta-title" },
2940 { vlc_meta_Artist, "meta-artist" },
2941 { vlc_meta_Genre, "meta-genre" },
2942 { vlc_meta_Copyright, "meta-copyright" },
2943 { vlc_meta_Description, "meta-description" },
2944 { vlc_meta_Date, "meta-date" },
2945 { vlc_meta_URL, "meta-url" },
2946 { 0, NULL }
2949 /* Get meta information from user */
2950 for( int i = 0; p_list[i].psz_name; i++ )
2952 char *psz_string = var_GetNonEmptyString( p_input, p_list[i].psz_name );
2953 if( !psz_string )
2954 continue;
2956 EnsureUTF8( psz_string );
2957 vlc_meta_Set( p_meta, p_list[i].i_meta, psz_string );
2958 free( psz_string );
2962 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2963 const demux_t ***ppp_attachment_demux,
2964 int i_new, input_attachment_t **pp_new, const demux_t *p_demux )
2966 int i_attachment = *pi_attachment;
2967 int i;
2969 input_attachment_t **pp_att = realloc( *ppp_attachment,
2970 sizeof(*pp_att) * ( i_attachment + i_new ) );
2971 if( likely(pp_att) )
2973 *ppp_attachment = pp_att;
2974 const demux_t **pp_attdmx = realloc( *ppp_attachment_demux,
2975 sizeof(*pp_attdmx) * ( i_attachment + i_new ) );
2976 if( likely(pp_attdmx) )
2978 *ppp_attachment_demux = pp_attdmx;
2980 for( i = 0; i < i_new; i++ )
2982 pp_att[i_attachment] = pp_new[i];
2983 pp_attdmx[i_attachment++] = p_demux;
2985 /* */
2986 *pi_attachment = i_attachment;
2987 free( pp_new );
2988 return;
2992 /* on alloc errors */
2993 for( i = 0; i < i_new; i++ )
2994 vlc_input_attachment_Delete( pp_new[i] );
2995 free( pp_new );
2998 /*****************************************************************************
2999 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
3000 * arturl and locking issue.
3001 *****************************************************************************/
3002 static void InputUpdateMeta( input_thread_t *p_input, demux_t *p_demux )
3004 vlc_meta_t *p_meta = vlc_meta_New();
3005 if( unlikely(p_meta == NULL) )
3006 return;
3008 demux_Control( p_demux, DEMUX_GET_META, p_meta );
3010 /* If metadata changed, then the attachments might have changed.
3011 We need to update them in case they contain album art. */
3012 input_attachment_t **attachment;
3013 int i_attachment;
3015 if( !demux_Control( p_demux, DEMUX_GET_ATTACHMENTS,
3016 &attachment, &i_attachment ) )
3018 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
3019 if( input_priv(p_input)->i_attachment > 0 )
3021 int j = 0;
3022 for( int i = 0; i < input_priv(p_input)->i_attachment; i++ )
3024 if( input_priv(p_input)->attachment_demux[i] == p_demux )
3025 vlc_input_attachment_Delete( input_priv(p_input)->attachment[i] );
3026 else
3028 input_priv(p_input)->attachment[j] = input_priv(p_input)->attachment[i];
3029 input_priv(p_input)->attachment_demux[j] = input_priv(p_input)->attachment_demux[i];
3030 j++;
3033 input_priv(p_input)->i_attachment = j;
3035 AppendAttachment( &input_priv(p_input)->i_attachment, &input_priv(p_input)->attachment, &input_priv(p_input)->attachment_demux,
3036 i_attachment, attachment, p_demux );
3037 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
3040 es_out_ControlSetMeta( input_priv(p_input)->p_es_out, p_meta );
3041 vlc_meta_Delete( p_meta );
3044 /*****************************************************************************
3045 * InputGetExtraFiles
3046 * Autodetect extra input list
3047 *****************************************************************************/
3048 static void InputGetExtraFilesPattern( input_thread_t *p_input,
3049 int *pi_list, char ***pppsz_list,
3050 const char *psz_path,
3051 const char *psz_match,
3052 const char *psz_format,
3053 int i_start, int i_stop )
3055 int i_list;
3056 char **ppsz_list;
3057 TAB_INIT( i_list, ppsz_list );
3059 char *psz_base = strdup( psz_path );
3060 if( !psz_base )
3061 goto exit;
3063 /* Remove the extension */
3064 char *psz_end = &psz_base[strlen(psz_base)-strlen(psz_match)];
3065 assert( psz_end >= psz_base);
3066 *psz_end = '\0';
3068 /* Try to list files */
3069 for( int i = i_start; i <= i_stop; i++ )
3071 char *psz_probe;
3072 if( asprintf( &psz_probe, psz_format, psz_base, i ) < 0 )
3073 break;
3075 char *filepath = get_path( psz_probe );
3077 struct stat st;
3078 if( filepath == NULL ||
3079 vlc_stat( filepath, &st ) || !S_ISREG( st.st_mode ) || !st.st_size )
3081 free( filepath );
3082 free( psz_probe );
3083 break;
3086 msg_Dbg( p_input, "Detected extra file `%s'", filepath );
3088 char* psz_uri = vlc_path2uri( filepath, NULL );
3089 if( psz_uri )
3090 TAB_APPEND( i_list, ppsz_list, psz_uri );
3092 free( filepath );
3093 free( psz_probe );
3095 free( psz_base );
3096 exit:
3097 *pi_list = i_list;
3098 *pppsz_list = ppsz_list;
3101 static void InputGetExtraFiles( input_thread_t *p_input,
3102 int *pi_list, char ***pppsz_list,
3103 const char **ppsz_access, const char *psz_path )
3105 static const struct pattern
3107 const char *psz_access_force;
3108 const char *psz_match;
3109 const char *psz_format;
3110 int i_start;
3111 int i_stop;
3112 } patterns[] = {
3113 /* XXX the order is important */
3114 { "concat", ".001", "%s.%.3d", 2, 999 },
3115 { NULL, ".part1.rar","%s.part%.1d.rar", 2, 9 },
3116 { NULL, ".part01.rar","%s.part%.2d.rar", 2, 99, },
3117 { NULL, ".part001.rar", "%s.part%.3d.rar", 2, 999 },
3118 { NULL, ".rar", "%s.r%.2d", 0, 99 },
3121 TAB_INIT( *pi_list, *pppsz_list );
3123 if( ( **ppsz_access && strcmp( *ppsz_access, "file" ) ) || !psz_path )
3124 return;
3126 const size_t i_path = strlen(psz_path);
3128 for( size_t i = 0; i < ARRAY_SIZE( patterns ); ++i )
3130 const struct pattern* pat = &patterns[i];
3131 const size_t i_ext = strlen( pat->psz_match );
3133 if( i_path < i_ext )
3134 continue;
3136 if( !strcmp( &psz_path[i_path-i_ext], pat->psz_match ) )
3138 InputGetExtraFilesPattern( p_input, pi_list, pppsz_list, psz_path,
3139 pat->psz_match, pat->psz_format, pat->i_start, pat->i_stop );
3141 if( *pi_list > 0 && pat->psz_access_force )
3142 *ppsz_access = pat->psz_access_force;
3143 return;
3148 /* */
3149 static void input_ChangeState( input_thread_t *p_input, int i_state )
3151 if( input_priv(p_input)->i_state == i_state )
3152 return;
3154 input_priv(p_input)->i_state = i_state;
3155 if( i_state == ERROR_S )
3156 input_item_SetErrorWhenReading( input_priv(p_input)->p_item, true );
3157 input_SendEventState( p_input, i_state );
3161 /*****************************************************************************
3162 * MRLSplit: parse the access, demux and url part of the
3163 * Media Resource Locator.
3164 *****************************************************************************/
3165 void input_SplitMRL( const char **access, const char **demux,
3166 const char **path, const char **anchor, char *buf )
3168 char *p;
3170 /* Separate <path> from <access>[/<demux>]:// */
3171 p = strstr( buf, "://" );
3172 if( p != NULL )
3174 *p = '\0';
3175 p += 3; /* skips "://" */
3176 *path = p;
3178 /* Remove HTML anchor if present (not supported).
3179 * The hash symbol itself should be URI-encoded. */
3180 p = strchr( p, '#' );
3181 if( p != NULL )
3183 *(p++) = '\0';
3184 *anchor = p;
3186 else
3187 *anchor = "";
3189 else
3191 #ifndef NDEBUG
3192 fprintf( stderr, "%s(\"%s\") probably not a valid URI!\n", __func__,
3193 buf );
3194 #endif
3195 /* Note: this is a valid non const pointer to "": */
3196 *path = buf + strlen( buf );
3199 /* Separate access from demux */
3200 p = strchr( buf, '/' );
3201 if( p != NULL )
3203 *(p++) = '\0';
3204 if( p[0] == '$' )
3205 p++;
3206 *demux = p;
3208 else
3209 *demux = "";
3211 /* We really don't want module name substitution here! */
3212 p = buf;
3213 if( p[0] == '$' )
3214 p++;
3215 *access = p;
3218 static const char *MRLSeekPoint( const char *str, int *title, int *chapter )
3220 char *end;
3221 unsigned long u;
3223 /* Look for the title */
3224 u = strtoul( str, &end, 0 );
3225 *title = (str == end || u > (unsigned long)INT_MAX) ? -1 : (int)u;
3226 str = end;
3228 /* Look for the chapter */
3229 if( *str == ':' )
3231 str++;
3232 u = strtoul( str, &end, 0 );
3233 *chapter = (str == end || u > (unsigned long)INT_MAX) ? -1 : (int)u;
3234 str = end;
3236 else
3237 *chapter = -1;
3239 return str;
3243 /*****************************************************************************
3244 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3246 * Syntax:
3247 * [url][@[title_start][:chapter_start][-[title_end][:chapter_end]]]
3248 *****************************************************************************/
3249 static void MRLSections( const char *p,
3250 int *pi_title_start, int *pi_title_end,
3251 int *pi_chapter_start, int *pi_chapter_end )
3253 *pi_title_start = *pi_title_end = *pi_chapter_start = *pi_chapter_end = -1;
3255 int title_start, chapter_start, title_end, chapter_end;
3257 if( !p )
3258 return;
3260 if( *p != '-' )
3261 p = MRLSeekPoint( p, &title_start, &chapter_start );
3262 else
3263 title_start = chapter_start = -1;
3265 if( *p == '-' )
3266 p = MRLSeekPoint( p + 1, &title_end, &chapter_end );
3267 else
3268 title_end = chapter_end = -1;
3270 if( *p ) /* syntax error */
3271 return;
3273 *pi_title_start = title_start;
3274 *pi_title_end = title_end;
3275 *pi_chapter_start = chapter_start;
3276 *pi_chapter_end = chapter_end;
3279 static int input_SlaveSourceAdd( input_thread_t *p_input,
3280 enum slave_type i_type, const char *psz_uri,
3281 unsigned i_flags )
3283 vlc_value_t count;
3284 const char *psz_es;
3285 const char *psz_forced_demux;
3286 const bool b_can_fail = i_flags & SLAVE_ADD_CANFAIL;
3287 const bool b_forced = i_flags & SLAVE_ADD_FORCED;
3288 const bool b_set_time = i_flags & SLAVE_ADD_SET_TIME;
3290 switch( i_type )
3292 case SLAVE_TYPE_SPU:
3293 psz_es = "spu-es";
3294 psz_forced_demux = "subtitle";
3295 break;
3296 case SLAVE_TYPE_AUDIO:
3297 psz_es = "audio-es";
3298 psz_forced_demux = NULL;
3299 break;
3300 default:
3301 vlc_assert_unreachable();
3304 if( b_forced )
3305 var_Change( p_input, psz_es, VLC_VAR_CHOICESCOUNT, &count, NULL );
3307 msg_Dbg( p_input, "loading %s slave: %s (forced: %d)", psz_es, psz_uri,
3308 b_forced );
3310 input_source_t *p_source = InputSourceNew( p_input, psz_uri,
3311 psz_forced_demux,
3312 b_can_fail || psz_forced_demux );
3314 if( psz_forced_demux && p_source == NULL )
3315 p_source = InputSourceNew( p_input, psz_uri, NULL, b_can_fail );
3317 if( p_source == NULL )
3319 msg_Warn( p_input, "failed to add %s as slave", psz_uri );
3320 return VLC_EGENERIC;
3323 if( i_type == SLAVE_TYPE_AUDIO )
3325 if( b_set_time )
3327 int64_t i_time;
3329 /* Set position */
3330 if( demux_Control( input_priv(p_input)->master->p_demux,
3331 DEMUX_GET_TIME, &i_time ) )
3333 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
3334 InputSourceDestroy( p_source );
3335 return VLC_EGENERIC;
3338 if( demux_Control( p_source->p_demux,
3339 DEMUX_SET_TIME, i_time, true ) )
3341 msg_Err( p_input, "seek failed for new slave" );
3342 InputSourceDestroy( p_source );
3343 return VLC_EGENERIC;
3347 /* Get meta (access and demux) */
3348 InputUpdateMeta( p_input, p_source->p_demux );
3351 TAB_APPEND( input_priv(p_input)->i_slave, input_priv(p_input)->slave, p_source );
3353 if( !b_forced )
3354 return VLC_SUCCESS;
3356 /* Select the ES */
3357 vlc_value_t list;
3359 if( var_Change( p_input, psz_es, VLC_VAR_GETCHOICES, &list, NULL ) )
3360 return VLC_SUCCESS;
3362 if( count.i_int == 0 )
3363 count.i_int++;
3364 /* if it was first one, there is disable too */
3366 if( count.i_int < list.p_list->i_count )
3368 const int i_id = list.p_list->p_values[count.i_int].i_int;
3370 es_out_Control( input_priv(p_input)->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3371 es_out_Control( input_priv(p_input)->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3373 var_FreeList( &list, NULL );
3375 return VLC_SUCCESS;
3378 static char *input_SubtitleFile2Uri( input_thread_t *p_input,
3379 const char *psz_subtitle )
3381 /* if we are provided a subtitle.sub file,
3382 * see if we don't have a subtitle.idx and use it instead */
3383 char *psz_idxpath = NULL;
3384 char *psz_extension = strrchr( psz_subtitle, '.');
3385 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3387 psz_idxpath = strdup( psz_subtitle );
3388 if( psz_idxpath )
3390 struct stat st;
3392 psz_extension = psz_extension - psz_subtitle + psz_idxpath;
3393 strcpy( psz_extension, ".idx" );
3395 if( !vlc_stat( psz_idxpath, &st ) && S_ISREG( st.st_mode ) )
3397 msg_Dbg( p_input, "using %s as subtitle file instead of %s",
3398 psz_idxpath, psz_subtitle );
3399 psz_subtitle = psz_idxpath;
3404 char *psz_uri = vlc_path2uri( psz_subtitle, NULL );
3405 free( psz_idxpath );
3406 return psz_uri;
3409 /*****************************************************************************
3410 * Statistics
3411 *****************************************************************************/
3412 void input_UpdateStatistic( input_thread_t *p_input,
3413 input_statistic_t i_type, int i_delta )
3415 assert( input_priv(p_input)->i_state != INIT_S );
3417 vlc_mutex_lock( &input_priv(p_input)->counters.counters_lock);
3418 switch( i_type )
3420 #define I(c) stats_Update( input_priv(p_input)->counters.c, i_delta, NULL )
3421 case INPUT_STATISTIC_DECODED_VIDEO:
3422 I(p_decoded_video);
3423 break;
3424 case INPUT_STATISTIC_DECODED_AUDIO:
3425 I(p_decoded_audio);
3426 break;
3427 case INPUT_STATISTIC_DECODED_SUBTITLE:
3428 I(p_decoded_sub);
3429 break;
3430 case INPUT_STATISTIC_SENT_PACKET:
3431 I(p_sout_sent_packets);
3432 break;
3433 #undef I
3434 case INPUT_STATISTIC_SENT_BYTE:
3436 uint64_t bytes;
3438 stats_Update( input_priv(p_input)->counters.p_sout_sent_bytes, i_delta, &bytes );
3439 stats_Update( input_priv(p_input)->counters.p_sout_send_bitrate, bytes, NULL );
3440 break;
3442 default:
3443 msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3444 break;
3446 vlc_mutex_unlock( &input_priv(p_input)->counters.counters_lock);
3449 /**/
3450 /* TODO FIXME nearly the same logic that snapshot code */
3451 char *input_CreateFilename(input_thread_t *input, const char *dir,
3452 const char *filenamefmt, const char *ext)
3454 char *path;
3455 char *filename = str_format(input, filenamefmt);
3456 if (unlikely(filename == NULL))
3457 return NULL;
3459 filename_sanitize(filename);
3461 if (((ext != NULL)
3462 ? asprintf(&path, "%s"DIR_SEP"%s.%s", dir, filename, ext)
3463 : asprintf(&path, "%s"DIR_SEP"%s", dir, filename)) < 0)
3464 path = NULL;
3466 free(filename);
3467 return path;