[vlc-commits] playlist/engine: RateOffsetCallback: refactor

Filip Roséen git at videolan.org
Sun May 21 21:21:56 CEST 2017


vlc | branch: master | Filip Roséen <filip at atch.se> | Sun May 21 20:48:12 2017 +0200| [305017310989f1f449c1778b57fdea7695d813de] | committer: Rémi Denis-Courmont

playlist/engine: RateOffsetCallback: refactor

Besides the shortening of the implementation, the refactor also make
sure that we return an error-code if var_SetFloat fails - instead of
silently pretending everything was ok.

Signed-off-by: Rémi Denis-Courmont <remi at remlab.net>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=305017310989f1f449c1778b57fdea7695d813de
---

 src/playlist/engine.c | 45 ++++++++++++++++-----------------------------
 1 file changed, 16 insertions(+), 29 deletions(-)

diff --git a/src/playlist/engine.c b/src/playlist/engine.c
index eeacbc59e7..848a86ad45 100644
--- a/src/playlist/engine.c
+++ b/src/playlist/engine.c
@@ -124,46 +124,33 @@ static int RateOffsetCallback( vlc_object_t *obj, char const *psz_cmd,
     playlist_t *p_playlist = (playlist_t *)obj;
     VLC_UNUSED(oldval); VLC_UNUSED(p_data); VLC_UNUSED(newval);
 
-    static const float pf_rate[] = {
+    static const float rates[] = {
         1.0/64, 1.0/32, 1.0/16, 1.0/8, 1.0/4, 1.0/3, 1.0/2, 2.0/3,
         1.0/1,
         3.0/2, 2.0/1, 3.0/1, 4.0/1, 8.0/1, 16.0/1, 32.0/1, 64.0/1,
     };
-    const size_t i_rate_count = sizeof(pf_rate)/sizeof(*pf_rate);
-
-    float f_rate;
-    struct input_thread_t *input;
 
     PL_LOCK;
-    input = pl_priv( p_playlist )->p_input;
-    f_rate = var_GetFloat( input ? (vlc_object_t *)input : obj, "rate" );
+    input_thread_t *input = pl_priv( p_playlist )->p_input;
+    float current_rate = var_GetFloat( input ? VLC_OBJECT( input ) : obj, "rate" );
     PL_UNLOCK;
 
-    if( !strcmp( psz_cmd, "rate-faster" ) )
-    {
-        /* compensate for input rounding errors */
-        float r = f_rate * 1.1f;
-        for( size_t i = 0; i < i_rate_count; i++ )
-            if( r < pf_rate[i] )
-            {
-                f_rate = pf_rate[i];
-                break;
-            }
-    }
-    else
+    const bool faster = !strcmp( psz_cmd, "rate-faster" );
+    float rate = current_rate * ( faster ? 1.1f : 0.9f );
+
+    /* find closest rate (if any) in the desired direction */
+    for( size_t i = 0; i < ARRAY_SIZE( rates ); ++i )
     {
-        /* compensate for input rounding errors */
-        float r = f_rate * .9f;
-        for( size_t i = 1; i < i_rate_count; i++ )
-            if( r <= pf_rate[i] )
-            {
-                f_rate = pf_rate[i - 1];
-                break;
-            }
+        if( ( faster && rates[i] > rate ) ||
+            (!faster && rates[i] >= rate && i ) )
+        {
+            rate = faster ? rates[i] : rates[i-1];
+            break;
+        }
     }
 
-    var_SetFloat( p_playlist, "rate", f_rate );
-    return VLC_SUCCESS;
+
+    return var_SetFloat( p_playlist, "rate", rate );
 }
 
 static int VideoSplitterCallback( vlc_object_t *p_this, char const *psz_cmd,



More information about the vlc-commits mailing list