diff --git a/androidApp/src/androidMain/kotlin/org/siloserver/silo/android/ui/screens/player/PlayerViewModel.kt b/androidApp/src/androidMain/kotlin/org/siloserver/silo/android/ui/screens/player/PlayerViewModel.kt index 1c4aa77d21..ccff3c7b9a 100644 --- a/androidApp/src/androidMain/kotlin/org/siloserver/silo/android/ui/screens/player/PlayerViewModel.kt +++ b/androidApp/src/androidMain/kotlin/org/siloserver/silo/android/ui/screens/player/PlayerViewModel.kt @@ -94,6 +94,7 @@ import org.siloserver.silo.model.catalog.FileVersion import org.siloserver.silo.model.catalog.VersionChapter import org.siloserver.silo.domain.player.MarkerKind import org.siloserver.silo.domain.player.MarkerTimeline +import org.siloserver.silo.domain.player.NextUpPrompt import org.siloserver.silo.model.catalog.MarkerSegment import org.siloserver.silo.model.catalog.TimeRange import org.siloserver.silo.model.settings.SubtitleAppearance @@ -701,7 +702,7 @@ class PlayerViewModel( val awaitingFirstFrame: Boolean get() = startedContentId != contentId - /** The marker occurrences the skip controls and Up Next read. */ + /** The marker occurrences the skip controls read. */ val markerTimeline: MarkerTimeline by lazy { MarkerTimeline.resolve(markerSegments, intro = intro, credits = credits, recap = recap, preview = preview) } @@ -1036,8 +1037,7 @@ class PlayerViewModel( .stateIn(viewModelScope, SharingStarted.Eagerly, IntroSkipMode.Default) val autoPlayNextEnabled: StateFlow = playerSettingsStore.autoPlayNextFlow .stateIn(viewModelScope, SharingStarted.Eagerly, true) - // Seconds before end to surface the Up Next card when no credits marker - // exists (0 = only at end). Credits marker wins when present. + // Seconds before the end of the file to surface Up Next (0 = only at end). private val nextUpPromptSeconds: StateFlow = playerSettingsStore.nextUpPromptSecondsFlow .stateIn(viewModelScope, SharingStarted.Eagerly, 30) @@ -1071,7 +1071,7 @@ class PlayerViewModel( // same VM persists across episodes and the guard accumulates the streak. // The guard reads the threshold lazily so a settings change applies live. private val autoPlayGuard = AutoPlayGuard(threshold = { passOutThreshold.value }) - // Once-per-episode guard for the credits/ended trigger; reset on each load. + // Once-per-episode guard for the before-end/ended trigger; reset on each load. private var autoAdvanceHandled = false // End point fired before the next episode resolved — remember it (and the // strongest videoEnded flag seen) so resolveNextEpisode can commit the card. @@ -3304,22 +3304,16 @@ class PlayerViewModel( ) } - // F2: surface the Up Next card when playback CROSSES the credits point - // (only on the before->after transition, so resuming inside the credits - // doesn't instantly trigger it). Without a credits marker, fall back to - // crossing (duration - nextUpPromptSeconds); 0 = only at end (iOS parity). - if (!seekWasActive) { - val creditsStart = _uiState.value.markerTimeline.upNextCreditsStart() - if (creditsStart != null) { - if (previousPosition < creditsStart && positionSec >= creditsStart) onApproachingEnd() - } else { - val promptSeconds = nextUpPromptSeconds.value - val duration = _uiState.value.duration - if (promptSeconds > 0 && duration > 0) { - val promptStart = duration - promptSeconds - if (previousPosition < promptStart && positionSec >= promptStart) onApproachingEnd() - } - } + // Surface Up Next when playback crosses the chosen point before the end + // of the file. Only the crossing counts, so resuming or seeking past it + // doesn't open the card; the stream ending still does. + if ( + !seekWasActive && + NextUpPrompt.crossesPromptPoint( + previousPosition, positionSec, _uiState.value.duration, nextUpPromptSeconds.value, + ) + ) { + onApproachingEnd() } // Forward to the lifecycle so its 10s reporter has a fresh sample. @@ -4828,8 +4822,8 @@ class PlayerViewModel( /** * Adopt the playing file's marker ranges (a `markers_updated` event or a - * reconcile read). The intro auto-skip observer and the credits-based F2 - * trigger read these from UiState, so updating them takes effect + * reconcile read). The intro auto-skip observer and the skip controls + * read these from UiState, so updating them takes effect * immediately. Passing `null` clears a marker the server says no longer * applies. */ @@ -6162,7 +6156,7 @@ class PlayerViewModel( it.copy(nextEpisode = info) } if (_uiState.value.contentId != forContentId) return@launch - // If the credits/end point already fired while we were still + // If the before-end/end point already fired while we were still // resolving, the card couldn't arm — commit it now with the // strongest video-ended flag observed. if (!autoAdvanceHandled) { @@ -6174,12 +6168,12 @@ class PlayerViewModel( } /** - * Credits reached (primary) or stream ended (fallback) — surface the Up Next - * card. When auto-play is on and the consecutive-auto-advance streak is + * The Next Up point before the end was crossed, or the stream ended — + * surface the Up Next card. When auto-play is on and the consecutive-auto-advance streak is * below the pass-out threshold, the card runs a countdown that plays the * next episode at zero. Once the streak hits the threshold (or auto-play is * off), the card shows WITHOUT a countdown so the user must explicitly - * choose (the pass-out gate). The credits prompt is once per episode; + * choose (the pass-out gate). The before-end prompt is once per episode; * a dismissed card reopens when playback ends. * * [videoEnded] is true on STATE_ENDED — the card reads "Playing Next"; a @@ -6202,7 +6196,7 @@ class PlayerViewModel( } if (autoAdvanceHandled) { if (videoEnded) { - // Keep Watching dismisses the credits prompt, but finishing the + // Keep Watching dismisses the before-end prompt, but finishing the // episode must still offer Play Now without restarting its timer. _uiState.update { it.copy(showUpNext = true, upNextVideoEnded = true) } } @@ -6262,7 +6256,7 @@ class PlayerViewModel( // Two anchors (iOS parity — silo-apple's PlayerViewModel derives the // countdown from movieTime and only auto-plays once playback truly // ends): - // - Card committed BEFORE the end (credits / prompt crossing): the + // - Card committed BEFORE the end (before-end crossing): the // countdown mirrors the remaining playback time, so it freezes on // pause, grows on a backward seek, and the advance fires only when // the player reports STATE_ENDED — never truncating the tail of an @@ -6308,7 +6302,7 @@ class PlayerViewModel( /** * Up Next dismiss — cancel the countdown and stay on the current playback. - * Suppress further credits prompts; playback end still reopens the card. + * Suppress further before-end prompts; playback end still reopens the card. */ fun dismissUpNext() { if (nextUpTransitionGate.isActive) return diff --git a/androidApp/src/androidUnitTest/kotlin/org/siloserver/silo/android/ui/screens/player/PlayerViewModelLoadOwnershipIntegrationTest.kt b/androidApp/src/androidUnitTest/kotlin/org/siloserver/silo/android/ui/screens/player/PlayerViewModelLoadOwnershipIntegrationTest.kt index b32a921c2e..f733fadb67 100644 --- a/androidApp/src/androidUnitTest/kotlin/org/siloserver/silo/android/ui/screens/player/PlayerViewModelLoadOwnershipIntegrationTest.kt +++ b/androidApp/src/androidUnitTest/kotlin/org/siloserver/silo/android/ui/screens/player/PlayerViewModelLoadOwnershipIntegrationTest.kt @@ -456,6 +456,36 @@ class PlayerViewModelLoadOwnershipIntegrationTest { } } + @Test + fun nextUpOpensAtThePromptSecondsBeforeTheFileEndNotAtTheCreditsMarker() = runTest(dispatcher) { + val starter = DeferredNonCooperativeStarter() + val fixture = playerViewModel(starter, backgroundScope) + val store = ViewModelStore().also { it.put("player", fixture.viewModel) } + try { + val viewModel = fixture.viewModel + viewModel.loadContent("episode-a", preferredFileId = 1) + starter.awaitRequestCount(1) + starter.complete(0, ready(starter.request(0), "session-a")) + viewModel.awaitState { it.sessionId == "session-a" && !it.isLoading } + viewModel.onMediaMountApplied(viewModel.uiState.value.mediaMountGeneration) + viewModel.offerNextEpisode() + viewModel.mutableUiState().update { + it.copy(credits = org.siloserver.silo.model.catalog.TimeRange(1000.0, 1200.0)) + } + + // The fake settings store chooses 30 seconds before the end. + viewModel.onPositionChanged(999_000L, 1_200_000L) + viewModel.onPositionChanged(1_001_000L, 1_200_000L) + assertFalse(viewModel.uiState.value.showUpNext) + viewModel.onPositionChanged(1_169_500L, 1_200_000L) + assertFalse(viewModel.uiState.value.showUpNext) + viewModel.onPositionChanged(1_170_500L, 1_200_000L) + assertTrue(viewModel.uiState.value.showUpNext) + } finally { + store.clear() + } + } + @Test fun keepWatchingSuppressesCreditsPromptButReopensNextUpAtPlaybackEnd() = runTest(dispatcher) { val starter = DeferredNonCooperativeStarter() diff --git a/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/player/TvPlayerScreen.kt b/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/player/TvPlayerScreen.kt index 237e656540..089fb0ba09 100644 --- a/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/player/TvPlayerScreen.kt +++ b/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/player/TvPlayerScreen.kt @@ -2483,7 +2483,7 @@ fun TvPlayerScreen( } else { // seekImmediate pre-writes position so a scrub // committed into the credits region isn't mistaken - // for natural playback crossing the credits point. + // for natural playback crossing the Up Next point. viewModel.seekImmediate(targetSec) } viewModel.setControlsVisible(true) @@ -2623,7 +2623,7 @@ fun TvPlayerScreen( } else { // Solo: seekImmediate pre-writes position so a // chapter jump into credits isn't mistaken for - // natural playback crossing the credits point. + // natural playback crossing the Up Next point. viewModel.seekImmediate(sec) } } diff --git a/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/player/TvPlayerViewModel.kt b/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/player/TvPlayerViewModel.kt index 5847428ae1..8077933766 100644 --- a/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/player/TvPlayerViewModel.kt +++ b/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/player/TvPlayerViewModel.kt @@ -109,6 +109,7 @@ import org.siloserver.silo.model.catalog.AudioTrack import org.siloserver.silo.model.catalog.FileVersion import org.siloserver.silo.domain.player.MarkerKind import org.siloserver.silo.domain.player.MarkerTimeline +import org.siloserver.silo.domain.player.NextUpPrompt import org.siloserver.silo.model.catalog.MarkerSegment import org.siloserver.silo.model.catalog.TimeRange import org.siloserver.silo.model.catalog.VersionChapter @@ -1861,8 +1862,8 @@ class TvPlayerViewModel( // ---- Next-episode auto-advance (F2) ---- private var autoAdvanceCount: Int = launchArgs.autoAdvanceCount private var autoAdvanceHandled = false // once-per-item guard - // The credits or the end of this item was reached. Unlike autoAdvanceHandled, - // opening Up Next by hand or Keep Watching never sets it. + // Playback reached the closing credits or the end of this item. Unlike + // autoAdvanceHandled, opening Up Next by hand or Keep Watching never sets it. private var reachedCreditsOrEnd = false // The identity generation this player was launched under. private val launchIdentityGeneration: Long? = identityTransitions?.generation?.value @@ -2008,6 +2009,9 @@ class TvPlayerViewModel( // Per-profile "Still watching?" threshold (default 3; 0 = off). val passOutThreshold: StateFlow = playerSettingsStore.passOutThresholdFlow .stateIn(viewModelScope, SharingStarted.Eagerly, 3) + // Seconds before the end of the file to surface Up Next (0 = only at end). + private val nextUpPromptSeconds: StateFlow = playerSettingsStore.nextUpPromptSecondsFlow + .stateIn(viewModelScope, SharingStarted.Eagerly, 30) val hdrEnabled: StateFlow = playerSettingsStore.hdrEnabledFlow .stateIn(viewModelScope, SharingStarted.Eagerly, true) val dolbyVisionEnabled: StateFlow = playerSettingsStore.dolbyVisionEnabledFlow @@ -3880,13 +3884,22 @@ class TvPlayerViewModel( if (positionSec > 0 && transientNetworkRetries > 0) { transientNetworkRetries = 0 } - // F2: auto-advance / prompt when playback CROSSES the credits point — - // only on the transition from before to after, so resuming an episode - // whose saved position is already inside the credits doesn't instantly - // skip to the next one (a seek into credits also won't trigger it). - if (!seekWasActive) { - _uiState.value.markerTimeline.upNextCreditsStart()?.let { creditsStart -> - if (previousPosition < creditsStart && positionSec >= creditsStart) onApproachingEnd() + // Surface Up Next when playback crosses the chosen point before the end + // of the file. Only the crossing counts, so resuming or seeking past it + // doesn't open the card; the stream ending still does. + if ( + !seekWasActive && + NextUpPrompt.crossesPromptPoint( + previousPosition, positionSec, _uiState.value.duration, nextUpPromptSeconds.value, + ) + ) { + onApproachingEnd() + } + // Playing into the closing credits counts as finishing the episode for + // the series page, though Up Next waits for the prompt point. + if (!seekWasActive && roomId == null) { + _uiState.value.markerTimeline.finalCreditsStart()?.let { creditsStart -> + if (previousPosition < creditsStart && positionSec >= creditsStart) reachedCreditsOrEnd = true } } // Forward to the lifecycle so its 10s reporter has a fresh sample. @@ -5234,7 +5247,7 @@ class TvPlayerViewModel( /** * Adopt the playing file's marker ranges (a `markers_updated` event or a - * reconcile read). Skip-intro and the credits-based F2 trigger read these + * reconcile read). Skip-intro and the skip controls read these * from UiState, so the update takes effect immediately; `null` clears a * marker the server dropped. */ @@ -5358,7 +5371,7 @@ class TvPlayerViewModel( } /** - * Called by the screen when the credits point is reached (primary) or the + * Called when playback crosses the Up Next point before the end or the * stream ends (fallback). Surfaces the Up-Next overlay — a 16:9 mini-player * beside the next-episode panel — as the end-of-playback surface (mirrors * tvOS PlayerNextUpScreen), replacing the old "Still watching?" dialog. @@ -5367,12 +5380,12 @@ class TvPlayerViewModel( * pass-out threshold, the overlay starts a countdown ring that plays the * next episode at zero. Once the streak hits the pass-out threshold (or * auto-play is off), the overlay shows with NO countdown so the user must - * explicitly choose Play Now / Keep Watching (the pass-out gate). The credits + * explicitly choose Play Now / Keep Watching (the pass-out gate). The before-end * prompt is once per episode; a dismissed card reopens when playback ends. * * [videoEnded] true when the stream has actually ended (STATE_ENDED) — the * panel reads "End of playback" / "Playing Next" and hides Keep Watching; - * false at the credits-crossing while video is still rolling. + * false at the before-end crossing while video is still rolling. */ fun onApproachingEnd(videoEnded: Boolean = false) { if (nextUpTransitionGate.isActive) return @@ -5381,8 +5394,10 @@ class TvPlayerViewModel( // transport gate. if (roomId != null) return // Recorded before the once-per-item guard below, which Keep Watching on - // a hand-opened Up Next also sets. - reachedCreditsOrEnd = true + // a hand-opened Up Next also sets. The before-end prompt can open while + // the story is still playing, so only the end counts here; the closing + // credits are recorded where playback crosses them. + if (videoEnded) reachedCreditsOrEnd = true if (shufflePlayback != null) { // A multi-part pick plays its later parts before the shuffle moves on. shuffleNextPartFileId?.let { fileId -> @@ -5451,7 +5466,7 @@ class TvPlayerViewModel( } /** - * Surface Up Next on demand, ahead of the credits trigger. + * Surface Up Next on demand, ahead of the before-end trigger. * * Routed through the same commit the automatic timing uses so the overlay, * the countdown gating and the auto-advance accounting behave identically — @@ -5509,7 +5524,7 @@ class TvPlayerViewModel( nextUpCountdownJob?.cancel() nextUpCountdownJob = viewModelScope.launch { // Two anchors, matching phone and tvOS: - // - Card committed BEFORE the end (credits crossing): the countdown + // - Card committed BEFORE the end (before-end crossing): the countdown // mirrors the remaining playback time, so it freezes on pause, // grows on a backward seek, and the advance fires only once the // player reports the stream ended. A fixed wall clock here cut off @@ -6614,7 +6629,7 @@ class TvPlayerViewModel( */ fun onSelectIntroPrompt(): Double? { val target = introAutoSkipController.select() ?: return null - // Pre-write the resolved source position so the credits crossing check + // Pre-write the resolved source position so the Next Up crossing check // treats this as a deliberate jump. The caller routes the actual seek // through either the room controller or seekImmediate; the latter owns // the pending-position guard for solo playback. diff --git a/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/settings/TvSettingsViewModel.kt b/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/settings/TvSettingsViewModel.kt index 15af51d852..7989b93d9b 100644 --- a/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/settings/TvSettingsViewModel.kt +++ b/androidTvApp/src/androidMain/kotlin/org/siloserver/silo/tv/ui/screens/settings/TvSettingsViewModel.kt @@ -168,7 +168,7 @@ class TvSettingsViewModel( val passOutThreshold: Int = 3, // Seconds before the end of an episode to surface the Up-Next prompt // (0 = at the very end). Mirrors tvOS `nextUpPromptSeconds`. - val nextUpPromptSeconds: Int = 10, + val nextUpPromptSeconds: Int = 30, /** * Keys this device holds its own value for. A profile-layered control * whose key is absent shows "Use profile setting" as its choice. diff --git a/shared/src/commonMain/kotlin/org/siloserver/silo/domain/player/MarkerTimeline.kt b/shared/src/commonMain/kotlin/org/siloserver/silo/domain/player/MarkerTimeline.kt index c1b7ffa5d3..6a906a425a 100644 --- a/shared/src/commonMain/kotlin/org/siloserver/silo/domain/player/MarkerTimeline.kt +++ b/shared/src/commonMain/kotlin/org/siloserver/silo/domain/player/MarkerTimeline.kt @@ -54,11 +54,11 @@ class MarkerTimeline private constructor(private val regions: List= promptStart + } +} diff --git a/shared/src/commonTest/kotlin/org/siloserver/silo/domain/player/MarkerTimelineTest.kt b/shared/src/commonTest/kotlin/org/siloserver/silo/domain/player/MarkerTimelineTest.kt index 1c09aca187..77ab2ef89e 100644 --- a/shared/src/commonTest/kotlin/org/siloserver/silo/domain/player/MarkerTimelineTest.kt +++ b/shared/src/commonTest/kotlin/org/siloserver/silo/domain/player/MarkerTimelineTest.kt @@ -88,15 +88,15 @@ class MarkerTimelineTest { assertEquals(TimeRange(600.0, 660.0), twoIntros.occurrenceAt(MarkerKind.Intro, 600.0)) } - @Test fun upNextWaitsForTheLastCreditsRangeSoAMidCreditsScenePlays() { - assertEquals(3189.0, timeline.upNextCreditsStart()) + @Test fun finalCreditsStartSkipsAMidCreditsScene() { + assertEquals(3189.0, timeline.finalCreditsStart()) val touching = MarkerTimeline.resolve( segments = listOf(MarkerSegment("credits", 1200.0, 1250.0), MarkerSegment("credits", 1250.5, 1300.0)), intro = null, credits = null, recap = null, preview = null, ) - assertEquals(1200.0, touching.upNextCreditsStart()) + assertEquals(1200.0, touching.finalCreditsStart()) val single = MarkerTimeline.resolve(null, intro = null, credits = TimeRange(1200.0, 1260.0), recap = null, preview = null) - assertEquals(1200.0, single.upNextCreditsStart()) - assertNull(MarkerTimeline.EMPTY.upNextCreditsStart()) + assertEquals(1200.0, single.finalCreditsStart()) + assertNull(MarkerTimeline.EMPTY.finalCreditsStart()) } } diff --git a/shared/src/commonTest/kotlin/org/siloserver/silo/domain/player/NextUpPromptTest.kt b/shared/src/commonTest/kotlin/org/siloserver/silo/domain/player/NextUpPromptTest.kt new file mode 100644 index 0000000000..c72ef76efb --- /dev/null +++ b/shared/src/commonTest/kotlin/org/siloserver/silo/domain/player/NextUpPromptTest.kt @@ -0,0 +1,26 @@ +package org.siloserver.silo.domain.player + +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class NextUpPromptTest { + // A 48:12 episode whose credits marker starts at 46:31: the marker must + // not move the prompt (silo-android#910). + private val duration = 2892.0 + + @Test fun promptOpensWhenPlaybackCrossesTheChosenSecondsBeforeTheEnd() { + assertTrue(NextUpPrompt.crossesPromptPoint(2861.6, 2862.1, duration, promptSeconds = 30)) + assertTrue(NextUpPrompt.crossesPromptPoint(2881.5, 2882.0, duration, promptSeconds = 10)) + assertFalse(NextUpPrompt.crossesPromptPoint(2791.0, 2792.0, duration, promptSeconds = 30)) + } + + @Test fun playbackAlreadyPastThePointDoesNotReopenThePrompt() { + assertFalse(NextUpPrompt.crossesPromptPoint(2870.0, 2870.5, duration, promptSeconds = 30)) + } + + @Test fun atEndAndUnknownDurationNeverOpenBeforeTheStreamEnds() { + assertFalse(NextUpPrompt.crossesPromptPoint(2891.0, 2892.0, duration, promptSeconds = 0)) + assertFalse(NextUpPrompt.crossesPromptPoint(10.0, 11.0, 0.0, promptSeconds = 30)) + } +}