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