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 );
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 * );
71 static void UpdateItemLength( input_thread_t *, int64_t i_length );
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 );
79 static void InputSourceClean( input_thread_t *, input_source_t * );
81 static void SlaveDemux( input_thread_t *p_input );
82 static void SlaveSeek( input_thread_t *p_input );
84 static void InputMetaUser( input_thread_t *p_input );
86 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item );
88 /*****************************************************************************
89 * This function creates a new input, and returns a pointer
90 * to its description. On error, it returns NULL.
92 * Variables for _public_ use:
95 * - rate,rate-slower, rate-faster
96 * - position, position-offset
98 * - title,title-next,title-prev
99 * - chapter,chapter-next, chapter-prev
100 * - program, audio-es, video-es, spu-es
101 * - audio-delay, spu-delay
106 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
107 * * For intf callback upon changes
109 * TODO explain when Callback is called
110 * TODO complete this list (?)
111 *****************************************************************************/
112 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
113 char *psz_header, vlc_bool_t b_quick )
115 input_thread_t *p_input = NULL; /* thread descriptor */
119 /* Allocate descriptor */
120 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
121 if( p_input == NULL )
123 msg_Err( p_parent, "out of memory" );
126 p_input->b_preparsing = b_quick;
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" );
188 if( !p_input->b_preparsing )
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 );
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 p_me->i_optional = 0;
367 if( var_CreateGetInteger( p_parent, "album-art" ) != ALBUM_ART_NEVER )
369 p_me->i_mandatory |= VLC_META_ENGINE_ART_URL;
373 p_me->i_optional |= VLC_META_ENGINE_ART_URL;
375 p_me->p_item = p_item;
376 p_me->p_module = module_Need( p_me, "meta engine", 0, VLC_FALSE );
379 if( !p_me->p_module )
381 msg_Err( p_parent, "no suitable meta engine module" );
382 vlc_object_detach( p_me );
383 vlc_object_destroy( p_me );
387 module_Unneed( p_me, p_me->p_module );
389 vlc_object_destroy( p_me );
395 * Request a running input thread to stop and die
397 * \param the input thread to stop
399 void input_StopThread( input_thread_t *p_input )
404 /* Set die for input */
405 p_input->b_die = VLC_TRUE;
407 /* We cannot touch p_input fields directly (we can from another thread),
408 * so use the vlc_object_find way, it's perfectly safe */
410 /* Set die for all access */
411 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
412 for( i = 0; i < p_list->i_count; i++ )
414 p_list->p_values[i].p_object->b_die = VLC_TRUE;
416 vlc_list_release( p_list );
418 /* Set die for all stream */
419 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
420 for( i = 0; i < p_list->i_count; i++ )
422 p_list->p_values[i].p_object->b_die = VLC_TRUE;
424 vlc_list_release( p_list );
426 /* Set die for all demux */
427 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
428 for( i = 0; i < p_list->i_count; i++ )
430 p_list->p_values[i].p_object->b_die = VLC_TRUE;
432 vlc_list_release( p_list );
434 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
438 * Clean up a dead input thread
439 * This function does not return until the thread is effectively cancelled.
441 * \param the input thread to kill
443 void input_DestroyThread( input_thread_t *p_input )
445 /* Join the thread */
446 vlc_thread_join( p_input );
448 /* Delete input lock (only after thread joined) */
449 vlc_mutex_destroy( &p_input->lock_control );
451 /* TODO: maybe input_DestroyThread should also delete p_input instead
452 * of the playlist but I'm not sure if it's possible */
455 /*****************************************************************************
456 * Run: main thread loop
457 * This is the "normal" thread that spawns the input processing chain,
458 * reads the stream, cleans up and waits
459 *****************************************************************************/
460 static int Run( input_thread_t *p_input )
462 /* Signal that the thread is launched */
463 vlc_thread_ready( p_input );
465 if( Init( p_input ) )
467 /* If we failed, wait before we are killed, and exit */
468 p_input->b_error = VLC_TRUE;
472 /* Tell we're dead */
473 p_input->b_dead = VLC_TRUE;
480 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
482 /* We have finish to demux data but not to play them */
483 while( !p_input->b_die )
485 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
488 msg_Dbg( p_input, "waiting decoder fifos to empty" );
490 msleep( INPUT_IDLE_SLEEP );
493 /* We have finished */
494 p_input->b_eof = VLC_TRUE;
497 /* Wait until we are asked to die */
498 if( !p_input->b_die )
509 /*****************************************************************************
510 * RunAndClean: main thread loop
511 * This is the "just forget me" thread that spawns the input processing chain,
512 * reads the stream, cleans up and releases memory
513 *****************************************************************************/
514 static int RunAndClean( input_thread_t *p_input )
516 /* Signal that the thread is launched */
517 vlc_thread_ready( p_input );
519 if( Init( p_input ) )
521 /* If we failed, just exit */
527 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
529 /* We have finished demuxing data but not playing it */
530 while( !p_input->b_die )
532 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
535 msg_Dbg( p_input, "waiting decoder fifos to empty" );
537 msleep( INPUT_IDLE_SLEEP );
540 /* We have finished */
541 p_input->b_eof = VLC_TRUE;
548 vlc_object_detach( p_input );
549 vlc_object_destroy( p_input );
555 /*****************************************************************************
556 * Main loop: Fill buffers from access, and demux
557 *****************************************************************************/
558 static void MainLoop( input_thread_t *p_input )
560 int64_t i_intf_update = 0;
561 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
563 vlc_bool_t b_force_update = VLC_FALSE;
569 if( p_input->i_state != PAUSE_S )
571 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
572 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
579 if( p_input->input.b_title_demux &&
580 p_input->input.p_demux->info.i_update )
582 i_ret = UpdateFromDemux( p_input );
583 b_force_update = VLC_TRUE;
585 else if( !p_input->input.b_title_demux &&
586 p_input->input.p_access &&
587 p_input->input.p_access->info.i_update )
589 i_ret = UpdateFromAccess( p_input );
590 b_force_update = VLC_TRUE;
594 if( i_ret == 0 ) /* EOF */
598 var_Get( p_input, "input-repeat", &repeat );
599 if( repeat.i_int == 0 )
601 /* End of file - we do not set b_die because only the
602 * playlist is allowed to do so. */
603 msg_Dbg( p_input, "EOF reached" );
604 p_input->input.b_eof = VLC_TRUE;
608 msg_Dbg( p_input, "repeating the same input (%d)",
610 if( repeat.i_int > 0 )
613 var_Set( p_input, "input-repeat", repeat );
616 /* Seek to start title/seekpoint */
617 val.i_int = p_input->input.i_title_start -
618 p_input->input.i_title_offset;
619 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
621 input_ControlPush( p_input,
622 INPUT_CONTROL_SET_TITLE, &val );
624 val.i_int = p_input->input.i_seekpoint_start -
625 p_input->input.i_seekpoint_offset;
626 if( val.i_int > 0 /* TODO: check upper boundary */ )
627 input_ControlPush( p_input,
628 INPUT_CONTROL_SET_SEEKPOINT, &val );
630 /* Seek to start position */
631 if( p_input->i_start > 0 )
633 val.i_time = p_input->i_start;
634 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
640 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
647 p_input->b_error = VLC_TRUE;
650 if( i_ret > 0 && p_input->i_slave > 0 )
652 SlaveDemux( p_input );
662 vlc_mutex_lock( &p_input->lock_control );
663 ControlReduce( p_input );
664 while( !ControlPopNoLock( p_input, &i_type, &val ) )
666 msg_Dbg( p_input, "control type=%d", i_type );
667 if( Control( p_input, i_type, val ) )
668 b_force_update = VLC_TRUE;
670 vlc_mutex_unlock( &p_input->lock_control );
672 if( b_force_update || i_intf_update < mdate() )
676 int64_t i_time, i_length;
677 /* update input status variables */
678 if( !demux2_Control( p_input->input.p_demux,
679 DEMUX_GET_POSITION, &f_pos ) )
681 val.f_float = (float)f_pos;
682 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
684 if( !demux2_Control( p_input->input.p_demux,
685 DEMUX_GET_TIME, &i_time ) )
687 p_input->i_time = i_time;
689 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
691 if( !demux2_Control( p_input->input.p_demux,
692 DEMUX_GET_LENGTH, &i_length ) )
695 var_Get( p_input, "length", &old_val );
696 val.i_time = i_length;
697 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
699 if( old_val.i_time != val.i_time )
701 UpdateItemLength( p_input, i_length );
705 var_SetBool( p_input, "intf-change", VLC_TRUE );
706 i_intf_update = mdate() + I64C(150000);
712 static int Init( input_thread_t * p_input )
722 /* Initialize optional stream output. (before access/demuxer)
723 * XXX: we add a special case if the uri starts by vlc.
724 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
725 * be destroyed in case of a file).
726 * (this will break playing of file starting by 'vlc:' but I don't
727 * want to add more logic, just force file by file:// or code it ;)
729 memset( &p_input->counters, 0, sizeof( p_input->counters ) );
730 if( !p_input->b_preparsing )
732 /* Prepare statistics */
733 #define INIT_COUNTER( p, type, compute ) p_input->counters.p_##p = \
734 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
735 if( p_input->p_libvlc->b_stats )
737 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
738 INIT_COUNTER( read_packets, INTEGER, COUNTER );
739 INIT_COUNTER( demux_read, INTEGER, COUNTER );
740 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
741 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
742 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
743 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
744 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
745 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
746 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
747 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
748 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
749 p_input->counters.p_sout_send_bitrate = NULL;
750 p_input->counters.p_sout_sent_packets = NULL;
751 p_input->counters.p_sout_sent_bytes = NULL;
752 if( p_input->counters.p_demux_bitrate )
753 p_input->counters.p_demux_bitrate->update_interval = 1000000;
754 if( p_input->counters.p_input_bitrate )
755 p_input->counters.p_input_bitrate->update_interval = 1000000;
757 vlc_mutex_init( p_input, &p_input->counters.counters_lock );
760 psz = var_GetString( p_input, "sout" );
761 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
763 p_input->p_sout = sout_NewInstance( p_input, psz );
764 if( p_input->p_sout == NULL )
766 msg_Err( p_input, "cannot start stream output instance, " \
771 if( p_input->p_libvlc->b_stats )
773 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
774 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
775 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
776 if( p_input->counters.p_sout_send_bitrate )
777 p_input->counters.p_sout_send_bitrate->update_interval =
785 p_input->p_es_out = input_EsOutNew( p_input );
786 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
787 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
789 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
790 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
792 if( InputSourceInit( p_input, &p_input->input,
793 p_input->input.p_item->psz_uri, NULL ) )
798 /* Create global title (from master) */
799 if( !p_input->b_preparsing )
801 p_input->i_title = p_input->input.i_title;
802 p_input->title = p_input->input.title;
803 p_input->i_title_offset = p_input->input.i_title_offset;
804 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
805 if( p_input->i_title > 0 )
807 /* Setup variables */
808 input_ControlVarNavigation( p_input );
809 input_ControlVarTitle( p_input, 0 );
813 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
814 p_input->b_can_pause = p_input->input.b_can_pause;
817 if( p_input->i_pts_delay < 0 )
818 p_input->i_pts_delay = 0;
820 /* If the desynchronisation requested by the user is < 0, we need to
821 * cache more data. */
822 var_Get( p_input, "audio-desync", &val );
823 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
825 /* Update cr_average depending on the caching */
826 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
827 p_input->input.i_cr_average /= 10;
828 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
831 /* Load master infos */
833 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
834 &val.i_time ) && val.i_time > 0 )
836 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
837 UpdateItemLength( p_input, val.i_time );
838 p_input->input.p_item->i_duration = val.i_time;
841 /* Start title/chapter */
842 if( !p_input->b_preparsing )
844 val.i_int = p_input->input.i_title_start -
845 p_input->input.i_title_offset;
846 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
847 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
848 val.i_int = p_input->input.i_seekpoint_start -
849 p_input->input.i_seekpoint_offset;
850 if( val.i_int > 0 /* TODO: check upper boundary */ )
851 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
855 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
857 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
860 if( p_input->i_start > 0 )
862 if( p_input->i_start >= val.i_time )
864 msg_Warn( p_input, "invalid start-time ignored" );
870 msg_Dbg( p_input, "starting at time: %ds",
871 (int)( p_input->i_start / I64C(1000000) ) );
873 s.i_time = p_input->i_start;
874 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
877 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
879 msg_Warn( p_input, "invalid stop-time ignored" );
885 /* Get fps and set it if not already set */
886 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
889 float f_requested_fps;
891 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
892 var_SetFloat( p_input, "sub-original-fps", f_fps );
894 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
895 if( f_requested_fps != f_fps )
897 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
899 var_SetFloat( p_input, "sub-fps", f_requested_fps );
903 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
906 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
910 /* Look for and add subtitle files */
911 psz_subtitle = var_GetString( p_input, "sub-file" );
914 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
915 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
918 var_Get( p_input, "sub-autodetect-file", &val );
921 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
922 char **subs = subtitles_Detect( p_input, psz_autopath,
923 p_input->input.p_item->psz_uri );
928 /* Try to autoselect the first autodetected subtitles file
929 * if no subtitles file was specified */
930 if( *psz_subtitle == 0 && subs && subs[0] )
932 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
937 /* Then, just add the following subtitles files */
938 for( ; subs && subs[i]; i++ )
940 if( strcmp( psz_subtitle, subs[i] ) )
942 sub = InputSourceNew( p_input );
943 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
945 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
950 if( subs ) free( subs );
951 if( psz_autopath ) free( psz_autopath );
953 free( psz_subtitle );
956 psz = var_GetString( p_input, "input-slave" );
960 input_source_t *slave;
963 while( *psz == ' ' || *psz == '#' )
967 if( ( psz_delim = strchr( psz, '#' ) ) )
976 msg_Dbg( p_input, "adding slave input '%s'", psz );
977 slave = InputSourceNew( p_input );
978 if( !InputSourceInit( p_input, slave, psz, NULL ) )
980 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
985 if( psz ) free( psz );
989 p_input->i_start = 0;
990 p_input->i_start = 0;
994 if( !p_input->b_preparsing )
996 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
997 i_es_out_mode = ES_OUT_MODE_AUTO;
999 if( p_input->p_sout )
1001 var_Get( p_input, "sout-all", &val );
1004 i_es_out_mode = ES_OUT_MODE_ALL;
1009 var_Get( p_input, "programs", &val );
1010 if ( val.p_list && val.p_list->i_count )
1012 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1013 /* Note : we should remove the "program" callback. */
1016 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1020 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1022 /* Inform the demuxer about waited group (needed only for DVB) */
1023 if( i_es_out_mode == ES_OUT_MODE_ALL )
1025 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1027 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1029 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
1034 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
1035 (int) var_GetInteger( p_input, "program" ), NULL );
1038 if( p_input->p_sout )
1040 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
1042 p_input->b_out_pace_control = VLC_FALSE;
1046 p_input->b_out_pace_control = VLC_TRUE;
1049 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
1051 /* We don't want a high input priority here or we'll
1052 * end-up sucking up all the CPU time */
1053 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1056 msg_Dbg( p_input, "starting in %s mode",
1057 p_input->b_out_pace_control ? "async" : "sync" );
1061 p_meta = p_input->input.p_item->p_meta;
1062 /* Get meta data from users */
1063 InputMetaUser( p_input );
1064 /* Get meta data from master input */
1065 demux2_Control( p_input->input.p_demux, DEMUX_GET_META, p_meta );
1067 /* Access_file does not give any meta, and there are no slave */
1068 if( !p_input->b_preparsing )
1070 if( p_input->input.p_access )
1071 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1074 /* Get meta data from slave input */
1075 for( i = 0; i < p_input->i_slave; i++ )
1077 demux2_Control( p_input->slave[i]->p_demux,
1078 DEMUX_GET_META, p_meta );
1079 if( p_input->slave[i]->p_access )
1081 access2_Control( p_input->slave[i]->p_access,
1082 ACCESS_GET_META, p_meta );
1087 UpdateMeta( p_input );
1089 if( !p_input->b_preparsing )
1091 msg_Dbg( p_input, "`%s' successfully opened",
1092 p_input->input.p_item->psz_uri );
1096 /* initialization is complete */
1097 input_ChangeState(p_input, PLAYING_S);
1102 if( p_input->p_es_out )
1103 input_EsOutDelete( p_input->p_es_out );
1105 if( p_input->p_sout )
1106 sout_DeleteInstance( p_input->p_sout );
1108 /* Mark them deleted */
1109 p_input->input.p_demux = NULL;
1110 p_input->input.p_stream = NULL;
1111 p_input->input.p_access = NULL;
1112 p_input->p_es_out = NULL;
1113 p_input->p_sout = NULL;
1115 return VLC_EGENERIC;
1118 /*****************************************************************************
1119 * Error: RunThread() error loop
1120 *****************************************************************************
1121 * This function is called when an error occurred during thread main's loop.
1122 *****************************************************************************/
1123 static void Error( input_thread_t *p_input )
1125 while( !p_input->b_die )
1128 msleep( INPUT_IDLE_SLEEP );
1132 /*****************************************************************************
1133 * End: end the input thread
1134 *****************************************************************************/
1135 static void End( input_thread_t * p_input )
1139 msg_Dbg( p_input, "closing input" );
1141 /* We are at the end */
1142 input_ChangeState(p_input, END_S);
1144 /* Clean control variables */
1145 input_ControlVarClean( p_input );
1147 /* Clean up master */
1148 InputSourceClean( p_input, &p_input->input );
1151 for( i = 0; i < p_input->i_slave; i++ )
1153 InputSourceClean( p_input, p_input->slave[i] );
1154 free( p_input->slave[i] );
1156 if( p_input->slave ) free( p_input->slave );
1158 /* Unload all modules */
1159 if( p_input->p_es_out )
1160 input_EsOutDelete( p_input->p_es_out );
1162 #define CL_CO( c ) stats_CounterClean( p_input->counters.p_##c ); p_input->counters.p_##c = NULL;
1163 if( p_input->p_libvlc->b_stats )
1165 vlc_mutex_lock( &p_input->counters.counters_lock );
1166 CL_CO( read_bytes );
1167 CL_CO( read_packets );
1168 CL_CO( demux_read );
1169 CL_CO( input_bitrate );
1170 CL_CO( demux_bitrate );
1171 CL_CO( played_abuffers );
1172 CL_CO( lost_abuffers );
1173 CL_CO( displayed_pictures );
1174 CL_CO( lost_pictures );
1175 CL_CO( decoded_audio) ;
1176 CL_CO( decoded_video );
1177 CL_CO( decoded_sub) ;
1178 vlc_mutex_unlock( &p_input->counters.counters_lock );
1181 /* Close optional stream output instance */
1182 if( p_input->p_sout )
1186 vlc_mutex_lock( &p_input->counters.counters_lock );
1187 CL_CO( sout_sent_packets );
1188 CL_CO( sout_sent_bytes );
1189 CL_CO( sout_send_bitrate );
1190 vlc_mutex_unlock( &p_input->counters.counters_lock );
1192 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool )
1194 /* attach sout to the playlist */
1195 msg_Dbg( p_input, "keeping sout" );
1196 vlc_object_detach( p_input->p_sout );
1197 vlc_object_attach( p_input->p_sout, p_input->p_libvlc->p_playlist );
1201 msg_Dbg( p_input, "destroying sout" );
1202 sout_DeleteInstance( p_input->p_sout );
1207 /* Tell we're dead */
1208 p_input->b_dead = VLC_TRUE;
1211 /*****************************************************************************
1213 *****************************************************************************/
1214 static inline int ControlPopNoLock( input_thread_t *p_input,
1215 int *pi_type, vlc_value_t *p_val )
1217 if( p_input->i_control <= 0 )
1219 return VLC_EGENERIC;
1222 *pi_type = p_input->control[0].i_type;
1223 *p_val = p_input->control[0].val;
1225 p_input->i_control--;
1226 if( p_input->i_control > 0 )
1230 for( i = 0; i < p_input->i_control; i++ )
1232 p_input->control[i].i_type = p_input->control[i+1].i_type;
1233 p_input->control[i].val = p_input->control[i+1].val;
1240 static void ControlReduce( input_thread_t *p_input )
1243 for( i = 1; i < p_input->i_control; i++ )
1245 const int i_lt = p_input->control[i-1].i_type;
1246 const int i_ct = p_input->control[i].i_type;
1248 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1249 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1253 ( i_ct == INPUT_CONTROL_SET_STATE ||
1254 i_ct == INPUT_CONTROL_SET_RATE ||
1255 i_ct == INPUT_CONTROL_SET_POSITION ||
1256 i_ct == INPUT_CONTROL_SET_TIME ||
1257 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1258 i_ct == INPUT_CONTROL_SET_TITLE ||
1259 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1260 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1263 // msg_Dbg( p_input, "merged at %d", i );
1264 /* Remove the i-1 */
1265 for( j = i; j < p_input->i_control; j++ )
1266 p_input->control[j-1] = p_input->control[j];
1267 p_input->i_control--;
1271 /* TODO but that's not that important
1272 - merge SET_X with SET_X_CMD
1273 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1274 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1281 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1284 vlc_bool_t b_force_update = VLC_FALSE;
1286 if( !p_input ) return b_force_update;
1290 case INPUT_CONTROL_SET_DIE:
1291 msg_Dbg( p_input, "control: stopping input" );
1292 /* Mark all submodules to die */
1293 if( p_input->input.p_access )
1294 p_input->input.p_access->b_die = VLC_TRUE;
1295 if( p_input->input.p_stream )
1296 p_input->input.p_stream->b_die = VLC_TRUE;
1297 p_input->input.p_demux->b_die = VLC_TRUE;
1299 p_input->b_die = VLC_TRUE;
1302 case INPUT_CONTROL_SET_POSITION:
1303 case INPUT_CONTROL_SET_POSITION_OFFSET:
1306 if( i_type == INPUT_CONTROL_SET_POSITION )
1308 f_pos = val.f_float;
1312 /* Should not fail */
1313 demux2_Control( p_input->input.p_demux,
1314 DEMUX_GET_POSITION, &f_pos );
1315 f_pos += val.f_float;
1317 if( f_pos < 0.0 ) f_pos = 0.0;
1318 if( f_pos > 1.0 ) f_pos = 1.0;
1319 /* Reset the decoders states and clock sync (before calling the demuxer */
1320 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1321 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1322 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1325 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1326 "%2.1f%% failed", f_pos * 100 );
1330 if( p_input->i_slave > 0 )
1331 SlaveSeek( p_input );
1333 b_force_update = VLC_TRUE;
1338 case INPUT_CONTROL_SET_TIME:
1339 case INPUT_CONTROL_SET_TIME_OFFSET:
1344 if( i_type == INPUT_CONTROL_SET_TIME )
1346 i_time = val.i_time;
1350 /* Should not fail */
1351 demux2_Control( p_input->input.p_demux,
1352 DEMUX_GET_TIME, &i_time );
1353 i_time += val.i_time;
1355 if( i_time < 0 ) i_time = 0;
1357 /* Reset the decoders states and clock sync (before calling the demuxer */
1358 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1359 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1361 i_ret = demux2_Control( p_input->input.p_demux,
1362 DEMUX_SET_TIME, i_time );
1367 /* Emulate it with a SET_POS */
1368 demux2_Control( p_input->input.p_demux,
1369 DEMUX_GET_LENGTH, &i_length );
1372 double f_pos = (double)i_time / (double)i_length;
1373 i_ret = demux2_Control( p_input->input.p_demux,
1374 DEMUX_SET_POSITION, f_pos );
1379 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1380 " failed or not possible", i_time );
1384 if( p_input->i_slave > 0 )
1385 SlaveSeek( p_input );
1387 b_force_update = VLC_TRUE;
1392 case INPUT_CONTROL_SET_STATE:
1393 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1394 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1397 if( p_input->input.p_access )
1398 i_ret = access2_Control( p_input->input.p_access,
1399 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1401 i_ret = demux2_Control( p_input->input.p_demux,
1402 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1406 /* FIXME What to do ? */
1407 msg_Warn( p_input, "cannot unset pause -> EOF" );
1408 vlc_mutex_unlock( &p_input->lock_control );
1409 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1410 vlc_mutex_lock( &p_input->lock_control );
1413 b_force_update = VLC_TRUE;
1415 /* Switch to play */
1416 p_input->i_state = PLAYING_S;
1417 val.i_int = PLAYING_S;
1418 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1421 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1422 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1424 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1425 p_input->b_can_pause )
1428 if( p_input->input.p_access )
1429 i_ret = access2_Control( p_input->input.p_access,
1430 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1432 i_ret = demux2_Control( p_input->input.p_demux,
1433 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1435 b_force_update = VLC_TRUE;
1439 msg_Warn( p_input, "cannot set pause state" );
1440 val.i_int = p_input->i_state;
1444 val.i_int = PAUSE_S;
1447 /* Switch to new state */
1448 p_input->i_state = val.i_int;
1449 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1451 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1453 b_force_update = VLC_TRUE;
1455 /* Correct "state" value */
1456 val.i_int = p_input->i_state;
1457 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1459 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1461 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1465 case INPUT_CONTROL_SET_RATE:
1466 case INPUT_CONTROL_SET_RATE_SLOWER:
1467 case INPUT_CONTROL_SET_RATE_FASTER:
1471 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1472 i_rate = p_input->i_rate * 2;
1473 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1474 i_rate = p_input->i_rate / 2;
1478 if( i_rate < INPUT_RATE_MIN )
1480 msg_Dbg( p_input, "cannot set rate faster" );
1481 i_rate = INPUT_RATE_MIN;
1483 else if( i_rate > INPUT_RATE_MAX )
1485 msg_Dbg( p_input, "cannot set rate slower" );
1486 i_rate = INPUT_RATE_MAX;
1488 if( i_rate != INPUT_RATE_DEFAULT &&
1489 ( !p_input->b_can_pace_control ||
1490 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1492 msg_Dbg( p_input, "cannot change rate" );
1493 i_rate = INPUT_RATE_DEFAULT;
1495 if( i_rate != p_input->i_rate )
1497 p_input->i_rate = i_rate;
1499 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1501 /* We haven't send data to decoder when rate != default */
1502 if( i_rate == INPUT_RATE_DEFAULT )
1503 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1506 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1508 b_force_update = VLC_TRUE;
1513 case INPUT_CONTROL_SET_PROGRAM:
1514 /* No need to force update, es_out does it if needed */
1515 es_out_Control( p_input->p_es_out,
1516 ES_OUT_SET_GROUP, val.i_int );
1518 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1522 case INPUT_CONTROL_SET_ES:
1523 /* No need to force update, es_out does it if needed */
1524 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1525 input_EsOutGetFromID( p_input->p_es_out,
1529 case INPUT_CONTROL_SET_AUDIO_DELAY:
1530 input_EsOutSetDelay( p_input->p_es_out,
1531 AUDIO_ES, val.i_time );
1532 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1535 case INPUT_CONTROL_SET_SPU_DELAY:
1536 input_EsOutSetDelay( p_input->p_es_out,
1537 SPU_ES, val.i_time );
1538 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1541 case INPUT_CONTROL_SET_TITLE:
1542 case INPUT_CONTROL_SET_TITLE_NEXT:
1543 case INPUT_CONTROL_SET_TITLE_PREV:
1544 if( p_input->input.b_title_demux &&
1545 p_input->input.i_title > 0 )
1548 /* FIXME handle demux title */
1549 demux_t *p_demux = p_input->input.p_demux;
1552 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1553 i_title = p_demux->info.i_title - 1;
1554 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1555 i_title = p_demux->info.i_title + 1;
1557 i_title = val.i_int;
1559 if( i_title >= 0 && i_title < p_input->input.i_title )
1561 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1562 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1564 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1565 input_ControlVarTitle( p_input, i_title );
1568 else if( p_input->input.i_title > 0 )
1570 access_t *p_access = p_input->input.p_access;
1573 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1574 i_title = p_access->info.i_title - 1;
1575 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1576 i_title = p_access->info.i_title + 1;
1578 i_title = val.i_int;
1580 if( i_title >= 0 && i_title < p_input->input.i_title )
1582 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1583 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1585 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1586 stream_AccessReset( p_input->input.p_stream );
1590 case INPUT_CONTROL_SET_SEEKPOINT:
1591 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1592 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1593 if( p_input->input.b_title_demux &&
1594 p_input->input.i_title > 0 )
1596 demux_t *p_demux = p_input->input.p_demux;
1598 int64_t i_input_time;
1599 int64_t i_seekpoint_time;
1601 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1603 i_seekpoint = p_demux->info.i_seekpoint;
1604 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1605 if( i_seekpoint_time >= 0 &&
1606 !demux2_Control( p_demux,
1607 DEMUX_GET_TIME, &i_input_time ) )
1609 if ( i_input_time < i_seekpoint_time + 3000000 )
1615 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1616 i_seekpoint = p_demux->info.i_seekpoint + 1;
1618 i_seekpoint = val.i_int;
1620 if( i_seekpoint >= 0 && i_seekpoint <
1621 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1623 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1624 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1626 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1629 else if( p_input->input.i_title > 0 )
1631 demux_t *p_demux = p_input->input.p_demux;
1632 access_t *p_access = p_input->input.p_access;
1634 int64_t i_input_time;
1635 int64_t i_seekpoint_time;
1637 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1639 i_seekpoint = p_access->info.i_seekpoint;
1640 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1641 if( i_seekpoint_time >= 0 &&
1642 demux2_Control( p_demux,
1643 DEMUX_GET_TIME, &i_input_time ) )
1645 if ( i_input_time < i_seekpoint_time + 3000000 )
1651 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1652 i_seekpoint = p_access->info.i_seekpoint + 1;
1654 i_seekpoint = val.i_int;
1656 if( i_seekpoint >= 0 && i_seekpoint <
1657 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1659 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1660 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1662 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1664 stream_AccessReset( p_input->input.p_stream );
1669 case INPUT_CONTROL_ADD_SLAVE:
1670 if( val.psz_string )
1672 input_source_t *slave = InputSourceNew( p_input );
1674 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1676 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1680 msg_Dbg( p_input, "adding %s as slave on the fly",
1684 if( demux2_Control( p_input->input.p_demux,
1685 DEMUX_GET_TIME, &i_time ) )
1687 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1688 InputSourceClean( p_input, slave );
1692 if( demux2_Control( slave->p_demux,
1693 DEMUX_SET_TIME, i_time ) )
1695 msg_Err( p_input, "seek failed for new slave" );
1696 InputSourceClean( p_input, slave );
1701 /* Get meta (access and demux) */
1702 access2_Control( slave->p_access, ACCESS_GET_META,
1704 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1705 UpdateMeta( p_input );
1707 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1711 msg_Warn( p_input, "failed to add %s as slave",
1715 free( val.psz_string );
1719 case INPUT_CONTROL_SET_BOOKMARK:
1721 msg_Err( p_input, "not yet implemented" );
1725 return b_force_update;
1728 /*****************************************************************************
1730 *****************************************************************************/
1731 static int UpdateFromDemux( input_thread_t *p_input )
1733 demux_t *p_demux = p_input->input.p_demux;
1736 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1738 v.i_int = p_demux->info.i_title;
1739 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1741 input_ControlVarTitle( p_input, p_demux->info.i_title );
1743 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1745 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1747 v.i_int = p_demux->info.i_seekpoint;
1748 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1750 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1752 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1754 /* Hmmm only works with master input */
1755 if( p_input->input.p_demux == p_demux )
1757 int i_title_end = p_input->input.i_title_end -
1758 p_input->input.i_title_offset;
1759 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1760 p_input->input.i_seekpoint_offset;
1762 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1764 if( p_demux->info.i_title > i_title_end ||
1765 ( p_demux->info.i_title == i_title_end &&
1766 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1768 else if( i_seekpoint_end >=0 )
1770 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1772 else if( i_title_end >= 0 )
1774 if( p_demux->info.i_title > i_title_end ) return 0;
1781 /*****************************************************************************
1783 *****************************************************************************/
1784 static int UpdateFromAccess( input_thread_t *p_input )
1786 access_t *p_access = p_input->input.p_access;
1789 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1791 v.i_int = p_access->info.i_title;
1792 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1794 input_ControlVarTitle( p_input, p_access->info.i_title );
1796 stream_AccessUpdate( p_input->input.p_stream );
1798 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1800 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1802 v.i_int = p_access->info.i_seekpoint;
1803 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1804 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1806 if( p_access->info.i_update & INPUT_UPDATE_META )
1808 /* TODO maybe multi - access ? */
1809 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1810 access2_Control( p_input->input.p_access,ACCESS_GET_META, p_meta );
1811 UpdateMeta( p_input );
1812 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1813 p_access->info.i_update &= ~INPUT_UPDATE_META;
1816 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1818 /* Hmmm only works with master input */
1819 if( p_input->input.p_access == p_access )
1821 int i_title_end = p_input->input.i_title_end -
1822 p_input->input.i_title_offset;
1823 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1824 p_input->input.i_seekpoint_offset;
1826 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1828 if( p_access->info.i_title > i_title_end ||
1829 ( p_access->info.i_title == i_title_end &&
1830 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1832 else if( i_seekpoint_end >=0 )
1834 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1836 else if( i_title_end >= 0 )
1838 if( p_access->info.i_title > i_title_end ) return 0;
1845 /*****************************************************************************
1847 *****************************************************************************/
1848 static int UpdateMeta( input_thread_t *p_input )
1850 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1854 if( p_meta->psz_title && !p_input->input.p_item->b_fixed_name )
1855 input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1857 /** \todo handle sout meta */
1862 /*****************************************************************************
1864 *****************************************************************************/
1865 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
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 if( !p_input->b_preparsing )
1873 pl_Yield( p_input );
1874 var_SetInteger( pl_Get( p_input ), "item-change",
1875 p_input->input.p_item->i_id );
1876 pl_Release( p_input )
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 )
1914 char *psz_dup = strdup( psz_mrl );
1922 if( !in ) return VLC_EGENERIC;
1925 if( !p_input->b_preparsing )
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,
2016 p_input->b_preparsing );
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,
2028 p_input->b_preparsing );
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,
2049 p_input->b_preparsing );
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 */
2086 if( !p_input->b_preparsing )
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, p_input->b_preparsing );
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,
2127 p_input->b_preparsing );
2128 if( in->p_demux == NULL )
2130 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2131 psz_access, psz_demux, psz_path );
2132 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2133 _("Can't recognize the input's format"),
2134 _("The format of '%s' can't be detected. "
2135 "Have a look the log for details."), psz_mrl );
2139 /* TODO get title from demux */
2140 if( !p_input->b_preparsing && in->i_title <= 0 )
2142 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2143 &in->title, &in->i_title,
2144 &in->i_title_offset, &in->i_seekpoint_offset ))
2151 in->b_title_demux = VLC_TRUE;
2156 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2157 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2159 if( psz_dup ) free( psz_dup );
2164 demux2_Delete( in->p_demux );
2167 stream_Delete( in->p_stream );
2170 access2_Delete( in->p_access );
2171 if( psz_dup ) free( psz_dup );
2173 return VLC_EGENERIC;
2176 /*****************************************************************************
2178 *****************************************************************************/
2179 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2182 demux2_Delete( in->p_demux );
2185 stream_Delete( in->p_stream );
2188 access2_Delete( in->p_access );
2190 if( in->i_title > 0 )
2193 for( i = 0; i < in->i_title; i++ )
2195 vlc_input_title_Delete( in->title[i] );
2201 static void SlaveDemux( input_thread_t *p_input )
2206 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2208 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2212 for( i = 0; i < p_input->i_slave; i++ )
2214 input_source_t *in = p_input->slave[i];
2220 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2225 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2227 msg_Err( p_input, "slave[%d] doesn't like "
2228 "DEMUX_GET_TIME -> EOF", i );
2233 if( i_stime >= i_time )
2236 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2242 i_ret = in->p_demux->pf_demux( in->p_demux );
2247 msg_Dbg( p_input, "slave %d EOF", i );
2248 in->b_eof = VLC_TRUE;
2253 static void SlaveSeek( input_thread_t *p_input )
2258 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2260 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2264 for( i = 0; i < p_input->i_slave; i++ )
2266 input_source_t *in = p_input->slave[i];
2268 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2270 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2271 in->b_eof = VLC_TRUE;
2275 /*****************************************************************************
2277 *****************************************************************************/
2278 static void InputMetaUser( input_thread_t *p_input )
2280 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
2283 if( !p_meta ) return;
2285 /* Get meta information from user */
2286 #define GET_META( field, s ) \
2287 var_Get( p_input, (s), &val ); \
2288 if( *val.psz_string ) { \
2289 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2290 p_meta->psz_ ## field = strdup( val.psz_string ); \
2292 free( val.psz_string )
2294 GET_META( title, "meta-title" );
2295 GET_META( author, "meta-author" );
2296 GET_META( artist, "meta-artist" );
2297 GET_META( genre, "meta-genre" );
2298 GET_META( copyright, "meta-copyright" );
2299 GET_META( description, "meta-description" );
2300 GET_META( date, "meta-date" );
2301 GET_META( url, "meta-url" );
2305 /*****************************************************************************
2306 * DecodeUrl: decode a given encoded url
2307 *****************************************************************************/
2308 static void DecodeUrl( char *psz )
2310 char *dup = strdup( psz );
2328 *psz++ = strtol( val, NULL, 16 );
2330 else if( *p == '+' )
2340 if( psz ) *psz++ ='\0';
2341 if( dup ) free( dup );
2344 /*****************************************************************************
2345 * MRLSplit: parse the access, demux and url part of the
2346 * Media Resource Locator.
2347 *****************************************************************************/
2348 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2349 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2351 char *psz_access = NULL;
2352 char *psz_demux = NULL;
2353 char *psz_path = NULL;
2354 char *psz, *psz_check;
2356 psz = strchr( psz_dup, ':' );
2358 /* '@' not allowed in access/demux part */
2359 psz_check = strchr( psz_dup, '@' );
2360 if( psz_check && psz_check < psz ) psz = 0;
2362 #if defined( WIN32 ) || defined( UNDER_CE )
2363 if( psz - psz_dup == 1 )
2365 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2374 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2378 psz = strchr( psz_dup, '/' );
2385 psz_access = psz_dup;
2392 if( !psz_access ) *ppsz_access = "";
2393 else *ppsz_access = psz_access;
2395 if( !psz_demux ) *ppsz_demux = "";
2396 else *ppsz_demux = psz_demux;
2398 if( !psz_path ) *ppsz_path = "";
2399 else *ppsz_path = psz_path;
2402 /*****************************************************************************
2403 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2406 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2407 *****************************************************************************/
2408 static void MRLSections( input_thread_t *p_input, char *psz_source,
2409 int *pi_title_start, int *pi_title_end,
2410 int *pi_chapter_start, int *pi_chapter_end )
2412 char *psz, *psz_end, *psz_next, *psz_check;
2414 *pi_title_start = *pi_title_end = -1;
2415 *pi_chapter_start = *pi_chapter_end = -1;
2417 /* Start by parsing titles and chapters */
2418 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2420 /* Check we are really dealing with a title/chapter section */
2421 psz_check = psz + 1;
2422 if( !*psz_check ) return;
2423 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2424 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2425 if( *psz_check == ':' && ++psz_check )
2426 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2427 if( *psz_check != '-' && *psz_check ) return;
2428 if( *psz_check == '-' && ++psz_check )
2429 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2430 if( *psz_check != ':' && *psz_check ) return;
2431 if( *psz_check == ':' && ++psz_check )
2432 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2433 if( *psz_check ) return;
2435 /* Separate start and end */
2437 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2439 /* Look for the start title */
2440 *pi_title_start = strtol( psz, &psz_next, 0 );
2441 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2442 *pi_title_end = *pi_title_start;
2445 /* Look for the start chapter */
2447 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2448 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2449 *pi_chapter_end = *pi_chapter_start;
2453 /* Look for the end title */
2454 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2455 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2458 /* Look for the end chapter */
2459 if( *psz_end ) psz_end++;
2460 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2461 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2464 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2465 psz_source, *pi_title_start, *pi_chapter_start,
2466 *pi_title_end, *pi_chapter_end );
2469 /*****************************************************************************
2470 * input_AddSubtitles: add a subtitles file and enable it
2471 *****************************************************************************/
2472 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2473 vlc_bool_t b_check_extension )
2475 input_source_t *sub;
2478 char *psz_path, *psz_extension;
2480 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2485 /* if we are provided a subtitle.sub file,
2486 * see if we don't have a subtitle.idx and use it instead */
2487 psz_path = strdup( psz_subtitle );
2490 psz_extension = strrchr( psz_path, '.');
2491 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2495 strcpy( psz_extension, ".idx" );
2496 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2497 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2500 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2501 psz_path, psz_subtitle );
2502 strcpy( psz_subtitle, psz_path );
2508 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2510 sub = InputSourceNew( p_input );
2511 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2513 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
2516 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2518 if( count.i_int == 0 )
2520 /* if it was first one, there is disable too */
2522 if( count.i_int < list.p_list->i_count )
2524 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2525 &list.p_list->p_values[count.i_int] );
2527 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2535 # define MAX_PATH 250
2537 int input_DownloadAndCacheArt( vlc_object_t *p_parent, input_item_t *p_item )
2543 int i_status = VLC_EGENERIC;
2548 || !p_item->p_meta->psz_arturl
2549 || !*p_item->p_meta->psz_arturl )
2551 return VLC_EGENERIC;
2553 if( !strncmp( p_item->p_meta->psz_arturl, "file", 4 ) )
2558 psz_artist = p_item->p_meta->psz_artist;
2559 psz_album = p_item->p_meta->psz_album;
2560 psz_type = strrchr( p_item->p_meta->psz_arturl, '.' );
2561 psz_filename = (char *)malloc( MAX_PATH );
2564 snprintf( psz_filename, MAX_PATH,
2565 "file://%s/" CONFIG_DIR "/art/%s/%s/art%s",
2566 p_parent->p_libvlc->psz_homedir,
2567 psz_artist, psz_album, psz_type );
2568 msg_Dbg( p_parent, "Saving album art to %s", psz_filename );
2570 /* Check if file exists */
2571 i_ret = utf8_stat( psz_filename+7, &a );
2574 msg_Dbg( p_parent, "Album art %s already exists", psz_filename );
2578 //if( i_ret == -1 && errno == ENOTDIR )
2580 /* GRUIKKKKKKKKKK (make sure that all the directories exist) */
2581 char *psz_dir = malloc( MAX_PATH );
2582 snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR,
2583 p_parent->p_libvlc->psz_homedir );
2584 utf8_mkdir( psz_dir );
2585 snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR "/art",
2586 p_parent->p_libvlc->psz_homedir );
2587 utf8_mkdir( psz_dir );
2588 snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR "/art/%s",
2589 p_parent->p_libvlc->psz_homedir, psz_artist );
2590 utf8_mkdir( psz_dir );
2591 snprintf( psz_dir, MAX_PATH, "%s/" CONFIG_DIR "/art/%s/%s",
2592 p_parent->p_libvlc->psz_homedir,
2593 psz_artist, psz_album );
2594 utf8_mkdir( psz_dir );
2598 stream_t *p_stream = stream_UrlNew( p_parent,
2599 p_item->p_meta->psz_arturl );
2603 void *p_buffer = malloc( 1<<16 );
2605 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
2606 while( ( l_read = stream_Read( p_stream, p_buffer, 1<<16 ) ) )
2608 fwrite( p_buffer, l_read, 1, p_file );
2612 stream_Delete( p_stream );
2613 msg_Dbg( p_parent, "Album art saved to %s\n", psz_filename );
2614 free( p_item->p_meta->psz_arturl );
2615 p_item->p_meta->psz_arturl = strdup( psz_filename );
2616 i_status = VLC_SUCCESS;
2619 free( psz_filename );