From 9e75c9816f1a2e20a0ce62db460441fd9c5843df Mon Sep 17 00:00:00 2001 From: Pauljd Date: Fri, 13 Nov 2026 11:39:10 +0000 Subject: [PATCH] Swap a heavy booked film to a lighter copy when it's tuned 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 --- .../tv/orbit/classic/EmbyPlaybackProtocol.kt | 11 ++++++-- .../java/tv/orbit/classic/ProgrammeLengths.kt | 13 ++++++++- .../test/java/tv/orbit/classic/RefiledTest.kt | 28 +++++++++++++++++++ docs/roadmap.md | 5 ++++ 4 files changed, 54 insertions(+), 3 deletions(-) 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 b18e23aa..135647f7 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackProtocol.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackProtocol.kt @@ -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, 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 = 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()) + 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 diff --git a/android/app/src/main/java/tv/orbit/classic/ProgrammeLengths.kt b/android/app/src/main/java/tv/orbit/classic/ProgrammeLengths.kt index e3dd5a43..dcb23d7b 100644 --- a/android/app/src/main/java/tv/orbit/classic/ProgrammeLengths.kt +++ b/android/app/src/main/java/tv/orbit/classic/ProgrammeLengths.kt @@ -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 { - 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 diff --git a/android/app/src/test/java/tv/orbit/classic/RefiledTest.kt b/android/app/src/test/java/tv/orbit/classic/RefiledTest.kt index 84e47163..38269524 100644 --- a/android/app/src/test/java/tv/orbit/classic/RefiledTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/RefiledTest.kt @@ -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) + } +} diff --git a/docs/roadmap.md b/docs/roadmap.md index be0a981d..ae8b2230 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -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.