1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
40 #include "input_internal.h"
43 #include "es_out_timeshift.h"
51 #include <vlc_dialog.h>
53 #include <vlc_charset.h>
55 #include <vlc_strings.h>
56 #include <vlc_modules.h>
58 /*****************************************************************************
60 *****************************************************************************/
61 static void Destructor( input_thread_t * p_input );
63 static void *Run ( void * );
65 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
66 const char *, bool, input_resource_t * );
67 static int Init ( input_thread_t *p_input );
68 static void End ( input_thread_t *p_input );
69 static void MainLoop( input_thread_t *p_input, bool b_interactive );
71 static inline int ControlPop( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline, bool b_postpone_seek );
72 static void ControlRelease( int i_type, vlc_value_t val );
73 static bool ControlIsSeekRequest( int i_type );
74 static bool Control( input_thread_t *, int, vlc_value_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 *);
83 static int InputSourceInit( input_thread_t *, input_source_t *,
84 const char *, const char *psz_forced_demux,
86 static void InputSourceClean( input_source_t * );
87 static void InputSourceMeta( input_thread_t *, input_source_t *, vlc_meta_t * );
90 //static void InputGetAttachments( input_thread_t *, input_source_t * );
91 static void SlaveDemux( input_thread_t *p_input, bool *pb_demux_polled );
92 static void SlaveSeek( input_thread_t *p_input );
94 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
95 static void InputUpdateMeta( input_thread_t *p_input, demux_t *p_demux );
96 static void InputGetExtraFiles( input_thread_t *p_input,
97 int *pi_list, char ***pppsz_list,
98 const char *psz_access, const char *psz_path );
100 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment, demux_t ***ppp_attachment_demux,
101 int i_new, input_attachment_t **pp_new, demux_t *p_demux );
109 static void input_SubtitleAdd( input_thread_t *, const char *, unsigned );
110 static void input_SubtitleFileAdd( input_thread_t *, char *, unsigned );
111 static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */
115 * Create a new input_thread_t.
117 * You need to call input_Start on it when you are done
118 * adding callback on the variables/events you want to monitor.
120 * \param p_parent a vlc_object
121 * \param p_item an input item
122 * \param psz_log an optional prefix for this input logs
123 * \param p_resource an optional input ressource
124 * \return a pointer to the spawned input thread
126 input_thread_t *input_Create( vlc_object_t *p_parent,
127 input_item_t *p_item,
128 const char *psz_log, input_resource_t *p_resource )
130 return Create( p_parent, p_item, psz_log, false, p_resource );
133 #undef input_CreateAndStart
135 * Create a new input_thread_t and start it.
137 * Provided for convenience.
141 input_thread_t *input_CreateAndStart( vlc_object_t *p_parent,
142 input_item_t *p_item, const char *psz_log )
144 input_thread_t *p_input = input_Create( p_parent, p_item, psz_log, NULL );
146 if( input_Start( p_input ) )
148 vlc_object_release( p_input );
156 * Initialize an input thread and run it until it stops by itself.
158 * \param p_parent a vlc_object
159 * \param p_item an input item
160 * \return an error code, VLC_SUCCESS on success
162 int input_Read( vlc_object_t *p_parent, input_item_t *p_item )
164 input_thread_t *p_input = Create( p_parent, p_item, NULL, false, NULL );
168 if( !Init( p_input ) )
170 MainLoop( p_input, false );
174 vlc_object_release( p_input );
179 * Initialize an input and initialize it to preparse the item
180 * This function is blocking. It will only accept parsing regular files.
182 * \param p_parent a vlc_object_t
183 * \param p_item an input item
184 * \return VLC_SUCCESS or an error
186 int input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
188 input_thread_t *p_input;
190 /* Allocate descriptor */
191 p_input = Create( p_parent, p_item, NULL, true, NULL );
195 if( !Init( p_input ) ) {
196 /* if the demux is a playlist, call Mainloop that will call
197 * demux_Demux in order to fetch sub items */
198 bool b_is_playlist = false;
200 if ( demux_Control( p_input->p->input.p_demux,
203 b_is_playlist = false;
205 MainLoop( p_input, false );
209 vlc_object_release( p_input );
215 * Start a input_thread_t created by input_Create.
217 * You must not start an already running input_thread_t.
219 * \param the input thread to start
221 int input_Start( input_thread_t *p_input )
223 /* Create thread and wait for its readiness. */
224 p_input->p->is_running = !vlc_clone( &p_input->p->thread,
225 Run, p_input, VLC_THREAD_PRIORITY_INPUT );
226 if( !p_input->p->is_running )
228 input_ChangeState( p_input, ERROR_S );
229 msg_Err( p_input, "cannot create input thread" );
236 * Request a running input thread to stop and die
238 * \param p_input the input thread to stop
240 void input_Stop( input_thread_t *p_input )
242 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
243 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
244 * unlock the control loop */
245 ObjectKillChildrens( VLC_OBJECT(p_input) );
246 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
249 void input_Join( input_thread_t *p_input )
251 if( p_input->p->is_running )
252 vlc_join( p_input->p->thread, NULL );
255 void input_Release( input_thread_t *p_input )
257 vlc_object_release( p_input );
263 * It does not call input_Stop itself.
265 void input_Close( input_thread_t *p_input )
267 input_Join( p_input );
268 input_Release( p_input );
272 * Get the item from an input thread
273 * FIXME it does not increase ref count of the item.
274 * if it is used after p_input is destroyed nothing prevent it from
277 input_item_t *input_GetItem( input_thread_t *p_input )
279 assert( p_input && p_input->p );
280 return p_input->p->p_item;
283 /*****************************************************************************
284 * This function creates a new input, and returns a pointer
285 * to its description. On error, it returns NULL.
287 * XXX Do not forget to update vlc_input.h if you add new variables.
288 *****************************************************************************/
289 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
290 const char *psz_header, bool b_quick,
291 input_resource_t *p_resource )
293 input_thread_t *p_input = NULL; /* thread descriptor */
296 /* Allocate descriptor */
297 p_input = vlc_custom_create( p_parent, sizeof( *p_input ), "input" );
298 if( p_input == NULL )
301 /* Construct a nice name for the input timer */
302 char psz_timer_name[255];
303 char * psz_name = input_item_GetName( p_item );
304 snprintf( psz_timer_name, sizeof(psz_timer_name),
305 "input launching for '%s'", psz_name );
307 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
311 p_input->p = calloc( 1, sizeof( input_thread_private_t ) );
314 vlc_object_release( p_input );
318 /* Parse input options */
319 vlc_mutex_lock( &p_item->lock );
320 assert( (int)p_item->optflagc == p_item->i_options );
321 for( i = 0; i < p_item->i_options; i++ )
322 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
323 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
324 vlc_mutex_unlock( &p_item->lock );
326 p_input->b_preparsing = b_quick;
327 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
329 /* Init Common fields */
330 p_input->b_eof = false;
331 p_input->p->b_can_pace_control = true;
332 p_input->p->i_start = 0;
333 p_input->p->i_time = 0;
334 p_input->p->i_stop = 0;
335 p_input->p->i_run = 0;
336 p_input->p->i_title = 0;
337 p_input->p->title = NULL;
338 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
339 p_input->p->i_state = INIT_S;
340 p_input->p->i_rate = INPUT_RATE_DEFAULT;
341 p_input->p->b_recording = false;
342 memset( &p_input->p->bookmark, 0, sizeof(p_input->p->bookmark) );
343 TAB_INIT( p_input->p->i_bookmark, p_input->p->pp_bookmark );
344 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
345 p_input->p->attachment_demux = NULL;
346 p_input->p->p_sout = NULL;
347 p_input->p->b_out_pace_control = false;
349 vlc_gc_incref( p_item ); /* Released in Destructor() */
350 p_input->p->p_item = p_item;
352 /* Init Input fields */
353 p_input->p->input.p_demux = NULL;
354 p_input->p->input.b_title_demux = false;
355 p_input->p->input.i_title = 0;
356 p_input->p->input.title = NULL;
357 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
358 p_input->p->input.b_can_pace_control = true;
359 p_input->p->input.b_can_rate_control = true;
360 p_input->p->input.b_rescale_ts = true;
361 p_input->p->input.b_eof = false;
363 vlc_mutex_lock( &p_item->lock );
365 if( !p_item->p_stats )
366 p_item->p_stats = stats_NewInputStats( p_input );
367 vlc_mutex_unlock( &p_item->lock );
370 p_input->p->i_slave = 0;
371 p_input->p->slave = NULL;
376 p_input->p->p_resource_private = NULL;
377 p_input->p->p_resource = input_resource_Hold( p_resource );
381 p_input->p->p_resource_private = input_resource_New( VLC_OBJECT( p_input ) );
382 p_input->p->p_resource = input_resource_Hold( p_input->p->p_resource_private );
384 input_resource_SetInput( p_input->p->p_resource, p_input );
386 /* Init control buffer */
387 vlc_mutex_init( &p_input->p->lock_control );
388 vlc_cond_init( &p_input->p->wait_control );
389 p_input->p->i_control = 0;
390 p_input->p->is_running = false;
392 /* Create Object Variables for private use only */
393 input_ConfigVarInit( p_input );
395 /* Create Objects variables for public Get and Set */
396 input_ControlVarInit( p_input );
399 if( !p_input->b_preparsing )
401 char *psz_bookmarks = var_GetNonEmptyString( p_input, "bookmarks" );
404 /* FIXME: have a common cfg parsing routine used by sout and others */
405 char *psz_parser, *psz_start, *psz_end;
406 psz_parser = psz_bookmarks;
407 while( (psz_start = strchr( psz_parser, '{' ) ) )
409 seekpoint_t *p_seekpoint;
412 psz_end = strchr( psz_start, '}' );
413 if( !psz_end ) break;
414 psz_parser = psz_end + 1;
415 backup = *psz_parser;
419 p_seekpoint = vlc_seekpoint_New();
420 while( (psz_end = strchr( psz_start, ',' ) ) )
423 if( !strncmp( psz_start, "name=", 5 ) )
425 p_seekpoint->psz_name = strdup(psz_start + 5);
427 else if( !strncmp( psz_start, "bytes=", 6 ) )
429 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
431 else if( !strncmp( psz_start, "time=", 5 ) )
433 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
436 psz_start = psz_end + 1;
438 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
439 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
440 p_seekpoint->i_time_offset );
441 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
442 vlc_seekpoint_Delete( p_seekpoint );
443 *psz_parser = backup;
445 free( psz_bookmarks );
449 /* Remove 'Now playing' info as it is probably outdated */
450 input_item_SetNowPlaying( p_item, NULL );
451 input_item_SetESNowPlaying( p_item, NULL );
452 input_SendEventMeta( p_input );
455 if( p_input->b_preparsing )
456 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
458 /* Make sure the interaction option is honored */
459 if( !var_InheritBool( p_input, "interact" ) )
460 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
463 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
464 vlc_mutex_init( &p_input->p->counters.counters_lock );
466 p_input->p->p_es_out_display = input_EsOutNew( p_input, p_input->p->i_rate );
467 p_input->p->p_es_out = NULL;
469 /* Set the destructor when we are sure we are initialized */
470 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
476 * Input destructor (called when the object's refcount reaches 0).
478 static void Destructor( input_thread_t * p_input )
481 char * psz_name = input_item_GetName( p_input->p->p_item );
482 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
486 if( p_input->p->p_es_out_display )
487 es_out_Delete( p_input->p->p_es_out_display );
489 if( p_input->p->p_resource )
490 input_resource_Release( p_input->p->p_resource );
491 if( p_input->p->p_resource_private )
492 input_resource_Release( p_input->p->p_resource_private );
494 vlc_gc_decref( p_input->p->p_item );
496 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
498 for( int i = 0; i < p_input->p->i_control; i++ )
500 input_control_t *p_ctrl = &p_input->p->control[i];
501 ControlRelease( p_ctrl->i_type, p_ctrl->val );
504 vlc_cond_destroy( &p_input->p->wait_control );
505 vlc_mutex_destroy( &p_input->p->lock_control );
509 /*****************************************************************************
510 * Run: main thread loop
511 * This is the "normal" thread that spawns the input processing chain,
512 * reads the stream, cleans up and waits
513 *****************************************************************************/
514 static void *Run( void *obj )
516 input_thread_t *p_input = (input_thread_t *)obj;
517 const int canc = vlc_savecancel();
519 if( !Init( p_input ) )
521 MainLoop( p_input, true ); /* FIXME it can be wrong (like with VLM) */
527 input_SendEventDead( p_input );
529 vlc_restorecancel( canc );
533 /*****************************************************************************
534 * Main loop: Fill buffers from access, and demux
535 *****************************************************************************/
539 * It asks the demuxer to demux some data
541 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, bool *pb_demux_polled, mtime_t i_start_mdate )
546 *pb_demux_polled = p_input->p->input.p_demux->pf_demux != NULL;
548 if( ( p_input->p->i_stop > 0 && p_input->p->i_time >= p_input->p->i_stop ) ||
549 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
552 i_ret = demux_Demux( p_input->p->input.p_demux );
556 if( p_input->p->input.p_demux->info.i_update )
558 if( p_input->p->input.p_demux->info.i_update & INPUT_UPDATE_TITLE_LIST )
560 UpdateTitleListfromDemux( p_input );
561 p_input->p->input.p_demux->info.i_update &= ~INPUT_UPDATE_TITLE_LIST;
563 if( p_input->p->input.b_title_demux )
565 i_ret = UpdateTitleSeekpointFromDemux( p_input );
568 UpdateGenericFromDemux( p_input );
572 if( i_ret == 0 ) /* EOF */
574 msg_Dbg( p_input, "EOF reached" );
575 p_input->p->input.b_eof = true;
576 es_out_Eos(p_input->p->p_es_out);
580 input_ChangeState( p_input, ERROR_S );
583 if( i_ret > 0 && p_input->p->i_slave > 0 )
586 SlaveDemux( p_input, &b_demux_polled );
588 *pb_demux_polled |= b_demux_polled;
592 static int MainLoopTryRepeat( input_thread_t *p_input, mtime_t *pi_start_mdate )
594 int i_repeat = var_GetInteger( p_input, "input-repeat" );
600 msg_Dbg( p_input, "repeating the same input (%d)", i_repeat );
604 var_SetInteger( p_input, "input-repeat", i_repeat );
607 /* Seek to start title/seekpoint */
608 val.i_int = p_input->p->input.i_title_start -
609 p_input->p->input.i_title_offset;
610 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
612 input_ControlPush( p_input,
613 INPUT_CONTROL_SET_TITLE, &val );
615 val.i_int = p_input->p->input.i_seekpoint_start -
616 p_input->p->input.i_seekpoint_offset;
617 if( val.i_int > 0 /* TODO: check upper boundary */ )
618 input_ControlPush( p_input,
619 INPUT_CONTROL_SET_SEEKPOINT, &val );
621 /* Seek to start position */
622 if( p_input->p->i_start > 0 )
624 val.i_time = p_input->p->i_start;
625 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &val );
630 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION, &val );
634 *pi_start_mdate = mdate();
639 * Update timing infos and statistics.
641 static void MainLoopStatistics( input_thread_t *p_input )
643 double f_position = 0.0;
645 mtime_t i_length = 0;
647 /* update input status variables */
648 if( demux_Control( p_input->p->input.p_demux,
649 DEMUX_GET_POSITION, &f_position ) )
652 if( demux_Control( p_input->p->input.p_demux,
653 DEMUX_GET_TIME, &i_time ) )
655 p_input->p->i_time = i_time;
657 if( demux_Control( p_input->p->input.p_demux,
658 DEMUX_GET_LENGTH, &i_length ) )
661 es_out_SetTimes( p_input->p->p_es_out, f_position, i_time, i_length );
663 /* update current bookmark */
664 vlc_mutex_lock( &p_input->p->p_item->lock );
665 p_input->p->bookmark.i_time_offset = i_time;
666 p_input->p->bookmark.i_byte_offset = -1;
667 vlc_mutex_unlock( &p_input->p->p_item->lock );
669 stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
670 input_SendEventStatistics( p_input );
675 * The main input loop.
677 static void MainLoop( input_thread_t *p_input, bool b_interactive )
679 mtime_t i_start_mdate = mdate();
680 mtime_t i_intf_update = 0;
681 mtime_t i_last_seek_mdate = 0;
682 bool b_pause_after_eof = b_interactive &&
683 var_CreateGetBool( p_input, "play-and-pause" );
685 while( vlc_object_alive( p_input ) && !p_input->b_error )
687 mtime_t i_wakeup = 0;
688 bool b_demux_polled = true;
689 bool b_force_update = false;
690 bool b_paused = p_input->p->i_state == PAUSE_S;
691 /* FIXME if p_input->p->i_state == PAUSE_S the access/access_demux
692 * is paused -> this may cause problem with some of them
693 * The same problem can be seen when seeking while paused */
695 b_paused = !es_out_GetBuffering( p_input->p->p_es_out ) || p_input->p->input.b_eof;
699 if( !p_input->p->input.b_eof )
701 MainLoopDemux( p_input, &b_force_update, &b_demux_polled, i_start_mdate );
703 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
705 else if( !es_out_GetEmpty( p_input->p->p_es_out ) )
707 msg_Dbg( p_input, "waiting decoder fifos to empty" );
708 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
710 /* Pause after eof only if the input is pausable.
711 * This way we won't trigger timeshifting for nothing */
712 else if( b_pause_after_eof && p_input->p->b_can_pause )
714 vlc_value_t val = { .i_int = PAUSE_S };
716 msg_Dbg( p_input, "pausing at EOF (pause after each)");
717 Control( p_input, INPUT_CONTROL_SET_STATE, val );
723 if( MainLoopTryRepeat( p_input, &i_start_mdate ) )
725 b_pause_after_eof = var_GetBool( p_input, "play-and-pause" );
733 mtime_t i_deadline = i_wakeup;
734 if( b_paused || !b_demux_polled )
735 i_deadline = i_intf_update;
740 mtime_t i_limit = i_deadline;
742 /* Postpone seeking until ES buffering is complete or at most
744 bool b_buffering = es_out_GetBuffering( p_input->p->p_es_out )
745 && !p_input->p->input.b_eof;
748 mtime_t now = mdate();
750 /* Recheck ES buffer level every 20 ms when seeking */
751 if( now < i_last_seek_mdate + INT64_C(125000)
752 && i_deadline > now + INT64_C(20000) )
753 i_limit = now + INT64_C(20000);
759 if( ControlPop( p_input, &i_type, &val, i_limit, b_buffering ) )
761 if( b_buffering && i_limit < i_deadline )
767 msg_Dbg( p_input, "control type=%d", i_type );
770 if( Control( p_input, i_type, val ) )
772 if( ControlIsSeekRequest( i_type ) )
773 i_last_seek_mdate = mdate();
774 b_force_update = true;
778 /* Update interface and statistics */
780 if( now >= i_intf_update || b_force_update )
782 MainLoopStatistics( p_input );
783 i_intf_update = now + INT64_C(250000);
784 b_force_update = false;
787 /* Update the wakeup time */
789 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
790 } while( now < i_wakeup );
793 if( !p_input->b_error )
794 input_ChangeState( p_input, END_S );
797 static void InitStatistics( input_thread_t * p_input )
799 if( p_input->b_preparsing ) return;
801 /* Prepare statistics */
802 #define INIT_COUNTER( c, compute ) p_input->p->counters.p_##c = \
803 stats_CounterCreate( STATS_##compute);
804 if( libvlc_stats( p_input ) )
806 INIT_COUNTER( read_bytes, COUNTER );
807 INIT_COUNTER( read_packets, COUNTER );
808 INIT_COUNTER( demux_read, COUNTER );
809 INIT_COUNTER( input_bitrate, DERIVATIVE );
810 INIT_COUNTER( demux_bitrate, DERIVATIVE );
811 INIT_COUNTER( demux_corrupted, COUNTER );
812 INIT_COUNTER( demux_discontinuity, COUNTER );
813 INIT_COUNTER( played_abuffers, COUNTER );
814 INIT_COUNTER( lost_abuffers, COUNTER );
815 INIT_COUNTER( displayed_pictures, COUNTER );
816 INIT_COUNTER( lost_pictures, COUNTER );
817 INIT_COUNTER( decoded_audio, COUNTER );
818 INIT_COUNTER( decoded_video, COUNTER );
819 INIT_COUNTER( decoded_sub, COUNTER );
820 p_input->p->counters.p_sout_send_bitrate = NULL;
821 p_input->p->counters.p_sout_sent_packets = NULL;
822 p_input->p->counters.p_sout_sent_bytes = NULL;
827 static int InitSout( input_thread_t * p_input )
829 if( p_input->b_preparsing )
832 /* Find a usable sout and attach it to p_input */
833 char *psz = var_GetNonEmptyString( p_input, "sout" );
834 if( psz && strncasecmp( p_input->p->p_item->psz_uri, "vlc:", 4 ) )
836 p_input->p->p_sout = input_resource_RequestSout( p_input->p->p_resource, NULL, psz );
837 if( !p_input->p->p_sout )
839 input_ChangeState( p_input, ERROR_S );
840 msg_Err( p_input, "cannot start stream output instance, " \
845 if( libvlc_stats( p_input ) )
847 INIT_COUNTER( sout_sent_packets, COUNTER );
848 INIT_COUNTER( sout_sent_bytes, COUNTER );
849 INIT_COUNTER( sout_send_bitrate, DERIVATIVE );
854 input_resource_RequestSout( p_input->p->p_resource, NULL, NULL );
862 static void InitTitle( input_thread_t * p_input )
864 input_source_t *p_master = &p_input->p->input;
866 if( p_input->b_preparsing )
869 vlc_mutex_lock( &p_input->p->p_item->lock );
870 /* Create global title (from master) */
871 p_input->p->i_title = p_master->i_title;
872 p_input->p->title = p_master->title;
873 p_input->p->i_title_offset = p_master->i_title_offset;
874 p_input->p->i_seekpoint_offset = p_master->i_seekpoint_offset;
875 if( p_input->p->i_title > 0 )
877 /* Setup variables */
878 input_ControlVarNavigation( p_input );
879 input_SendEventTitle( p_input, 0 );
883 p_input->p->b_can_pace_control = p_master->b_can_pace_control;
884 p_input->p->b_can_pause = p_master->b_can_pause;
885 p_input->p->b_can_rate_control = p_master->b_can_rate_control;
886 vlc_mutex_unlock( &p_input->p->p_item->lock );
889 static void StartTitle( input_thread_t * p_input )
893 /* Start title/chapter */
894 val.i_int = p_input->p->input.i_title_start -
895 p_input->p->input.i_title_offset;
896 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
897 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
899 val.i_int = p_input->p->input.i_seekpoint_start -
900 p_input->p->input.i_seekpoint_offset;
901 if( val.i_int > 0 /* TODO: check upper boundary */ )
902 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
904 /* Start/stop/run time */
905 p_input->p->i_start = llroundf(1000000.f
906 * var_GetFloat( p_input, "start-time" ));
907 p_input->p->i_stop = llroundf(1000000.f
908 * var_GetFloat( p_input, "stop-time" ));
909 p_input->p->i_run = llroundf(1000000.f
910 * var_GetFloat( p_input, "run-time" ));
911 if( p_input->p->i_run < 0 )
913 msg_Warn( p_input, "invalid run-time ignored" );
914 p_input->p->i_run = 0;
917 if( p_input->p->i_start > 0 )
921 msg_Dbg( p_input, "starting at time: %ds",
922 (int)( p_input->p->i_start / CLOCK_FREQ ) );
924 s.i_time = p_input->p->i_start;
925 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
927 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
929 msg_Warn( p_input, "invalid stop-time ignored" );
930 p_input->p->i_stop = 0;
932 p_input->p->b_fast_seek = var_GetBool( p_input, "input-fast-seek" );
935 static void LoadSubtitles( input_thread_t *p_input )
938 /* Get fps and set it if not already set */
939 const float f_fps = p_input->p->f_fps;
942 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
943 var_SetFloat( p_input, "sub-original-fps", f_fps );
945 float f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
946 if( f_requested_fps != f_fps )
948 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
950 var_SetFloat( p_input, "sub-fps", f_requested_fps );
954 const int i_delay = var_CreateGetInteger( p_input, "sub-delay" );
956 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
958 /* Look for and add subtitle files */
959 unsigned i_flags = SUB_FORCED;
961 char *psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
962 if( psz_subtitle != NULL )
964 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
965 input_SubtitleFileAdd( p_input, psz_subtitle, i_flags );
966 i_flags = SUB_NOFLAG;
969 if( var_GetBool( p_input, "sub-autodetect-file" ) )
971 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
972 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
973 p_input->p->p_item->psz_uri );
974 free( psz_autopath );
976 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
978 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
980 i_flags |= SUB_CANFAIL;
981 input_SubtitleFileAdd( p_input, ppsz_subs[i], i_flags );
982 i_flags = SUB_NOFLAG;
985 free( ppsz_subs[i] );
989 free( psz_subtitle );
991 /* Load subtitles from attachments */
992 int i_attachment = 0;
993 input_attachment_t **pp_attachment = NULL;
995 vlc_mutex_lock( &p_input->p->p_item->lock );
996 for( int i = 0; i < p_input->p->i_attachment; i++ )
998 const input_attachment_t *a = p_input->p->attachment[i];
999 if( !strcmp( a->psz_mime, "application/x-srt" ) )
1000 TAB_APPEND( i_attachment, pp_attachment,
1001 vlc_input_attachment_New( a->psz_name, NULL,
1002 a->psz_description, NULL, 0 ) );
1004 vlc_mutex_unlock( &p_input->p->p_item->lock );
1006 if( i_attachment > 0 )
1007 var_Create( p_input, "sub-description", VLC_VAR_STRING );
1008 for( int i = 0; i < i_attachment; i++ )
1010 input_attachment_t *a = pp_attachment[i];
1014 if( a->psz_name[i] &&
1015 asprintf( &psz_mrl, "attachment://%s", a->psz_name ) >= 0 )
1017 var_SetString( p_input, "sub-description", a->psz_description ? a->psz_description : "");
1019 input_SubtitleAdd( p_input, psz_mrl, i_flags );
1021 i_flags = SUB_NOFLAG;
1024 vlc_input_attachment_Delete( a );
1026 free( pp_attachment );
1027 if( i_attachment > 0 )
1028 var_Destroy( p_input, "sub-description" );
1031 static void LoadSlaves( input_thread_t *p_input )
1033 char *psz = var_GetNonEmptyString( p_input, "input-slave" );
1037 char *psz_org = psz;
1038 while( psz && *psz )
1040 while( *psz == ' ' || *psz == '#' )
1043 char *psz_delim = strchr( psz, '#' );
1045 *psz_delim++ = '\0';
1050 char *uri = strstr(psz, "://")
1051 ? strdup( psz ) : vlc_path2uri( psz, NULL );
1055 msg_Dbg( p_input, "adding slave input '%s'", uri );
1057 input_source_t *p_slave = InputSourceNew( p_input );
1058 if( p_slave && !InputSourceInit( p_input, p_slave, uri, NULL, false ) )
1059 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, p_slave );
1067 static void UpdatePtsDelay( input_thread_t *p_input )
1069 input_thread_private_t *p_sys = p_input->p;
1071 /* Get max pts delay from input source */
1072 mtime_t i_pts_delay = p_sys->input.i_pts_delay;
1073 for( int i = 0; i < p_sys->i_slave; i++ )
1074 i_pts_delay = __MAX( i_pts_delay, p_sys->slave[i]->i_pts_delay );
1076 if( i_pts_delay < 0 )
1079 /* Take care of audio/spu delay */
1080 const mtime_t i_audio_delay = var_GetTime( p_input, "audio-delay" );
1081 const mtime_t i_spu_delay = var_GetTime( p_input, "spu-delay" );
1082 const mtime_t i_extra_delay = __MIN( i_audio_delay, i_spu_delay );
1083 if( i_extra_delay < 0 )
1084 i_pts_delay -= i_extra_delay;
1086 /* Update cr_average depending on the caching */
1087 const int i_cr_average = var_GetInteger( p_input, "cr-average" ) * i_pts_delay / DEFAULT_PTS_DELAY;
1090 es_out_SetDelay( p_input->p->p_es_out_display, AUDIO_ES, i_audio_delay );
1091 es_out_SetDelay( p_input->p->p_es_out_display, SPU_ES, i_spu_delay );
1092 es_out_SetJitter( p_input->p->p_es_out, i_pts_delay, 0, i_cr_average );
1095 static void InitPrograms( input_thread_t * p_input )
1100 /* Compute correct pts_delay */
1101 UpdatePtsDelay( p_input );
1104 i_es_out_mode = ES_OUT_MODE_AUTO;
1105 if( p_input->p->p_sout )
1109 if( (prgms = var_GetNonEmptyString( p_input, "programs" )) != NULL )
1113 TAB_INIT( list.i_count, list.p_values );
1114 for( const char *prgm = strtok_r( prgms, ",", &buf );
1116 prgm = strtok_r( NULL, ",", &buf ) )
1118 vlc_value_t val = { .i_int = atoi( prgm ) };
1119 INSERT_ELEM( list.p_values, list.i_count, list.i_count, val );
1122 if( list.i_count > 0 )
1123 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1124 /* Note : we should remove the "program" callback. */
1128 else if( var_GetBool( p_input, "sout-all" ) )
1130 i_es_out_mode = ES_OUT_MODE_ALL;
1133 es_out_SetMode( p_input->p->p_es_out, i_es_out_mode );
1135 /* Inform the demuxer about waited group (needed only for DVB) */
1136 if( i_es_out_mode == ES_OUT_MODE_ALL )
1138 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1140 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1142 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1144 TAB_CLEAN( list.i_count, list.p_values );
1148 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1149 es_out_GetGroupForced( p_input->p->p_es_out ), NULL );
1153 static int Init( input_thread_t * p_input )
1155 for( int i = 0; i < p_input->p->p_item->i_options; i++ )
1157 if( !strncmp( p_input->p->p_item->ppsz_options[i], "meta-file", 9 ) )
1159 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1160 var_SetString( p_input, "sout", "" );
1161 var_SetBool( p_input, "sout-all", false );
1162 var_SetString( p_input, "input-slave", "" );
1163 var_SetInteger( p_input, "input-repeat", 0 );
1164 var_SetString( p_input, "sub-file", "" );
1165 var_SetBool( p_input, "sub-autodetect-file", false );
1169 InitStatistics( p_input );
1171 if( InitSout( p_input ) )
1176 p_input->p->p_es_out = input_EsOutTimeshiftNew( p_input, p_input->p->p_es_out_display, p_input->p->i_rate );
1179 input_ChangeState( p_input, OPENING_S );
1180 input_SendEventCache( p_input, 0.0 );
1183 if( InputSourceInit( p_input, &p_input->p->input,
1184 p_input->p->p_item->psz_uri, NULL, false ) )
1189 InitTitle( p_input );
1191 /* Load master infos */
1194 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1198 i_length = input_item_GetDuration( p_input->p->p_item );
1199 input_SendEventLength( p_input, i_length );
1201 input_SendEventPosition( p_input, 0.0, 0 );
1203 if( !p_input->b_preparsing )
1205 StartTitle( p_input );
1206 LoadSubtitles( p_input );
1207 LoadSlaves( p_input );
1208 InitPrograms( p_input );
1210 double f_rate = var_InheritFloat( p_input, "rate" );
1211 if( f_rate != 0.0 && f_rate != 1.0 )
1213 vlc_value_t val = { .i_int = INPUT_RATE_DEFAULT / f_rate };
1214 input_ControlPush( p_input, INPUT_CONTROL_SET_RATE, &val );
1218 if( !p_input->b_preparsing && p_input->p->p_sout )
1220 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1222 if( p_input->p->b_can_pace_control && p_input->p->b_out_pace_control )
1224 /* We don't want a high input priority here or we'll
1225 * end-up sucking up all the CPU time */
1226 vlc_set_priority( p_input->p->thread, VLC_THREAD_PRIORITY_LOW );
1229 msg_Dbg( p_input, "starting in %s mode",
1230 p_input->p->b_out_pace_control ? "async" : "sync" );
1233 vlc_meta_t *p_meta = vlc_meta_New();
1234 if( p_meta != NULL )
1236 /* Get meta data from users */
1237 InputMetaUser( p_input, p_meta );
1239 /* Get meta data from master input */
1240 InputSourceMeta( p_input, &p_input->p->input, p_meta );
1242 /* And from slave */
1243 for( int i = 0; i < p_input->p->i_slave; i++ )
1244 InputSourceMeta( p_input, p_input->p->slave[i], p_meta );
1246 es_out_ControlSetMeta( p_input->p->p_es_out, p_meta );
1247 vlc_meta_Delete( p_meta );
1250 msg_Dbg( p_input, "`%s' successfully opened",
1251 p_input->p->p_item->psz_uri );
1253 /* initialization is complete */
1254 input_ChangeState( p_input, PLAYING_S );
1259 input_ChangeState( p_input, ERROR_S );
1261 if( p_input->p->p_es_out )
1262 es_out_Delete( p_input->p->p_es_out );
1263 es_out_SetMode( p_input->p->p_es_out_display, ES_OUT_MODE_END );
1264 if( p_input->p->p_resource )
1266 if( p_input->p->p_sout )
1267 input_resource_RequestSout( p_input->p->p_resource,
1268 p_input->p->p_sout, NULL );
1269 input_resource_SetInput( p_input->p->p_resource, NULL );
1270 if( p_input->p->p_resource_private )
1271 input_resource_Terminate( p_input->p->p_resource_private );
1274 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1276 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1277 stats_CounterClean( p_input->p->counters.p_##c );\
1278 p_input->p->counters.p_##c = NULL; } while(0)
1279 EXIT_COUNTER( read_bytes );
1280 EXIT_COUNTER( read_packets );
1281 EXIT_COUNTER( demux_read );
1282 EXIT_COUNTER( input_bitrate );
1283 EXIT_COUNTER( demux_bitrate );
1284 EXIT_COUNTER( demux_corrupted );
1285 EXIT_COUNTER( demux_discontinuity );
1286 EXIT_COUNTER( played_abuffers );
1287 EXIT_COUNTER( lost_abuffers );
1288 EXIT_COUNTER( displayed_pictures );
1289 EXIT_COUNTER( lost_pictures );
1290 EXIT_COUNTER( decoded_audio );
1291 EXIT_COUNTER( decoded_video );
1292 EXIT_COUNTER( decoded_sub );
1294 if( p_input->p->p_sout )
1296 EXIT_COUNTER( sout_sent_packets );
1297 EXIT_COUNTER( sout_sent_bytes );
1298 EXIT_COUNTER( sout_send_bitrate );
1303 /* Mark them deleted */
1304 p_input->p->input.p_demux = NULL;
1305 p_input->p->p_es_out = NULL;
1306 p_input->p->p_sout = NULL;
1308 return VLC_EGENERIC;
1311 /*****************************************************************************
1312 * End: end the input thread
1313 *****************************************************************************/
1314 static void End( input_thread_t * p_input )
1318 /* We are at the end */
1319 input_ChangeState( p_input, END_S );
1321 /* Clean control variables */
1322 input_ControlVarStop( p_input );
1324 /* Stop es out activity */
1325 es_out_SetMode( p_input->p->p_es_out, ES_OUT_MODE_NONE );
1327 /* Clean up master */
1328 InputSourceClean( &p_input->p->input );
1331 for( i = 0; i < p_input->p->i_slave; i++ )
1333 InputSourceClean( p_input->p->slave[i] );
1334 free( p_input->p->slave[i] );
1336 free( p_input->p->slave );
1338 /* Unload all modules */
1339 if( p_input->p->p_es_out )
1340 es_out_Delete( p_input->p->p_es_out );
1341 es_out_SetMode( p_input->p->p_es_out_display, ES_OUT_MODE_END );
1343 if( !p_input->b_preparsing )
1345 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1346 if( libvlc_stats( p_input ) )
1348 /* make sure we are up to date */
1349 stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
1350 CL_CO( read_bytes );
1351 CL_CO( read_packets );
1352 CL_CO( demux_read );
1353 CL_CO( input_bitrate );
1354 CL_CO( demux_bitrate );
1355 CL_CO( demux_corrupted );
1356 CL_CO( demux_discontinuity );
1357 CL_CO( played_abuffers );
1358 CL_CO( lost_abuffers );
1359 CL_CO( displayed_pictures );
1360 CL_CO( lost_pictures );
1361 CL_CO( decoded_audio) ;
1362 CL_CO( decoded_video );
1363 CL_CO( decoded_sub) ;
1366 /* Close optional stream output instance */
1367 if( p_input->p->p_sout )
1369 CL_CO( sout_sent_packets );
1370 CL_CO( sout_sent_bytes );
1371 CL_CO( sout_send_bitrate );
1376 vlc_mutex_lock( &p_input->p->p_item->lock );
1377 if( p_input->p->i_attachment > 0 )
1379 for( i = 0; i < p_input->p->i_attachment; i++ )
1380 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1381 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1382 free( p_input->p->attachment_demux);
1383 p_input->p->attachment_demux = NULL;
1385 vlc_mutex_unlock( &p_input->p->p_item->lock );
1388 input_resource_RequestSout( p_input->p->p_resource,
1389 p_input->p->p_sout, NULL );
1390 input_resource_SetInput( p_input->p->p_resource, NULL );
1391 if( p_input->p->p_resource_private )
1392 input_resource_Terminate( p_input->p->p_resource_private );
1395 /*****************************************************************************
1397 *****************************************************************************/
1398 void input_ControlPush( input_thread_t *p_input,
1399 int i_type, vlc_value_t *p_val )
1401 vlc_mutex_lock( &p_input->p->lock_control );
1402 if( i_type == INPUT_CONTROL_SET_DIE )
1404 /* Special case, empty the control */
1405 for( int i = 0; i < p_input->p->i_control; i++ )
1407 input_control_t *p_ctrl = &p_input->p->control[i];
1408 ControlRelease( p_ctrl->i_type, p_ctrl->val );
1410 p_input->p->i_control = 0;
1413 if( p_input->p->i_control >= INPUT_CONTROL_FIFO_SIZE )
1415 msg_Err( p_input, "input control fifo overflow, trashing type=%d",
1418 ControlRelease( i_type, *p_val );
1427 memset( &c.val, 0, sizeof(c.val) );
1429 p_input->p->control[p_input->p->i_control++] = c;
1431 vlc_cond_signal( &p_input->p->wait_control );
1432 vlc_mutex_unlock( &p_input->p->lock_control );
1435 static int ControlGetReducedIndexLocked( input_thread_t *p_input )
1437 const int i_lt = p_input->p->control[0].i_type;
1439 for( i = 1; i < p_input->p->i_control; i++ )
1441 const int i_ct = p_input->p->control[i].i_type;
1444 ( i_ct == INPUT_CONTROL_SET_STATE ||
1445 i_ct == INPUT_CONTROL_SET_RATE ||
1446 i_ct == INPUT_CONTROL_SET_POSITION ||
1447 i_ct == INPUT_CONTROL_SET_TIME ||
1448 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1449 i_ct == INPUT_CONTROL_SET_TITLE ||
1450 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1451 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1457 /* TODO but that's not that important
1458 - merge SET_X with SET_X_CMD
1459 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1460 - ignore SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1470 static inline int ControlPop( input_thread_t *p_input,
1471 int *pi_type, vlc_value_t *p_val,
1472 mtime_t i_deadline, bool b_postpone_seek )
1474 input_thread_private_t *p_sys = p_input->p;
1476 vlc_mutex_lock( &p_sys->lock_control );
1477 while( p_sys->i_control <= 0 ||
1478 ( b_postpone_seek && ControlIsSeekRequest( p_sys->control[0].i_type ) ) )
1480 if( !vlc_object_alive( p_input ) || i_deadline < 0 )
1482 vlc_mutex_unlock( &p_sys->lock_control );
1483 return VLC_EGENERIC;
1486 if( vlc_cond_timedwait( &p_sys->wait_control, &p_sys->lock_control,
1489 vlc_mutex_unlock( &p_sys->lock_control );
1490 return VLC_EGENERIC;
1495 const int i_index = ControlGetReducedIndexLocked( p_input );
1498 *pi_type = p_sys->control[i_index].i_type;
1499 *p_val = p_sys->control[i_index].val;
1501 p_sys->i_control -= i_index + 1;
1502 if( p_sys->i_control > 0 )
1503 memmove( &p_sys->control[0], &p_sys->control[i_index+1],
1504 sizeof(*p_sys->control) * p_sys->i_control );
1505 vlc_mutex_unlock( &p_sys->lock_control );
1509 static bool ControlIsSeekRequest( int i_type )
1513 case INPUT_CONTROL_SET_POSITION:
1514 case INPUT_CONTROL_SET_TIME:
1515 case INPUT_CONTROL_SET_TITLE:
1516 case INPUT_CONTROL_SET_TITLE_NEXT:
1517 case INPUT_CONTROL_SET_TITLE_PREV:
1518 case INPUT_CONTROL_SET_SEEKPOINT:
1519 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1520 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1521 case INPUT_CONTROL_SET_BOOKMARK:
1522 case INPUT_CONTROL_NAV_ACTIVATE:
1523 case INPUT_CONTROL_NAV_UP:
1524 case INPUT_CONTROL_NAV_DOWN:
1525 case INPUT_CONTROL_NAV_LEFT:
1526 case INPUT_CONTROL_NAV_RIGHT:
1533 static void ControlRelease( int i_type, vlc_value_t val )
1537 case INPUT_CONTROL_ADD_SUBTITLE:
1538 case INPUT_CONTROL_ADD_SLAVE:
1539 free( val.psz_string );
1548 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1550 int i_ret = VLC_SUCCESS;
1551 int i_state = PAUSE_S;
1553 if( p_input->p->b_can_pause )
1555 demux_t *p_demux = p_input->p->input.p_demux;
1557 if( p_demux->s != NULL )
1558 i_ret = stream_Control( p_demux->s, STREAM_SET_PAUSE_STATE, true );
1560 i_ret = demux_Control( p_demux, DEMUX_SET_PAUSE_STATE, true );
1564 msg_Warn( p_input, "cannot set pause state" );
1570 i_ret = es_out_SetPauseState( p_input->p->p_es_out,
1571 p_input->p->b_can_pause, true,
1575 msg_Warn( p_input, "cannot set pause state at es_out level" );
1579 /* Switch to new state */
1580 input_ChangeState( p_input, i_state );
1583 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1585 int i_ret = VLC_SUCCESS;
1587 if( p_input->p->b_can_pause )
1589 demux_t *p_demux = p_input->p->input.p_demux;
1591 if( p_demux->s != NULL )
1592 i_ret = stream_Control( p_demux->s, STREAM_SET_PAUSE_STATE, false );
1594 i_ret = demux_Control( p_demux, DEMUX_SET_PAUSE_STATE, false );
1597 /* FIXME What to do ? */
1598 msg_Warn( p_input, "cannot unset pause -> EOF" );
1599 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1603 /* Switch to play */
1604 input_ChangeState( p_input, PLAYING_S );
1608 es_out_SetPauseState( p_input->p->p_es_out, false, false, i_control_date );
1611 static bool Control( input_thread_t *p_input,
1612 int i_type, vlc_value_t val )
1614 const mtime_t i_control_date = mdate();
1615 /* FIXME b_force_update is abused, it should be carefully checked */
1616 bool b_force_update = false;
1619 return b_force_update;
1623 case INPUT_CONTROL_SET_DIE:
1624 msg_Dbg( p_input, "control: stopping input" );
1626 /* Mark all submodules to die */
1627 ObjectKillChildrens( VLC_OBJECT(p_input) );
1630 case INPUT_CONTROL_SET_POSITION:
1632 if( p_input->p->b_recording )
1634 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1638 float f_pos = val.f_float;
1641 else if( f_pos > 1.f )
1643 /* Reset the decoders states and clock sync (before calling the demuxer */
1644 es_out_SetTime( p_input->p->p_es_out, -1 );
1645 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1646 (double) f_pos, !p_input->p->b_fast_seek ) )
1648 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1649 "%2.1f%% failed", (double)(f_pos * 100.f) );
1653 if( p_input->p->i_slave > 0 )
1654 SlaveSeek( p_input );
1655 p_input->p->input.b_eof = false;
1657 b_force_update = true;
1662 case INPUT_CONTROL_SET_TIME:
1667 if( p_input->p->b_recording )
1669 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1673 i_time = val.i_time;
1677 /* Reset the decoders states and clock sync (before calling the demuxer */
1678 es_out_SetTime( p_input->p->p_es_out, -1 );
1680 i_ret = demux_Control( p_input->p->input.p_demux,
1681 DEMUX_SET_TIME, i_time,
1682 !p_input->p->b_fast_seek );
1687 /* Emulate it with a SET_POS */
1688 if( !demux_Control( p_input->p->input.p_demux,
1689 DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
1691 double f_pos = (double)i_time / (double)i_length;
1692 i_ret = demux_Control( p_input->p->input.p_demux,
1693 DEMUX_SET_POSITION, f_pos,
1694 !p_input->p->b_fast_seek );
1699 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1700 " failed or not possible", i_time );
1704 if( p_input->p->i_slave > 0 )
1705 SlaveSeek( p_input );
1706 p_input->p->input.b_eof = false;
1708 b_force_update = true;
1713 case INPUT_CONTROL_SET_STATE:
1714 if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1715 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1716 else if( p_input->p->i_state == PAUSE_S )
1718 ControlUnpause( p_input, i_control_date );
1720 b_force_update = true;
1722 else if( val.i_int == PAUSE_S && p_input->p->i_state == PLAYING_S /* &&
1723 p_input->p->b_can_pause */ )
1725 ControlPause( p_input, i_control_date );
1727 b_force_update = true;
1729 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause && 0 )
1731 b_force_update = true;
1733 /* Correct "state" value */
1734 input_ChangeState( p_input, p_input->p->i_state );
1738 case INPUT_CONTROL_SET_RATE:
1740 /* Get rate and direction */
1741 int i_rate = abs( val.i_int );
1742 int i_rate_sign = val.i_int < 0 ? -1 : 1;
1744 /* Check rate bound */
1745 if( i_rate < INPUT_RATE_MIN )
1747 msg_Dbg( p_input, "cannot set rate faster" );
1748 i_rate = INPUT_RATE_MIN;
1750 else if( i_rate > INPUT_RATE_MAX )
1752 msg_Dbg( p_input, "cannot set rate slower" );
1753 i_rate = INPUT_RATE_MAX;
1756 /* Apply direction */
1757 if( i_rate_sign < 0 )
1759 if( p_input->p->input.b_rescale_ts )
1761 msg_Dbg( p_input, "cannot set negative rate" );
1762 i_rate = p_input->p->i_rate;
1763 assert( i_rate > 0 );
1767 i_rate *= i_rate_sign;
1771 if( i_rate != INPUT_RATE_DEFAULT &&
1772 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1773 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1775 msg_Dbg( p_input, "cannot change rate" );
1776 i_rate = INPUT_RATE_DEFAULT;
1778 if( i_rate != p_input->p->i_rate &&
1779 !p_input->p->b_can_pace_control && p_input->p->b_can_rate_control )
1781 demux_t *p_demux = p_input->p->input.p_demux;
1782 int i_ret = VLC_EGENERIC;
1784 if( p_demux->s == NULL )
1786 if( !p_input->p->input.b_rescale_ts )
1787 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1789 i_ret = demux_Control( p_input->p->input.p_demux,
1790 DEMUX_SET_RATE, &i_rate );
1794 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1795 i_rate = p_input->p->i_rate;
1800 if( i_rate != p_input->p->i_rate )
1802 p_input->p->i_rate = i_rate;
1803 input_SendEventRate( p_input, i_rate );
1805 if( p_input->p->input.b_rescale_ts )
1807 const int i_rate_source = (p_input->p->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1808 es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1811 b_force_update = true;
1816 case INPUT_CONTROL_SET_PROGRAM:
1817 /* No need to force update, es_out does it if needed */
1818 es_out_Control( p_input->p->p_es_out,
1819 ES_OUT_SET_GROUP, val.i_int );
1821 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1825 case INPUT_CONTROL_SET_ES:
1826 /* No need to force update, es_out does it if needed */
1827 es_out_Control( p_input->p->p_es_out_display,
1828 ES_OUT_SET_ES_BY_ID, (int)val.i_int );
1830 demux_Control( p_input->p->input.p_demux, DEMUX_SET_ES, (int)val.i_int );
1833 case INPUT_CONTROL_RESTART_ES:
1834 es_out_Control( p_input->p->p_es_out_display,
1835 ES_OUT_RESTART_ES_BY_ID, (int)val.i_int );
1838 case INPUT_CONTROL_SET_AUDIO_DELAY:
1839 input_SendEventAudioDelay( p_input, val.i_time );
1840 UpdatePtsDelay( p_input );
1843 case INPUT_CONTROL_SET_SPU_DELAY:
1844 input_SendEventSubtitleDelay( p_input, val.i_time );
1845 UpdatePtsDelay( p_input );
1848 case INPUT_CONTROL_SET_TITLE:
1849 case INPUT_CONTROL_SET_TITLE_NEXT:
1850 case INPUT_CONTROL_SET_TITLE_PREV:
1852 if( p_input->p->b_recording )
1854 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1857 if( p_input->p->input.i_title <= 0 )
1860 int i_title = p_input->p->input.p_demux->info.i_title;
1861 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1863 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1866 i_title = val.i_int;
1867 if( i_title < 0 || i_title >= p_input->p->input.i_title )
1870 es_out_SetTime( p_input->p->p_es_out, -1 );
1871 demux_Control( p_input->p->input.p_demux,
1872 DEMUX_SET_TITLE, i_title );
1873 input_SendEventTitle( p_input, i_title );
1876 case INPUT_CONTROL_SET_SEEKPOINT:
1877 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1878 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1880 if( p_input->p->b_recording )
1882 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1885 if( p_input->p->input.i_title <= 0 )
1888 demux_t *p_demux = p_input->p->input.p_demux;
1890 int i_title = p_demux->info.i_title;
1891 int i_seekpoint = p_demux->info.i_seekpoint;
1893 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1895 int64_t i_seekpoint_time = p_input->p->input.title[i_title]->seekpoint[i_seekpoint]->i_time_offset;
1896 int64_t i_input_time = var_GetTime( p_input, "time" );
1897 if( i_seekpoint_time >= 0 && i_input_time >= 0 )
1899 if( i_input_time < i_seekpoint_time + 3000000 )
1905 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1908 i_seekpoint = val.i_int;
1910 || i_seekpoint >= p_input->p->input.title[i_title]->i_seekpoint )
1913 es_out_SetTime( p_input->p->p_es_out, -1 );
1914 demux_Control( p_input->p->input.p_demux,
1915 DEMUX_SET_SEEKPOINT, i_seekpoint );
1916 input_SendEventSeekpoint( p_input, i_title, i_seekpoint );
1920 case INPUT_CONTROL_ADD_SUBTITLE:
1921 if( val.psz_string )
1922 input_SubtitleFileAdd( p_input, val.psz_string, true );
1925 case INPUT_CONTROL_ADD_SLAVE:
1926 if( val.psz_string )
1928 const char *uri = val.psz_string;
1929 input_source_t *slave = InputSourceNew( p_input );
1931 if( slave && !InputSourceInit( p_input, slave, uri, NULL, false ) )
1936 msg_Dbg( p_input, "adding %s as slave on the fly", uri );
1939 if( demux_Control( p_input->p->input.p_demux,
1940 DEMUX_GET_TIME, &i_time ) )
1942 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1943 InputSourceClean( slave );
1947 if( demux_Control( slave->p_demux,
1948 DEMUX_SET_TIME, i_time, true ) )
1950 msg_Err( p_input, "seek failed for new slave" );
1951 InputSourceClean( slave );
1956 /* Get meta (access and demux) */
1957 InputUpdateMeta( p_input, slave->p_demux );
1959 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1964 msg_Warn( p_input, "failed to add %s as slave", uri );
1969 case INPUT_CONTROL_SET_RECORD_STATE:
1970 if( !!p_input->p->b_recording != !!val.b_bool )
1972 if( p_input->p->input.b_can_stream_record )
1974 if( demux_Control( p_input->p->input.p_demux,
1975 DEMUX_SET_RECORD_STATE, val.b_bool ) )
1980 if( es_out_SetRecordState( p_input->p->p_es_out_display, val.b_bool ) )
1983 p_input->p->b_recording = val.b_bool;
1985 input_SendEventRecord( p_input, val.b_bool );
1987 b_force_update = true;
1991 case INPUT_CONTROL_SET_FRAME_NEXT:
1992 if( p_input->p->i_state == PAUSE_S )
1994 es_out_SetFrameNext( p_input->p->p_es_out );
1996 else if( p_input->p->i_state == PLAYING_S )
1998 ControlPause( p_input, i_control_date );
2002 msg_Err( p_input, "invalid state for frame next" );
2004 b_force_update = true;
2007 case INPUT_CONTROL_SET_BOOKMARK:
2009 mtime_t time_offset = -1;
2011 vlc_mutex_lock( &p_input->p->p_item->lock );
2012 if( val.i_int >= 0 && val.i_int < p_input->p->i_bookmark )
2014 const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[val.i_int];
2015 time_offset = p_bookmark->i_time_offset;
2017 vlc_mutex_unlock( &p_input->p->p_item->lock );
2019 if( time_offset < 0 )
2021 msg_Err( p_input, "invalid bookmark %"PRId64, val.i_int );
2025 val.i_time = time_offset;
2026 b_force_update = Control( p_input, INPUT_CONTROL_SET_TIME, val );
2030 case INPUT_CONTROL_NAV_ACTIVATE:
2031 case INPUT_CONTROL_NAV_UP:
2032 case INPUT_CONTROL_NAV_DOWN:
2033 case INPUT_CONTROL_NAV_LEFT:
2034 case INPUT_CONTROL_NAV_RIGHT:
2035 demux_Control( p_input->p->input.p_demux, i_type
2036 - INPUT_CONTROL_NAV_ACTIVATE + DEMUX_NAV_ACTIVATE );
2040 msg_Err( p_input, "not yet implemented" );
2044 ControlRelease( i_type, val );
2045 return b_force_update;
2048 /*****************************************************************************
2049 * UpdateTitleSeekpoint
2050 *****************************************************************************/
2051 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2052 int i_title, int i_seekpoint )
2054 int i_title_end = p_input->p->input.i_title_end -
2055 p_input->p->input.i_title_offset;
2056 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2057 p_input->p->input.i_seekpoint_offset;
2059 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2061 if( i_title > i_title_end ||
2062 ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2065 else if( i_seekpoint_end >= 0 )
2067 if( i_seekpoint > i_seekpoint_end )
2070 else if( i_title_end >= 0 )
2072 if( i_title > i_title_end )
2077 /*****************************************************************************
2079 *****************************************************************************/
2080 static int UpdateTitleSeekpointFromDemux( input_thread_t *p_input )
2082 demux_t *p_demux = p_input->p->input.p_demux;
2084 /* TODO event-like */
2085 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2087 input_SendEventTitle( p_input, p_demux->info.i_title );
2089 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2091 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2093 input_SendEventSeekpoint( p_input,
2094 p_demux->info.i_title, p_demux->info.i_seekpoint );
2096 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2099 /* Hmmm only works with master input */
2100 if( p_input->p->input.p_demux == p_demux )
2101 return UpdateTitleSeekpoint( p_input,
2102 p_demux->info.i_title,
2103 p_demux->info.i_seekpoint );
2107 static void UpdateGenericFromDemux( input_thread_t *p_input )
2109 demux_t *p_demux = p_input->p->input.p_demux;
2111 if( p_demux->info.i_update & INPUT_UPDATE_META )
2113 InputUpdateMeta( p_input, p_demux );
2114 p_demux->info.i_update &= ~INPUT_UPDATE_META;
2120 if( !demux_Control( p_demux, DEMUX_GET_SIGNAL, &quality, &strength ) )
2121 input_SendEventSignal( p_input, quality, strength );
2125 static void UpdateTitleListfromDemux( input_thread_t *p_input )
2127 input_source_t *in = &p_input->p->input;
2129 /* Delete the preexisting titles */
2130 if( in->i_title > 0 )
2132 for( int i = 0; i < in->i_title; i++ )
2133 vlc_input_title_Delete( in->title[i] );
2134 TAB_CLEAN( in->i_title, in->title );
2135 in->b_title_demux = false;
2138 /* Get the new title list */
2139 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2140 &in->title, &in->i_title,
2141 &in->i_title_offset, &in->i_seekpoint_offset ) )
2142 TAB_INIT( in->i_title, in->title );
2144 in->b_title_demux = true;
2146 InitTitle( p_input );
2150 /*****************************************************************************
2152 *****************************************************************************/
2153 static input_source_t *InputSourceNew( input_thread_t *p_input )
2155 VLC_UNUSED(p_input);
2157 return calloc( 1, sizeof( input_source_t ) );
2160 /*****************************************************************************
2162 *****************************************************************************/
2163 static int InputSourceInit( input_thread_t *p_input,
2164 input_source_t *in, const char *psz_mrl,
2165 const char *psz_forced_demux, bool b_in_can_fail )
2167 const char *psz_access, *psz_demux, *psz_path, *psz_anchor = NULL;
2168 char *psz_var_demux = NULL;
2172 char *psz_dup = strdup( psz_mrl );
2174 if( psz_dup == NULL )
2178 input_SplitMRL( &psz_access, &psz_demux, &psz_path, &psz_anchor, psz_dup );
2180 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2181 psz_mrl, psz_access, psz_demux, psz_path );
2182 if( !p_input->b_preparsing )
2184 /* Find optional titles and seekpoints */
2185 MRLSections( psz_anchor, &in->i_title_start, &in->i_title_end,
2186 &in->i_seekpoint_start, &in->i_seekpoint_end );
2187 if( psz_forced_demux && *psz_forced_demux )
2189 psz_demux = psz_forced_demux;
2191 else if( *psz_demux == '\0' )
2193 /* special hack for forcing a demuxer with --demux=module
2194 * (and do nothing with a list) */
2195 psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2196 psz_demux = psz_var_demux;
2197 msg_Dbg( p_input, "specified demux `%s'", psz_demux );
2200 /* Try access_demux first */
2201 in->p_demux = demux_New( p_input, p_input, psz_access, psz_demux,
2202 psz_path, NULL, p_input->p->p_es_out, false );
2208 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2211 mtime_t i_pts_delay;
2215 /* Get infos from access_demux */
2216 in->b_title_demux = true;
2217 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2218 &in->title, &in->i_title,
2219 &in->i_title_offset, &in->i_seekpoint_offset ) )
2221 TAB_INIT( in->i_title, in->title );
2223 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2224 &in->b_can_pace_control ) )
2225 in->b_can_pace_control = false;
2227 assert( in->p_demux->pf_demux != NULL || !in->b_can_pace_control );
2229 if( !in->b_can_pace_control )
2231 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2232 &in->b_can_rate_control, &in->b_rescale_ts ) )
2234 in->b_can_rate_control = false;
2235 in->b_rescale_ts = true; /* not used */
2240 in->b_can_rate_control = true;
2241 in->b_rescale_ts = true;
2243 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2244 &in->b_can_pause ) )
2245 in->b_can_pause = false;
2246 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2247 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2248 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control && in->b_can_rate_control );
2251 if( demux_Control( in->p_demux, DEMUX_CAN_SEEK, &b_can_seek ) )
2253 var_SetBool( p_input, "can-seek", b_can_seek );
2256 { /* Now try a real access */
2257 access_t *p_access = access_New( p_input, p_input,
2258 psz_access, psz_demux, psz_path );
2259 if( p_access == NULL )
2261 if( vlc_object_alive( p_input ) )
2263 msg_Err( p_input, "open of `%s' failed", psz_mrl );
2264 if( !b_in_can_fail )
2265 dialog_Fatal( p_input, _("Your input can't be opened"),
2266 _("VLC is unable to open the MRL '%s'."
2267 " Check the log for details."), psz_mrl );
2272 /* Access-forced demuxer (PARENTAL ADVISORY: EXPLICIT HACK) */
2273 if( !psz_demux[0] || !strcasecmp( psz_demux, "any" ) )
2274 psz_demux = p_access->psz_demux;
2278 char **ppsz_input_list;
2280 TAB_INIT( i_input_list, ppsz_input_list );
2282 /* On master stream only, use input-list */
2283 if( &p_input->p->input == in )
2289 psz_parser = var_CreateGetNonEmptyString( p_input, "input-list" );
2291 while( psz_parser && *psz_parser )
2293 char *p = strchr( psz_parser, ',' );
2299 char *psz_name = strdup( psz_parser );
2301 TAB_APPEND( i_input_list, ppsz_input_list, psz_name );
2308 /* Autodetect extra files if none specified */
2309 if( i_input_list <= 0 )
2311 InputGetExtraFiles( p_input, &i_input_list, &ppsz_input_list,
2312 psz_access, psz_path );
2314 if( i_input_list > 0 )
2315 TAB_APPEND( i_input_list, ppsz_input_list, NULL );
2317 /* Create the stream_t */
2318 stream_t *p_stream = stream_AccessNew( p_access, ppsz_input_list );
2319 if( ppsz_input_list )
2321 for( int i = 0; ppsz_input_list[i] != NULL; i++ )
2322 free( ppsz_input_list[i] );
2323 TAB_CLEAN( i_input_list, ppsz_input_list );
2326 if( p_stream == NULL )
2328 msg_Warn( p_input, "cannot create a stream_t from access" );
2332 /* Add stream filters */
2333 char *psz_stream_filter = var_GetNonEmptyString( p_input,
2335 p_stream = stream_FilterChainNew( p_stream, psz_stream_filter,
2336 var_GetBool( p_input, "input-record-native" ) );
2337 free( psz_stream_filter );
2339 if( !p_input->b_preparsing )
2343 stream_Control( p_stream, STREAM_CAN_CONTROL_PACE,
2344 &in->b_can_pace_control );
2345 in->b_can_rate_control = in->b_can_pace_control;
2346 in->b_rescale_ts = true;
2348 stream_Control( p_stream, STREAM_CAN_PAUSE, &in->b_can_pause );
2349 var_SetBool( p_input, "can-pause",
2350 in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2351 var_SetBool( p_input, "can-rate",
2352 !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2353 var_SetBool( p_input, "can-rewind",
2354 !in->b_rescale_ts && !in->b_can_pace_control );
2356 stream_Control( p_stream, STREAM_CAN_SEEK, &b );
2357 var_SetBool( p_input, "can-seek", b );
2359 in->b_title_demux = false;
2361 stream_Control( p_stream, STREAM_GET_PTS_DELAY, &i_pts_delay );
2364 in->p_demux = demux_New( p_input, p_input, psz_access, psz_demux,
2365 /* Take access/stream redirections into account: */
2366 p_stream->psz_path ? p_stream->psz_path : psz_path,
2367 p_stream, p_input->p->p_es_out,
2368 p_input->b_preparsing );
2370 if( in->p_demux == NULL )
2372 if( vlc_object_alive( p_input ) )
2374 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2375 psz_access, psz_demux, psz_path );
2376 if( !b_in_can_fail )
2377 dialog_Fatal( VLC_OBJECT( p_input ),
2378 _("VLC can't recognize the input's format"),
2379 _("The format of '%s' cannot be detected. "
2380 "Have a look at the log for details."), psz_mrl );
2382 stream_Delete( p_stream );
2385 assert( in->p_demux->pf_demux != NULL );
2387 /* Get title from demux */
2388 if( !p_input->b_preparsing && in->i_title <= 0 )
2390 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2391 &in->title, &in->i_title,
2392 &in->i_title_offset, &in->i_seekpoint_offset ))
2394 TAB_INIT( in->i_title, in->title );
2398 in->b_title_demux = true;
2403 free( psz_var_demux );
2406 /* Set record capabilities */
2407 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2408 in->b_can_stream_record = false;
2410 if( !var_GetBool( p_input, "input-record-native" ) )
2411 in->b_can_stream_record = false;
2412 var_SetBool( p_input, "can-record", true );
2414 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2418 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2419 if( !p_input->b_preparsing )
2422 input_attachment_t **attachment;
2423 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2424 &attachment, &i_attachment ) )
2426 vlc_mutex_lock( &p_input->p->p_item->lock );
2427 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment, &p_input->p->attachment_demux,
2428 i_attachment, attachment, in->p_demux );
2429 vlc_mutex_unlock( &p_input->p->p_item->lock );
2432 /* PTS delay: request from demux first. This is required for
2433 * access_demux and some special cases like SDP demux. Otherwise,
2434 * fallback to access */
2435 if( demux_Control( in->p_demux, DEMUX_GET_PTS_DELAY,
2436 &in->i_pts_delay ) )
2437 in->i_pts_delay = i_pts_delay;
2438 if( in->i_pts_delay > INPUT_PTS_DELAY_MAX )
2439 in->i_pts_delay = INPUT_PTS_DELAY_MAX;
2440 else if( in->i_pts_delay < 0 )
2441 in->i_pts_delay = 0;
2444 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) && f_fps > 0.0 )
2446 vlc_mutex_lock( &p_input->p->p_item->lock );
2447 p_input->p->f_fps = f_fps;
2448 vlc_mutex_unlock( &p_input->p->p_item->lock );
2451 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2452 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2458 demux_Delete( in->p_demux );
2460 free( psz_var_demux );
2463 return VLC_EGENERIC;
2466 /*****************************************************************************
2468 *****************************************************************************/
2469 static void InputSourceClean( input_source_t *in )
2474 demux_Delete( in->p_demux );
2476 if( in->i_title > 0 )
2478 for( i = 0; i < in->i_title; i++ )
2479 vlc_input_title_Delete( in->title[i] );
2480 TAB_CLEAN( in->i_title, in->title );
2484 /*****************************************************************************
2486 *****************************************************************************/
2487 static void InputSourceMeta( input_thread_t *p_input,
2488 input_source_t *p_source, vlc_meta_t *p_meta )
2490 demux_t *p_demux = p_source->p_demux;
2492 /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
2495 bool has_meta = false;
2497 /* Read demux meta */
2498 if( !demux_Control( p_demux, DEMUX_GET_META, p_meta ) )
2501 bool has_unsupported;
2502 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &has_unsupported ) )
2503 has_unsupported = true;
2505 /* If the demux report unsupported meta data, or if we don't have meta data
2506 * try an external "meta reader" */
2507 if( has_meta && !has_unsupported )
2510 demux_meta_t *p_demux_meta =
2511 vlc_custom_create( p_demux, sizeof( *p_demux_meta ), "demux meta" );
2514 p_demux_meta->p_demux = p_demux;
2515 p_demux_meta->p_item = p_input->p->p_item;
2517 module_t *p_id3 = module_need( p_demux_meta, "meta reader", NULL, false );
2520 if( p_demux_meta->p_meta )
2522 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2523 vlc_meta_Delete( p_demux_meta->p_meta );
2526 if( p_demux_meta->i_attachments > 0 )
2528 vlc_mutex_lock( &p_input->p->p_item->lock );
2529 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment, &p_input->p->attachment_demux,
2530 p_demux_meta->i_attachments, p_demux_meta->attachments, p_demux);
2531 vlc_mutex_unlock( &p_input->p->p_item->lock );
2533 module_unneed( p_demux, p_id3 );
2535 vlc_object_release( p_demux_meta );
2539 static void SlaveDemux( input_thread_t *p_input, bool *pb_demux_polled )
2544 *pb_demux_polled = false;
2545 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2547 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2551 for( i = 0; i < p_input->p->i_slave; i++ )
2553 input_source_t *in = p_input->p->slave[i];
2559 const bool b_demux_polled = in->p_demux->pf_demux != NULL;
2560 if( !b_demux_polled )
2563 *pb_demux_polled = true;
2565 /* Call demux_Demux until we have read enough data */
2566 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2571 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2573 msg_Err( p_input, "slave[%d] doesn't like "
2574 "DEMUX_GET_TIME -> EOF", i );
2579 if( i_stime >= i_time )
2585 if( ( i_ret = demux_Demux( in->p_demux ) ) <= 0 )
2591 i_ret = demux_Demux( in->p_demux );
2596 msg_Dbg( p_input, "slave %d EOF", i );
2602 static void SlaveSeek( input_thread_t *p_input )
2607 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2609 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2613 for( i = 0; i < p_input->p->i_slave; i++ )
2615 input_source_t *in = p_input->p->slave[i];
2617 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time, true ) )
2620 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2630 /*****************************************************************************
2632 *****************************************************************************/
2633 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2635 static const struct { int i_meta; const char *psz_name; } p_list[] = {
2636 { vlc_meta_Title, "meta-title" },
2637 { vlc_meta_Artist, "meta-artist" },
2638 { vlc_meta_Genre, "meta-genre" },
2639 { vlc_meta_Copyright, "meta-copyright" },
2640 { vlc_meta_Description, "meta-description" },
2641 { vlc_meta_Date, "meta-date" },
2642 { vlc_meta_URL, "meta-url" },
2646 /* Get meta information from user */
2647 for( int i = 0; p_list[i].psz_name; i++ )
2649 char *psz_string = var_GetNonEmptyString( p_input, p_list[i].psz_name );
2653 EnsureUTF8( psz_string );
2654 vlc_meta_Set( p_meta, p_list[i].i_meta, psz_string );
2659 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment, demux_t ***ppp_attachment_demux,
2660 int i_new, input_attachment_t **pp_new, demux_t *p_demux )
2662 int i_attachment = *pi_attachment;
2663 input_attachment_t **attachment = *ppp_attachment;
2664 demux_t **attachment_demux = *ppp_attachment_demux;
2667 attachment = xrealloc( attachment,
2668 sizeof(*attachment) * ( i_attachment + i_new ) );
2669 attachment_demux = xrealloc( attachment_demux,
2670 sizeof(*attachment_demux) * ( i_attachment + i_new ) );
2671 for( i = 0; i < i_new; i++ )
2673 attachment[i_attachment] = pp_new[i];
2674 attachment_demux[i_attachment++] = p_demux;
2679 *pi_attachment = i_attachment;
2680 *ppp_attachment = attachment;
2681 *ppp_attachment_demux = attachment_demux;
2684 /*****************************************************************************
2685 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2686 * arturl and locking issue.
2687 *****************************************************************************/
2688 static void InputUpdateMeta( input_thread_t *p_input, demux_t *p_demux )
2690 vlc_meta_t *p_meta = vlc_meta_New();
2691 if( unlikely(p_meta == NULL) )
2694 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2696 /* If metadata changed, then the attachments might have changed.
2697 We need to update them in case they contain album art. */
2698 input_attachment_t **attachment;
2701 if( !demux_Control( p_demux, DEMUX_GET_ATTACHMENTS,
2702 &attachment, &i_attachment ) )
2704 vlc_mutex_lock( &p_input->p->p_item->lock );
2705 if( p_input->p->i_attachment > 0 )
2708 for( int i = 0; i < p_input->p->i_attachment; i++ )
2710 if( p_input->p->attachment_demux[i] == p_demux )
2711 vlc_input_attachment_Delete( p_input->p->attachment[i] );
2714 p_input->p->attachment[j] = p_input->p->attachment[i];
2715 p_input->p->attachment_demux[j] = p_input->p->attachment_demux[i];
2719 p_input->p->i_attachment = j;
2721 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment, &p_input->p->attachment_demux,
2722 i_attachment, attachment, p_demux );
2723 vlc_mutex_unlock( &p_input->p->p_item->lock );
2726 es_out_ControlSetMeta( p_input->p->p_es_out, p_meta );
2727 vlc_meta_Delete( p_meta );
2730 /*****************************************************************************
2731 * InputGetExtraFiles
2732 * Autodetect extra input list
2733 *****************************************************************************/
2734 static void InputGetExtraFilesPattern( input_thread_t *p_input,
2735 int *pi_list, char ***pppsz_list,
2736 const char *psz_path,
2737 const char *psz_match,
2738 const char *psz_format,
2739 int i_start, int i_stop )
2744 TAB_INIT( i_list, ppsz_list );
2746 char *psz_base = strdup( psz_path );
2750 /* Remove the extension */
2751 char *psz_end = &psz_base[strlen(psz_base)-strlen(psz_match)];
2752 assert( psz_end >= psz_base);
2755 /* Try to list files */
2756 for( int i = i_start; i <= i_stop; i++ )
2761 if( asprintf( &psz_file, psz_format, psz_base, i ) < 0 )
2764 char *psz_tmp_path = get_path( psz_file );
2766 if( vlc_stat( psz_tmp_path, &st ) || !S_ISREG( st.st_mode ) || !st.st_size )
2769 free( psz_tmp_path );
2773 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2774 TAB_APPEND( i_list, ppsz_list, psz_file );
2775 free( psz_tmp_path );
2780 *pppsz_list = ppsz_list;
2783 static void InputGetExtraFiles( input_thread_t *p_input,
2784 int *pi_list, char ***pppsz_list,
2785 const char *psz_access, const char *psz_path )
2789 const char *psz_match;
2790 const char *psz_format;
2794 /* XXX the order is important */
2795 { ".001", "%s.%.3d", 2, 999 },
2796 { NULL, NULL, 0, 0 }
2799 TAB_INIT( *pi_list, *pppsz_list );
2801 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2804 const size_t i_path = strlen(psz_path);
2806 for( int i = 0; p_pattern[i].psz_match != NULL; i++ )
2808 const size_t i_ext = strlen(p_pattern[i].psz_match );
2810 if( i_path < i_ext )
2812 if( !strcmp( &psz_path[i_path-i_ext], p_pattern[i].psz_match ) )
2814 InputGetExtraFilesPattern( p_input, pi_list, pppsz_list,
2816 p_pattern[i].psz_match, p_pattern[i].psz_format,
2817 p_pattern[i].i_start, p_pattern[i].i_stop );
2824 static void input_ChangeState( input_thread_t *p_input, int i_state )
2826 const bool b_changed = p_input->p->i_state != i_state;
2828 p_input->p->i_state = i_state;
2829 if( i_state == ERROR_S )
2830 p_input->b_error = true;
2831 else if( i_state == END_S )
2832 p_input->b_eof = true;
2836 input_item_SetErrorWhenReading( p_input->p->p_item, p_input->b_error );
2837 input_SendEventState( p_input, i_state );
2842 /*****************************************************************************
2843 * MRLSplit: parse the access, demux and url part of the
2844 * Media Resource Locator.
2845 *****************************************************************************/
2846 void input_SplitMRL( const char **access, const char **demux,
2847 const char **path, const char **anchor, char *buf )
2851 /* Separate <path> from <access>[/<demux>]:// */
2852 p = strstr( buf, "://" );
2856 p += 3; /* skips "://" */
2859 /* Remove HTML anchor if present (not supported).
2860 * The hash symbol itself should be URI-encoded. */
2861 p = strchr( p, '#' );
2873 fprintf( stderr, "%s(\"%s\") probably not a valid URI!\n", __func__,
2876 /* Note: this is a valid non const pointer to "": */
2877 *path = buf + strlen( buf );
2880 /* Separate access from demux */
2881 p = strchr( buf, '/' );
2892 /* We really don't want module name substitution here! */
2899 static const char *MRLSeekPoint( const char *str, int *title, int *chapter )
2904 /* Look for the title */
2905 u = strtoul( str, &end, 0 );
2906 *title = (str == end || u > (unsigned long)INT_MAX) ? -1 : (int)u;
2909 /* Look for the chapter */
2913 u = strtoul( str, &end, 0 );
2914 *chapter = (str == end || u > (unsigned long)INT_MAX) ? -1 : (int)u;
2924 /*****************************************************************************
2925 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2928 * [url][@[title_start][:chapter_start][-[title_end][:chapter_end]]]
2929 *****************************************************************************/
2930 static void MRLSections( const char *p,
2931 int *pi_title_start, int *pi_title_end,
2932 int *pi_chapter_start, int *pi_chapter_end )
2934 *pi_title_start = *pi_title_end = *pi_chapter_start = *pi_chapter_end = -1;
2936 int title_start, chapter_start, title_end, chapter_end;
2942 p = MRLSeekPoint( p, &title_start, &chapter_start );
2944 title_start = chapter_start = -1;
2947 p = MRLSeekPoint( p + 1, &title_end, &chapter_end );
2949 title_end = chapter_end = -1;
2951 if( *p ) /* syntax error */
2954 *pi_title_start = title_start;
2955 *pi_title_end = title_end;
2956 *pi_chapter_start = chapter_start;
2957 *pi_chapter_end = chapter_end;
2960 /*****************************************************************************
2961 * input_AddSubtitles: add a subtitle file and enable it
2962 *****************************************************************************/
2963 static void input_SubtitleAdd( input_thread_t *p_input,
2964 const char *url, unsigned i_flags )
2966 input_source_t *sub = InputSourceNew( p_input );
2972 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2974 if( InputSourceInit( p_input, sub, url, "subtitle",
2975 (i_flags & SUB_CANFAIL) ) )
2980 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2982 if( !(i_flags & SUB_FORCED) )
2988 if( var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES, &list, NULL ) )
2990 if( count.i_int == 0 )
2992 /* if it was first one, there is disable too */
2994 if( count.i_int < list.p_list->i_count )
2996 const int i_id = list.p_list->p_values[count.i_int].i_int;
2998 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
2999 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3001 var_FreeList( &list, NULL );
3004 static void input_SubtitleFileAdd( input_thread_t *p_input, char *psz_subtitle,
3007 /* if we are provided a subtitle.sub file,
3008 * see if we don't have a subtitle.idx and use it instead */
3009 char *psz_path = strdup( psz_subtitle );
3010 if( likely(psz_path != NULL) )
3012 char *psz_extension = strrchr( psz_path, '.');
3013 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3017 strcpy( psz_extension, ".idx" );
3019 if( !vlc_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3021 msg_Dbg( p_input, "using %s as subtitle file instead of %s",
3022 psz_path, psz_subtitle );
3023 strcpy( psz_subtitle, psz_path ); /* <- FIXME! constify */
3029 char *url = vlc_path2uri( psz_subtitle, NULL );
3033 input_SubtitleAdd( p_input, url, i_flags );
3037 /*****************************************************************************
3039 *****************************************************************************/
3040 void input_UpdateStatistic( input_thread_t *p_input,
3041 input_statistic_t i_type, int i_delta )
3043 assert( p_input->p->i_state != INIT_S );
3045 vlc_mutex_lock( &p_input->p->counters.counters_lock);
3048 #define I(c) stats_Update( p_input->p->counters.c, i_delta, NULL )
3049 case INPUT_STATISTIC_DECODED_VIDEO:
3052 case INPUT_STATISTIC_DECODED_AUDIO:
3055 case INPUT_STATISTIC_DECODED_SUBTITLE:
3058 case INPUT_STATISTIC_SENT_PACKET:
3059 I(p_sout_sent_packets);
3062 case INPUT_STATISTIC_SENT_BYTE:
3066 stats_Update( p_input->p->counters.p_sout_sent_bytes, i_delta, &bytes );
3067 stats_Update( p_input->p->counters.p_sout_send_bitrate, bytes, NULL );
3071 msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3074 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
3078 /* TODO FIXME nearly the same logic that snapshot code */
3079 char *input_CreateFilename( input_thread_t *input, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3084 path = vlc_opendir( psz_path );
3089 char *psz_tmp = str_format( input, psz_prefix );
3093 filename_sanitize( psz_tmp );
3095 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3097 psz_extension ? "." : "",
3098 psz_extension ? psz_extension : "" ) < 0 )
3105 psz_file = str_format( input, psz_path );
3106 path_sanitize( psz_file );