]> git.sesse.net Git - vlc/blobdiff - src/input/input.c
Fixed input bookmark thread safety, support and event.
[vlc] / src / input / input.c
index 9e48c5c5b818e77efaeaa8e5bee24cc0001e75f0..4c0b4c99b189b48b5e93098942c1f33d6629f694 100644 (file)
@@ -42,6 +42,7 @@
 #include "access.h"
 #include "demux.h"
 #include "stream.h"
+#include "item.h"
 
 #include <vlc_sout.h>
 #include "../stream_output/stream_output.h"
@@ -70,12 +71,17 @@ static void             WaitDie   ( input_thread_t *p_input );
 static void             End     ( input_thread_t *p_input );
 static void             MainLoop( input_thread_t *p_input );
 
+static void ObjectKillChildrens( input_thread_t *, vlc_object_t * );
+
 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline );
 static void       ControlReduce( input_thread_t * );
 static bool Control( input_thread_t *, int, vlc_value_t );
 
-static int  UpdateFromAccess( input_thread_t * );
-static int  UpdateFromDemux( input_thread_t * );
+static int  UpdateTitleSeekpointFromAccess( input_thread_t * );
+static void UpdateGenericFromAccess( input_thread_t * );
+
+static int  UpdateTitleSeekpointFromDemux( input_thread_t * );
+static void UpdateGenericFromDemux( input_thread_t * );
 
 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
 
@@ -83,6 +89,8 @@ static input_source_t *InputSourceNew( input_thread_t *);
 static int  InputSourceInit( input_thread_t *, input_source_t *,
                              const char *, const char *psz_forced_demux );
 static void InputSourceClean( input_source_t * );
+static void InputSourceMeta( input_thread_t *, input_source_t *, vlc_meta_t * );
+
 /* TODO */
 //static void InputGetAttachments( input_thread_t *, input_source_t * );
 static void SlaveDemux( input_thread_t *p_input );
@@ -93,8 +101,6 @@ static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
 static char *InputGetExtraFiles( input_thread_t *p_input,
                                  const char *psz_access, const char *psz_path );
 
-static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
-static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
                               int i_new, input_attachment_t **pp_new );
 
@@ -175,17 +181,20 @@ static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
     p_input->i_state = INIT_S;
     p_input->p->i_rate = INPUT_RATE_DEFAULT;
     p_input->p->b_recording = false;
-    TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
+    memset( &p_input->p->bookmark, 0, sizeof(p_input->p->bookmark) );
+    TAB_INIT( p_input->p->i_bookmark, p_input->p->pp_bookmark );
     TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
     p_input->p->p_es_out_display = NULL;
     p_input->p->p_es_out = NULL;
     p_input->p->p_sout   = NULL;
     p_input->p->b_out_pace_control = false;
     p_input->i_pts_delay = 0;
+    p_input->p->i_cr_average = 0;
 
-    /* Init Input fields */
     vlc_gc_incref( p_item ); /* Released in Destructor() */
-    p_input->p->input.p_item = p_item;
+    p_input->p->p_item = p_item;
+
+    /* Init Input fields */
     p_input->p->input.p_access = NULL;
     p_input->p->input.p_stream = NULL;
     p_input->p->input.p_demux  = NULL;
@@ -197,7 +206,6 @@ static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
     p_input->p->input.b_can_rate_control = true;
     p_input->p->input.b_rescale_ts = true;
     p_input->p->input.b_eof = false;
-    p_input->p->input.i_cr_average = 0;
 
     vlc_mutex_lock( &p_item->lock );
 
@@ -229,7 +237,7 @@ static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
     input_ControlVarInit( p_input );
 
     /* */
-    p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
+    p_input->p->i_cr_average = var_GetInteger( p_input, "cr-average" );
 
     if( !p_input->b_preparsing )
     {
@@ -311,7 +319,7 @@ static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
 static void Destructor( input_thread_t * p_input )
 {
 #ifndef NDEBUG
-    char * psz_name = input_item_GetName( p_input->p->input.p_item );
+    char * psz_name = input_item_GetName( p_input->p->p_item );
     msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
     free( psz_name );
 #endif
@@ -324,7 +332,7 @@ static void Destructor( input_thread_t * p_input )
     if( p_input->p->p_sout )
         sout_DeleteInstance( p_input->p->p_sout );
 #endif
-    vlc_gc_decref( p_input->p->input.p_item );
+    vlc_gc_decref( p_input->p->p_item );
 
     vlc_mutex_destroy( &p_input->p->counters.counters_lock );
 
@@ -440,23 +448,6 @@ int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
  *
  * \param the input thread to stop
  */
-static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
-{
-    vlc_list_t *p_list;
-    int i;
-
-    if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
-        p_obj->i_object_type == VLC_OBJECT_AOUT ||
-        p_obj == VLC_OBJECT(p_input->p->p_sout) )
-        return;
-
-    vlc_object_kill( p_obj );
-
-    p_list = vlc_list_children( p_obj );
-    for( i = 0; i < p_list->i_count; i++ )
-        ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
-    vlc_list_release( p_list );
-}
 void input_StopThread( input_thread_t *p_input )
 {
     /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
@@ -476,6 +467,39 @@ sout_instance_t *input_DetachSout( input_thread_t *p_input )
     return p_sout;
 }
 
+/**
+ * Get the item from an input thread
+ * FIXME it does not increase ref count of the item.
+ * if it is used after p_input is destroyed nothing prevent it from
+ * being freed.
+ */
+input_item_t *input_GetItem( input_thread_t *p_input )
+{
+    assert( p_input && p_input->p );
+    return p_input->p->p_item;
+}
+
+/*****************************************************************************
+ * ObjectKillChildrens
+ *****************************************************************************/
+static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
+{
+    vlc_list_t *p_list;
+    int i;
+
+    if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
+        p_obj->i_object_type == VLC_OBJECT_AOUT ||
+        p_obj == VLC_OBJECT(p_input->p->p_sout) )
+        return;
+
+    vlc_object_kill( p_obj );
+
+    p_list = vlc_list_children( p_obj );
+    for( i = 0; i < p_list->i_count; i++ )
+        ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
+    vlc_list_release( p_list );
+}
+
 /*****************************************************************************
  * Run: main thread loop
  * This is the "normal" thread that spawns the input processing chain,
@@ -551,23 +575,28 @@ static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *p
         ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
         i_ret = 0; /* EOF */
     else
-        i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
+        i_ret = demux_Demux( p_input->p->input.p_demux );
 
     if( i_ret > 0 )
     {
-        /* TODO */
-        if( p_input->p->input.b_title_demux &&
-            p_input->p->input.p_demux->info.i_update )
+        if( p_input->p->input.p_demux->info.i_update )
         {
-            i_ret = UpdateFromDemux( p_input );
-            *pb_changed = true;
+            if( p_input->p->input.b_title_demux )
+            {
+                i_ret = UpdateTitleSeekpointFromDemux( p_input );
+                *pb_changed = true;
+            }
+            UpdateGenericFromDemux( p_input );
         }
-        else if( !p_input->p->input.b_title_demux &&
-                  p_input->p->input.p_access &&
-                  p_input->p->input.p_access->info.i_update )
+        else if( p_input->p->input.p_access &&
+                 p_input->p->input.p_access->info.i_update )
         {
-            i_ret = UpdateFromAccess( p_input );
-            *pb_changed = true;
+            if( !p_input->p->input.b_title_demux )
+            {
+                i_ret = UpdateTitleSeekpointFromAccess( p_input );
+                *pb_changed = true;
+            }
+            UpdateGenericFromAccess( p_input );
         }
     }
 
@@ -662,6 +691,13 @@ static void MainLoopInterface( input_thread_t *p_input )
         i_length = 0;
 
     es_out_SetTimes( p_input->p->p_es_out, f_position, i_time, i_length );
+
+    /* update current bookmark */
+    vlc_mutex_lock( &p_input->p->p_item->lock );
+    p_input->p->bookmark.i_time_offset = i_time;
+    if( p_input->p->input.p_stream )
+        p_input->p->bookmark.i_byte_offset = stream_Tell( p_input->p->input.p_stream );
+    vlc_mutex_unlock( &p_input->p->p_item->lock );
 }
 
 /**
@@ -670,7 +706,7 @@ static void MainLoopInterface( input_thread_t *p_input )
  */
 static void MainLoopStatistic( input_thread_t *p_input )
 {
-    stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
+    stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
     /* Are we the thread responsible for computing global stats ? */
     if( libvlc_priv( p_input->p_libvlc )->p_stats_computer == p_input )
     {
@@ -817,7 +853,7 @@ static int InitSout( input_thread_t * p_input )
 
     /* Find a usable sout and attach it to p_input */
     psz = var_GetNonEmptyString( p_input, "sout" );
-    if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
+    if( psz && strncasecmp( p_input->p->p_item->psz_uri, "vlc:", 4 ) )
     {
         /* Check the validity of the provided sout */
         if( p_input->p->p_sout )
@@ -877,15 +913,16 @@ static int InitSout( input_thread_t * p_input )
 
 static void InitTitle( input_thread_t * p_input )
 {
-    vlc_value_t val;
+    input_source_t *p_master = &p_input->p->input;
 
-    if( p_input->b_preparsing ) return;
+    if( p_input->b_preparsing )
+        return;
 
     /* Create global title (from master) */
-    p_input->p->i_title = p_input->p->input.i_title;
-    p_input->p->title   = p_input->p->input.title;
-    p_input->p->i_title_offset = p_input->p->input.i_title_offset;
-    p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
+    p_input->p->i_title = p_master->i_title;
+    p_input->p->title   = p_master->title;
+    p_input->p->i_title_offset = p_master->i_title_offset;
+    p_input->p->i_seekpoint_offset = p_master->i_seekpoint_offset;
     if( p_input->p->i_title > 0 )
     {
         /* Setup variables */
@@ -894,9 +931,9 @@ static void InitTitle( input_thread_t * p_input )
     }
 
     /* Global flag */
-    p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
-    p_input->p->b_can_pause        = p_input->p->input.b_can_pause;
-    p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
+    p_input->b_can_pace_control    = p_master->b_can_pace_control;
+    p_input->p->b_can_pause        = p_master->b_can_pause;
+    p_input->p->b_can_rate_control = p_master->b_can_rate_control;
 
     /* Fix pts delay */
     if( p_input->i_pts_delay < 0 )
@@ -904,54 +941,43 @@ static void InitTitle( input_thread_t * p_input )
 
     /* If the desynchronisation requested by the user is < 0, we need to
      * cache more data. */
-    var_Get( p_input, "audio-desync", &val );
-    if( val.i_int < 0 )
-        p_input->i_pts_delay -= (val.i_int * 1000);
+    const int i_desynch = var_GetInteger( p_input, "audio-desync" );
+    if( i_desynch < 0 )
+        p_input->i_pts_delay -= i_desynch * 1000;
 
     /* Update cr_average depending on the caching */
-    p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
-    p_input->p->input.i_cr_average /= 10;
-    if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
+    p_input->p->i_cr_average *= (10 * p_input->i_pts_delay / 200000);
+    p_input->p->i_cr_average /= 10;
+    if( p_input->p->i_cr_average < 10 )
+        p_input->p->i_cr_average = 10;
 }
 
 static void StartTitle( input_thread_t * p_input )
 {
-    double f_fps;
     vlc_value_t val;
-    int i_delay;
-    char *psz;
-    char *psz_subtitle;
-    int64_t i_length;
 
     /* Start title/chapter */
-
-    if( p_input->b_preparsing )
-    {
-        p_input->p->i_start = 0;
-        return;
-    }
-
     val.i_int = p_input->p->input.i_title_start -
                 p_input->p->input.i_title_offset;
     if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
         input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
+
     val.i_int = p_input->p->input.i_seekpoint_start -
                 p_input->p->input.i_seekpoint_offset;
     if( val.i_int > 0 /* TODO: check upper boundary */ )
         input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
 
-    /* Start time*/
-    /* Set start time */
+    /* Start/stop/run time */
     p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
     p_input->p->i_stop  = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
     p_input->p->i_run   = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
-    i_length = var_GetTime( p_input, "length" );
     if( p_input->p->i_run < 0 )
     {
         msg_Warn( p_input, "invalid run-time ignored" );
         p_input->p->i_run = 0;
     }
 
+    const mtime_t i_length = var_GetTime( p_input, "length" );
     if( p_input->p->i_start > 0 )
     {
         if( p_input->p->i_start >= i_length )
@@ -974,11 +1000,14 @@ static void StartTitle( input_thread_t * p_input )
         msg_Warn( p_input, "invalid stop-time ignored" );
         p_input->p->i_stop = 0;
     }
+}
 
+static void LoadSubtitles( input_thread_t *p_input )
+{
     /* Load subtitles */
     /* Get fps and set it if not already set */
-    if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
-        f_fps > 1.0 )
+    const double f_fps = p_input->p->f_fps;
+    if( f_fps > 1.0 )
     {
         float f_requested_fps;
 
@@ -994,26 +1023,23 @@ static void StartTitle( input_thread_t * p_input )
         }
     }
 
-    i_delay = var_CreateGetInteger( p_input, "sub-delay" );
+    const int i_delay = var_CreateGetInteger( p_input, "sub-delay" );
     if( i_delay != 0 )
-    {
         var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
-    }
 
     /* Look for and add subtitle files */
-    psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
+    char *psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
     if( psz_subtitle != NULL )
     {
         msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
         SubtitleAdd( p_input, psz_subtitle, true );
     }
 
-    var_Get( p_input, "sub-autodetect-file", &val );
-    if( val.b_bool )
+    if( var_GetBool( p_input, "sub-autodetect-file" ) )
     {
         char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
         char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
-                                             p_input->p->input.p_item->psz_uri );
+                                             p_input->p->p_item->psz_uri );
         free( psz_autopath );
 
         for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
@@ -1030,39 +1056,38 @@ static void StartTitle( input_thread_t * p_input )
         free( ppsz_subs );
     }
     free( psz_subtitle );
+}
 
-    /* Look for slave */
-    psz = var_GetNonEmptyString( p_input, "input-slave" );
-    if( psz != NULL )
+static void LoadSlaves( input_thread_t *p_input )
+{
+    char *psz = var_GetNonEmptyString( p_input, "input-slave" );
+    if( !psz )
+        return;
+
+    char *psz_org = psz;
+    while( psz && *psz )
     {
-        char *psz_delim;
-        input_source_t *slave;
-        while( psz && *psz )
-        {
-            while( *psz == ' ' || *psz == '#' )
-            {
-                psz++;
-            }
-            if( ( psz_delim = strchr( psz, '#' ) ) )
-            {
-                *psz_delim++ = '\0';
-            }
-            if( *psz == 0 )
-            {
-                break;
-            }
+        while( *psz == ' ' || *psz == '#' )
+            psz++;
 
-            msg_Dbg( p_input, "adding slave input '%s'", psz );
-            slave = InputSourceNew( p_input );
-            if( !InputSourceInit( p_input, slave, psz, NULL ) )
-            {
-                TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
-            }
-            else free( slave );
-            psz = psz_delim;
-        }
-        free( psz );
+        char *psz_delim = strchr( psz, '#' );
+        if( psz_delim )
+            *psz_delim++ = '\0';
+
+        if( *psz == 0 )
+            break;
+
+        msg_Dbg( p_input, "adding slave input '%s'", psz );
+
+        input_source_t *p_slave = InputSourceNew( p_input );
+        if( !InputSourceInit( p_input, p_slave, psz, NULL ) )
+            TAB_APPEND( p_input->p->i_slave, p_input->p->slave, p_slave );
+        else
+            free( p_slave );
+
+        psz = psz_delim;
     }
+    free( psz_org );
 }
 
 static void InitPrograms( input_thread_t * p_input )
@@ -1070,8 +1095,6 @@ static void InitPrograms( input_thread_t * p_input )
     int i_es_out_mode;
     vlc_value_t val;
 
-    if( p_input->b_preparsing ) return;
-
     /* Set up es_out */
     es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
     i_es_out_mode = ES_OUT_MODE_AUTO;
@@ -1123,9 +1146,9 @@ static int Init( input_thread_t * p_input )
     vlc_meta_t *p_meta;
     int i, ret;
 
-    for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
+    for( i = 0; i < p_input->p->p_item->i_options; i++ )
     {
-        if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
+        if( !strncmp( p_input->p->p_item->ppsz_options[i], "meta-file", 9 ) )
         {
             msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
             var_SetString( p_input, "sout", "" );
@@ -1154,7 +1177,7 @@ static int Init( input_thread_t * p_input )
     var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
 
     if( InputSourceInit( p_input, &p_input->p->input,
-                         p_input->p->input.p_item->psz_uri, NULL ) )
+                         p_input->p->p_item->psz_uri, NULL ) )
     {
         goto error;
     }
@@ -1168,12 +1191,16 @@ static int Init( input_thread_t * p_input )
                          &i_length ) )
         i_length = 0;
     if( i_length <= 0 )
-        i_length = input_item_GetDuration( p_input->p->input.p_item );
+        i_length = input_item_GetDuration( p_input->p->p_item );
     input_SendEventTimes( p_input, 0.0, 0, i_length );
 
-    StartTitle( p_input );
-
-    InitPrograms( p_input );
+    if( !p_input->b_preparsing )
+    {
+        StartTitle( p_input );
+        LoadSubtitles( p_input );
+        LoadSlaves( p_input );
+        InitPrograms( p_input );
+    }
 
     if( !p_input->b_preparsing && p_input->p->p_sout )
     {
@@ -1191,22 +1218,26 @@ static int Init( input_thread_t * p_input )
     }
 
     p_meta = vlc_meta_New();
+    if( p_meta )
+    {
+        /* Get meta data from users */
+        InputMetaUser( p_input, p_meta );
 
-    /* Get meta data from users */
-    InputMetaUser( p_input, p_meta );
-
-    /* Get meta data from master input */
-    DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
+        /* Get meta data from master input */
+        InputSourceMeta( p_input, &p_input->p->input, p_meta );
 
-    /* Access_file does not give any meta, and there are no slave */
-    AccessMeta( p_input, p_meta );
+        /* And from slave */
+        for( int i = 0; i < p_input->p->i_slave; i++ )
+            InputSourceMeta( p_input, p_input->p->slave[i], p_meta );
 
-    InputUpdateMeta( p_input, p_meta );
+        /* */
+        InputUpdateMeta( p_input, p_meta );
+    }
 
     if( !p_input->b_preparsing )
     {
         msg_Dbg( p_input, "`%s' successfully opened",
-                 p_input->p->input.p_item->psz_uri );
+                 p_input->p->p_item->psz_uri );
 
     }
 
@@ -1326,11 +1357,12 @@ static void End( input_thread_t * p_input )
             libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
 
             /* make sure we are up to date */
-            stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
+            stats_ComputeInputStats( p_input, p_input->p->p_item->p_stats );
             if( p_private->p_stats_computer == p_input )
             {
                 stats_ComputeGlobalStats( p_input->p_libvlc,
                                           p_input->p_libvlc->p_stats );
+                /* FIXME how can it be thread safe ? */
                 p_private->p_stats_computer = NULL;
             }
             CL_CO( read_bytes );
@@ -1580,19 +1612,13 @@ static bool Control( input_thread_t *p_input, int i_type,
                 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
                 break;
             }
-            if( i_type == INPUT_CONTROL_SET_POSITION )
-            {
-                f_pos = val.f_float;
-            }
-            else
-            {
-                /* Should not fail */
-                demux_Control( p_input->p->input.p_demux,
-                                DEMUX_GET_POSITION, &f_pos );
-                f_pos += val.f_float;
-            }
-            if( f_pos < 0.0 ) f_pos = 0.0;
-            if( f_pos > 1.0 ) f_pos = 1.0;
+            f_pos = val.f_float;
+            if( i_type != INPUT_CONTROL_SET_POSITION )
+                f_pos += var_GetFloat( p_input, "position" );
+            if( f_pos < 0.0 )
+                f_pos = 0.0;
+            else if( f_pos > 1.0 )
+                f_pos = 1.0;
             /* Reset the decoders states and clock sync (before calling the demuxer */
             es_out_SetTime( p_input->p->p_es_out, -1 );
             if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
@@ -1624,18 +1650,12 @@ static bool Control( input_thread_t *p_input, int i_type,
                 break;
             }
 
-            if( i_type == INPUT_CONTROL_SET_TIME )
-            {
-                i_time = val.i_time;
-            }
-            else
-            {
-                /* Should not fail */
-                demux_Control( p_input->p->input.p_demux,
-                                DEMUX_GET_TIME, &i_time );
-                i_time += val.i_time;
-            }
-            if( i_time < 0 ) i_time = 0;
+            i_time = val.i_time;
+            if( i_type != INPUT_CONTROL_SET_TIME )
+                i_time += var_GetTime( p_input, "time" );
+
+            if( i_time < 0 )
+                i_time = 0;
 
             /* Reset the decoders states and clock sync (before calling the demuxer */
             es_out_SetTime( p_input->p->p_es_out, -1 );
@@ -1934,9 +1954,8 @@ static bool Control( input_thread_t *p_input, int i_type,
                 {
                     i_seekpoint = p_demux->info.i_seekpoint;
                     i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
-                    if( i_seekpoint_time >= 0 &&
-                         !demux_Control( p_demux,
-                                          DEMUX_GET_TIME, &i_input_time ) )
+                    i_input_time = var_GetTime( p_input, "time" );
+                    if( i_seekpoint_time >= 0 && i_input_time >= 0 )
                     {
                         if( i_input_time < i_seekpoint_time + 3000000 )
                             i_seekpoint--;
@@ -1960,7 +1979,6 @@ static bool Control( input_thread_t *p_input, int i_type,
             }
             else if( p_input->p->input.i_title > 0 )
             {
-                demux_t *p_demux = p_input->p->input.p_demux;
                 access_t *p_access = p_input->p->input.p_access;
                 int i_seekpoint;
                 int64_t i_input_time;
@@ -1970,9 +1988,8 @@ static bool Control( input_thread_t *p_input, int i_type,
                 {
                     i_seekpoint = p_access->info.i_seekpoint;
                     i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
-                    if( i_seekpoint_time >= 0 &&
-                        demux_Control( p_demux,
-                                        DEMUX_GET_TIME, &i_input_time ) )
+                    i_input_time = var_GetTime( p_input, "time" );
+                    if( i_seekpoint_time >= 0 && i_input_time >= 0 )
                     {
                         if( i_input_time < i_seekpoint_time + 3000000 )
                             i_seekpoint--;
@@ -2039,10 +2056,12 @@ static bool Control( input_thread_t *p_input, int i_type,
 
                     /* Get meta (access and demux) */
                     p_meta = vlc_meta_New();
-                    access_Control( slave->p_access, ACCESS_GET_META,
-                                     p_meta );
-                    demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
-                    InputUpdateMeta( p_input, p_meta );
+                    if( p_meta )
+                    {
+                        access_Control( slave->p_access, ACCESS_GET_META, p_meta );
+                        demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
+                        InputUpdateMeta( p_input, p_meta );
+                    }
 
                     TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
                 }
@@ -2096,6 +2115,45 @@ static bool Control( input_thread_t *p_input, int i_type,
             break;
 
         case INPUT_CONTROL_SET_BOOKMARK:
+        {
+            seekpoint_t bookmark;
+
+            bookmark.i_time_offset = -1;
+            bookmark.i_byte_offset = -1;
+
+            vlc_mutex_lock( &p_input->p->p_item->lock );
+            if( val.i_int >= 0 && val.i_int < p_input->p->i_bookmark )
+            {
+                const seekpoint_t *p_bookmark = p_input->p->pp_bookmark[val.i_int];
+                bookmark.i_time_offset = p_bookmark->i_time_offset;
+                bookmark.i_byte_offset = p_bookmark->i_byte_offset;
+            }
+            vlc_mutex_unlock( &p_input->p->p_item->lock );
+
+            if( bookmark.i_time_offset < 0 && bookmark.i_byte_offset < 0 )
+            {
+                msg_Err( p_input, "invalid bookmark %d", val.i_int );
+                break;
+            }
+
+            if( bookmark.i_time_offset >= 0 )
+            {
+                val.i_time = bookmark.i_time_offset;
+                b_force_update = Control( p_input, INPUT_CONTROL_SET_TIME, val );
+            }
+            else if( bookmark.i_byte_offset >= 0 &&
+                     p_input->p->input.p_stream )
+            {
+                const int64_t i_size = stream_Size( p_input->p->input.p_stream );
+                if( i_size > 0 && bookmark.i_byte_offset <= i_size )
+                {
+                    val.f_float = (double)bookmark.i_byte_offset / i_size;
+                    b_force_update = Control( p_input, INPUT_CONTROL_SET_POSITION, val );
+                }
+            }
+            break;
+        }
+
         default:
             msg_Err( p_input, "not yet implemented" );
             break;
@@ -2105,7 +2163,7 @@ static bool Control( input_thread_t *p_input, int i_type,
 }
 
 /*****************************************************************************
- * UpdateFromDemux:
+ * UpdateTitleSeekpoint
  *****************************************************************************/
 static int UpdateTitleSeekpoint( input_thread_t *p_input,
                                  int i_title, int i_seekpoint )
@@ -2133,7 +2191,10 @@ static int UpdateTitleSeekpoint( input_thread_t *p_input,
     }
     return 1;
 }
-static int UpdateFromDemux( input_thread_t *p_input )
+/*****************************************************************************
+ * Update*FromDemux:
+ *****************************************************************************/
+static int UpdateTitleSeekpointFromDemux( input_thread_t *p_input )
 {
     demux_t *p_demux = p_input->p->input.p_demux;
 
@@ -2151,7 +2212,6 @@ static int UpdateFromDemux( input_thread_t *p_input )
 
         p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
     }
-    p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
 
     /* Hmmm only works with master input */
     if( p_input->p->input.p_demux == p_demux )
@@ -2161,10 +2221,29 @@ static int UpdateFromDemux( input_thread_t *p_input )
     return 1;
 }
 
+static void UpdateGenericFromDemux( input_thread_t *p_input )
+{
+    demux_t *p_demux = p_input->p->input.p_demux;
+
+    if( p_demux->info.i_update & INPUT_UPDATE_META )
+    {
+        vlc_meta_t *p_meta = vlc_meta_New();
+        if( p_meta )
+        {
+            demux_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
+            InputUpdateMeta( p_input, p_meta );
+        }
+        p_demux->info.i_update &= ~INPUT_UPDATE_META;
+    }
+
+    p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
+}
+
+
 /*****************************************************************************
- * UpdateFromAccess:
+ * Update*FromAccess:
  *****************************************************************************/
-static int UpdateFromAccess( input_thread_t *p_input )
+static int UpdateTitleSeekpointFromAccess( input_thread_t *p_input )
 {
     access_t *p_access = p_input->p->input.p_access;
 
@@ -2183,12 +2262,26 @@ static int UpdateFromAccess( input_thread_t *p_input )
 
         p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
     }
+    /* Hmmm only works with master input */
+    if( p_input->p->input.p_access == p_access )
+        return UpdateTitleSeekpoint( p_input,
+                                     p_access->info.i_title,
+                                     p_access->info.i_seekpoint );
+    return 1;
+}
+static void UpdateGenericFromAccess( input_thread_t *p_input )
+{
+    access_t *p_access = p_input->p->input.p_access;
+
     if( p_access->info.i_update & INPUT_UPDATE_META )
     {
         /* TODO maybe multi - access ? */
         vlc_meta_t *p_meta = vlc_meta_New();
-        access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
-        InputUpdateMeta( p_input, p_meta );
+        if( p_meta )
+        {
+            access_Control( p_input->p->input.p_access, ACCESS_GET_META, p_meta );
+            InputUpdateMeta( p_input, p_meta );
+        }
         p_access->info.i_update &= ~INPUT_UPDATE_META;
     }
     if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
@@ -2205,13 +2298,6 @@ static int UpdateFromAccess( input_thread_t *p_input )
     }
 
     p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
-
-    /* Hmmm only works with master input */
-    if( p_input->p->input.p_access == p_access )
-        return UpdateTitleSeekpoint( p_input,
-                                     p_access->info.i_title,
-                                     p_access->info.i_seekpoint );
-    return 1;
 }
 
 /*****************************************************************************
@@ -2220,10 +2306,8 @@ static int UpdateFromAccess( input_thread_t *p_input )
 static input_source_t *InputSourceNew( input_thread_t *p_input )
 {
     VLC_UNUSED(p_input);
-    input_source_t *in = malloc( sizeof( input_source_t ) );
-    if( in )
-        memset( in, 0, sizeof( input_source_t ) );
-    return in;
+
+    return calloc( 1,  sizeof( input_source_t ) );
 }
 
 /*****************************************************************************
@@ -2535,17 +2619,17 @@ static int InputSourceInit( input_thread_t *p_input,
         if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
                              &attachment, &i_attachment ) )
         {
-            vlc_mutex_lock( &p_input->p->input.p_item->lock );
+            vlc_mutex_lock( &p_input->p->p_item->lock );
             AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
                               i_attachment, attachment );
-            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
+            vlc_mutex_unlock( &p_input->p->p_item->lock );
         }
     }
-    if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
+    if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) && f_fps > 0.0 )
     {
-        vlc_mutex_lock( &p_input->p->input.p_item->lock );
-        in->f_fps = f_fps;
-        vlc_mutex_unlock( &p_input->p->input.p_item->lock );
+        vlc_mutex_lock( &p_input->p->p_item->lock );
+        p_input->p->f_fps = f_fps;
+        vlc_mutex_unlock( &p_input->p->p_item->lock );
     }
 
     if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
@@ -2593,6 +2677,58 @@ static void InputSourceClean( input_source_t *in )
     }
 }
 
+/*****************************************************************************
+ * InputSourceMeta:
+ *****************************************************************************/
+static void InputSourceMeta( input_thread_t *p_input,
+                             input_source_t *p_source, vlc_meta_t *p_meta )
+{
+    access_t *p_access = p_source->p_access;
+    demux_t *p_demux = p_source->p_demux;
+
+    /* XXX Remember that checking against p_item->p_meta->i_status & ITEM_PREPARSED
+     * is a bad idea */
+
+    /* Read access meta */
+    if( p_access )
+        access_Control( p_access, ACCESS_GET_META, p_meta );
+
+    /* Read demux meta */
+    demux_Control( p_demux, DEMUX_GET_META, p_meta );
+
+    /* If the demux report unsupported meta data, try an external "meta reader" */
+    bool b_bool;
+    if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
+        return;
+    if( !b_bool )
+        return;
+
+    demux_meta_t *p_demux_meta = p_demux->p_private = calloc( 1, sizeof(*p_demux_meta) );
+    if( !p_demux_meta )
+        return;
+
+    module_t *p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
+    if( p_id3 )
+    {
+        if( p_demux_meta->p_meta )
+        {
+            vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
+            vlc_meta_Delete( p_demux_meta->p_meta );
+        }
+
+        if( p_demux_meta->i_attachments > 0 )
+        {
+            vlc_mutex_lock( &p_input->p->p_item->lock );
+            AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
+                              p_demux_meta->i_attachments, p_demux_meta->attachments );
+            vlc_mutex_unlock( &p_input->p->p_item->lock );
+        }
+        module_unneed( p_demux, p_id3 );
+    }
+    free( p_demux_meta );
+}
+
+
 static void SlaveDemux( input_thread_t *p_input )
 {
     int64_t i_time;
@@ -2629,13 +2765,13 @@ static void SlaveDemux( input_thread_t *p_input )
                 if( i_stime >= i_time )
                     break;
 
-                if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
+                if( ( i_ret = demux_Demux( in->p_demux ) ) <= 0 )
                     break;
             }
         }
         else
         {
-            i_ret = in->p_demux->pf_demux( in->p_demux );
+            i_ret = demux_Demux( in->p_demux );
         }
 
         if( i_ret <= 0 )
@@ -2651,8 +2787,6 @@ static void SlaveSeek( input_thread_t *p_input )
     int64_t i_time;
     int i;
 
-    if( !p_input ) return;
-
     if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
     {
         msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
@@ -2683,14 +2817,12 @@ static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
 {
     vlc_value_t val;
 
-    if( !p_meta ) return;
-
     /* Get meta information from user */
-#define GET_META( field, s ) \
+#define GET_META( field, s ) do { \
     var_Get( p_input, (s), &val );  \
-    if( *val.psz_string ) \
+    if( val.psz_string && *val.psz_string ) \
         vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
-    free( val.psz_string )
+    free( val.psz_string ); } while(0)
 
     GET_META( Title, "meta-title" );
     GET_META( Artist, "meta-artist" );
@@ -2702,75 +2834,16 @@ static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
 #undef GET_META
 }
 
-/*****************************************************************************
- * InputGetExtraFiles
- *  Autodetect extra input list
- *****************************************************************************/
-static char *InputGetExtraFiles( input_thread_t *p_input,
-                                 const char *psz_access, const char *psz_path )
-{
-    char *psz_list = NULL;
-
-    if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
-        return NULL;
-
-
-    const char *psz_ext = strrchr( psz_path, '.' );
-    if( !psz_ext || strcmp( psz_ext, ".001" ) )
-        return NULL;
-
-    char *psz_file = strdup( psz_path );
-    if( !psz_file )
-        return NULL;
-
-    /* Try to list .xyz files */
-    for( int i = 2; i < 999; i++ )
-    {
-        char *psz_ext = strrchr( psz_file, '.' );
-        struct stat st;
-
-        snprintf( psz_ext, 5, ".%.3d", i );
-
-        if( utf8_stat( psz_file, &st )
-         || !S_ISREG( st.st_mode ) || !st.st_size )
-            continue;
-
-        msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
-
-        if( psz_list )
-        {
-            char *psz_old = psz_list;
-            /* FIXME how to handle file with ',' ?*/
-            if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
-            {
-                psz_list = psz_old;
-                break;
-            }
-        }
-        else
-        {
-            psz_list = strdup( psz_file );
-        }
-    }
-    free( psz_file );
-
-    return psz_list;
-}
-
 /*****************************************************************************
  * InputUpdateMeta: merge p_item meta data with p_meta taking care of
  * arturl and locking issue.
  *****************************************************************************/
 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
 {
-    input_item_t *p_item = p_input->p->input.p_item;
-    char * psz_arturl = NULL;
-    char *psz_title = NULL;
-
-    if( !p_meta )
-        return;
+    input_item_t *p_item = p_input->p->p_item;
 
-    psz_arturl = input_item_GetArtURL( p_item );
+    char *psz_title = NULL;
+    char *psz_arturl = input_item_GetArtURL( p_item );
 
     vlc_mutex_lock( &p_item->lock );
 
@@ -2811,7 +2884,6 @@ static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
     /** \todo handle sout meta */
 }
 
-
 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
                               int i_new, input_attachment_t **pp_new )
 {
@@ -2829,80 +2901,63 @@ static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_atta
     *pi_attachment = i_attachment;
     *ppp_attachment = attachment;
 }
-
-static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
+/*****************************************************************************
+ * InputGetExtraFiles
+ *  Autodetect extra input list
+ *****************************************************************************/
+static char *InputGetExtraFiles( input_thread_t *p_input,
+                                 const char *psz_access, const char *psz_path )
 {
-    int i;
+    char *psz_list = NULL;
 
-    if( p_input->b_preparsing )
-        return;
+    if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
+        return NULL;
 
-    if( p_input->p->input.p_access )
-        access_Control( p_input->p->input.p_access, ACCESS_GET_META,
-                         p_meta );
 
-    /* Get meta data from slave input */
-    for( i = 0; i < p_input->p->i_slave; i++ )
-    {
-        DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
-        if( p_input->p->slave[i]->p_access )
-        {
-            access_Control( p_input->p->slave[i]->p_access,
-                             ACCESS_GET_META, p_meta );
-        }
-    }
-}
+    const char *psz_ext = strrchr( psz_path, '.' );
+    if( !psz_ext || strcmp( psz_ext, ".001" ) )
+        return NULL;
 
-static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
-{
-    bool b_bool;
-    module_t *p_id3;
+    char *psz_file = strdup( psz_path );
+    if( !psz_file )
+        return NULL;
 
-#if 0
-    /* XXX I am not sure it is a great idea, besides, there is more than that
-     * if we want to do it right */
-    vlc_mutex_lock( &p_item->lock );
-    if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
+    /* Try to list .xyz files */
+    for( int i = 2; i < 999; i++ )
     {
-        vlc_mutex_unlock( &p_item->lock );
-        return;
-    }
-    vlc_mutex_unlock( &p_item->lock );
-#endif
+        char *psz_ext = strrchr( psz_file, '.' );
+        struct stat st;
 
-    demux_Control( p_demux, DEMUX_GET_META, p_meta );
-    if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
-        return;
-    if( !b_bool )
-        return;
+        snprintf( psz_ext, 5, ".%.3d", i );
 
-    p_demux->p_private = malloc( sizeof( demux_meta_t ) );
-    if(! p_demux->p_private )
-        return;
+        if( utf8_stat( psz_file, &st )
+         || !S_ISREG( st.st_mode ) || !st.st_size )
+            continue;
 
-    p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
-    if( p_id3 )
-    {
-        demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
+        msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
 
-        if( p_demux_meta->p_meta )
+        if( psz_list )
         {
-            vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
-            vlc_meta_Delete( p_demux_meta->p_meta );
+            char *psz_old = psz_list;
+            /* FIXME how to handle file with ',' ?*/
+            if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
+            {
+                psz_list = psz_old;
+                break;
+            }
         }
-
-        if( p_demux_meta->i_attachments > 0 )
+        else
         {
-            vlc_mutex_lock( &p_input->p->input.p_item->lock );
-            AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
-                              p_demux_meta->i_attachments, p_demux_meta->attachments );
-            vlc_mutex_unlock( &p_input->p->input.p_item->lock );
+            psz_list = strdup( psz_file );
         }
-        module_unneed( p_demux, p_id3 );
     }
-    free( p_demux->p_private );
+    free( psz_file );
+
+    return psz_list;
 }
 
+
+/* */
 static void input_ChangeState( input_thread_t *p_input, int i_state )
 {
     const bool b_changed = p_input->i_state != i_state;
@@ -2915,7 +2970,7 @@ static void input_ChangeState( input_thread_t *p_input, int i_state )
 
     if( b_changed )
     {
-        input_item_SetErrorWhenReading( p_input->p->input.p_item, p_input->b_error );
+        input_item_SetErrorWhenReading( p_input->p->p_item, p_input->b_error );
         input_SendEventState( p_input, i_state );
     }
 }