[Android] Code cleanup

Nicolas Pomepuy git at videolan.org
Mon Sep 5 11:05:57 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Sep  1 06:58:24 2022 +0200| [50ab62f12d0bacf678b39642d9eff5ef535ad2ba] | committer: Nicolas Pomepuy

Code cleanup

> https://code.videolan.org/videolan/vlc-android/commit/50ab62f12d0bacf678b39642d9eff5ef535ad2ba
---

 .../src/org/videolan/vlc/util/FrameRateManager.kt  | 93 +++++-----------------
 1 file changed, 19 insertions(+), 74 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/util/FrameRateManager.kt b/application/vlc-android/src/org/videolan/vlc/util/FrameRateManager.kt
index 1a974f2c6..64518d468 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/FrameRateManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/FrameRateManager.kt
@@ -18,11 +18,9 @@ import org.videolan.vlc.PlaybackService
 import java.math.BigDecimal
 import java.math.RoundingMode
 
+private const val TAG = "VLC/FrameRateMatch"
+private const val SHORT_VIDEO_LENGTH = 300000
 class FrameRateManager(var context: Context, var service: PlaybackService) {
-    companion object {
-        private const val TAG = "VLC/FrameRateMatch"
-        private const val SHORT_VIDEO_LENGTH = 300000
-    }
 
     init {
         // listen for display change and resume play
@@ -46,38 +44,21 @@ class FrameRateManager(var context: Context, var service: PlaybackService) {
             }
         }
 
-        val displayManager =
-            context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
+        val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
         displayManager.registerDisplayListener(displayListener, Handler(Looper.getMainLooper()))
     }
 
-    fun matchFrameRate(
-        surfaceView: SurfaceView,
-        window: Window
-    ) {
+    fun matchFrameRate(surfaceView: SurfaceView, window: Window) {
         /* automatic frame rate switching for displays/HDMI
         most media will be either 23.976, 24, 25, 29.97, 30, 48, 50, 59.94, and 60 fps */
         service.mediaplayer.currentVideoTrack?.let { videoTrack ->
-            val videoFrameRate =
-                videoTrack.frameRateNum / videoTrack.frameRateDen.toFloat()
-            if (BuildConfig.DEBUG) {
-                Log.d(TAG, "Media frame rate - $videoFrameRate fps")
-            }
+            val videoFrameRate = videoTrack.frameRateNum / videoTrack.frameRateDen.toFloat()
             val surface = surfaceView.holder.surface
 
             when {
-                Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> setFrameRateS(
-                    videoFrameRate,
-                    surface
-                )
-                Build.VERSION.SDK_INT == Build.VERSION_CODES.R -> setFrameRateR(
-                    videoFrameRate,
-                    surface
-                )
-                Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> setFrameRateM(
-                    videoFrameRate,
-                    window
-                )
+                Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> setFrameRateS(videoFrameRate, surface)
+                Build.VERSION.SDK_INT == Build.VERSION_CODES.R -> setFrameRateR(videoFrameRate, surface)
+                Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> setFrameRateM(videoFrameRate, window)
                 else -> {}
             }
         }
@@ -85,44 +66,30 @@ class FrameRateManager(var context: Context, var service: PlaybackService) {
 
     @RequiresApi(Build.VERSION_CODES.R)
     fun setFrameRateR(videoFrameRate: Float, surface: Surface) {
-        if (BuildConfig.DEBUG) {
-            Log.d(TAG, "Optimal frame rate will be set by Android system")
-        }
+        if (BuildConfig.DEBUG) Log.d(TAG, "Optimal frame rate will be set by Android system")
 
         //Android 11 does not support Frame Rate Strategy
-        surface.setFrameRate(
-            videoFrameRate,
-            Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE
-        )
+        surface.setFrameRate(videoFrameRate, Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE)
     }
 
     @RequiresApi(Build.VERSION_CODES.S)
     fun setFrameRateS(videoFrameRate: Float, surface: Surface) {
-        if (BuildConfig.DEBUG) {
-            Log.d(TAG, "Optimal frame rate will be set by Android system")
-        }
+        if (BuildConfig.DEBUG) Log.d(TAG, "Optimal frame rate will be set by Android system")
 
         //on Android 12 and up supports Frame Rate Strategy
         //for short video less than 5 minutes, only change frame rate if seamless
         val display = context.display
 
         if (service.mediaplayer.length < SHORT_VIDEO_LENGTH) {
-            surface.setFrameRate(
-                videoFrameRate,
-                Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
-                Surface.CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS
-            )
+            surface.setFrameRate(videoFrameRate, Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE, Surface.CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS)
         } else {
             //detect if a non-seamless refresh rate switch is about to happen
             var seamless = false
             val androidDisplayManager: DisplayManager =
-                context.getSystemService(AppCompatActivity.DISPLAY_SERVICE) as DisplayManager
+                    context.getSystemService(AppCompatActivity.DISPLAY_SERVICE) as DisplayManager
             display?.mode?.alternativeRefreshRates?.let { refreshRates ->
                 for (rate in refreshRates) {
-                    if ((videoFrameRate.toString()
-                            .startsWith(rate.toString())) || (rate.toString()
-                            .startsWith(videoFrameRate.toString())) || rate % videoFrameRate == 0F
-                    ) {
+                    if ((videoFrameRate.toString().startsWith(rate.toString())) || (rate.toString().startsWith(videoFrameRate.toString())) || rate % videoFrameRate == 0F) {
                         seamless = true
                         break
                     }
@@ -131,20 +98,12 @@ class FrameRateManager(var context: Context, var service: PlaybackService) {
 
             if (seamless) {
                 //switch will be seamless
-                surface.setFrameRate(
-                    videoFrameRate,
-                    Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
-                    Surface.CHANGE_FRAME_RATE_ALWAYS
-                )
+                surface.setFrameRate(videoFrameRate, Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE, Surface.CHANGE_FRAME_RATE_ALWAYS)
             } else if (!seamless && (androidDisplayManager.matchContentFrameRateUserPreference == DisplayManager.MATCH_CONTENT_FRAMERATE_ALWAYS)) {
                 //switch will be non seamless, check if user has opted in for this at the OS level
                 //TODO: only included this here because Android guide makes it sound like seamless-behavior includes stuff like HDMI switching
                 //may have to remove this block since we intend to switch only if it will be seamless
-                surface.setFrameRate(
-                    videoFrameRate,
-                    Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE,
-                    Surface.CHANGE_FRAME_RATE_ALWAYS
-                )
+                surface.setFrameRate(videoFrameRate, Surface.FRAME_RATE_COMPATIBILITY_FIXED_SOURCE, Surface.CHANGE_FRAME_RATE_ALWAYS)
             }
         }
     }
@@ -165,17 +124,8 @@ class FrameRateManager(var context: Context, var service: PlaybackService) {
                         continue
                     }
 
-                    if (BuildConfig.DEBUG) {
-                        Log.d(TAG, "Supported display mode - $mode")
-                    }
-                    if (BigDecimal(videoFrameRate.toString()).setScale(
-                            1,
-                            RoundingMode.FLOOR
-                        ) == BigDecimal(mode.refreshRate.toString()).setScale(
-                            1,
-                            RoundingMode.FLOOR
-                        )
-                    ) {
+                    if (BuildConfig.DEBUG) Log.d(TAG, "Supported display mode - $mode")
+                    if (BigDecimal(videoFrameRate.toString()).setScale(1, RoundingMode.FLOOR) == BigDecimal(mode.refreshRate.toString()).setScale(1, RoundingMode.FLOOR)) {
                         //this is the best frame rate because it's exactly the same as source media
                         modeToUse = mode
                         break
@@ -186,12 +136,7 @@ class FrameRateManager(var context: Context, var service: PlaybackService) {
                     }
                 }
 
-                if (BuildConfig.DEBUG) {
-                    Log.d(
-                        TAG,
-                        "We will use ${modeToUse.refreshRate} frame rate"
-                    )
-                }
+                if (BuildConfig.DEBUG) Log.d(TAG, "We will use ${modeToUse.refreshRate} frame rate")
 
                 // set frame rate
                 if (modeToUse != currentMode) {



More information about the Android mailing list