0.123.1 (versionCode 316): frame rate measured from frames, seeks aren't stalls, DivX 3 unplayable

Found by the on-TV playtest matrix: no test clip states its frame rate, refilling
after a seek counted as a stall (would stop the overnight comparison after three
tunes), and DivX 3 AVIs played as sound alone. Third matrix run: every case as
intended, including 23.976/24/50 Hz switching and the TV returning to 59.94.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 16:29:21 +00:00
co-authored by Claude Opus 5.5
parent d9131d3979
commit 584aa16321
9 changed files with 127 additions and 12 deletions
@@ -10,7 +10,9 @@ internal fun videoMimeFor(codec: String): String? = when (codec.trim().lowercase
"vp9" -> "video/x-vnd.on2.vp9"
"av1" -> "video/av01"
"mpeg2video", "mpeg2" -> "video/mpeg2"
"mpeg4", "msmpeg4v3", "divx", "xvid" -> "video/mp4v-es"
"mpeg4", "divx", "xvid" -> "video/mp4v-es"
// Microsoft's MPEG-4 (DivX 3 and older) isn't MPEG-4 Part 2: no Android TV decodes it (9 November 2026, tested).
"msmpeg4v1", "msmpeg4v2", "msmpeg4v3", "msmpeg4", "div3" -> "video/x-ms-mpeg4"
"h263" -> "video/3gpp"
"vc1", "wmv3" -> "video/wvc1"
else -> null
@@ -187,8 +187,11 @@ internal object CatalogueShadow {
/** What the full-screen player is doing, for background work that must give way to it (9 November 2026). */
internal object PlaybackHealth {
@Volatile var playing = false
/** Times playback has stalled to rebuffer after it started, since Orbit opened. */
/** Times playback has stalled to rebuffer after it started, since Orbit opened; refilling just after a seek isn't counted. */
@Volatile var stalls = 0
/** When the player last seeked (elapsedRealtime). */
@Volatile var seekedAt = 0L
const val SEEK_SETTLE_MS = 5_000L
}
/**
@@ -250,6 +250,16 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl
format.sampleMimeType, format.channelCount, format.sampleRate, format.language, format.width, format.height))
}
val video = tracks.groups.filter { it.type == C.TRACK_TYPE_VIDEO }
// An AVI whose picture Media3 can't even read (DivX 3, found 9 November 2026) comes through as sound alone.
val aviWithoutPicture = video.isEmpty() && tracks.groups.any { it.type == C.TRACK_TYPE_AUDIO } && plan.url.substringBefore('?').endsWith(".avi")
if (scheduled != null && aviWithoutPicture) {
android.util.Log.w("OrbitSchedule", "channel=${scheduled.channel.number} noVideoTrack=avi")
player.stop(); ready = false
Unplayable.note(plan.item.id, plan.item.title, scheduled.channel.number, "This TV can't read the picture in this AVI file.")
onUnplayable?.invoke(plan.item.id)
failure = "This programme won't play on this box. Orbit moves on at ${clock(scheduled.programme.endMs)}."
return
}
// Sound with no picture: the box can't decode this file's video.
if (scheduled != null && video.isNotEmpty() && video.none { it.isSupported }) {
android.util.Log.w("OrbitSchedule", "channel=${scheduled.channel.number} noVideoDecoder=${video.first().getTrackFormat(0).sampleMimeType}")
@@ -264,8 +274,12 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl
subtitleChoice(subtitlePreference?.value, audioLanguages(), available.map { it.getTrackFormat(0).language.orEmpty() })?.let { chooseSubtitle(it, available) }
}
}
override fun onPositionDiscontinuity(oldPosition: Player.PositionInfo, newPosition: Player.PositionInfo, reason: Int) {
if (reason == Player.DISCONTINUITY_REASON_SEEK || reason == Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT) PlaybackHealth.seekedAt = SystemClock.elapsedRealtime()
}
override fun onPlaybackStateChanged(state: Int) {
if (state == Player.STATE_BUFFERING && ready) PlaybackHealth.stalls++
// A stall is buffering that nobody asked for: refilling after a seek (tuning in, Catch Up, skipping) isn't one.
if (state == Player.STATE_BUFFERING && ready && SystemClock.elapsedRealtime() - PlaybackHealth.seekedAt > PlaybackHealth.SEEK_SETTLE_MS) PlaybackHealth.stalls++
PlaybackHealth.playing = state == Player.STATE_READY || state == Player.STATE_BUFFERING
buffering = state == Player.STATE_BUFFERING
if (state == Player.STATE_ENDED && scheduled != null) onEndedEarly?.invoke()
@@ -311,11 +325,15 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl
override fun onTracksChanged(tracks: Tracks) {
if (!PlayerSwitches.frameRate || window == null) return
val fps = tracks.groups.firstOrNull { it.type == C.TRACK_TYPE_VIDEO && it.isSelected }?.getTrackFormat(0)?.frameRate ?: return
FrameRateMatch.apply(window, fps)
if (fps > 0) FrameRateMatch.apply(window, 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) } }
player.addListener(matcher)
onDispose { player.removeListener(matcher); window?.let(FrameRateMatch::reset) }
if (PlayerSwitches.frameRate) player.setVideoFrameMetadataListener(probe)
onDispose { player.removeListener(matcher); runCatching { player.clearVideoFrameMetadataListener(probe) }; window?.let(FrameRateMatch::reset) }
}
val covered = ident != null
// Luna's goodnight: the programme fades out beneath her, and back up if a button keeps Orbit on.
@@ -56,3 +56,43 @@ internal object FrameRateMatch {
window.attributes = window.attributes.also { it.preferredDisplayModeId = 0 }
}
}
/**
* Measures a video's frame rate from the presentation times of its first frames (9 November 2026), for files that don't
* state it. Calls [onRate] once with the rate; called on the playback thread. Timestamps in MKV are often whole
* milliseconds and AVI's can arrive out of order, so it takes about five seconds of frames and works from the span.
*/
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
internal class FrameRateProbe(private val onRate: (Float) -> Unit) : androidx.media3.exoplayer.video.VideoFrameMetadataListener {
private val times = ArrayList<Long>(FRAMES)
@Volatile private var done = false
override fun onVideoFrameAboutToBeRendered(presentationTimeUs: Long, releaseTimeNs: Long, format: androidx.media3.common.Format, mediaFormat: android.media.MediaFormat?) {
if (done) return
if (times.isNotEmpty() && kotlin.math.abs(presentationTimeUs - times.last()) > 1_000_000L) times.clear() // a seek: start again
times += presentationTimeUs
if (times.size < FRAMES) return
done = true
frameRateFrom(times)?.let(onRate)
}
companion object {
const val FRAMES = 120
/** The usual video rates; a measurement within 0.03 of one is taken as that rate exactly. */
private val STANDARD = floatArrayOf(23.976f, 24f, 25f, 29.97f, 30f, 47.952f, 48f, 50f, 59.94f, 60f)
/**
* The rate from frame times in microseconds. The times are sorted; the typical gap (the mean of those near the
* median, so a dropped frame doesn't count) says how many frame intervals the span holds, and the span gives the rate.
*/
fun frameRateFrom(timesUs: List<Long>): Float? {
val t = timesUs.distinct().sorted()
val gaps = t.zipWithNext { a, b -> b - a }.filter { it in 4_000L..200_000L }.sorted()
if (gaps.size < 5) return null
val median = gaps[gaps.size / 2]
val typical = gaps.filter { it < median * 1.5 && it > median * 0.5 }.average()
val span = t.last() - t.first()
val intervals = Math.round(span / typical)
if (intervals < 4) return null
val fps = (intervals * 1_000_000.0 / span).toFloat()
return STANDARD.minByOrNull { kotlin.math.abs(it - fps) }?.takeIf { kotlin.math.abs(it - fps) < 0.03f } ?: fps
}
}
}