demux: ts: fix potential null deref (cid #1412981)
[vlc.git] / src / input / input.c
blob55375012480d4f976369401d55d8f36329fdedbb
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 /* The renderer is passed after its refcount was incremented.
330 * The input thread is now responsible for releasing it */
331 priv->p_renderer = p_renderer;
333 priv->viewpoint_changed = false;
334 /* Fetch the viewpoint from the mediaplayer or the playlist if any */
335 vlc_viewpoint_t *p_viewpoint = var_InheritAddress( p_input, "viewpoint" );
336 if (p_viewpoint != NULL)
337 priv->viewpoint = *p_viewpoint;
338 else
339 vlc_viewpoint_init( &priv->viewpoint );
341 input_item_Hold( p_item ); /* Released in Destructor() */
342 priv->p_item = p_item;
344 /* Init Input fields */
345 priv->master = NULL;
346 vlc_mutex_lock( &p_item->lock );
348 if( !p_item->p_stats )
349 p_item->p_stats = stats_NewInputStats( p_input );
351 /* setup the preparse depth of the item
352 * if we are preparsing, use the i_preparse_depth of the parent item */
353 if( !priv->b_preparsing )
355 char *psz_rec = var_InheritString( p_parent, "recursive" );
357 if( psz_rec != NULL )
359 if ( !strcasecmp( psz_rec, "none" ) )
360 p_item->i_preparse_depth = 0;
361 else if ( !strcasecmp( psz_rec, "collapse" ) )
362 p_item->i_preparse_depth = 1;
363 else
364 p_item->i_preparse_depth = -1; /* default is expand */
365 free (psz_rec);
366 } else
367 p_item->i_preparse_depth = -1;
369 else
370 p_input->obj.flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
372 /* Make sure the interaction option is honored */
373 if( !var_InheritBool( p_input, "interact" ) )
374 p_input->obj.flags |= OBJECT_FLAGS_NOINTERACT;
375 else if( p_item->b_preparse_interact )
377 /* If true, this item was asked explicitly to interact with the user
378 * (via libvlc_MetadataRequest). Sub items created from this input won't
379 * have this flag and won't interact with the user */
380 p_input->obj.flags &= ~OBJECT_FLAGS_NOINTERACT;
383 vlc_mutex_unlock( &p_item->lock );
385 /* No slave */
386 priv->i_slave = 0;
387 priv->slave = NULL;
389 /* */
390 if( p_resource )
392 priv->p_resource_private = NULL;
393 priv->p_resource = input_resource_Hold( p_resource );
395 else
397 priv->p_resource_private = input_resource_New( VLC_OBJECT( p_input ) );
398 priv->p_resource = input_resource_Hold( priv->p_resource_private );
400 input_resource_SetInput( priv->p_resource, p_input );
402 /* Init control buffer */
403 vlc_mutex_init( &priv->lock_control );
404 vlc_cond_init( &priv->wait_control );
405 priv->i_control = 0;
406 vlc_interrupt_init(&priv->interrupt);
408 /* Create Object Variables for private use only */
409 input_ConfigVarInit( p_input );
411 /* Create Objects variables for public Get and Set */
412 input_ControlVarInit( p_input );
414 /* */
415 if( !priv->b_preparsing )
417 char *psz_bookmarks = var_GetNonEmptyString( p_input, "bookmarks" );
418 if( psz_bookmarks )
420 /* FIXME: have a common cfg parsing routine used by sout and others */
421 char *psz_parser, *psz_start, *psz_end;
422 psz_parser = psz_bookmarks;
423 while( (psz_start = strchr( psz_parser, '{' ) ) )
425 seekpoint_t *p_seekpoint;
426 char backup;
427 psz_start++;
428 psz_end = strchr( psz_start, '}' );
429 if( !psz_end ) break;
430 psz_parser = psz_end + 1;
431 backup = *psz_parser;
432 *psz_parser = 0;
433 *psz_end = ',';
435 p_seekpoint = vlc_seekpoint_New();
437 if( unlikely( p_seekpoint == NULL ) )
438 break;
440 while( (psz_end = strchr( psz_start, ',' ) ) )
442 *psz_end = 0;
443 if( !strncmp( psz_start, "name=", 5 ) )
445 free( p_seekpoint->psz_name );
447 p_seekpoint->psz_name = strdup(psz_start + 5);
449 else if( !strncmp( psz_start, "time=", 5 ) )
451 p_seekpoint->i_time_offset = atof(psz_start + 5) *
452 CLOCK_FREQ;
454 psz_start = psz_end + 1;
456 msg_Dbg( p_input, "adding bookmark: %s, time=%"PRId64,
457 p_seekpoint->psz_name,
458 p_seekpoint->i_time_offset );
459 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
460 vlc_seekpoint_Delete( p_seekpoint );
461 *psz_parser = backup;
463 free( psz_bookmarks );
467 /* Remove 'Now playing' info as it is probably outdated */
468 input_item_SetNowPlaying( p_item, NULL );
469 input_item_SetESNowPlaying( p_item, NULL );
470 input_SendEventMeta( p_input );
472 /* */
473 memset( &priv->counters, 0, sizeof( priv->counters ) );
474 vlc_mutex_init( &priv->counters.counters_lock );
476 priv->p_es_out_display = input_EsOutNew( p_input, priv->i_rate );
477 priv->p_es_out = NULL;
479 /* Set the destructor when we are sure we are initialized */
480 vlc_object_set_destructor( p_input, input_Destructor );
482 return p_input;
485 /*****************************************************************************
486 * Run: main thread loop
487 * This is the "normal" thread that spawns the input processing chain,
488 * reads the stream, cleans up and waits
489 *****************************************************************************/
490 static void *Run( void *data )
492 input_thread_private_t *priv = data;
493 input_thread_t *p_input = &priv->input;
495 vlc_interrupt_set(&priv->interrupt);
497 if( !Init( p_input ) )
499 if( priv->b_can_pace_control && priv->b_out_pace_control )
501 /* We don't want a high input priority here or we'll
502 * end-up sucking up all the CPU time */
503 vlc_set_priority( priv->thread, VLC_THREAD_PRIORITY_LOW );
506 MainLoop( p_input, true ); /* FIXME it can be wrong (like with VLM) */
508 /* Clean up */
509 End( p_input );
512 input_SendEventDead( p_input );
513 return NULL;
516 static void *Preparse( void *data )
518 input_thread_private_t *priv = data;
519 input_thread_t *p_input = &priv->input;
521 vlc_interrupt_set(&priv->interrupt);
523 if( !Init( p_input ) )
524 { /* if the demux is a playlist, call Mainloop that will call
525 * demux_Demux in order to fetch sub items */
526 bool b_is_playlist = false;
528 if ( input_item_ShouldPreparseSubItems( priv->p_item )
529 && demux_Control( priv->master->p_demux, DEMUX_IS_PLAYLIST,
530 &b_is_playlist ) )
531 b_is_playlist = false;
532 if( b_is_playlist )
533 MainLoop( p_input, false );
534 End( p_input );
537 input_SendEventDead( p_input );
538 return NULL;
541 bool input_Stopped( input_thread_t *input )
543 input_thread_private_t *sys = input_priv(input);
544 bool ret;
546 vlc_mutex_lock( &sys->lock_control );
547 ret = sys->is_stopped;
548 vlc_mutex_unlock( &sys->lock_control );
549 return ret;
552 /*****************************************************************************
553 * Main loop: Fill buffers from access, and demux
554 *****************************************************************************/
557 * MainLoopDemux
558 * It asks the demuxer to demux some data
560 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed )
562 int i_ret;
563 demux_t *p_demux = input_priv(p_input)->master->p_demux;
565 *pb_changed = false;
567 if( input_priv(p_input)->i_stop > 0 && input_priv(p_input)->i_time >= input_priv(p_input)->i_stop )
568 i_ret = VLC_DEMUXER_EOF;
569 else
570 i_ret = demux_Demux( p_demux );
572 i_ret = i_ret > 0 ? VLC_DEMUXER_SUCCESS : ( i_ret < 0 ? VLC_DEMUXER_EGENERIC : VLC_DEMUXER_EOF);
574 if( i_ret == VLC_DEMUXER_SUCCESS )
576 if( demux_TestAndClearFlags( p_demux, INPUT_UPDATE_TITLE_LIST ) )
577 UpdateTitleListfromDemux( p_input );
579 if( input_priv(p_input)->master->b_title_demux )
581 i_ret = UpdateTitleSeekpointFromDemux( p_input );
582 *pb_changed = true;
585 UpdateGenericFromDemux( p_input );
588 if( i_ret == VLC_DEMUXER_EOF )
590 msg_Dbg( p_input, "EOF reached" );
591 input_priv(p_input)->master->b_eof = true;
592 es_out_Eos(input_priv(p_input)->p_es_out);
594 else if( i_ret == VLC_DEMUXER_EGENERIC )
596 input_ChangeState( p_input, ERROR_S );
598 else if( input_priv(p_input)->i_slave > 0 )
599 SlaveDemux( p_input );
602 static int MainLoopTryRepeat( input_thread_t *p_input )
604 int i_repeat = var_GetInteger( p_input, "input-repeat" );
605 if( i_repeat <= 0 )
606 return VLC_EGENERIC;
608 vlc_value_t val;
610 msg_Dbg( p_input, "repeating the same input (%d)", i_repeat );
611 if( i_repeat > 0 )
613 i_repeat--;
614 var_SetInteger( p_input, "input-repeat", i_repeat );
617 /* Seek to start title/seekpoint */
618 val.i_int = input_priv(p_input)->master->i_title_start -
619 input_priv(p_input)->master->i_title_offset;
620 if( val.i_int < 0 || val.i_int >= input_priv(p_input)->master->i_title )
621 val.i_int = 0;
622 input_ControlPush( p_input,
623 INPUT_CONTROL_SET_TITLE, &val );
625 val.i_int = input_priv(p_input)->master->i_seekpoint_start -
626 input_priv(p_input)->master->i_seekpoint_offset;
627 if( val.i_int > 0 /* TODO: check upper boundary */ )
628 input_ControlPush( p_input,
629 INPUT_CONTROL_SET_SEEKPOINT, &val );
631 /* Seek to start position */
632 if( input_priv(p_input)->i_start > 0 )
634 val.i_int = input_priv(p_input)->i_start;
635 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &val );
637 else
639 val.f_float = 0.f;
640 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION, &val );
643 return VLC_SUCCESS;
647 * Update timing infos and statistics.
649 static void MainLoopStatistics( input_thread_t *p_input )
651 double f_position = 0.0;
652 mtime_t i_time = 0;
653 mtime_t i_length = 0;
655 /* update input status variables */
656 if( demux_Control( input_priv(p_input)->master->p_demux,
657 DEMUX_GET_POSITION, &f_position ) )
658 f_position = 0.0;
660 if( demux_Control( input_priv(p_input)->master->p_demux,
661 DEMUX_GET_TIME, &i_time ) )
662 i_time = 0;
663 input_priv(p_input)->i_time = i_time;
665 if( demux_Control( input_priv(p_input)->master->p_demux,
666 DEMUX_GET_LENGTH, &i_length ) )
667 i_length = 0;
669 es_out_SetTimes( input_priv(p_input)->p_es_out, f_position, i_time, i_length );
671 /* update current bookmark */
672 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
673 input_priv(p_input)->bookmark.i_time_offset = i_time;
674 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
676 stats_ComputeInputStats( p_input, input_priv(p_input)->p_item->p_stats );
677 input_SendEventStatistics( p_input );
681 * MainLoop
682 * The main input loop.
684 static void MainLoop( input_thread_t *p_input, bool b_interactive )
686 mtime_t i_intf_update = 0;
687 mtime_t i_last_seek_mdate = 0;
689 if( b_interactive && var_InheritBool( p_input, "start-paused" ) )
690 ControlPause( p_input, mdate() );
692 bool b_pause_after_eof = b_interactive &&
693 var_InheritBool( p_input, "play-and-pause" );
694 bool b_paused_at_eof = false;
696 demux_t *p_demux = input_priv(p_input)->master->p_demux;
697 const bool b_can_demux = p_demux->pf_demux != NULL;
699 while( !input_Stopped( p_input ) && input_priv(p_input)->i_state != ERROR_S )
701 mtime_t i_wakeup = -1;
702 bool b_paused = input_priv(p_input)->i_state == PAUSE_S;
703 /* FIXME if input_priv(p_input)->i_state == PAUSE_S the access/access_demux
704 * is paused -> this may cause problem with some of them
705 * The same problem can be seen when seeking while paused */
706 if( b_paused )
707 b_paused = !es_out_GetBuffering( input_priv(p_input)->p_es_out )
708 || input_priv(p_input)->master->b_eof;
710 if( !b_paused )
712 if( !input_priv(p_input)->master->b_eof )
714 bool b_force_update = false;
716 MainLoopDemux( p_input, &b_force_update );
718 if( b_can_demux )
719 i_wakeup = es_out_GetWakeup( input_priv(p_input)->p_es_out );
720 if( b_force_update )
721 i_intf_update = 0;
723 b_paused_at_eof = false;
725 else if( !es_out_GetEmpty( input_priv(p_input)->p_es_out ) )
727 msg_Dbg( p_input, "waiting decoder fifos to empty" );
728 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
730 /* Pause after eof only if the input is pausable.
731 * This way we won't trigger timeshifting for nothing */
732 else if( b_pause_after_eof && input_priv(p_input)->b_can_pause )
734 if( b_paused_at_eof )
735 break;
737 vlc_value_t val = { .i_int = PAUSE_S };
739 msg_Dbg( p_input, "pausing at EOF (pause after each)");
740 Control( p_input, INPUT_CONTROL_SET_STATE, val );
742 b_paused = true;
743 b_paused_at_eof = true;
745 else
747 if( MainLoopTryRepeat( p_input ) )
748 break;
751 /* Update interface and statistics */
752 mtime_t now = mdate();
753 if( now >= i_intf_update )
755 MainLoopStatistics( p_input );
756 i_intf_update = now + INT64_C(250000);
760 /* Handle control */
761 for( ;; )
763 mtime_t i_deadline = i_wakeup;
765 /* Postpone seeking until ES buffering is complete or at most
766 * 125 ms. */
767 bool b_postpone = es_out_GetBuffering( input_priv(p_input)->p_es_out )
768 && !input_priv(p_input)->master->b_eof;
769 if( b_postpone )
771 mtime_t now = mdate();
773 /* Recheck ES buffer level every 20 ms when seeking */
774 if( now < i_last_seek_mdate + INT64_C(125000)
775 && (i_deadline < 0 || i_deadline > now + INT64_C(20000)) )
776 i_deadline = now + INT64_C(20000);
777 else
778 b_postpone = false;
781 int i_type;
782 vlc_value_t val;
784 if( ControlPop( p_input, &i_type, &val, i_deadline, b_postpone ) )
786 if( b_postpone )
787 continue;
788 break; /* Wake-up time reached */
791 #ifndef NDEBUG
792 msg_Dbg( p_input, "control type=%d", i_type );
793 #endif
794 if( Control( p_input, i_type, val ) )
796 if( ControlIsSeekRequest( i_type ) )
797 i_last_seek_mdate = mdate();
798 i_intf_update = 0;
801 /* Update the wakeup time */
802 if( i_wakeup != 0 )
803 i_wakeup = es_out_GetWakeup( input_priv(p_input)->p_es_out );
808 static void InitStatistics( input_thread_t *p_input )
810 input_thread_private_t *priv = input_priv(p_input);
812 if( priv->b_preparsing ) return;
814 /* Prepare statistics */
815 #define INIT_COUNTER( c, compute ) free( priv->counters.p_##c ); \
816 priv->counters.p_##c = \
817 stats_CounterCreate( STATS_##compute);
818 if( libvlc_stats( p_input ) )
820 INIT_COUNTER( read_bytes, COUNTER );
821 INIT_COUNTER( read_packets, COUNTER );
822 INIT_COUNTER( demux_read, COUNTER );
823 INIT_COUNTER( input_bitrate, DERIVATIVE );
824 INIT_COUNTER( demux_bitrate, DERIVATIVE );
825 INIT_COUNTER( demux_corrupted, COUNTER );
826 INIT_COUNTER( demux_discontinuity, COUNTER );
827 INIT_COUNTER( played_abuffers, COUNTER );
828 INIT_COUNTER( lost_abuffers, COUNTER );
829 INIT_COUNTER( displayed_pictures, COUNTER );
830 INIT_COUNTER( lost_pictures, COUNTER );
831 INIT_COUNTER( decoded_audio, COUNTER );
832 INIT_COUNTER( decoded_video, COUNTER );
833 INIT_COUNTER( decoded_sub, COUNTER );
834 priv->counters.p_sout_send_bitrate = NULL;
835 priv->counters.p_sout_sent_packets = NULL;
836 priv->counters.p_sout_sent_bytes = NULL;
840 #ifdef ENABLE_SOUT
841 static int InitSout( input_thread_t * p_input )
843 input_thread_private_t *priv = input_priv(p_input);
845 if( priv->b_preparsing )
846 return VLC_SUCCESS;
848 /* Find a usable sout and attach it to p_input */
849 char *psz = NULL;
850 if( priv->p_renderer )
852 const char *psz_renderer_sout = vlc_renderer_item_sout( priv->p_renderer );
853 if( asprintf( &psz, "#%s", psz_renderer_sout ) < 0 )
854 return VLC_ENOMEM;
856 if( !psz )
857 psz = var_GetNonEmptyString( p_input, "sout" );
858 if( psz && strncasecmp( priv->p_item->psz_uri, "vlc:", 4 ) )
860 priv->p_sout = input_resource_RequestSout( priv->p_resource, NULL, psz );
861 if( priv->p_sout == NULL )
863 input_ChangeState( p_input, ERROR_S );
864 msg_Err( p_input, "cannot start stream output instance, " \
865 "aborting" );
866 free( psz );
867 return VLC_EGENERIC;
869 if( libvlc_stats( p_input ) )
871 INIT_COUNTER( sout_sent_packets, COUNTER );
872 INIT_COUNTER( sout_sent_bytes, COUNTER );
873 INIT_COUNTER( sout_send_bitrate, DERIVATIVE );
876 else
878 input_resource_RequestSout( priv->p_resource, NULL, NULL );
880 free( psz );
882 return VLC_SUCCESS;
884 #endif
886 static void InitTitle( input_thread_t * p_input )
888 input_thread_private_t *priv = input_priv(p_input);
889 input_source_t *p_master = priv->master;
891 if( priv->b_preparsing )
892 return;
894 vlc_mutex_lock( &priv->p_item->lock );
895 /* Create global title (from master) */
896 priv->i_title = p_master->i_title;
897 priv->title = p_master->title;
898 priv->i_title_offset = p_master->i_title_offset;
899 priv->i_seekpoint_offset = p_master->i_seekpoint_offset;
900 if( priv->i_title > 0 )
902 /* Setup variables */
903 input_ControlVarNavigation( p_input );
904 input_SendEventTitle( p_input, 0 );
907 /* Global flag */
908 priv->b_can_pace_control = p_master->b_can_pace_control;
909 priv->b_can_pause = p_master->b_can_pause;
910 priv->b_can_rate_control = p_master->b_can_rate_control;
911 vlc_mutex_unlock( &priv->p_item->lock );
914 static void StartTitle( input_thread_t * p_input )
916 input_thread_private_t *priv = input_priv(p_input);
917 vlc_value_t val;
919 /* Start title/chapter */
920 val.i_int = priv->master->i_title_start - priv->master->i_title_offset;
921 if( val.i_int > 0 && val.i_int < priv->master->i_title )
922 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
924 val.i_int = priv->master->i_seekpoint_start -
925 priv->master->i_seekpoint_offset;
926 if( val.i_int > 0 /* TODO: check upper boundary */ )
927 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
929 /* Start/stop/run time */
930 priv->i_start = llroundf(1000000.f
931 * var_GetFloat( p_input, "start-time" ));
932 priv->i_stop = llroundf(1000000.f
933 * var_GetFloat( p_input, "stop-time" ));
934 if( priv->i_stop <= 0 )
936 priv->i_stop = llroundf(1000000.f
937 * var_GetFloat( p_input, "run-time" ));
938 if( priv->i_stop < 0 )
940 msg_Warn( p_input, "invalid run-time ignored" );
941 priv->i_stop = 0;
943 else
944 priv->i_stop += priv->i_start;
947 if( priv->i_start > 0 )
949 vlc_value_t s;
951 msg_Dbg( p_input, "starting at time: %"PRId64"s",
952 priv->i_start / CLOCK_FREQ );
954 s.i_int = priv->i_start;
955 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
957 if( priv->i_stop > 0 && priv->i_stop <= priv->i_start )
959 msg_Warn( p_input, "invalid stop-time ignored" );
960 priv->i_stop = 0;
962 priv->b_fast_seek = var_GetBool( p_input, "input-fast-seek" );
965 static int SlaveCompare(const void *a, const void *b)
967 const input_item_slave_t *p_slave0 = *((const input_item_slave_t **) a);
968 const input_item_slave_t *p_slave1 = *((const input_item_slave_t **) b);
970 if( p_slave0 == NULL || p_slave1 == NULL )
972 /* Put NULL (or rejected) subs at the end */
973 return p_slave0 == NULL ? 1 : p_slave1 == NULL ? -1 : 0;
976 if( p_slave0->i_priority > p_slave1->i_priority )
977 return -1;
979 if( p_slave0->i_priority < p_slave1->i_priority )
980 return 1;
982 return 0;
985 static bool SlaveExists( input_item_slave_t **pp_slaves, int i_slaves,
986 const char *psz_uri)
988 for( int i = 0; i < i_slaves; i++ )
990 if( pp_slaves[i] != NULL
991 && !strcmp( pp_slaves[i]->psz_uri, psz_uri ) )
992 return true;
994 return false;
997 static void SetSubtitlesOptions( input_thread_t *p_input )
999 /* Get fps and set it if not already set */
1000 const float f_fps = input_priv(p_input)->master->f_fps;
1001 if( f_fps > 1.f )
1003 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
1004 var_SetFloat( p_input, "sub-original-fps", f_fps );
1006 float f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1007 if( f_requested_fps != f_fps )
1009 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1010 VLC_VAR_DOINHERIT );
1011 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1015 const int i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1016 if( i_delay != 0 )
1017 var_SetInteger( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1020 static void GetVarSlaves( input_thread_t *p_input,
1021 input_item_slave_t ***ppp_slaves, int *p_slaves )
1023 char *psz = var_GetNonEmptyString( p_input, "input-slave" );
1024 if( !psz )
1025 return;
1027 input_item_slave_t **pp_slaves = *ppp_slaves;
1028 int i_slaves = *p_slaves;
1030 char *psz_org = psz;
1031 while( psz && *psz )
1033 while( *psz == ' ' || *psz == '#' )
1034 psz++;
1036 char *psz_delim = strchr( psz, '#' );
1037 if( psz_delim )
1038 *psz_delim++ = '\0';
1040 if( *psz == 0 )
1041 break;
1043 char *uri = strstr(psz, "://")
1044 ? strdup( psz ) : vlc_path2uri( psz, NULL );
1045 psz = psz_delim;
1046 if( uri == NULL )
1047 continue;
1049 input_item_slave_t *p_slave =
1050 input_item_slave_New( uri, SLAVE_TYPE_AUDIO, SLAVE_PRIORITY_USER );
1051 free( uri );
1053 if( unlikely( p_slave == NULL ) )
1054 break;
1055 TAB_APPEND(i_slaves, pp_slaves, p_slave);
1057 free( psz_org );
1059 *ppp_slaves = pp_slaves; /* in case of realloc */
1060 *p_slaves = i_slaves;
1063 static void LoadSlaves( input_thread_t *p_input )
1065 input_item_slave_t **pp_slaves;
1066 int i_slaves;
1067 TAB_INIT( i_slaves, pp_slaves );
1069 /* Look for and add slaves */
1071 char *psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1072 if( psz_subtitle != NULL )
1074 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1075 char *psz_uri = input_SubtitleFile2Uri( p_input, psz_subtitle );
1076 free( psz_subtitle );
1077 psz_subtitle = NULL;
1078 if( psz_uri != NULL )
1080 input_item_slave_t *p_slave =
1081 input_item_slave_New( psz_uri, SLAVE_TYPE_SPU,
1082 SLAVE_PRIORITY_USER );
1083 free( psz_uri );
1084 if( p_slave )
1086 TAB_APPEND(i_slaves, pp_slaves, p_slave);
1087 psz_subtitle = p_slave->psz_uri;
1092 if( var_GetBool( p_input, "sub-autodetect-file" ) )
1094 /* Add local subtitles */
1095 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1097 if( subtitles_Detect( p_input, psz_autopath, input_priv(p_input)->p_item->psz_uri,
1098 &pp_slaves, &i_slaves ) == VLC_SUCCESS )
1100 /* check that we did not add the subtitle through sub-file */
1101 if( psz_subtitle != NULL )
1103 for( int i = 1; i < i_slaves; i++ )
1105 input_item_slave_t *p_curr = pp_slaves[i];
1106 if( p_curr != NULL
1107 && !strcmp( psz_subtitle, p_curr->psz_uri ) )
1109 /* reject current sub */
1110 input_item_slave_Delete( p_curr );
1111 pp_slaves[i] = NULL;
1116 free( psz_autopath );
1119 /* Add slaves found by the directory demuxer or via libvlc */
1120 input_item_t *p_item = input_priv(p_input)->p_item;
1121 vlc_mutex_lock( &p_item->lock );
1123 /* Move item slaves to local pp_slaves */
1124 for( int i = 0; i < p_item->i_slaves; i++ )
1126 input_item_slave_t *p_slave = p_item->pp_slaves[i];
1127 if( !SlaveExists( pp_slaves, i_slaves, p_slave->psz_uri ) )
1128 TAB_APPEND(i_slaves, pp_slaves, p_slave);
1129 else
1130 input_item_slave_Delete( p_slave );
1132 /* Slaves that are successfully loaded will be added back to the item */
1133 TAB_CLEAN( p_item->i_slaves, p_item->pp_slaves );
1134 vlc_mutex_unlock( &p_item->lock );
1136 /* Add slaves from the "input-slave" option */
1137 GetVarSlaves( p_input, &pp_slaves, &i_slaves );
1139 if( i_slaves > 0 )
1140 qsort( pp_slaves, i_slaves, sizeof (input_item_slave_t*),
1141 SlaveCompare );
1143 /* add all detected slaves */
1144 bool p_forced[2] = { false, false };
1145 static_assert( SLAVE_TYPE_AUDIO <= 1 && SLAVE_TYPE_SPU <= 1,
1146 "slave type size mismatch");
1147 for( int i = 0; i < i_slaves && pp_slaves[i] != NULL; i++ )
1149 input_item_slave_t *p_slave = pp_slaves[i];
1150 /* Slaves added via options should not fail */
1151 unsigned i_flags = p_slave->i_priority != SLAVE_PRIORITY_USER
1152 ? SLAVE_ADD_CANFAIL : SLAVE_ADD_NOFLAG;
1153 bool b_forced = false;
1155 /* Force the first subtitle with the highest priority or with the
1156 * forced flag */
1157 if( !p_forced[p_slave->i_type]
1158 && ( p_slave->b_forced || p_slave->i_priority == SLAVE_PRIORITY_USER ) )
1160 i_flags |= SLAVE_ADD_FORCED;
1161 b_forced = true;
1164 if( input_SlaveSourceAdd( p_input, p_slave->i_type, p_slave->psz_uri,
1165 i_flags ) == VLC_SUCCESS )
1167 input_item_AddSlave( input_priv(p_input)->p_item, p_slave );
1168 if( b_forced )
1169 p_forced[p_slave->i_type] = true;
1171 else
1172 input_item_slave_Delete( p_slave );
1174 TAB_CLEAN( i_slaves, pp_slaves );
1176 /* Load subtitles from attachments */
1177 int i_attachment = 0;
1178 input_attachment_t **pp_attachment = NULL;
1180 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
1181 for( int i = 0; i < input_priv(p_input)->i_attachment; i++ )
1183 const input_attachment_t *a = input_priv(p_input)->attachment[i];
1184 if( !strcmp( a->psz_mime, "application/x-srt" ) )
1185 TAB_APPEND( i_attachment, pp_attachment,
1186 vlc_input_attachment_New( a->psz_name, NULL,
1187 a->psz_description, NULL, 0 ) );
1189 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
1191 if( i_attachment > 0 )
1192 var_Create( p_input, "sub-description", VLC_VAR_STRING );
1193 for( int i = 0; i < i_attachment; i++ )
1195 input_attachment_t *a = pp_attachment[i];
1196 if( !a )
1197 continue;
1198 char *psz_mrl;
1199 if( a->psz_name[0] &&
1200 asprintf( &psz_mrl, "attachment://%s", a->psz_name ) >= 0 )
1202 var_SetString( p_input, "sub-description", a->psz_description ? a->psz_description : "");
1204 /* Force the first subtitle from attachment if there is no
1205 * subtitles already forced */
1206 if( input_SlaveSourceAdd( p_input, SLAVE_TYPE_SPU, psz_mrl,
1207 p_forced[ SLAVE_TYPE_SPU ] ?
1208 SLAVE_ADD_NOFLAG : SLAVE_ADD_FORCED ) == VLC_SUCCESS )
1209 p_forced[ SLAVE_TYPE_SPU ] = true;
1211 free( psz_mrl );
1212 /* Don't update item slaves for attachements */
1214 vlc_input_attachment_Delete( a );
1216 free( pp_attachment );
1217 if( i_attachment > 0 )
1218 var_Destroy( p_input, "sub-description" );
1221 static void UpdatePtsDelay( input_thread_t *p_input )
1223 input_thread_private_t *p_sys = input_priv(p_input);
1225 /* Get max pts delay from input source */
1226 mtime_t i_pts_delay = p_sys->master->i_pts_delay;
1227 for( int i = 0; i < p_sys->i_slave; i++ )
1228 i_pts_delay = __MAX( i_pts_delay, p_sys->slave[i]->i_pts_delay );
1230 if( i_pts_delay < 0 )
1231 i_pts_delay = 0;
1233 /* Take care of audio/spu delay */
1234 const mtime_t i_audio_delay = var_GetInteger( p_input, "audio-delay" );
1235 const mtime_t i_spu_delay = var_GetInteger( p_input, "spu-delay" );
1236 const mtime_t i_extra_delay = __MIN( i_audio_delay, i_spu_delay );
1237 if( i_extra_delay < 0 )
1238 i_pts_delay -= i_extra_delay;
1240 /* Update cr_average depending on the caching */
1241 const int i_cr_average = var_GetInteger( p_input, "cr-average" ) * i_pts_delay / DEFAULT_PTS_DELAY;
1243 /* */
1244 es_out_SetDelay( input_priv(p_input)->p_es_out_display, AUDIO_ES, i_audio_delay );
1245 es_out_SetDelay( input_priv(p_input)->p_es_out_display, SPU_ES, i_spu_delay );
1246 es_out_SetJitter( input_priv(p_input)->p_es_out, i_pts_delay, 0, i_cr_average );
1249 static void InitPrograms( input_thread_t * p_input )
1251 int i_es_out_mode;
1252 vlc_list_t list;
1254 /* Compute correct pts_delay */
1255 UpdatePtsDelay( p_input );
1257 /* Set up es_out */
1258 i_es_out_mode = ES_OUT_MODE_AUTO;
1259 if( input_priv(p_input)->p_sout )
1261 char *prgms;
1263 if( (prgms = var_GetNonEmptyString( p_input, "programs" )) != NULL )
1265 char *buf;
1267 TAB_INIT( list.i_count, list.p_values );
1268 for( const char *prgm = strtok_r( prgms, ",", &buf );
1269 prgm != NULL;
1270 prgm = strtok_r( NULL, ",", &buf ) )
1272 vlc_value_t val = { .i_int = atoi( prgm ) };
1273 TAB_APPEND(list.i_count, list.p_values, val);
1276 if( list.i_count > 0 )
1277 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1278 /* Note : we should remove the "program" callback. */
1280 free( prgms );
1282 else if( var_GetBool( p_input, "sout-all" ) )
1284 i_es_out_mode = ES_OUT_MODE_ALL;
1287 es_out_SetMode( input_priv(p_input)->p_es_out, i_es_out_mode );
1289 /* Inform the demuxer about waited group (needed only for DVB) */
1290 if( i_es_out_mode == ES_OUT_MODE_ALL )
1292 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_GROUP, -1, NULL );
1294 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1296 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_GROUP, -1,
1297 &list );
1298 TAB_CLEAN( list.i_count, list.p_values );
1300 else
1302 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_GROUP,
1303 es_out_GetGroupForced( input_priv(p_input)->p_es_out ), NULL );
1307 static int Init( input_thread_t * p_input )
1309 input_thread_private_t *priv = input_priv(p_input);
1310 input_source_t *master;
1312 if( var_Type( p_input->obj.parent, "meta-file" ) )
1314 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1315 var_SetString( p_input, "sout", "" );
1316 var_SetBool( p_input, "sout-all", false );
1317 var_SetString( p_input, "input-slave", "" );
1318 var_SetInteger( p_input, "input-repeat", 0 );
1319 var_SetString( p_input, "sub-file", "" );
1320 var_SetBool( p_input, "sub-autodetect-file", false );
1323 InitStatistics( p_input );
1324 #ifdef ENABLE_SOUT
1325 if( InitSout( p_input ) )
1326 goto error;
1327 #endif
1329 /* Create es out */
1330 priv->p_es_out = input_EsOutTimeshiftNew( p_input, priv->p_es_out_display,
1331 priv->i_rate );
1332 if( priv->p_es_out == NULL )
1333 goto error;
1335 /* */
1336 input_ChangeState( p_input, OPENING_S );
1337 input_SendEventCache( p_input, 0.0 );
1339 /* */
1340 master = InputSourceNew( p_input, priv->p_item->psz_uri, NULL, false );
1341 if( master == NULL )
1342 goto error;
1343 priv->master = master;
1345 InitTitle( p_input );
1347 /* Load master infos */
1348 /* Init length */
1349 mtime_t i_length;
1350 if( demux_Control( master->p_demux, DEMUX_GET_LENGTH, &i_length ) )
1351 i_length = 0;
1352 if( i_length <= 0 )
1353 i_length = input_item_GetDuration( priv->p_item );
1354 input_SendEventLength( p_input, i_length );
1356 input_SendEventPosition( p_input, 0.0, 0 );
1358 if( !priv->b_preparsing )
1360 StartTitle( p_input );
1361 SetSubtitlesOptions( p_input );
1362 LoadSlaves( p_input );
1363 InitPrograms( p_input );
1365 double f_rate = var_InheritFloat( p_input, "rate" );
1366 if( f_rate != 0.0 && f_rate != 1.0 )
1368 vlc_value_t val = { .i_int = INPUT_RATE_DEFAULT / f_rate };
1369 input_ControlPush( p_input, INPUT_CONTROL_SET_RATE, &val );
1373 if( !priv->b_preparsing && priv->p_sout )
1375 priv->b_out_pace_control = priv->p_sout->i_out_pace_nocontrol > 0;
1377 msg_Dbg( p_input, "starting in %ssync mode",
1378 priv->b_out_pace_control ? "a" : "" );
1381 vlc_meta_t *p_meta = vlc_meta_New();
1382 if( p_meta != NULL )
1384 /* Get meta data from users */
1385 InputMetaUser( p_input, p_meta );
1387 /* Get meta data from master input */
1388 InputSourceMeta( p_input, master, p_meta );
1390 /* And from slave */
1391 for( int i = 0; i < priv->i_slave; i++ )
1392 InputSourceMeta( p_input, priv->slave[i], p_meta );
1394 es_out_ControlSetMeta( priv->p_es_out, p_meta );
1395 vlc_meta_Delete( p_meta );
1398 msg_Dbg( p_input, "`%s' successfully opened",
1399 input_priv(p_input)->p_item->psz_uri );
1401 /* initialization is complete */
1402 input_ChangeState( p_input, PLAYING_S );
1404 return VLC_SUCCESS;
1406 error:
1407 input_ChangeState( p_input, ERROR_S );
1409 if( input_priv(p_input)->p_es_out )
1410 es_out_Delete( input_priv(p_input)->p_es_out );
1411 es_out_SetMode( input_priv(p_input)->p_es_out_display, ES_OUT_MODE_END );
1412 if( input_priv(p_input)->p_resource )
1414 if( input_priv(p_input)->p_sout )
1415 input_resource_RequestSout( input_priv(p_input)->p_resource,
1416 input_priv(p_input)->p_sout, NULL );
1417 input_resource_SetInput( input_priv(p_input)->p_resource, NULL );
1418 if( input_priv(p_input)->p_resource_private )
1419 input_resource_Terminate( input_priv(p_input)->p_resource_private );
1422 if( !priv->b_preparsing && libvlc_stats( p_input ) )
1424 #define EXIT_COUNTER( c ) do { if( input_priv(p_input)->counters.p_##c ) \
1425 stats_CounterClean( input_priv(p_input)->counters.p_##c );\
1426 input_priv(p_input)->counters.p_##c = NULL; } while(0)
1427 EXIT_COUNTER( read_bytes );
1428 EXIT_COUNTER( read_packets );
1429 EXIT_COUNTER( demux_read );
1430 EXIT_COUNTER( input_bitrate );
1431 EXIT_COUNTER( demux_bitrate );
1432 EXIT_COUNTER( demux_corrupted );
1433 EXIT_COUNTER( demux_discontinuity );
1434 EXIT_COUNTER( played_abuffers );
1435 EXIT_COUNTER( lost_abuffers );
1436 EXIT_COUNTER( displayed_pictures );
1437 EXIT_COUNTER( lost_pictures );
1438 EXIT_COUNTER( decoded_audio );
1439 EXIT_COUNTER( decoded_video );
1440 EXIT_COUNTER( decoded_sub );
1442 if( input_priv(p_input)->p_sout )
1444 EXIT_COUNTER( sout_sent_packets );
1445 EXIT_COUNTER( sout_sent_bytes );
1446 EXIT_COUNTER( sout_send_bitrate );
1448 #undef EXIT_COUNTER
1451 /* Mark them deleted */
1452 input_priv(p_input)->p_es_out = NULL;
1453 input_priv(p_input)->p_sout = NULL;
1455 return VLC_EGENERIC;
1458 /*****************************************************************************
1459 * End: end the input thread
1460 *****************************************************************************/
1461 static void End( input_thread_t * p_input )
1463 input_thread_private_t *priv = input_priv(p_input);
1465 /* We are at the end */
1466 input_ChangeState( p_input, END_S );
1468 /* Clean control variables */
1469 input_ControlVarStop( p_input );
1471 /* Stop es out activity */
1472 es_out_SetMode( priv->p_es_out, ES_OUT_MODE_NONE );
1474 /* Delete slave */
1475 for( int i = 0; i < priv->i_slave; i++ )
1476 InputSourceDestroy( priv->slave[i] );
1477 free( priv->slave );
1479 /* Clean up master */
1480 InputSourceDestroy( priv->master );
1481 priv->i_title = 0;
1482 priv->title = NULL;
1483 priv->i_title_offset = 0;
1484 priv->i_seekpoint_offset = 0;
1486 /* Unload all modules */
1487 if( priv->p_es_out )
1488 es_out_Delete( priv->p_es_out );
1489 es_out_SetMode( priv->p_es_out_display, ES_OUT_MODE_END );
1491 if( !priv->b_preparsing )
1493 #define CL_CO( c ) \
1494 do { \
1495 stats_CounterClean( priv->counters.p_##c ); \
1496 priv->counters.p_##c = NULL; \
1497 } while (0)
1499 if( libvlc_stats( p_input ) )
1501 /* make sure we are up to date */
1502 stats_ComputeInputStats( p_input, priv->p_item->p_stats );
1503 CL_CO( read_bytes );
1504 CL_CO( read_packets );
1505 CL_CO( demux_read );
1506 CL_CO( input_bitrate );
1507 CL_CO( demux_bitrate );
1508 CL_CO( demux_corrupted );
1509 CL_CO( demux_discontinuity );
1510 CL_CO( played_abuffers );
1511 CL_CO( lost_abuffers );
1512 CL_CO( displayed_pictures );
1513 CL_CO( lost_pictures );
1514 CL_CO( decoded_audio) ;
1515 CL_CO( decoded_video );
1516 CL_CO( decoded_sub) ;
1519 /* Close optional stream output instance */
1520 if( priv->p_sout )
1522 CL_CO( sout_sent_packets );
1523 CL_CO( sout_sent_bytes );
1524 CL_CO( sout_send_bitrate );
1526 #undef CL_CO
1529 vlc_mutex_lock( &priv->p_item->lock );
1530 if( priv->i_attachment > 0 )
1532 for( int i = 0; i < priv->i_attachment; i++ )
1533 vlc_input_attachment_Delete( priv->attachment[i] );
1534 TAB_CLEAN( priv->i_attachment, priv->attachment );
1535 free( priv->attachment_demux);
1536 priv->attachment_demux = NULL;
1539 /* clean bookmarks */
1540 for( int i = 0; i < priv->i_bookmark; ++i )
1541 vlc_seekpoint_Delete( priv->pp_bookmark[i] );
1542 TAB_CLEAN( priv->i_bookmark, priv->pp_bookmark );
1544 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
1546 /* */
1547 input_resource_RequestSout( input_priv(p_input)->p_resource,
1548 input_priv(p_input)->p_sout, NULL );
1549 input_resource_SetInput( input_priv(p_input)->p_resource, NULL );
1550 if( input_priv(p_input)->p_resource_private )
1551 input_resource_Terminate( input_priv(p_input)->p_resource_private );
1554 /*****************************************************************************
1555 * Control
1556 *****************************************************************************/
1557 void input_ControlPush( input_thread_t *p_input,
1558 int i_type, vlc_value_t *p_val )
1560 input_thread_private_t *sys = input_priv(p_input);
1562 vlc_mutex_lock( &sys->lock_control );
1563 if( sys->is_stopped || sys->i_control >= INPUT_CONTROL_FIFO_SIZE )
1565 if( sys->is_stopped )
1566 msg_Dbg( p_input, "input control stopped, trashing type=%d",
1567 i_type );
1568 else
1569 msg_Err( p_input, "input control fifo overflow, trashing type=%d",
1570 i_type );
1571 if( p_val )
1572 ControlRelease( i_type, *p_val );
1574 else
1576 input_control_t c;
1577 c.i_type = i_type;
1578 if( p_val )
1579 c.val = *p_val;
1580 else
1581 memset( &c.val, 0, sizeof(c.val) );
1583 sys->control[sys->i_control++] = c;
1585 vlc_cond_signal( &sys->wait_control );
1587 vlc_mutex_unlock( &sys->lock_control );
1590 static int ControlGetReducedIndexLocked( input_thread_t *p_input )
1592 const int i_lt = input_priv(p_input)->control[0].i_type;
1593 int i;
1594 for( i = 1; i < input_priv(p_input)->i_control; i++ )
1596 const int i_ct = input_priv(p_input)->control[i].i_type;
1598 if( i_lt == i_ct &&
1599 ( i_ct == INPUT_CONTROL_SET_STATE ||
1600 i_ct == INPUT_CONTROL_SET_RATE ||
1601 i_ct == INPUT_CONTROL_SET_POSITION ||
1602 i_ct == INPUT_CONTROL_SET_TIME ||
1603 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1604 i_ct == INPUT_CONTROL_SET_TITLE ||
1605 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1606 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1608 continue;
1610 else
1612 /* TODO but that's not that important
1613 - merge SET_X with SET_X_CMD
1614 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1615 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1618 break;
1621 return i - 1;
1625 static inline int ControlPop( input_thread_t *p_input,
1626 int *pi_type, vlc_value_t *p_val,
1627 mtime_t i_deadline, bool b_postpone_seek )
1629 input_thread_private_t *p_sys = input_priv(p_input);
1631 vlc_mutex_lock( &p_sys->lock_control );
1632 while( p_sys->i_control <= 0 ||
1633 ( b_postpone_seek && ControlIsSeekRequest( p_sys->control[0].i_type ) ) )
1635 if( p_sys->is_stopped )
1637 vlc_mutex_unlock( &p_sys->lock_control );
1638 return VLC_EGENERIC;
1641 if( i_deadline >= 0 )
1643 if( vlc_cond_timedwait( &p_sys->wait_control, &p_sys->lock_control,
1644 i_deadline ) )
1646 vlc_mutex_unlock( &p_sys->lock_control );
1647 return VLC_EGENERIC;
1650 else
1651 vlc_cond_wait( &p_sys->wait_control, &p_sys->lock_control );
1654 /* */
1655 const int i_index = ControlGetReducedIndexLocked( p_input );
1657 for( int i = 0; i < i_index; ++i )
1659 /* Release Reduced controls */
1660 ControlRelease( p_sys->control[i].i_type, p_sys->control[i].val );
1663 /* */
1664 *pi_type = p_sys->control[i_index].i_type;
1665 *p_val = p_sys->control[i_index].val;
1667 p_sys->i_control -= i_index + 1;
1668 if( p_sys->i_control > 0 )
1669 memmove( &p_sys->control[0], &p_sys->control[i_index+1],
1670 sizeof(*p_sys->control) * p_sys->i_control );
1671 vlc_mutex_unlock( &p_sys->lock_control );
1673 return VLC_SUCCESS;
1675 static bool ControlIsSeekRequest( int i_type )
1677 switch( i_type )
1679 case INPUT_CONTROL_SET_POSITION:
1680 case INPUT_CONTROL_SET_TIME:
1681 case INPUT_CONTROL_SET_TITLE:
1682 case INPUT_CONTROL_SET_TITLE_NEXT:
1683 case INPUT_CONTROL_SET_TITLE_PREV:
1684 case INPUT_CONTROL_SET_SEEKPOINT:
1685 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1686 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1687 case INPUT_CONTROL_SET_BOOKMARK:
1688 case INPUT_CONTROL_NAV_ACTIVATE:
1689 case INPUT_CONTROL_NAV_UP:
1690 case INPUT_CONTROL_NAV_DOWN:
1691 case INPUT_CONTROL_NAV_LEFT:
1692 case INPUT_CONTROL_NAV_RIGHT:
1693 case INPUT_CONTROL_NAV_POPUP:
1694 case INPUT_CONTROL_NAV_MENU:
1695 return true;
1696 default:
1697 return false;
1701 static void ControlRelease( int i_type, vlc_value_t val )
1703 switch( i_type )
1705 case INPUT_CONTROL_ADD_SLAVE:
1706 if( val.p_address )
1707 input_item_slave_Delete( val.p_address );
1708 break;
1709 case INPUT_CONTROL_SET_VIEWPOINT:
1710 case INPUT_CONTROL_SET_INITIAL_VIEWPOINT:
1711 case INPUT_CONTROL_UPDATE_VIEWPOINT:
1712 free( val.p_address );
1713 break;
1715 default:
1716 break;
1720 /* Pause input */
1721 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1723 int i_state = PAUSE_S;
1725 if( input_priv(p_input)->b_can_pause )
1727 demux_t *p_demux = input_priv(p_input)->master->p_demux;
1729 if( demux_Control( p_demux, DEMUX_SET_PAUSE_STATE, true ) )
1731 msg_Warn( p_input, "cannot set pause state" );
1732 return;
1736 /* */
1737 if( es_out_SetPauseState( input_priv(p_input)->p_es_out, input_priv(p_input)->b_can_pause,
1738 true, i_control_date ) )
1740 msg_Warn( p_input, "cannot set pause state at es_out level" );
1741 return;
1744 /* Switch to new state */
1745 input_ChangeState( p_input, i_state );
1748 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1750 if( input_priv(p_input)->b_can_pause )
1752 demux_t *p_demux = input_priv(p_input)->master->p_demux;
1754 if( demux_Control( p_demux, DEMUX_SET_PAUSE_STATE, false ) )
1756 msg_Err( p_input, "cannot resume" );
1757 input_ChangeState( p_input, ERROR_S );
1758 return;
1762 /* Switch to play */
1763 input_ChangeState( p_input, PLAYING_S );
1764 es_out_SetPauseState( input_priv(p_input)->p_es_out, false, false, i_control_date );
1767 static void ViewpointApply( input_thread_t *p_input )
1769 input_thread_private_t *priv = input_priv(p_input);
1771 vlc_viewpoint_clip( &priv->viewpoint );
1773 vout_thread_t **pp_vout;
1774 size_t i_vout;
1775 input_resource_HoldVouts( priv->p_resource, &pp_vout, &i_vout );
1777 for( size_t i = 0; i < i_vout; ++i )
1779 var_SetAddress( pp_vout[i], "viewpoint", &priv->viewpoint );
1780 /* This variable can only be read from callbacks */
1781 var_Change( pp_vout[i], "viewpoint", VLC_VAR_SETVALUE,
1782 &(vlc_value_t) { .p_address = NULL }, NULL );
1783 vlc_object_release( pp_vout[i] );
1785 free( pp_vout );
1787 audio_output_t *p_aout = input_resource_HoldAout( priv->p_resource );
1788 if( p_aout )
1791 var_SetAddress( p_aout, "viewpoint", &priv->viewpoint );
1792 /* This variable can only be read from callbacks */
1793 var_Change( p_aout, "viewpoint", VLC_VAR_SETVALUE,
1794 &(vlc_value_t) { .p_address = NULL }, NULL );
1795 vlc_object_release( p_aout );
1799 static void ControlNav( input_thread_t *p_input, int i_type )
1801 input_thread_private_t *priv = input_priv(p_input);
1803 if( !demux_Control( priv->master->p_demux, i_type
1804 - INPUT_CONTROL_NAV_ACTIVATE + DEMUX_NAV_ACTIVATE ) )
1805 return; /* The demux handled the navigation control */
1807 /* Handle Up/Down/Left/Right if the demux can't navigate */
1808 vlc_viewpoint_t vp = {};
1809 int vol_direction = 0;
1810 int seek_direction = 0;
1811 switch( i_type )
1813 case INPUT_CONTROL_NAV_UP:
1814 vol_direction = 1;
1815 vp.pitch = -1.f;
1816 break;
1817 case INPUT_CONTROL_NAV_DOWN:
1818 vol_direction = -1;
1819 vp.pitch = 1.f;
1820 break;
1821 case INPUT_CONTROL_NAV_LEFT:
1822 seek_direction = -1;
1823 vp.yaw = -1.f;
1824 break;
1825 case INPUT_CONTROL_NAV_RIGHT:
1826 seek_direction = 1;
1827 vp.yaw = 1.f;
1828 break;
1829 case INPUT_CONTROL_NAV_ACTIVATE:
1830 case INPUT_CONTROL_NAV_POPUP:
1831 case INPUT_CONTROL_NAV_MENU:
1832 return;
1833 default:
1834 vlc_assert_unreachable();
1837 /* Try to change the viewpoint if possible */
1838 vout_thread_t **pp_vout;
1839 size_t i_vout;
1840 bool b_viewpoint_ch = false;
1841 input_resource_HoldVouts( priv->p_resource, &pp_vout, &i_vout );
1842 for( size_t i = 0; i < i_vout; ++i )
1844 if( !b_viewpoint_ch
1845 && var_GetBool( pp_vout[i], "viewpoint-changeable" ) )
1846 b_viewpoint_ch = true;
1847 vlc_object_release( pp_vout[i] );
1849 free( pp_vout );
1851 if( b_viewpoint_ch )
1853 priv->viewpoint_changed = true;
1854 priv->viewpoint.yaw += vp.yaw;
1855 priv->viewpoint.pitch += vp.pitch;
1856 priv->viewpoint.roll += vp.roll;
1857 priv->viewpoint.fov += vp.fov;
1858 ViewpointApply( p_input );
1859 return;
1862 /* Seek or change volume if the input doesn't have navigation or viewpoint */
1863 if( seek_direction != 0 )
1865 mtime_t it = var_InheritInteger( p_input, "short-jump-size" );
1866 var_SetInteger( p_input, "time-offset", it * seek_direction * CLOCK_FREQ );
1868 else
1870 assert( vol_direction != 0 );
1871 audio_output_t *p_aout = input_resource_HoldAout( priv->p_resource );
1872 if( p_aout )
1874 aout_VolumeUpdate( p_aout, vol_direction, NULL );
1875 vlc_object_release( p_aout );
1880 #ifdef ENABLE_SOUT
1881 static void ControlUpdateSout( input_thread_t *p_input, const char* psz_chain )
1883 var_SetString( p_input, "sout", psz_chain );
1884 if( psz_chain && *psz_chain )
1886 if( InitSout( p_input ) != VLC_SUCCESS )
1888 msg_Err( p_input, "Failed to start sout" );
1889 return;
1892 else
1894 input_resource_RequestSout( input_priv(p_input)->p_resource,
1895 input_priv(p_input)->p_sout, NULL );
1896 input_priv(p_input)->p_sout = NULL;
1898 es_out_Control( input_priv(p_input)->p_es_out, ES_OUT_RESTART_ALL_ES );
1900 #endif
1902 static void ControlInsertDemuxFilter( input_thread_t* p_input, const char* psz_demux_chain )
1904 input_source_t *p_inputSource = input_priv(p_input)->master;
1905 demux_t *p_filtered_demux = demux_FilterChainNew( p_inputSource->p_demux, psz_demux_chain );
1906 if ( p_filtered_demux != NULL )
1907 p_inputSource->p_demux = p_filtered_demux;
1908 else if ( psz_demux_chain != NULL )
1909 msg_Dbg(p_input, "Failed to create demux filter %s", psz_demux_chain);
1912 static bool Control( input_thread_t *p_input,
1913 int i_type, vlc_value_t val )
1915 const mtime_t i_control_date = mdate();
1916 /* FIXME b_force_update is abused, it should be carefully checked */
1917 bool b_force_update = false;
1919 if( !p_input )
1920 return b_force_update;
1922 switch( i_type )
1924 case INPUT_CONTROL_SET_POSITION:
1926 if( input_priv(p_input)->b_recording )
1928 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION ignored while recording" );
1929 break;
1932 float f_pos = val.f_float;
1933 if( f_pos < 0.f )
1934 f_pos = 0.f;
1935 else if( f_pos > 1.f )
1936 f_pos = 1.f;
1937 /* Reset the decoders states and clock sync (before calling the demuxer */
1938 es_out_SetTime( input_priv(p_input)->p_es_out, -1 );
1939 if( demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_POSITION,
1940 (double) f_pos, !input_priv(p_input)->b_fast_seek ) )
1942 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION "
1943 "%2.1f%% failed", (double)(f_pos * 100.f) );
1945 else
1947 if( input_priv(p_input)->i_slave > 0 )
1948 SlaveSeek( p_input );
1949 input_priv(p_input)->master->b_eof = false;
1951 b_force_update = true;
1953 break;
1956 case INPUT_CONTROL_SET_TIME:
1958 int64_t i_time;
1959 int i_ret;
1961 if( input_priv(p_input)->b_recording )
1963 msg_Err( p_input, "INPUT_CONTROL_SET_TIME ignored while recording" );
1964 break;
1967 i_time = val.i_int;
1968 if( i_time < 0 )
1969 i_time = 0;
1971 /* Reset the decoders states and clock sync (before calling the demuxer */
1972 es_out_SetTime( input_priv(p_input)->p_es_out, -1 );
1974 i_ret = demux_Control( input_priv(p_input)->master->p_demux,
1975 DEMUX_SET_TIME, i_time,
1976 !input_priv(p_input)->b_fast_seek );
1977 if( i_ret )
1979 int64_t i_length;
1981 /* Emulate it with a SET_POS */
1982 if( !demux_Control( input_priv(p_input)->master->p_demux,
1983 DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
1985 double f_pos = (double)i_time / (double)i_length;
1986 i_ret = demux_Control( input_priv(p_input)->master->p_demux,
1987 DEMUX_SET_POSITION, f_pos,
1988 !input_priv(p_input)->b_fast_seek );
1991 if( i_ret )
1993 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME %"PRId64
1994 " failed or not possible", i_time );
1996 else
1998 if( input_priv(p_input)->i_slave > 0 )
1999 SlaveSeek( p_input );
2000 input_priv(p_input)->master->b_eof = false;
2002 b_force_update = true;
2004 break;
2007 case INPUT_CONTROL_SET_STATE:
2008 switch( val.i_int )
2010 case PLAYING_S:
2011 if( input_priv(p_input)->i_state == PAUSE_S )
2013 ControlUnpause( p_input, i_control_date );
2014 b_force_update = true;
2016 break;
2017 case PAUSE_S:
2018 if( input_priv(p_input)->i_state == PLAYING_S )
2020 ControlPause( p_input, i_control_date );
2021 b_force_update = true;
2023 break;
2024 default:
2025 msg_Err( p_input, "invalid INPUT_CONTROL_SET_STATE" );
2027 break;
2029 case INPUT_CONTROL_SET_RATE:
2031 /* Get rate and direction */
2032 long long i_rate = llabs( val.i_int );
2033 int i_rate_sign = val.i_int < 0 ? -1 : 1;
2035 /* Check rate bound */
2036 if( i_rate < INPUT_RATE_MIN )
2038 msg_Dbg( p_input, "cannot set rate faster" );
2039 i_rate = INPUT_RATE_MIN;
2041 else if( i_rate > INPUT_RATE_MAX )
2043 msg_Dbg( p_input, "cannot set rate slower" );
2044 i_rate = INPUT_RATE_MAX;
2047 /* Apply direction */
2048 if( i_rate_sign < 0 )
2050 if( input_priv(p_input)->master->b_rescale_ts )
2052 msg_Dbg( p_input, "cannot set negative rate" );
2053 i_rate = input_priv(p_input)->i_rate;
2054 assert( i_rate > 0 );
2056 else
2058 i_rate *= i_rate_sign;
2062 if( i_rate != INPUT_RATE_DEFAULT &&
2063 ( ( !input_priv(p_input)->b_can_rate_control && !input_priv(p_input)->master->b_rescale_ts ) ||
2064 ( input_priv(p_input)->p_sout && !input_priv(p_input)->b_out_pace_control ) ) )
2066 msg_Dbg( p_input, "cannot change rate" );
2067 i_rate = INPUT_RATE_DEFAULT;
2069 if( i_rate != input_priv(p_input)->i_rate &&
2070 !input_priv(p_input)->b_can_pace_control && input_priv(p_input)->b_can_rate_control )
2072 if( !input_priv(p_input)->master->b_rescale_ts )
2073 es_out_Control( input_priv(p_input)->p_es_out, ES_OUT_RESET_PCR );
2075 if( demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_RATE,
2076 &i_rate ) )
2078 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
2079 i_rate = input_priv(p_input)->i_rate;
2083 /* */
2084 if( i_rate != input_priv(p_input)->i_rate )
2086 input_priv(p_input)->i_rate = i_rate;
2087 input_SendEventRate( p_input, i_rate );
2089 if( input_priv(p_input)->master->b_rescale_ts )
2091 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;
2092 es_out_SetRate( input_priv(p_input)->p_es_out, i_rate_source, i_rate );
2095 b_force_update = true;
2097 break;
2100 case INPUT_CONTROL_SET_PROGRAM:
2101 /* No need to force update, es_out does it if needed */
2102 es_out_Control( input_priv(p_input)->p_es_out,
2103 ES_OUT_SET_GROUP, val.i_int );
2105 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_GROUP, val.i_int,
2106 NULL );
2107 break;
2109 case INPUT_CONTROL_SET_ES:
2110 /* No need to force update, es_out does it if needed */
2111 es_out_Control( input_priv(p_input)->p_es_out_display,
2112 ES_OUT_SET_ES_BY_ID, (int)val.i_int );
2114 demux_Control( input_priv(p_input)->master->p_demux, DEMUX_SET_ES, (int)val.i_int );
2115 break;
2117 case INPUT_CONTROL_RESTART_ES:
2118 es_out_Control( input_priv(p_input)->p_es_out_display,
2119 ES_OUT_RESTART_ES_BY_ID, (int)val.i_int );
2120 break;
2122 case INPUT_CONTROL_SET_VIEWPOINT:
2123 case INPUT_CONTROL_SET_INITIAL_VIEWPOINT:
2124 case INPUT_CONTROL_UPDATE_VIEWPOINT:
2126 input_thread_private_t *priv = input_priv(p_input);
2127 const vlc_viewpoint_t *p_vp = val.p_address;
2129 if ( i_type == INPUT_CONTROL_SET_INITIAL_VIEWPOINT )
2132 /* Set the initial viewpoint if it had not been changed by the
2133 * user. */
2134 if( !priv->viewpoint_changed )
2135 priv->viewpoint = *p_vp;
2136 /* Update viewpoints of aout and every vouts in all cases. */
2138 else if ( i_type == INPUT_CONTROL_SET_VIEWPOINT)
2140 priv->viewpoint_changed = true;
2141 priv->viewpoint = *p_vp;
2143 else
2145 priv->viewpoint_changed = true;
2146 priv->viewpoint.yaw += p_vp->yaw;
2147 priv->viewpoint.pitch += p_vp->pitch;
2148 priv->viewpoint.roll += p_vp->roll;
2149 priv->viewpoint.fov += p_vp->fov;
2152 ViewpointApply( p_input );
2153 break;
2156 case INPUT_CONTROL_SET_AUDIO_DELAY:
2157 input_SendEventAudioDelay( p_input, val.i_int );
2158 UpdatePtsDelay( p_input );
2159 break;
2161 case INPUT_CONTROL_SET_SPU_DELAY:
2162 input_SendEventSubtitleDelay( p_input, val.i_int );
2163 UpdatePtsDelay( p_input );
2164 break;
2166 case INPUT_CONTROL_SET_TITLE:
2167 case INPUT_CONTROL_SET_TITLE_NEXT:
2168 case INPUT_CONTROL_SET_TITLE_PREV:
2170 if( input_priv(p_input)->b_recording )
2172 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
2173 break;
2175 if( input_priv(p_input)->master->i_title <= 0 )
2176 break;
2178 int i_title = demux_GetTitle( input_priv(p_input)->master->p_demux );
2179 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
2180 i_title--;
2181 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
2182 i_title++;
2183 else
2184 i_title = val.i_int;
2185 if( i_title < 0 || i_title >= input_priv(p_input)->master->i_title )
2186 break;
2188 es_out_SetTime( input_priv(p_input)->p_es_out, -1 );
2189 demux_Control( input_priv(p_input)->master->p_demux,
2190 DEMUX_SET_TITLE, i_title );
2191 input_SendEventTitle( p_input, i_title );
2192 break;
2194 case INPUT_CONTROL_SET_SEEKPOINT:
2195 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
2196 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
2198 if( input_priv(p_input)->b_recording )
2200 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
2201 break;
2203 if( input_priv(p_input)->master->i_title <= 0 )
2204 break;
2206 demux_t *p_demux = input_priv(p_input)->master->p_demux;
2208 int i_title = demux_GetTitle( p_demux );
2209 int i_seekpoint = demux_GetSeekpoint( p_demux );
2211 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
2213 int64_t i_seekpoint_time = input_priv(p_input)->master->title[i_title]->seekpoint[i_seekpoint]->i_time_offset;
2214 int64_t i_input_time = var_GetInteger( p_input, "time" );
2215 if( i_seekpoint_time >= 0 && i_input_time >= 0 )
2217 if( i_input_time < i_seekpoint_time + 3000000 )
2218 i_seekpoint--;
2220 else
2221 i_seekpoint--;
2223 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
2224 i_seekpoint++;
2225 else
2226 i_seekpoint = val.i_int;
2227 if( i_seekpoint < 0
2228 || i_seekpoint >= input_priv(p_input)->master->title[i_title]->i_seekpoint )
2229 break;
2231 es_out_SetTime( input_priv(p_input)->p_es_out, -1 );
2232 demux_Control( input_priv(p_input)->master->p_demux,
2233 DEMUX_SET_SEEKPOINT, i_seekpoint );
2234 input_SendEventSeekpoint( p_input, i_title, i_seekpoint );
2235 break;
2238 case INPUT_CONTROL_ADD_SLAVE:
2239 if( val.p_address )
2241 input_item_slave_t *p_item_slave = val.p_address;
2242 unsigned i_flags = SLAVE_ADD_CANFAIL | SLAVE_ADD_SET_TIME;
2243 if( p_item_slave->b_forced )
2244 i_flags |= SLAVE_ADD_FORCED;
2246 if( input_SlaveSourceAdd( p_input, p_item_slave->i_type,
2247 p_item_slave->psz_uri, i_flags )
2248 == VLC_SUCCESS )
2250 /* Update item slaves */
2251 input_item_AddSlave( input_priv(p_input)->p_item, p_item_slave );
2252 /* The slave is now owned by the item */
2253 val.p_address = NULL;
2256 break;
2258 case INPUT_CONTROL_SET_RECORD_STATE:
2259 if( !!input_priv(p_input)->b_recording != !!val.b_bool )
2261 if( input_priv(p_input)->master->b_can_stream_record )
2263 if( demux_Control( input_priv(p_input)->master->p_demux,
2264 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2265 val.b_bool = false;
2267 else
2269 if( es_out_SetRecordState( input_priv(p_input)->p_es_out_display, val.b_bool ) )
2270 val.b_bool = false;
2272 input_priv(p_input)->b_recording = val.b_bool;
2274 input_SendEventRecord( p_input, val.b_bool );
2276 b_force_update = true;
2278 break;
2280 case INPUT_CONTROL_SET_FRAME_NEXT:
2281 if( input_priv(p_input)->i_state == PAUSE_S )
2283 es_out_SetFrameNext( input_priv(p_input)->p_es_out );
2285 else if( input_priv(p_input)->i_state == PLAYING_S )
2287 ControlPause( p_input, i_control_date );
2289 else
2291 msg_Err( p_input, "invalid state for frame next" );
2293 b_force_update = true;
2294 break;
2296 case INPUT_CONTROL_SET_BOOKMARK:
2298 mtime_t time_offset = -1;
2300 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
2301 if( val.i_int >= 0 && val.i_int < input_priv(p_input)->i_bookmark )
2303 const seekpoint_t *p_bookmark = input_priv(p_input)->pp_bookmark[val.i_int];
2304 time_offset = p_bookmark->i_time_offset;
2306 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
2308 if( time_offset < 0 )
2310 msg_Err( p_input, "invalid bookmark %"PRId64, val.i_int );
2311 break;
2314 val.i_int = time_offset;
2315 b_force_update = Control( p_input, INPUT_CONTROL_SET_TIME, val );
2316 break;
2318 case INPUT_CONTROL_SET_RENDERER:
2320 #ifdef ENABLE_SOUT
2321 vlc_renderer_item_t *p_item = val.p_address;
2322 input_thread_private_t *p_priv = input_priv( p_input );
2323 // We do not support switching from a renderer to another for now
2324 if ( p_item == NULL && p_priv->p_renderer == NULL )
2325 break;
2327 if ( p_priv->p_renderer )
2329 ControlUpdateSout( p_input, NULL );
2330 demux_FilterDisable( p_priv->master->p_demux,
2331 vlc_renderer_item_demux_filter( p_priv->p_renderer ) );
2332 vlc_renderer_item_release( p_priv->p_renderer );
2333 p_priv->p_renderer = NULL;
2335 if( p_item != NULL )
2337 p_priv->p_renderer = vlc_renderer_item_hold( p_item );
2338 ControlUpdateSout( p_input, vlc_renderer_item_sout( p_item ) );
2339 if( !demux_FilterEnable( p_priv->master->p_demux,
2340 vlc_renderer_item_demux_filter( p_priv->p_renderer ) ) )
2342 ControlInsertDemuxFilter( p_input,
2343 vlc_renderer_item_demux_filter( p_item ) );
2345 input_resource_TerminateVout( p_priv->p_resource );
2347 #endif
2348 break;
2351 case INPUT_CONTROL_NAV_ACTIVATE:
2352 case INPUT_CONTROL_NAV_UP:
2353 case INPUT_CONTROL_NAV_DOWN:
2354 case INPUT_CONTROL_NAV_LEFT:
2355 case INPUT_CONTROL_NAV_RIGHT:
2356 case INPUT_CONTROL_NAV_POPUP:
2357 case INPUT_CONTROL_NAV_MENU:
2358 ControlNav( p_input, i_type );
2359 break;
2361 default:
2362 msg_Err( p_input, "not yet implemented" );
2363 break;
2366 ControlRelease( i_type, val );
2367 return b_force_update;
2370 /*****************************************************************************
2371 * UpdateTitleSeekpoint
2372 *****************************************************************************/
2373 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2374 int i_title, int i_seekpoint )
2376 int i_title_end = input_priv(p_input)->master->i_title_end -
2377 input_priv(p_input)->master->i_title_offset;
2378 int i_seekpoint_end = input_priv(p_input)->master->i_seekpoint_end -
2379 input_priv(p_input)->master->i_seekpoint_offset;
2381 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2383 if( i_title > i_title_end ||
2384 ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2385 return VLC_DEMUXER_EOF;
2387 else if( i_seekpoint_end >= 0 )
2389 if( i_seekpoint > i_seekpoint_end )
2390 return VLC_DEMUXER_EOF;
2392 else if( i_title_end >= 0 )
2394 if( i_title > i_title_end )
2395 return VLC_DEMUXER_EOF;
2397 return VLC_DEMUXER_SUCCESS;
2399 /*****************************************************************************
2400 * Update*FromDemux:
2401 *****************************************************************************/
2402 static int UpdateTitleSeekpointFromDemux( input_thread_t *p_input )
2404 demux_t *p_demux = input_priv(p_input)->master->p_demux;
2406 /* TODO event-like */
2407 if( demux_TestAndClearFlags( p_demux, INPUT_UPDATE_TITLE ) )
2408 input_SendEventTitle( p_input, demux_GetTitle( p_demux ) );
2410 if( demux_TestAndClearFlags( p_demux, INPUT_UPDATE_SEEKPOINT ) )
2411 input_SendEventSeekpoint( p_input, demux_GetTitle( p_demux ),
2412 demux_GetSeekpoint( p_demux ) );
2414 return UpdateTitleSeekpoint( p_input,
2415 demux_GetTitle( p_demux ),
2416 demux_GetSeekpoint( p_demux ) );
2419 static void UpdateGenericFromDemux( input_thread_t *p_input )
2421 demux_t *p_demux = input_priv(p_input)->master->p_demux;
2423 if( demux_TestAndClearFlags( p_demux, INPUT_UPDATE_META ) )
2424 InputUpdateMeta( p_input, p_demux );
2427 double quality;
2428 double strength;
2430 if( !demux_Control( p_demux, DEMUX_GET_SIGNAL, &quality, &strength ) )
2431 input_SendEventSignal( p_input, quality, strength );
2435 static void UpdateTitleListfromDemux( input_thread_t *p_input )
2437 input_thread_private_t *priv = input_priv(p_input);
2438 input_source_t *in = priv->master;
2440 /* Delete the preexisting titles */
2441 if( in->i_title > 0 )
2443 for( int i = 0; i < in->i_title; i++ )
2444 vlc_input_title_Delete( in->title[i] );
2445 TAB_CLEAN( in->i_title, in->title );
2446 priv->i_title = 0;
2447 priv->title = NULL;
2448 in->b_title_demux = false;
2451 /* Get the new title list */
2452 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2453 &in->title, &in->i_title,
2454 &in->i_title_offset, &in->i_seekpoint_offset ) )
2455 TAB_INIT( in->i_title, in->title );
2456 else
2457 in->b_title_demux = true;
2459 InitTitle( p_input );
2462 static int
2463 InputStreamHandleAnchor( input_source_t *source, stream_t **stream,
2464 char const *anchor )
2466 char const* extra;
2467 if( stream_extractor_AttachParsed( stream, anchor, &extra ) )
2469 msg_Err( source, "unable to attach stream-extractors for %s",
2470 (*stream)->psz_url );
2472 return VLC_EGENERIC;
2475 if( vlc_stream_directory_Attach( stream, NULL ) )
2476 msg_Dbg( source, "attachment of directory-extractor failed for %s",
2477 (*stream)->psz_url );
2479 MRLSections( extra ? extra : "",
2480 &source->i_title_start, &source->i_title_end,
2481 &source->i_seekpoint_start, &source->i_seekpoint_end );
2483 return VLC_SUCCESS;
2486 static demux_t *InputDemuxNew( input_thread_t *p_input, input_source_t *p_source,
2487 const char *psz_access, const char *psz_demux,
2488 const char *psz_path, const char *psz_anchor )
2490 input_thread_private_t *priv = input_priv(p_input );
2491 demux_t *p_demux = NULL;
2493 /* first, try to create an access demux */
2494 p_demux = demux_NewAdvanced( VLC_OBJECT( p_source ), p_input,
2495 psz_access, psz_demux, psz_path,
2496 NULL, priv->p_es_out, priv->b_preparsing );
2497 if( p_demux )
2499 MRLSections( psz_anchor,
2500 &p_source->i_title_start, &p_source->i_title_end,
2501 &p_source->i_seekpoint_start, &p_source->i_seekpoint_end );
2503 return p_demux;
2506 /* not an access-demux: create the underlying access stream */
2507 char *psz_base_mrl;
2509 if( asprintf( &psz_base_mrl, "%s://%s", psz_access, psz_path ) < 0 )
2510 return NULL;
2512 char *psz_filters = var_InheritString( p_source, "stream-filter" );
2513 stream_t* p_stream = stream_AccessNew( VLC_OBJECT( p_source ), p_input,
2514 priv->b_preparsing,
2515 psz_base_mrl );
2516 FREENULL( psz_base_mrl );
2518 if( p_stream == NULL )
2519 goto error;
2521 /* attach explicit stream filters to stream */
2522 if( psz_filters )
2523 p_stream = stream_FilterChainNew( p_stream, psz_filters );
2525 FREENULL( psz_filters );
2527 /* handle anchors */
2528 if( InputStreamHandleAnchor( p_source, &p_stream, psz_anchor ) )
2529 goto error;
2531 /* attach conditional record stream-filter */
2532 if( var_InheritBool( p_source, "input-record-native" ) )
2533 p_stream = stream_FilterChainNew( p_stream, "record" );
2535 /* create a regular demux with the access stream created */
2536 p_demux = demux_NewAdvanced( VLC_OBJECT( p_source ), p_input,
2537 psz_access, psz_demux, psz_path,
2538 p_stream, priv->p_es_out,
2539 priv->b_preparsing );
2540 if( p_demux )
2541 return p_demux;
2543 error:
2544 free( psz_base_mrl );
2545 free( psz_filters );
2547 if( p_stream )
2548 vlc_stream_Delete( p_stream );
2550 return NULL;
2553 /*****************************************************************************
2554 * InputSourceNew:
2555 *****************************************************************************/
2556 static input_source_t *InputSourceNew( input_thread_t *p_input,
2557 const char *psz_mrl,
2558 const char *psz_forced_demux,
2559 bool b_in_can_fail )
2561 input_thread_private_t *priv = input_priv(p_input);
2562 input_source_t *in = vlc_custom_create( p_input, sizeof( *in ),
2563 "input source" );
2564 if( unlikely(in == NULL) )
2565 return NULL;
2567 const char *psz_access, *psz_demux, *psz_path, *psz_anchor = NULL;
2569 assert( psz_mrl );
2570 char *psz_dup = strdup( psz_mrl );
2571 char *psz_demux_var = NULL;
2573 if( psz_dup == NULL )
2575 vlc_object_release( in );
2576 return NULL;
2579 /* Split uri */
2580 input_SplitMRL( &psz_access, &psz_demux, &psz_path, &psz_anchor, psz_dup );
2582 if( psz_demux == NULL || psz_demux[0] == '\0' )
2583 psz_demux = psz_demux_var = var_InheritString( in, "demux" );
2585 if( psz_forced_demux != NULL )
2586 psz_demux = psz_forced_demux;
2588 if( psz_demux == NULL )
2589 psz_demux = "any";
2591 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2592 psz_mrl, psz_access, psz_demux, psz_path );
2594 if( input_priv(p_input)->master == NULL /* XXX ugly */)
2595 { /* On master stream only, use input-list */
2596 char *str = var_InheritString( p_input, "input-list" );
2597 if( str != NULL )
2599 char *list;
2601 var_Create( p_input, "concat-list", VLC_VAR_STRING );
2602 if( likely(asprintf( &list, "%s://%s,%s", psz_access, psz_path,
2603 str ) >= 0) )
2605 var_SetString( p_input, "concat-list", list );
2606 free( list );
2608 free( str );
2609 psz_access = "concat";
2613 if( strcasecmp( psz_access, "concat" ) )
2614 { /* Autodetect extra files if none specified */
2615 int count;
2616 char **tab;
2618 TAB_INIT( count, tab );
2619 InputGetExtraFiles( p_input, &count, &tab, &psz_access, psz_path );
2620 if( count > 0 )
2622 char *list = NULL;
2624 for( int i = 0; i < count; i++ )
2626 char *str;
2627 if( asprintf( &str, "%s,%s", list ? list : psz_mrl,
2628 tab[i] ) < 0 )
2629 break;
2631 free( tab[i] );
2632 free( list );
2633 list = str;
2636 var_Create( p_input, "concat-list", VLC_VAR_STRING );
2637 if( likely(list != NULL) )
2639 var_SetString( p_input, "concat-list", list );
2640 free( list );
2643 TAB_CLEAN( count, tab );
2646 in->p_demux = InputDemuxNew( p_input, in, psz_access, psz_demux,
2647 psz_path, psz_anchor );
2649 free( psz_demux_var );
2650 free( psz_dup );
2652 if( in->p_demux == NULL )
2654 if( !b_in_can_fail && !input_Stopped( p_input ) )
2655 vlc_dialog_display_error( p_input, _("Your input can't be opened"),
2656 _("VLC is unable to open the MRL '%s'."
2657 " Check the log for details."), psz_mrl );
2658 vlc_object_release( in );
2659 return NULL;
2662 char *psz_demux_chain = NULL;
2663 if( priv->p_renderer )
2665 const char* psz_renderer_demux = vlc_renderer_item_demux_filter(
2666 priv->p_renderer );
2667 if( psz_renderer_demux )
2668 psz_demux_chain = strdup( psz_renderer_demux );
2670 if( !psz_demux_chain )
2671 psz_demux_chain = var_GetNonEmptyString(p_input, "demux-filter");
2672 if( psz_demux_chain != NULL ) /* add the chain of demux filters */
2674 in->p_demux = demux_FilterChainNew( in->p_demux, psz_demux_chain );
2675 free( psz_demux_chain );
2677 if( in->p_demux == NULL )
2679 msg_Err(p_input, "Failed to create demux filter");
2680 vlc_object_release( in );
2681 return NULL;
2685 /* Get infos from (access_)demux */
2686 bool b_can_seek;
2687 if( demux_Control( in->p_demux, DEMUX_CAN_SEEK, &b_can_seek ) )
2688 b_can_seek = false;
2689 var_SetBool( p_input, "can-seek", b_can_seek );
2691 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2692 &in->b_can_pace_control ) )
2693 in->b_can_pace_control = false;
2695 assert( in->p_demux->pf_demux != NULL || !in->b_can_pace_control );
2697 if( !in->b_can_pace_control )
2699 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2700 &in->b_can_rate_control ) )
2702 in->b_can_rate_control = false;
2703 in->b_rescale_ts = true;
2705 else
2706 in->b_rescale_ts = !in->b_can_rate_control;
2708 else
2710 in->b_can_rate_control = true;
2711 in->b_rescale_ts = true;
2714 demux_Control( in->p_demux, DEMUX_CAN_PAUSE, &in->b_can_pause );
2716 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2717 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2718 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control && in->b_can_rate_control );
2720 /* Set record capabilities */
2721 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2722 in->b_can_stream_record = false;
2723 #ifdef ENABLE_SOUT
2724 if( !var_GetBool( p_input, "input-record-native" ) )
2725 in->b_can_stream_record = false;
2726 var_SetBool( p_input, "can-record", true );
2727 #else
2728 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2729 #endif
2731 /* get attachment
2732 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2733 if( !input_priv(p_input)->b_preparsing )
2735 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2736 &in->title, &in->i_title,
2737 &in->i_title_offset, &in->i_seekpoint_offset ))
2739 TAB_INIT( in->i_title, in->title );
2741 else
2743 in->b_title_demux = true;
2746 int i_attachment;
2747 input_attachment_t **attachment;
2748 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2749 &attachment, &i_attachment ) )
2751 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
2752 AppendAttachment( &input_priv(p_input)->i_attachment, &input_priv(p_input)->attachment, &input_priv(p_input)->attachment_demux,
2753 i_attachment, attachment, in->p_demux );
2754 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
2757 demux_Control( in->p_demux, DEMUX_GET_PTS_DELAY, &in->i_pts_delay );
2758 if( in->i_pts_delay > INPUT_PTS_DELAY_MAX )
2759 in->i_pts_delay = INPUT_PTS_DELAY_MAX;
2760 else if( in->i_pts_delay < 0 )
2761 in->i_pts_delay = 0;
2764 if( demux_Control( in->p_demux, DEMUX_GET_FPS, &in->f_fps ) )
2765 in->f_fps = 0.f;
2767 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2768 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2770 return in;
2773 /*****************************************************************************
2774 * InputSourceDestroy:
2775 *****************************************************************************/
2776 static void InputSourceDestroy( input_source_t *in )
2778 int i;
2780 if( in->p_demux )
2781 demux_Delete( in->p_demux );
2783 if( in->i_title > 0 )
2785 for( i = 0; i < in->i_title; i++ )
2786 vlc_input_title_Delete( in->title[i] );
2787 TAB_CLEAN( in->i_title, in->title );
2790 vlc_object_release( in );
2793 /*****************************************************************************
2794 * InputSourceMeta:
2795 *****************************************************************************/
2796 static void InputSourceMeta( input_thread_t *p_input,
2797 input_source_t *p_source, vlc_meta_t *p_meta )
2799 demux_t *p_demux = p_source->p_demux;
2801 /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
2802 * is a bad idea */
2804 bool has_meta = false;
2806 /* Read demux meta */
2807 if( !demux_Control( p_demux, DEMUX_GET_META, p_meta ) )
2808 has_meta = true;
2810 bool has_unsupported;
2811 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &has_unsupported ) )
2812 has_unsupported = true;
2814 /* If the demux report unsupported meta data, or if we don't have meta data
2815 * try an external "meta reader" */
2816 if( has_meta && !has_unsupported )
2817 return;
2819 demux_meta_t *p_demux_meta =
2820 vlc_custom_create( p_source, sizeof( *p_demux_meta ), "demux meta" );
2821 if( unlikely(p_demux_meta == NULL) )
2822 return;
2823 p_demux_meta->p_item = input_priv(p_input)->p_item;
2825 module_t *p_id3 = module_need( p_demux_meta, "meta reader", NULL, false );
2826 if( p_id3 )
2828 if( p_demux_meta->p_meta )
2830 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2831 vlc_meta_Delete( p_demux_meta->p_meta );
2834 if( p_demux_meta->i_attachments > 0 )
2836 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
2837 AppendAttachment( &input_priv(p_input)->i_attachment, &input_priv(p_input)->attachment, &input_priv(p_input)->attachment_demux,
2838 p_demux_meta->i_attachments, p_demux_meta->attachments, p_demux);
2839 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
2841 module_unneed( p_demux, p_id3 );
2843 vlc_object_release( p_demux_meta );
2847 static void SlaveDemux( input_thread_t *p_input )
2849 int64_t i_time;
2850 int i;
2852 if( demux_Control( input_priv(p_input)->master->p_demux, DEMUX_GET_TIME, &i_time ) )
2854 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2855 return;
2858 for( i = 0; i < input_priv(p_input)->i_slave; i++ )
2860 input_source_t *in = input_priv(p_input)->slave[i];
2861 int i_ret;
2863 if( in->b_eof )
2864 continue;
2866 /* Call demux_Demux until we have read enough data */
2867 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2869 for( ;; )
2871 int64_t i_stime;
2872 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2874 msg_Err( p_input, "slave[%d] doesn't like "
2875 "DEMUX_GET_TIME -> EOF", i );
2876 i_ret = 0;
2877 break;
2880 if( i_stime >= i_time )
2882 i_ret = 1;
2883 break;
2886 if( ( i_ret = demux_Demux( in->p_demux ) ) <= 0 )
2887 break;
2890 else
2892 i_ret = demux_Demux( in->p_demux );
2895 if( i_ret <= 0 )
2897 msg_Dbg( p_input, "slave %d EOF", i );
2898 in->b_eof = true;
2903 static void SlaveSeek( input_thread_t *p_input )
2905 int64_t i_time;
2906 int i;
2908 if( demux_Control( input_priv(p_input)->master->p_demux, DEMUX_GET_TIME, &i_time ) )
2910 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2911 return;
2914 for( i = 0; i < input_priv(p_input)->i_slave; i++ )
2916 input_source_t *in = input_priv(p_input)->slave[i];
2918 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time, true ) )
2920 if( !in->b_eof )
2921 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2922 in->b_eof = true;
2924 else
2926 in->b_eof = false;
2931 /*****************************************************************************
2932 * InputMetaUser:
2933 *****************************************************************************/
2934 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2936 static const struct { int i_meta; const char *psz_name; } p_list[] = {
2937 { vlc_meta_Title, "meta-title" },
2938 { vlc_meta_Artist, "meta-artist" },
2939 { vlc_meta_Genre, "meta-genre" },
2940 { vlc_meta_Copyright, "meta-copyright" },
2941 { vlc_meta_Description, "meta-description" },
2942 { vlc_meta_Date, "meta-date" },
2943 { vlc_meta_URL, "meta-url" },
2944 { 0, NULL }
2947 /* Get meta information from user */
2948 for( int i = 0; p_list[i].psz_name; i++ )
2950 char *psz_string = var_GetNonEmptyString( p_input, p_list[i].psz_name );
2951 if( !psz_string )
2952 continue;
2954 EnsureUTF8( psz_string );
2955 vlc_meta_Set( p_meta, p_list[i].i_meta, psz_string );
2956 free( psz_string );
2960 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2961 const demux_t ***ppp_attachment_demux,
2962 int i_new, input_attachment_t **pp_new, const demux_t *p_demux )
2964 int i_attachment = *pi_attachment;
2965 int i;
2967 input_attachment_t **pp_att = realloc( *ppp_attachment,
2968 sizeof(*pp_att) * ( i_attachment + i_new ) );
2969 if( likely(pp_att) )
2971 *ppp_attachment = pp_att;
2972 const demux_t **pp_attdmx = realloc( *ppp_attachment_demux,
2973 sizeof(*pp_attdmx) * ( i_attachment + i_new ) );
2974 if( likely(pp_attdmx) )
2976 *ppp_attachment_demux = pp_attdmx;
2978 for( i = 0; i < i_new; i++ )
2980 pp_att[i_attachment] = pp_new[i];
2981 pp_attdmx[i_attachment++] = p_demux;
2983 /* */
2984 *pi_attachment = i_attachment;
2985 free( pp_new );
2986 return;
2990 /* on alloc errors */
2991 for( i = 0; i < i_new; i++ )
2992 vlc_input_attachment_Delete( pp_new[i] );
2993 free( pp_new );
2996 /*****************************************************************************
2997 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2998 * arturl and locking issue.
2999 *****************************************************************************/
3000 static void InputUpdateMeta( input_thread_t *p_input, demux_t *p_demux )
3002 vlc_meta_t *p_meta = vlc_meta_New();
3003 if( unlikely(p_meta == NULL) )
3004 return;
3006 demux_Control( p_demux, DEMUX_GET_META, p_meta );
3008 /* If metadata changed, then the attachments might have changed.
3009 We need to update them in case they contain album art. */
3010 input_attachment_t **attachment;
3011 int i_attachment;
3013 if( !demux_Control( p_demux, DEMUX_GET_ATTACHMENTS,
3014 &attachment, &i_attachment ) )
3016 vlc_mutex_lock( &input_priv(p_input)->p_item->lock );
3017 if( input_priv(p_input)->i_attachment > 0 )
3019 int j = 0;
3020 for( int i = 0; i < input_priv(p_input)->i_attachment; i++ )
3022 if( input_priv(p_input)->attachment_demux[i] == p_demux )
3023 vlc_input_attachment_Delete( input_priv(p_input)->attachment[i] );
3024 else
3026 input_priv(p_input)->attachment[j] = input_priv(p_input)->attachment[i];
3027 input_priv(p_input)->attachment_demux[j] = input_priv(p_input)->attachment_demux[i];
3028 j++;
3031 input_priv(p_input)->i_attachment = j;
3033 AppendAttachment( &input_priv(p_input)->i_attachment, &input_priv(p_input)->attachment, &input_priv(p_input)->attachment_demux,
3034 i_attachment, attachment, p_demux );
3035 vlc_mutex_unlock( &input_priv(p_input)->p_item->lock );
3038 es_out_ControlSetMeta( input_priv(p_input)->p_es_out, p_meta );
3039 vlc_meta_Delete( p_meta );
3042 /*****************************************************************************
3043 * InputGetExtraFiles
3044 * Autodetect extra input list
3045 *****************************************************************************/
3046 static void InputGetExtraFilesPattern( input_thread_t *p_input,
3047 int *pi_list, char ***pppsz_list,
3048 const char *psz_path,
3049 const char *psz_match,
3050 const char *psz_format,
3051 int i_start, int i_stop )
3053 int i_list;
3054 char **ppsz_list;
3055 TAB_INIT( i_list, ppsz_list );
3057 char *psz_base = strdup( psz_path );
3058 if( !psz_base )
3059 goto exit;
3061 /* Remove the extension */
3062 char *psz_end = &psz_base[strlen(psz_base)-strlen(psz_match)];
3063 assert( psz_end >= psz_base);
3064 *psz_end = '\0';
3066 /* Try to list files */
3067 for( int i = i_start; i <= i_stop; i++ )
3069 char *psz_probe;
3070 if( asprintf( &psz_probe, psz_format, psz_base, i ) < 0 )
3071 break;
3073 char *filepath = get_path( psz_probe );
3075 struct stat st;
3076 if( filepath == NULL ||
3077 vlc_stat( filepath, &st ) || !S_ISREG( st.st_mode ) || !st.st_size )
3079 free( filepath );
3080 free( psz_probe );
3081 break;
3084 msg_Dbg( p_input, "Detected extra file `%s'", filepath );
3086 char* psz_uri = vlc_path2uri( filepath, NULL );
3087 if( psz_uri )
3088 TAB_APPEND( i_list, ppsz_list, psz_uri );
3090 free( filepath );
3091 free( psz_probe );
3093 free( psz_base );
3094 exit:
3095 *pi_list = i_list;
3096 *pppsz_list = ppsz_list;
3099 static void InputGetExtraFiles( input_thread_t *p_input,
3100 int *pi_list, char ***pppsz_list,
3101 const char **ppsz_access, const char *psz_path )
3103 static const struct pattern
3105 const char *psz_access_force;
3106 const char *psz_match;
3107 const char *psz_format;
3108 int i_start;
3109 int i_stop;
3110 } patterns[] = {
3111 /* XXX the order is important */
3112 { "concat", ".001", "%s.%.3d", 2, 999 },
3113 { NULL, ".part1.rar","%s.part%.1d.rar", 2, 9 },
3114 { NULL, ".part01.rar","%s.part%.2d.rar", 2, 99, },
3115 { NULL, ".part001.rar", "%s.part%.3d.rar", 2, 999 },
3116 { NULL, ".rar", "%s.r%.2d", 0, 99 },
3119 TAB_INIT( *pi_list, *pppsz_list );
3121 if( ( **ppsz_access && strcmp( *ppsz_access, "file" ) ) || !psz_path )
3122 return;
3124 const size_t i_path = strlen(psz_path);
3126 for( size_t i = 0; i < ARRAY_SIZE( patterns ); ++i )
3128 const struct pattern* pat = &patterns[i];
3129 const size_t i_ext = strlen( pat->psz_match );
3131 if( i_path < i_ext )
3132 continue;
3134 if( !strcmp( &psz_path[i_path-i_ext], pat->psz_match ) )
3136 InputGetExtraFilesPattern( p_input, pi_list, pppsz_list, psz_path,
3137 pat->psz_match, pat->psz_format, pat->i_start, pat->i_stop );
3139 if( *pi_list > 0 && pat->psz_access_force )
3140 *ppsz_access = pat->psz_access_force;
3141 return;
3146 /* */
3147 static void input_ChangeState( input_thread_t *p_input, int i_state )
3149 if( input_priv(p_input)->i_state == i_state )
3150 return;
3152 input_priv(p_input)->i_state = i_state;
3153 if( i_state == ERROR_S )
3154 input_item_SetErrorWhenReading( input_priv(p_input)->p_item, true );
3155 input_SendEventState( p_input, i_state );
3159 /*****************************************************************************
3160 * MRLSplit: parse the access, demux and url part of the
3161 * Media Resource Locator.
3162 *****************************************************************************/
3163 void input_SplitMRL( const char **access, const char **demux,
3164 const char **path, const char **anchor, char *buf )
3166 char *p;
3168 /* Separate <path> from <access>[/<demux>]:// */
3169 p = strstr( buf, "://" );
3170 if( p != NULL )
3172 *p = '\0';
3173 p += 3; /* skips "://" */
3174 *path = p;
3176 /* Remove HTML anchor if present (not supported).
3177 * The hash symbol itself should be URI-encoded. */
3178 p = strchr( p, '#' );
3179 if( p != NULL )
3181 *(p++) = '\0';
3182 *anchor = p;
3184 else
3185 *anchor = "";
3187 else
3189 #ifndef NDEBUG
3190 fprintf( stderr, "%s(\"%s\") probably not a valid URI!\n", __func__,
3191 buf );
3192 #endif
3193 /* Note: this is a valid non const pointer to "": */
3194 *path = buf + strlen( buf );
3197 /* Separate access from demux */
3198 p = strchr( buf, '/' );
3199 if( p != NULL )
3201 *(p++) = '\0';
3202 if( p[0] == '$' )
3203 p++;
3204 *demux = p;
3206 else
3207 *demux = "";
3209 /* We really don't want module name substitution here! */
3210 p = buf;
3211 if( p[0] == '$' )
3212 p++;
3213 *access = p;
3216 static const char *MRLSeekPoint( const char *str, int *title, int *chapter )
3218 char *end;
3219 unsigned long u;
3221 /* Look for the title */
3222 u = strtoul( str, &end, 0 );
3223 *title = (str == end || u > (unsigned long)INT_MAX) ? -1 : (int)u;
3224 str = end;
3226 /* Look for the chapter */
3227 if( *str == ':' )
3229 str++;
3230 u = strtoul( str, &end, 0 );
3231 *chapter = (str == end || u > (unsigned long)INT_MAX) ? -1 : (int)u;
3232 str = end;
3234 else
3235 *chapter = -1;
3237 return str;
3241 /*****************************************************************************
3242 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3244 * Syntax:
3245 * [url][@[title_start][:chapter_start][-[title_end][:chapter_end]]]
3246 *****************************************************************************/
3247 static void MRLSections( const char *p,
3248 int *pi_title_start, int *pi_title_end,
3249 int *pi_chapter_start, int *pi_chapter_end )
3251 *pi_title_start = *pi_title_end = *pi_chapter_start = *pi_chapter_end = -1;
3253 int title_start, chapter_start, title_end, chapter_end;
3255 if( !p )
3256 return;
3258 if( *p != '-' )
3259 p = MRLSeekPoint( p, &title_start, &chapter_start );
3260 else
3261 title_start = chapter_start = -1;
3263 if( *p == '-' )
3264 p = MRLSeekPoint( p + 1, &title_end, &chapter_end );
3265 else
3266 title_end = chapter_end = -1;
3268 if( *p ) /* syntax error */
3269 return;
3271 *pi_title_start = title_start;
3272 *pi_title_end = title_end;
3273 *pi_chapter_start = chapter_start;
3274 *pi_chapter_end = chapter_end;
3277 static int input_SlaveSourceAdd( input_thread_t *p_input,
3278 enum slave_type i_type, const char *psz_uri,
3279 unsigned i_flags )
3281 vlc_value_t count;
3282 const char *psz_es;
3283 const char *psz_forced_demux;
3284 const bool b_can_fail = i_flags & SLAVE_ADD_CANFAIL;
3285 const bool b_forced = i_flags & SLAVE_ADD_FORCED;
3286 const bool b_set_time = i_flags & SLAVE_ADD_SET_TIME;
3288 switch( i_type )
3290 case SLAVE_TYPE_SPU:
3291 psz_es = "spu-es";
3292 psz_forced_demux = "subtitle";
3293 break;
3294 case SLAVE_TYPE_AUDIO:
3295 psz_es = "audio-es";
3296 psz_forced_demux = NULL;
3297 break;
3298 default:
3299 vlc_assert_unreachable();
3302 if( b_forced )
3303 var_Change( p_input, psz_es, VLC_VAR_CHOICESCOUNT, &count, NULL );
3305 msg_Dbg( p_input, "loading %s slave: %s (forced: %d)", psz_es, psz_uri,
3306 b_forced );
3308 input_source_t *p_source = InputSourceNew( p_input, psz_uri,
3309 psz_forced_demux,
3310 b_can_fail || psz_forced_demux );
3312 if( psz_forced_demux && p_source == NULL )
3313 p_source = InputSourceNew( p_input, psz_uri, NULL, b_can_fail );
3315 if( p_source == NULL )
3317 msg_Warn( p_input, "failed to add %s as slave", psz_uri );
3318 return VLC_EGENERIC;
3321 if( i_type == SLAVE_TYPE_AUDIO )
3323 if( b_set_time )
3325 int64_t i_time;
3327 /* Set position */
3328 if( demux_Control( input_priv(p_input)->master->p_demux,
3329 DEMUX_GET_TIME, &i_time ) )
3331 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
3332 InputSourceDestroy( p_source );
3333 return VLC_EGENERIC;
3336 if( demux_Control( p_source->p_demux,
3337 DEMUX_SET_TIME, i_time, true ) )
3339 msg_Err( p_input, "seek failed for new slave" );
3340 InputSourceDestroy( p_source );
3341 return VLC_EGENERIC;
3345 /* Get meta (access and demux) */
3346 InputUpdateMeta( p_input, p_source->p_demux );
3349 TAB_APPEND( input_priv(p_input)->i_slave, input_priv(p_input)->slave, p_source );
3351 if( !b_forced )
3352 return VLC_SUCCESS;
3354 /* Select the ES */
3355 vlc_value_t list;
3357 if( var_Change( p_input, psz_es, VLC_VAR_GETCHOICES, &list, NULL ) )
3358 return VLC_SUCCESS;
3360 if( count.i_int == 0 )
3361 count.i_int++;
3362 /* if it was first one, there is disable too */
3364 if( count.i_int < list.p_list->i_count )
3366 const int i_id = list.p_list->p_values[count.i_int].i_int;
3368 es_out_Control( input_priv(p_input)->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3369 es_out_Control( input_priv(p_input)->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3371 var_FreeList( &list, NULL );
3373 return VLC_SUCCESS;
3376 static char *input_SubtitleFile2Uri( input_thread_t *p_input,
3377 const char *psz_subtitle )
3379 /* if we are provided a subtitle.sub file,
3380 * see if we don't have a subtitle.idx and use it instead */
3381 char *psz_idxpath = NULL;
3382 char *psz_extension = strrchr( psz_subtitle, '.');
3383 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3385 psz_idxpath = strdup( psz_subtitle );
3386 if( psz_idxpath )
3388 struct stat st;
3390 psz_extension = psz_extension - psz_subtitle + psz_idxpath;
3391 strcpy( psz_extension, ".idx" );
3393 if( !vlc_stat( psz_idxpath, &st ) && S_ISREG( st.st_mode ) )
3395 msg_Dbg( p_input, "using %s as subtitle file instead of %s",
3396 psz_idxpath, psz_subtitle );
3397 psz_subtitle = psz_idxpath;
3402 char *psz_uri = vlc_path2uri( psz_subtitle, NULL );
3403 free( psz_idxpath );
3404 return psz_uri;
3407 /*****************************************************************************
3408 * Statistics
3409 *****************************************************************************/
3410 void input_UpdateStatistic( input_thread_t *p_input,
3411 input_statistic_t i_type, int i_delta )
3413 assert( input_priv(p_input)->i_state != INIT_S );
3415 vlc_mutex_lock( &input_priv(p_input)->counters.counters_lock);
3416 switch( i_type )
3418 #define I(c) stats_Update( input_priv(p_input)->counters.c, i_delta, NULL )
3419 case INPUT_STATISTIC_DECODED_VIDEO:
3420 I(p_decoded_video);
3421 break;
3422 case INPUT_STATISTIC_DECODED_AUDIO:
3423 I(p_decoded_audio);
3424 break;
3425 case INPUT_STATISTIC_DECODED_SUBTITLE:
3426 I(p_decoded_sub);
3427 break;
3428 case INPUT_STATISTIC_SENT_PACKET:
3429 I(p_sout_sent_packets);
3430 break;
3431 #undef I
3432 case INPUT_STATISTIC_SENT_BYTE:
3434 uint64_t bytes;
3436 stats_Update( input_priv(p_input)->counters.p_sout_sent_bytes, i_delta, &bytes );
3437 stats_Update( input_priv(p_input)->counters.p_sout_send_bitrate, bytes, NULL );
3438 break;
3440 default:
3441 msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3442 break;
3444 vlc_mutex_unlock( &input_priv(p_input)->counters.counters_lock);
3447 /**/
3448 /* TODO FIXME nearly the same logic that snapshot code */
3449 char *input_CreateFilename(input_thread_t *input, const char *dir,
3450 const char *filenamefmt, const char *ext)
3452 char *path;
3453 char *filename = str_format(input, filenamefmt);
3454 if (unlikely(filename == NULL))
3455 return NULL;
3457 filename_sanitize(filename);
3459 if (((ext != NULL)
3460 ? asprintf(&path, "%s"DIR_SEP"%s.%s", dir, filename, ext)
3461 : asprintf(&path, "%s"DIR_SEP"%s", dir, filename)) < 0)
3462 path = NULL;
3464 free(filename);
3465 return path;