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 *****************************************************************************/
32 #include <vlc_common.h>
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 void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
91 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
92 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
93 int i_new, input_attachment_t **pp_new );
95 /*****************************************************************************
96 * This function creates a new input, and returns a pointer
97 * to its description. On error, it returns NULL.
99 * Variables for _public_ use:
102 * - rate,rate-slower, rate-faster
103 * - position, position-offset
104 * - time, time-offset
105 * - title,title-next,title-prev
106 * - chapter,chapter-next, chapter-prev
107 * - program, audio-es, video-es, spu-es
108 * - audio-delay, spu-delay
113 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
114 * or not, for that check position != 0.0)
116 * * For intf callback upon changes
118 * - rate-change for when playback rate changes
119 * TODO explain when Callback is called
120 * TODO complete this list (?)
121 *****************************************************************************/
122 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
123 const char *psz_header, bool b_quick,
124 sout_instance_t *p_sout )
126 static const char input_name[] = "input";
127 input_thread_t *p_input = NULL; /* thread descriptor */
131 /* Allocate descriptor */
132 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
133 VLC_OBJECT_INPUT, input_name );
134 if( p_input == NULL )
136 msg_Err( p_parent, "out of memory" );
140 /* Construct a nice name for the input timer */
141 char psz_timer_name[255];
142 char * psz_name = input_item_GetName( p_item );
143 snprintf( psz_timer_name, sizeof(psz_timer_name),
144 "input launching for '%s'", psz_name );
147 /* Start a timer to mesure how long it takes
148 * to launch an input */
149 stats_TimerStart( p_input, psz_timer_name,
150 STATS_TIMER_INPUT_LAUNCHING );
152 MALLOC_NULL( p_input->p, input_thread_private_t );
153 memset( p_input->p, 0, sizeof( input_thread_private_t ) );
155 /* One "randomly" selected input thread is responsible for computing
156 * the global stats. Check if there is already someone doing this */
157 if( p_input->p_libvlc->p_stats && !b_quick )
159 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
160 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
161 if( priv->p_stats_computer == NULL )
162 priv->p_stats_computer = p_input;
163 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
166 p_input->b_preparsing = b_quick;
167 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
170 vlc_event_manager_t * p_em = &p_input->p->event_manager;
171 vlc_event_manager_init_with_vlc_object( p_em, p_input );
172 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
174 /* Init Common fields */
175 p_input->b_eof = false;
176 p_input->b_can_pace_control = true;
177 p_input->p->i_start = 0;
179 p_input->p->i_stop = 0;
180 p_input->p->i_run = 0;
181 p_input->p->i_title = 0;
182 p_input->p->title = NULL;
183 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
184 p_input->i_state = INIT_S;
185 p_input->p->i_rate = INPUT_RATE_DEFAULT;
186 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
187 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
188 p_input->p->p_es_out = NULL;
189 p_input->p->p_sout = NULL;
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->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=%"PRId64", time=%"PRId64,
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;
296 p_input->p->p_sout = p_sout;
298 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
299 vlc_mutex_init( &p_input->p->counters.counters_lock );
301 /* Attach only once we are ready */
302 vlc_object_attach( p_input, p_parent );
304 /* Set the destructor when we are sure we are initialized */
305 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
311 * Input destructor (called when the object's refcount reaches 0).
313 static void Destructor( input_thread_t * p_input )
315 input_thread_private_t *priv = p_input->p;
317 vlc_event_manager_fini( &p_input->p->event_manager );
319 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
320 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
323 sout_DeleteInstance( priv->p_sout );
325 vlc_gc_decref( p_input->p->input.p_item );
327 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
329 vlc_mutex_destroy( &priv->lock_control );
334 * Initialize an input thread and run it. You will need to monitor the
335 * thread to clean up after it is done
337 * \param p_parent a vlc_object
338 * \param p_item an input item
339 * \return a pointer to the spawned input thread
341 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
342 input_item_t *p_item )
344 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
348 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
349 input_item_t *p_item,
350 const char *psz_log, sout_instance_t *p_sout )
352 input_thread_t *p_input;
354 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
358 /* Create thread and wait for its readiness. */
359 if( vlc_thread_create( p_input, "input", Run,
360 VLC_THREAD_PRIORITY_INPUT, true ) )
362 input_ChangeState( p_input, ERROR_S );
363 msg_Err( p_input, "cannot create input thread" );
364 vlc_object_detach( p_input );
365 vlc_object_release( p_input );
373 * Initialize an input thread and run it. This thread will clean after itself,
374 * you can forget about it. It can work either in blocking or non-blocking mode
376 * \param p_parent a vlc_object
377 * \param p_item an input item
378 * \param b_block should we block until read is finished ?
379 * \return the input object id if non blocking, an error code else
381 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
384 input_thread_t *p_input;
386 p_input = Create( p_parent, p_item, NULL, false, NULL );
392 RunAndDestroy( p_input );
397 if( vlc_thread_create( p_input, "input", RunAndDestroy,
398 VLC_THREAD_PRIORITY_INPUT, true ) )
400 input_ChangeState( p_input, ERROR_S );
401 msg_Err( p_input, "cannot create input thread" );
402 vlc_object_release( p_input );
406 return p_input->i_object_id;
410 * Initialize an input and initialize it to preparse the item
411 * This function is blocking. It will only accept to parse files
413 * \param p_parent a vlc_object_t
414 * \param p_item an input item
415 * \return VLC_SUCCESS or an error
417 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
419 input_thread_t *p_input;
421 /* Allocate descriptor */
422 p_input = Create( p_parent, p_item, NULL, true, NULL );
426 if( !Init( p_input ) )
429 vlc_object_detach( p_input );
430 vlc_object_release( p_input );
436 * Request a running input thread to stop and die
438 * \param the input thread to stop
440 void input_StopThread( input_thread_t *p_input )
445 /* Set die for input */
446 vlc_object_kill( p_input );
447 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
449 /* We cannot touch p_input fields directly (we come from another thread),
450 * so use the vlc_object_find way, it's perfectly safe */
452 /* Set die for all access */
453 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
454 for( i = 0; i < p_list->i_count; i++ )
456 vlc_object_kill( p_list->p_values[i].p_object );
458 vlc_list_release( p_list );
460 /* Set die for all stream */
461 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
462 for( i = 0; i < p_list->i_count; i++ )
464 vlc_object_kill( p_list->p_values[i].p_object );
466 vlc_list_release( p_list );
468 /* Set die for all demux */
469 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
470 for( i = 0; i < p_list->i_count; i++ )
472 vlc_object_kill( p_list->p_values[i].p_object );
474 vlc_list_release( p_list );
476 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
479 sout_instance_t * input_DetachSout( input_thread_t *p_input )
481 sout_instance_t *p_sout = p_input->p->p_sout;
482 vlc_object_detach( p_sout );
483 p_input->p->p_sout = NULL;
487 /*****************************************************************************
488 * Run: main thread loop
489 * This is the "normal" thread that spawns the input processing chain,
490 * reads the stream, cleans up and waits
491 *****************************************************************************/
492 static int Run( input_thread_t *p_input )
494 /* Signal that the thread is launched */
495 vlc_thread_ready( p_input );
497 if( Init( p_input ) )
499 /* If we failed, wait before we are killed, and exit */
500 p_input->b_error = true;
502 /* FIXME: we don't want to depend on the playlist */
503 playlist_t * p_playlist = vlc_object_find( p_input,
504 VLC_OBJECT_PLAYLIST, FIND_PARENT );
507 playlist_Signal( p_playlist );
508 vlc_object_release( p_playlist );
513 /* Tell we're dead */
514 p_input->b_dead = true;
521 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
523 /* We have finish to demux data but not to play them */
524 while( !p_input->b_die )
526 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
529 msg_Dbg( p_input, "waiting decoder fifos to empty" );
531 msleep( INPUT_IDLE_SLEEP );
534 /* We have finished */
535 p_input->b_eof = true;
536 playlist_Signal( libvlc_priv (p_input->p_libvlc)->p_playlist );
539 /* Wait until we are asked to die */
540 if( !p_input->b_die )
551 /*****************************************************************************
552 * RunAndDestroy: main thread loop
553 * This is the "just forget me" thread that spawns the input processing chain,
554 * reads the stream, cleans up and releases memory
555 *****************************************************************************/
556 static int RunAndDestroy( input_thread_t *p_input )
558 /* Signal that the thread is launched */
559 vlc_thread_ready( p_input );
561 if( Init( p_input ) )
566 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
568 /* We have finished demuxing data but not playing it */
569 while( !p_input->b_die )
571 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
574 msg_Dbg( p_input, "waiting decoder fifos to empty" );
576 msleep( INPUT_IDLE_SLEEP );
579 /* We have finished */
580 p_input->b_eof = true;
588 vlc_object_release( p_input );
592 /*****************************************************************************
593 * Main loop: Fill buffers from access, and demux
594 *****************************************************************************/
595 static void MainLoop( input_thread_t *p_input )
597 int64_t i_start_mdate = mdate();
598 int64_t i_intf_update = 0;
602 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
604 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
606 bool b_force_update = false;
612 if( p_input->i_state != PAUSE_S )
614 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
615 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
618 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
623 if( p_input->p->input.b_title_demux &&
624 p_input->p->input.p_demux->info.i_update )
626 i_ret = UpdateFromDemux( p_input );
627 b_force_update = true;
629 else if( !p_input->p->input.b_title_demux &&
630 p_input->p->input.p_access &&
631 p_input->p->input.p_access->info.i_update )
633 i_ret = UpdateFromAccess( p_input );
634 b_force_update = true;
638 if( i_ret == 0 ) /* EOF */
642 var_Get( p_input, "input-repeat", &repeat );
643 if( repeat.i_int == 0 )
645 /* End of file - we do not set b_die because only the
646 * playlist is allowed to do so. */
647 input_ChangeState( p_input, END_S );
648 msg_Dbg( p_input, "EOF reached" );
649 p_input->p->input.b_eof = true;
653 msg_Dbg( p_input, "repeating the same input (%d)",
655 if( repeat.i_int > 0 )
658 var_Set( p_input, "input-repeat", repeat );
661 /* Seek to start title/seekpoint */
662 val.i_int = p_input->p->input.i_title_start -
663 p_input->p->input.i_title_offset;
664 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
666 input_ControlPush( p_input,
667 INPUT_CONTROL_SET_TITLE, &val );
669 val.i_int = p_input->p->input.i_seekpoint_start -
670 p_input->p->input.i_seekpoint_offset;
671 if( val.i_int > 0 /* TODO: check upper boundary */ )
672 input_ControlPush( p_input,
673 INPUT_CONTROL_SET_SEEKPOINT, &val );
675 /* Seek to start position */
676 if( p_input->p->i_start > 0 )
678 val.i_time = p_input->p->i_start;
679 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
685 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
690 i_start_mdate = mdate();
695 p_input->b_error = true;
698 if( i_ret > 0 && p_input->p->i_slave > 0 )
700 SlaveDemux( p_input );
710 vlc_mutex_lock( &p_input->p->lock_control );
711 ControlReduce( p_input );
712 while( !ControlPopNoLock( p_input, &i_type, &val ) )
714 msg_Dbg( p_input, "control type=%d", i_type );
715 if( Control( p_input, i_type, val ) )
716 b_force_update = true;
718 vlc_mutex_unlock( &p_input->p->lock_control );
720 if( b_force_update || i_intf_update < mdate() )
724 int64_t i_time, i_length;
725 /* update input status variables */
726 if( !demux_Control( p_input->p->input.p_demux,
727 DEMUX_GET_POSITION, &f_pos ) )
729 val.f_float = (float)f_pos;
730 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
732 if( !demux_Control( p_input->p->input.p_demux,
733 DEMUX_GET_TIME, &i_time ) )
735 p_input->i_time = i_time;
737 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
739 if( !demux_Control( p_input->p->input.p_demux,
740 DEMUX_GET_LENGTH, &i_length ) )
743 var_Get( p_input, "length", &old_val );
744 val.i_time = i_length;
745 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
747 if( old_val.i_time != val.i_time )
749 UpdateItemLength( p_input, i_length );
753 var_SetBool( p_input, "intf-change", true );
754 i_intf_update = mdate() + INT64_C(150000);
756 /* 150ms * 8 = ~ 1 second */
757 if( ++i_updates % 8 == 0 )
759 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
760 /* Are we the thread responsible for computing global stats ? */
761 if( libvlc_priv (p_input->p_libvlc)->p_stats_computer == p_input )
763 stats_ComputeGlobalStats( p_input->p_libvlc,
764 p_input->p_libvlc->p_stats );
770 static void InitStatistics( input_thread_t * p_input )
772 if( p_input->b_preparsing ) return;
774 /* Prepare statistics */
775 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
776 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
777 if( libvlc_stats (p_input) )
779 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
780 INIT_COUNTER( read_packets, INTEGER, COUNTER );
781 INIT_COUNTER( demux_read, INTEGER, COUNTER );
782 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
783 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
784 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
785 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
786 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
787 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
788 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
789 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
790 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
791 p_input->p->counters.p_sout_send_bitrate = NULL;
792 p_input->p->counters.p_sout_sent_packets = NULL;
793 p_input->p->counters.p_sout_sent_bytes = NULL;
794 if( p_input->p->counters.p_demux_bitrate )
795 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
796 if( p_input->p->counters.p_input_bitrate )
797 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
802 static int InitSout( input_thread_t * p_input )
806 if( p_input->b_preparsing ) return VLC_SUCCESS;
808 /* Find a usable sout and attach it to p_input */
809 psz = var_GetNonEmptyString( p_input, "sout" );
810 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
812 /* Check the validity of the provided sout */
813 if( p_input->p->p_sout )
815 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
817 msg_Dbg( p_input, "destroying unusable sout" );
819 sout_DeleteInstance( p_input->p->p_sout );
820 p_input->p->p_sout = NULL;
824 if( p_input->p->p_sout )
827 msg_Dbg( p_input, "sout keep: reusing sout" );
828 msg_Dbg( p_input, "sout keep: you probably want to use "
829 "gather stream_out" );
830 vlc_object_attach( p_input->p->p_sout, p_input );
834 /* Create a new one */
835 p_input->p->p_sout = sout_NewInstance( p_input, psz );
836 if( !p_input->p->p_sout )
838 input_ChangeState( p_input, ERROR_S );
839 msg_Err( p_input, "cannot start stream output instance, " \
845 if( libvlc_stats (p_input) )
847 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
848 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
849 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
850 if( p_input->p->counters.p_sout_send_bitrate )
851 p_input->p->counters.p_sout_send_bitrate->update_interval =
855 else if( p_input->p->p_sout )
857 msg_Dbg( p_input, "destroying useless sout" );
859 sout_DeleteInstance( p_input->p->p_sout );
860 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 )
915 /* Start title/chapter */
917 if( p_input->b_preparsing )
919 p_input->p->i_start = 0;
923 val.i_int = p_input->p->input.i_title_start -
924 p_input->p->input.i_title_offset;
925 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
926 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
927 val.i_int = p_input->p->input.i_seekpoint_start -
928 p_input->p->input.i_seekpoint_offset;
929 if( val.i_int > 0 /* TODO: check upper boundary */ )
930 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
934 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
935 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
936 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
937 i_length = var_GetTime( p_input, "length" );
938 if( p_input->p->i_run < 0 )
940 msg_Warn( p_input, "invalid run-time ignored" );
941 p_input->p->i_run = 0;
944 if( p_input->p->i_start > 0 )
946 if( p_input->p->i_start >= i_length )
948 msg_Warn( p_input, "invalid start-time ignored" );
954 msg_Dbg( p_input, "starting at time: %ds",
955 (int)( p_input->p->i_start / INT64_C(1000000) ) );
957 s.i_time = p_input->p->i_start;
958 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
961 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
963 msg_Warn( p_input, "invalid stop-time ignored" );
964 p_input->p->i_stop = 0;
968 /* Get fps and set it if not already set */
969 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
972 float f_requested_fps;
974 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
975 var_SetFloat( p_input, "sub-original-fps", f_fps );
977 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
978 if( f_requested_fps != f_fps )
980 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
982 var_SetFloat( p_input, "sub-fps", f_requested_fps );
986 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
989 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
992 /* Look for and add subtitle files */
993 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
994 if( psz_subtitle != NULL )
996 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
997 input_AddSubtitles( p_input, psz_subtitle, false );
1000 var_Get( p_input, "sub-autodetect-file", &val );
1003 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1004 char **subs = subtitles_Detect( p_input, psz_autopath,
1005 p_input->p->input.p_item->psz_uri );
1006 input_source_t *sub;
1008 if( psz_autopath == NULL )
1009 psz_autopath = strdup("");
1011 /* Try to autoselect the first autodetected subtitles file
1012 * if no subtitles file was specified */
1013 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1015 input_AddSubtitles( p_input, subs[0], false );
1020 /* Then, just add the following subtitles files */
1021 for( ; subs && subs[i]; i++ )
1023 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1025 sub = InputSourceNew( p_input );
1026 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1028 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1035 free( psz_autopath );
1037 free( psz_subtitle );
1039 /* Look for slave */
1040 psz = var_GetNonEmptyString( p_input, "input-slave" );
1044 input_source_t *slave;
1045 while( psz && *psz )
1047 while( *psz == ' ' || *psz == '#' )
1051 if( ( psz_delim = strchr( psz, '#' ) ) )
1053 *psz_delim++ = '\0';
1060 msg_Dbg( p_input, "adding slave input '%s'", psz );
1061 slave = InputSourceNew( p_input );
1062 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1064 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1073 static void InitPrograms( input_thread_t * p_input )
1078 if( p_input->b_preparsing ) return;
1081 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1082 i_es_out_mode = ES_OUT_MODE_AUTO;
1084 if( p_input->p->p_sout )
1086 var_Get( p_input, "sout-all", &val );
1089 i_es_out_mode = ES_OUT_MODE_ALL;
1094 var_Get( p_input, "programs", &val );
1095 if ( val.p_list && val.p_list->i_count )
1097 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1098 /* Note : we should remove the "program" callback. */
1101 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1105 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1107 /* Inform the demuxer about waited group (needed only for DVB) */
1108 if( i_es_out_mode == ES_OUT_MODE_ALL )
1110 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1112 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1114 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1119 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1120 (int) var_GetInteger( p_input, "program" ), NULL );
1124 static int Init( input_thread_t * p_input )
1130 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1132 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1134 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1135 var_SetString( p_input, "sout", "" );
1136 var_SetBool( p_input, "sout-all", false );
1137 var_SetString( p_input, "input-slave", "" );
1138 var_SetInteger( p_input, "input-repeat", 0 );
1139 var_SetString( p_input, "sub-file", "" );
1140 var_SetBool( p_input, "sub-autodetect-file", false );
1144 InitStatistics( p_input );
1146 ret = InitSout( p_input );
1147 if( ret != VLC_SUCCESS )
1148 return ret; /* FIXME: goto error; should be better here */
1152 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1153 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1154 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1156 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1157 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1159 if( InputSourceInit( p_input, &p_input->p->input,
1160 p_input->p->input.p_item->psz_uri, NULL ) )
1165 InitTitle( p_input );
1167 /* Load master infos */
1169 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1170 &val.i_time ) && val.i_time > 0 )
1172 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1173 UpdateItemLength( p_input, val.i_time );
1177 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1178 if( val.i_time > 0 )
1179 { /* fallback: gets length from metadata */
1180 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1181 UpdateItemLength( p_input, val.i_time );
1185 StartTitle( p_input );
1187 InitPrograms( p_input );
1189 if( !p_input->b_preparsing && p_input->p->p_sout )
1191 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1193 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1195 /* We don't want a high input priority here or we'll
1196 * end-up sucking up all the CPU time */
1197 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1200 msg_Dbg( p_input, "starting in %s mode",
1201 p_input->p->b_out_pace_control ? "async" : "sync" );
1204 p_meta = vlc_meta_New();
1206 /* Get meta data from users */
1207 InputMetaUser( p_input, p_meta );
1209 /* Get meta data from master input */
1210 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1212 /* Access_file does not give any meta, and there are no slave */
1213 AccessMeta( p_input, p_meta );
1215 InputUpdateMeta( p_input, p_meta );
1217 if( !p_input->b_preparsing )
1219 msg_Dbg( p_input, "`%s' successfully opened",
1220 p_input->p->input.p_item->psz_uri );
1224 /* initialization is complete */
1225 input_ChangeState( p_input, PLAYING_S );
1230 input_ChangeState( p_input, ERROR_S );
1232 if( p_input->p->p_es_out )
1233 input_EsOutDelete( p_input->p->p_es_out );
1235 if( p_input->p->p_sout )
1237 vlc_object_detach( p_input->p->p_sout );
1238 sout_DeleteInstance( p_input->p->p_sout );
1242 if( !p_input->b_preparsing && libvlc_stats (p_input) )
1244 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1245 stats_CounterClean( p_input->p->counters.p_##c );\
1246 p_input->p->counters.p_##c = NULL; } while(0)
1247 EXIT_COUNTER( read_bytes );
1248 EXIT_COUNTER( read_packets );
1249 EXIT_COUNTER( demux_read );
1250 EXIT_COUNTER( input_bitrate );
1251 EXIT_COUNTER( demux_bitrate );
1252 EXIT_COUNTER( played_abuffers );
1253 EXIT_COUNTER( lost_abuffers );
1254 EXIT_COUNTER( displayed_pictures );
1255 EXIT_COUNTER( lost_pictures );
1256 EXIT_COUNTER( decoded_audio );
1257 EXIT_COUNTER( decoded_video );
1258 EXIT_COUNTER( decoded_sub );
1260 if( p_input->p->p_sout )
1262 EXIT_COUNTER( sout_sent_packets );
1263 EXIT_COUNTER (sout_sent_bytes );
1264 EXIT_COUNTER( sout_send_bitrate );
1269 /* Mark them deleted */
1270 p_input->p->input.p_demux = NULL;
1271 p_input->p->input.p_stream = NULL;
1272 p_input->p->input.p_access = NULL;
1273 p_input->p->p_es_out = NULL;
1274 p_input->p->p_sout = NULL;
1276 return VLC_EGENERIC;
1279 /*****************************************************************************
1280 * Error: RunThread() error loop
1281 *****************************************************************************
1282 * This function is called when an error occurred during thread main's loop.
1283 *****************************************************************************/
1284 static void Error( input_thread_t *p_input )
1286 input_ChangeState( p_input, ERROR_S );
1287 while( !p_input->b_die )
1290 msleep( INPUT_IDLE_SLEEP );
1294 /*****************************************************************************
1295 * End: end the input thread
1296 *****************************************************************************/
1297 static void End( input_thread_t * p_input )
1301 /* We are at the end */
1302 input_ChangeState( p_input, END_S );
1304 /* Clean control variables */
1305 input_ControlVarClean( p_input );
1307 /* Clean up master */
1308 InputSourceClean( &p_input->p->input );
1311 for( i = 0; i < p_input->p->i_slave; i++ )
1313 InputSourceClean( p_input->p->slave[i] );
1314 free( p_input->p->slave[i] );
1316 free( p_input->p->slave );
1318 /* Unload all modules */
1319 if( p_input->p->p_es_out )
1320 input_EsOutDelete( p_input->p->p_es_out );
1322 if( !p_input->b_preparsing )
1324 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1325 if( libvlc_stats (p_input) )
1327 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
1329 /* make sure we are up to date */
1330 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1331 if( priv->p_stats_computer == p_input )
1333 stats_ComputeGlobalStats( p_input->p_libvlc,
1334 p_input->p_libvlc->p_stats );
1335 priv->p_stats_computer = NULL;
1337 CL_CO( read_bytes );
1338 CL_CO( read_packets );
1339 CL_CO( demux_read );
1340 CL_CO( input_bitrate );
1341 CL_CO( demux_bitrate );
1342 CL_CO( played_abuffers );
1343 CL_CO( lost_abuffers );
1344 CL_CO( displayed_pictures );
1345 CL_CO( lost_pictures );
1346 CL_CO( decoded_audio) ;
1347 CL_CO( decoded_video );
1348 CL_CO( decoded_sub) ;
1351 /* Close optional stream output instance */
1352 if( p_input->p->p_sout )
1354 CL_CO( sout_sent_packets );
1355 CL_CO( sout_sent_bytes );
1356 CL_CO( sout_send_bitrate );
1358 vlc_object_detach( p_input->p->p_sout );
1363 if( p_input->p->i_attachment > 0 )
1365 for( i = 0; i < p_input->p->i_attachment; i++ )
1366 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1367 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1370 /* Tell we're dead */
1371 p_input->b_dead = true;
1374 /*****************************************************************************
1376 *****************************************************************************/
1377 static inline int ControlPopNoLock( input_thread_t *p_input,
1378 int *pi_type, vlc_value_t *p_val )
1380 if( p_input->p->i_control <= 0 )
1382 return VLC_EGENERIC;
1385 *pi_type = p_input->p->control[0].i_type;
1386 *p_val = p_input->p->control[0].val;
1388 p_input->p->i_control--;
1389 if( p_input->p->i_control > 0 )
1393 for( i = 0; i < p_input->p->i_control; i++ )
1395 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1396 p_input->p->control[i].val = p_input->p->control[i+1].val;
1403 static void ControlReduce( input_thread_t *p_input )
1410 for( i = 1; i < p_input->p->i_control; i++ )
1412 const int i_lt = p_input->p->control[i-1].i_type;
1413 const int i_ct = p_input->p->control[i].i_type;
1415 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1416 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1420 ( i_ct == INPUT_CONTROL_SET_STATE ||
1421 i_ct == INPUT_CONTROL_SET_RATE ||
1422 i_ct == INPUT_CONTROL_SET_POSITION ||
1423 i_ct == INPUT_CONTROL_SET_TIME ||
1424 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1425 i_ct == INPUT_CONTROL_SET_TITLE ||
1426 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1427 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1430 // msg_Dbg( p_input, "merged at %d", i );
1431 /* Remove the i-1 */
1432 for( j = i; j < p_input->p->i_control; j++ )
1433 p_input->p->control[j-1] = p_input->p->control[j];
1434 p_input->p->i_control--;
1438 /* TODO but that's not that important
1439 - merge SET_X with SET_X_CMD
1440 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1441 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1448 static bool Control( input_thread_t *p_input, int i_type,
1451 bool b_force_update = false;
1453 if( !p_input ) return b_force_update;
1457 case INPUT_CONTROL_SET_DIE:
1458 msg_Dbg( p_input, "control: stopping input" );
1459 /* Mark all submodules to die */
1460 if( p_input->p->input.p_access )
1461 vlc_object_kill( p_input->p->input.p_access );
1462 if( p_input->p->input.p_stream )
1463 vlc_object_kill( p_input->p->input.p_stream );
1464 vlc_object_kill( p_input->p->input.p_demux );
1466 vlc_object_kill( p_input );
1469 case INPUT_CONTROL_SET_POSITION:
1470 case INPUT_CONTROL_SET_POSITION_OFFSET:
1473 if( i_type == INPUT_CONTROL_SET_POSITION )
1475 f_pos = val.f_float;
1479 /* Should not fail */
1480 demux_Control( p_input->p->input.p_demux,
1481 DEMUX_GET_POSITION, &f_pos );
1482 f_pos += val.f_float;
1484 if( f_pos < 0.0 ) f_pos = 0.0;
1485 if( f_pos > 1.0 ) f_pos = 1.0;
1486 /* Reset the decoders states and clock sync (before calling the demuxer */
1487 input_EsOutChangePosition( p_input->p->p_es_out );
1488 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1491 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1492 "%2.1f%% failed", f_pos * 100 );
1496 if( p_input->p->i_slave > 0 )
1497 SlaveSeek( p_input );
1499 b_force_update = true;
1504 case INPUT_CONTROL_SET_TIME:
1505 case INPUT_CONTROL_SET_TIME_OFFSET:
1510 if( i_type == INPUT_CONTROL_SET_TIME )
1512 i_time = val.i_time;
1516 /* Should not fail */
1517 demux_Control( p_input->p->input.p_demux,
1518 DEMUX_GET_TIME, &i_time );
1519 i_time += val.i_time;
1521 if( i_time < 0 ) i_time = 0;
1523 /* Reset the decoders states and clock sync (before calling the demuxer */
1524 input_EsOutChangePosition( p_input->p->p_es_out );
1526 i_ret = demux_Control( p_input->p->input.p_demux,
1527 DEMUX_SET_TIME, i_time );
1532 /* Emulate it with a SET_POS */
1533 demux_Control( p_input->p->input.p_demux,
1534 DEMUX_GET_LENGTH, &i_length );
1537 double f_pos = (double)i_time / (double)i_length;
1538 i_ret = demux_Control( p_input->p->input.p_demux,
1539 DEMUX_SET_POSITION, f_pos );
1544 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1545 " failed or not possible", i_time );
1549 if( p_input->p->i_slave > 0 )
1550 SlaveSeek( p_input );
1552 b_force_update = true;
1557 case INPUT_CONTROL_SET_STATE:
1558 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1559 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1562 if( p_input->p->input.p_access )
1563 i_ret = access_Control( p_input->p->input.p_access,
1564 ACCESS_SET_PAUSE_STATE, false );
1566 i_ret = demux_Control( p_input->p->input.p_demux,
1567 DEMUX_SET_PAUSE_STATE, false );
1571 /* FIXME What to do ? */
1572 msg_Warn( p_input, "cannot unset pause -> EOF" );
1573 vlc_mutex_unlock( &p_input->p->lock_control );
1574 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1575 vlc_mutex_lock( &p_input->p->lock_control );
1578 b_force_update = true;
1580 /* Switch to play */
1581 p_input->i_state = PLAYING_S;
1582 val.i_int = PLAYING_S;
1583 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1587 input_EsOutChangeState( p_input->p->p_es_out );
1589 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1590 p_input->p->b_can_pause )
1593 if( p_input->p->input.p_access )
1594 i_ret = access_Control( p_input->p->input.p_access,
1595 ACCESS_SET_PAUSE_STATE, true );
1597 i_ret = demux_Control( p_input->p->input.p_demux,
1598 DEMUX_SET_PAUSE_STATE, true );
1600 b_force_update = true;
1604 msg_Warn( p_input, "cannot set pause state" );
1605 val.i_int = p_input->i_state;
1609 val.i_int = PAUSE_S;
1612 /* Switch to new state */
1613 p_input->i_state = val.i_int;
1614 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1618 input_EsOutChangeState( p_input->p->p_es_out );
1620 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1622 b_force_update = true;
1624 /* Correct "state" value */
1625 val.i_int = p_input->i_state;
1626 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1628 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1630 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1634 case INPUT_CONTROL_SET_RATE:
1635 case INPUT_CONTROL_SET_RATE_SLOWER:
1636 case INPUT_CONTROL_SET_RATE_FASTER:
1640 if( i_type == INPUT_CONTROL_SET_RATE )
1646 static const int ppi_factor[][2] = {
1647 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1649 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1658 for( i = 0; ppi_factor[i][0] != 0; i++ )
1660 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1661 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1662 if( i_test_e < i_error )
1668 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1670 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1672 if( ppi_factor[i_idx+1][0] > 0 )
1673 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1675 i_rate = INPUT_RATE_MAX+1;
1679 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1681 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1683 i_rate = INPUT_RATE_MIN-1;
1687 if( i_rate < INPUT_RATE_MIN )
1689 msg_Dbg( p_input, "cannot set rate faster" );
1690 i_rate = INPUT_RATE_MIN;
1692 else if( i_rate > INPUT_RATE_MAX )
1694 msg_Dbg( p_input, "cannot set rate slower" );
1695 i_rate = INPUT_RATE_MAX;
1697 if( i_rate != INPUT_RATE_DEFAULT &&
1698 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1699 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1701 msg_Dbg( p_input, "cannot change rate" );
1702 i_rate = INPUT_RATE_DEFAULT;
1704 if( i_rate != p_input->p->i_rate &&
1705 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1708 if( p_input->p->input.p_access )
1709 i_ret = VLC_EGENERIC;
1711 i_ret = demux_Control( p_input->p->input.p_demux,
1712 DEMUX_SET_RATE, &i_rate );
1715 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1716 i_rate = p_input->p->i_rate;
1721 if( i_rate != p_input->p->i_rate )
1724 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1725 var_SetBool( p_input, "rate-change", true );
1727 p_input->p->i_rate = i_rate;
1729 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1730 if( p_input->p->input.b_rescale_ts )
1731 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1733 b_force_update = true;
1738 case INPUT_CONTROL_SET_PROGRAM:
1739 /* No need to force update, es_out does it if needed */
1740 es_out_Control( p_input->p->p_es_out,
1741 ES_OUT_SET_GROUP, val.i_int );
1743 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1747 case INPUT_CONTROL_SET_ES:
1748 /* No need to force update, es_out does it if needed */
1749 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1750 input_EsOutGetFromID( p_input->p->p_es_out,
1754 case INPUT_CONTROL_SET_AUDIO_DELAY:
1755 input_EsOutSetDelay( p_input->p->p_es_out,
1756 AUDIO_ES, val.i_time );
1757 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1760 case INPUT_CONTROL_SET_SPU_DELAY:
1761 input_EsOutSetDelay( p_input->p->p_es_out,
1762 SPU_ES, val.i_time );
1763 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1766 case INPUT_CONTROL_SET_TITLE:
1767 case INPUT_CONTROL_SET_TITLE_NEXT:
1768 case INPUT_CONTROL_SET_TITLE_PREV:
1769 if( p_input->p->input.b_title_demux &&
1770 p_input->p->input.i_title > 0 )
1773 /* FIXME handle demux title */
1774 demux_t *p_demux = p_input->p->input.p_demux;
1777 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1778 i_title = p_demux->info.i_title - 1;
1779 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1780 i_title = p_demux->info.i_title + 1;
1782 i_title = val.i_int;
1784 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1786 input_EsOutChangePosition( p_input->p->p_es_out );
1788 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1789 input_ControlVarTitle( p_input, i_title );
1792 else if( p_input->p->input.i_title > 0 )
1794 access_t *p_access = p_input->p->input.p_access;
1797 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1798 i_title = p_access->info.i_title - 1;
1799 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1800 i_title = p_access->info.i_title + 1;
1802 i_title = val.i_int;
1804 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1806 input_EsOutChangePosition( p_input->p->p_es_out );
1808 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1809 stream_AccessReset( p_input->p->input.p_stream );
1813 case INPUT_CONTROL_SET_SEEKPOINT:
1814 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1815 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1816 if( p_input->p->input.b_title_demux &&
1817 p_input->p->input.i_title > 0 )
1819 demux_t *p_demux = p_input->p->input.p_demux;
1821 int64_t i_input_time;
1822 int64_t i_seekpoint_time;
1824 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1826 i_seekpoint = p_demux->info.i_seekpoint;
1827 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1828 if( i_seekpoint_time >= 0 &&
1829 !demux_Control( p_demux,
1830 DEMUX_GET_TIME, &i_input_time ) )
1832 if ( i_input_time < i_seekpoint_time + 3000000 )
1838 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1839 i_seekpoint = p_demux->info.i_seekpoint + 1;
1841 i_seekpoint = val.i_int;
1843 if( i_seekpoint >= 0 && i_seekpoint <
1844 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1847 input_EsOutChangePosition( p_input->p->p_es_out );
1849 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1852 else if( p_input->p->input.i_title > 0 )
1854 demux_t *p_demux = p_input->p->input.p_demux;
1855 access_t *p_access = p_input->p->input.p_access;
1857 int64_t i_input_time;
1858 int64_t i_seekpoint_time;
1860 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1862 i_seekpoint = p_access->info.i_seekpoint;
1863 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1864 if( i_seekpoint_time >= 0 &&
1865 demux_Control( p_demux,
1866 DEMUX_GET_TIME, &i_input_time ) )
1868 if ( i_input_time < i_seekpoint_time + 3000000 )
1874 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1875 i_seekpoint = p_access->info.i_seekpoint + 1;
1877 i_seekpoint = val.i_int;
1879 if( i_seekpoint >= 0 && i_seekpoint <
1880 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1882 input_EsOutChangePosition( p_input->p->p_es_out );
1884 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1886 stream_AccessReset( p_input->p->input.p_stream );
1891 case INPUT_CONTROL_ADD_SLAVE:
1892 if( val.psz_string )
1894 input_source_t *slave = InputSourceNew( p_input );
1896 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1902 msg_Dbg( p_input, "adding %s as slave on the fly",
1906 if( demux_Control( p_input->p->input.p_demux,
1907 DEMUX_GET_TIME, &i_time ) )
1909 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1910 InputSourceClean( slave );
1914 if( demux_Control( slave->p_demux,
1915 DEMUX_SET_TIME, i_time ) )
1917 msg_Err( p_input, "seek failed for new slave" );
1918 InputSourceClean( slave );
1923 /* Get meta (access and demux) */
1924 p_meta = vlc_meta_New();
1925 access_Control( slave->p_access, ACCESS_GET_META,
1927 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1928 InputUpdateMeta( p_input, p_meta );
1930 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1935 msg_Warn( p_input, "failed to add %s as slave",
1939 free( val.psz_string );
1943 case INPUT_CONTROL_SET_BOOKMARK:
1945 msg_Err( p_input, "not yet implemented" );
1949 return b_force_update;
1952 /*****************************************************************************
1954 *****************************************************************************/
1955 static int UpdateFromDemux( input_thread_t *p_input )
1957 demux_t *p_demux = p_input->p->input.p_demux;
1960 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1962 v.i_int = p_demux->info.i_title;
1963 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1965 input_ControlVarTitle( p_input, p_demux->info.i_title );
1967 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1969 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1971 v.i_int = p_demux->info.i_seekpoint;
1972 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1974 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1976 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1978 /* Hmmm only works with master input */
1979 if( p_input->p->input.p_demux == p_demux )
1981 int i_title_end = p_input->p->input.i_title_end -
1982 p_input->p->input.i_title_offset;
1983 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1984 p_input->p->input.i_seekpoint_offset;
1986 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1988 if( p_demux->info.i_title > i_title_end ||
1989 ( p_demux->info.i_title == i_title_end &&
1990 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1992 else if( i_seekpoint_end >=0 )
1994 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1996 else if( i_title_end >= 0 )
1998 if( p_demux->info.i_title > i_title_end ) return 0;
2005 /*****************************************************************************
2007 *****************************************************************************/
2008 static int UpdateFromAccess( input_thread_t *p_input )
2010 access_t *p_access = p_input->p->input.p_access;
2013 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2015 v.i_int = p_access->info.i_title;
2016 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2018 input_ControlVarTitle( p_input, p_access->info.i_title );
2020 stream_AccessUpdate( p_input->p->input.p_stream );
2022 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2024 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2026 v.i_int = p_access->info.i_seekpoint;
2027 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2028 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2030 if( p_access->info.i_update & INPUT_UPDATE_META )
2032 /* TODO maybe multi - access ? */
2033 vlc_meta_t *p_meta = vlc_meta_New();
2034 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2035 InputUpdateMeta( p_input, p_meta );
2036 p_access->info.i_update &= ~INPUT_UPDATE_META;
2039 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2041 /* Hmmm only works with master input */
2042 if( p_input->p->input.p_access == p_access )
2044 int i_title_end = p_input->p->input.i_title_end -
2045 p_input->p->input.i_title_offset;
2046 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2047 p_input->p->input.i_seekpoint_offset;
2049 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2051 if( p_access->info.i_title > i_title_end ||
2052 ( p_access->info.i_title == i_title_end &&
2053 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2055 else if( i_seekpoint_end >=0 )
2057 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2059 else if( i_title_end >= 0 )
2061 if( p_access->info.i_title > i_title_end ) return 0;
2068 /*****************************************************************************
2070 *****************************************************************************/
2071 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2073 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2076 /*****************************************************************************
2078 *****************************************************************************/
2079 static input_source_t *InputSourceNew( input_thread_t *p_input )
2081 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2083 memset( in, 0, sizeof( input_source_t ) );
2087 /*****************************************************************************
2089 *****************************************************************************/
2090 static int InputSourceInit( input_thread_t *p_input,
2091 input_source_t *in, const char *psz_mrl,
2092 const char *psz_forced_demux )
2094 char psz_dup[strlen (psz_mrl) + 1];
2095 const char *psz_access;
2096 const char *psz_demux;
2103 strcpy( psz_dup, psz_mrl );
2105 if( !in ) return VLC_EGENERIC;
2106 if( !p_input ) return VLC_EGENERIC;
2109 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2111 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2112 psz_mrl, psz_access, psz_demux, psz_path );
2113 if( !p_input->b_preparsing )
2115 /* Hack to allow udp://@:port syntax */
2117 (strncmp( psz_access, "udp", 3 ) &&
2118 strncmp( psz_access, "rtp", 3 )) )
2120 /* Find optional titles and seekpoints */
2121 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2122 &in->i_seekpoint_start, &in->i_seekpoint_end );
2125 if( psz_forced_demux && *psz_forced_demux )
2127 psz_demux = psz_forced_demux;
2129 else if( *psz_demux == '\0' )
2131 /* special hack for forcing a demuxer with --demux=module
2132 * (and do nothing with a list) */
2133 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2135 if( psz_var_demux != NULL &&
2136 !strchr(psz_var_demux, ',' ) &&
2137 !strchr(psz_var_demux, ':' ) )
2139 psz_demux = psz_var_demux;
2141 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2145 /* Try access_demux if no demux given */
2146 if( *psz_demux == '\0' )
2148 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2149 NULL, p_input->p->p_es_out, false );
2154 /* Preparsing is only for file:// */
2157 if( !*psz_access ) /* path without scheme:// */
2158 psz_access = "file";
2159 if( strcmp( psz_access, "file" ) )
2161 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2166 int64_t i_pts_delay;
2168 /* Get infos from access_demux */
2169 demux_Control( in->p_demux,
2170 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2171 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2173 in->b_title_demux = true;
2174 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2175 &in->title, &in->i_title,
2176 &in->i_title_offset, &in->i_seekpoint_offset ) )
2181 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2182 &in->b_can_pace_control ) )
2183 in->b_can_pace_control = false;
2185 if( !in->b_can_pace_control )
2187 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2188 &in->b_can_rate_control, &in->b_rescale_ts ) )
2190 in->b_can_rate_control = false;
2191 in->b_rescale_ts = true; /* not used */
2196 in->b_can_rate_control = true;
2197 in->b_rescale_ts = true;
2199 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2200 &in->b_can_pause ) )
2201 in->b_can_pause = false;
2202 var_SetBool( p_input, "can-pause", in->b_can_pause );
2204 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2206 if( ret != VLC_SUCCESS )
2208 var_Set( p_input, "seekable", val );
2212 int64_t i_pts_delay;
2214 input_ChangeState( p_input, OPENING_S );
2216 /* Now try a real access */
2217 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2219 /* Access failed, URL encoded ? */
2220 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2222 decode_URI( psz_path );
2224 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2225 psz_access, psz_demux, psz_path );
2227 in->p_access = access_New( p_input,
2228 psz_access, psz_demux, psz_path );
2230 if( in->p_access == NULL )
2232 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2234 intf_UserFatal( VLC_OBJECT( p_input), false,
2235 _("Your input can't be opened"),
2236 _("VLC is unable to open the MRL '%s'."
2237 " Check the log for details."), psz_mrl );
2242 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2243 while( psz && *psz )
2245 access_t *p_access = in->p_access;
2246 char *end = strchr( psz, ':' );
2251 in->p_access = access_FilterNew( in->p_access, psz );
2252 if( in->p_access == NULL )
2254 in->p_access = p_access;
2255 msg_Warn( p_input, "failed to insert access filter %s",
2263 /* Get infos from access */
2264 if( !p_input->b_preparsing )
2266 access_Control( in->p_access,
2267 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2268 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2270 in->b_title_demux = false;
2271 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2272 &in->title, &in->i_title,
2273 &in->i_title_offset, &in->i_seekpoint_offset ) )
2279 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2280 &in->b_can_pace_control );
2281 in->b_can_rate_control = in->b_can_pace_control;
2282 in->b_rescale_ts = true;
2284 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2286 var_SetBool( p_input, "can-pause", in->b_can_pause );
2287 access_Control( in->p_access, ACCESS_CAN_SEEK,
2289 var_Set( p_input, "seekable", val );
2292 input_ChangeState( p_input, BUFFERING_S );
2294 /* Create the stream_t */
2295 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2296 if( in->p_stream == NULL )
2298 msg_Warn( p_input, "cannot create a stream_t from access" );
2302 /* Open a demuxer */
2303 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2305 psz_demux = in->p_access->psz_demux;
2309 /* Take access redirections into account */
2310 char *psz_real_path;
2311 char *psz_buf = NULL;
2312 if( in->p_access->psz_path )
2314 const char *psz_a, *psz_d;
2315 psz_buf = strdup( in->p_access->psz_path );
2316 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2320 psz_real_path = psz_path;
2322 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2324 in->p_stream, p_input->p->p_es_out,
2325 p_input->b_preparsing );
2329 if( in->p_demux == NULL )
2331 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2332 psz_access, psz_demux, psz_path );
2333 intf_UserFatal( VLC_OBJECT( p_input ), false,
2334 _("VLC can't recognize the input's format"),
2335 _("The format of '%s' cannot be detected. "
2336 "Have a look the log for details."), psz_mrl );
2340 /* Get title from demux */
2341 if( !p_input->b_preparsing && in->i_title <= 0 )
2343 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2344 &in->title, &in->i_title,
2345 &in->i_title_offset, &in->i_seekpoint_offset ))
2347 TAB_INIT( in->i_title, in->title );
2351 in->b_title_demux = true;
2357 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2358 if( 1 || !p_input->b_preparsing )
2361 input_attachment_t **attachment;
2362 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2363 &attachment, &i_attachment ) )
2365 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2366 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2367 i_attachment, attachment );
2368 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2371 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2373 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2375 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2378 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2379 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2384 input_ChangeState( p_input, ERROR_S );
2387 demux_Delete( in->p_demux );
2390 stream_Delete( in->p_stream );
2393 access_Delete( in->p_access );
2395 return VLC_EGENERIC;
2398 /*****************************************************************************
2400 *****************************************************************************/
2401 static void InputSourceClean( input_source_t *in )
2406 demux_Delete( in->p_demux );
2409 stream_Delete( in->p_stream );
2412 access_Delete( in->p_access );
2414 if( in->i_title > 0 )
2416 for( i = 0; i < in->i_title; i++ )
2417 vlc_input_title_Delete( in->title[i] );
2418 TAB_CLEAN( in->i_title, in->title );
2422 static void SlaveDemux( input_thread_t *p_input )
2427 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2429 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2433 for( i = 0; i < p_input->p->i_slave; i++ )
2435 input_source_t *in = p_input->p->slave[i];
2441 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2446 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2448 msg_Err( p_input, "slave[%d] doesn't like "
2449 "DEMUX_GET_TIME -> EOF", i );
2454 if( i_stime >= i_time )
2457 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2463 i_ret = in->p_demux->pf_demux( in->p_demux );
2468 msg_Dbg( p_input, "slave %d EOF", i );
2474 static void SlaveSeek( input_thread_t *p_input )
2479 if( !p_input ) return;
2481 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2483 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2487 for( i = 0; i < p_input->p->i_slave; i++ )
2489 input_source_t *in = p_input->p->slave[i];
2491 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2493 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2499 /*****************************************************************************
2501 *****************************************************************************/
2502 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2506 if( !p_meta ) return;
2508 /* Get meta information from user */
2509 #define GET_META( field, s ) \
2510 var_Get( p_input, (s), &val ); \
2511 if( *val.psz_string ) \
2512 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2513 free( val.psz_string )
2515 GET_META( Title, "meta-title" );
2516 GET_META( Artist, "meta-artist" );
2517 GET_META( Genre, "meta-genre" );
2518 GET_META( Copyright, "meta-copyright" );
2519 GET_META( Description, "meta-description" );
2520 GET_META( Date, "meta-date" );
2521 GET_META( URL, "meta-url" );
2525 /*****************************************************************************
2526 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2527 * arturl and locking issue.
2528 *****************************************************************************/
2529 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2531 input_item_t *p_item = p_input->p->input.p_item;
2532 char * psz_arturl = NULL;
2533 char *psz_title = NULL;
2534 int i_arturl_event = false;
2539 psz_arturl = input_item_GetArtURL( p_item );
2541 vlc_mutex_lock( &p_item->lock );
2542 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2543 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2545 vlc_meta_Merge( p_item->p_meta, p_meta );
2547 if( psz_arturl && *psz_arturl )
2549 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2550 i_arturl_event = true;
2553 vlc_meta_Delete( p_meta );
2555 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2557 /* Don't look for art cover if sout
2558 * XXX It can change when sout has meta data support */
2559 if( p_input->p->p_sout && !p_input->b_preparsing )
2561 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2562 i_arturl_event = true;
2566 input_ExtractAttachmentAndCacheArt( p_input );
2572 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2574 p_meta = vlc_meta_New();
2575 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2577 vlc_mutex_unlock( &p_item->lock );
2579 input_item_SetPreparsed( p_item, true );
2581 if( i_arturl_event == true )
2585 /* Notify interested third parties */
2586 event.type = vlc_InputItemMetaChanged;
2587 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2588 vlc_event_send( &p_item->event_manager, &event );
2593 input_Control( p_input, INPUT_SET_NAME, psz_title );
2597 /** \todo handle sout meta */
2601 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2602 int i_new, input_attachment_t **pp_new )
2604 int i_attachment = *pi_attachment;
2605 input_attachment_t **attachment = *ppp_attachment;
2608 attachment = realloc( attachment,
2609 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2610 for( i = 0; i < i_new; i++ )
2611 attachment[i_attachment++] = pp_new[i];
2615 *pi_attachment = i_attachment;
2616 *ppp_attachment = attachment;
2619 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2623 if( p_input->b_preparsing )
2626 if( p_input->p->input.p_access )
2627 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2630 /* Get meta data from slave input */
2631 for( i = 0; i < p_input->p->i_slave; i++ )
2633 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2634 if( p_input->p->slave[i]->p_access )
2636 access_Control( p_input->p->slave[i]->p_access,
2637 ACCESS_GET_META, p_meta );
2642 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2649 /* XXX I am not sure it is a great idea, besides, there is more than that
2650 * if we want to do it right */
2651 vlc_mutex_lock( &p_item->lock );
2652 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2654 vlc_mutex_unlock( &p_item->lock );
2657 vlc_mutex_unlock( &p_item->lock );
2660 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2661 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2666 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2667 if(! p_demux->p_private )
2670 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2673 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2675 if( p_demux_meta->p_meta )
2677 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2678 vlc_meta_Delete( p_demux_meta->p_meta );
2681 if( p_demux_meta->i_attachments > 0 )
2683 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2684 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2685 p_demux_meta->i_attachments, p_demux_meta->attachments );
2686 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2688 module_Unneed( p_demux, p_id3 );
2690 free( p_demux->p_private );
2694 /*****************************************************************************
2695 * MRLSplit: parse the access, demux and url part of the
2696 * Media Resource Locator.
2697 *****************************************************************************/
2698 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2701 char *psz_access = NULL;
2702 char *psz_demux = NULL;
2705 /* Either there is an access/demux specification before ://
2706 * or we have a plain local file path. */
2707 psz_path = strstr( psz_dup, "://" );
2708 if( psz_path != NULL )
2711 psz_path += 3; /* skips "://" */
2713 /* Separate access from demux (<access>/<demux>://<path>) */
2714 psz_access = psz_dup;
2715 psz_demux = strchr( psz_access, '/' );
2717 *psz_demux++ = '\0';
2719 /* We really don't want module name substitution here! */
2720 if( psz_access[0] == '$' )
2722 if( psz_demux && psz_demux[0] == '$' )
2730 *ppsz_access = psz_access ? psz_access : "";
2731 *ppsz_demux = psz_demux ? psz_demux : "";
2732 *ppsz_path = psz_path ? psz_path : "";
2735 /*****************************************************************************
2736 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2739 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2740 *****************************************************************************/
2741 static void MRLSections( input_thread_t *p_input, char *psz_source,
2742 int *pi_title_start, int *pi_title_end,
2743 int *pi_chapter_start, int *pi_chapter_end )
2745 char *psz, *psz_end, *psz_next, *psz_check;
2747 *pi_title_start = *pi_title_end = -1;
2748 *pi_chapter_start = *pi_chapter_end = -1;
2750 /* Start by parsing titles and chapters */
2751 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2753 /* Check we are really dealing with a title/chapter section */
2754 psz_check = psz + 1;
2755 if( !*psz_check ) return;
2756 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2757 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2758 if( *psz_check == ':' && ++psz_check )
2759 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2760 if( *psz_check != '-' && *psz_check ) return;
2761 if( *psz_check == '-' && ++psz_check )
2762 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2763 if( *psz_check != ':' && *psz_check ) return;
2764 if( *psz_check == ':' && ++psz_check )
2765 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2766 if( *psz_check ) return;
2768 /* Separate start and end */
2770 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2772 /* Look for the start title */
2773 *pi_title_start = strtol( psz, &psz_next, 0 );
2774 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2775 *pi_title_end = *pi_title_start;
2778 /* Look for the start chapter */
2780 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2781 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2782 *pi_chapter_end = *pi_chapter_start;
2786 /* Look for the end title */
2787 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2788 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2791 /* Look for the end chapter */
2792 if( *psz_end ) psz_end++;
2793 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2794 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2797 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2798 psz_source, *pi_title_start, *pi_chapter_start,
2799 *pi_title_end, *pi_chapter_end );
2802 /*****************************************************************************
2803 * input_AddSubtitles: add a subtitles file and enable it
2804 *****************************************************************************/
2805 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2806 bool b_check_extension )
2808 input_source_t *sub;
2811 char *psz_path, *psz_extension;
2813 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2818 /* if we are provided a subtitle.sub file,
2819 * see if we don't have a subtitle.idx and use it instead */
2820 psz_path = strdup( psz_subtitle );
2823 psz_extension = strrchr( psz_path, '.');
2824 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2828 strcpy( psz_extension, ".idx" );
2829 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2830 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2833 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2834 psz_path, psz_subtitle );
2835 strcpy( psz_subtitle, psz_path );
2841 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2843 sub = InputSourceNew( p_input );
2844 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2846 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2849 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2851 if( count.i_int == 0 )
2853 /* if it was first one, there is disable too */
2855 if( count.i_int < list.p_list->i_count )
2857 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2858 &list.p_list->p_values[count.i_int] );
2860 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );