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 *****************************************************************************/
33 #include "input_internal.h"
36 #include "../stream_output/stream_output.h"
38 #include <vlc_playlist.h>
39 #include <vlc_interface.h>
41 #include <vlc_demux.h>
42 #include <vlc_charset.h>
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Run ( input_thread_t *p_input );
52 static int RunAndDestroy ( input_thread_t *p_input );
54 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
55 const char *, vlc_bool_t, sout_instance_t * );
56 static int Init ( input_thread_t *p_input );
57 static void Error ( input_thread_t *p_input );
58 static void End ( input_thread_t *p_input );
59 static void MainLoop( input_thread_t *p_input );
60 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout );
62 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
63 static void ControlReduce( input_thread_t * );
64 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
66 static int UpdateFromAccess( input_thread_t * );
67 static int UpdateFromDemux( input_thread_t * );
69 static void UpdateItemLength( input_thread_t *, int64_t i_length );
71 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
73 static input_source_t *InputSourceNew( input_thread_t *);
74 static int InputSourceInit( input_thread_t *, input_source_t *,
75 const char *, const char *psz_forced_demux );
76 static void InputSourceClean( input_source_t * );
78 //static void InputGetAttachments( input_thread_t *, input_source_t * );
80 static void SlaveDemux( input_thread_t *p_input );
81 static void SlaveSeek( input_thread_t *p_input );
83 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
84 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
86 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t * );
87 static void SoutKeep( sout_instance_t * );
89 /*****************************************************************************
90 * This function creates a new input, and returns a pointer
91 * to its description. On error, it returns NULL.
93 * Variables for _public_ use:
96 * - rate,rate-slower, rate-faster
97 * - position, position-offset
99 * - title,title-next,title-prev
100 * - chapter,chapter-next, chapter-prev
101 * - program, audio-es, video-es, spu-es
102 * - audio-delay, spu-delay
107 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
108 * * For intf callback upon changes
110 * TODO explain when Callback is called
111 * TODO complete this list (?)
112 *****************************************************************************/
113 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
114 const char *psz_header, vlc_bool_t b_quick, sout_instance_t *p_sout )
116 input_thread_t *p_input = NULL; /* thread descriptor */
120 /* Allocate descriptor */
121 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
122 if( p_input == NULL )
124 msg_Err( p_parent, "out of memory" );
127 MALLOC_NULL( p_input->p, input_thread_private_t );
129 /* One "randomly" selected input thread is responsible for computing
130 * the global stats. Check if there is already someone doing this */
131 if( p_input->p_libvlc->p_playlist->p_stats && !b_quick )
133 vlc_mutex_lock( &p_input->p_libvlc->p_playlist->p_stats->lock );
134 if( p_input->p_libvlc->p_playlist->p_stats_computer == NULL )
136 p_input->p_libvlc->p_playlist->p_stats_computer = p_input;
138 vlc_mutex_unlock( &p_input->p_libvlc->p_playlist->p_stats->lock );
141 p_input->b_preparsing = b_quick;
142 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
144 /* Init Common fields */
145 p_input->b_eof = VLC_FALSE;
146 p_input->b_can_pace_control = VLC_TRUE;
147 p_input->p->i_start = 0;
149 p_input->p->i_stop = 0;
150 p_input->p->i_title = 0;
151 p_input->p->title = NULL;
152 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
153 p_input->i_state = INIT_S;
154 p_input->p->i_rate = INPUT_RATE_DEFAULT;
155 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
156 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
157 p_input->p->p_es_out = NULL;
158 p_input->p->p_sout = NULL;
159 p_input->p->b_sout_keep = VLC_FALSE;
160 p_input->p->b_out_pace_control = VLC_FALSE;
161 p_input->i_pts_delay = 0;
163 /* Init Input fields */
164 p_input->p->input.p_item = p_item;
165 p_input->p->input.p_access = NULL;
166 p_input->p->input.p_stream = NULL;
167 p_input->p->input.p_demux = NULL;
168 p_input->p->input.b_title_demux = VLC_FALSE;
169 p_input->p->input.i_title = 0;
170 p_input->p->input.title = NULL;
171 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
172 p_input->p->input.b_can_pace_control = VLC_TRUE;
173 p_input->p->input.b_eof = VLC_FALSE;
174 p_input->p->input.i_cr_average = 0;
176 vlc_mutex_lock( &p_item->lock );
177 if( !p_input->p->input.p_item->p_meta )
178 p_input->p->input.p_item->p_meta = vlc_meta_New();
180 if( !p_item->p_stats )
181 p_item->p_stats = stats_NewInputStats( p_input );
182 vlc_mutex_unlock( &p_item->lock );
185 p_input->p->i_slave = 0;
186 p_input->p->slave = NULL;
188 /* Init control buffer */
189 vlc_mutex_init( p_input, &p_input->p->lock_control );
190 p_input->p->i_control = 0;
192 /* Parse input options */
193 vlc_mutex_lock( &p_item->lock );
194 for( i = 0; i < p_item->i_options; i++ )
195 var_OptionParse( p_input, p_item->ppsz_options[i] );
196 vlc_mutex_unlock( &p_item->lock );
198 /* Create Object Variables for private use only */
199 input_ConfigVarInit( p_input );
201 /* Create Objects variables for public Get and Set */
202 input_ControlVarInit( p_input );
204 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
206 if( !p_input->b_preparsing )
208 var_Get( p_input, "bookmarks", &val );
211 /* FIXME: have a common cfg parsing routine used by sout and others */
212 char *psz_parser, *psz_start, *psz_end;
213 psz_parser = val.psz_string;
214 while( (psz_start = strchr( psz_parser, '{' ) ) )
216 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
219 psz_end = strchr( psz_start, '}' );
220 if( !psz_end ) break;
221 psz_parser = psz_end + 1;
222 backup = *psz_parser;
225 while( (psz_end = strchr( psz_start, ',' ) ) )
228 if( !strncmp( psz_start, "name=", 5 ) )
230 p_seekpoint->psz_name = psz_start + 5;
232 else if( !strncmp( psz_start, "bytes=", 6 ) )
234 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
236 else if( !strncmp( psz_start, "time=", 5 ) )
238 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
241 psz_start = psz_end + 1;
243 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
244 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
245 p_seekpoint->i_time_offset );
246 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
247 vlc_seekpoint_Delete( p_seekpoint );
248 *psz_parser = backup;
250 free( val.psz_string );
254 /* Remove 'Now playing' info as it is probably outdated */
255 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT), VLC_META_NOW_PLAYING );
256 vlc_mutex_lock( &p_item->lock );
257 vlc_meta_SetNowPlaying( p_item->p_meta, NULL );
258 vlc_mutex_unlock( &p_item->lock );
261 if( p_input->b_preparsing )
262 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
266 p_input->p->p_sout = p_sout;
268 /* Attach only once we are ready */
269 vlc_object_attach( p_input, p_parent );
274 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout )
276 vlc_object_detach( p_input );
280 if( p_input->p->p_sout )
283 *pp_sout = p_input->p->p_sout;
284 else if( p_input->p->b_sout_keep )
285 SoutKeep( p_input->p->p_sout );
287 sout_DeleteInstance( p_input->p->p_sout );
290 vlc_mutex_destroy( &p_input->p->lock_control );
293 vlc_object_destroy( p_input );
297 * Initialize an input thread and run it. You will need to monitor the
298 * thread to clean up after it is done
300 * \param p_parent a vlc_object
301 * \param p_item an input item
302 * \return a pointer to the spawned input thread
304 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
305 input_item_t *p_item )
307 vlc_bool_t b_sout_keep;
308 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
309 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
311 if( !p_input && p_sout )
314 p_input->p->b_sout_keep = b_sout_keep;
319 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
320 input_item_t *p_item,
321 const char *psz_log, sout_instance_t *p_sout )
323 input_thread_t *p_input;
325 p_input = Create( p_parent, p_item, psz_log, VLC_FALSE, p_sout );
329 /* Create thread and wait for its readiness. */
330 if( vlc_thread_create( p_input, "input", Run,
331 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
333 input_ChangeState( p_input, ERROR_S );
334 msg_Err( p_input, "cannot create input thread" );
335 Destroy( p_input, &p_sout );
343 * Initialize an input thread and run it. This thread will clean after himself,
344 * you can forget about it. It can work either in blocking or non-blocking mode
346 * \param p_parent a vlc_object
347 * \param p_item an input item
348 * \param b_block should we block until read is finished ?
349 * \return the input object id if non blocking, an error code else
351 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
354 vlc_bool_t b_sout_keep;
355 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
356 input_thread_t *p_input;
358 p_input = Create( p_parent, p_item, NULL, VLC_FALSE, p_sout );
359 if( !p_input && p_sout )
364 p_input->p->b_sout_keep = b_sout_keep;
368 RunAndDestroy( p_input );
373 if( vlc_thread_create( p_input, "input", RunAndDestroy,
374 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
376 input_ChangeState( p_input, ERROR_S );
377 msg_Err( p_input, "cannot create input thread" );
378 Destroy( p_input, NULL );
382 return p_input->i_object_id;
386 * Initialize an input and initialize it to preparse the item
387 * This function is blocking. It will only accept to parse files
389 * \param p_parent a vlc_object_t
390 * \param p_item an input item
391 * \return VLC_SUCCESS or an error
393 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
395 input_thread_t *p_input;
397 /* Allocate descriptor */
398 p_input = Create( p_parent, p_item, NULL, VLC_TRUE, NULL );
402 if( !Init( p_input ) )
405 Destroy( p_input, NULL );
411 * Request a running input thread to stop and die
413 * \param the input thread to stop
415 void input_StopThread( input_thread_t *p_input )
420 /* Set die for input */
421 vlc_object_kill( p_input );
422 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
424 /* We cannot touch p_input fields directly (we come from another thread),
425 * so use the vlc_object_find way, it's perfectly safe */
427 /* Set die for all access */
428 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
429 for( i = 0; i < p_list->i_count; i++ )
431 vlc_object_kill( p_list->p_values[i].p_object );
433 vlc_list_release( p_list );
435 /* Set die for all stream */
436 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
437 for( i = 0; i < p_list->i_count; i++ )
439 vlc_object_kill( p_list->p_values[i].p_object );
441 vlc_list_release( p_list );
443 /* Set die for all demux */
444 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
445 for( i = 0; i < p_list->i_count; i++ )
447 vlc_object_kill( p_list->p_values[i].p_object );
449 vlc_list_release( p_list );
451 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
455 * Clean up a dead input thread
456 * This function does not return until the thread is effectively cancelled.
458 * \param the input thread to kill
460 void input_DestroyThread( input_thread_t *p_input )
462 input_DestroyThreadExtended( p_input, NULL );
465 void input_DestroyThreadExtended( input_thread_t *p_input, sout_instance_t **pp_sout )
467 /* Join the thread */
468 vlc_thread_join( p_input );
471 Destroy( p_input, pp_sout );
474 /*****************************************************************************
475 * Run: main thread loop
476 * This is the "normal" thread that spawns the input processing chain,
477 * reads the stream, cleans up and waits
478 *****************************************************************************/
479 static int Run( input_thread_t *p_input )
481 /* Signal that the thread is launched */
482 vlc_thread_ready( p_input );
484 if( Init( p_input ) )
486 /* If we failed, wait before we are killed, and exit */
487 p_input->b_error = VLC_TRUE;
488 playlist_Signal( pl_Get( p_input ) );
492 /* Tell we're dead */
493 p_input->b_dead = VLC_TRUE;
500 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
502 /* We have finish to demux data but not to play them */
503 while( !p_input->b_die )
505 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
508 msg_Dbg( p_input, "waiting decoder fifos to empty" );
510 msleep( INPUT_IDLE_SLEEP );
513 /* We have finished */
514 p_input->b_eof = VLC_TRUE;
515 playlist_Signal( pl_Get( p_input ) );
518 /* Wait until we are asked to die */
519 if( !p_input->b_die )
530 /*****************************************************************************
531 * RunAndDestroy: main thread loop
532 * This is the "just forget me" thread that spawns the input processing chain,
533 * reads the stream, cleans up and releases memory
534 *****************************************************************************/
535 static int RunAndDestroy( input_thread_t *p_input )
537 /* Signal that the thread is launched */
538 vlc_thread_ready( p_input );
540 if( Init( p_input ) )
545 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
547 /* We have finished demuxing data but not playing it */
548 while( !p_input->b_die )
550 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
553 msg_Dbg( p_input, "waiting decoder fifos to empty" );
555 msleep( INPUT_IDLE_SLEEP );
558 /* We have finished */
559 p_input->b_eof = VLC_TRUE;
567 Destroy( p_input, NULL );
571 /*****************************************************************************
572 * Main loop: Fill buffers from access, and demux
573 *****************************************************************************/
574 static void MainLoop( input_thread_t *p_input )
576 int64_t i_intf_update = 0;
579 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
581 vlc_bool_t b_force_update = VLC_FALSE;
587 if( p_input->i_state != PAUSE_S )
589 if( p_input->p->i_stop <= 0 || p_input->i_time < p_input->p->i_stop )
590 i_ret=p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
597 if( p_input->p->input.b_title_demux &&
598 p_input->p->input.p_demux->info.i_update )
600 i_ret = UpdateFromDemux( p_input );
601 b_force_update = VLC_TRUE;
603 else if( !p_input->p->input.b_title_demux &&
604 p_input->p->input.p_access &&
605 p_input->p->input.p_access->info.i_update )
607 i_ret = UpdateFromAccess( p_input );
608 b_force_update = VLC_TRUE;
612 if( i_ret == 0 ) /* EOF */
616 var_Get( p_input, "input-repeat", &repeat );
617 if( repeat.i_int == 0 )
619 /* End of file - we do not set b_die because only the
620 * playlist is allowed to do so. */
621 input_ChangeState( p_input, END_S );
622 msg_Dbg( p_input, "EOF reached" );
623 p_input->p->input.b_eof = VLC_TRUE;
627 msg_Dbg( p_input, "repeating the same input (%d)",
629 if( repeat.i_int > 0 )
632 var_Set( p_input, "input-repeat", repeat );
635 /* Seek to start title/seekpoint */
636 val.i_int = p_input->p->input.i_title_start -
637 p_input->p->input.i_title_offset;
638 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
640 input_ControlPush( p_input,
641 INPUT_CONTROL_SET_TITLE, &val );
643 val.i_int = p_input->p->input.i_seekpoint_start -
644 p_input->p->input.i_seekpoint_offset;
645 if( val.i_int > 0 /* TODO: check upper boundary */ )
646 input_ControlPush( p_input,
647 INPUT_CONTROL_SET_SEEKPOINT, &val );
649 /* Seek to start position */
650 if( p_input->p->i_start > 0 )
652 val.i_time = p_input->p->i_start;
653 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
659 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
666 p_input->b_error = VLC_TRUE;
669 if( i_ret > 0 && p_input->p->i_slave > 0 )
671 SlaveDemux( p_input );
681 vlc_mutex_lock( &p_input->p->lock_control );
682 ControlReduce( p_input );
683 while( !ControlPopNoLock( p_input, &i_type, &val ) )
685 msg_Dbg( p_input, "control type=%d", i_type );
686 if( Control( p_input, i_type, val ) )
687 b_force_update = VLC_TRUE;
689 vlc_mutex_unlock( &p_input->p->lock_control );
691 if( b_force_update || i_intf_update < mdate() )
695 int64_t i_time, i_length;
696 /* update input status variables */
697 if( !demux2_Control( p_input->p->input.p_demux,
698 DEMUX_GET_POSITION, &f_pos ) )
700 val.f_float = (float)f_pos;
701 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
703 if( !demux2_Control( p_input->p->input.p_demux,
704 DEMUX_GET_TIME, &i_time ) )
706 p_input->i_time = i_time;
708 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
710 if( !demux2_Control( p_input->p->input.p_demux,
711 DEMUX_GET_LENGTH, &i_length ) )
714 var_Get( p_input, "length", &old_val );
715 val.i_time = i_length;
716 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
718 if( old_val.i_time != val.i_time )
720 UpdateItemLength( p_input, i_length );
724 var_SetBool( p_input, "intf-change", VLC_TRUE );
725 i_intf_update = mdate() + I64C(150000);
727 /* 150ms * 8 = ~ 1 second */
728 if( ++i_updates % 8 == 0 )
730 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
731 /* Are we the thread responsible for computing global stats ? */
732 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
734 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
735 p_input->p_libvlc->p_playlist->p_stats );
741 static int Init( input_thread_t * p_input )
751 /* Initialize optional stream output. (before access/demuxer)
752 * XXX: we add a special case if the uri starts by vlc.
753 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
754 * be destroyed in case of a file).
755 * (this will break playing of file starting by 'vlc:' but I don't
756 * want to add more logic, just force file by file:// or code it ;)
758 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
759 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
761 if( !p_input->b_preparsing )
763 /* Prepare statistics */
764 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
765 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
766 if( p_input->p_libvlc->b_stats )
768 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
769 INIT_COUNTER( read_packets, INTEGER, COUNTER );
770 INIT_COUNTER( demux_read, INTEGER, COUNTER );
771 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
772 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
773 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
774 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
775 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
776 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
777 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
778 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
779 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
780 p_input->p->counters.p_sout_send_bitrate = NULL;
781 p_input->p->counters.p_sout_sent_packets = NULL;
782 p_input->p->counters.p_sout_sent_bytes = NULL;
783 if( p_input->p->counters.p_demux_bitrate )
784 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
785 if( p_input->p->counters.p_input_bitrate )
786 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
789 /* Find a usable sout and attach it to p_input */
790 psz = var_GetString( p_input, "sout" );
791 if( *psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
793 /* Check the validity of the provided sout */
794 if( p_input->p->p_sout )
796 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
798 msg_Dbg( p_input, "destroying unusable sout" );
800 sout_DeleteInstance( p_input->p->p_sout );
801 p_input->p->p_sout = NULL;
805 if( p_input->p->p_sout )
808 msg_Dbg( p_input, "sout keep: reusing sout" );
809 msg_Dbg( p_input, "sout keep: you probably want to use "
810 "gather stream_out" );
811 vlc_object_attach( p_input->p->p_sout, p_input );
815 /* Create a new one */
816 p_input->p->p_sout = sout_NewInstance( p_input, psz );
818 if( !p_input->p->p_sout )
820 input_ChangeState( p_input, ERROR_S );
821 msg_Err( p_input, "cannot start stream output instance, " \
828 if( p_input->p_libvlc->b_stats )
830 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
831 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
832 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
833 if( p_input->p->counters.p_sout_send_bitrate )
834 p_input->p->counters.p_sout_send_bitrate->update_interval =
838 else if( p_input->p->p_sout )
840 msg_Dbg( p_input, "destroying useless sout" );
842 sout_DeleteInstance( p_input->p->p_sout );
843 p_input->p->p_sout = NULL;
849 p_input->p->p_es_out = input_EsOutNew( p_input );
850 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
851 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
853 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
854 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
856 if( InputSourceInit( p_input, &p_input->p->input,
857 p_input->p->input.p_item->psz_uri, NULL ) )
862 /* Create global title (from master) */
863 if( !p_input->b_preparsing )
865 p_input->p->i_title = p_input->p->input.i_title;
866 p_input->p->title = p_input->p->input.title;
867 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
868 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
869 if( p_input->p->i_title > 0 )
871 /* Setup variables */
872 input_ControlVarNavigation( p_input );
873 input_ControlVarTitle( p_input, 0 );
877 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
878 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
881 if( p_input->i_pts_delay < 0 )
882 p_input->i_pts_delay = 0;
884 /* If the desynchronisation requested by the user is < 0, we need to
885 * cache more data. */
886 var_Get( p_input, "audio-desync", &val );
887 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
889 /* Update cr_average depending on the caching */
890 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
891 p_input->p->input.i_cr_average /= 10;
892 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
895 /* Load master infos */
897 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
898 &val.i_time ) && val.i_time > 0 )
900 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
901 UpdateItemLength( p_input, val.i_time );
904 /* Start title/chapter */
905 if( !p_input->b_preparsing )
907 val.i_int = p_input->p->input.i_title_start -
908 p_input->p->input.i_title_offset;
909 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
910 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
911 val.i_int = p_input->p->input.i_seekpoint_start -
912 p_input->p->input.i_seekpoint_offset;
913 if( val.i_int > 0 /* TODO: check upper boundary */ )
914 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
918 p_input->p->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
920 p_input->p->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
923 if( p_input->p->i_start > 0 )
925 if( p_input->p->i_start >= val.i_time )
927 msg_Warn( p_input, "invalid start-time ignored" );
933 msg_Dbg( p_input, "starting at time: %ds",
934 (int)( p_input->p->i_start / I64C(1000000) ) );
936 s.i_time = p_input->p->i_start;
937 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
940 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
942 msg_Warn( p_input, "invalid stop-time ignored" );
943 p_input->p->i_stop = 0;
947 /* Get fps and set it if not already set */
948 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
951 float f_requested_fps;
953 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
954 var_SetFloat( p_input, "sub-original-fps", f_fps );
956 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
957 if( f_requested_fps != f_fps )
959 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
961 var_SetFloat( p_input, "sub-fps", f_requested_fps );
965 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
968 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
971 /* Look for and add subtitle files */
972 psz_subtitle = var_GetString( p_input, "sub-file" );
975 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
976 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
979 var_Get( p_input, "sub-autodetect-file", &val );
982 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
983 char **subs = subtitles_Detect( p_input, psz_autopath,
984 p_input->p->input.p_item->psz_uri );
988 /* Try to autoselect the first autodetected subtitles file
989 * if no subtitles file was specified */
990 if( *psz_subtitle == 0 && subs && subs[0] )
992 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
997 /* Then, just add the following subtitles files */
998 for( ; subs && subs[i]; i++ )
1000 if( strcmp( psz_subtitle, subs[i] ) )
1002 sub = InputSourceNew( p_input );
1003 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1005 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1011 if( subs ) free( subs );
1012 if( psz_autopath ) free( psz_autopath );
1014 free( psz_subtitle );
1016 /* Look for slave */
1017 psz = var_GetString( p_input, "input-slave" );
1021 input_source_t *slave;
1022 while( psz && *psz )
1024 while( *psz == ' ' || *psz == '#' )
1028 if( ( psz_delim = strchr( psz, '#' ) ) )
1030 *psz_delim++ = '\0';
1037 msg_Dbg( p_input, "adding slave input '%s'", psz );
1038 slave = InputSourceNew( p_input );
1039 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1041 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1047 if( psz ) free( psz );
1051 p_input->p->i_start = 0;
1052 p_input->p->i_start = 0;
1056 if( !p_input->b_preparsing )
1058 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1059 i_es_out_mode = ES_OUT_MODE_AUTO;
1061 if( p_input->p->p_sout )
1063 var_Get( p_input, "sout-all", &val );
1066 i_es_out_mode = ES_OUT_MODE_ALL;
1071 var_Get( p_input, "programs", &val );
1072 if ( val.p_list && val.p_list->i_count )
1074 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1075 /* Note : we should remove the "program" callback. */
1078 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1082 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1084 /* Inform the demuxer about waited group (needed only for DVB) */
1085 if( i_es_out_mode == ES_OUT_MODE_ALL )
1087 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1089 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1091 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1096 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1097 (int) var_GetInteger( p_input, "program" ), NULL );
1100 if( p_input->p->p_sout )
1102 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1104 p_input->p->b_out_pace_control = VLC_FALSE;
1108 p_input->p->b_out_pace_control = VLC_TRUE;
1111 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1113 /* We don't want a high input priority here or we'll
1114 * end-up sucking up all the CPU time */
1115 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1118 msg_Dbg( p_input, "starting in %s mode",
1119 p_input->p->b_out_pace_control ? "async" : "sync" );
1123 p_meta = vlc_meta_New();
1124 /* Get meta data from users */
1125 InputMetaUser( p_input, p_meta );
1127 /* Get meta data from master input */
1128 demux2_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
1130 /* Access_file does not give any meta, and there are no slave */
1131 if( !p_input->b_preparsing )
1133 if( p_input->p->input.p_access )
1134 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1137 /* Get meta data from slave input */
1138 for( i = 0; i < p_input->p->i_slave; i++ )
1140 demux2_Control( p_input->p->slave[i]->p_demux,
1141 DEMUX_GET_META, p_meta );
1142 if( p_input->p->slave[i]->p_access )
1144 access2_Control( p_input->p->slave[i]->p_access,
1145 ACCESS_GET_META, p_meta );
1150 InputUpdateMeta( p_input, p_meta );
1152 if( !p_input->b_preparsing )
1154 msg_Dbg( p_input, "`%s' successfully opened",
1155 p_input->p->input.p_item->psz_uri );
1159 /* initialization is complete */
1160 input_ChangeState( p_input, PLAYING_S );
1165 input_ChangeState( p_input, ERROR_S );
1167 if( p_input->p->p_es_out )
1168 input_EsOutDelete( p_input->p->p_es_out );
1170 if( p_input->p->p_sout )
1172 vlc_object_detach( p_input->p->p_sout );
1173 sout_DeleteInstance( p_input->p->p_sout );
1177 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1179 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1180 stats_CounterClean( p_input->p->counters.p_##c );\
1181 p_input->p->counters.p_##c = NULL; } while(0)
1182 EXIT_COUNTER( read_bytes );
1183 EXIT_COUNTER( read_packets );
1184 EXIT_COUNTER( demux_read );
1185 EXIT_COUNTER( input_bitrate );
1186 EXIT_COUNTER( demux_bitrate );
1187 EXIT_COUNTER( played_abuffers );
1188 EXIT_COUNTER( lost_abuffers );
1189 EXIT_COUNTER( displayed_pictures );
1190 EXIT_COUNTER( lost_pictures );
1191 EXIT_COUNTER( decoded_audio );
1192 EXIT_COUNTER( decoded_video );
1193 EXIT_COUNTER( decoded_sub );
1195 if( p_input->p->p_sout )
1197 EXIT_COUNTER( sout_sent_packets );
1198 EXIT_COUNTER (sout_sent_bytes );
1199 EXIT_COUNTER( sout_send_bitrate );
1204 /* Mark them deleted */
1205 p_input->p->input.p_demux = NULL;
1206 p_input->p->input.p_stream = NULL;
1207 p_input->p->input.p_access = NULL;
1208 p_input->p->p_es_out = NULL;
1209 p_input->p->p_sout = NULL;
1211 return VLC_EGENERIC;
1214 /*****************************************************************************
1215 * Error: RunThread() error loop
1216 *****************************************************************************
1217 * This function is called when an error occurred during thread main's loop.
1218 *****************************************************************************/
1219 static void Error( input_thread_t *p_input )
1221 while( !p_input->b_die )
1224 input_ChangeState( p_input, ERROR_S );
1225 msleep( INPUT_IDLE_SLEEP );
1229 /*****************************************************************************
1230 * End: end the input thread
1231 *****************************************************************************/
1232 static void End( input_thread_t * p_input )
1236 /* We are at the end */
1237 input_ChangeState( p_input, END_S );
1239 /* Clean control variables */
1240 input_ControlVarClean( p_input );
1242 /* Clean up master */
1243 InputSourceClean( &p_input->p->input );
1246 for( i = 0; i < p_input->p->i_slave; i++ )
1248 InputSourceClean( p_input->p->slave[i] );
1249 free( p_input->p->slave[i] );
1251 if( p_input->p->slave ) free( p_input->p->slave );
1253 /* Unload all modules */
1254 if( p_input->p->p_es_out )
1255 input_EsOutDelete( p_input->p->p_es_out );
1257 if( !p_input->b_preparsing )
1259 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1260 if( p_input->p_libvlc->b_stats )
1262 /* make sure we are up to date */
1263 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1264 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1266 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1267 p_input->p_libvlc->p_playlist->p_stats );
1268 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1270 CL_CO( read_bytes );
1271 CL_CO( read_packets );
1272 CL_CO( demux_read );
1273 CL_CO( input_bitrate );
1274 CL_CO( demux_bitrate );
1275 CL_CO( played_abuffers );
1276 CL_CO( lost_abuffers );
1277 CL_CO( displayed_pictures );
1278 CL_CO( lost_pictures );
1279 CL_CO( decoded_audio) ;
1280 CL_CO( decoded_video );
1281 CL_CO( decoded_sub) ;
1284 /* Close optional stream output instance */
1285 if( p_input->p->p_sout )
1287 CL_CO( sout_sent_packets );
1288 CL_CO( sout_sent_bytes );
1289 CL_CO( sout_send_bitrate );
1291 vlc_object_detach( p_input->p->p_sout );
1296 if( p_input->p->i_attachment > 0 )
1298 for( i = 0; i < p_input->p->i_attachment; i++ )
1299 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1300 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1303 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1305 /* Tell we're dead */
1306 p_input->b_dead = VLC_TRUE;
1309 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t *pb_sout_keep )
1311 vlc_bool_t b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1312 sout_instance_t *p_sout = NULL;
1315 /* Search sout-keep options
1316 * XXX it has to be done here, but it is duplicated work :( */
1317 vlc_mutex_lock( &p_item->lock );
1318 for( i = 0; i < p_item->i_options; i++ )
1320 const char *psz_option = p_item->ppsz_options[i];
1323 if( *psz_option == ':' )
1326 if( !strcmp( psz_option, "sout-keep" ) )
1327 b_keep_sout = VLC_TRUE;
1328 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1329 b_keep_sout = VLC_FALSE;
1331 vlc_mutex_unlock( &p_item->lock );
1333 /* Find a potential sout to reuse
1334 * XXX it might be unusable but this will be checked later */
1337 /* Remove the sout from the playlist garbage collector */
1338 playlist_t *p_playlist = pl_Yield( p_parent );
1340 vlc_mutex_lock( &p_playlist->gc_lock );
1341 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1344 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1346 vlc_object_release( p_sout );
1351 vlc_object_detach( p_sout ); /* Remove it from the GC */
1353 vlc_object_release( p_sout );
1356 vlc_mutex_unlock( &p_playlist->gc_lock );
1358 pl_Release( p_parent );
1362 *pb_sout_keep = b_keep_sout;
1366 static void SoutKeep( sout_instance_t *p_sout )
1368 /* attach sout to the playlist */
1369 playlist_t *p_playlist = pl_Yield( p_sout );
1371 msg_Dbg( p_sout, "sout has been kept" );
1372 vlc_object_attach( p_sout, p_playlist );
1374 pl_Release( p_sout );
1377 /*****************************************************************************
1379 *****************************************************************************/
1380 static inline int ControlPopNoLock( input_thread_t *p_input,
1381 int *pi_type, vlc_value_t *p_val )
1383 if( p_input->p->i_control <= 0 )
1385 return VLC_EGENERIC;
1388 *pi_type = p_input->p->control[0].i_type;
1389 *p_val = p_input->p->control[0].val;
1391 p_input->p->i_control--;
1392 if( p_input->p->i_control > 0 )
1396 for( i = 0; i < p_input->p->i_control; i++ )
1398 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1399 p_input->p->control[i].val = p_input->p->control[i+1].val;
1406 static void ControlReduce( input_thread_t *p_input )
1413 for( i = 1; i < p_input->p->i_control; i++ )
1415 const int i_lt = p_input->p->control[i-1].i_type;
1416 const int i_ct = p_input->p->control[i].i_type;
1418 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1419 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1423 ( i_ct == INPUT_CONTROL_SET_STATE ||
1424 i_ct == INPUT_CONTROL_SET_RATE ||
1425 i_ct == INPUT_CONTROL_SET_POSITION ||
1426 i_ct == INPUT_CONTROL_SET_TIME ||
1427 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1428 i_ct == INPUT_CONTROL_SET_TITLE ||
1429 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1430 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1433 // msg_Dbg( p_input, "merged at %d", i );
1434 /* Remove the i-1 */
1435 for( j = i; j < p_input->p->i_control; j++ )
1436 p_input->p->control[j-1] = p_input->p->control[j];
1437 p_input->p->i_control--;
1441 /* TODO but that's not that important
1442 - merge SET_X with SET_X_CMD
1443 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1444 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1451 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1454 vlc_bool_t b_force_update = VLC_FALSE;
1456 if( !p_input ) return b_force_update;
1460 case INPUT_CONTROL_SET_DIE:
1461 msg_Dbg( p_input, "control: stopping input" );
1462 /* Mark all submodules to die */
1463 if( p_input->p->input.p_access )
1464 vlc_object_kill( p_input->p->input.p_access );
1465 if( p_input->p->input.p_stream )
1466 vlc_object_kill( p_input->p->input.p_stream );
1467 vlc_object_kill( p_input->p->input.p_demux );
1469 vlc_object_kill( p_input );
1472 case INPUT_CONTROL_SET_POSITION:
1473 case INPUT_CONTROL_SET_POSITION_OFFSET:
1476 if( i_type == INPUT_CONTROL_SET_POSITION )
1478 f_pos = val.f_float;
1482 /* Should not fail */
1483 demux2_Control( p_input->p->input.p_demux,
1484 DEMUX_GET_POSITION, &f_pos );
1485 f_pos += val.f_float;
1487 if( f_pos < 0.0 ) f_pos = 0.0;
1488 if( f_pos > 1.0 ) f_pos = 1.0;
1489 /* Reset the decoders states and clock sync (before calling the demuxer */
1490 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1491 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1492 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1495 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1496 "%2.1f%% failed", f_pos * 100 );
1500 if( p_input->p->i_slave > 0 )
1501 SlaveSeek( p_input );
1503 b_force_update = VLC_TRUE;
1508 case INPUT_CONTROL_SET_TIME:
1509 case INPUT_CONTROL_SET_TIME_OFFSET:
1514 if( i_type == INPUT_CONTROL_SET_TIME )
1516 i_time = val.i_time;
1520 /* Should not fail */
1521 demux2_Control( p_input->p->input.p_demux,
1522 DEMUX_GET_TIME, &i_time );
1523 i_time += val.i_time;
1525 if( i_time < 0 ) i_time = 0;
1527 /* Reset the decoders states and clock sync (before calling the demuxer */
1528 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1529 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1531 i_ret = demux2_Control( p_input->p->input.p_demux,
1532 DEMUX_SET_TIME, i_time );
1537 /* Emulate it with a SET_POS */
1538 demux2_Control( p_input->p->input.p_demux,
1539 DEMUX_GET_LENGTH, &i_length );
1542 double f_pos = (double)i_time / (double)i_length;
1543 i_ret = demux2_Control( p_input->p->input.p_demux,
1544 DEMUX_SET_POSITION, f_pos );
1549 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1550 " failed or not possible", i_time );
1554 if( p_input->p->i_slave > 0 )
1555 SlaveSeek( p_input );
1557 b_force_update = VLC_TRUE;
1562 case INPUT_CONTROL_SET_STATE:
1563 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1564 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1567 if( p_input->p->input.p_access )
1568 i_ret = access2_Control( p_input->p->input.p_access,
1569 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1571 i_ret = demux2_Control( p_input->p->input.p_demux,
1572 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1576 /* FIXME What to do ? */
1577 msg_Warn( p_input, "cannot unset pause -> EOF" );
1578 vlc_mutex_unlock( &p_input->p->lock_control );
1579 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1580 vlc_mutex_lock( &p_input->p->lock_control );
1583 b_force_update = VLC_TRUE;
1585 /* Switch to play */
1586 p_input->i_state = PLAYING_S;
1587 val.i_int = PLAYING_S;
1588 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1591 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1593 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1594 p_input->p->b_can_pause )
1597 if( p_input->p->input.p_access )
1598 i_ret = access2_Control( p_input->p->input.p_access,
1599 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1601 i_ret = demux2_Control( p_input->p->input.p_demux,
1602 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1604 b_force_update = VLC_TRUE;
1608 msg_Warn( p_input, "cannot set pause state" );
1609 val.i_int = p_input->i_state;
1613 val.i_int = PAUSE_S;
1616 /* Switch to new state */
1617 p_input->i_state = val.i_int;
1618 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1620 /* Send discontinuity to decoders (it will allow them to flush
1622 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_FALSE, VLC_FALSE );
1624 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1626 b_force_update = VLC_TRUE;
1628 /* Correct "state" value */
1629 val.i_int = p_input->i_state;
1630 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1632 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1634 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1638 case INPUT_CONTROL_SET_RATE:
1639 case INPUT_CONTROL_SET_RATE_SLOWER:
1640 case INPUT_CONTROL_SET_RATE_FASTER:
1644 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1645 i_rate = p_input->p->i_rate * 2;
1646 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1647 i_rate = p_input->p->i_rate / 2;
1651 if( i_rate < INPUT_RATE_MIN )
1653 msg_Dbg( p_input, "cannot set rate faster" );
1654 i_rate = INPUT_RATE_MIN;
1656 else if( i_rate > INPUT_RATE_MAX )
1658 msg_Dbg( p_input, "cannot set rate slower" );
1659 i_rate = INPUT_RATE_MAX;
1661 if( i_rate != INPUT_RATE_DEFAULT &&
1662 ( !p_input->b_can_pace_control ||
1663 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1665 msg_Dbg( p_input, "cannot change rate" );
1666 i_rate = INPUT_RATE_DEFAULT;
1668 if( i_rate != p_input->p->i_rate )
1671 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1673 input_EsOutDiscontinuity( p_input->p->p_es_out,
1674 VLC_FALSE, VLC_FALSE );
1676 p_input->p->i_rate = i_rate;
1678 input_EsOutSetRate( p_input->p->p_es_out );
1680 b_force_update = VLC_TRUE;
1685 case INPUT_CONTROL_SET_PROGRAM:
1686 /* No need to force update, es_out does it if needed */
1687 es_out_Control( p_input->p->p_es_out,
1688 ES_OUT_SET_GROUP, val.i_int );
1690 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1694 case INPUT_CONTROL_SET_ES:
1695 /* No need to force update, es_out does it if needed */
1696 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1697 input_EsOutGetFromID( p_input->p->p_es_out,
1701 case INPUT_CONTROL_SET_AUDIO_DELAY:
1702 input_EsOutSetDelay( p_input->p->p_es_out,
1703 AUDIO_ES, val.i_time );
1704 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1707 case INPUT_CONTROL_SET_SPU_DELAY:
1708 input_EsOutSetDelay( p_input->p->p_es_out,
1709 SPU_ES, val.i_time );
1710 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1713 case INPUT_CONTROL_SET_TITLE:
1714 case INPUT_CONTROL_SET_TITLE_NEXT:
1715 case INPUT_CONTROL_SET_TITLE_PREV:
1716 if( p_input->p->input.b_title_demux &&
1717 p_input->p->input.i_title > 0 )
1720 /* FIXME handle demux title */
1721 demux_t *p_demux = p_input->p->input.p_demux;
1724 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1725 i_title = p_demux->info.i_title - 1;
1726 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1727 i_title = p_demux->info.i_title + 1;
1729 i_title = val.i_int;
1731 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1733 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1734 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1736 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1737 input_ControlVarTitle( p_input, i_title );
1740 else if( p_input->p->input.i_title > 0 )
1742 access_t *p_access = p_input->p->input.p_access;
1745 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1746 i_title = p_access->info.i_title - 1;
1747 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1748 i_title = p_access->info.i_title + 1;
1750 i_title = val.i_int;
1752 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1754 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1755 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1757 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1758 stream_AccessReset( p_input->p->input.p_stream );
1762 case INPUT_CONTROL_SET_SEEKPOINT:
1763 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1764 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1765 if( p_input->p->input.b_title_demux &&
1766 p_input->p->input.i_title > 0 )
1768 demux_t *p_demux = p_input->p->input.p_demux;
1770 int64_t i_input_time;
1771 int64_t i_seekpoint_time;
1773 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1775 i_seekpoint = p_demux->info.i_seekpoint;
1776 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1777 if( i_seekpoint_time >= 0 &&
1778 !demux2_Control( p_demux,
1779 DEMUX_GET_TIME, &i_input_time ) )
1781 if ( i_input_time < i_seekpoint_time + 3000000 )
1787 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1788 i_seekpoint = p_demux->info.i_seekpoint + 1;
1790 i_seekpoint = val.i_int;
1792 if( i_seekpoint >= 0 && i_seekpoint <
1793 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1795 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1796 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1798 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1801 else if( p_input->p->input.i_title > 0 )
1803 demux_t *p_demux = p_input->p->input.p_demux;
1804 access_t *p_access = p_input->p->input.p_access;
1806 int64_t i_input_time;
1807 int64_t i_seekpoint_time;
1809 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1811 i_seekpoint = p_access->info.i_seekpoint;
1812 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1813 if( i_seekpoint_time >= 0 &&
1814 demux2_Control( p_demux,
1815 DEMUX_GET_TIME, &i_input_time ) )
1817 if ( i_input_time < i_seekpoint_time + 3000000 )
1823 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1824 i_seekpoint = p_access->info.i_seekpoint + 1;
1826 i_seekpoint = val.i_int;
1828 if( i_seekpoint >= 0 && i_seekpoint <
1829 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1831 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1832 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1834 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1836 stream_AccessReset( p_input->p->input.p_stream );
1841 case INPUT_CONTROL_ADD_SLAVE:
1842 if( val.psz_string )
1844 input_source_t *slave = InputSourceNew( p_input );
1846 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1852 msg_Dbg( p_input, "adding %s as slave on the fly",
1856 if( demux2_Control( p_input->p->input.p_demux,
1857 DEMUX_GET_TIME, &i_time ) )
1859 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1860 InputSourceClean( slave );
1864 if( demux2_Control( slave->p_demux,
1865 DEMUX_SET_TIME, i_time ) )
1867 msg_Err( p_input, "seek failed for new slave" );
1868 InputSourceClean( slave );
1873 /* Get meta (access and demux) */
1874 p_meta = vlc_meta_New();
1875 access2_Control( slave->p_access, ACCESS_GET_META,
1877 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1878 InputUpdateMeta( p_input, p_meta );
1880 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1885 msg_Warn( p_input, "failed to add %s as slave",
1889 free( val.psz_string );
1893 case INPUT_CONTROL_SET_BOOKMARK:
1895 msg_Err( p_input, "not yet implemented" );
1899 return b_force_update;
1902 /*****************************************************************************
1904 *****************************************************************************/
1905 static int UpdateFromDemux( input_thread_t *p_input )
1907 demux_t *p_demux = p_input->p->input.p_demux;
1910 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1912 v.i_int = p_demux->info.i_title;
1913 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1915 input_ControlVarTitle( p_input, p_demux->info.i_title );
1917 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1919 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1921 v.i_int = p_demux->info.i_seekpoint;
1922 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1924 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1926 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1928 /* Hmmm only works with master input */
1929 if( p_input->p->input.p_demux == p_demux )
1931 int i_title_end = p_input->p->input.i_title_end -
1932 p_input->p->input.i_title_offset;
1933 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1934 p_input->p->input.i_seekpoint_offset;
1936 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1938 if( p_demux->info.i_title > i_title_end ||
1939 ( p_demux->info.i_title == i_title_end &&
1940 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1942 else if( i_seekpoint_end >=0 )
1944 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1946 else if( i_title_end >= 0 )
1948 if( p_demux->info.i_title > i_title_end ) return 0;
1955 /*****************************************************************************
1957 *****************************************************************************/
1958 static int UpdateFromAccess( input_thread_t *p_input )
1960 access_t *p_access = p_input->p->input.p_access;
1963 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1965 v.i_int = p_access->info.i_title;
1966 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1968 input_ControlVarTitle( p_input, p_access->info.i_title );
1970 stream_AccessUpdate( p_input->p->input.p_stream );
1972 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1974 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1976 v.i_int = p_access->info.i_seekpoint;
1977 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1978 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1980 if( p_access->info.i_update & INPUT_UPDATE_META )
1982 /* TODO maybe multi - access ? */
1983 vlc_meta_t *p_meta = vlc_meta_New();
1984 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
1985 InputUpdateMeta( p_input, p_meta );
1986 var_SetBool( p_input, "item-change", p_input->p->input.p_item->i_id );
1987 p_access->info.i_update &= ~INPUT_UPDATE_META;
1990 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1992 /* Hmmm only works with master input */
1993 if( p_input->p->input.p_access == p_access )
1995 int i_title_end = p_input->p->input.i_title_end -
1996 p_input->p->input.i_title_offset;
1997 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1998 p_input->p->input.i_seekpoint_offset;
2000 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2002 if( p_access->info.i_title > i_title_end ||
2003 ( p_access->info.i_title == i_title_end &&
2004 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2006 else if( i_seekpoint_end >=0 )
2008 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2010 else if( i_title_end >= 0 )
2012 if( p_access->info.i_title > i_title_end ) return 0;
2019 /*****************************************************************************
2021 *****************************************************************************/
2022 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2024 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2025 p_input->p->input.p_item->i_duration = i_length;
2026 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2028 if( !p_input->b_preparsing )
2030 pl_Yield( p_input );
2031 var_SetInteger( pl_Get( p_input ), "item-change",
2032 p_input->p->input.p_item->i_id );
2033 pl_Release( p_input )
2037 /*****************************************************************************
2039 *****************************************************************************/
2040 static input_source_t *InputSourceNew( input_thread_t *p_input )
2042 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2046 msg_Err( p_input, "out of memory for new input source" );
2051 in->p_access = NULL;
2052 in->p_stream = NULL;
2054 in->b_title_demux = VLC_FALSE;
2055 TAB_INIT( in->i_title, in->title );
2056 in->b_can_pace_control = VLC_TRUE;
2057 in->b_eof = VLC_FALSE;
2058 in->i_cr_average = 0;
2063 /*****************************************************************************
2065 *****************************************************************************/
2066 static int InputSourceInit( input_thread_t *p_input,
2067 input_source_t *in, const char *psz_mrl,
2068 const char *psz_forced_demux )
2070 char psz_dup[strlen (psz_mrl) + 1];
2071 const char *psz_access;
2072 const char *psz_demux;
2078 strcpy( psz_dup, psz_mrl );
2080 if( !in ) return VLC_EGENERIC;
2081 if( !p_input ) return VLC_EGENERIC;
2084 if( !p_input->b_preparsing )
2086 MRLSplit( VLC_OBJECT(p_input), psz_dup,
2087 &psz_access, &psz_demux, &psz_path );
2089 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2090 psz_mrl, psz_access, psz_demux, psz_path );
2092 /* Hack to allow udp://@:port syntax */
2094 (strncmp( psz_access, "udp", 3 ) &&
2095 strncmp( psz_access, "rtp", 3 )) )
2097 /* Find optional titles and seekpoints */
2098 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2099 &in->i_seekpoint_start, &in->i_seekpoint_end );
2101 if( psz_forced_demux && *psz_forced_demux )
2103 psz_demux = psz_forced_demux;
2105 else if( !psz_demux || *psz_demux == '\0' )
2107 /* special hack for forcing a demuxer with --demux=module
2108 * (and do nothing with a list) */
2109 char *psz_var_demux = var_GetString( p_input, "demux" );
2111 if( *psz_var_demux != '\0' &&
2112 !strchr(psz_var_demux, ',' ) &&
2113 !strchr(psz_var_demux, ':' ) )
2115 psz_demux = psz_var_demux;
2117 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2119 else if( psz_var_demux )
2121 free( psz_var_demux );
2125 /* Try access_demux if no demux given */
2126 if( *psz_demux == '\0' )
2128 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2129 NULL, p_input->p->p_es_out, VLC_FALSE );
2135 if( !strncmp( psz_path, "file://", 7 ) )
2137 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2139 psz_access = "file";
2144 int64_t i_pts_delay;
2146 /* Get infos from access_demux */
2147 demux2_Control( in->p_demux,
2148 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2149 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2151 in->b_title_demux = VLC_TRUE;
2152 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2153 &in->title, &in->i_title,
2154 &in->i_title_offset, &in->i_seekpoint_offset ) )
2159 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2160 &in->b_can_pace_control );
2161 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2165 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2171 int64_t i_pts_delay;
2173 input_ChangeState( p_input, OPENING_S );
2175 /* Now try a real access */
2176 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2177 p_input->b_preparsing );
2179 /* Access failed, URL encoded ? */
2180 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2182 decode_URI( psz_path );
2184 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2185 psz_access, psz_demux, psz_path );
2187 in->p_access = access2_New( p_input,
2188 psz_access, psz_demux, psz_path,
2189 p_input->b_preparsing );
2192 if( in->p_access == NULL )
2194 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2196 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2197 _("Your input can't be opened"),
2198 _("VLC is unable to open the MRL '%s'."
2199 " Check the log for details."), psz_mrl );
2204 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2205 while( psz && *psz )
2207 access_t *p_access = in->p_access;
2208 char *end = strchr( psz, ':' );
2213 in->p_access = access2_FilterNew( in->p_access, psz );
2214 if( in->p_access == NULL )
2216 in->p_access = p_access;
2217 msg_Warn( p_input, "failed to insert access filter %s",
2223 if( psz_tmp ) free( psz_tmp );
2225 /* Get infos from access */
2226 if( !p_input->b_preparsing )
2228 access2_Control( in->p_access,
2229 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2230 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2232 in->b_title_demux = VLC_FALSE;
2233 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2234 &in->title, &in->i_title,
2235 &in->i_title_offset, &in->i_seekpoint_offset ) )
2241 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2242 &in->b_can_pace_control );
2243 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2245 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2247 var_Set( p_input, "seekable", val );
2250 input_ChangeState( p_input, BUFFERING_S );
2252 /* Create the stream_t */
2253 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2254 if( in->p_stream == NULL )
2256 msg_Warn( p_input, "cannot create a stream_t from access" );
2260 /* Open a demuxer */
2261 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2263 psz_demux = in->p_access->psz_demux;
2267 /* Take access redirections into account */
2268 char *psz_real_path;
2269 char *psz_buf = NULL;
2270 if( in->p_access->psz_path )
2272 const char *psz_a, *psz_d;
2273 psz_buf = strdup( in->p_access->psz_path );
2274 MRLSplit( VLC_OBJECT(p_input), psz_buf,
2275 &psz_a, &psz_d, &psz_real_path );
2279 psz_real_path = psz_path;
2281 in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2283 in->p_stream, p_input->p->p_es_out,
2284 p_input->b_preparsing );
2288 if( in->p_demux == NULL )
2290 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2291 psz_access, psz_demux, psz_path );
2292 intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2293 _("Can't recognize the input's format"),
2294 _("The format of '%s' can't be detected. "
2295 "Have a look the log for details."), psz_mrl );
2299 /* Get title from demux */
2300 if( !p_input->b_preparsing && in->i_title <= 0 )
2302 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2303 &in->title, &in->i_title,
2304 &in->i_title_offset, &in->i_seekpoint_offset ))
2306 TAB_INIT( in->i_title, in->title );
2310 in->b_title_demux = VLC_TRUE;
2314 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2315 if( 1 || !p_input->b_preparsing )
2318 input_attachment_t **attachment;
2319 if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2320 &attachment, &i_attachment ) )
2323 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2324 p_input->p->attachment = realloc( p_input->p->attachment,
2325 sizeof(input_attachment_t**) * ( p_input->p->i_attachment + i_attachment ) );
2326 for( i = 0; i < i_attachment; i++ )
2327 p_input->p->attachment[p_input->p->i_attachment++] = attachment[i];
2330 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2335 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2336 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2341 input_ChangeState( p_input, ERROR_S );
2344 demux2_Delete( in->p_demux );
2347 stream_Delete( in->p_stream );
2350 access2_Delete( in->p_access );
2352 return VLC_EGENERIC;
2355 /*****************************************************************************
2357 *****************************************************************************/
2358 static void InputSourceClean( input_source_t *in )
2363 demux2_Delete( in->p_demux );
2366 stream_Delete( in->p_stream );
2369 access2_Delete( in->p_access );
2371 if( in->i_title > 0 )
2373 for( i = 0; i < in->i_title; i++ )
2374 vlc_input_title_Delete( in->title[i] );
2375 TAB_CLEAN( in->i_title, in->title );
2379 static void SlaveDemux( input_thread_t *p_input )
2384 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2386 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2390 for( i = 0; i < p_input->p->i_slave; i++ )
2392 input_source_t *in = p_input->p->slave[i];
2398 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2403 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2405 msg_Err( p_input, "slave[%d] doesn't like "
2406 "DEMUX_GET_TIME -> EOF", i );
2411 if( i_stime >= i_time )
2414 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2420 i_ret = in->p_demux->pf_demux( in->p_demux );
2425 msg_Dbg( p_input, "slave %d EOF", i );
2426 in->b_eof = VLC_TRUE;
2431 static void SlaveSeek( input_thread_t *p_input )
2436 if( !p_input ) return;
2438 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2440 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2444 for( i = 0; i < p_input->p->i_slave; i++ )
2446 input_source_t *in = p_input->p->slave[i];
2448 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2450 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2451 in->b_eof = VLC_TRUE;
2456 /*****************************************************************************
2458 *****************************************************************************/
2459 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2463 if( !p_meta ) return;
2465 /* Get meta information from user */
2466 #define GET_META( field, s ) \
2467 var_Get( p_input, (s), &val ); \
2468 if( *val.psz_string ) { \
2469 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2470 p_meta->psz_ ## field = strdup( val.psz_string ); \
2472 free( val.psz_string )
2474 GET_META( title, "meta-title" );
2475 GET_META( artist, "meta-artist" );
2476 GET_META( genre, "meta-genre" );
2477 GET_META( copyright, "meta-copyright" );
2478 GET_META( description, "meta-description" );
2479 GET_META( date, "meta-date" );
2480 GET_META( url, "meta-url" );
2484 /*****************************************************************************
2485 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2486 * arturl and locking issue.
2487 *****************************************************************************/
2488 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2490 input_item_t *p_item = p_input->p->input.p_item;
2491 char *psz_title = NULL;
2496 vlc_mutex_lock( &p_item->lock );
2497 if( p_meta->psz_title && !p_item->b_fixed_name )
2498 psz_title = strdup( p_meta->psz_title );
2500 if( p_item->p_meta )
2502 char *psz_arturl = p_item->p_meta->psz_arturl;
2503 p_item->p_meta->psz_arturl = NULL;
2505 vlc_meta_Merge( p_item->p_meta, p_meta );
2507 if( psz_arturl && *psz_arturl )
2508 vlc_meta_SetArtURL( p_item->p_meta, psz_arturl );
2510 vlc_meta_Delete( p_meta );
2514 p_item->p_meta = p_meta;
2516 if( p_item->p_meta->psz_arturl && !strncmp( p_item->p_meta->psz_arturl, "attachment://", strlen("attachment") ) )
2518 /* Don't look for art cover if sout
2519 * XXX It can change when sout has meta data support */
2520 if( p_input->p->p_sout && !p_input->b_preparsing )
2521 vlc_meta_SetArtURL( p_item->p_meta, "" );
2523 input_ExtractAttachmentAndCacheArt( p_input );
2526 p_item->p_meta->i_status |= ITEM_PREPARSED;
2527 vlc_mutex_unlock( &p_item->lock );
2531 input_Control( p_input, INPUT_SET_NAME, psz_title );
2535 /** \todo handle sout meta */
2539 static inline vlc_bool_t IsValidAccess( const char *psz )
2543 while( ( c = *psz ) != '\0' )
2548 if( ( !isascii( c ) || !isalnum( c ) ) && ( c != '/' ) )
2552 /* should not happen though */
2557 /*****************************************************************************
2558 * MRLSplit: parse the access, demux and url part of the
2559 * Media Resource Locator.
2560 *****************************************************************************/
2561 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2562 const char **ppsz_access, const char **ppsz_demux,
2565 const char *psz_access = "";
2566 const char *psz_demux = "";
2570 psz = strchr( psz_dup, ':' );
2574 /* Guess whether ':' is part of a local filename, or separates
2575 * access/demux from path */
2576 if( !IsValidAccess( psz_dup ) )
2578 #if defined( WIN32 ) || defined( UNDER_CE )
2579 else if( ( psz - psz_dup == 1 ) && isalpha( psz_dup[0] ) )
2581 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2590 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2594 psz = strchr( psz_dup, '/' );
2601 psz_access = psz_dup;
2608 *ppsz_access = psz_access;
2609 *ppsz_demux = psz_demux;
2610 *ppsz_path = psz_path;
2613 /*****************************************************************************
2614 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2617 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2618 *****************************************************************************/
2619 static void MRLSections( input_thread_t *p_input, char *psz_source,
2620 int *pi_title_start, int *pi_title_end,
2621 int *pi_chapter_start, int *pi_chapter_end )
2623 char *psz, *psz_end, *psz_next, *psz_check;
2625 *pi_title_start = *pi_title_end = -1;
2626 *pi_chapter_start = *pi_chapter_end = -1;
2628 /* Start by parsing titles and chapters */
2629 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2631 /* Check we are really dealing with a title/chapter section */
2632 psz_check = psz + 1;
2633 if( !*psz_check ) return;
2634 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2635 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2636 if( *psz_check == ':' && ++psz_check )
2637 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2638 if( *psz_check != '-' && *psz_check ) return;
2639 if( *psz_check == '-' && ++psz_check )
2640 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2641 if( *psz_check != ':' && *psz_check ) return;
2642 if( *psz_check == ':' && ++psz_check )
2643 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2644 if( *psz_check ) return;
2646 /* Separate start and end */
2648 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2650 /* Look for the start title */
2651 *pi_title_start = strtol( psz, &psz_next, 0 );
2652 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2653 *pi_title_end = *pi_title_start;
2656 /* Look for the start chapter */
2658 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2659 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2660 *pi_chapter_end = *pi_chapter_start;
2664 /* Look for the end title */
2665 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2666 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2669 /* Look for the end chapter */
2670 if( *psz_end ) psz_end++;
2671 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2672 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2675 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2676 psz_source, *pi_title_start, *pi_chapter_start,
2677 *pi_title_end, *pi_chapter_end );
2680 /*****************************************************************************
2681 * input_AddSubtitles: add a subtitles file and enable it
2682 *****************************************************************************/
2683 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2684 vlc_bool_t b_check_extension )
2686 input_source_t *sub;
2689 char *psz_path, *psz_extension;
2691 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2696 /* if we are provided a subtitle.sub file,
2697 * see if we don't have a subtitle.idx and use it instead */
2698 psz_path = strdup( psz_subtitle );
2701 psz_extension = strrchr( psz_path, '.');
2702 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2706 strcpy( psz_extension, ".idx" );
2707 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2708 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2711 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2712 psz_path, psz_subtitle );
2713 strcpy( psz_subtitle, psz_path );
2719 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2721 sub = InputSourceNew( p_input );
2722 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2724 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2727 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2729 if( count.i_int == 0 )
2731 /* if it was first one, there is disable too */
2733 if( count.i_int < list.p_list->i_count )
2735 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2736 &list.p_list->p_values[count.i_int] );
2738 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );