Modern Apps git

← Modern-Apps.git

youpipe: default playback to device-native resolution (#826)

Commit
40ac218441aba278104d17074f620136d0022ad5
Author
vayun-mathur <[email protected]>
When
2026-10-18 19:14 UTC (1 month ago)
Parents
9c42711

Diff

youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.ktindex 8a1949db28..08f7cf13c6 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/ui/VideoPlayerPlaybackState.kt@@ -10,6 +10,7 @@ import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue+import androidx.compose.ui.platform.LocalContext import androidx.media3.common.text.Cue import androidx.media3.common.util.UnstableApi import androidx.media3.session.MediaController@@ -17,9 +18,31 @@ import com.vayunmathur.youpipe.platform.CastPlayback import com.vayunmathur.youpipe.rememberIsInPipMode import com.vayunmathur.youpipe.util.SponsorSegment import com.vayunmathur.youpipe.util.YouPipeViewModel+import kotlin.math.min import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.first +/**+ * Default stream for #826: the best resolution at or below the device's native+ * resolution (landscape height = the smaller screen edge), so a 1080p phone+ * does not stream 4K by default and burn bandwidth. The quality menu still+ * lists every stream, so picking higher remains one tap away.+ *+ * Pure so it is unit-testable. Falls back to the lowest stream when everything+ * exceeds the cap (never empty when input is non-empty), and to the highest+ * when the device height is unknown.+ */+internal fun selectDefaultVideoStream(+    videoStreams: List<VideoStream>,+    deviceHeightPx: Int,+): VideoStream? {+    if (videoStreams.isEmpty()) return null+    if (deviceHeightPx <= 0) return videoStreams.first()+    return videoStreams.filter { it.height in 1..deviceHeightPx }+        .maxByOrNull { it.height }+        ?: videoStreams.minByOrNull { it.height }+}+ /**  * Playback-owned state for [VideoPlayer]: audio/language selection, the media controller  * lifecycle, transport observers, history writes, the media item, subtitles and tempo.@@ -103,7 +126,17 @@ internal fun rememberVideoPlayerPlaybackState(      val controllerState = remember { mutableStateOf<MediaController?>(null) }     var controller by controllerState-    val currentVideoStreamState = remember(videoStreams) { mutableStateOf(videoStreams.first()) }+    // #826: default to the best stream at or below the device's native resolution+    // (smaller screen edge = landscape height) instead of the absolute highest,+    // so a 1080p phone does not stream 4K by default. Keyed on the stream list+    // only: reseeds per video, never clobbers a manual pick mid-playback.+    val context = LocalContext.current+    val deviceHeightPx = remember(context) {+        context.resources.displayMetrics.let { min(it.widthPixels, it.heightPixels) }+    }+    val currentVideoStreamState = remember(videoStreams) {+        mutableStateOf(selectDefaultVideoStream(videoStreams, deviceHeightPx) ?: videoStreams.first())+    }     var currentVideoStream by currentVideoStreamState     // Always highest quality audio for selected language (user request: remove bitrate chooser)     val currentAudioStreamState = remember(audioStreamOptions) { mutableStateOf(audioStreamOptions.maxByOrNull { it.bitrate }) }
youpipe/src/test/java/com/vayunmathur/youpipe/ui/SelectDefaultVideoStreamTest.kt
diff --git a/youpipe/src/test/java/com/vayunmathur/youpipe/ui/SelectDefaultVideoStreamTest.kt b/youpipe/src/test/java/com/vayunmathur/youpipe/ui/SelectDefaultVideoStreamTest.ktnew file mode 100644index 0000000000..2dfb7ac71e--- /dev/null+++ b/youpipe/src/test/java/com/vayunmathur/youpipe/ui/SelectDefaultVideoStreamTest.kt@@ -0,0 +1,57 @@+package com.vayunmathur.youpipe.ui++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull++private fun stream(height: Int) = VideoStream(+    url = "https://example.com/$height",+    width = height * 16 / 9,+    height = height,+    bitrate = height * 1000,+    fps = 30,+    quality = "${height}p",+    codec = "avc",+    size = 0L,+)++class SelectDefaultVideoStreamTest {++    private val streams = listOf(stream(2160), stream(1080), stream(720), stream(480))++    @Test+    fun `1080p device picks 1080p not 4k`() {+        assertEquals(1080, selectDefaultVideoStream(streams, deviceHeightPx = 1080)?.height)+    }++    @Test+    fun `720p device picks 720p`() {+        assertEquals(720, selectDefaultVideoStream(streams, deviceHeightPx = 720)?.height)+    }++    @Test+    fun `4k device picks 4k`() {+        assertEquals(2160, selectDefaultVideoStream(streams, deviceHeightPx = 2160)?.height)+    }++    @Test+    fun `in-between device height picks next stream down`() {+        // 1440p phone: 1080p is the best that fits.+        assertEquals(1080, selectDefaultVideoStream(streams, deviceHeightPx = 1440)?.height)+    }++    @Test+    fun `tiny device falls back to lowest stream instead of nothing`() {+        assertEquals(480, selectDefaultVideoStream(streams, deviceHeightPx = 360)?.height)+    }++    @Test+    fun `unknown device height keeps old behavior of highest stream`() {+        assertEquals(2160, selectDefaultVideoStream(streams, deviceHeightPx = 0)?.height)+    }++    @Test+    fun `empty stream list returns null`() {+        assertNull(selectDefaultVideoStream(emptyList(), deviceHeightPx = 1080))+    }+}