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