Channels got ready ahead
Port of release 0.108.65. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -517,6 +517,8 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
||||
if ((mainGuide || mainHome || mainWhatsOn || mainCatchUp) && liveSession != null && liveChannel != null) {
|
||||
if (watchingChannel) ScheduledChannelPlayer(liveSession, liveChannel, channelClockOffset,
|
||||
intro = remember { CurtainIntro.takeIf { IntroTrial.take() } },
|
||||
neighbours = availableChannels.map { it.number }.let { numbers -> listOf(1, -1).map { channelAfter(numbers, selectedNumber, it) } }
|
||||
.filter { it != selectedNumber }.distinct().mapNotNull { n -> availableChannels.firstOrNull { it.number == n } },
|
||||
onClose = { watchingChannel = false; channelClockOffset = 0 },
|
||||
onStep = { step ->
|
||||
val next = channelAfter(availableChannels.map { it.number }, selectedNumber, step)
|
||||
@@ -556,7 +558,7 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
|
||||
onRemind = { item -> saveReminders(liveReminders(reminders, System.currentTimeMillis()) + ProgrammeReminder(item.channel, item.startMs, item.title)) })
|
||||
else -> GuideGrid(channelsIn(guideCategory, availableChannels).ifEmpty { availableChannels }, selectedNumber, modifier, guideCategory, homeCategories(availableChannels.map { it.number }), picture,
|
||||
onCategory = { guideCategory = it }, onWatch = ::tune, onBack = { guideCategory = ChannelCategory.All; onHome() }, onCatchUp = { catchUpOpen = it },
|
||||
onPicture = { tune(selectedNumber, 0) })
|
||||
onPicture = { tune(selectedNumber, 0) }, onRest = { rested -> scope.launch { TuneAhead.warm(liveSession, rested) } })
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -122,6 +122,8 @@ internal fun GuideGrid(channels: List<SampleChannel>, selectedNumber: Int, modif
|
||||
categories: List<ChannelCategory> = listOf(ChannelCategory.All), picture: @Composable () -> Unit = {}, onCategory: (ChannelCategory) -> Unit = {}, onWatch: (Int, Long) -> Unit, onBack: () -> Unit,
|
||||
/** OK on the picture: full screen on what it's showing (Paul, 6 November 2026). */
|
||||
onPicture: () -> Unit = {},
|
||||
/** The channel on the row the cursor has rested on for a moment, to get it ready ahead ([TuneAhead]). */
|
||||
onRest: (SampleChannel) -> Unit = {},
|
||||
/** OK on a finished programme that can be caught up. */
|
||||
onCatchUp: (CatchUpEntry) -> Unit = {}) {
|
||||
val title = category.label
|
||||
@@ -141,6 +143,7 @@ internal fun GuideGrid(channels: List<SampleChannel>, selectedNumber: Int, modif
|
||||
val windowEnd = windowStart + GUIDE_SLOTS * GUIDE_SLOT_MS
|
||||
val nowFraction = guideNowFraction(now, windowStart, windowEnd)
|
||||
val channel = channels[row.coerceIn(channels.indices)]
|
||||
LaunchedEffect(channel.number) { delay(1_200); onRest(channel) }
|
||||
// Never before now (Paul, 6 November 2026: going back past what's on was confusing). Catch Up has its own page.
|
||||
val earliest = now
|
||||
val focused = guideAt(channel, maxOf(cursor, earliest))
|
||||
|
||||
@@ -39,6 +39,8 @@ internal fun ScheduledChannelPlayer(session: EmbySampleSession, channel: SampleC
|
||||
onClose: () -> Unit, onStep: (Int) -> Unit = {}, onLast: () -> Unit = {}, onNumber: (Int) -> Boolean = { false }, handoff: LiveHandoff? = null, onUnplayable: ((String) -> Unit)? = null,
|
||||
/** A short film over the start of this visit, such as the Drive-In's curtains. The programme opens underneath at its proper place. */
|
||||
intro: IntroSource? = null,
|
||||
/** The channels either side, got ready in the background once this one is playing ([TuneAhead]). */
|
||||
neighbours: List<SampleChannel> = emptyList(),
|
||||
/** Catch Up: see [EmbyPlaybackDialog]. */
|
||||
onShift: (Long) -> Unit = {}, onLive: () -> Unit = {}) {
|
||||
// Outlives each programme and channel, and ends when the viewer leaves full screen.
|
||||
@@ -100,6 +102,12 @@ internal fun ScheduledChannelPlayer(session: EmbySampleSession, channel: SampleC
|
||||
} catch (cancelled: CancellationException) { throw cancelled } catch (_: Exception) { }
|
||||
}
|
||||
val loadedNow = listOfNotNull(loaded, playing).firstOrNull { it.startMs == current.startMs }
|
||||
// Once this channel has settled, the ones either side are asked for theirs, so changing channel starts sooner.
|
||||
LaunchedEffect(channel.number, neighbours.map { it.number }) {
|
||||
if (clockOffsetMs != 0L) return@LaunchedEffect
|
||||
delay(4_000)
|
||||
neighbours.forEach { TuneAhead.warm(session, it) }
|
||||
}
|
||||
val prepareMutex = remember(channel) { Mutex() }
|
||||
val handedNow = handed?.takeIf { it.available && it.channelNumber == channel.number }
|
||||
var prepared by remember(channel) { mutableStateOf(handedNow?.plan) }
|
||||
@@ -118,12 +126,14 @@ internal fun ScheduledChannelPlayer(session: EmbySampleSession, channel: SampleC
|
||||
prepared = null; preparedSlot = null; problem = null
|
||||
try {
|
||||
val tuneStarted = android.os.SystemClock.elapsedRealtime()
|
||||
val plan = prepareMutex.withLock {
|
||||
// Got ready ahead (TuneAhead) when it's a channel either side, or a guide row rested on.
|
||||
val ahead = if (clockOffsetMs == 0L) TuneAhead.take(session, current.media.item, current.media.sourceId) else null
|
||||
val plan = ahead ?: prepareMutex.withLock {
|
||||
withContext(Dispatchers.IO) {
|
||||
session.prepareForTuning(current.media.item, current.media.sourceId) { Thread.sleep(2_000) }
|
||||
}
|
||||
}
|
||||
android.util.Log.i("OrbitSchedule", "channel=${channel.number} tunePrepareMs=${android.os.SystemClock.elapsedRealtime() - tuneStarted}")
|
||||
android.util.Log.i("OrbitSchedule", "channel=${channel.number} tunePrepareMs=${android.os.SystemClock.elapsedRealtime() - tuneStarted} ahead=${ahead != null}")
|
||||
if (!scheduledRuntimeMatches(plan.durationMs, current.media.durationMs))
|
||||
throw EmbyCheckException("This programme's runtime changed. Return and rebuild the sample channel.")
|
||||
preparedSlot = current.startMs; prepared = plan
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package tv.orbit.classic
|
||||
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
/**
|
||||
* Channels got ready ahead (Paul, 6 November 2026: a channel you hadn't been watching took up to 12 seconds to tune). Up
|
||||
* to about 7 seconds of that is asking Emby for the programme's stream. While you watch, the channels either side are
|
||||
* asked for theirs in the background, and in the guide, the row you rest on; tuning to one then starts straight from
|
||||
* the answer. Nothing is played or downloaded ahead, only asked for, and an answer is only used for the same programme
|
||||
* within a few minutes.
|
||||
*/
|
||||
internal object TuneAhead {
|
||||
private const val KEEP_MS = 5 * 60_000L
|
||||
private class Ready(val session: EmbySampleSession, val plan: EmbyPlaybackPlan, val at: Long)
|
||||
private val ready = mutableMapOf<String, Ready>()
|
||||
private val asking = mutableSetOf<String>()
|
||||
|
||||
private fun key(item: SampleItem, sourceId: String) = "${item.id}/$sourceId"
|
||||
|
||||
/** The programme on [channel] now, asked for in the background unless it's already ready or being asked for. */
|
||||
suspend fun warm(session: EmbySampleSession, channel: SampleChannel) {
|
||||
if (offAirUntil(channel.number, System.currentTimeMillis()) != null) return
|
||||
val programme = channel.at(System.currentTimeMillis())
|
||||
if (kidsAsleep(takeoverOf(channel.number, programme))) return
|
||||
val k = key(programme.media.item, programme.media.sourceId)
|
||||
synchronized(this) {
|
||||
val have = ready[k]
|
||||
if (have != null && have.session === session && System.currentTimeMillis() - have.at < KEEP_MS) return
|
||||
if (!asking.add(k)) return
|
||||
}
|
||||
try {
|
||||
val started = android.os.SystemClock.elapsedRealtime()
|
||||
val plan = withContext(Dispatchers.IO) { session.prepare(programme.media.item, preferredSourceId = programme.media.sourceId) }
|
||||
synchronized(this) { ready[k] = Ready(session, plan, System.currentTimeMillis()); ready.entries.removeAll { System.currentTimeMillis() - it.value.at >= KEEP_MS } }
|
||||
android.util.Log.i("OrbitSchedule", "channel=${channel.number} readyAheadMs=${android.os.SystemClock.elapsedRealtime() - started}")
|
||||
} catch (cancelled: CancellationException) { throw cancelled }
|
||||
catch (_: Exception) { /* Tuning asks again as usual. */ }
|
||||
finally { synchronized(this) { asking.remove(k) } }
|
||||
}
|
||||
|
||||
/** A ready answer for this programme, used once. */
|
||||
fun take(session: EmbySampleSession, item: SampleItem, sourceId: String): EmbyPlaybackPlan? = synchronized(this) {
|
||||
ready.remove(key(item, sourceId))?.takeIf { it.session === session && System.currentTimeMillis() - it.at < KEEP_MS }?.plan
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user