1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 the VideoLAN team
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
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include "input_internal.h"
41 #include "../stream_output/stream_output.h"
43 #include <vlc_interface.h>
45 #include <vlc_charset.h>
46 #include <vlc_playlist.h>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
52 /*****************************************************************************
54 *****************************************************************************/
55 static void Destructor( input_thread_t * p_input );
57 static int Run ( input_thread_t *p_input );
58 static int RunAndDestroy ( input_thread_t *p_input );
60 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
61 const char *, bool, sout_instance_t * );
62 static int Init ( input_thread_t *p_input );
63 static void Error ( input_thread_t *p_input );
64 static void End ( input_thread_t *p_input );
65 static void MainLoop( input_thread_t *p_input );
67 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
68 static void ControlReduce( input_thread_t * );
69 static bool Control( input_thread_t *, int, vlc_value_t );
71 static int UpdateFromAccess( input_thread_t * );
72 static int UpdateFromDemux( input_thread_t * );
74 static void UpdateItemLength( input_thread_t *, int64_t i_length );
76 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
78 static input_source_t *InputSourceNew( input_thread_t *);
79 static int InputSourceInit( input_thread_t *, input_source_t *,
80 const char *, const char *psz_forced_demux );
81 static void InputSourceClean( input_source_t * );
83 //static void InputGetAttachments( input_thread_t *, input_source_t * );
84 static void SlaveDemux( input_thread_t *p_input );
85 static void SlaveSeek( input_thread_t *p_input );
87 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
88 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
90 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, bool * );
91 static void SoutKeep( sout_instance_t * );
93 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
94 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
95 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
96 int i_new, input_attachment_t **pp_new );
98 /*****************************************************************************
99 * This function creates a new input, and returns a pointer
100 * to its description. On error, it returns NULL.
102 * Variables for _public_ use:
105 * - rate,rate-slower, rate-faster
106 * - position, position-offset
107 * - time, time-offset
108 * - title,title-next,title-prev
109 * - chapter,chapter-next, chapter-prev
110 * - program, audio-es, video-es, spu-es
111 * - audio-delay, spu-delay
116 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
117 * or not, for that check position != 0.0)
119 * * For intf callback upon changes
121 * - rate-change for when playback rate changes
122 * TODO explain when Callback is called
123 * TODO complete this list (?)
124 *****************************************************************************/
125 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
126 const char *psz_header, bool b_quick,
127 sout_instance_t *p_sout )
129 static const char input_name[] = "input";
130 input_thread_t *p_input = NULL; /* thread descriptor */
134 /* Allocate descriptor */
135 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
136 VLC_OBJECT_INPUT, input_name );
137 if( p_input == NULL )
139 msg_Err( p_parent, "out of memory" );
143 /* Construct a nice name for the input timer */
144 char psz_timer_name[255];
145 char * psz_name = input_item_GetName( p_item );
146 snprintf( psz_timer_name, sizeof(psz_timer_name),
147 "input launching for '%s'", psz_name );
150 /* Start a timer to mesure how long it takes
151 * to launch an input */
152 stats_TimerStart( p_input, psz_timer_name,
153 STATS_TIMER_INPUT_LAUNCHING );
155 MALLOC_NULL( p_input->p, input_thread_private_t );
157 /* One "randomly" selected input thread is responsible for computing
158 * the global stats. Check if there is already someone doing this */
159 if( p_input->p_libvlc->p_stats && !b_quick )
161 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
162 if( p_input->p_libvlc->p_stats_computer == NULL )
164 p_input->p_libvlc->p_stats_computer = p_input;
166 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
169 p_input->b_preparsing = b_quick;
170 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
172 /* Init Common fields */
173 p_input->b_eof = false;
174 p_input->b_can_pace_control = true;
175 p_input->p->i_start = 0;
177 p_input->p->i_stop = 0;
178 p_input->p->i_run = 0;
179 p_input->p->i_title = 0;
180 p_input->p->title = NULL;
181 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
182 p_input->i_state = INIT_S;
183 p_input->p->i_rate = INPUT_RATE_DEFAULT;
184 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
185 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
186 p_input->p->p_es_out = NULL;
187 p_input->p->p_sout = NULL;
188 p_input->p->b_owns_its_sout = true;
189 p_input->p->b_sout_keep = false;
190 p_input->p->b_out_pace_control = false;
191 p_input->i_pts_delay = 0;
193 /* Init Input fields */
194 vlc_gc_incref( p_item ); /* Released in Destructor() */
195 p_input->p->input.p_item = p_item;
196 p_input->p->input.p_access = NULL;
197 p_input->p->input.p_stream = NULL;
198 p_input->p->input.p_demux = NULL;
199 p_input->p->input.b_title_demux = false;
200 p_input->p->input.i_title = 0;
201 p_input->p->input.title = NULL;
202 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
203 p_input->p->input.b_can_pace_control = true;
204 p_input->p->input.b_can_rate_control = true;
205 p_input->p->input.b_rescale_ts = true;
206 p_input->p->input.b_eof = false;
207 p_input->p->input.i_cr_average = 0;
209 vlc_mutex_lock( &p_item->lock );
211 if( !p_item->p_stats )
212 p_item->p_stats = stats_NewInputStats( p_input );
213 vlc_mutex_unlock( &p_item->lock );
216 p_input->p->i_slave = 0;
217 p_input->p->slave = NULL;
219 /* Init control buffer */
220 vlc_mutex_init( p_input, &p_input->p->lock_control );
221 p_input->p->i_control = 0;
223 /* Parse input options */
224 vlc_mutex_lock( &p_item->lock );
225 assert( p_item->optflagc == p_item->i_options );
226 for( i = 0; i < p_item->i_options; i++ )
227 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
228 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
229 vlc_mutex_unlock( &p_item->lock );
231 /* Create Object Variables for private use only */
232 input_ConfigVarInit( p_input );
234 /* Create Objects variables for public Get and Set */
235 input_ControlVarInit( p_input );
237 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
239 if( !p_input->b_preparsing )
241 var_Get( p_input, "bookmarks", &val );
244 /* FIXME: have a common cfg parsing routine used by sout and others */
245 char *psz_parser, *psz_start, *psz_end;
246 psz_parser = val.psz_string;
247 while( (psz_start = strchr( psz_parser, '{' ) ) )
249 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
252 psz_end = strchr( psz_start, '}' );
253 if( !psz_end ) break;
254 psz_parser = psz_end + 1;
255 backup = *psz_parser;
258 while( (psz_end = strchr( psz_start, ',' ) ) )
261 if( !strncmp( psz_start, "name=", 5 ) )
263 p_seekpoint->psz_name = strdup(psz_start + 5);
265 else if( !strncmp( psz_start, "bytes=", 6 ) )
267 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
269 else if( !strncmp( psz_start, "time=", 5 ) )
271 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
274 psz_start = psz_end + 1;
276 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
277 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
278 p_seekpoint->i_time_offset );
279 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
280 vlc_seekpoint_Delete( p_seekpoint );
281 *psz_parser = backup;
283 free( val.psz_string );
287 /* Remove 'Now playing' info as it is probably outdated */
288 input_item_SetNowPlaying( p_item, NULL );
291 if( p_input->b_preparsing )
292 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
297 p_input->p->p_sout = p_sout;
298 p_input->p->b_owns_its_sout = false;
301 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
302 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
304 /* Attach only once we are ready */
305 vlc_object_attach( p_input, p_parent );
307 /* Set the destructor when we are sure we are initialized */
308 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
314 * Input destructor (called when the object's refcount reaches 0).
316 static void Destructor( input_thread_t * p_input )
318 input_thread_private_t *priv = p_input->p;
320 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
321 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
323 if( priv->b_owns_its_sout && priv->p_sout )
325 if( priv->b_sout_keep )
326 SoutKeep( priv->p_sout );
329 sout_DeleteInstance( priv->p_sout );
334 vlc_gc_decref( p_input->p->input.p_item );
336 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
338 vlc_mutex_destroy( &priv->lock_control );
343 * Initialize an input thread and run it. You will need to monitor the
344 * thread to clean up after it is done
346 * \param p_parent a vlc_object
347 * \param p_item an input item
348 * \return a pointer to the spawned input thread
350 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
351 input_item_t *p_item )
354 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
355 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
357 if( !p_input && p_sout )
360 p_input->p->b_sout_keep = b_sout_keep;
365 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
366 input_item_t *p_item,
367 const char *psz_log, sout_instance_t *p_sout )
369 input_thread_t *p_input;
371 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
375 /* Create thread and wait for its readiness. */
376 if( vlc_thread_create( p_input, "input", Run,
377 VLC_THREAD_PRIORITY_INPUT, true ) )
379 input_ChangeState( p_input, ERROR_S );
380 msg_Err( p_input, "cannot create input thread" );
381 vlc_object_detach( p_input );
382 vlc_object_release( p_input );
390 * Initialize an input thread and run it. This thread will clean after himself,
391 * you can forget about it. It can work either in blocking or non-blocking mode
393 * \param p_parent a vlc_object
394 * \param p_item an input item
395 * \param b_block should we block until read is finished ?
396 * \return the input object id if non blocking, an error code else
398 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
402 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
403 input_thread_t *p_input;
405 p_input = Create( p_parent, p_item, NULL, false, p_sout );
406 if( !p_input && p_sout )
411 p_input->p->b_sout_keep = b_sout_keep;
415 RunAndDestroy( p_input );
420 if( vlc_thread_create( p_input, "input", RunAndDestroy,
421 VLC_THREAD_PRIORITY_INPUT, true ) )
423 input_ChangeState( p_input, ERROR_S );
424 msg_Err( p_input, "cannot create input thread" );
425 vlc_object_release( p_input );
429 return p_input->i_object_id;
433 * Initialize an input and initialize it to preparse the item
434 * This function is blocking. It will only accept to parse files
436 * \param p_parent a vlc_object_t
437 * \param p_item an input item
438 * \return VLC_SUCCESS or an error
440 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
442 input_thread_t *p_input;
444 /* Allocate descriptor */
445 p_input = Create( p_parent, p_item, NULL, true, NULL );
449 if( !Init( p_input ) )
452 vlc_object_detach( p_input );
453 vlc_object_release( p_input );
459 * Request a running input thread to stop and die
461 * \param the input thread to stop
463 void input_StopThread( input_thread_t *p_input )
468 /* Set die for input */
469 vlc_object_kill( p_input );
470 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
472 /* We cannot touch p_input fields directly (we come from another thread),
473 * so use the vlc_object_find way, it's perfectly safe */
475 /* Set die for all access */
476 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
477 for( i = 0; i < p_list->i_count; i++ )
479 vlc_object_kill( p_list->p_values[i].p_object );
481 vlc_list_release( p_list );
483 /* Set die for all stream */
484 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
485 for( i = 0; i < p_list->i_count; i++ )
487 vlc_object_kill( p_list->p_values[i].p_object );
489 vlc_list_release( p_list );
491 /* Set die for all demux */
492 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
493 for( i = 0; i < p_list->i_count; i++ )
495 vlc_object_kill( p_list->p_values[i].p_object );
497 vlc_list_release( p_list );
499 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
502 sout_instance_t * input_DetachSout( input_thread_t *p_input )
504 p_input->p->b_owns_its_sout = false;
505 return p_input->p->p_sout;
508 /*****************************************************************************
509 * Run: main thread loop
510 * This is the "normal" thread that spawns the input processing chain,
511 * reads the stream, cleans up and waits
512 *****************************************************************************/
513 static int Run( input_thread_t *p_input )
515 /* Signal that the thread is launched */
516 vlc_thread_ready( p_input );
518 if( Init( p_input ) )
520 /* If we failed, wait before we are killed, and exit */
521 p_input->b_error = true;
523 /* FIXME: we don't want to depend on the playlist */
524 playlist_t * p_playlist = vlc_object_find( p_input,
525 VLC_OBJECT_PLAYLIST, FIND_PARENT );
528 playlist_Signal( p_playlist );
529 vlc_object_release( p_playlist );
534 /* Tell we're dead */
535 p_input->b_dead = true;
542 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
544 /* We have finish to demux data but not to play them */
545 while( !p_input->b_die )
547 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
550 msg_Dbg( p_input, "waiting decoder fifos to empty" );
552 msleep( INPUT_IDLE_SLEEP );
555 /* We have finished */
556 p_input->b_eof = true;
557 playlist_Signal( pl_Get( p_input ) );
560 /* Wait until we are asked to die */
561 if( !p_input->b_die )
572 /*****************************************************************************
573 * RunAndDestroy: main thread loop
574 * This is the "just forget me" thread that spawns the input processing chain,
575 * reads the stream, cleans up and releases memory
576 *****************************************************************************/
577 static int RunAndDestroy( input_thread_t *p_input )
579 /* Signal that the thread is launched */
580 vlc_thread_ready( p_input );
582 if( Init( p_input ) )
587 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
589 /* We have finished demuxing data but not playing it */
590 while( !p_input->b_die )
592 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
595 msg_Dbg( p_input, "waiting decoder fifos to empty" );
597 msleep( INPUT_IDLE_SLEEP );
600 /* We have finished */
601 p_input->b_eof = true;
609 vlc_object_release( p_input );
613 /*****************************************************************************
614 * Main loop: Fill buffers from access, and demux
615 *****************************************************************************/
616 static void MainLoop( input_thread_t *p_input )
618 int64_t i_start_mdate = mdate();
619 int64_t i_intf_update = 0;
623 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
625 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
627 bool b_force_update = false;
633 if( p_input->i_state != PAUSE_S )
635 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
636 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
639 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
644 if( p_input->p->input.b_title_demux &&
645 p_input->p->input.p_demux->info.i_update )
647 i_ret = UpdateFromDemux( p_input );
648 b_force_update = true;
650 else if( !p_input->p->input.b_title_demux &&
651 p_input->p->input.p_access &&
652 p_input->p->input.p_access->info.i_update )
654 i_ret = UpdateFromAccess( p_input );
655 b_force_update = true;
659 if( i_ret == 0 ) /* EOF */
663 var_Get( p_input, "input-repeat", &repeat );
664 if( repeat.i_int == 0 )
666 /* End of file - we do not set b_die because only the
667 * playlist is allowed to do so. */
668 input_ChangeState( p_input, END_S );
669 msg_Dbg( p_input, "EOF reached" );
670 p_input->p->input.b_eof = true;
674 msg_Dbg( p_input, "repeating the same input (%d)",
676 if( repeat.i_int > 0 )
679 var_Set( p_input, "input-repeat", repeat );
682 /* Seek to start title/seekpoint */
683 val.i_int = p_input->p->input.i_title_start -
684 p_input->p->input.i_title_offset;
685 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
687 input_ControlPush( p_input,
688 INPUT_CONTROL_SET_TITLE, &val );
690 val.i_int = p_input->p->input.i_seekpoint_start -
691 p_input->p->input.i_seekpoint_offset;
692 if( val.i_int > 0 /* TODO: check upper boundary */ )
693 input_ControlPush( p_input,
694 INPUT_CONTROL_SET_SEEKPOINT, &val );
696 /* Seek to start position */
697 if( p_input->p->i_start > 0 )
699 val.i_time = p_input->p->i_start;
700 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
706 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
711 i_start_mdate = mdate();
716 p_input->b_error = true;
719 if( i_ret > 0 && p_input->p->i_slave > 0 )
721 SlaveDemux( p_input );
731 vlc_mutex_lock( &p_input->p->lock_control );
732 ControlReduce( p_input );
733 while( !ControlPopNoLock( p_input, &i_type, &val ) )
735 msg_Dbg( p_input, "control type=%d", i_type );
736 if( Control( p_input, i_type, val ) )
737 b_force_update = true;
739 vlc_mutex_unlock( &p_input->p->lock_control );
741 if( b_force_update || i_intf_update < mdate() )
745 int64_t i_time, i_length;
746 /* update input status variables */
747 if( !demux_Control( p_input->p->input.p_demux,
748 DEMUX_GET_POSITION, &f_pos ) )
750 val.f_float = (float)f_pos;
751 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
753 if( !demux_Control( p_input->p->input.p_demux,
754 DEMUX_GET_TIME, &i_time ) )
756 p_input->i_time = i_time;
758 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
760 if( !demux_Control( p_input->p->input.p_demux,
761 DEMUX_GET_LENGTH, &i_length ) )
764 var_Get( p_input, "length", &old_val );
765 val.i_time = i_length;
766 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
768 if( old_val.i_time != val.i_time )
770 UpdateItemLength( p_input, i_length );
774 var_SetBool( p_input, "intf-change", true );
775 i_intf_update = mdate() + I64C(150000);
777 /* 150ms * 8 = ~ 1 second */
778 if( ++i_updates % 8 == 0 )
780 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
781 /* Are we the thread responsible for computing global stats ? */
782 if( p_input->p_libvlc->p_stats_computer == p_input )
784 stats_ComputeGlobalStats( p_input->p_libvlc,
785 p_input->p_libvlc->p_stats );
791 static void InitStatistics( input_thread_t * p_input )
793 if( p_input->b_preparsing ) return;
795 /* Prepare statistics */
796 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
797 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
798 if( p_input->p_libvlc->b_stats )
800 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
801 INIT_COUNTER( read_packets, INTEGER, COUNTER );
802 INIT_COUNTER( demux_read, INTEGER, COUNTER );
803 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
804 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
805 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
806 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
807 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
808 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
809 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
810 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
811 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
812 p_input->p->counters.p_sout_send_bitrate = NULL;
813 p_input->p->counters.p_sout_sent_packets = NULL;
814 p_input->p->counters.p_sout_sent_bytes = NULL;
815 if( p_input->p->counters.p_demux_bitrate )
816 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
817 if( p_input->p->counters.p_input_bitrate )
818 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
822 static int InitSout( input_thread_t * p_input )
826 if( p_input->b_preparsing ) return VLC_SUCCESS;
828 /* Find a usable sout and attach it to p_input */
829 psz = var_GetNonEmptyString( p_input, "sout" );
830 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
832 /* Check the validity of the provided sout */
833 if( p_input->p->p_sout )
835 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
837 msg_Dbg( p_input, "destroying unusable sout" );
839 sout_DeleteInstance( p_input->p->p_sout );
840 p_input->p->p_sout = NULL;
844 if( p_input->p->p_sout )
847 msg_Dbg( p_input, "sout keep: reusing sout" );
848 msg_Dbg( p_input, "sout keep: you probably want to use "
849 "gather stream_out" );
850 vlc_object_attach( p_input->p->p_sout, p_input );
854 /* Create a new one */
855 p_input->p->p_sout = sout_NewInstance( p_input, psz );
856 if( !p_input->p->p_sout )
858 input_ChangeState( p_input, ERROR_S );
859 msg_Err( p_input, "cannot start stream output instance, " \
865 if( p_input->p_libvlc->b_stats )
867 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
868 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
869 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
870 if( p_input->p->counters.p_sout_send_bitrate )
871 p_input->p->counters.p_sout_send_bitrate->update_interval =
875 else if( p_input->p->p_sout )
877 msg_Dbg( p_input, "destroying useless sout" );
879 sout_DeleteInstance( p_input->p->p_sout );
880 p_input->p->p_sout = NULL;
887 static void InitTitle( input_thread_t * p_input )
891 if( p_input->b_preparsing ) return;
893 /* Create global title (from master) */
894 p_input->p->i_title = p_input->p->input.i_title;
895 p_input->p->title = p_input->p->input.title;
896 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
897 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
898 if( p_input->p->i_title > 0 )
900 /* Setup variables */
901 input_ControlVarNavigation( p_input );
902 input_ControlVarTitle( p_input, 0 );
906 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
907 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
908 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
911 if( p_input->i_pts_delay < 0 )
912 p_input->i_pts_delay = 0;
914 /* If the desynchronisation requested by the user is < 0, we need to
915 * cache more data. */
916 var_Get( p_input, "audio-desync", &val );
917 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
919 /* Update cr_average depending on the caching */
920 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
921 p_input->p->input.i_cr_average /= 10;
922 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
925 static void StartTitle( input_thread_t * p_input )
933 /* Start title/chapter */
935 if( p_input->b_preparsing )
937 p_input->p->i_start = 0;
941 val.i_int = p_input->p->input.i_title_start -
942 p_input->p->input.i_title_offset;
943 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
944 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
945 val.i_int = p_input->p->input.i_seekpoint_start -
946 p_input->p->input.i_seekpoint_offset;
947 if( val.i_int > 0 /* TODO: check upper boundary */ )
948 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
952 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
953 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
954 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
955 if( p_input->p->i_run < 0 )
957 msg_Warn( p_input, "invalid run-time ignored" );
958 p_input->p->i_run = 0;
961 if( p_input->p->i_start > 0 )
963 if( p_input->p->i_start >= val.i_time )
965 msg_Warn( p_input, "invalid start-time ignored" );
971 msg_Dbg( p_input, "starting at time: %ds",
972 (int)( p_input->p->i_start / I64C(1000000) ) );
974 s.i_time = p_input->p->i_start;
975 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
978 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
980 msg_Warn( p_input, "invalid stop-time ignored" );
981 p_input->p->i_stop = 0;
985 /* Get fps and set it if not already set */
986 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
989 float f_requested_fps;
991 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
992 var_SetFloat( p_input, "sub-original-fps", f_fps );
994 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
995 if( f_requested_fps != f_fps )
997 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
999 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1003 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1006 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1009 /* Look for and add subtitle files */
1010 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1011 if( psz_subtitle != NULL )
1013 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1014 input_AddSubtitles( p_input, psz_subtitle, false );
1017 var_Get( p_input, "sub-autodetect-file", &val );
1020 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1021 char **subs = subtitles_Detect( p_input, psz_autopath,
1022 p_input->p->input.p_item->psz_uri );
1023 input_source_t *sub;
1025 if( psz_autopath == NULL )
1026 psz_autopath = strdup("");
1028 /* Try to autoselect the first autodetected subtitles file
1029 * if no subtitles file was specified */
1030 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1032 input_AddSubtitles( p_input, subs[0], false );
1037 /* Then, just add the following subtitles files */
1038 for( ; subs && subs[i]; i++ )
1040 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1042 sub = InputSourceNew( p_input );
1043 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1045 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1052 free( psz_autopath );
1054 free( psz_subtitle );
1056 /* Look for slave */
1057 psz = var_GetNonEmptyString( p_input, "input-slave" );
1061 input_source_t *slave;
1062 while( psz && *psz )
1064 while( *psz == ' ' || *psz == '#' )
1068 if( ( psz_delim = strchr( psz, '#' ) ) )
1070 *psz_delim++ = '\0';
1077 msg_Dbg( p_input, "adding slave input '%s'", psz );
1078 slave = InputSourceNew( p_input );
1079 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1081 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1090 static void InitPrograms( input_thread_t * p_input )
1095 if( p_input->b_preparsing ) return;
1098 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1099 i_es_out_mode = ES_OUT_MODE_AUTO;
1101 if( p_input->p->p_sout )
1103 var_Get( p_input, "sout-all", &val );
1106 i_es_out_mode = ES_OUT_MODE_ALL;
1111 var_Get( p_input, "programs", &val );
1112 if ( val.p_list && val.p_list->i_count )
1114 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1115 /* Note : we should remove the "program" callback. */
1118 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1122 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1124 /* Inform the demuxer about waited group (needed only for DVB) */
1125 if( i_es_out_mode == ES_OUT_MODE_ALL )
1127 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1129 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1131 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1136 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1137 (int) var_GetInteger( p_input, "program" ), NULL );
1141 static int Init( input_thread_t * p_input )
1147 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1149 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1151 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1152 var_SetString( p_input, "sout", "" );
1153 var_SetBool( p_input, "sout-all", false );
1154 var_SetString( p_input, "input-slave", "" );
1155 var_SetInteger( p_input, "input-repeat", 0 );
1156 var_SetString( p_input, "sub-file", "" );
1157 var_SetBool( p_input, "sub-autodetect-file", false );
1161 InitStatistics( p_input );
1162 ret = InitSout( p_input );
1164 if( ret != VLC_SUCCESS )
1165 return ret; /* FIXME: goto error; should be better here */
1168 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1169 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1170 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1172 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1173 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1175 if( InputSourceInit( p_input, &p_input->p->input,
1176 p_input->p->input.p_item->psz_uri, NULL ) )
1181 InitTitle( p_input );
1183 /* Load master infos */
1185 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1186 &val.i_time ) && val.i_time > 0 )
1188 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1189 UpdateItemLength( p_input, val.i_time );
1193 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1194 if( val.i_time > 0 )
1195 { /* fallback: gets length from metadata */
1196 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1197 UpdateItemLength( p_input, val.i_time );
1201 StartTitle( p_input );
1203 InitPrograms( p_input );
1205 if( !p_input->b_preparsing && p_input->p->p_sout )
1207 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1209 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1211 /* We don't want a high input priority here or we'll
1212 * end-up sucking up all the CPU time */
1213 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1216 msg_Dbg( p_input, "starting in %s mode",
1217 p_input->p->b_out_pace_control ? "async" : "sync" );
1220 p_meta = vlc_meta_New();
1222 /* Get meta data from users */
1223 InputMetaUser( p_input, p_meta );
1225 /* Get meta data from master input */
1226 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1228 /* Access_file does not give any meta, and there are no slave */
1229 AccessMeta( p_input, p_meta );
1231 InputUpdateMeta( p_input, p_meta );
1233 if( !p_input->b_preparsing )
1235 msg_Dbg( p_input, "`%s' successfully opened",
1236 p_input->p->input.p_item->psz_uri );
1240 /* initialization is complete */
1241 input_ChangeState( p_input, PLAYING_S );
1246 input_ChangeState( p_input, ERROR_S );
1248 if( p_input->p->p_es_out )
1249 input_EsOutDelete( p_input->p->p_es_out );
1251 if( p_input->p->p_sout )
1253 vlc_object_detach( p_input->p->p_sout );
1254 sout_DeleteInstance( p_input->p->p_sout );
1258 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1260 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1261 stats_CounterClean( p_input->p->counters.p_##c );\
1262 p_input->p->counters.p_##c = NULL; } while(0)
1263 EXIT_COUNTER( read_bytes );
1264 EXIT_COUNTER( read_packets );
1265 EXIT_COUNTER( demux_read );
1266 EXIT_COUNTER( input_bitrate );
1267 EXIT_COUNTER( demux_bitrate );
1268 EXIT_COUNTER( played_abuffers );
1269 EXIT_COUNTER( lost_abuffers );
1270 EXIT_COUNTER( displayed_pictures );
1271 EXIT_COUNTER( lost_pictures );
1272 EXIT_COUNTER( decoded_audio );
1273 EXIT_COUNTER( decoded_video );
1274 EXIT_COUNTER( decoded_sub );
1276 if( p_input->p->p_sout )
1278 EXIT_COUNTER( sout_sent_packets );
1279 EXIT_COUNTER (sout_sent_bytes );
1280 EXIT_COUNTER( sout_send_bitrate );
1285 /* Mark them deleted */
1286 p_input->p->input.p_demux = NULL;
1287 p_input->p->input.p_stream = NULL;
1288 p_input->p->input.p_access = NULL;
1289 p_input->p->p_es_out = NULL;
1290 p_input->p->p_sout = NULL;
1292 return VLC_EGENERIC;
1295 /*****************************************************************************
1296 * Error: RunThread() error loop
1297 *****************************************************************************
1298 * This function is called when an error occurred during thread main's loop.
1299 *****************************************************************************/
1300 static void Error( input_thread_t *p_input )
1302 input_ChangeState( p_input, ERROR_S );
1303 while( !p_input->b_die )
1306 msleep( INPUT_IDLE_SLEEP );
1310 /*****************************************************************************
1311 * End: end the input thread
1312 *****************************************************************************/
1313 static void End( input_thread_t * p_input )
1317 /* We are at the end */
1318 input_ChangeState( p_input, END_S );
1320 /* Clean control variables */
1321 input_ControlVarClean( p_input );
1323 /* Clean up master */
1324 InputSourceClean( &p_input->p->input );
1327 for( i = 0; i < p_input->p->i_slave; i++ )
1329 InputSourceClean( p_input->p->slave[i] );
1330 free( p_input->p->slave[i] );
1332 free( p_input->p->slave );
1334 /* Unload all modules */
1335 if( p_input->p->p_es_out )
1336 input_EsOutDelete( p_input->p->p_es_out );
1338 if( !p_input->b_preparsing )
1340 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1341 if( p_input->p_libvlc->b_stats )
1343 /* make sure we are up to date */
1344 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1345 if( p_input->p_libvlc->p_stats_computer == p_input )
1347 stats_ComputeGlobalStats( p_input->p_libvlc,
1348 p_input->p_libvlc->p_stats );
1349 p_input->p_libvlc->p_stats_computer = NULL;
1351 CL_CO( read_bytes );
1352 CL_CO( read_packets );
1353 CL_CO( demux_read );
1354 CL_CO( input_bitrate );
1355 CL_CO( demux_bitrate );
1356 CL_CO( played_abuffers );
1357 CL_CO( lost_abuffers );
1358 CL_CO( displayed_pictures );
1359 CL_CO( lost_pictures );
1360 CL_CO( decoded_audio) ;
1361 CL_CO( decoded_video );
1362 CL_CO( decoded_sub) ;
1365 /* Close optional stream output instance */
1366 if( p_input->p->p_sout )
1368 CL_CO( sout_sent_packets );
1369 CL_CO( sout_sent_bytes );
1370 CL_CO( sout_send_bitrate );
1372 vlc_object_detach( p_input->p->p_sout );
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 );
1384 /* Tell we're dead */
1385 p_input->b_dead = true;
1388 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, bool *pb_sout_keep )
1390 bool b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1391 sout_instance_t *p_sout = NULL;
1394 /* Search sout-keep options
1395 * XXX it has to be done here, but it is duplicated work :( */
1396 vlc_mutex_lock( &p_item->lock );
1397 for( i = 0; i < p_item->i_options; i++ )
1399 const char *psz_option = p_item->ppsz_options[i];
1402 if( *psz_option == ':' )
1405 if( !strcmp( psz_option, "sout-keep" ) )
1407 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1408 b_keep_sout = false;
1410 vlc_mutex_unlock( &p_item->lock );
1412 /* Find a potential sout to reuse
1413 * XXX it might be unusable but this will be checked later */
1416 p_sout = vlc_object_find( p_parent->p_libvlc, VLC_OBJECT_SOUT,
1420 if( p_sout->p_parent != VLC_OBJECT( p_sout->p_libvlc ) )
1422 vlc_object_release( p_sout );
1427 vlc_object_detach( p_sout ); /* Remove it from the GC */
1428 vlc_object_release( p_sout );
1434 *pb_sout_keep = b_keep_sout;
1439 static void SoutKeep( sout_instance_t *p_sout )
1441 msg_Dbg( p_sout, "sout has been kept" );
1442 vlc_object_attach( p_sout, p_sout->p_libvlc );
1445 /*****************************************************************************
1447 *****************************************************************************/
1448 static inline int ControlPopNoLock( input_thread_t *p_input,
1449 int *pi_type, vlc_value_t *p_val )
1451 if( p_input->p->i_control <= 0 )
1453 return VLC_EGENERIC;
1456 *pi_type = p_input->p->control[0].i_type;
1457 *p_val = p_input->p->control[0].val;
1459 p_input->p->i_control--;
1460 if( p_input->p->i_control > 0 )
1464 for( i = 0; i < p_input->p->i_control; i++ )
1466 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1467 p_input->p->control[i].val = p_input->p->control[i+1].val;
1474 static void ControlReduce( input_thread_t *p_input )
1481 for( i = 1; i < p_input->p->i_control; i++ )
1483 const int i_lt = p_input->p->control[i-1].i_type;
1484 const int i_ct = p_input->p->control[i].i_type;
1486 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1487 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1491 ( i_ct == INPUT_CONTROL_SET_STATE ||
1492 i_ct == INPUT_CONTROL_SET_RATE ||
1493 i_ct == INPUT_CONTROL_SET_POSITION ||
1494 i_ct == INPUT_CONTROL_SET_TIME ||
1495 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1496 i_ct == INPUT_CONTROL_SET_TITLE ||
1497 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1498 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1501 // msg_Dbg( p_input, "merged at %d", i );
1502 /* Remove the i-1 */
1503 for( j = i; j < p_input->p->i_control; j++ )
1504 p_input->p->control[j-1] = p_input->p->control[j];
1505 p_input->p->i_control--;
1509 /* TODO but that's not that important
1510 - merge SET_X with SET_X_CMD
1511 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1512 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1519 static bool Control( input_thread_t *p_input, int i_type,
1522 bool b_force_update = false;
1524 if( !p_input ) return b_force_update;
1528 case INPUT_CONTROL_SET_DIE:
1529 msg_Dbg( p_input, "control: stopping input" );
1530 /* Mark all submodules to die */
1531 if( p_input->p->input.p_access )
1532 vlc_object_kill( p_input->p->input.p_access );
1533 if( p_input->p->input.p_stream )
1534 vlc_object_kill( p_input->p->input.p_stream );
1535 vlc_object_kill( p_input->p->input.p_demux );
1537 vlc_object_kill( p_input );
1540 case INPUT_CONTROL_SET_POSITION:
1541 case INPUT_CONTROL_SET_POSITION_OFFSET:
1544 if( i_type == INPUT_CONTROL_SET_POSITION )
1546 f_pos = val.f_float;
1550 /* Should not fail */
1551 demux_Control( p_input->p->input.p_demux,
1552 DEMUX_GET_POSITION, &f_pos );
1553 f_pos += val.f_float;
1555 if( f_pos < 0.0 ) f_pos = 0.0;
1556 if( f_pos > 1.0 ) f_pos = 1.0;
1557 /* Reset the decoders states and clock sync (before calling the demuxer */
1558 input_EsOutChangePosition( p_input->p->p_es_out );
1559 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1562 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1563 "%2.1f%% failed", f_pos * 100 );
1567 if( p_input->p->i_slave > 0 )
1568 SlaveSeek( p_input );
1570 b_force_update = true;
1575 case INPUT_CONTROL_SET_TIME:
1576 case INPUT_CONTROL_SET_TIME_OFFSET:
1581 if( i_type == INPUT_CONTROL_SET_TIME )
1583 i_time = val.i_time;
1587 /* Should not fail */
1588 demux_Control( p_input->p->input.p_demux,
1589 DEMUX_GET_TIME, &i_time );
1590 i_time += val.i_time;
1592 if( i_time < 0 ) i_time = 0;
1594 /* Reset the decoders states and clock sync (before calling the demuxer */
1595 input_EsOutChangePosition( p_input->p->p_es_out );
1597 i_ret = demux_Control( p_input->p->input.p_demux,
1598 DEMUX_SET_TIME, i_time );
1603 /* Emulate it with a SET_POS */
1604 demux_Control( p_input->p->input.p_demux,
1605 DEMUX_GET_LENGTH, &i_length );
1608 double f_pos = (double)i_time / (double)i_length;
1609 i_ret = demux_Control( p_input->p->input.p_demux,
1610 DEMUX_SET_POSITION, f_pos );
1615 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1616 " failed or not possible", i_time );
1620 if( p_input->p->i_slave > 0 )
1621 SlaveSeek( p_input );
1623 b_force_update = true;
1628 case INPUT_CONTROL_SET_STATE:
1629 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1630 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1633 if( p_input->p->input.p_access )
1634 i_ret = access_Control( p_input->p->input.p_access,
1635 ACCESS_SET_PAUSE_STATE, false );
1637 i_ret = demux_Control( p_input->p->input.p_demux,
1638 DEMUX_SET_PAUSE_STATE, false );
1642 /* FIXME What to do ? */
1643 msg_Warn( p_input, "cannot unset pause -> EOF" );
1644 vlc_mutex_unlock( &p_input->p->lock_control );
1645 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1646 vlc_mutex_lock( &p_input->p->lock_control );
1649 b_force_update = true;
1651 /* Switch to play */
1652 p_input->i_state = PLAYING_S;
1653 val.i_int = PLAYING_S;
1654 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1658 input_EsOutChangeState( p_input->p->p_es_out );
1660 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1661 p_input->p->b_can_pause )
1664 if( p_input->p->input.p_access )
1665 i_ret = access_Control( p_input->p->input.p_access,
1666 ACCESS_SET_PAUSE_STATE, true );
1668 i_ret = demux_Control( p_input->p->input.p_demux,
1669 DEMUX_SET_PAUSE_STATE, true );
1671 b_force_update = true;
1675 msg_Warn( p_input, "cannot set pause state" );
1676 val.i_int = p_input->i_state;
1680 val.i_int = PAUSE_S;
1683 /* Switch to new state */
1684 p_input->i_state = val.i_int;
1685 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1689 input_EsOutChangeState( p_input->p->p_es_out );
1691 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1693 b_force_update = true;
1695 /* Correct "state" value */
1696 val.i_int = p_input->i_state;
1697 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1699 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1701 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1705 case INPUT_CONTROL_SET_RATE:
1706 case INPUT_CONTROL_SET_RATE_SLOWER:
1707 case INPUT_CONTROL_SET_RATE_FASTER:
1711 if( i_type == INPUT_CONTROL_SET_RATE )
1717 static const int ppi_factor[][2] = {
1718 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1720 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1729 for( i = 0; ppi_factor[i][0] != 0; i++ )
1731 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1732 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1733 if( i_test_e < i_error )
1739 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1741 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1743 if( ppi_factor[i_idx+1][0] > 0 )
1744 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1746 i_rate = INPUT_RATE_MAX+1;
1750 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1752 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1754 i_rate = INPUT_RATE_MIN-1;
1758 if( i_rate < INPUT_RATE_MIN )
1760 msg_Dbg( p_input, "cannot set rate faster" );
1761 i_rate = INPUT_RATE_MIN;
1763 else if( i_rate > INPUT_RATE_MAX )
1765 msg_Dbg( p_input, "cannot set rate slower" );
1766 i_rate = INPUT_RATE_MAX;
1768 if( i_rate != INPUT_RATE_DEFAULT &&
1769 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1770 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1772 msg_Dbg( p_input, "cannot change rate" );
1773 i_rate = INPUT_RATE_DEFAULT;
1775 if( i_rate != p_input->p->i_rate &&
1776 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1779 if( p_input->p->input.p_access )
1780 i_ret = VLC_EGENERIC;
1782 i_ret = demux_Control( p_input->p->input.p_demux,
1783 DEMUX_SET_RATE, &i_rate );
1786 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1787 i_rate = p_input->p->i_rate;
1792 if( i_rate != p_input->p->i_rate )
1795 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1796 var_SetBool( p_input, "rate-change", true );
1798 p_input->p->i_rate = i_rate;
1800 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1801 if( p_input->p->input.b_rescale_ts )
1802 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1804 b_force_update = true;
1809 case INPUT_CONTROL_SET_PROGRAM:
1810 /* No need to force update, es_out does it if needed */
1811 es_out_Control( p_input->p->p_es_out,
1812 ES_OUT_SET_GROUP, val.i_int );
1814 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1818 case INPUT_CONTROL_SET_ES:
1819 /* No need to force update, es_out does it if needed */
1820 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1821 input_EsOutGetFromID( p_input->p->p_es_out,
1825 case INPUT_CONTROL_SET_AUDIO_DELAY:
1826 input_EsOutSetDelay( p_input->p->p_es_out,
1827 AUDIO_ES, val.i_time );
1828 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1831 case INPUT_CONTROL_SET_SPU_DELAY:
1832 input_EsOutSetDelay( p_input->p->p_es_out,
1833 SPU_ES, val.i_time );
1834 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1837 case INPUT_CONTROL_SET_TITLE:
1838 case INPUT_CONTROL_SET_TITLE_NEXT:
1839 case INPUT_CONTROL_SET_TITLE_PREV:
1840 if( p_input->p->input.b_title_demux &&
1841 p_input->p->input.i_title > 0 )
1844 /* FIXME handle demux title */
1845 demux_t *p_demux = p_input->p->input.p_demux;
1848 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1849 i_title = p_demux->info.i_title - 1;
1850 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1851 i_title = p_demux->info.i_title + 1;
1853 i_title = val.i_int;
1855 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1857 input_EsOutChangePosition( p_input->p->p_es_out );
1859 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1860 input_ControlVarTitle( p_input, i_title );
1863 else if( p_input->p->input.i_title > 0 )
1865 access_t *p_access = p_input->p->input.p_access;
1868 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1869 i_title = p_access->info.i_title - 1;
1870 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1871 i_title = p_access->info.i_title + 1;
1873 i_title = val.i_int;
1875 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1877 input_EsOutChangePosition( p_input->p->p_es_out );
1879 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1880 stream_AccessReset( p_input->p->input.p_stream );
1884 case INPUT_CONTROL_SET_SEEKPOINT:
1885 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1886 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1887 if( p_input->p->input.b_title_demux &&
1888 p_input->p->input.i_title > 0 )
1890 demux_t *p_demux = p_input->p->input.p_demux;
1892 int64_t i_input_time;
1893 int64_t i_seekpoint_time;
1895 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1897 i_seekpoint = p_demux->info.i_seekpoint;
1898 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1899 if( i_seekpoint_time >= 0 &&
1900 !demux_Control( p_demux,
1901 DEMUX_GET_TIME, &i_input_time ) )
1903 if ( i_input_time < i_seekpoint_time + 3000000 )
1909 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1910 i_seekpoint = p_demux->info.i_seekpoint + 1;
1912 i_seekpoint = val.i_int;
1914 if( i_seekpoint >= 0 && i_seekpoint <
1915 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1918 input_EsOutChangePosition( p_input->p->p_es_out );
1920 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1923 else if( p_input->p->input.i_title > 0 )
1925 demux_t *p_demux = p_input->p->input.p_demux;
1926 access_t *p_access = p_input->p->input.p_access;
1928 int64_t i_input_time;
1929 int64_t i_seekpoint_time;
1931 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1933 i_seekpoint = p_access->info.i_seekpoint;
1934 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1935 if( i_seekpoint_time >= 0 &&
1936 demux_Control( p_demux,
1937 DEMUX_GET_TIME, &i_input_time ) )
1939 if ( i_input_time < i_seekpoint_time + 3000000 )
1945 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1946 i_seekpoint = p_access->info.i_seekpoint + 1;
1948 i_seekpoint = val.i_int;
1950 if( i_seekpoint >= 0 && i_seekpoint <
1951 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1953 input_EsOutChangePosition( p_input->p->p_es_out );
1955 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1957 stream_AccessReset( p_input->p->input.p_stream );
1962 case INPUT_CONTROL_ADD_SLAVE:
1963 if( val.psz_string )
1965 input_source_t *slave = InputSourceNew( p_input );
1967 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1973 msg_Dbg( p_input, "adding %s as slave on the fly",
1977 if( demux_Control( p_input->p->input.p_demux,
1978 DEMUX_GET_TIME, &i_time ) )
1980 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1981 InputSourceClean( slave );
1985 if( demux_Control( slave->p_demux,
1986 DEMUX_SET_TIME, i_time ) )
1988 msg_Err( p_input, "seek failed for new slave" );
1989 InputSourceClean( slave );
1994 /* Get meta (access and demux) */
1995 p_meta = vlc_meta_New();
1996 access_Control( slave->p_access, ACCESS_GET_META,
1998 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1999 InputUpdateMeta( p_input, p_meta );
2001 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2006 msg_Warn( p_input, "failed to add %s as slave",
2010 free( val.psz_string );
2014 case INPUT_CONTROL_SET_BOOKMARK:
2016 msg_Err( p_input, "not yet implemented" );
2020 return b_force_update;
2023 /*****************************************************************************
2025 *****************************************************************************/
2026 static int UpdateFromDemux( input_thread_t *p_input )
2028 demux_t *p_demux = p_input->p->input.p_demux;
2031 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2033 v.i_int = p_demux->info.i_title;
2034 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2036 input_ControlVarTitle( p_input, p_demux->info.i_title );
2038 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2040 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2042 v.i_int = p_demux->info.i_seekpoint;
2043 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2045 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2047 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2049 /* Hmmm only works with master input */
2050 if( p_input->p->input.p_demux == p_demux )
2052 int i_title_end = p_input->p->input.i_title_end -
2053 p_input->p->input.i_title_offset;
2054 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2055 p_input->p->input.i_seekpoint_offset;
2057 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2059 if( p_demux->info.i_title > i_title_end ||
2060 ( p_demux->info.i_title == i_title_end &&
2061 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2063 else if( i_seekpoint_end >=0 )
2065 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2067 else if( i_title_end >= 0 )
2069 if( p_demux->info.i_title > i_title_end ) return 0;
2076 /*****************************************************************************
2078 *****************************************************************************/
2079 static int UpdateFromAccess( input_thread_t *p_input )
2081 access_t *p_access = p_input->p->input.p_access;
2084 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2086 v.i_int = p_access->info.i_title;
2087 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2089 input_ControlVarTitle( p_input, p_access->info.i_title );
2091 stream_AccessUpdate( p_input->p->input.p_stream );
2093 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2095 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2097 v.i_int = p_access->info.i_seekpoint;
2098 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2099 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2101 if( p_access->info.i_update & INPUT_UPDATE_META )
2103 /* TODO maybe multi - access ? */
2104 vlc_meta_t *p_meta = vlc_meta_New();
2105 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2106 InputUpdateMeta( p_input, p_meta );
2107 var_SetInteger( pl_Get( p_input ), "item-change", p_input->p->input.p_item->i_id );
2108 p_access->info.i_update &= ~INPUT_UPDATE_META;
2111 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2113 /* Hmmm only works with master input */
2114 if( p_input->p->input.p_access == p_access )
2116 int i_title_end = p_input->p->input.i_title_end -
2117 p_input->p->input.i_title_offset;
2118 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2119 p_input->p->input.i_seekpoint_offset;
2121 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2123 if( p_access->info.i_title > i_title_end ||
2124 ( p_access->info.i_title == i_title_end &&
2125 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2127 else if( i_seekpoint_end >=0 )
2129 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2131 else if( i_title_end >= 0 )
2133 if( p_access->info.i_title > i_title_end ) return 0;
2140 /*****************************************************************************
2142 *****************************************************************************/
2143 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2145 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2147 if( !p_input->b_preparsing )
2149 pl_Yield( p_input );
2150 var_SetInteger( pl_Get( p_input ), "item-change",
2151 p_input->p->input.p_item->i_id );
2152 pl_Release( p_input );
2156 /*****************************************************************************
2158 *****************************************************************************/
2159 static input_source_t *InputSourceNew( input_thread_t *p_input )
2161 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2165 msg_Err( p_input, "out of memory for new input source" );
2170 in->p_access = NULL;
2171 in->p_stream = NULL;
2173 in->b_title_demux = false;
2174 TAB_INIT( in->i_title, in->title );
2175 in->b_can_pause = true;
2176 in->b_can_pace_control = true;
2177 in->b_can_rate_control = true;
2178 in->b_rescale_ts = true;
2181 in->i_cr_average = 0;
2186 /*****************************************************************************
2188 *****************************************************************************/
2189 static int InputSourceInit( input_thread_t *p_input,
2190 input_source_t *in, const char *psz_mrl,
2191 const char *psz_forced_demux )
2193 char psz_dup[strlen (psz_mrl) + 1];
2194 const char *psz_access;
2195 const char *psz_demux;
2202 strcpy( psz_dup, psz_mrl );
2204 if( !in ) return VLC_EGENERIC;
2205 if( !p_input ) return VLC_EGENERIC;
2208 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2210 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2211 psz_mrl, psz_access, psz_demux, psz_path );
2212 if( !p_input->b_preparsing )
2214 /* Hack to allow udp://@:port syntax */
2216 (strncmp( psz_access, "udp", 3 ) &&
2217 strncmp( psz_access, "rtp", 3 )) )
2219 /* Find optional titles and seekpoints */
2220 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2221 &in->i_seekpoint_start, &in->i_seekpoint_end );
2224 if( psz_forced_demux && *psz_forced_demux )
2226 psz_demux = psz_forced_demux;
2228 else if( *psz_demux == '\0' )
2230 /* special hack for forcing a demuxer with --demux=module
2231 * (and do nothing with a list) */
2232 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2234 if( psz_var_demux != NULL &&
2235 !strchr(psz_var_demux, ',' ) &&
2236 !strchr(psz_var_demux, ':' ) )
2238 psz_demux = psz_var_demux;
2240 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2244 /* Try access_demux if no demux given */
2245 if( *psz_demux == '\0' )
2247 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2248 NULL, p_input->p->p_es_out, false );
2253 /* Preparsing is only for file:// */
2256 if( !*psz_access ) /* path without scheme:// */
2257 psz_access = "file";
2258 if( strcmp( psz_access, "file" ) )
2260 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2265 int64_t i_pts_delay;
2267 /* Get infos from access_demux */
2268 demux_Control( in->p_demux,
2269 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2270 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2272 in->b_title_demux = true;
2273 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2274 &in->title, &in->i_title,
2275 &in->i_title_offset, &in->i_seekpoint_offset ) )
2280 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2281 &in->b_can_pace_control ) )
2282 in->b_can_pace_control = false;
2284 if( !in->b_can_pace_control )
2286 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2287 &in->b_can_rate_control, &in->b_rescale_ts ) )
2289 in->b_can_rate_control = false;
2290 in->b_rescale_ts = true; /* not used */
2295 in->b_can_rate_control = true;
2296 in->b_rescale_ts = true;
2298 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2299 &in->b_can_pause ) )
2300 in->b_can_pause = false;
2301 var_SetBool( p_input, "can-pause", in->b_can_pause );
2303 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2305 if( ret != VLC_SUCCESS )
2307 var_Set( p_input, "seekable", val );
2311 int64_t i_pts_delay;
2313 input_ChangeState( p_input, OPENING_S );
2315 /* Now try a real access */
2316 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2318 /* Access failed, URL encoded ? */
2319 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2321 decode_URI( psz_path );
2323 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2324 psz_access, psz_demux, psz_path );
2326 in->p_access = access_New( p_input,
2327 psz_access, psz_demux, psz_path );
2329 if( in->p_access == NULL )
2331 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2333 intf_UserFatal( VLC_OBJECT( p_input), false,
2334 _("Your input can't be opened"),
2335 _("VLC is unable to open the MRL '%s'."
2336 " Check the log for details."), psz_mrl );
2341 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2342 while( psz && *psz )
2344 access_t *p_access = in->p_access;
2345 char *end = strchr( psz, ':' );
2350 in->p_access = access_FilterNew( in->p_access, psz );
2351 if( in->p_access == NULL )
2353 in->p_access = p_access;
2354 msg_Warn( p_input, "failed to insert access filter %s",
2362 /* Get infos from access */
2363 if( !p_input->b_preparsing )
2365 access_Control( in->p_access,
2366 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2367 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2369 in->b_title_demux = false;
2370 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2371 &in->title, &in->i_title,
2372 &in->i_title_offset, &in->i_seekpoint_offset ) )
2378 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2379 &in->b_can_pace_control );
2380 in->b_can_rate_control = in->b_can_pace_control;
2381 in->b_rescale_ts = true;
2383 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2385 var_SetBool( p_input, "can-pause", in->b_can_pause );
2386 access_Control( in->p_access, ACCESS_CAN_SEEK,
2388 var_Set( p_input, "seekable", val );
2391 input_ChangeState( p_input, BUFFERING_S );
2393 /* Create the stream_t */
2394 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2395 if( in->p_stream == NULL )
2397 msg_Warn( p_input, "cannot create a stream_t from access" );
2401 /* Open a demuxer */
2402 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2404 psz_demux = in->p_access->psz_demux;
2408 /* Take access redirections into account */
2409 char *psz_real_path;
2410 char *psz_buf = NULL;
2411 if( in->p_access->psz_path )
2413 const char *psz_a, *psz_d;
2414 psz_buf = strdup( in->p_access->psz_path );
2415 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2419 psz_real_path = psz_path;
2421 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2423 in->p_stream, p_input->p->p_es_out,
2424 p_input->b_preparsing );
2428 if( in->p_demux == NULL )
2430 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2431 psz_access, psz_demux, psz_path );
2432 intf_UserFatal( VLC_OBJECT( p_input ), false,
2433 _("VLC can't recognize the input's format"),
2434 _("The format of '%s' cannot be detected. "
2435 "Have a look the log for details."), psz_mrl );
2439 /* Get title from demux */
2440 if( !p_input->b_preparsing && in->i_title <= 0 )
2442 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2443 &in->title, &in->i_title,
2444 &in->i_title_offset, &in->i_seekpoint_offset ))
2446 TAB_INIT( in->i_title, in->title );
2450 in->b_title_demux = true;
2456 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2457 if( 1 || !p_input->b_preparsing )
2460 input_attachment_t **attachment;
2461 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2462 &attachment, &i_attachment ) )
2464 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2465 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2466 i_attachment, attachment );
2467 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2470 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2472 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2474 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2477 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2478 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2483 input_ChangeState( p_input, ERROR_S );
2486 demux_Delete( in->p_demux );
2489 stream_Delete( in->p_stream );
2492 access_Delete( in->p_access );
2494 return VLC_EGENERIC;
2497 /*****************************************************************************
2499 *****************************************************************************/
2500 static void InputSourceClean( input_source_t *in )
2505 demux_Delete( in->p_demux );
2508 stream_Delete( in->p_stream );
2511 access_Delete( in->p_access );
2513 if( in->i_title > 0 )
2515 for( i = 0; i < in->i_title; i++ )
2516 vlc_input_title_Delete( in->title[i] );
2517 TAB_CLEAN( in->i_title, in->title );
2521 static void SlaveDemux( input_thread_t *p_input )
2526 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2528 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2532 for( i = 0; i < p_input->p->i_slave; i++ )
2534 input_source_t *in = p_input->p->slave[i];
2540 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2545 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2547 msg_Err( p_input, "slave[%d] doesn't like "
2548 "DEMUX_GET_TIME -> EOF", i );
2553 if( i_stime >= i_time )
2556 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2562 i_ret = in->p_demux->pf_demux( in->p_demux );
2567 msg_Dbg( p_input, "slave %d EOF", i );
2573 static void SlaveSeek( input_thread_t *p_input )
2578 if( !p_input ) return;
2580 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2582 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2586 for( i = 0; i < p_input->p->i_slave; i++ )
2588 input_source_t *in = p_input->p->slave[i];
2590 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2592 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2598 /*****************************************************************************
2600 *****************************************************************************/
2601 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2605 if( !p_meta ) return;
2607 /* Get meta information from user */
2608 #define GET_META( field, s ) \
2609 var_Get( p_input, (s), &val ); \
2610 if( *val.psz_string ) \
2611 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2612 free( val.psz_string )
2614 GET_META( Title, "meta-title" );
2615 GET_META( Artist, "meta-artist" );
2616 GET_META( Genre, "meta-genre" );
2617 GET_META( Copyright, "meta-copyright" );
2618 GET_META( Description, "meta-description" );
2619 GET_META( Date, "meta-date" );
2620 GET_META( URL, "meta-url" );
2624 /*****************************************************************************
2625 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2626 * arturl and locking issue.
2627 *****************************************************************************/
2628 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2630 input_item_t *p_item = p_input->p->input.p_item;
2631 char * psz_arturl = NULL;
2632 char *psz_title = NULL;
2633 int i_arturl_event = false;
2638 psz_arturl = input_item_GetArtURL( p_item );
2640 vlc_mutex_lock( &p_item->lock );
2641 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2642 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2644 vlc_meta_Merge( p_item->p_meta, p_meta );
2646 if( psz_arturl && *psz_arturl )
2648 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2649 i_arturl_event = true;
2652 vlc_meta_Delete( p_meta );
2654 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2656 /* Don't look for art cover if sout
2657 * XXX It can change when sout has meta data support */
2658 if( p_input->p->p_sout && !p_input->b_preparsing )
2660 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2661 i_arturl_event = true;
2665 input_ExtractAttachmentAndCacheArt( p_input );
2671 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2673 p_meta = vlc_meta_New();
2674 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2676 vlc_mutex_unlock( &p_item->lock );
2678 input_item_SetPreparsed( p_item, true );
2680 if( i_arturl_event == true )
2684 /* Notify interested third parties */
2685 event.type = vlc_InputItemMetaChanged;
2686 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2687 vlc_event_send( &p_item->event_manager, &event );
2692 input_Control( p_input, INPUT_SET_NAME, psz_title );
2696 /** \todo handle sout meta */
2700 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2701 int i_new, input_attachment_t **pp_new )
2703 int i_attachment = *pi_attachment;
2704 input_attachment_t **attachment = *ppp_attachment;
2707 attachment = realloc( attachment,
2708 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2709 for( i = 0; i < i_new; i++ )
2710 attachment[i_attachment++] = pp_new[i];
2714 *pi_attachment = i_attachment;
2715 *ppp_attachment = attachment;
2718 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2722 if( p_input->b_preparsing )
2725 if( p_input->p->input.p_access )
2726 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2729 /* Get meta data from slave input */
2730 for( i = 0; i < p_input->p->i_slave; i++ )
2732 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2733 if( p_input->p->slave[i]->p_access )
2735 access_Control( p_input->p->slave[i]->p_access,
2736 ACCESS_GET_META, p_meta );
2741 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2748 /* XXX I am not sure it is a great idea, besides, there is more than that
2749 * if we want to do it right */
2750 vlc_mutex_lock( &p_item->lock );
2751 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2753 vlc_mutex_unlock( &p_item->lock );
2756 vlc_mutex_unlock( &p_item->lock );
2759 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2760 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2765 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2766 if(! p_demux->p_private )
2769 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2772 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2774 if( p_demux_meta->p_meta )
2776 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2777 vlc_meta_Delete( p_demux_meta->p_meta );
2780 if( p_demux_meta->i_attachments > 0 )
2782 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2783 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2784 p_demux_meta->i_attachments, p_demux_meta->attachments );
2785 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2787 module_Unneed( p_demux, p_id3 );
2789 free( p_demux->p_private );
2793 /*****************************************************************************
2794 * MRLSplit: parse the access, demux and url part of the
2795 * Media Resource Locator.
2796 *****************************************************************************/
2797 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2800 char *psz_access = NULL;
2801 char *psz_demux = NULL;
2804 /* Either there is an access/demux specification before ://
2805 * or we have a plain local file path. */
2806 psz_path = strstr( psz_dup, "://" );
2807 if( psz_path != NULL )
2810 psz_path += 3; /* skips "://" */
2812 /* Separate access from demux (<access>/<demux>://<path>) */
2813 psz_access = psz_dup;
2814 psz_demux = strchr( psz_access, '/' );
2816 *psz_demux++ = '\0';
2823 *ppsz_access = psz_access ? psz_access : "";
2824 *ppsz_demux = psz_demux ? psz_demux : "";
2825 *ppsz_path = psz_path ? psz_path : "";
2828 /*****************************************************************************
2829 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2832 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2833 *****************************************************************************/
2834 static void MRLSections( input_thread_t *p_input, char *psz_source,
2835 int *pi_title_start, int *pi_title_end,
2836 int *pi_chapter_start, int *pi_chapter_end )
2838 char *psz, *psz_end, *psz_next, *psz_check;
2840 *pi_title_start = *pi_title_end = -1;
2841 *pi_chapter_start = *pi_chapter_end = -1;
2843 /* Start by parsing titles and chapters */
2844 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2846 /* Check we are really dealing with a title/chapter section */
2847 psz_check = psz + 1;
2848 if( !*psz_check ) return;
2849 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2850 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2851 if( *psz_check == ':' && ++psz_check )
2852 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2853 if( *psz_check != '-' && *psz_check ) return;
2854 if( *psz_check == '-' && ++psz_check )
2855 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2856 if( *psz_check != ':' && *psz_check ) return;
2857 if( *psz_check == ':' && ++psz_check )
2858 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2859 if( *psz_check ) return;
2861 /* Separate start and end */
2863 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2865 /* Look for the start title */
2866 *pi_title_start = strtol( psz, &psz_next, 0 );
2867 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2868 *pi_title_end = *pi_title_start;
2871 /* Look for the start chapter */
2873 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2874 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2875 *pi_chapter_end = *pi_chapter_start;
2879 /* Look for the end title */
2880 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2881 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2884 /* Look for the end chapter */
2885 if( *psz_end ) psz_end++;
2886 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2887 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2890 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2891 psz_source, *pi_title_start, *pi_chapter_start,
2892 *pi_title_end, *pi_chapter_end );
2895 /*****************************************************************************
2896 * input_AddSubtitles: add a subtitles file and enable it
2897 *****************************************************************************/
2898 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2899 bool b_check_extension )
2901 input_source_t *sub;
2904 char *psz_path, *psz_extension;
2906 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2911 /* if we are provided a subtitle.sub file,
2912 * see if we don't have a subtitle.idx and use it instead */
2913 psz_path = strdup( psz_subtitle );
2916 psz_extension = strrchr( psz_path, '.');
2917 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2921 strcpy( psz_extension, ".idx" );
2922 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2923 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2926 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2927 psz_path, psz_subtitle );
2928 strcpy( psz_subtitle, psz_path );
2934 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2936 sub = InputSourceNew( p_input );
2937 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2939 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2942 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2944 if( count.i_int == 0 )
2946 /* if it was first one, there is disable too */
2948 if( count.i_int < list.p_list->i_count )
2950 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2951 &list.p_list->p_values[count.i_int] );
2953 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );