-
-
Notifications
You must be signed in to change notification settings - Fork 139
Implement refresh rate matching for native player #997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TwoToneEddy
wants to merge
3
commits into
DonutWare:develop
Choose a base branch
from
TwoToneEddy:match-fr
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
android/app/src/main/kotlin/nl/jknaapen/fladder/utility/RefreshRateHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package nl.jknaapen.fladder.utility | ||
|
|
||
| import android.hardware.display.DisplayManager | ||
| import android.os.Build | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.util.Log | ||
| import android.view.Display | ||
| import android.view.Window | ||
| import kotlinx.coroutines.CompletableDeferred | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.withContext | ||
| import kotlinx.coroutines.withTimeoutOrNull | ||
| import kotlin.math.roundToInt | ||
| import kotlin.time.Duration.Companion.seconds | ||
|
|
||
| private const val TAG = "RefreshRateHelper" | ||
|
|
||
| suspend fun applyRefreshRate( | ||
| window: Window, | ||
| displayManager: DisplayManager, | ||
| videoWidth: Int, | ||
| videoHeight: Int, | ||
| frameRate: Float, | ||
| ) = withContext(Dispatchers.IO) { | ||
| val display = displayManager.getDisplay(Display.DEFAULT_DISPLAY) | ||
| val displayModes = display.supportedModes | ||
| .orEmpty() | ||
| .map { RefreshRateDisplayMode(it) } | ||
| .sortedWith( | ||
| compareByDescending<RefreshRateDisplayMode> { it.physicalWidth * it.physicalHeight } | ||
| .thenBy { it.refreshRateRounded } | ||
| ) | ||
|
|
||
| val currentMode = display.mode | ||
| val targetMode = findDisplayMode( | ||
| displayModes = displayModes, | ||
| streamWidth = videoWidth, | ||
| streamHeight = videoHeight, | ||
| targetFrameRate = frameRate, | ||
| ) | ||
|
|
||
| Log.d(TAG, "Video: ${videoWidth}x${videoHeight} @ ${frameRate}fps — target mode: $targetMode, current: $currentMode") | ||
|
|
||
| if (targetMode == null || targetMode.modeId == currentMode.modeId) return@withContext | ||
|
|
||
| val listener = DisplayChangeListener(display.displayId) | ||
| displayManager.registerDisplayListener(listener, Handler(Looper.getMainLooper())) | ||
| try { | ||
| withContext(Dispatchers.Main) { | ||
| val attrs = window.attributes | ||
| attrs.preferredDisplayModeId = targetMode.modeId | ||
| window.attributes = attrs | ||
| } | ||
| withTimeoutOrNull(5.seconds) { listener.deferred.await() } | ||
| } finally { | ||
| displayManager.unregisterDisplayListener(listener) | ||
| } | ||
|
|
||
| // Wait for non-seamless switches (https://developer.android.com/media/optimize/performance/frame-rate) | ||
| val targetRateMillis = (targetMode.refreshRate * 1000).roundToInt() | ||
| val currentRateMillis = (currentMode.refreshRate * 1000).roundToInt() | ||
| val isSeamless = targetRateMillis == currentRateMillis || | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
| currentMode.alternativeRefreshRates | ||
| .map { (it * 1000).roundToInt() } | ||
| .any { it % targetRateMillis == 0 } | ||
| } else { | ||
| false | ||
| } | ||
| if (!isSeamless) { | ||
| delay(2.seconds) | ||
| } | ||
| } | ||
|
|
||
| fun resetRefreshRate(window: Window) { | ||
| val attrs = window.attributes | ||
| attrs.preferredDisplayModeId = 0 | ||
| window.attributes = attrs | ||
| } | ||
|
|
||
| private fun findDisplayMode( | ||
| displayModes: List<RefreshRateDisplayMode>, | ||
| streamWidth: Int, | ||
| streamHeight: Int, | ||
| targetFrameRate: Float, | ||
| ): RefreshRateDisplayMode? { | ||
| val streamRate = (targetFrameRate * 1000).roundToInt() | ||
| val candidates = displayModes | ||
| .filter { it.physicalWidth >= streamWidth && it.physicalHeight >= streamHeight } | ||
| .filter { frameRateMatches(it.refreshRateRounded, streamRate) } | ||
|
|
||
| // Exact resolution + exact frame rate | ||
| return candidates.firstOrNull { | ||
| it.physicalWidth == streamWidth && it.physicalHeight == streamHeight && it.refreshRateRounded == streamRate | ||
| } | ||
| // Next highest resolution + exact frame rate | ||
| ?: candidates.lastOrNull { | ||
| it.physicalWidth >= streamWidth && it.physicalHeight >= streamHeight && it.refreshRateRounded == streamRate | ||
| } | ||
| // Exact resolution + acceptable frame rate | ||
| ?: candidates.lastOrNull { | ||
| it.physicalWidth == streamWidth && it.physicalHeight == streamHeight | ||
| } | ||
| // Next highest resolution + acceptable frame rate | ||
| ?: candidates.lastOrNull { | ||
| it.physicalWidth >= streamWidth && it.physicalHeight >= streamHeight | ||
| } | ||
| // Highest resolution at exact frame rate | ||
| ?: displayModes | ||
| .filter { it.refreshRateRounded == streamRate } | ||
| .maxByOrNull { it.physicalWidth * it.physicalHeight } | ||
| // Fallback: highest resolution | ||
| ?: displayModes.maxByOrNull { it.physicalWidth * it.physicalHeight } | ||
| } | ||
|
|
||
| private fun frameRateMatches(refreshRateRounded: Int, streamRate: Int): Boolean { | ||
| return refreshRateRounded % streamRate == 0 || | ||
| refreshRateRounded == (streamRate * 2.5).roundToInt() | ||
| } | ||
|
|
||
| data class RefreshRateDisplayMode( | ||
| val modeId: Int, | ||
| val physicalWidth: Int, | ||
| val physicalHeight: Int, | ||
| val refreshRate: Float, | ||
| ) { | ||
| val refreshRateRounded: Int = (refreshRate * 1000).roundToInt() | ||
|
|
||
| constructor(mode: Display.Mode) : this( | ||
| mode.modeId, | ||
| mode.physicalWidth, | ||
| mode.physicalHeight, | ||
| mode.refreshRate, | ||
| ) | ||
| } | ||
|
|
||
| private class DisplayChangeListener(val displayId: Int) : DisplayManager.DisplayListener { | ||
| val deferred = CompletableDeferred<Unit>() | ||
| override fun onDisplayAdded(displayId: Int) {} | ||
| override fun onDisplayRemoved(displayId: Int) {} | ||
| override fun onDisplayChanged(displayId: Int) { | ||
| if (displayId == this.displayId) deferred.complete(Unit) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=2G -XX:+HeapDumpOnOutOfMemoryError | ||
| android.useAndroidX=true | ||
| android.enableJetifier=true | ||
| # This builtInKotlin flag was added automatically by Flutter migrator | ||
| android.builtInKotlin=false | ||
| # This newDsl flag was added automatically by Flutter migrator | ||
| android.newDsl=false | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these might be added because the flutter version you use is different from the version in .fvmrc? Probably best to remove this if we are unsure what they do exactly.