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" );
142 MALLOC_NULL( p_input->p, input_thread_private_t );
144 /* One "randomly" selected input thread is responsible for computing
145 * the global stats. Check if there is already someone doing this */
146 if( p_input->p_libvlc->p_stats && !b_quick )
148 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
149 if( p_input->p_libvlc->p_stats_computer == NULL )
151 p_input->p_libvlc->p_stats_computer = p_input;
153 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
156 p_input->b_preparsing = b_quick;
157 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
159 /* Init Common fields */
160 p_input->b_eof = false;
161 p_input->b_can_pace_control = true;
162 p_input->p->i_start = 0;
164 p_input->p->i_stop = 0;
165 p_input->p->i_run = 0;
166 p_input->p->i_title = 0;
167 p_input->p->title = NULL;
168 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
169 p_input->i_state = INIT_S;
170 p_input->p->i_rate = INPUT_RATE_DEFAULT;
171 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
172 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
173 p_input->p->p_es_out = NULL;
174 p_input->p->p_sout = NULL;
175 p_input->p->b_owns_its_sout = true;
176 p_input->p->b_sout_keep = false;
177 p_input->p->b_out_pace_control = false;
178 p_input->i_pts_delay = 0;
180 /* Init Input fields */
181 vlc_gc_incref( p_item ); /* Released in Destructor() */
182 p_input->p->input.p_item = p_item;
183 p_input->p->input.p_access = NULL;
184 p_input->p->input.p_stream = NULL;
185 p_input->p->input.p_demux = NULL;
186 p_input->p->input.b_title_demux = false;
187 p_input->p->input.i_title = 0;
188 p_input->p->input.title = NULL;
189 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
190 p_input->p->input.b_can_pace_control = true;
191 p_input->p->input.b_can_rate_control = true;
192 p_input->p->input.b_rescale_ts = true;
193 p_input->p->input.b_eof = false;
194 p_input->p->input.i_cr_average = 0;
196 vlc_mutex_lock( &p_item->lock );
198 if( !p_item->p_stats )
199 p_item->p_stats = stats_NewInputStats( p_input );
200 vlc_mutex_unlock( &p_item->lock );
203 p_input->p->i_slave = 0;
204 p_input->p->slave = NULL;
206 /* Init control buffer */
207 vlc_mutex_init( p_input, &p_input->p->lock_control );
208 p_input->p->i_control = 0;
210 /* Parse input options */
211 vlc_mutex_lock( &p_item->lock );
212 assert( p_item->optflagc == p_item->i_options );
213 for( i = 0; i < p_item->i_options; i++ )
214 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
215 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
216 vlc_mutex_unlock( &p_item->lock );
218 /* Create Object Variables for private use only */
219 input_ConfigVarInit( p_input );
221 /* Create Objects variables for public Get and Set */
222 input_ControlVarInit( p_input );
224 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
226 if( !p_input->b_preparsing )
228 var_Get( p_input, "bookmarks", &val );
231 /* FIXME: have a common cfg parsing routine used by sout and others */
232 char *psz_parser, *psz_start, *psz_end;
233 psz_parser = val.psz_string;
234 while( (psz_start = strchr( psz_parser, '{' ) ) )
236 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
239 psz_end = strchr( psz_start, '}' );
240 if( !psz_end ) break;
241 psz_parser = psz_end + 1;
242 backup = *psz_parser;
245 while( (psz_end = strchr( psz_start, ',' ) ) )
248 if( !strncmp( psz_start, "name=", 5 ) )
250 p_seekpoint->psz_name = strdup(psz_start + 5);
252 else if( !strncmp( psz_start, "bytes=", 6 ) )
254 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
256 else if( !strncmp( psz_start, "time=", 5 ) )
258 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
261 psz_start = psz_end + 1;
263 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
264 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
265 p_seekpoint->i_time_offset );
266 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
267 vlc_seekpoint_Delete( p_seekpoint );
268 *psz_parser = backup;
270 free( val.psz_string );
274 /* Remove 'Now playing' info as it is probably outdated */
275 input_Control( p_input, INPUT_DEL_INFO,
276 _(VLC_META_INFO_CAT),
277 _(VLC_META_NOW_PLAYING) );
278 input_item_SetNowPlaying( p_item, NULL );
281 if( p_input->b_preparsing )
282 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
287 p_input->p->p_sout = p_sout;
288 p_input->p->b_owns_its_sout = false;
291 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
292 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
294 /* Attach only once we are ready */
295 vlc_object_attach( p_input, p_parent );
297 /* Set the destructor when we are sure we are initialized */
298 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
304 * Input destructor (called when the object's refcount reaches 0).
306 static void Destructor( input_thread_t * p_input )
308 input_thread_private_t *priv = p_input->p;
310 if( priv->b_owns_its_sout && priv->p_sout )
312 if( priv->b_sout_keep )
313 SoutKeep( priv->p_sout );
315 sout_DeleteInstance( priv->p_sout );
318 vlc_gc_decref( p_input->p->input.p_item );
320 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
322 vlc_mutex_destroy( &priv->lock_control );
327 * Initialize an input thread and run it. You will need to monitor the
328 * thread to clean up after it is done
330 * \param p_parent a vlc_object
331 * \param p_item an input item
332 * \return a pointer to the spawned input thread
334 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
335 input_item_t *p_item )
338 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
339 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
341 if( !p_input && p_sout )
344 p_input->p->b_sout_keep = b_sout_keep;
349 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
350 input_item_t *p_item,
351 const char *psz_log, sout_instance_t *p_sout )
353 input_thread_t *p_input;
355 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
359 /* Create thread and wait for its readiness. */
360 if( vlc_thread_create( p_input, "input", Run,
361 VLC_THREAD_PRIORITY_INPUT, true ) )
363 input_ChangeState( p_input, ERROR_S );
364 msg_Err( p_input, "cannot create input thread" );
365 vlc_object_detach( p_input );
366 vlc_object_release( p_input );
374 * Initialize an input thread and run it. This thread will clean after himself,
375 * you can forget about it. It can work either in blocking or non-blocking mode
377 * \param p_parent a vlc_object
378 * \param p_item an input item
379 * \param b_block should we block until read is finished ?
380 * \return the input object id if non blocking, an error code else
382 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
386 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
387 input_thread_t *p_input;
389 p_input = Create( p_parent, p_item, NULL, false, p_sout );
390 if( !p_input && p_sout )
395 p_input->p->b_sout_keep = b_sout_keep;
399 RunAndDestroy( p_input );
404 if( vlc_thread_create( p_input, "input", RunAndDestroy,
405 VLC_THREAD_PRIORITY_INPUT, true ) )
407 input_ChangeState( p_input, ERROR_S );
408 msg_Err( p_input, "cannot create input thread" );
409 vlc_object_release( p_input );
413 return p_input->i_object_id;
417 * Initialize an input and initialize it to preparse the item
418 * This function is blocking. It will only accept to parse files
420 * \param p_parent a vlc_object_t
421 * \param p_item an input item
422 * \return VLC_SUCCESS or an error
424 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
426 input_thread_t *p_input;
428 /* Allocate descriptor */
429 p_input = Create( p_parent, p_item, NULL, true, NULL );
433 if( !Init( p_input ) )
436 vlc_object_detach( p_input );
437 vlc_object_release( p_input );
443 * Request a running input thread to stop and die
445 * \param the input thread to stop
447 void input_StopThread( input_thread_t *p_input )
452 /* Set die for input */
453 vlc_object_kill( p_input );
454 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
456 /* We cannot touch p_input fields directly (we come from another thread),
457 * so use the vlc_object_find way, it's perfectly safe */
459 /* Set die for all access */
460 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
461 for( i = 0; i < p_list->i_count; i++ )
463 vlc_object_kill( p_list->p_values[i].p_object );
465 vlc_list_release( p_list );
467 /* Set die for all stream */
468 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
469 for( i = 0; i < p_list->i_count; i++ )
471 vlc_object_kill( p_list->p_values[i].p_object );
473 vlc_list_release( p_list );
475 /* Set die for all demux */
476 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, 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 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
486 sout_instance_t * input_DetachSout( input_thread_t *p_input )
488 p_input->p->b_owns_its_sout = false;
489 return p_input->p->p_sout;
492 /*****************************************************************************
493 * Run: main thread loop
494 * This is the "normal" thread that spawns the input processing chain,
495 * reads the stream, cleans up and waits
496 *****************************************************************************/
497 static int Run( input_thread_t *p_input )
499 /* Signal that the thread is launched */
500 vlc_thread_ready( p_input );
502 if( Init( p_input ) )
504 /* If we failed, wait before we are killed, and exit */
505 p_input->b_error = true;
507 /* FIXME: we don't want to depend on the playlist */
508 playlist_t * p_playlist = vlc_object_find( p_input,
509 VLC_OBJECT_PLAYLIST, FIND_PARENT );
512 playlist_Signal( p_playlist );
513 vlc_object_release( p_playlist );
518 /* Tell we're dead */
519 p_input->b_dead = true;
526 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
528 /* We have finish to demux data but not to play them */
529 while( !p_input->b_die )
531 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
534 msg_Dbg( p_input, "waiting decoder fifos to empty" );
536 msleep( INPUT_IDLE_SLEEP );
539 /* We have finished */
540 p_input->b_eof = true;
541 playlist_Signal( pl_Get( p_input ) );
544 /* Wait until we are asked to die */
545 if( !p_input->b_die )
556 /*****************************************************************************
557 * RunAndDestroy: main thread loop
558 * This is the "just forget me" thread that spawns the input processing chain,
559 * reads the stream, cleans up and releases memory
560 *****************************************************************************/
561 static int RunAndDestroy( input_thread_t *p_input )
563 /* Signal that the thread is launched */
564 vlc_thread_ready( p_input );
566 if( Init( p_input ) )
571 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
573 /* We have finished demuxing data but not playing it */
574 while( !p_input->b_die )
576 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
579 msg_Dbg( p_input, "waiting decoder fifos to empty" );
581 msleep( INPUT_IDLE_SLEEP );
584 /* We have finished */
585 p_input->b_eof = true;
593 vlc_object_release( p_input );
597 /*****************************************************************************
598 * Main loop: Fill buffers from access, and demux
599 *****************************************************************************/
600 static void MainLoop( input_thread_t *p_input )
602 int64_t i_start_mdate = mdate();
603 int64_t i_intf_update = 0;
606 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
608 bool b_force_update = false;
614 if( p_input->i_state != PAUSE_S )
616 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
617 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
620 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
625 if( p_input->p->input.b_title_demux &&
626 p_input->p->input.p_demux->info.i_update )
628 i_ret = UpdateFromDemux( p_input );
629 b_force_update = true;
631 else if( !p_input->p->input.b_title_demux &&
632 p_input->p->input.p_access &&
633 p_input->p->input.p_access->info.i_update )
635 i_ret = UpdateFromAccess( p_input );
636 b_force_update = true;
640 if( i_ret == 0 ) /* EOF */
644 var_Get( p_input, "input-repeat", &repeat );
645 if( repeat.i_int == 0 )
647 /* End of file - we do not set b_die because only the
648 * playlist is allowed to do so. */
649 input_ChangeState( p_input, END_S );
650 msg_Dbg( p_input, "EOF reached" );
651 p_input->p->input.b_eof = true;
655 msg_Dbg( p_input, "repeating the same input (%d)",
657 if( repeat.i_int > 0 )
660 var_Set( p_input, "input-repeat", repeat );
663 /* Seek to start title/seekpoint */
664 val.i_int = p_input->p->input.i_title_start -
665 p_input->p->input.i_title_offset;
666 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
668 input_ControlPush( p_input,
669 INPUT_CONTROL_SET_TITLE, &val );
671 val.i_int = p_input->p->input.i_seekpoint_start -
672 p_input->p->input.i_seekpoint_offset;
673 if( val.i_int > 0 /* TODO: check upper boundary */ )
674 input_ControlPush( p_input,
675 INPUT_CONTROL_SET_SEEKPOINT, &val );
677 /* Seek to start position */
678 if( p_input->p->i_start > 0 )
680 val.i_time = p_input->p->i_start;
681 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
687 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
692 i_start_mdate = mdate();
697 p_input->b_error = true;
700 if( i_ret > 0 && p_input->p->i_slave > 0 )
702 SlaveDemux( p_input );
712 vlc_mutex_lock( &p_input->p->lock_control );
713 ControlReduce( p_input );
714 while( !ControlPopNoLock( p_input, &i_type, &val ) )
716 msg_Dbg( p_input, "control type=%d", i_type );
717 if( Control( p_input, i_type, val ) )
718 b_force_update = true;
720 vlc_mutex_unlock( &p_input->p->lock_control );
722 if( b_force_update || i_intf_update < mdate() )
726 int64_t i_time, i_length;
727 /* update input status variables */
728 if( !demux_Control( p_input->p->input.p_demux,
729 DEMUX_GET_POSITION, &f_pos ) )
731 val.f_float = (float)f_pos;
732 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
734 if( !demux_Control( p_input->p->input.p_demux,
735 DEMUX_GET_TIME, &i_time ) )
737 p_input->i_time = i_time;
739 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
741 if( !demux_Control( p_input->p->input.p_demux,
742 DEMUX_GET_LENGTH, &i_length ) )
745 var_Get( p_input, "length", &old_val );
746 val.i_time = i_length;
747 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
749 if( old_val.i_time != val.i_time )
751 UpdateItemLength( p_input, i_length );
755 var_SetBool( p_input, "intf-change", true );
756 i_intf_update = mdate() + I64C(150000);
758 /* 150ms * 8 = ~ 1 second */
759 if( ++i_updates % 8 == 0 )
761 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
762 /* Are we the thread responsible for computing global stats ? */
763 if( p_input->p_libvlc->p_stats_computer == p_input )
765 stats_ComputeGlobalStats( p_input->p_libvlc,
766 p_input->p_libvlc->p_stats );
772 static void InitStatistics( input_thread_t * p_input )
774 if( p_input->b_preparsing ) return;
776 /* Prepare statistics */
777 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
778 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
779 if( p_input->p_libvlc->b_stats )
781 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
782 INIT_COUNTER( read_packets, INTEGER, COUNTER );
783 INIT_COUNTER( demux_read, INTEGER, COUNTER );
784 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
785 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
786 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
787 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
788 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
789 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
790 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
791 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
792 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
793 p_input->p->counters.p_sout_send_bitrate = NULL;
794 p_input->p->counters.p_sout_sent_packets = NULL;
795 p_input->p->counters.p_sout_sent_bytes = NULL;
796 if( p_input->p->counters.p_demux_bitrate )
797 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
798 if( p_input->p->counters.p_input_bitrate )
799 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
803 static int InitSout( input_thread_t * p_input )
807 if( p_input->b_preparsing ) return VLC_SUCCESS;
809 /* Find a usable sout and attach it to p_input */
810 psz = var_GetNonEmptyString( p_input, "sout" );
811 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
813 /* Check the validity of the provided sout */
814 if( p_input->p->p_sout )
816 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
818 msg_Dbg( p_input, "destroying unusable sout" );
820 sout_DeleteInstance( p_input->p->p_sout );
821 p_input->p->p_sout = NULL;
825 if( p_input->p->p_sout )
828 msg_Dbg( p_input, "sout keep: reusing sout" );
829 msg_Dbg( p_input, "sout keep: you probably want to use "
830 "gather stream_out" );
831 vlc_object_attach( p_input->p->p_sout, p_input );
835 /* Create a new one */
836 p_input->p->p_sout = sout_NewInstance( p_input, 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, " \
846 if( p_input->p_libvlc->b_stats )
848 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
849 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
850 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
851 if( p_input->p->counters.p_sout_send_bitrate )
852 p_input->p->counters.p_sout_send_bitrate->update_interval =
856 else if( p_input->p->p_sout )
858 msg_Dbg( p_input, "destroying useless sout" );
860 sout_DeleteInstance( p_input->p->p_sout );
861 p_input->p->p_sout = NULL;
868 static void InitTitle( input_thread_t * p_input )
872 if( p_input->b_preparsing ) return;
874 /* Create global title (from master) */
875 p_input->p->i_title = p_input->p->input.i_title;
876 p_input->p->title = p_input->p->input.title;
877 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
878 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
879 if( p_input->p->i_title > 0 )
881 /* Setup variables */
882 input_ControlVarNavigation( p_input );
883 input_ControlVarTitle( p_input, 0 );
887 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
888 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
889 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
892 if( p_input->i_pts_delay < 0 )
893 p_input->i_pts_delay = 0;
895 /* If the desynchronisation requested by the user is < 0, we need to
896 * cache more data. */
897 var_Get( p_input, "audio-desync", &val );
898 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
900 /* Update cr_average depending on the caching */
901 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
902 p_input->p->input.i_cr_average /= 10;
903 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
906 static void StartTitle( input_thread_t * p_input )
914 /* Start title/chapter */
916 if( p_input->b_preparsing )
918 p_input->p->i_start = 0;
922 val.i_int = p_input->p->input.i_title_start -
923 p_input->p->input.i_title_offset;
924 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
925 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
926 val.i_int = p_input->p->input.i_seekpoint_start -
927 p_input->p->input.i_seekpoint_offset;
928 if( val.i_int > 0 /* TODO: check upper boundary */ )
929 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
933 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
934 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
935 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
936 if( p_input->p->i_run < 0 )
938 msg_Warn( p_input, "invalid run-time ignored" );
939 p_input->p->i_run = 0;
942 if( p_input->p->i_start > 0 )
944 if( p_input->p->i_start >= val.i_time )
946 msg_Warn( p_input, "invalid start-time ignored" );
952 msg_Dbg( p_input, "starting at time: %ds",
953 (int)( p_input->p->i_start / I64C(1000000) ) );
955 s.i_time = p_input->p->i_start;
956 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
959 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
961 msg_Warn( p_input, "invalid stop-time ignored" );
962 p_input->p->i_stop = 0;
966 /* Get fps and set it if not already set */
967 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
970 float f_requested_fps;
972 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
973 var_SetFloat( p_input, "sub-original-fps", f_fps );
975 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
976 if( f_requested_fps != f_fps )
978 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
980 var_SetFloat( p_input, "sub-fps", f_requested_fps );
984 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
987 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
990 /* Look for and add subtitle files */
991 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
992 if( psz_subtitle != NULL )
994 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
995 input_AddSubtitles( p_input, psz_subtitle, false );
998 var_Get( p_input, "sub-autodetect-file", &val );
1001 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1002 char **subs = subtitles_Detect( p_input, psz_autopath,
1003 p_input->p->input.p_item->psz_uri );
1004 input_source_t *sub;
1006 if( psz_autopath == NULL )
1007 psz_autopath = strdup("");
1009 /* Try to autoselect the first autodetected subtitles file
1010 * if no subtitles file was specified */
1011 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1013 input_AddSubtitles( p_input, subs[0], false );
1018 /* Then, just add the following subtitles files */
1019 for( ; subs && subs[i]; i++ )
1021 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1023 sub = InputSourceNew( p_input );
1024 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1026 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1033 free( psz_autopath );
1035 free( psz_subtitle );
1037 /* Look for slave */
1038 psz = var_GetNonEmptyString( p_input, "input-slave" );
1042 input_source_t *slave;
1043 while( psz && *psz )
1045 while( *psz == ' ' || *psz == '#' )
1049 if( ( psz_delim = strchr( psz, '#' ) ) )
1051 *psz_delim++ = '\0';
1058 msg_Dbg( p_input, "adding slave input '%s'", psz );
1059 slave = InputSourceNew( p_input );
1060 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1062 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1071 static void InitPrograms( input_thread_t * p_input )
1076 if( p_input->b_preparsing ) return;
1079 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1080 i_es_out_mode = ES_OUT_MODE_AUTO;
1082 if( p_input->p->p_sout )
1084 var_Get( p_input, "sout-all", &val );
1087 i_es_out_mode = ES_OUT_MODE_ALL;
1092 var_Get( p_input, "programs", &val );
1093 if ( val.p_list && val.p_list->i_count )
1095 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1096 /* Note : we should remove the "program" callback. */
1099 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1103 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1105 /* Inform the demuxer about waited group (needed only for DVB) */
1106 if( i_es_out_mode == ES_OUT_MODE_ALL )
1108 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1110 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1112 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1117 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1118 (int) var_GetInteger( p_input, "program" ), NULL );
1122 static int Init( input_thread_t * p_input )
1128 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1130 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1132 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1133 var_SetString( p_input, "sout", "" );
1134 var_SetBool( p_input, "sout-all", false );
1135 var_SetString( p_input, "input-slave", "" );
1136 var_SetInteger( p_input, "input-repeat", 0 );
1137 var_SetString( p_input, "sub-file", "" );
1138 var_SetBool( p_input, "sub-autodetect-file", false );
1142 InitStatistics( p_input );
1143 ret = InitSout( p_input );
1145 if( ret != VLC_SUCCESS )
1146 return ret; /* FIXME: goto error; should be better here */
1149 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1150 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1151 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1153 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1154 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1156 if( InputSourceInit( p_input, &p_input->p->input,
1157 p_input->p->input.p_item->psz_uri, NULL ) )
1162 InitTitle( p_input );
1164 /* Load master infos */
1166 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1167 &val.i_time ) && val.i_time > 0 )
1169 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1170 UpdateItemLength( p_input, val.i_time );
1174 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1175 if( val.i_time > 0 )
1176 { /* fallback: gets length from metadata */
1177 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1178 UpdateItemLength( p_input, val.i_time );
1182 StartTitle( p_input );
1184 InitPrograms( p_input );
1186 if( !p_input->b_preparsing && p_input->p->p_sout )
1188 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1190 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1192 /* We don't want a high input priority here or we'll
1193 * end-up sucking up all the CPU time */
1194 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1197 msg_Dbg( p_input, "starting in %s mode",
1198 p_input->p->b_out_pace_control ? "async" : "sync" );
1201 p_meta = vlc_meta_New();
1203 /* Get meta data from users */
1204 InputMetaUser( p_input, p_meta );
1206 /* Get meta data from master input */
1207 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1209 /* Access_file does not give any meta, and there are no slave */
1210 AccessMeta( p_input, p_meta );
1212 if( !p_input->b_preparsing )
1214 msg_Dbg( p_input, "`%s' successfully opened",
1215 p_input->p->input.p_item->psz_uri );
1219 /* initialization is complete */
1220 input_ChangeState( p_input, PLAYING_S );
1225 input_ChangeState( p_input, ERROR_S );
1227 if( p_input->p->p_es_out )
1228 input_EsOutDelete( p_input->p->p_es_out );
1230 if( p_input->p->p_sout )
1232 vlc_object_detach( p_input->p->p_sout );
1233 sout_DeleteInstance( p_input->p->p_sout );
1237 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1239 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1240 stats_CounterClean( p_input->p->counters.p_##c );\
1241 p_input->p->counters.p_##c = NULL; } while(0)
1242 EXIT_COUNTER( read_bytes );
1243 EXIT_COUNTER( read_packets );
1244 EXIT_COUNTER( demux_read );
1245 EXIT_COUNTER( input_bitrate );
1246 EXIT_COUNTER( demux_bitrate );
1247 EXIT_COUNTER( played_abuffers );
1248 EXIT_COUNTER( lost_abuffers );
1249 EXIT_COUNTER( displayed_pictures );
1250 EXIT_COUNTER( lost_pictures );
1251 EXIT_COUNTER( decoded_audio );
1252 EXIT_COUNTER( decoded_video );
1253 EXIT_COUNTER( decoded_sub );
1255 if( p_input->p->p_sout )
1257 EXIT_COUNTER( sout_sent_packets );
1258 EXIT_COUNTER (sout_sent_bytes );
1259 EXIT_COUNTER( sout_send_bitrate );
1264 /* Mark them deleted */
1265 p_input->p->input.p_demux = NULL;
1266 p_input->p->input.p_stream = NULL;
1267 p_input->p->input.p_access = NULL;
1268 p_input->p->p_es_out = NULL;
1269 p_input->p->p_sout = NULL;
1271 return VLC_EGENERIC;
1274 /*****************************************************************************
1275 * Error: RunThread() error loop
1276 *****************************************************************************
1277 * This function is called when an error occurred during thread main's loop.
1278 *****************************************************************************/
1279 static void Error( input_thread_t *p_input )
1281 input_ChangeState( p_input, ERROR_S );
1282 while( !p_input->b_die )
1285 msleep( INPUT_IDLE_SLEEP );
1289 /*****************************************************************************
1290 * End: end the input thread
1291 *****************************************************************************/
1292 static void End( input_thread_t * p_input )
1296 /* We are at the end */
1297 input_ChangeState( p_input, END_S );
1299 /* Clean control variables */
1300 input_ControlVarClean( p_input );
1302 /* Clean up master */
1303 InputSourceClean( &p_input->p->input );
1306 for( i = 0; i < p_input->p->i_slave; i++ )
1308 InputSourceClean( p_input->p->slave[i] );
1309 free( p_input->p->slave[i] );
1311 free( p_input->p->slave );
1313 /* Unload all modules */
1314 if( p_input->p->p_es_out )
1315 input_EsOutDelete( p_input->p->p_es_out );
1317 if( !p_input->b_preparsing )
1319 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1320 if( p_input->p_libvlc->b_stats )
1322 /* make sure we are up to date */
1323 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1324 if( p_input->p_libvlc->p_stats_computer == p_input )
1326 stats_ComputeGlobalStats( p_input->p_libvlc,
1327 p_input->p_libvlc->p_stats );
1328 p_input->p_libvlc->p_stats_computer = NULL;
1330 CL_CO( read_bytes );
1331 CL_CO( read_packets );
1332 CL_CO( demux_read );
1333 CL_CO( input_bitrate );
1334 CL_CO( demux_bitrate );
1335 CL_CO( played_abuffers );
1336 CL_CO( lost_abuffers );
1337 CL_CO( displayed_pictures );
1338 CL_CO( lost_pictures );
1339 CL_CO( decoded_audio) ;
1340 CL_CO( decoded_video );
1341 CL_CO( decoded_sub) ;
1344 /* Close optional stream output instance */
1345 if( p_input->p->p_sout )
1347 CL_CO( sout_sent_packets );
1348 CL_CO( sout_sent_bytes );
1349 CL_CO( sout_send_bitrate );
1351 vlc_object_detach( p_input->p->p_sout );
1356 if( p_input->p->i_attachment > 0 )
1358 for( i = 0; i < p_input->p->i_attachment; i++ )
1359 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1360 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1363 /* Tell we're dead */
1364 p_input->b_dead = true;
1367 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, bool *pb_sout_keep )
1369 bool b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1370 sout_instance_t *p_sout = NULL;
1373 /* Search sout-keep options
1374 * XXX it has to be done here, but it is duplicated work :( */
1375 vlc_mutex_lock( &p_item->lock );
1376 for( i = 0; i < p_item->i_options; i++ )
1378 const char *psz_option = p_item->ppsz_options[i];
1381 if( *psz_option == ':' )
1384 if( !strcmp( psz_option, "sout-keep" ) )
1386 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1387 b_keep_sout = false;
1389 vlc_mutex_unlock( &p_item->lock );
1391 /* Find a potential sout to reuse
1392 * XXX it might be unusable but this will be checked later */
1395 /* Remove the sout from the playlist garbage collector */
1396 /* FIXME: we don't want to depend on the playlist, sout
1397 * should be attached to libvlc */
1398 playlist_t * p_playlist = vlc_object_find( p_parent,
1399 VLC_OBJECT_PLAYLIST, FIND_PARENT );
1402 vlc_mutex_lock( &p_playlist->gc_lock );
1403 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1406 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1408 vlc_object_release( p_sout );
1413 vlc_object_detach( p_sout ); /* Remove it from the GC */
1415 vlc_object_release( p_sout );
1418 vlc_mutex_unlock( &p_playlist->gc_lock );
1420 vlc_object_release( p_playlist );
1425 *pb_sout_keep = b_keep_sout;
1430 static void SoutKeep( sout_instance_t *p_sout )
1432 /* FIXME: we don't want to depend on the playlist, sout
1433 * should be attached to libvlc */
1434 playlist_t * p_playlist = vlc_object_find( p_sout, VLC_OBJECT_PLAYLIST,
1436 if( !p_playlist ) return;
1438 msg_Dbg( p_sout, "sout has been kept" );
1439 vlc_object_attach( p_sout, p_playlist );
1441 pl_Release( p_sout );
1444 /*****************************************************************************
1446 *****************************************************************************/
1447 static inline int ControlPopNoLock( input_thread_t *p_input,
1448 int *pi_type, vlc_value_t *p_val )
1450 if( p_input->p->i_control <= 0 )
1452 return VLC_EGENERIC;
1455 *pi_type = p_input->p->control[0].i_type;
1456 *p_val = p_input->p->control[0].val;
1458 p_input->p->i_control--;
1459 if( p_input->p->i_control > 0 )
1463 for( i = 0; i < p_input->p->i_control; i++ )
1465 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1466 p_input->p->control[i].val = p_input->p->control[i+1].val;
1473 static void ControlReduce( input_thread_t *p_input )
1480 for( i = 1; i < p_input->p->i_control; i++ )
1482 const int i_lt = p_input->p->control[i-1].i_type;
1483 const int i_ct = p_input->p->control[i].i_type;
1485 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1486 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1490 ( i_ct == INPUT_CONTROL_SET_STATE ||
1491 i_ct == INPUT_CONTROL_SET_RATE ||
1492 i_ct == INPUT_CONTROL_SET_POSITION ||
1493 i_ct == INPUT_CONTROL_SET_TIME ||
1494 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1495 i_ct == INPUT_CONTROL_SET_TITLE ||
1496 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1497 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1500 // msg_Dbg( p_input, "merged at %d", i );
1501 /* Remove the i-1 */
1502 for( j = i; j < p_input->p->i_control; j++ )
1503 p_input->p->control[j-1] = p_input->p->control[j];
1504 p_input->p->i_control--;
1508 /* TODO but that's not that important
1509 - merge SET_X with SET_X_CMD
1510 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1511 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1518 static bool Control( input_thread_t *p_input, int i_type,
1521 bool b_force_update = false;
1523 if( !p_input ) return b_force_update;
1527 case INPUT_CONTROL_SET_DIE:
1528 msg_Dbg( p_input, "control: stopping input" );
1529 /* Mark all submodules to die */
1530 if( p_input->p->input.p_access )
1531 vlc_object_kill( p_input->p->input.p_access );
1532 if( p_input->p->input.p_stream )
1533 vlc_object_kill( p_input->p->input.p_stream );
1534 vlc_object_kill( p_input->p->input.p_demux );
1536 vlc_object_kill( p_input );
1539 case INPUT_CONTROL_SET_POSITION:
1540 case INPUT_CONTROL_SET_POSITION_OFFSET:
1543 if( i_type == INPUT_CONTROL_SET_POSITION )
1545 f_pos = val.f_float;
1549 /* Should not fail */
1550 demux_Control( p_input->p->input.p_demux,
1551 DEMUX_GET_POSITION, &f_pos );
1552 f_pos += val.f_float;
1554 if( f_pos < 0.0 ) f_pos = 0.0;
1555 if( f_pos > 1.0 ) f_pos = 1.0;
1556 /* Reset the decoders states and clock sync (before calling the demuxer */
1557 input_EsOutChangePosition( p_input->p->p_es_out );
1558 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1561 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1562 "%2.1f%% failed", f_pos * 100 );
1566 if( p_input->p->i_slave > 0 )
1567 SlaveSeek( p_input );
1569 b_force_update = true;
1574 case INPUT_CONTROL_SET_TIME:
1575 case INPUT_CONTROL_SET_TIME_OFFSET:
1580 if( i_type == INPUT_CONTROL_SET_TIME )
1582 i_time = val.i_time;
1586 /* Should not fail */
1587 demux_Control( p_input->p->input.p_demux,
1588 DEMUX_GET_TIME, &i_time );
1589 i_time += val.i_time;
1591 if( i_time < 0 ) i_time = 0;
1593 /* Reset the decoders states and clock sync (before calling the demuxer */
1594 input_EsOutChangePosition( p_input->p->p_es_out );
1596 i_ret = demux_Control( p_input->p->input.p_demux,
1597 DEMUX_SET_TIME, i_time );
1602 /* Emulate it with a SET_POS */
1603 demux_Control( p_input->p->input.p_demux,
1604 DEMUX_GET_LENGTH, &i_length );
1607 double f_pos = (double)i_time / (double)i_length;
1608 i_ret = demux_Control( p_input->p->input.p_demux,
1609 DEMUX_SET_POSITION, f_pos );
1614 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1615 " failed or not possible", i_time );
1619 if( p_input->p->i_slave > 0 )
1620 SlaveSeek( p_input );
1622 b_force_update = true;
1627 case INPUT_CONTROL_SET_STATE:
1628 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1629 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1632 if( p_input->p->input.p_access )
1633 i_ret = access_Control( p_input->p->input.p_access,
1634 ACCESS_SET_PAUSE_STATE, false );
1636 i_ret = demux_Control( p_input->p->input.p_demux,
1637 DEMUX_SET_PAUSE_STATE, false );
1641 /* FIXME What to do ? */
1642 msg_Warn( p_input, "cannot unset pause -> EOF" );
1643 vlc_mutex_unlock( &p_input->p->lock_control );
1644 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1645 vlc_mutex_lock( &p_input->p->lock_control );
1648 b_force_update = true;
1650 /* Switch to play */
1651 p_input->i_state = PLAYING_S;
1652 val.i_int = PLAYING_S;
1653 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1657 input_EsOutChangeState( p_input->p->p_es_out );
1659 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1660 p_input->p->b_can_pause )
1663 if( p_input->p->input.p_access )
1664 i_ret = access_Control( p_input->p->input.p_access,
1665 ACCESS_SET_PAUSE_STATE, true );
1667 i_ret = demux_Control( p_input->p->input.p_demux,
1668 DEMUX_SET_PAUSE_STATE, true );
1670 b_force_update = true;
1674 msg_Warn( p_input, "cannot set pause state" );
1675 val.i_int = p_input->i_state;
1679 val.i_int = PAUSE_S;
1682 /* Switch to new state */
1683 p_input->i_state = val.i_int;
1684 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1688 input_EsOutChangeState( p_input->p->p_es_out );
1690 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1692 b_force_update = true;
1694 /* Correct "state" value */
1695 val.i_int = p_input->i_state;
1696 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1698 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1700 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1704 case INPUT_CONTROL_SET_RATE:
1705 case INPUT_CONTROL_SET_RATE_SLOWER:
1706 case INPUT_CONTROL_SET_RATE_FASTER:
1710 if( i_type == INPUT_CONTROL_SET_RATE )
1716 static const int ppi_factor[][2] = {
1717 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1719 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1728 for( i = 0; ppi_factor[i][0] != 0; i++ )
1730 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1731 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1732 if( i_test_e < i_error )
1738 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1740 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1742 if( ppi_factor[i_idx+1][0] > 0 )
1743 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1745 i_rate = INPUT_RATE_MAX+1;
1749 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1751 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1753 i_rate = INPUT_RATE_MIN-1;
1757 if( i_rate < INPUT_RATE_MIN )
1759 msg_Dbg( p_input, "cannot set rate faster" );
1760 i_rate = INPUT_RATE_MIN;
1762 else if( i_rate > INPUT_RATE_MAX )
1764 msg_Dbg( p_input, "cannot set rate slower" );
1765 i_rate = INPUT_RATE_MAX;
1767 if( i_rate != INPUT_RATE_DEFAULT &&
1768 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1769 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1771 msg_Dbg( p_input, "cannot change rate" );
1772 i_rate = INPUT_RATE_DEFAULT;
1774 if( i_rate != p_input->p->i_rate &&
1775 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1778 if( p_input->p->input.p_access )
1779 i_ret = VLC_EGENERIC;
1781 i_ret = demux_Control( p_input->p->input.p_demux,
1782 DEMUX_SET_RATE, &i_rate );
1785 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1786 i_rate = p_input->p->i_rate;
1791 if( i_rate != p_input->p->i_rate )
1794 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1795 var_SetBool( p_input, "rate-change", true );
1797 p_input->p->i_rate = i_rate;
1799 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1800 if( p_input->p->input.b_rescale_ts )
1801 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1803 b_force_update = true;
1808 case INPUT_CONTROL_SET_PROGRAM:
1809 /* No need to force update, es_out does it if needed */
1810 es_out_Control( p_input->p->p_es_out,
1811 ES_OUT_SET_GROUP, val.i_int );
1813 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1817 case INPUT_CONTROL_SET_ES:
1818 /* No need to force update, es_out does it if needed */
1819 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1820 input_EsOutGetFromID( p_input->p->p_es_out,
1824 case INPUT_CONTROL_SET_AUDIO_DELAY:
1825 input_EsOutSetDelay( p_input->p->p_es_out,
1826 AUDIO_ES, val.i_time );
1827 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1830 case INPUT_CONTROL_SET_SPU_DELAY:
1831 input_EsOutSetDelay( p_input->p->p_es_out,
1832 SPU_ES, val.i_time );
1833 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1836 case INPUT_CONTROL_SET_TITLE:
1837 case INPUT_CONTROL_SET_TITLE_NEXT:
1838 case INPUT_CONTROL_SET_TITLE_PREV:
1839 if( p_input->p->input.b_title_demux &&
1840 p_input->p->input.i_title > 0 )
1843 /* FIXME handle demux title */
1844 demux_t *p_demux = p_input->p->input.p_demux;
1847 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1848 i_title = p_demux->info.i_title - 1;
1849 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1850 i_title = p_demux->info.i_title + 1;
1852 i_title = val.i_int;
1854 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1856 input_EsOutChangePosition( p_input->p->p_es_out );
1858 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1859 input_ControlVarTitle( p_input, i_title );
1862 else if( p_input->p->input.i_title > 0 )
1864 access_t *p_access = p_input->p->input.p_access;
1867 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1868 i_title = p_access->info.i_title - 1;
1869 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1870 i_title = p_access->info.i_title + 1;
1872 i_title = val.i_int;
1874 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1876 input_EsOutChangePosition( p_input->p->p_es_out );
1878 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1879 stream_AccessReset( p_input->p->input.p_stream );
1883 case INPUT_CONTROL_SET_SEEKPOINT:
1884 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1885 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1886 if( p_input->p->input.b_title_demux &&
1887 p_input->p->input.i_title > 0 )
1889 demux_t *p_demux = p_input->p->input.p_demux;
1891 int64_t i_input_time;
1892 int64_t i_seekpoint_time;
1894 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1896 i_seekpoint = p_demux->info.i_seekpoint;
1897 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1898 if( i_seekpoint_time >= 0 &&
1899 !demux_Control( p_demux,
1900 DEMUX_GET_TIME, &i_input_time ) )
1902 if ( i_input_time < i_seekpoint_time + 3000000 )
1908 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1909 i_seekpoint = p_demux->info.i_seekpoint + 1;
1911 i_seekpoint = val.i_int;
1913 if( i_seekpoint >= 0 && i_seekpoint <
1914 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1917 input_EsOutChangePosition( p_input->p->p_es_out );
1919 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1922 else if( p_input->p->input.i_title > 0 )
1924 demux_t *p_demux = p_input->p->input.p_demux;
1925 access_t *p_access = p_input->p->input.p_access;
1927 int64_t i_input_time;
1928 int64_t i_seekpoint_time;
1930 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1932 i_seekpoint = p_access->info.i_seekpoint;
1933 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1934 if( i_seekpoint_time >= 0 &&
1935 demux_Control( p_demux,
1936 DEMUX_GET_TIME, &i_input_time ) )
1938 if ( i_input_time < i_seekpoint_time + 3000000 )
1944 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1945 i_seekpoint = p_access->info.i_seekpoint + 1;
1947 i_seekpoint = val.i_int;
1949 if( i_seekpoint >= 0 && i_seekpoint <
1950 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1952 input_EsOutChangePosition( p_input->p->p_es_out );
1954 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1956 stream_AccessReset( p_input->p->input.p_stream );
1961 case INPUT_CONTROL_ADD_SLAVE:
1962 if( val.psz_string )
1964 input_source_t *slave = InputSourceNew( p_input );
1966 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1972 msg_Dbg( p_input, "adding %s as slave on the fly",
1976 if( demux_Control( p_input->p->input.p_demux,
1977 DEMUX_GET_TIME, &i_time ) )
1979 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1980 InputSourceClean( slave );
1984 if( demux_Control( slave->p_demux,
1985 DEMUX_SET_TIME, i_time ) )
1987 msg_Err( p_input, "seek failed for new slave" );
1988 InputSourceClean( slave );
1993 /* Get meta (access and demux) */
1994 p_meta = vlc_meta_New();
1995 access_Control( slave->p_access, ACCESS_GET_META,
1997 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1998 InputUpdateMeta( p_input, p_meta );
2000 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2005 msg_Warn( p_input, "failed to add %s as slave",
2009 free( val.psz_string );
2013 case INPUT_CONTROL_SET_BOOKMARK:
2015 msg_Err( p_input, "not yet implemented" );
2019 return b_force_update;
2022 /*****************************************************************************
2024 *****************************************************************************/
2025 static int UpdateFromDemux( input_thread_t *p_input )
2027 demux_t *p_demux = p_input->p->input.p_demux;
2030 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2032 v.i_int = p_demux->info.i_title;
2033 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2035 input_ControlVarTitle( p_input, p_demux->info.i_title );
2037 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2039 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2041 v.i_int = p_demux->info.i_seekpoint;
2042 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2044 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2046 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2048 /* Hmmm only works with master input */
2049 if( p_input->p->input.p_demux == p_demux )
2051 int i_title_end = p_input->p->input.i_title_end -
2052 p_input->p->input.i_title_offset;
2053 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2054 p_input->p->input.i_seekpoint_offset;
2056 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2058 if( p_demux->info.i_title > i_title_end ||
2059 ( p_demux->info.i_title == i_title_end &&
2060 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2062 else if( i_seekpoint_end >=0 )
2064 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2066 else if( i_title_end >= 0 )
2068 if( p_demux->info.i_title > i_title_end ) return 0;
2075 /*****************************************************************************
2077 *****************************************************************************/
2078 static int UpdateFromAccess( input_thread_t *p_input )
2080 access_t *p_access = p_input->p->input.p_access;
2083 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2085 v.i_int = p_access->info.i_title;
2086 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2088 input_ControlVarTitle( p_input, p_access->info.i_title );
2090 stream_AccessUpdate( p_input->p->input.p_stream );
2092 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2094 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2096 v.i_int = p_access->info.i_seekpoint;
2097 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2098 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2100 if( p_access->info.i_update & INPUT_UPDATE_META )
2102 /* TODO maybe multi - access ? */
2103 vlc_meta_t *p_meta = vlc_meta_New();
2104 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2105 InputUpdateMeta( p_input, p_meta );
2106 var_SetInteger( pl_Get( p_input ), "item-change", p_input->p->input.p_item->i_id );
2107 p_access->info.i_update &= ~INPUT_UPDATE_META;
2110 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2112 /* Hmmm only works with master input */
2113 if( p_input->p->input.p_access == p_access )
2115 int i_title_end = p_input->p->input.i_title_end -
2116 p_input->p->input.i_title_offset;
2117 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2118 p_input->p->input.i_seekpoint_offset;
2120 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2122 if( p_access->info.i_title > i_title_end ||
2123 ( p_access->info.i_title == i_title_end &&
2124 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2126 else if( i_seekpoint_end >=0 )
2128 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2130 else if( i_title_end >= 0 )
2132 if( p_access->info.i_title > i_title_end ) return 0;
2139 /*****************************************************************************
2141 *****************************************************************************/
2142 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2144 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2146 if( !p_input->b_preparsing )
2148 pl_Yield( p_input );
2149 var_SetInteger( pl_Get( p_input ), "item-change",
2150 p_input->p->input.p_item->i_id );
2151 pl_Release( p_input );
2155 /*****************************************************************************
2157 *****************************************************************************/
2158 static input_source_t *InputSourceNew( input_thread_t *p_input )
2160 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2164 msg_Err( p_input, "out of memory for new input source" );
2169 in->p_access = NULL;
2170 in->p_stream = NULL;
2172 in->b_title_demux = false;
2173 TAB_INIT( in->i_title, in->title );
2174 in->b_can_pause = true;
2175 in->b_can_pace_control = true;
2176 in->b_can_rate_control = true;
2177 in->b_rescale_ts = true;
2180 in->i_cr_average = 0;
2185 /*****************************************************************************
2187 *****************************************************************************/
2188 static int InputSourceInit( input_thread_t *p_input,
2189 input_source_t *in, const char *psz_mrl,
2190 const char *psz_forced_demux )
2192 char psz_dup[strlen (psz_mrl) + 1];
2193 const char *psz_access;
2194 const char *psz_demux;
2201 strcpy( psz_dup, psz_mrl );
2203 if( !in ) return VLC_EGENERIC;
2204 if( !p_input ) return VLC_EGENERIC;
2207 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2209 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2210 psz_mrl, psz_access, psz_demux, psz_path );
2211 if( !p_input->b_preparsing )
2213 /* Hack to allow udp://@:port syntax */
2215 (strncmp( psz_access, "udp", 3 ) &&
2216 strncmp( psz_access, "rtp", 3 )) )
2218 /* Find optional titles and seekpoints */
2219 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2220 &in->i_seekpoint_start, &in->i_seekpoint_end );
2223 if( psz_forced_demux && *psz_forced_demux )
2225 psz_demux = psz_forced_demux;
2227 else if( *psz_demux == '\0' )
2229 /* special hack for forcing a demuxer with --demux=module
2230 * (and do nothing with a list) */
2231 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2233 if( psz_var_demux != NULL &&
2234 !strchr(psz_var_demux, ',' ) &&
2235 !strchr(psz_var_demux, ':' ) )
2237 psz_demux = psz_var_demux;
2239 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2243 /* Try access_demux if no demux given */
2244 if( *psz_demux == '\0' )
2246 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2247 NULL, p_input->p->p_es_out, false );
2252 /* Preparsing is only for file:// */
2255 if( !*psz_access ) /* path without scheme:// */
2256 psz_access = "file";
2257 if( strcmp( psz_access, "file" ) )
2259 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2264 int64_t i_pts_delay;
2266 /* Get infos from access_demux */
2267 demux_Control( in->p_demux,
2268 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2269 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2271 in->b_title_demux = true;
2272 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2273 &in->title, &in->i_title,
2274 &in->i_title_offset, &in->i_seekpoint_offset ) )
2279 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2280 &in->b_can_pace_control ) )
2281 in->b_can_pace_control = false;
2283 if( !in->b_can_pace_control )
2285 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2286 &in->b_can_rate_control, &in->b_rescale_ts ) )
2288 in->b_can_rate_control = false;
2289 in->b_rescale_ts = true; /* not used */
2294 in->b_can_rate_control = true;
2295 in->b_rescale_ts = true;
2297 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2298 &in->b_can_pause ) )
2299 in->b_can_pause = false;
2300 var_SetBool( p_input, "can-pause", in->b_can_pause );
2302 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2304 if( ret != VLC_SUCCESS )
2306 var_Set( p_input, "seekable", val );
2310 int64_t i_pts_delay;
2312 input_ChangeState( p_input, OPENING_S );
2314 /* Now try a real access */
2315 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2317 /* Access failed, URL encoded ? */
2318 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2320 decode_URI( psz_path );
2322 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2323 psz_access, psz_demux, psz_path );
2325 in->p_access = access_New( p_input,
2326 psz_access, psz_demux, psz_path );
2328 if( in->p_access == NULL )
2330 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2332 intf_UserFatal( VLC_OBJECT( p_input), false,
2333 _("Your input can't be opened"),
2334 _("VLC is unable to open the MRL '%s'."
2335 " Check the log for details."), psz_mrl );
2340 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2341 while( psz && *psz )
2343 access_t *p_access = in->p_access;
2344 char *end = strchr( psz, ':' );
2349 in->p_access = access_FilterNew( in->p_access, psz );
2350 if( in->p_access == NULL )
2352 in->p_access = p_access;
2353 msg_Warn( p_input, "failed to insert access filter %s",
2361 /* Get infos from access */
2362 if( !p_input->b_preparsing )
2364 access_Control( in->p_access,
2365 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2366 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2368 in->b_title_demux = false;
2369 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2370 &in->title, &in->i_title,
2371 &in->i_title_offset, &in->i_seekpoint_offset ) )
2377 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2378 &in->b_can_pace_control );
2379 in->b_can_rate_control = in->b_can_pace_control;
2380 in->b_rescale_ts = true;
2382 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2384 var_SetBool( p_input, "can-pause", in->b_can_pause );
2385 access_Control( in->p_access, ACCESS_CAN_SEEK,
2387 var_Set( p_input, "seekable", val );
2390 input_ChangeState( p_input, BUFFERING_S );
2392 /* Create the stream_t */
2393 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2394 if( in->p_stream == NULL )
2396 msg_Warn( p_input, "cannot create a stream_t from access" );
2400 /* Open a demuxer */
2401 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2403 psz_demux = in->p_access->psz_demux;
2407 /* Take access redirections into account */
2408 char *psz_real_path;
2409 char *psz_buf = NULL;
2410 if( in->p_access->psz_path )
2412 const char *psz_a, *psz_d;
2413 psz_buf = strdup( in->p_access->psz_path );
2414 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2418 psz_real_path = psz_path;
2420 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2422 in->p_stream, p_input->p->p_es_out,
2423 p_input->b_preparsing );
2427 if( in->p_demux == NULL )
2429 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2430 psz_access, psz_demux, psz_path );
2431 intf_UserFatal( VLC_OBJECT( p_input ), false,
2432 _("VLC can't recognize the input's format"),
2433 _("The format of '%s' cannot be detected. "
2434 "Have a look the log for details."), psz_mrl );
2438 /* Get title from demux */
2439 if( !p_input->b_preparsing && in->i_title <= 0 )
2441 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2442 &in->title, &in->i_title,
2443 &in->i_title_offset, &in->i_seekpoint_offset ))
2445 TAB_INIT( in->i_title, in->title );
2449 in->b_title_demux = true;
2455 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2456 if( 1 || !p_input->b_preparsing )
2459 input_attachment_t **attachment;
2460 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2461 &attachment, &i_attachment ) )
2463 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2464 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2465 i_attachment, attachment );
2466 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2469 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2471 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2473 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2476 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2477 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2482 input_ChangeState( p_input, ERROR_S );
2485 demux_Delete( in->p_demux );
2488 stream_Delete( in->p_stream );
2491 access_Delete( in->p_access );
2493 return VLC_EGENERIC;
2496 /*****************************************************************************
2498 *****************************************************************************/
2499 static void InputSourceClean( input_source_t *in )
2504 demux_Delete( in->p_demux );
2507 stream_Delete( in->p_stream );
2510 access_Delete( in->p_access );
2512 if( in->i_title > 0 )
2514 for( i = 0; i < in->i_title; i++ )
2515 vlc_input_title_Delete( in->title[i] );
2516 TAB_CLEAN( in->i_title, in->title );
2520 static void SlaveDemux( input_thread_t *p_input )
2525 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2527 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2531 for( i = 0; i < p_input->p->i_slave; i++ )
2533 input_source_t *in = p_input->p->slave[i];
2539 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2544 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2546 msg_Err( p_input, "slave[%d] doesn't like "
2547 "DEMUX_GET_TIME -> EOF", i );
2552 if( i_stime >= i_time )
2555 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2561 i_ret = in->p_demux->pf_demux( in->p_demux );
2566 msg_Dbg( p_input, "slave %d EOF", i );
2572 static void SlaveSeek( input_thread_t *p_input )
2577 if( !p_input ) return;
2579 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2581 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2585 for( i = 0; i < p_input->p->i_slave; i++ )
2587 input_source_t *in = p_input->p->slave[i];
2589 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2591 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2597 /*****************************************************************************
2599 *****************************************************************************/
2600 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2604 if( !p_meta ) return;
2606 /* Get meta information from user */
2607 #define GET_META( field, s ) \
2608 var_Get( p_input, (s), &val ); \
2609 if( *val.psz_string ) \
2610 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2611 free( val.psz_string )
2613 GET_META( Title, "meta-title" );
2614 GET_META( Artist, "meta-artist" );
2615 GET_META( Genre, "meta-genre" );
2616 GET_META( Copyright, "meta-copyright" );
2617 GET_META( Description, "meta-description" );
2618 GET_META( Date, "meta-date" );
2619 GET_META( URL, "meta-url" );
2623 /*****************************************************************************
2624 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2625 * arturl and locking issue.
2626 *****************************************************************************/
2627 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2629 input_item_t *p_item = p_input->p->input.p_item;
2630 char * psz_arturl = NULL;
2631 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 );
2698 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2699 for( i = 0; ppsz_all_keys[i]; i++ )
2701 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2702 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2703 free( ppsz_all_keys[i] );
2705 free( ppsz_all_keys );
2706 vlc_meta_Delete( p_meta );
2709 /** \todo handle sout meta */
2713 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2714 int i_new, input_attachment_t **pp_new )
2716 int i_attachment = *pi_attachment;
2717 input_attachment_t **attachment = *ppp_attachment;
2720 attachment = realloc( attachment,
2721 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2722 for( i = 0; i < i_new; i++ )
2723 attachment[i_attachment++] = pp_new[i];
2727 *pi_attachment = i_attachment;
2728 *ppp_attachment = attachment;
2731 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2735 if( p_input->b_preparsing )
2738 if( p_input->p->input.p_access )
2739 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2742 /* Get meta data from slave input */
2743 for( i = 0; i < p_input->p->i_slave; i++ )
2745 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2746 if( p_input->p->slave[i]->p_access )
2748 access_Control( p_input->p->slave[i]->p_access,
2749 ACCESS_GET_META, p_meta );
2754 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2761 /* XXX I am not sure it is a great idea, besides, there is more than that
2762 * if we want to do it right */
2763 vlc_mutex_lock( &p_item->lock );
2764 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2766 vlc_mutex_unlock( &p_item->lock );
2769 vlc_mutex_unlock( &p_item->lock );
2772 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2773 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2778 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2779 if(! p_demux->p_private )
2782 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2785 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2787 if( p_demux_meta->p_meta )
2789 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2790 vlc_meta_Delete( p_demux_meta->p_meta );
2793 if( p_demux_meta->i_attachments > 0 )
2795 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2796 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2797 p_demux_meta->i_attachments, p_demux_meta->attachments );
2798 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2800 module_Unneed( p_demux, p_id3 );
2802 free( p_demux->p_private );
2806 /*****************************************************************************
2807 * MRLSplit: parse the access, demux and url part of the
2808 * Media Resource Locator.
2809 *****************************************************************************/
2810 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2813 char *psz_access = NULL;
2814 char *psz_demux = NULL;
2817 /* Either there is an access/demux specification before ://
2818 * or we have a plain local file path. */
2819 psz_path = strstr( psz_dup, "://" );
2820 if( psz_path != NULL )
2823 psz_path += 3; /* skips "://" */
2825 /* Separate access from demux (<access>/<demux>://<path>) */
2826 psz_access = psz_dup;
2827 psz_demux = strchr( psz_access, '/' );
2829 *psz_demux++ = '\0';
2836 *ppsz_access = psz_access ? psz_access : "";
2837 *ppsz_demux = psz_demux ? psz_demux : "";
2838 *ppsz_path = psz_path ? psz_path : "";
2841 /*****************************************************************************
2842 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2845 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2846 *****************************************************************************/
2847 static void MRLSections( input_thread_t *p_input, char *psz_source,
2848 int *pi_title_start, int *pi_title_end,
2849 int *pi_chapter_start, int *pi_chapter_end )
2851 char *psz, *psz_end, *psz_next, *psz_check;
2853 *pi_title_start = *pi_title_end = -1;
2854 *pi_chapter_start = *pi_chapter_end = -1;
2856 /* Start by parsing titles and chapters */
2857 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2859 /* Check we are really dealing with a title/chapter section */
2860 psz_check = psz + 1;
2861 if( !*psz_check ) return;
2862 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2863 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2864 if( *psz_check == ':' && ++psz_check )
2865 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2866 if( *psz_check != '-' && *psz_check ) return;
2867 if( *psz_check == '-' && ++psz_check )
2868 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2869 if( *psz_check != ':' && *psz_check ) return;
2870 if( *psz_check == ':' && ++psz_check )
2871 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2872 if( *psz_check ) return;
2874 /* Separate start and end */
2876 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2878 /* Look for the start title */
2879 *pi_title_start = strtol( psz, &psz_next, 0 );
2880 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2881 *pi_title_end = *pi_title_start;
2884 /* Look for the start chapter */
2886 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2887 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2888 *pi_chapter_end = *pi_chapter_start;
2892 /* Look for the end title */
2893 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2894 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2897 /* Look for the end chapter */
2898 if( *psz_end ) psz_end++;
2899 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2900 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2903 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2904 psz_source, *pi_title_start, *pi_chapter_start,
2905 *pi_title_end, *pi_chapter_end );
2908 /*****************************************************************************
2909 * input_AddSubtitles: add a subtitles file and enable it
2910 *****************************************************************************/
2911 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2912 bool b_check_extension )
2914 input_source_t *sub;
2917 char *psz_path, *psz_extension;
2919 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2924 /* if we are provided a subtitle.sub file,
2925 * see if we don't have a subtitle.idx and use it instead */
2926 psz_path = strdup( psz_subtitle );
2929 psz_extension = strrchr( psz_path, '.');
2930 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2934 strcpy( psz_extension, ".idx" );
2935 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2936 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2939 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2940 psz_path, psz_subtitle );
2941 strcpy( psz_subtitle, psz_path );
2947 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2949 sub = InputSourceNew( p_input );
2950 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2952 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2955 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2957 if( count.i_int == 0 )
2959 /* if it was first one, there is disable too */
2961 if( count.i_int < list.p_list->i_count )
2963 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2964 &list.p_list->p_values[count.i_int] );
2966 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );