diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 4415bb63..285fc279 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -9,8 +9,8 @@ android { applicationId = "tv.orbit.classic" minSdk = 26 targetSdk = 36 - versionCode = 314 - versionName = "0.122.1-smooth-row" + versionCode = 315 + versionName = "0.123.0-player" } buildFeatures { compose = true } // The finished build for the TVs (Paul, 6 November 2026: the test build stuttered on the box). Not debuggable, so diff --git a/android/app/src/main/java/tv/orbit/classic/DirectContainers.kt b/android/app/src/main/java/tv/orbit/classic/DirectContainers.kt index 5957bfd3..82c6d3d7 100644 --- a/android/app/src/main/java/tv/orbit/classic/DirectContainers.kt +++ b/android/app/src/main/java/tv/orbit/classic/DirectContainers.kt @@ -1,6 +1,9 @@ package tv.orbit.classic -internal const val DirectVideoContainers = "mp4,mkv,mov,m4v,webm,ts,mpegts,3gp" +internal const val DirectVideoContainersBase = "mp4,mkv,mov,m4v,webm,ts,mpegts,3gp" + +/** The containers Orbit plays directly; AVI only when its switch is on (PlayerSwitches, 9 November 2026). */ +internal val DirectVideoContainers: String get() = if (PlayerSwitches.avi) "$DirectVideoContainersBase,avi" else DirectVideoContainersBase internal fun supportedDirectContainer(value: String): String? = value.trim().lowercase(java.util.Locale.ROOT) .takeIf { it in DirectVideoContainers.split(",") } diff --git a/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt b/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt index 51f6b7dc..e188cdbc 100644 --- a/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt +++ b/android/app/src/main/java/tv/orbit/classic/EmbyPlaybackDialog.kt @@ -300,6 +300,20 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl } } DisposableEffect(player) { onDispose { watchAudioClock(false) } } + // Frame-rate matching (9 November 2026; off unless switched on): the TV's refresh follows the programme's, and goes + // back to the TV's own choice when the programme closes. + DisposableEffect(player) { + val window = generateSequence(context) { (it as? android.content.ContextWrapper)?.baseContext }.filterIsInstance().firstOrNull()?.window + val matcher = object : Player.Listener { + 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) + } + } + player.addListener(matcher) + onDispose { player.removeListener(matcher); 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. LaunchedEffect(player, SleepTimer.goodnight) { diff --git a/android/app/src/main/java/tv/orbit/classic/MainActivity.kt b/android/app/src/main/java/tv/orbit/classic/MainActivity.kt index 4f0c174c..8d2b1dee 100644 --- a/android/app/src/main/java/tv/orbit/classic/MainActivity.kt +++ b/android/app/src/main/java/tv/orbit/classic/MainActivity.kt @@ -70,6 +70,9 @@ class MainActivity : ComponentActivity() { if (intent?.getBooleanExtra("orbit_read_evidence", false) == true) EvidenceRequest.pending = true // Provider contract, step 3: the catalogue switch and the read-only shadow comparison. when (intent?.getStringExtra("orbit_provider_catalogue")) { "on" -> ProviderSwitches.setCatalogue(this, true); "off" -> ProviderSwitches.setCatalogue(this, false) } + // Player improvements (9 November 2026): frame-rate matching and AVI, each off until switched on. + when (intent?.getStringExtra("orbit_frame_rate")) { "on" -> PlayerSwitches.setFrameRate(this, true); "off" -> PlayerSwitches.setFrameRate(this, false) } + when (intent?.getStringExtra("orbit_avi")) { "on" -> PlayerSwitches.setAvi(this, true); "off" -> PlayerSwitches.setAvi(this, false) } if (intent?.hasExtra("orbit_shadow_catalogue") == true) CatalogueShadow.pending = intent.getIntExtra("orbit_shadow_catalogue", 5) } override fun onNewIntent(intent: android.content.Intent) { super.onNewIntent(intent); setIntent(intent); readAdbRequests(intent) } @@ -83,6 +86,7 @@ class MainActivity : ComponentActivity() { // The guide's logos, decoded ahead on a background thread so the guide opens with them ready. Thread({ Pictures.warmLogos(resources) }, "orbit-logos").apply { priority = Thread.MIN_PRIORITY }.start() ProviderSwitches.load(this) + PlayerSwitches.load(this) readAdbRequests(intent) // The hand-run playback test opens over Orbit, which stops in the background as it would for any other app, and comes back when it ends. intent?.getStringExtra("orbit_play_test")?.takeIf(EmbyClient::validId)?.let { item -> diff --git a/android/app/src/main/java/tv/orbit/classic/PlayerSwitches.kt b/android/app/src/main/java/tv/orbit/classic/PlayerSwitches.kt new file mode 100644 index 00000000..2470504e --- /dev/null +++ b/android/app/src/main/java/tv/orbit/classic/PlayerSwitches.kt @@ -0,0 +1,58 @@ +package tv.orbit.classic + +import android.content.Context +import android.view.Window +import kotlin.math.abs +import kotlin.math.roundToInt + +/** + * Player improvements (9 November 2026), each off until Paul turns it on, from adb only for now: + * am start -n tv.orbit.classic/.MainActivity --es orbit_frame_rate on|off --es orbit_avi on|off + * - Frame-rate matching: the TV's refresh rate follows the programme's (a 23.976 film at 23.976 Hz, a 25 fps UK show at + * 50 Hz), so motion is smooth instead of judder. The TV may blank for a second or two when it switches. + * - AVI: AVI files play directly (Media3 reads AVI; this TV decodes MPEG-4/XviD and MP3). Off, they stay unplayable + * and out of channel plans, as today; on, new plans may include them. Schedules already planned are not changed. + */ +internal object PlayerSwitches { + @Volatile var frameRate = false + @Volatile var avi = false + private const val FRAME_RATE = "player_frame_rate" + private const val AVI = "player_avi" + private fun prefs(context: Context) = context.getSharedPreferences("orbit_settings", Context.MODE_PRIVATE) + fun load(context: Context) { prefs(context).let { frameRate = it.getBoolean(FRAME_RATE, false); avi = it.getBoolean(AVI, false) } } + fun setFrameRate(context: Context, on: Boolean) { frameRate = on; prefs(context).edit().putBoolean(FRAME_RATE, on).apply(); log("frameRate", on) } + fun setAvi(context: Context, on: Boolean) { avi = on; prefs(context).edit().putBoolean(AVI, on).apply(); log("avi", on) } + private fun log(name: String, on: Boolean) = android.util.Log.i("OrbitPlayer", "$name switch=${if (on) "on" else "off"}") +} + +internal data class DisplayModeInfo(val id: Int, val width: Int, val height: Int, val refreshHz: Float) + +internal object FrameRateMatch { + /** + * The display mode for a programme at [fps]: same resolution as now, refresh a whole multiple of the frame rate, the + * highest such (25 fps prefers 50 Hz to 25 Hz). Null when the current mode already fits, the rate is unknown, or no + * mode fits. 23.976 and 24 are told apart (a 0.01 fps tolerance), since showing one at the other judders once a minute. + */ + fun pick(modes: List, current: DisplayModeInfo, fps: Float): Int? { + if (fps.isNaN() || fps !in 10f..121f) return null + fun fits(refresh: Float): Boolean { val k = (refresh / fps).roundToInt(); return k >= 1 && abs(refresh / k - fps) < 0.01f } + if (fits(current.refreshHz)) return null + return modes.filter { it.width == current.width && it.height == current.height && fits(it.refreshHz) }.maxByOrNull { it.refreshHz }?.id + } + + fun apply(window: Window, fps: Float) { + val display = window.decorView.display ?: return + val modes = display.supportedModes.map { DisplayModeInfo(it.modeId, it.physicalWidth, it.physicalHeight, it.refreshRate) } + val now = display.mode.let { DisplayModeInfo(it.modeId, it.physicalWidth, it.physicalHeight, it.refreshRate) } + val id = pick(modes, now, fps) ?: return + if (window.attributes.preferredDisplayModeId == id) return + window.attributes = window.attributes.also { it.preferredDisplayModeId = id } + android.util.Log.i("OrbitPlayer", "frameRate fps=$fps from=${now.refreshHz} mode=$id") + } + + /** Back to the TV's own choice, when the programme closes. */ + fun reset(window: Window) { + if (window.attributes.preferredDisplayModeId == 0) return + window.attributes = window.attributes.also { it.preferredDisplayModeId = 0 } + } +} diff --git a/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt b/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt new file mode 100644 index 00000000..a5dfd591 --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/PlayerSwitchesTest.kt @@ -0,0 +1,49 @@ +package tv.orbit.classic + +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +/** Frame-rate matching and AVI (9 November 2026). The modes are the downstairs TV's own, read with dumpsys display. */ +class PlayerSwitchesTest { + private val tv = listOf(841 to 59.94006f, 842 to 60.000004f, 851 to 50f, 852 to 29.97003f, 853 to 30.000002f, 854 to 25f, 855 to 23.976025f, 856 to 24.000002f) + .map { (id, hz) -> DisplayModeInfo(id, 1920, 1080, hz) } + listOf(843 to 59.94006f, 849 to 23.976025f).map { (id, hz) -> DisplayModeInfo(id, 3840, 2160, hz) } + private val at60 = tv.first { it.id == 842 } + + @After fun off() { PlayerSwitches.avi = false; PlayerSwitches.frameRate = false } + + @Test fun filmsGoToTheirOwnRate() { + assertEquals(855, FrameRateMatch.pick(tv, at60, 23.976f)) + assertEquals(855, FrameRateMatch.pick(tv, at60, 23.98f)) // a rounded rate in the file still finds 23.976 + assertEquals(856, FrameRateMatch.pick(tv, at60, 24f)) + } + + @Test fun ukShowsGoTo50AndUsShowsTo5994() { + assertEquals(851, FrameRateMatch.pick(tv, at60, 25f)) + assertEquals(841, FrameRateMatch.pick(tv, at60, 29.97f)) + assertEquals(841, FrameRateMatch.pick(tv, at60, 59.94f)) + } + + @Test fun noSwitchWhenItAlreadyFitsOrIsUnknown() { + assertNull(FrameRateMatch.pick(tv, at60, 30f)) // 60 Hz already shows 30 fps evenly + assertNull(FrameRateMatch.pick(tv, at60, 60f)) + assertNull(FrameRateMatch.pick(tv, tv.first { it.id == 851 }, 25f)) + assertNull(FrameRateMatch.pick(tv, at60, -1f)) // Media3's "no value" + assertNull(FrameRateMatch.pick(tv, at60, Float.NaN)) + assertEquals(851, FrameRateMatch.pick(tv, at60, 12.5f)) // 12.5 fps shows evenly at 50 Hz + assertNull(FrameRateMatch.pick(tv, at60, 14.2f)) // nothing on this TV fits + } + + @Test fun neverChangesResolution() { + assertEquals(849, FrameRateMatch.pick(tv, tv.first { it.id == 843 }, 23.976f)) + assertNull(FrameRateMatch.pick(tv, tv.first { it.id == 843 }, 25f)) // no 4K 50 Hz in this list: stay, rather than drop to 1080 + } + + @Test fun aviOnlyWhenSwitchedOn() { + assertNull(supportedDirectContainer("avi")); assertEquals(false, DirectVideoContainers.contains("avi")) + PlayerSwitches.avi = true + assertEquals("avi", supportedDirectContainer(" AVI ")); assertEquals(true, DirectVideoContainers.endsWith(",avi")) + assertEquals("mkv", supportedDirectContainer("mkv")) + } +}