Swap a heavy booked film to a lighter copy when it's tuned
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 11m3s
release-gate / verify (push) Failing after 14m48s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 2m1s

Films booked this week keep their 4K copies until each channel's weekly refresh.
openBooked now notices a heavy copy (EmbyPlaybackPlan.heavy, from copyWeight),
looks once for a lighter copy of the same film, plays it and updates the plan.
One search per film, only for heavy copies. Roadmap: copies matched to the
household's TV and server at set-up (Paul's idea).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 11:39:10 +00:00
co-authored by Claude Opus 5.5
parent 0bee17ec42
commit 9e75c9816f
4 changed files with 54 additions and 3 deletions
@@ -13,7 +13,9 @@ internal class PlaybackSubtitle(val url: String, val mime: String, val language:
internal class EmbyPlaybackPlan(val item: SampleItem, val url: String, val sourceId: String,
val playSessionId: String, val subtitles: List<PlaybackSubtitle>, 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)
val frameRate: Float? = null,
/** A heavy copy (4K, a very high bitrate, or lossless-only sound; see [copyWeight]): a lighter one may be worth finding. */
val heavy: Boolean = false)
/** 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? {
@@ -168,7 +170,8 @@ 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, streamFrameRate(streams))
return EmbyPlaybackPlan(item, url, sourceId, playId, subtitles, runtime, streamFrameRate(streams),
heavy = copyWeight(FilmCandidate(item.id, item.title, null, null, listOf(source))) >= 2)
}
fun checkToonamiSeries(startIndex: Int = 0, checkActive: () -> Unit = {}): List<SeriesMatches> =
discoverToonamiSeries(transport, userId, ::currentToken, startIndex, checkActive)
@@ -501,6 +504,10 @@ internal class EmbySampleSession(private val transport: EmbyTransport, private v
* first scan re-files titles). One search for the same title, matched exactly as a channel's list is; null when there's
* no copy, or only the one that's gone.
*/
/** Films already looked at for a lighter copy this session, so a heavy film is searched for at most once. */
private val lighterChecked = java.util.Collections.synchronizedSet(HashSet<String>())
fun lighterFilm(item: SampleItem): ScheduledMedia? = if (item.type != "Movie" || !lighterChecked.add(item.id)) null else refiledFilm(item)
fun refiledFilm(item: SampleItem): ScheduledMedia? {
if (item.type != "Movie") return null
val film = findFilm(transport, userId, ::currentToken, item.title, BoxDecoders::canDecode).film ?: return null
@@ -102,7 +102,18 @@ internal fun retimedPlan(plan: ChannelPlan, startMs: Long, actualMs: Long): Chan
* the schedule is updated to it, so the guide and every screen agree. Returns the plan and the programme as now booked.
*/
internal fun openBooked(session: EmbySampleSession, channel: SampleChannel, store: ScheduleStore, show: SampleBroadcast, live: Boolean, ahead: Boolean = false): Pair<EmbyPlaybackPlan, SampleBroadcast> {
try { return session.prepareForTuning(show.media.item, show.media.sourceId, ahead) { Thread.sleep(2_000) } to show }
try {
val plan = session.prepareForTuning(show.media.item, show.media.sourceId, ahead) { Thread.sleep(2_000) }
// A film booked as a heavy copy (13 November 2026: 4K remuxes drifting out of sync) swaps to a lighter copy of the same
// film when the library has one: one search, once per film, and the schedule is updated so it stays swapped.
if (live && plan.heavy) runCatching { session.lighterFilm(show.media.item) }.getOrNull()?.let { lighter ->
android.util.Log.i("OrbitSchedule", "channel=${channel.number} lighterCopy old=${show.media.item.id} new=${lighter.item.id}")
PlanEdits.replace(channel, store, session.accountId, show.startMs, show.media.item.id, lighter)
val booked = show.copy(media = lighter.copy(durationMs = show.media.durationMs), fileMs = null)
return session.prepareForTuning(lighter.item, lighter.sourceId, ahead) { Thread.sleep(2_000) } to booked
}
return plan to show
}
catch (failure: EmbyCheckException) {
if (failure.httpStatus != 404 && failure.httpStatus != 500) throw failure
val found = session.refiledFilm(show.media.item) ?: session.refiledEpisode(show.media.item) ?: throw failure
@@ -80,3 +80,31 @@ class CopyChoiceTest {
assertEquals("aaa1", resolveFilm(FilmIdentity("An American Werewolf in London", 1981), listOf(heavy)).film!!.id)
}
}
/** 13 November 2026: a film already booked as a heavy copy swaps to a lighter one when tuned, searched for once. */
class LighterCopyTest {
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com" }
@org.junit.After fun noServer() { EmbyServer.origin = "" }
private val booked = SampleItem("Fixture Werewolf", "Movie", 97, "", "heavy1", 97 * 60_000L)
private fun source(id: String, width: Int, bitrate: Long) = JSONObject().put("Id", id).put("Container", "mkv").put("Bitrate", bitrate)
.put("SupportsDirectStream", true).put("RunTimeTicks", 97 * 60_000L * 10_000)
.put("MediaStreams", JSONArray().put(JSONObject().put("Type", "Video").put("Codec", if (width > 1920) "hevc" else "h264").put("Width", width)))
@Test fun aHeavyCopyIsMarkedAndALighterOneFoundOnce() {
var searches = 0
val transport = EmbyTransport { r ->
when {
r.path.contains("/PlaybackInfo") -> EmbyReply(200, JSONObject().put("PlaySessionId", "play1").put("ErrorCode", JSONObject.NULL)
.put("MediaSources", JSONArray().put(source("srcheavy1", 3840, 54_000_000))).toString())
r.path.contains("IncludeItemTypes=Movie") -> { searches++; EmbyReply(200, JSONObject().put("Items", JSONArray()
.put(JSONObject().put("Id", "heavy1").put("Type", "Movie").put("Name", "Fixture Werewolf").put("ProductionYear", 1981).put("MediaSources", JSONArray().put(source("srcheavy1", 3840, 54_000_000))))
.put(JSONObject().put("Id", "light1").put("Type", "Movie").put("Name", "Fixture Werewolf").put("ProductionYear", 1981).put("MediaSources", JSONArray().put(source("srclight1", 1920, 9_000_000))))).toString()) }
else -> EmbyReply(404, "")
}
}
val session = EmbySampleSession(transport, "fixture-token-private", "fixture-user", EmbySample(listOf(booked), 1))
assertTrue(session.prepareForTuning(booked, "srcheavy1").heavy)
assertEquals("light1", session.lighterFilm(booked)!!.item.id)
assertNull(session.lighterFilm(booked))
assertEquals(1, searches)
}
}
+5
View File
@@ -294,6 +294,11 @@ capacity test and household-sized limits wait for the VPS's first scan.
Readiness review: https://claude.ai/artifact/1vegC5yGHxppXrFHW7v47v
**Copies matched to the household (Paul, 13 November 2026):** at first set-up, Orbit learns what the TV can show (4K, HDR,
Dolby Vision, its sound formats) and how fast the server streams to it, and picks each film's copy to suit. Today it simply
prefers the lightest copy the box can play (copyWeight), which suits a far-away server; a 4K HDR TV on a fast home server
might prefer the 4K copy. For RC2 or RC3.
## Future polish ideas
Paul's ideas, 1 November 2026. Not fixes and not scheduled.