Modern Apps git

← Modern-Apps.git

youpipe: keep the cast across videos, and let the TV skip

Commit
fa701b2f7c3eff397ad7cde4796c69deedd920ff
Author
vayun-mathur <[email protected]>
When
2026-08-23 07:15 UTC (3 months ago)
Parents
5296145

Diff

youpipe/src/main/java/com/vayunmathur/youpipe/platform/CastPlayback.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/platform/CastPlayback.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/platform/CastPlayback.ktindex 57d600cde5..edc1183502 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/platform/CastPlayback.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/platform/CastPlayback.kt@@ -127,6 +127,9 @@ object CastPlayback {      private var reportJob: Job? = null +    /** Counting down to closing a session nothing is drawing into. See [attachPlayer]. */+    private var orphanJob: Job? = null+     /**      * The player a remote command is applied to.      *@@ -139,8 +142,39 @@ object CastPlayback {     /** Called by the player screen once its `MediaController` has connected, and with null as it goes. */     fun attachPlayer(newPlayer: Player?) {         player = newPlayer+        if (newPlayer != null) {+            orphanJob?.cancel()+            orphanJob = null+            return+        }+        if (_state.value !is State.Casting) return+        // **The grace period is what lets a cast survive navigation.** Leaving a video used to end the+        // session outright, on the reasoning that there is one video output and it cannot follow the+        // user to another screen. True as far as it went - but it also meant a "next" from the+        // television dropped the cast, which is the one thing next is for. What is actually wanted is+        // to distinguish "the user has gone somewhere else" from "one player is being replaced by+        // another", and the only honest difference between them is whether something draws into the+        // surface again shortly. So the session is held briefly rather than closed, and a replacement+        // player cancels the timer.+        orphanJob?.cancel()+        orphanJob = scope.launch {+            delay(ORPHAN_GRACE_MS)+            Log.i(TAG, "nothing drew into the cast for ${ORPHAN_GRACE_MS}ms; ending it")+            close()+        }     } +    /**+     * How the player screen answers a `Next` or `Previous` from the television.+     *+     * A callback into navigation rather than something this object can do itself, because there is no+     * player queue to advance and there cannot easily be one: SABR needs per-video extractor state that+     * only `loadVideo` establishes, so "next" is the same act as tapping a related video. Whether there+     * is anything to go to is reported separately, in [Transport.hasNext] and [Transport.hasPrevious] -+     * a remote must not offer a button that does nothing.+     */+    var onNavigate: ((next: Boolean) -> Unit)? = null+     private var client: CastClient? = null      fun support(context: Context): CastClient.Support = CastClient(context).support()@@ -199,11 +233,14 @@ object CastPlayback {      /** Back to playing locally. Idempotent. */     fun close() {+        orphanJob?.cancel()+        orphanJob = null         reportJob?.cancel()         reportJob = null         CastAudioTap.detach()         client?.close()         client = null+        _transport.update { it.copy(hasNext = false, hasPrevious = false) }         _state.value = State.Idle     } @@ -273,8 +310,14 @@ object CastPlayback {                 // `playbackParameters` here would drop whatever pitch the user had chosen.                 update { it.copy(speed = speed.toFloat().coerceIn(MIN_SPEED, MAX_SPEED)) }             }-            // Volume and the queue are somebody else's to answer; ignored rather than guessed at.-            PlaybackAction.SetVolume, PlaybackAction.Next, PlaybackAction.Previous -> Unit+            // Volume is somebody else's to answer; ignored rather than guessed at.+            PlaybackAction.SetVolume -> Unit+            // Only offered when the phone said there was something to go to, but checked again here:+            // the TV's copy of that is up to half a second old.+            PlaybackAction.Next ->+                if (_transport.value.hasNext) onNavigate?.invoke(true)+            PlaybackAction.Previous ->+                if (_transport.value.hasPrevious) onNavigate?.invoke(false)         }     } @@ -306,6 +349,15 @@ object CastPlayback {      * re-anchors before its interpolation has drifted anywhere visible.      */     private const val HEARTBEAT_MS = 500L++    /**+     * How long a session is kept with nothing rendering into it.+     *+     * Long enough for a `MediaController` to reconnect on the next screen - it is built asynchronously+     * - and short enough that walking away from the video leaves a frozen frame on the television for a+     * moment rather than for the evening.+     */+    private const val ORPHAN_GRACE_MS = 5_000L }  /**
youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPage.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPage.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPage.ktindex 7d969158f4..f9dc2aac25 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPage.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPage.kt@@ -80,6 +80,7 @@ import com.vayunmathur.youpipe.util.DownloadManager import com.vayunmathur.youpipe.R import com.vayunmathur.youpipe.Route import com.vayunmathur.youpipe.findActivity+import com.vayunmathur.youpipe.platform.CastPlayback import com.vayunmathur.youpipe.rememberIsInPipMode import com.vayunmathur.youpipe.util.VideoDetailActions import com.vayunmathur.youpipe.util.VideoDetailUiState@@ -250,6 +251,30 @@ fun VideoPage(     val history by ypvm.historyVideos.collectAsState()     val progressById = remember(history) { history.associate { it.id to it.progress } } +    // ---- next and previous, for the television's remote ----+    // There is no player queue to advance and there cannot easily be one: SABR needs per-video+    // extractor state that only `loadVideo` establishes. So "next" is exactly what tapping a related+    // video does, and "previous" is the way back the user came - both of which need the back stack, so+    // both live here rather than in the player.+    val canGoNext = relatedVideos.isNotEmpty()+    val canGoPrevious = backStack.backStack.count { it is Route.VideoPage } > 1+    LaunchedEffect(canGoNext, canGoPrevious) {+        CastPlayback.update { it.copy(hasNext = canGoNext, hasPrevious = canGoPrevious) }+    }+    DisposableEffect(relatedVideos, canGoPrevious) {+        val handler: (Boolean) -> Unit = { next ->+            if (next) {+                relatedVideos.firstOrNull()?.let { backStack.add(Route.VideoPage(it.videoID)) }+            } else if (canGoPrevious) {+                backStack.pop()+            }+        }+        CastPlayback.onNavigate = handler+        // Cleared only if it is still ours. A navigation composes the incoming screen before disposing+        // the outgoing one, so an unconditional null here would undo the handler that just replaced it.+        onDispose { if (CastPlayback.onNavigate === handler) CastPlayback.onNavigate = null }+    }+     VideoDetailScreen(         state = VideoDetailUiState(             loaded = videoData != null,
youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayer.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayer.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayer.ktindex 8b49ef483f..ac01c73ab7 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayer.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayer.kt@@ -454,11 +454,11 @@ fun VideoPlayer(             player.volume = 1f         }     }-    DisposableEffect(Unit) {-        // Leaving the video ends the cast: there is one video output and it cannot follow the user to-        // another screen.-        onDispose { CastPlayback.close() }-    }+    // No DisposableEffect closing the cast here any more. Leaving a video used to end the session, on+    // the reasoning that there is one video output and it cannot follow the user to another screen -+    // which also meant a "next" from the television dropped the cast. CastPlayback now holds the+    // session briefly when nothing is drawing into it, so a replacement player takes it over and only+    // genuinely walking away ends it.      // When paused, keep controls visible (no auto-hide). When playing resumes, re-show and start timer.     LaunchedEffect(isPlaying) {