diff --git a/android/app/src/main/java/tv/orbit/classic/FilmIdentity.kt b/android/app/src/main/java/tv/orbit/classic/FilmIdentity.kt index 76934faa..a204fc15 100644 --- a/android/app/src/main/java/tv/orbit/classic/FilmIdentity.kt +++ b/android/app/src/main/java/tv/orbit/classic/FilmIdentity.kt @@ -59,9 +59,11 @@ internal fun resolveFilm(identity: FilmIdentity, candidates: List else -> "film=\"${identity.label}\" missing: the library has that name only from $years" }) if (filmCount(film) > 1) return FilmLookup(null, "film=\"${identity.label}\" ambiguous: two different films share that name and year, so the list needs an IMDb ID") - // Several copies of one film (another edition, a better file) are all fine. The first this box can play is used. + // Several copies of one film (another edition, a better file) are all fine. The lightest this box can play is used + // (1.0 RC1, 13 November 2026): a 1080p copy before a 4K remux, so a living-room box streaming from a server far away + // doesn't fall behind or drift out of sync (American Werewolf's 54 Mbps 4K copy did; its 10 Mbps 1080p copy was there). val passed = mutableListOf() - for (copy in film.sortedBy { it.id }) { + for (copy in film.sortedWith(compareBy { copyWeight(it) }.thenBy { it.id })) { val source = copy.sources.firstOrNull { supportedDirectContainer(it.optString("Container")) != null && EmbyClient.validId(it.optString("Id")) && sourceVideoCodec(it)?.let(canDecode) != false } if (source == null) { passed += "${copy.id} ${if (copy.sources.isEmpty()) "no media" else "can't play here"}"; continue } val ticks = source.optLong("RunTimeTicks", 0).takeIf { it > 0 } ?: copy.runTimeTicks @@ -73,6 +75,21 @@ internal fun resolveFilm(identity: FilmIdentity, candidates: List return FilmLookup(null, "film=\"${identity.label}\" unplayable: ${passed.joinToString()}") } +/** + * How heavy a copy is to stream and play, lower first: 4K picture, a very high bitrate, and only lossless surround sound + * (TrueHD, DTS-HD MA) each count against it. Ties keep the library's order. + */ +internal fun copyWeight(copy: FilmCandidate): Int { + val source = copy.sources.firstOrNull() ?: return 9 + val streams = source.optJSONArray("MediaStreams")?.let { a -> (0 until minOf(a.length(), 32)).mapNotNull { a.optJSONObject(it) } }.orEmpty() + val width = streams.firstOrNull { it.optString("Type") == "Video" }?.optInt("Width", 0) ?: 0 + val bitrate = source.optLong("Bitrate", 0) + val audio = streams.filter { it.optString("Type") == "Audio" } + fun lossless(a: JSONObject) = a.optString("Codec").lowercase().let { it == "truehd" || it == "mlp" } || a.optString("Profile").contains("MA", ignoreCase = false) + return (if (width > 1920) 2 else 0) + (if (bitrate > 30_000_000) 2 else if (bitrate > 15_000_000) 1 else 0) + + (if (audio.isNotEmpty() && audio.all(::lossless)) 1 else 0) +} + /** A library row as a candidate. Rows that aren't films, or carry an invalid ID, are left out. */ internal fun filmCandidate(row: JSONObject): FilmCandidate? { val id = row.optString("Id") 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 a6584786..84e47163 100644 --- a/android/app/src/test/java/tv/orbit/classic/RefiledTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/RefiledTest.kt @@ -63,3 +63,20 @@ class DolbyVisionCopyTest { assertEquals("h264", sourceVideoCodec(source("None", "h264"))) } } + +/** 13 November 2026: American Werewolf's 54 Mbps 4K copy drifted out of sync; its 10 Mbps 1080p copy was in the library. */ +class CopyChoiceTest { + private fun copy(id: String, width: Int, bitrate: Long, vararg audio: Pair) = FilmCandidate(id, "An American Werewolf in London", 1981, "tt0082010", + listOf(JSONObject().put("Id", "src$id").put("Container", "mkv").put("Bitrate", bitrate).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)) + .also { a -> audio.forEach { (c, p) -> a.put(JSONObject().put("Type", "Audio").put("Codec", c).put("Profile", p)) } }))) + @Test fun theLighterCopyIsChosen() { + val heavy = copy("aaa1", 3840, 54_488_349, "dts" to "DTS-HD MA", "dts" to "DTS") + val light = copy("zzz9", 1920, 9_647_280, "ac3" to "") + val found = resolveFilm(FilmIdentity("An American Werewolf in London", 1981), listOf(heavy, light)) + assertEquals("zzz9", found.film!!.id) + assertTrue(copyWeight(heavy) > copyWeight(light)) + // With only the 4K copy, it's still used. + assertEquals("aaa1", resolveFilm(FilmIdentity("An American Werewolf in London", 1981), listOf(heavy)).film!!.id) + } +}