From 8ec7141218bce0a935a8a23cdd00fdaf8c09f09e Mon Sep 17 00:00:00 2001 From: Pauljd Date: Mon, 9 Nov 2026 17:34:14 +0000 Subject: [PATCH] Frame rate from the library, so the TV switches before the first frame PlaybackInfo's RealFrameRate (or AverageFrameRate) is carried in the plan, and the provider contract gains an optional frameRate that every provider can fill (Plex and local files later); without it the first frames are measured as before. Full screen is a dialog, so the preference is set on the dialog's window as well as the activity's. Still off unless the frame-rate switch is on. Co-Authored-By: Claude Opus 5.5 --- .../tv/orbit/classic/EmbyPlaybackDialog.kt | 21 ++++++++++++------- .../tv/orbit/classic/EmbyPlaybackProtocol.kt | 12 +++++++++-- .../java/tv/orbit/classic/EmbyProvider.kt | 2 +- .../java/tv/orbit/classic/MediaProvider.kt | 7 ++++++- .../tv/orbit/classic/PlayerSwitchesTest.kt | 10 +++++++++ 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt b/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt index 6d0aac33..798b4998 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt @@ -323,22 +323,29 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl } DisposableEffect(player) { onDispose { watchAudioClock(false) } } // Frame-rate matching (9 November 2026; off unless switched on): the TV's refresh follows the programme's, and goes - // back to the TV's own choice when the programme closes. + // back to the TV's own choice when the programme closes. Full screen is a dialog window over the activity, so the + // preference is set on both: Android takes it from the window on top. + val dialogWindow = (androidx.compose.ui.platform.LocalView.current.parent as? androidx.compose.ui.window.DialogWindowProvider)?.window DisposableEffect(player) { - val window = generateSequence(context) { (it as? android.content.ContextWrapper)?.baseContext }.filterIsInstance().firstOrNull()?.window + val windows = listOfNotNull(dialogWindow, generateSequence(context) { (it as? android.content.ContextWrapper)?.baseContext }.filterIsInstance().firstOrNull()?.window) + fun match(fps: Float) = windows.forEach { FrameRateMatch.apply(it, fps) } val matcher = object : Player.Listener { override fun onTracksChanged(tracks: Tracks) { - if (!PlayerSwitches.frameRate || window == null) return + if (!PlayerSwitches.frameRate || plan.frameRate != null) return val fps = tracks.groups.firstOrNull { it.type == C.TRACK_TYPE_VIDEO && it.isSelected }?.getTrackFormat(0)?.frameRate ?: return - if (fps > 0) FrameRateMatch.apply(window, fps) + if (fps > 0) match(fps) } } // Most files don't state their frame rate (9 November 2026: none of the test MKVs or AVIs did), so it's measured // from the first frames' timestamps instead, and applied on the main thread. - val probe = FrameRateProbe { fps -> if (PlayerSwitches.frameRate && window != null) android.os.Handler(android.os.Looper.getMainLooper()).post { FrameRateMatch.apply(window, fps) } } + val probe = FrameRateProbe { fps -> if (PlayerSwitches.frameRate) android.os.Handler(android.os.Looper.getMainLooper()).post { match(fps) } } player.addListener(matcher) - if (PlayerSwitches.frameRate) player.setVideoFrameMetadataListener(probe) - onDispose { player.removeListener(matcher); runCatching { player.clearVideoFrameMetadataListener(probe) }; window?.let(FrameRateMatch::reset) } + // Known from the library: switched now, before the first frame, so the TV blanks as you tune in and not after. + // Otherwise measured from the first frames. + val known = plan.frameRate + if (PlayerSwitches.frameRate && known != null) match(known) + else if (PlayerSwitches.frameRate) player.setVideoFrameMetadataListener(probe) + onDispose { player.removeListener(matcher); runCatching { player.clearVideoFrameMetadataListener(probe) }; windows.forEach(FrameRateMatch::reset) } } val covered = ident != null // Luna's goodnight: the programme fades out beneath her, and back up if a button keeps Orbit on. diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackProtocol.kt b/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackProtocol.kt index b73db705..2beb6cfc 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackProtocol.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackProtocol.kt @@ -11,7 +11,15 @@ private const val MAX_SERIES_COPIES = 4 internal class PlaybackSubtitle(val url: String, val mime: String, val language: String?, val label: String) internal class EmbyPlaybackPlan(val item: SampleItem, val url: String, val sourceId: String, - val playSessionId: String, val subtitles: List, val durationMs: Long? = item.durationMs) + val playSessionId: String, val subtitles: List, val durationMs: Long? = item.durationMs, + /** The picture's frame rate as the library knows it (9 November 2026), so the TV can switch before the first frame. */ + val frameRate: Float? = null) + +/** A video stream's frame rate from the server's reply: the real rate, else the average; null when it's missing or odd. */ +internal fun streamFrameRate(streams: JSONArray?): Float? { + val video = (0 until minOf(streams?.length() ?: 0, 32)).mapNotNull { streams?.optJSONObject(it) }.firstOrNull { it.optString("Type") == "Video" } ?: return null + return listOf("RealFrameRate", "AverageFrameRate").map { video.optDouble(it) }.firstOrNull { !it.isNaN() && it in 10.0..121.0 }?.toFloat() +} internal object PlaybackUrls { fun isProvider(uri: URI, server: String = EmbyServer.origin): Boolean { @@ -147,7 +155,7 @@ internal class EmbySampleSession(private val transport: EmbyTransport, private v EmbyClient.clean(stream.optString("DisplayTitle").ifBlank { "Subtitles ${index + 1}" }, 80)) } val runtime = (source.optLong("RunTimeTicks", 0) / 10_000).takeIf { it > 0 } ?: item.durationMs - return EmbyPlaybackPlan(item, url, sourceId, playId, subtitles, runtime) + return EmbyPlaybackPlan(item, url, sourceId, playId, subtitles, runtime, streamFrameRate(streams)) } fun checkToonamiSeries(startIndex: Int = 0, checkActive: () -> Unit = {}): List = discoverToonamiSeries(transport, userId, ::currentToken, startIndex, checkActive) diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyProvider.kt b/android/app/src/main/java/tv/orbit/classic/EmbyProvider.kt index 7f67dc54..6a5de3b5 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyProvider.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyProvider.kt @@ -92,7 +92,7 @@ internal class EmbyProvider( plans[item.value] = plan PlaybackPlan(plan.url, session.mediaHeaders(plan.url), subtitles = plan.subtitles.mapIndexed { i, s -> TrackInfo("$i", s.language, s.label, external = true) }, - container = plan.url.substringBefore('?').substringAfterLast('.'), directPlay = true) + container = plan.url.substringBefore('?').substringAfterLast('.'), directPlay = true, frameRate = plan.frameRate) } override suspend fun reportProgress(item: ItemId, positionMs: Long, state: PlaybackState) { diff --git a/android/app/src/main/java/tv/orbit/classic/MediaProvider.kt b/android/app/src/main/java/tv/orbit/classic/MediaProvider.kt index 356d7aef..ece61600 100644 --- a/android/app/src/main/java/tv/orbit/classic/MediaProvider.kt +++ b/android/app/src/main/java/tv/orbit/classic/MediaProvider.kt @@ -119,7 +119,12 @@ internal data class TrackInfo(val id: String, val language: String? = null, val * string with them, so a plan can be logged or shown without giving anything away. */ internal class PlaybackPlan(val uri: String, val headers: Map, val subtitles: List = emptyList(), - val audio: List = emptyList(), val container: String? = null, val directPlay: Boolean = true) { + val audio: List = emptyList(), val container: String? = null, val directPlay: Boolean = true, + /** + * The picture's frame rate when the provider knows it (9 November 2026): Emby, Jellyfin and Silo say it in PlaybackInfo, + * Plex in its stream details, and a local file in its headers. Null means Orbit measures the first frames instead. + */ + val frameRate: Float? = null) { fun loggable(): String = "plan ${uri.substringBefore('?').substringAfterLast('/')} container=${container ?: "?"} " + "subtitles=${subtitles.size} audio=${audio.size} headers=${headers.size}" override fun toString() = loggable() diff --git a/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt b/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt index a1d13e74..d03195f5 100644 --- a/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt @@ -67,4 +67,14 @@ class PlayerSwitchesTest { assertEquals("video/x-ms-mpeg4", videoMimeFor("msmpeg4v3")) assertEquals("video/mp4v-es", videoMimeFor("xvid")) } + + @Test fun theLibrarysFrameRateIsRead() { + fun streams(vararg video: Pair) = org.json.JSONArray().put(org.json.JSONObject().put("Type", "Audio")) + .put(org.json.JSONObject().put("Type", "Video").apply { video.forEach { (k, v) -> put(k, v) } }) + assertEquals(23.976f, streamFrameRate(streams("RealFrameRate" to 23.976, "AverageFrameRate" to 23.9))!!, 0.0001f) + assertEquals(25f, streamFrameRate(streams("AverageFrameRate" to 25.0))!!, 0.0001f) + assertNull(streamFrameRate(streams("RealFrameRate" to 1000.0))) + assertNull(streamFrameRate(streams())) + assertNull(streamFrameRate(null)) + } }