1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2004 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/input.h>
33 #include <vlc/decoder.h>
36 #include "input_internal.h"
38 #include "stream_output.h"
39 #include "vlc_playlist.h"
40 #include "vlc_interface.h"
41 #include "vlc_interaction.h"
42 #include "vlc_meta_engine.h"
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Run ( input_thread_t *p_input );
54 static int RunAndClean ( input_thread_t *p_input );
56 static input_thread_t * Create ( vlc_object_t *, input_item_t *, char *,
58 static int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
59 static void Error ( input_thread_t *p_input );
60 static void End ( input_thread_t *p_input );
61 static void MainLoop( input_thread_t *p_input );
63 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
64 static void ControlReduce( input_thread_t * );
65 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
67 static int UpdateFromAccess( input_thread_t * );
68 static int UpdateFromDemux( input_thread_t * );
69 static int UpdateMeta( input_thread_t *, vlc_bool_t );
71 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
73 static void DecodeUrl( char * );
74 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
76 static input_source_t *InputSourceNew( input_thread_t *);
77 static int InputSourceInit( input_thread_t *, input_source_t *,
78 char *, char *psz_forced_demux,
80 static void InputSourceClean( input_thread_t *, input_source_t * );
82 static void SlaveDemux( input_thread_t *p_input );
83 static void SlaveSeek( input_thread_t *p_input );
85 static void InputMetaUser( input_thread_t *p_input );
87 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item );
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 char *psz_header, vlc_bool_t b_quick )
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 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
129 /* Init Common fields */
130 p_input->b_eof = VLC_FALSE;
131 p_input->b_can_pace_control = VLC_TRUE;
132 p_input->i_start = 0;
135 p_input->i_title = 0;
136 p_input->title = NULL;
137 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
138 p_input->i_state = INIT_S;
139 p_input->i_rate = INPUT_RATE_DEFAULT;
140 p_input->i_bookmark = 0;
141 p_input->bookmark = NULL;
142 p_input->p_meta = NULL;
143 p_input->p_es_out = NULL;
144 p_input->p_sout = NULL;
145 p_input->b_out_pace_control = VLC_FALSE;
146 p_input->i_pts_delay = 0;
148 /* Init Input fields */
149 p_input->input.p_item = p_item;
150 p_input->input.p_access = NULL;
151 p_input->input.p_stream = NULL;
152 p_input->input.p_demux = NULL;
153 p_input->input.b_title_demux = VLC_FALSE;
154 p_input->input.i_title = 0;
155 p_input->input.title = NULL;
156 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
157 p_input->input.b_can_pace_control = VLC_TRUE;
158 p_input->input.b_eof = VLC_FALSE;
159 p_input->input.i_cr_average = 0;
161 if( !p_input->input.p_item->p_meta )
162 p_input->input.p_item->p_meta = vlc_meta_New();
163 stats_ReinitInputStats( p_item->p_stats );
166 p_input->i_slave = 0;
167 p_input->slave = NULL;
169 /* Init control buffer */
170 vlc_mutex_init( p_input, &p_input->lock_control );
171 p_input->i_control = 0;
173 /* Parse input options */
174 vlc_mutex_lock( &p_item->lock );
175 for( i = 0; i < p_item->i_options; i++ )
177 var_OptionParse( p_input, p_item->ppsz_options[i] );
179 vlc_mutex_unlock( &p_item->lock );
181 /* Create Object Variables for private use only */
182 input_ConfigVarInit( p_input );
184 /* Create Objects variables for public Get and Set */
185 input_ControlVarInit( p_input );
186 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
190 var_Get( p_input, "bookmarks", &val );
193 /* FIXME: have a common cfg parsing routine used by sout and others */
194 char *psz_parser, *psz_start, *psz_end;
195 psz_parser = val.psz_string;
196 while( (psz_start = strchr( psz_parser, '{' ) ) )
198 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
201 psz_end = strchr( psz_start, '}' );
202 if( !psz_end ) break;
203 psz_parser = psz_end + 1;
204 backup = *psz_parser;
207 while( (psz_end = strchr( psz_start, ',' ) ) )
210 if( !strncmp( psz_start, "name=", 5 ) )
212 p_seekpoint->psz_name = psz_start + 5;
214 else if( !strncmp( psz_start, "bytes=", 6 ) )
216 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
218 else if( !strncmp( psz_start, "time=", 5 ) )
220 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
223 psz_start = psz_end + 1;
225 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
226 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
227 p_seekpoint->i_time_offset );
228 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
229 vlc_seekpoint_Delete( p_seekpoint );
230 *psz_parser = backup;
232 free( val.psz_string );
236 /* Remove 'Now playing' info as it is probably outdated */
237 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT),
238 VLC_META_NOW_PLAYING ); /* ? Don't translate as it might has been copied ? */
244 * Initialize an input thread and run it. You will need to monitor the
245 * thread to clean up after it is done
247 * \param p_parent a vlc_object
248 * \param p_item an input item
249 * \return a pointer to the spawned input thread
251 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
252 input_item_t *p_item )
254 return __input_CreateThread2( p_parent, p_item, NULL );
259 input_thread_t *__input_CreateThread2( vlc_object_t *p_parent,
260 input_item_t *p_item,
263 input_thread_t *p_input = NULL; /* thread descriptor */
265 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
266 /* Now we can attach our new input */
267 vlc_object_attach( p_input, p_parent );
269 /* Create thread and wait for its readiness. */
270 if( vlc_thread_create( p_input, "input", Run,
271 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
273 msg_Err( p_input, "cannot create input thread" );
274 vlc_object_detach( p_input );
275 vlc_object_destroy( p_input );
284 * Initialize an input thread and run it. This thread will clean after himself,
285 * you can forget about it. It can work either in blocking or non-blocking mode
287 * \param p_parent a vlc_object
288 * \param p_item an input item
289 * \param b_block should we block until read is finished ?
290 * \return the input object id if non blocking, an error code else
292 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
295 input_thread_t *p_input = NULL; /* thread descriptor */
297 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
298 /* Now we can attach our new input */
299 vlc_object_attach( p_input, p_parent );
303 RunAndClean( p_input );
308 if( vlc_thread_create( p_input, "input", RunAndClean,
309 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
311 msg_Err( p_input, "cannot create input thread" );
312 vlc_object_detach( p_input );
313 vlc_object_destroy( p_input );
317 return p_input->i_object_id;
321 * Initialize an input and initialize it to preparse the item
322 * This function is blocking. It will only accept to parse files
324 * \param p_parent a vlc_object_t
325 * \param p_item an input item
326 * \return VLC_SUCCESS or an error
328 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
330 input_thread_t *p_input = NULL; /* thread descriptor */
332 /* Allocate descriptor */
333 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
334 p_input->i_flags |= OBJECT_FLAGS_QUIET;
335 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
337 /* Now we can attach our new input */
338 vlc_object_attach( p_input, p_parent );
340 Init( p_input, VLC_TRUE );
342 /* Clean up master */
343 InputSourceClean( p_input, &p_input->input );
345 /* Unload all modules */
346 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
348 vlc_object_detach( p_input );
349 vlc_object_destroy( p_input );
354 int __input_SecondaryPreparse( vlc_object_t *p_parent, input_item_t *p_item )
356 struct meta_engine_t *p_me;
358 /* FIXME: don't launch any module if we already have all the needed
359 * info. Easiest way to do this would be to add a dummy module.
360 * I'll do that later */
362 p_me = vlc_object_create( p_parent, VLC_OBJECT_META_ENGINE );
363 p_me->i_flags |= OBJECT_FLAGS_NOINTERACT;
364 p_me->i_mandatory = VLC_META_ENGINE_TITLE
365 | VLC_META_ENGINE_ARTIST
366 | VLC_META_ENGINE_ART_URL;
367 p_me->i_optional = 0;
368 p_me->p_item = p_item;
369 p_me->p_module = module_Need( p_me, "meta engine", 0, VLC_FALSE );
372 if( !p_me->p_module )
374 msg_Err( p_parent, "no suitable meta engine module" );
375 vlc_object_detach( p_me );
376 vlc_object_destroy( p_me );
380 module_Unneed( p_me, p_me->p_module );
382 vlc_object_destroy( p_me );
384 input_DownloadAndCacheArt( p_parent, p_item );
390 * Request a running input thread to stop and die
392 * \param the input thread to stop
394 void input_StopThread( input_thread_t *p_input )
399 /* Set die for input */
400 p_input->b_die = VLC_TRUE;
402 /* We cannot touch p_input fields directly (we can from another thread),
403 * so use the vlc_object_find way, it's perfectly safe */
405 /* Set die for all access */
406 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
407 for( i = 0; i < p_list->i_count; i++ )
409 p_list->p_values[i].p_object->b_die = VLC_TRUE;
411 vlc_list_release( p_list );
413 /* Set die for all stream */
414 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
415 for( i = 0; i < p_list->i_count; i++ )
417 p_list->p_values[i].p_object->b_die = VLC_TRUE;
419 vlc_list_release( p_list );
421 /* Set die for all demux */
422 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
423 for( i = 0; i < p_list->i_count; i++ )
425 p_list->p_values[i].p_object->b_die = VLC_TRUE;
427 vlc_list_release( p_list );
429 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
433 * Clean up a dead input thread
434 * This function does not return until the thread is effectively cancelled.
436 * \param the input thread to kill
438 void input_DestroyThread( input_thread_t *p_input )
440 /* Join the thread */
441 vlc_thread_join( p_input );
443 /* Delete input lock (only after thread joined) */
444 vlc_mutex_destroy( &p_input->lock_control );
446 /* TODO: maybe input_DestroyThread should also delete p_input instead
447 * of the playlist but I'm not sure if it's possible */
450 /*****************************************************************************
451 * Run: main thread loop
452 * This is the "normal" thread that spawns the input processing chain,
453 * reads the stream, cleans up and waits
454 *****************************************************************************/
455 static int Run( input_thread_t *p_input )
457 /* Signal that the thread is launched */
458 vlc_thread_ready( p_input );
460 if( Init( p_input, VLC_FALSE ) )
462 /* If we failed, wait before we are killed, and exit */
463 p_input->b_error = VLC_TRUE;
467 /* Tell we're dead */
468 p_input->b_dead = VLC_TRUE;
475 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
477 /* We have finish to demux data but not to play them */
478 while( !p_input->b_die )
480 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
483 msg_Dbg( p_input, "waiting decoder fifos to empty" );
485 msleep( INPUT_IDLE_SLEEP );
488 /* We have finished */
489 p_input->b_eof = VLC_TRUE;
492 /* Wait until we are asked to die */
493 if( !p_input->b_die )
504 /*****************************************************************************
505 * RunAndClean: main thread loop
506 * This is the "just forget me" thread that spawns the input processing chain,
507 * reads the stream, cleans up and releases memory
508 *****************************************************************************/
509 static int RunAndClean( input_thread_t *p_input )
511 /* Signal that the thread is launched */
512 vlc_thread_ready( p_input );
514 if( Init( p_input, VLC_FALSE ) )
516 /* If we failed, just exit */
522 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
524 /* We have finished demuxing data but not playing it */
525 while( !p_input->b_die )
527 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
530 msg_Dbg( p_input, "waiting decoder fifos to empty" );
532 msleep( INPUT_IDLE_SLEEP );
535 /* We have finished */
536 p_input->b_eof = VLC_TRUE;
543 vlc_object_detach( p_input );
544 vlc_object_destroy( p_input );
550 /*****************************************************************************
551 * Main loop: Fill buffers from access, and demux
552 *****************************************************************************/
553 static void MainLoop( input_thread_t *p_input )
555 int64_t i_intf_update = 0;
556 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
558 vlc_bool_t b_force_update = VLC_FALSE;
564 if( p_input->i_state != PAUSE_S )
566 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
567 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
574 if( p_input->input.b_title_demux &&
575 p_input->input.p_demux->info.i_update )
577 i_ret = UpdateFromDemux( p_input );
578 b_force_update = VLC_TRUE;
580 else if( !p_input->input.b_title_demux &&
581 p_input->input.p_access &&
582 p_input->input.p_access->info.i_update )
584 i_ret = UpdateFromAccess( p_input );
585 b_force_update = VLC_TRUE;
589 if( i_ret == 0 ) /* EOF */
593 var_Get( p_input, "input-repeat", &repeat );
594 if( repeat.i_int == 0 )
596 /* End of file - we do not set b_die because only the
597 * playlist is allowed to do so. */
598 msg_Dbg( p_input, "EOF reached" );
599 p_input->input.b_eof = VLC_TRUE;
603 msg_Dbg( p_input, "repeating the same input (%d)",
605 if( repeat.i_int > 0 )
608 var_Set( p_input, "input-repeat", repeat );
611 /* Seek to start title/seekpoint */
612 val.i_int = p_input->input.i_title_start -
613 p_input->input.i_title_offset;
614 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
616 input_ControlPush( p_input,
617 INPUT_CONTROL_SET_TITLE, &val );
619 val.i_int = p_input->input.i_seekpoint_start -
620 p_input->input.i_seekpoint_offset;
621 if( val.i_int > 0 /* TODO: check upper boundary */ )
622 input_ControlPush( p_input,
623 INPUT_CONTROL_SET_SEEKPOINT, &val );
625 /* Seek to start position */
626 if( p_input->i_start > 0 )
628 val.i_time = p_input->i_start;
629 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
635 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
642 p_input->b_error = VLC_TRUE;
645 if( i_ret > 0 && p_input->i_slave > 0 )
647 SlaveDemux( p_input );
657 vlc_mutex_lock( &p_input->lock_control );
658 ControlReduce( p_input );
659 while( !ControlPopNoLock( p_input, &i_type, &val ) )
661 msg_Dbg( p_input, "control type=%d", i_type );
662 if( Control( p_input, i_type, val ) )
663 b_force_update = VLC_TRUE;
665 vlc_mutex_unlock( &p_input->lock_control );
667 if( b_force_update || i_intf_update < mdate() )
671 int64_t i_time, i_length;
672 /* update input status variables */
673 if( !demux2_Control( p_input->input.p_demux,
674 DEMUX_GET_POSITION, &f_pos ) )
676 val.f_float = (float)f_pos;
677 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
679 if( !demux2_Control( p_input->input.p_demux,
680 DEMUX_GET_TIME, &i_time ) )
682 p_input->i_time = i_time;
684 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
686 if( !demux2_Control( p_input->input.p_demux,
687 DEMUX_GET_LENGTH, &i_length ) )
690 var_Get( p_input, "length", &old_val );
691 val.i_time = i_length;
692 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
694 if( old_val.i_time != val.i_time )
696 UpdateItemLength( p_input, i_length, VLC_TRUE );
700 var_SetBool( p_input, "intf-change", VLC_TRUE );
701 i_intf_update = mdate() + I64C(150000);
707 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
717 /* Initialize optional stream output. (before access/demuxer)
718 * XXX: we add a special case if the uri starts by vlc.
719 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
720 * be destroyed in case of a file).
721 * (this will break playing of file starting by 'vlc:' but I don't
722 * want to add more logic, just force file by file:// or code it ;)
724 memset( &p_input->counters, 0, sizeof( p_input->counters ) );
727 /* Prepare statistics */
728 #define INIT_COUNTER( p, type, compute ) p_input->counters.p_##p = \
729 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
730 if( p_input->p_libvlc->b_stats )
732 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
733 INIT_COUNTER( read_packets, INTEGER, COUNTER );
734 INIT_COUNTER( demux_read, INTEGER, COUNTER );
735 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
736 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
737 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
738 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
739 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
740 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
741 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
742 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
743 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
744 p_input->counters.p_sout_send_bitrate = NULL;
745 p_input->counters.p_sout_sent_packets = NULL;
746 p_input->counters.p_sout_sent_bytes = NULL;
747 if( p_input->counters.p_demux_bitrate )
748 p_input->counters.p_demux_bitrate->update_interval = 1000000;
749 if( p_input->counters.p_input_bitrate )
750 p_input->counters.p_input_bitrate->update_interval = 1000000;
752 vlc_mutex_init( p_input, &p_input->counters.counters_lock );
755 psz = var_GetString( p_input, "sout" );
756 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
758 p_input->p_sout = sout_NewInstance( p_input, psz );
759 if( p_input->p_sout == NULL )
761 msg_Err( p_input, "cannot start stream output instance, " \
766 if( p_input->p_libvlc->b_stats )
768 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
769 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
770 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
771 if( p_input->counters.p_sout_send_bitrate )
772 p_input->counters.p_sout_send_bitrate->update_interval =
780 p_input->p_es_out = input_EsOutNew( p_input );
781 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
782 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
784 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
785 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
787 if( InputSourceInit( p_input, &p_input->input,
788 p_input->input.p_item->psz_uri, NULL, b_quick ) )
793 /* Create global title (from master) */
796 p_input->i_title = p_input->input.i_title;
797 p_input->title = p_input->input.title;
798 p_input->i_title_offset = p_input->input.i_title_offset;
799 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
800 if( p_input->i_title > 0 )
802 /* Setup variables */
803 input_ControlVarNavigation( p_input );
804 input_ControlVarTitle( p_input, 0 );
808 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
809 p_input->b_can_pause = p_input->input.b_can_pause;
812 if( p_input->i_pts_delay < 0 )
813 p_input->i_pts_delay = 0;
815 /* If the desynchronisation requested by the user is < 0, we need to
816 * cache more data. */
817 var_Get( p_input, "audio-desync", &val );
818 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
820 /* Update cr_average depending on the caching */
821 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
822 p_input->input.i_cr_average /= 10;
823 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
826 /* Load master infos */
828 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
829 &val.i_time ) && val.i_time > 0 )
831 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
832 UpdateItemLength( p_input, val.i_time, b_quick );
833 p_input->input.p_item->i_duration = val.i_time;
836 /* Start title/chapter */
839 val.i_int = p_input->input.i_title_start -
840 p_input->input.i_title_offset;
841 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
842 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
843 val.i_int = p_input->input.i_seekpoint_start -
844 p_input->input.i_seekpoint_offset;
845 if( val.i_int > 0 /* TODO: check upper boundary */ )
846 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
850 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
852 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
855 if( p_input->i_start > 0 )
857 if( p_input->i_start >= val.i_time )
859 msg_Warn( p_input, "invalid start-time ignored" );
865 msg_Dbg( p_input, "starting at time: %ds",
866 (int)( p_input->i_start / I64C(1000000) ) );
868 s.i_time = p_input->i_start;
869 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
872 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
874 msg_Warn( p_input, "invalid stop-time ignored" );
880 /* Get fps and set it if not already set */
881 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
884 float f_requested_fps;
886 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
887 var_SetFloat( p_input, "sub-original-fps", f_fps );
889 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
890 if( f_requested_fps != f_fps )
892 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
894 var_SetFloat( p_input, "sub-fps", f_requested_fps );
898 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
901 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
905 /* Look for and add subtitle files */
906 psz_subtitle = var_GetString( p_input, "sub-file" );
909 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
910 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
913 var_Get( p_input, "sub-autodetect-file", &val );
916 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
917 char **subs = subtitles_Detect( p_input, psz_autopath,
918 p_input->input.p_item->psz_uri );
923 /* Try to autoselect the first autodetected subtitles file
924 * if no subtitles file was specified */
925 if( *psz_subtitle == 0 && subs && subs[0] )
927 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
932 /* Then, just add the following subtitles files */
933 for( ; subs && subs[i]; i++ )
935 if( strcmp( psz_subtitle, subs[i] ) )
937 sub = InputSourceNew( p_input );
938 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
941 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
946 if( subs ) free( subs );
947 if( psz_autopath ) free( psz_autopath );
949 free( psz_subtitle );
952 psz = var_GetString( p_input, "input-slave" );
956 input_source_t *slave;
959 while( *psz == ' ' || *psz == '#' )
963 if( ( psz_delim = strchr( psz, '#' ) ) )
972 msg_Dbg( p_input, "adding slave input '%s'", psz );
973 slave = InputSourceNew( p_input );
974 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
976 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
981 if( psz ) free( psz );
985 p_input->i_start = 0;
986 p_input->i_start = 0;
992 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
993 i_es_out_mode = ES_OUT_MODE_AUTO;
995 if( p_input->p_sout )
997 var_Get( p_input, "sout-all", &val );
1000 i_es_out_mode = ES_OUT_MODE_ALL;
1005 var_Get( p_input, "programs", &val );
1006 if ( val.p_list && val.p_list->i_count )
1008 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1009 /* Note : we should remove the "program" callback. */
1012 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1016 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1018 /* Inform the demuxer about waited group (needed only for DVB) */
1019 if( i_es_out_mode == ES_OUT_MODE_ALL )
1021 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1023 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1025 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
1030 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
1031 (int) var_GetInteger( p_input, "program" ), NULL );
1034 if( p_input->p_sout )
1036 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
1038 p_input->b_out_pace_control = VLC_FALSE;
1042 p_input->b_out_pace_control = VLC_TRUE;
1045 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
1047 /* We don't want a high input priority here or we'll
1048 * end-up sucking up all the CPU time */
1049 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1052 msg_Dbg( p_input, "starting in %s mode",
1053 p_input->b_out_pace_control ? "async" : "sync" );
1057 p_meta = p_input->input.p_item->p_meta;
1058 /* Get meta data from users */
1059 InputMetaUser( p_input );
1060 /* Get meta data from master input */
1061 demux2_Control( p_input->input.p_demux, DEMUX_GET_META, p_meta );
1063 /* Access_file does not give any meta, and there are no slave */
1066 if( p_input->input.p_access )
1067 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1070 /* Get meta data from slave input */
1071 for( i = 0; i < p_input->i_slave; i++ )
1073 demux2_Control( p_input->slave[i]->p_demux,
1074 DEMUX_GET_META, p_meta );
1075 if( p_input->slave[i]->p_access )
1077 access2_Control( p_input->slave[i]->p_access,
1078 ACCESS_GET_META, p_meta );
1083 UpdateMeta( p_input, b_quick );
1087 msg_Dbg( p_input, "`%s' successfully opened",
1088 p_input->input.p_item->psz_uri );
1092 /* initialization is complete */
1093 input_ChangeState(p_input, PLAYING_S);
1098 if( p_input->p_es_out )
1099 input_EsOutDelete( p_input->p_es_out );
1101 if( p_input->p_sout )
1102 sout_DeleteInstance( p_input->p_sout );
1104 /* Mark them deleted */
1105 p_input->input.p_demux = NULL;
1106 p_input->input.p_stream = NULL;
1107 p_input->input.p_access = NULL;
1108 p_input->p_es_out = NULL;
1109 p_input->p_sout = NULL;
1111 return VLC_EGENERIC;
1114 /*****************************************************************************
1115 * Error: RunThread() error loop
1116 *****************************************************************************
1117 * This function is called when an error occurred during thread main's loop.
1118 *****************************************************************************/
1119 static void Error( input_thread_t *p_input )
1121 while( !p_input->b_die )
1124 msleep( INPUT_IDLE_SLEEP );
1128 /*****************************************************************************
1129 * End: end the input thread
1130 *****************************************************************************/
1131 static void End( input_thread_t * p_input )
1135 msg_Dbg( p_input, "closing input" );
1137 /* We are at the end */
1138 input_ChangeState(p_input, END_S);
1140 /* Clean control variables */
1141 input_ControlVarClean( p_input );
1143 /* Clean up master */
1144 InputSourceClean( p_input, &p_input->input );
1147 for( i = 0; i < p_input->i_slave; i++ )
1149 InputSourceClean( p_input, p_input->slave[i] );
1150 free( p_input->slave[i] );
1152 if( p_input->slave ) free( p_input->slave );
1154 /* Unload all modules */
1155 if( p_input->p_es_out )
1156 input_EsOutDelete( p_input->p_es_out );
1158 #define CL_CO( c ) stats_CounterClean( p_input->counters.p_##c ); p_input->counters.p_##c = NULL;
1159 if( p_input->p_libvlc->b_stats )
1161 vlc_mutex_lock( &p_input->counters.counters_lock );
1162 CL_CO( read_bytes );
1163 CL_CO( read_packets );
1164 CL_CO( demux_read );
1165 CL_CO( input_bitrate );
1166 CL_CO( demux_bitrate );
1167 CL_CO( played_abuffers );
1168 CL_CO( lost_abuffers );
1169 CL_CO( displayed_pictures );
1170 CL_CO( lost_pictures );
1171 CL_CO( decoded_audio) ;
1172 CL_CO( decoded_video );
1173 CL_CO( decoded_sub) ;
1174 vlc_mutex_unlock( &p_input->counters.counters_lock );
1177 /* Close optional stream output instance */
1178 if( p_input->p_sout )
1182 vlc_mutex_lock( &p_input->counters.counters_lock );
1183 CL_CO( sout_sent_packets );
1184 CL_CO( sout_sent_bytes );
1185 CL_CO( sout_send_bitrate );
1186 vlc_mutex_unlock( &p_input->counters.counters_lock );
1188 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool )
1190 /* attach sout to the playlist */
1191 msg_Dbg( p_input, "keeping sout" );
1192 vlc_object_detach( p_input->p_sout );
1193 vlc_object_attach( p_input->p_sout, p_input->p_libvlc->p_playlist );
1197 msg_Dbg( p_input, "destroying sout" );
1198 sout_DeleteInstance( p_input->p_sout );
1203 /* Tell we're dead */
1204 p_input->b_dead = VLC_TRUE;
1207 /*****************************************************************************
1209 *****************************************************************************/
1210 static inline int ControlPopNoLock( input_thread_t *p_input,
1211 int *pi_type, vlc_value_t *p_val )
1213 if( p_input->i_control <= 0 )
1215 return VLC_EGENERIC;
1218 *pi_type = p_input->control[0].i_type;
1219 *p_val = p_input->control[0].val;
1221 p_input->i_control--;
1222 if( p_input->i_control > 0 )
1226 for( i = 0; i < p_input->i_control; i++ )
1228 p_input->control[i].i_type = p_input->control[i+1].i_type;
1229 p_input->control[i].val = p_input->control[i+1].val;
1236 static void ControlReduce( input_thread_t *p_input )
1239 for( i = 1; i < p_input->i_control; i++ )
1241 const int i_lt = p_input->control[i-1].i_type;
1242 const int i_ct = p_input->control[i].i_type;
1244 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1245 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1249 ( i_ct == INPUT_CONTROL_SET_STATE ||
1250 i_ct == INPUT_CONTROL_SET_RATE ||
1251 i_ct == INPUT_CONTROL_SET_POSITION ||
1252 i_ct == INPUT_CONTROL_SET_TIME ||
1253 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1254 i_ct == INPUT_CONTROL_SET_TITLE ||
1255 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1256 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1259 // msg_Dbg( p_input, "merged at %d", i );
1260 /* Remove the i-1 */
1261 for( j = i; j < p_input->i_control; j++ )
1262 p_input->control[j-1] = p_input->control[j];
1263 p_input->i_control--;
1267 /* TODO but that's not that important
1268 - merge SET_X with SET_X_CMD
1269 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1270 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1277 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1280 vlc_bool_t b_force_update = VLC_FALSE;
1282 if( !p_input ) return b_force_update;
1286 case INPUT_CONTROL_SET_DIE:
1287 msg_Dbg( p_input, "control: stopping input" );
1288 /* Mark all submodules to die */
1289 if( p_input->input.p_access )
1290 p_input->input.p_access->b_die = VLC_TRUE;
1291 if( p_input->input.p_stream )
1292 p_input->input.p_stream->b_die = VLC_TRUE;
1293 p_input->input.p_demux->b_die = VLC_TRUE;
1295 p_input->b_die = VLC_TRUE;
1298 case INPUT_CONTROL_SET_POSITION:
1299 case INPUT_CONTROL_SET_POSITION_OFFSET:
1302 if( i_type == INPUT_CONTROL_SET_POSITION )
1304 f_pos = val.f_float;
1308 /* Should not fail */
1309 demux2_Control( p_input->input.p_demux,
1310 DEMUX_GET_POSITION, &f_pos );
1311 f_pos += val.f_float;
1313 if( f_pos < 0.0 ) f_pos = 0.0;
1314 if( f_pos > 1.0 ) f_pos = 1.0;
1315 /* Reset the decoders states and clock sync (before calling the demuxer */
1316 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1317 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1318 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1321 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1322 "%2.1f%% failed", f_pos * 100 );
1326 if( p_input->i_slave > 0 )
1327 SlaveSeek( p_input );
1329 b_force_update = VLC_TRUE;
1334 case INPUT_CONTROL_SET_TIME:
1335 case INPUT_CONTROL_SET_TIME_OFFSET:
1340 if( i_type == INPUT_CONTROL_SET_TIME )
1342 i_time = val.i_time;
1346 /* Should not fail */
1347 demux2_Control( p_input->input.p_demux,
1348 DEMUX_GET_TIME, &i_time );
1349 i_time += val.i_time;
1351 if( i_time < 0 ) i_time = 0;
1353 /* Reset the decoders states and clock sync (before calling the demuxer */
1354 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1355 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1357 i_ret = demux2_Control( p_input->input.p_demux,
1358 DEMUX_SET_TIME, i_time );
1363 /* Emulate it with a SET_POS */
1364 demux2_Control( p_input->input.p_demux,
1365 DEMUX_GET_LENGTH, &i_length );
1368 double f_pos = (double)i_time / (double)i_length;
1369 i_ret = demux2_Control( p_input->input.p_demux,
1370 DEMUX_SET_POSITION, f_pos );
1375 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1376 " failed or not possible", i_time );
1380 if( p_input->i_slave > 0 )
1381 SlaveSeek( p_input );
1383 b_force_update = VLC_TRUE;
1388 case INPUT_CONTROL_SET_STATE:
1389 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1390 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1393 if( p_input->input.p_access )
1394 i_ret = access2_Control( p_input->input.p_access,
1395 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1397 i_ret = demux2_Control( p_input->input.p_demux,
1398 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1402 /* FIXME What to do ? */
1403 msg_Warn( p_input, "cannot unset pause -> EOF" );
1404 vlc_mutex_unlock( &p_input->lock_control );
1405 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1406 vlc_mutex_lock( &p_input->lock_control );
1409 b_force_update = VLC_TRUE;
1411 /* Switch to play */
1412 p_input->i_state = PLAYING_S;
1413 val.i_int = PLAYING_S;
1414 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1417 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1418 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1420 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1421 p_input->b_can_pause )
1424 if( p_input->input.p_access )
1425 i_ret = access2_Control( p_input->input.p_access,
1426 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1428 i_ret = demux2_Control( p_input->input.p_demux,
1429 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1431 b_force_update = VLC_TRUE;
1435 msg_Warn( p_input, "cannot set pause state" );
1436 val.i_int = p_input->i_state;
1440 val.i_int = PAUSE_S;
1443 /* Switch to new state */
1444 p_input->i_state = val.i_int;
1445 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1447 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1449 b_force_update = VLC_TRUE;
1451 /* Correct "state" value */
1452 val.i_int = p_input->i_state;
1453 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1455 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1457 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1461 case INPUT_CONTROL_SET_RATE:
1462 case INPUT_CONTROL_SET_RATE_SLOWER:
1463 case INPUT_CONTROL_SET_RATE_FASTER:
1467 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1468 i_rate = p_input->i_rate * 2;
1469 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1470 i_rate = p_input->i_rate / 2;
1474 if( i_rate < INPUT_RATE_MIN )
1476 msg_Dbg( p_input, "cannot set rate faster" );
1477 i_rate = INPUT_RATE_MIN;
1479 else if( i_rate > INPUT_RATE_MAX )
1481 msg_Dbg( p_input, "cannot set rate slower" );
1482 i_rate = INPUT_RATE_MAX;
1484 if( i_rate != INPUT_RATE_DEFAULT &&
1485 ( !p_input->b_can_pace_control ||
1486 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1488 msg_Dbg( p_input, "cannot change rate" );
1489 i_rate = INPUT_RATE_DEFAULT;
1491 if( i_rate != p_input->i_rate )
1493 p_input->i_rate = i_rate;
1495 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1497 /* We haven't send data to decoder when rate != default */
1498 if( i_rate == INPUT_RATE_DEFAULT )
1499 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1502 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1504 b_force_update = VLC_TRUE;
1509 case INPUT_CONTROL_SET_PROGRAM:
1510 /* No need to force update, es_out does it if needed */
1511 es_out_Control( p_input->p_es_out,
1512 ES_OUT_SET_GROUP, val.i_int );
1514 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1518 case INPUT_CONTROL_SET_ES:
1519 /* No need to force update, es_out does it if needed */
1520 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1521 input_EsOutGetFromID( p_input->p_es_out,
1525 case INPUT_CONTROL_SET_AUDIO_DELAY:
1526 input_EsOutSetDelay( p_input->p_es_out,
1527 AUDIO_ES, val.i_time );
1528 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1531 case INPUT_CONTROL_SET_SPU_DELAY:
1532 input_EsOutSetDelay( p_input->p_es_out,
1533 SPU_ES, val.i_time );
1534 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1537 case INPUT_CONTROL_SET_TITLE:
1538 case INPUT_CONTROL_SET_TITLE_NEXT:
1539 case INPUT_CONTROL_SET_TITLE_PREV:
1540 if( p_input->input.b_title_demux &&
1541 p_input->input.i_title > 0 )
1544 /* FIXME handle demux title */
1545 demux_t *p_demux = p_input->input.p_demux;
1548 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1549 i_title = p_demux->info.i_title - 1;
1550 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1551 i_title = p_demux->info.i_title + 1;
1553 i_title = val.i_int;
1555 if( i_title >= 0 && i_title < p_input->input.i_title )
1557 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1558 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1560 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1561 input_ControlVarTitle( p_input, i_title );
1564 else if( p_input->input.i_title > 0 )
1566 access_t *p_access = p_input->input.p_access;
1569 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1570 i_title = p_access->info.i_title - 1;
1571 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1572 i_title = p_access->info.i_title + 1;
1574 i_title = val.i_int;
1576 if( i_title >= 0 && i_title < p_input->input.i_title )
1578 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1579 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1581 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1582 stream_AccessReset( p_input->input.p_stream );
1586 case INPUT_CONTROL_SET_SEEKPOINT:
1587 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1588 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1589 if( p_input->input.b_title_demux &&
1590 p_input->input.i_title > 0 )
1592 demux_t *p_demux = p_input->input.p_demux;
1594 int64_t i_input_time;
1595 int64_t i_seekpoint_time;
1597 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1599 i_seekpoint = p_demux->info.i_seekpoint;
1600 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1601 if( i_seekpoint_time >= 0 &&
1602 !demux2_Control( p_demux,
1603 DEMUX_GET_TIME, &i_input_time ) )
1605 if ( i_input_time < i_seekpoint_time + 3000000 )
1611 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1612 i_seekpoint = p_demux->info.i_seekpoint + 1;
1614 i_seekpoint = val.i_int;
1616 if( i_seekpoint >= 0 && i_seekpoint <
1617 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1619 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1620 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1622 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1625 else if( p_input->input.i_title > 0 )
1627 demux_t *p_demux = p_input->input.p_demux;
1628 access_t *p_access = p_input->input.p_access;
1630 int64_t i_input_time;
1631 int64_t i_seekpoint_time;
1633 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1635 i_seekpoint = p_access->info.i_seekpoint;
1636 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1637 if( i_seekpoint_time >= 0 &&
1638 demux2_Control( p_demux,
1639 DEMUX_GET_TIME, &i_input_time ) )
1641 if ( i_input_time < i_seekpoint_time + 3000000 )
1647 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1648 i_seekpoint = p_access->info.i_seekpoint + 1;
1650 i_seekpoint = val.i_int;
1652 if( i_seekpoint >= 0 && i_seekpoint <
1653 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1655 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1656 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1658 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1660 stream_AccessReset( p_input->input.p_stream );
1665 case INPUT_CONTROL_ADD_SLAVE:
1666 if( val.psz_string )
1668 input_source_t *slave = InputSourceNew( p_input );
1670 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1673 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1677 msg_Dbg( p_input, "adding %s as slave on the fly",
1681 if( demux2_Control( p_input->input.p_demux,
1682 DEMUX_GET_TIME, &i_time ) )
1684 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1685 InputSourceClean( p_input, slave );
1689 if( demux2_Control( slave->p_demux,
1690 DEMUX_SET_TIME, i_time ) )
1692 msg_Err( p_input, "seek failed for new slave" );
1693 InputSourceClean( p_input, slave );
1698 /* Get meta (access and demux) */
1699 access2_Control( slave->p_access, ACCESS_GET_META,
1701 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1702 UpdateMeta( p_input, VLC_FALSE );
1704 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1708 msg_Warn( p_input, "failed to add %s as slave",
1712 free( val.psz_string );
1716 case INPUT_CONTROL_SET_BOOKMARK:
1718 msg_Err( p_input, "not yet implemented" );
1722 return b_force_update;
1725 /*****************************************************************************
1727 *****************************************************************************/
1728 static int UpdateFromDemux( input_thread_t *p_input )
1730 demux_t *p_demux = p_input->input.p_demux;
1733 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1735 v.i_int = p_demux->info.i_title;
1736 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1738 input_ControlVarTitle( p_input, p_demux->info.i_title );
1740 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1742 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1744 v.i_int = p_demux->info.i_seekpoint;
1745 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1747 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1749 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1751 /* Hmmm only works with master input */
1752 if( p_input->input.p_demux == p_demux )
1754 int i_title_end = p_input->input.i_title_end -
1755 p_input->input.i_title_offset;
1756 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1757 p_input->input.i_seekpoint_offset;
1759 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1761 if( p_demux->info.i_title > i_title_end ||
1762 ( p_demux->info.i_title == i_title_end &&
1763 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1765 else if( i_seekpoint_end >=0 )
1767 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1769 else if( i_title_end >= 0 )
1771 if( p_demux->info.i_title > i_title_end ) return 0;
1778 /*****************************************************************************
1780 *****************************************************************************/
1781 static int UpdateFromAccess( input_thread_t *p_input )
1783 access_t *p_access = p_input->input.p_access;
1786 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1788 v.i_int = p_access->info.i_title;
1789 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1791 input_ControlVarTitle( p_input, p_access->info.i_title );
1793 stream_AccessUpdate( p_input->input.p_stream );
1795 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1797 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1799 v.i_int = p_access->info.i_seekpoint;
1800 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1801 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1803 if( p_access->info.i_update & INPUT_UPDATE_META )
1805 /* TODO maybe multi - access ? */
1806 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1807 access2_Control( p_input->input.p_access,ACCESS_GET_META, p_meta );
1808 UpdateMeta( p_input, VLC_FALSE );
1809 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1810 p_access->info.i_update &= ~INPUT_UPDATE_META;
1813 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1815 /* Hmmm only works with master input */
1816 if( p_input->input.p_access == p_access )
1818 int i_title_end = p_input->input.i_title_end -
1819 p_input->input.i_title_offset;
1820 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1821 p_input->input.i_seekpoint_offset;
1823 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1825 if( p_access->info.i_title > i_title_end ||
1826 ( p_access->info.i_title == i_title_end &&
1827 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1829 else if( i_seekpoint_end >=0 )
1831 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1833 else if( i_title_end >= 0 )
1835 if( p_access->info.i_title > i_title_end ) return 0;
1842 /*****************************************************************************
1844 *****************************************************************************/
1845 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1847 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1851 if( p_meta->psz_title && !p_input->input.p_item->b_fixed_name )
1852 input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1854 /** \todo handle sout meta */
1859 /*****************************************************************************
1861 *****************************************************************************/
1862 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1863 vlc_bool_t b_quick )
1865 char psz_buffer[MSTRTIME_MAX_SIZE];
1867 vlc_mutex_lock( &p_input->input.p_item->lock );
1868 p_input->input.p_item->i_duration = i_length;
1869 vlc_mutex_unlock( &p_input->input.p_item->lock );
1871 pl_Yield( p_input );
1872 var_SetInteger( pl_Get( p_input ), "item-change",
1873 p_input->input.p_item->i_id );
1874 pl_Release( p_input )
1876 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1877 msecstotimestr( psz_buffer, i_length / 1000 ) );
1880 /*****************************************************************************
1882 *****************************************************************************/
1883 static input_source_t *InputSourceNew( input_thread_t *p_input )
1885 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1889 msg_Err( p_input, "out of memory for new input source" );
1894 in->p_access = NULL;
1895 in->p_stream = NULL;
1897 in->b_title_demux = VLC_FALSE;
1900 in->b_can_pace_control = VLC_TRUE;
1901 in->b_eof = VLC_FALSE;
1902 in->i_cr_average = 0;
1907 /*****************************************************************************
1909 *****************************************************************************/
1910 static int InputSourceInit( input_thread_t *p_input,
1911 input_source_t *in, char *psz_mrl,
1912 char *psz_forced_demux, vlc_bool_t b_quick )
1914 char *psz_dup = strdup( psz_mrl );
1922 if( !in ) return VLC_EGENERIC;
1927 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1928 &psz_access, &psz_demux, &psz_path );
1930 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1931 psz_mrl, psz_access, psz_demux, psz_path );
1933 /* Hack to allow udp://@:port syntax */
1935 (strncmp( psz_access, "udp", 3 ) &&
1936 strncmp( psz_access, "rtp", 3 )) )
1938 /* Find optional titles and seekpoints */
1939 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1940 &in->i_seekpoint_start, &in->i_seekpoint_end );
1942 if( psz_forced_demux && *psz_forced_demux )
1944 psz_demux = psz_forced_demux;
1946 else if( !psz_demux || *psz_demux == '\0' )
1948 /* special hack for forcing a demuxer with --demux=module
1949 * (and do nothing with a list) */
1950 char *psz_var_demux = var_GetString( p_input, "demux" );
1952 if( *psz_var_demux != '\0' &&
1953 !strchr(psz_var_demux, ',' ) &&
1954 !strchr(psz_var_demux, ':' ) )
1956 psz_demux = psz_var_demux;
1958 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
1960 else if( psz_var_demux )
1962 free( psz_var_demux );
1966 /* Try access_demux if no demux given */
1967 if( *psz_demux == '\0' )
1969 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1970 NULL, p_input->p_es_out, VLC_FALSE );
1976 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
1977 psz_demux = strdup( "" );
1978 psz_access = strdup( "file" );
1983 int64_t i_pts_delay;
1985 /* Get infos from access_demux */
1986 demux2_Control( in->p_demux,
1987 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1988 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1990 in->b_title_demux = VLC_TRUE;
1991 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1992 &in->title, &in->i_title,
1993 &in->i_title_offset, &in->i_seekpoint_offset ) )
1998 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1999 &in->b_can_pace_control );
2000 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2004 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2010 int64_t i_pts_delay;
2012 input_ChangeState( p_input, OPENING_S);
2014 /* Now try a real access */
2015 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2018 /* Access failed, URL encoded ? */
2019 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2021 DecodeUrl( psz_path );
2023 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2024 psz_access, psz_demux, psz_path );
2026 in->p_access = access2_New( p_input,
2027 psz_access, psz_demux, psz_path,
2030 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2031 * are forbidden in filenames on Win32. */
2033 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2034 if( in->p_access == NULL &&
2035 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2037 if( psz_dup ) free( psz_dup );
2038 psz_dup = strdup( psz_mrl );
2040 if( psz_forced_demux && *psz_forced_demux )
2042 psz_demux = psz_forced_demux;
2044 else psz_demux = "";
2047 in->p_access = access2_New( p_input,
2048 psz_access, psz_demux, psz_path,
2053 if( in->p_access == NULL )
2055 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2056 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2057 _("Your input can't be opened"),
2058 _("VLC is unable to open the MRL '%s'."
2059 " Check the log for details."), psz_mrl );
2064 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2065 while( psz && *psz )
2067 access_t *p_access = in->p_access;
2068 char *end = strchr( psz, ':' );
2073 in->p_access = access2_FilterNew( in->p_access, psz );
2074 if( in->p_access == NULL )
2076 in->p_access = p_access;
2077 msg_Warn( p_input, "failed to insert access filter %s",
2083 if( psz_tmp ) free( psz_tmp );
2085 /* Get infos from access */
2088 access2_Control( in->p_access,
2089 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2090 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2092 in->b_title_demux = VLC_FALSE;
2093 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2094 &in->title, &in->i_title,
2095 &in->i_title_offset, &in->i_seekpoint_offset ) )
2101 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2102 &in->b_can_pace_control );
2103 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2105 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2107 var_Set( p_input, "seekable", val );
2110 input_ChangeState( p_input, BUFFERING_S);
2112 /* Create the stream_t */
2113 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2114 if( in->p_stream == NULL )
2116 msg_Warn( p_input, "cannot create a stream_t from access" );
2120 /* Open a demuxer */
2121 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2123 psz_demux = in->p_access->psz_demux;
2125 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2126 in->p_stream, p_input->p_es_out, b_quick );
2127 if( in->p_demux == NULL )
2129 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2130 psz_access, psz_demux, psz_path );
2131 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2132 _("Can't recognize the input's format"),
2133 _("The format of '%s' can't be detected. "
2134 "Have a look the log for details."), psz_mrl );
2138 /* TODO get title from demux */
2139 if( !b_quick && in->i_title <= 0 )
2141 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2142 &in->title, &in->i_title,
2143 &in->i_title_offset, &in->i_seekpoint_offset ))
2150 in->b_title_demux = VLC_TRUE;
2155 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2156 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2158 if( psz_dup ) free( psz_dup );
2163 demux2_Delete( in->p_demux );
2166 stream_Delete( in->p_stream );
2169 access2_Delete( in->p_access );
2170 if( psz_dup ) free( psz_dup );
2172 return VLC_EGENERIC;
2175 /*****************************************************************************
2177 *****************************************************************************/
2178 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2181 demux2_Delete( in->p_demux );
2184 stream_Delete( in->p_stream );
2187 access2_Delete( in->p_access );
2189 if( in->i_title > 0 )
2192 for( i = 0; i < in->i_title; i++ )
2194 vlc_input_title_Delete( in->title[i] );
2200 static void SlaveDemux( input_thread_t *p_input )
2205 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2207 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2211 for( i = 0; i < p_input->i_slave; i++ )
2213 input_source_t *in = p_input->slave[i];
2219 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2224 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2226 msg_Err( p_input, "slave[%d] doesn't like "
2227 "DEMUX_GET_TIME -> EOF", i );
2232 if( i_stime >= i_time )
2235 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2241 i_ret = in->p_demux->pf_demux( in->p_demux );
2246 msg_Dbg( p_input, "slave %d EOF", i );
2247 in->b_eof = VLC_TRUE;
2252 static void SlaveSeek( input_thread_t *p_input )
2257 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2259 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2263 for( i = 0; i < p_input->i_slave; i++ )
2265 input_source_t *in = p_input->slave[i];
2267 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2269 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2270 in->b_eof = VLC_TRUE;
2274 /*****************************************************************************
2276 *****************************************************************************/
2277 static void InputMetaUser( input_thread_t *p_input )
2279 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
2282 if( !p_meta ) return;
2284 /* Get meta information from user */
2285 #define GET_META( field, s ) \
2286 var_Get( p_input, (s), &val ); \
2287 if( *val.psz_string ) { \
2288 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2289 p_meta->psz_ ## field = strdup( val.psz_string ); \
2291 free( val.psz_string )
2293 GET_META( title, "meta-title" );
2294 GET_META( author, "meta-author" );
2295 GET_META( artist, "meta-artist" );
2296 GET_META( genre, "meta-genre" );
2297 GET_META( copyright, "meta-copyright" );
2298 GET_META( description, "meta-description" );
2299 GET_META( date, "meta-date" );
2300 GET_META( url, "meta-url" );
2304 /*****************************************************************************
2305 * DecodeUrl: decode a given encoded url
2306 *****************************************************************************/
2307 static void DecodeUrl( char *psz )
2309 char *dup = strdup( psz );
2327 *psz++ = strtol( val, NULL, 16 );
2329 else if( *p == '+' )
2339 if( psz ) *psz++ ='\0';
2340 if( dup ) free( dup );
2343 /*****************************************************************************
2344 * MRLSplit: parse the access, demux and url part of the
2345 * Media Resource Locator.
2346 *****************************************************************************/
2347 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2348 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2350 char *psz_access = NULL;
2351 char *psz_demux = NULL;
2352 char *psz_path = NULL;
2353 char *psz, *psz_check;
2355 psz = strchr( psz_dup, ':' );
2357 /* '@' not allowed in access/demux part */
2358 psz_check = strchr( psz_dup, '@' );
2359 if( psz_check && psz_check < psz ) psz = 0;
2361 #if defined( WIN32 ) || defined( UNDER_CE )
2362 if( psz - psz_dup == 1 )
2364 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2373 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2377 psz = strchr( psz_dup, '/' );
2384 psz_access = psz_dup;
2391 if( !psz_access ) *ppsz_access = "";
2392 else *ppsz_access = psz_access;
2394 if( !psz_demux ) *ppsz_demux = "";
2395 else *ppsz_demux = psz_demux;
2397 if( !psz_path ) *ppsz_path = "";
2398 else *ppsz_path = psz_path;
2401 /*****************************************************************************
2402 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2405 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2406 *****************************************************************************/
2407 static void MRLSections( input_thread_t *p_input, char *psz_source,
2408 int *pi_title_start, int *pi_title_end,
2409 int *pi_chapter_start, int *pi_chapter_end )
2411 char *psz, *psz_end, *psz_next, *psz_check;
2413 *pi_title_start = *pi_title_end = -1;
2414 *pi_chapter_start = *pi_chapter_end = -1;
2416 /* Start by parsing titles and chapters */
2417 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2419 /* Check we are really dealing with a title/chapter section */
2420 psz_check = psz + 1;
2421 if( !*psz_check ) return;
2422 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2423 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2424 if( *psz_check == ':' && ++psz_check )
2425 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2426 if( *psz_check != '-' && *psz_check ) return;
2427 if( *psz_check == '-' && ++psz_check )
2428 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2429 if( *psz_check != ':' && *psz_check ) return;
2430 if( *psz_check == ':' && ++psz_check )
2431 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2432 if( *psz_check ) return;
2434 /* Separate start and end */
2436 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2438 /* Look for the start title */
2439 *pi_title_start = strtol( psz, &psz_next, 0 );
2440 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2441 *pi_title_end = *pi_title_start;
2444 /* Look for the start chapter */
2446 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2447 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2448 *pi_chapter_end = *pi_chapter_start;
2452 /* Look for the end title */
2453 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2454 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2457 /* Look for the end chapter */
2458 if( *psz_end ) psz_end++;
2459 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2460 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2463 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2464 psz_source, *pi_title_start, *pi_chapter_start,
2465 *pi_title_end, *pi_chapter_end );
2468 /*****************************************************************************
2469 * input_AddSubtitles: add a subtitles file and enable it
2470 *****************************************************************************/
2471 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2472 vlc_bool_t b_check_extension )
2474 input_source_t *sub;
2477 char *psz_path, *psz_extension;
2479 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2484 /* if we are provided a subtitle.sub file,
2485 * see if we don't have a subtitle.idx and use it instead */
2486 psz_path = strdup( psz_subtitle );
2489 psz_extension = strrchr( psz_path, '.');
2490 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2494 strcpy( psz_extension, ".idx" );
2495 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2496 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2499 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2500 psz_path, psz_subtitle );
2501 strcpy( psz_subtitle, psz_path );
2507 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2509 sub = InputSourceNew( p_input );
2510 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle", VLC_FALSE ) )
2512 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
2515 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2517 if( count.i_int == 0 )
2519 /* if it was first one, there is disable too */
2521 if( count.i_int < list.p_list->i_count )
2523 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2524 &list.p_list->p_values[count.i_int] );
2526 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2534 # define MAX_PATH 250
2536 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item )
2542 int i_status = VLC_EGENERIC;
2547 || !p_item->p_meta->psz_arturl
2548 || !*p_item->p_meta->psz_arturl )
2550 return VLC_EGENERIC;
2552 if( !strncmp( p_item->p_meta->psz_arturl, "file", 4 ) )
2557 psz_artist = p_item->p_meta->psz_artist;
2558 psz_album = p_item->p_meta->psz_album;
2559 psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
2560 psz_filename = (char *)malloc( MAX_PATH );
2563 snprintf( psz_filename, MAX_PATH,
2564 "file://%s/" CONFIG_DIR "/art/%s/%s/art%s",
2565 p_parent->p_libvlc->psz_homedir,
2566 psz_artist, psz_album, psz_type );
2567 msg_Dbg( p_parent, "Saving album art to %s", psz_filename );
2569 /* Check if file exists */
2570 i_ret = utf8_stat( psz_filename+7, &a );
2573 msg_Dbg( p_parent, "Album art %s already exists", psz_filename );
2577 //if( i_ret == -1 && errno == ENOTDIR )
2579 /* GRUIKKKKKKKKKK (make sure that all the directories exist) */
2580 char *psz_dir = malloc( MAX_PATH );
2581 snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR,
2582 p_parent->p_libvlc->psz_homedir );
2583 utf8_mkdir( psz_dir );
2584 snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR "/art",
2585 p_parent->p_libvlc->psz_homedir );
2586 utf8_mkdir( psz_dir );
2587 snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR "/art/%s",
2588 p_parent->p_libvlc->psz_homedir, psz_artist );
2589 utf8_mkdir( psz_dir );
2590 snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR "/art/%s/%s",
2591 p_parent->p_libvlc->psz_homedir,
2592 psz_artist, psz_album );
2593 utf8_mkdir( psz_dir );
2597 stream_t *p_stream = stream_UrlNew( p_parent,
2598 p_item->p_meta->psz_arturl );
2602 void *p_buffer = malloc( 1<<16 );
2604 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
2605 while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
2607 fwrite( p_buffer, l_read, 1, p_file );
2611 stream_Delete( p_stream );
2612 msg_Dbg( p_parent, "Album art saved to %s\n", psz_filename );
2613 free( p_item->p_meta->psz_arturl );
2614 p_item->p_meta->psz_arturl = strdup( psz_filename );
2615 i_status = VLC_SUCCESS;
2618 free( psz_filename );