0.95.1: when a programme's file ends before its slot, play and hold the channel ident (NEXT AT …) instead of a black screen
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 1m58s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m28s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-03 19:41:43 +00:00
co-authored by Claude Opus 5.5
parent c7bd97d6bc
commit 86e90efc02
8 changed files with 57 additions and 11 deletions
@@ -66,7 +66,7 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
connection.connectTimeout = 10_000
connection.readTimeout = request.readTimeoutMs ?: if (request.path == "/Sessions/Logout") 5_000 else 15_000
connection.setRequestProperty("Accept", "application/json")
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.95.0\"")
connection.setRequestProperty("X-Emby-Authorization", "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.95.1\"")
request.token?.let { connection.setRequestProperty("X-Emby-Token", it) }
if (request.method == "POST") {
val bytes = request.body.orEmpty().toByteArray(Charsets.UTF_8)
@@ -49,7 +49,9 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl
/** A window that stays open across programmes. Without one, this opens its own. */
window: PlaybackWindow? = null,
/** Called when the programme's first picture is on screen, so anything held over the changeover can go. */
onFirstFrame: (() -> Unit)? = null) {
onFirstFrame: (() -> Unit)? = null,
/** Called when a scheduled programme's file runs out before its slot ends. */
onEndedEarly: (() -> Unit)? = null) {
val context = LocalContext.current
val logoSetting = remember { OrbitSettings(context).channelLogo }
var failure by remember { mutableStateOf<String?>(null) }
@@ -203,6 +205,7 @@ internal fun EmbyPlaybackDialog(session: EmbySampleSession, plan: EmbyPlaybackPl
}
}
override fun onPlaybackStateChanged(state: Int) {
if (state == Player.STATE_ENDED && scheduled != null) onEndedEarly?.invoke()
if (state == Player.STATE_READY) {
if (scheduled != null && !joinedSchedule) {
joinedSchedule = true
@@ -48,6 +48,12 @@ internal fun identLength(number: Int, atMs: Long = 0) = when (number) {
internal enum class IdentPhase { None, LoadNext, Showing }
/** Under this much left, a file running out is just its credits ending a moment early, which the ident covers anyway. */
internal const val EARLY_END_GRACE_MS = 3_000L
/** A file that stops well before its slot ends: Emby listed it longer than it really runs. */
internal fun finishedEarly(nowMs: Long, endMs: Long) = endMs - nowMs > EARLY_END_GRACE_MS
/** The ident covers the end of a show, usually its credits, so the next one still starts exactly on time. */
internal fun identPhase(startMs: Long, endMs: Long, nowMs: Long, identMs: Long): IdentPhase = when {
endMs - startMs < IDENT_SHORTEST_PROGRAMME_MS -> IdentPhase.None
@@ -82,7 +88,9 @@ internal fun loadNextPlayer(context: Context, session: EmbySampleSession, plan:
/** The channel's ident over the end of the show, saying what's next. A channel with no film of its own gets its logo over the next show's artwork. */
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
@Composable
internal fun IdentOverlay(session: EmbySampleSession, channel: SampleChannel, next: SampleBroadcast, identMs: Long, startedLateMs: Long) {
internal fun IdentOverlay(session: EmbySampleSession, channel: SampleChannel, next: SampleBroadcast, identMs: Long, startedLateMs: Long,
/** Filling time after a programme ended early: says when the next one starts, and keeps to a still picture if idents are off. */
early: Boolean = false) {
val context = LocalContext.current
val mood = if (channel.number == TOTS_NUMBER) totsMood(next.startMs) else null
val settings = remember { OrbitSettings(context) }
@@ -92,7 +100,7 @@ internal fun IdentOverlay(session: EmbySampleSession, channel: SampleChannel, ne
val video = identVideo(channel.number, next.startMs)
// With idents off, Tots still says goodbye with a picture that doesn't move.
if (mood != null && !settings.idents) Image(painterResource(mood.still), null, Modifier.fillMaxSize())
else if (video != null) {
else if (video != null && !(early && !settings.idents)) {
val player = remember(video) {
ExoPlayer.Builder(context).build().apply {
setMediaItem(MediaItem.fromUri(RawResourceDataSource.buildRawResourceUri(video)))
@@ -111,7 +119,7 @@ internal fun IdentOverlay(session: EmbySampleSession, channel: SampleChannel, ne
}
}
Column(Modifier.align(Alignment.BottomStart).padding(start = 40.dp, bottom = 32.dp).background(Navy.copy(alpha = .72f)).padding(horizontal = 14.dp, vertical = 8.dp)) {
Text("NEXT", color = Color(0xFFD5DCEA), fontSize = 12.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 2.sp)
Text(if (early) "NEXT AT ${clock(next.startMs)}" else "NEXT", color = Color(0xFFD5DCEA), fontSize = 12.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 2.sp)
Text(guideTitleParts(next.media.item.title).first, color = Color.White, fontSize = 20.sp, fontWeight = FontWeight.SemiBold, maxLines = 1, overflow = TextOverflow.Ellipsis)
}
}
@@ -118,6 +118,10 @@ internal fun ScheduledChannelPlayer(session: EmbySampleSession, channel: SampleC
// The ident playing out, held on its last frame until the next programme's first picture is on screen.
var heldIdent by remember(channel) { mutableStateOf<SampleBroadcast?>(null) }
LaunchedEffect(phase == IdentPhase.Showing, next.startMs) { if (phase == IdentPhase.Showing) heldIdent = next }
// A file can stop before its slot ends, when Emby lists it longer than it really runs (a Tots Night Garden on
// 3 November stopped 3 minutes early). Instead of a black screen, the channel's ident plays and holds its last
// frame, saying what's next and when, until the next programme's booked start. The schedule itself doesn't move.
var endedEarly by remember(channel) { mutableStateOf<Long?>(null) }
LaunchedEffect(heldIdent) {
val held = heldIdent ?: return@LaunchedEffect
// However the next programme fares, the screen never stays on the ident for long.
@@ -138,7 +142,15 @@ internal fun ScheduledChannelPlayer(session: EmbySampleSession, channel: SampleC
onHandBack = { running ->
if (handoff != null && clockOffsetMs == 0L) { handoff.returned = LiveStream(channel.number, current.startMs, plan, running); true } else false
},
window = window, onFirstFrame = { if (heldIdent?.startMs == current.startMs) heldIdent = null })
window = window, onFirstFrame = { if (heldIdent?.startMs == current.startMs) heldIdent = null },
onEndedEarly = {
val at = System.currentTimeMillis() + clockOffsetMs
if (channel is ScheduledChannel && next.startMs == current.endMs && finishedEarly(at, current.endMs) && heldIdent == null) {
PlaybackNotes.add(notes, "channel=${channel.number} endedEarlyMs=${current.endMs - at} item=${current.media.item.id}")
android.util.Log.w("OrbitSchedule", "channel=${channel.number} endedEarlyMs=${current.endMs - at} item=${current.media.item.id}")
endedEarly = next.startMs; heldIdent = next
}
})
} else {
SideEffect { window.onBack = { if (introPlayback?.showing == true) introPlayback.skip() else onClose() } }
if (problem == null) ChannelTuningBackdrop(channel.number) { ProgrammeArtwork(session, channel, current) }
@@ -158,8 +170,9 @@ internal fun ScheduledChannelPlayer(session: EmbySampleSession, channel: SampleC
heldIdent?.let { held ->
key(held.startMs) {
val length = identLength(channel.number, held.startMs)
val startedLate = remember { (System.currentTimeMillis() + clockOffsetMs - (held.startMs - length)).coerceAtLeast(0) }
IdentOverlay(session, channel, held, length, startedLate)
val early = endedEarly == held.startMs
val startedLate = remember { if (early) 0L else (System.currentTimeMillis() + clockOffsetMs - (held.startMs - length)).coerceAtLeast(0) }
IdentOverlay(session, channel, held, length, startedLate, early)
}
}
introPlayback?.let { IntroOverlay(it) }
@@ -33,7 +33,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
next.instanceFollowRedirects = false
next.connectTimeout = 10_000
next.readTimeout = 20_000
next.setRequestProperty("User-Agent", "OrbitTV/0.95.0")
next.setRequestProperty("User-Agent", "OrbitTV/0.95.1")
next.setRequestProperty("Accept-Encoding", "identity")
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {