Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

feat: add VOD chapter navigation policy

David Nashash94b6238b79c5
diff --git a/feature/playback/src/main/java/com/streamvault/feature/playback/player/VodChapterNavigation.kt b/feature/playback/src/main/java/com/streamvault/feature/playback/player/VodChapterNavigation.kt
new file mode 100644
index 00000000..a5b10962
--- /dev/null
+++ b/feature/playback/src/main/java/com/streamvault/feature/playback/player/VodChapterNavigation.kt
@@ -0,0 +1,51 @@
+package com.streamvault.feature.playback.player
+
+import com.streamvault.player.PlayerChapter
+
+const val CHAPTER_RESTART_THRESHOLD_MS = 3_000L
+
+/** Returns the chapter containing [positionMs], or null when the position is in a gap. */
+internal fun currentChapter(
+ chapters: List<PlayerChapter>,
+ positionMs: Long
+): PlayerChapter? = chapters
+ .sortedBy(PlayerChapter::startTimeMs)
+ .firstOrNull { chapter ->
+ val endTimeMs = chapter.endTimeMs
+ positionMs >= chapter.startTimeMs &&
+ (endTimeMs == null || positionMs < endTimeMs)
+ }
+
+/**
+ * Returns the target for a previous-chapter action.
+ *
+ * A press later than the restart threshold restarts the current chapter. Near its
+ * start (or while in a gap), it targets the preceding chapter instead.
+ */
+internal fun previousChapterTarget(
+ chapters: List<PlayerChapter>,
+ positionMs: Long,
+ restartThresholdMs: Long = CHAPTER_RESTART_THRESHOLD_MS
+): Long? {
+ val sortedChapters = chapters.sortedBy(PlayerChapter::startTimeMs)
+ val current = currentChapter(sortedChapters, positionMs)
+ if (current != null && positionMs - current.startTimeMs > restartThresholdMs) {
+ return current.startTimeMs
+ }
+
+ return sortedChapters
+ .asSequence()
+ .filter { chapter -> chapter.startTimeMs < positionMs }
+ .lastOrNull { chapter -> chapter != current }
+ ?.startTimeMs
+}
+
+/** Returns the first chapter start strictly after [positionMs]. */
+internal fun nextChapterTarget(
+ chapters: List<PlayerChapter>,
+ positionMs: Long
+): Long? = chapters
+ .asSequence()
+ .sortedBy(PlayerChapter::startTimeMs)
+ .firstOrNull { chapter -> chapter.startTimeMs > positionMs }
+ ?.startTimeMs
diff --git a/feature/playback/src/main/java/com/streamvault/feature/playback/player/VodOverlayState.kt b/feature/playback/src/main/java/com/streamvault/feature/playback/player/VodOverlayState.kt
new file mode 100644
index 00000000..bbf95f3a
--- /dev/null
+++ b/feature/playback/src/main/java/com/streamvault/feature/playback/player/VodOverlayState.kt
@@ -0,0 +1,57 @@
+package com.streamvault.feature.playback.player
+
+import com.streamvault.player.PlayerChapter
+import java.util.Locale
+
+internal data class VodOverlayState(
+ val isVod: Boolean,
+ val chapters: List<PlayerChapter>,
+ val currentChapter: PlayerChapter?,
+ val previousChapterTargetMs: Long?,
+ val nextChapterTargetMs: Long?,
+ val showChapterAction: Boolean,
+ val showEpisodesAction: Boolean,
+ val showSubtitleAction: Boolean,
+ val showAudioAction: Boolean,
+ val showVideoQualityAction: Boolean,
+ val showExternalPlayerAction: Boolean,
+ val showSettingsAction: Boolean
+)
+
+internal fun buildVodOverlayState(
+ contentType: String,
+ isCatchUpPlayback: Boolean,
+ chapters: List<PlayerChapter>,
+ currentPositionMs: Long,
+ showEpisodesAction: Boolean,
+ subtitleTrackCount: Int,
+ audioTrackCount: Int,
+ videoQualityCount: Int,
+ showExternalPlayerAction: Boolean,
+ isCastConnected: Boolean
+): VodOverlayState {
+ val normalizedContentType = contentType.uppercase(Locale.ROOT)
+ val isVod = !isCatchUpPlayback && normalizedContentType in VOD_CONTENT_TYPES
+ val visibleChapters = if (isVod) {
+ chapters.sortedBy(PlayerChapter::startTimeMs)
+ } else {
+ emptyList()
+ }
+
+ return VodOverlayState(
+ isVod = isVod,
+ chapters = visibleChapters,
+ currentChapter = currentChapter(visibleChapters, currentPositionMs),
+ previousChapterTargetMs = previousChapterTarget(visibleChapters, currentPositionMs),
+ nextChapterTargetMs = nextChapterTarget(visibleChapters, currentPositionMs),
+ showChapterAction = isVod && visibleChapters.isNotEmpty() && !isCastConnected,
+ showEpisodesAction = isVod && normalizedContentType == "SERIES_EPISODE" && showEpisodesAction,
+ showSubtitleAction = isVod && subtitleTrackCount > 0,
+ showAudioAction = isVod && audioTrackCount > 0,
+ showVideoQualityAction = isVod && videoQualityCount > 0,
+ showExternalPlayerAction = isVod && showExternalPlayerAction,
+ showSettingsAction = isVod
+ )
+}
+
+private val VOD_CONTENT_TYPES = setOf("VOD", "MOVIE", "SERIES_EPISODE")
diff --git a/feature/playback/src/test/java/com/streamvault/feature/playback/player/VodChapterNavigationTest.kt b/feature/playback/src/test/java/com/streamvault/feature/playback/player/VodChapterNavigationTest.kt
new file mode 100644
index 00000000..175ba508
--- /dev/null
+++ b/feature/playback/src/test/java/com/streamvault/feature/playback/player/VodChapterNavigationTest.kt
@@ -0,0 +1,71 @@
+package com.streamvault.feature.playback.player
+
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.player.PlayerChapter
+import org.junit.Test
+
+class VodChapterNavigationTest {
+
+ private val chapters = listOf(
+ PlayerChapter(index = 1, title = "Opening", startTimeMs = 0L, endTimeMs = 60_000L),
+ PlayerChapter(index = 2, title = "The turn", startTimeMs = 60_000L, endTimeMs = 120_000L),
+ PlayerChapter(index = 3, title = "Finale", startTimeMs = 120_000L, endTimeMs = null)
+ )
+
+ @Test
+ fun `current chapter includes its start and excludes its end`() {
+ assertThat(currentChapter(chapters, positionMs = 60_000L)?.title).isEqualTo("The turn")
+ assertThat(currentChapter(chapters, positionMs = 119_999L)?.title).isEqualTo("The turn")
+ assertThat(currentChapter(chapters, positionMs = 120_000L)?.title).isEqualTo("Finale")
+ }
+
+ @Test
+ fun `current chapter is absent in a gap`() {
+ val chaptersWithGap = listOf(
+ chapters[0],
+ chapters[1].copy(startTimeMs = 70_000L)
+ )
+
+ assertThat(currentChapter(chaptersWithGap, positionMs = 65_000L)).isNull()
+ }
+
+ @Test
+ fun `previous target restarts current chapter after threshold`() {
+ assertThat(previousChapterTarget(chapters, positionMs = 75_000L)).isEqualTo(60_000L)
+ }
+
+ @Test
+ fun `previous target selects prior chapter near current start`() {
+ assertThat(previousChapterTarget(chapters, positionMs = 61_000L)).isEqualTo(0L)
+ }
+
+ @Test
+ fun `previous target selects prior chapter from a gap`() {
+ val chaptersWithGap = listOf(
+ chapters[0],
+ chapters[1].copy(startTimeMs = 70_000L)
+ )
+
+ assertThat(previousChapterTarget(chaptersWithGap, positionMs = 65_000L)).isEqualTo(0L)
+ }
+
+ @Test
+ fun `next target selects first chapter after position`() {
+ assertThat(nextChapterTarget(chapters, positionMs = 60_000L)).isEqualTo(120_000L)
+ assertThat(nextChapterTarget(chapters, positionMs = 119_999L)).isEqualTo(120_000L)
+ }
+
+ @Test
+ fun `next target is absent after final chapter`() {
+ assertThat(nextChapterTarget(chapters, positionMs = 120_000L)).isNull()
+ }
+
+ @Test
+ fun `navigation tolerates unordered input`() {
+ val unordered = listOf(chapters[2], chapters[0], chapters[1])
+
+ assertThat(currentChapter(unordered, positionMs = 80_000L)?.title).isEqualTo("The turn")
+ assertThat(previousChapterTarget(unordered, positionMs = 80_000L)).isEqualTo(60_000L)
+ assertThat(nextChapterTarget(unordered, positionMs = 80_000L)).isEqualTo(120_000L)
+ }
+}
diff --git a/feature/playback/src/test/java/com/streamvault/feature/playback/player/VodOverlayStateTest.kt b/feature/playback/src/test/java/com/streamvault/feature/playback/player/VodOverlayStateTest.kt
new file mode 100644
index 00000000..d30fc392
--- /dev/null
+++ b/feature/playback/src/test/java/com/streamvault/feature/playback/player/VodOverlayStateTest.kt
@@ -0,0 +1,145 @@
+package com.streamvault.feature.playback.player
+
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.player.PlayerChapter
+import org.junit.Test
+
+class VodOverlayStateTest {
+
+ private val chapter = PlayerChapter(
+ index = 1,
+ title = "Opening",
+ startTimeMs = 0L,
+ endTimeMs = 60_000L
+ )
+
+ @Test
+ fun `movie exposes chapter and track actions`() {
+ val state = buildVodOverlayState(
+ contentType = "MOVIE",
+ isCatchUpPlayback = false,
+ chapters = listOf(chapter),
+ currentPositionMs = 30_000L,
+ showEpisodesAction = false,
+ subtitleTrackCount = 2,
+ audioTrackCount = 1,
+ videoQualityCount = 3,
+ showExternalPlayerAction = true,
+ isCastConnected = false
+ )
+
+ assertThat(state.isVod).isTrue()
+ assertThat(state.currentChapter).isEqualTo(chapter)
+ assertThat(state.showChapterAction).isTrue()
+ assertThat(state.showEpisodesAction).isFalse()
+ assertThat(state.showSubtitleAction).isTrue()
+ assertThat(state.showAudioAction).isTrue()
+ assertThat(state.showVideoQualityAction).isTrue()
+ assertThat(state.showExternalPlayerAction).isTrue()
+ }
+
+ @Test
+ fun `series episode exposes episode action only when host says it is available`() {
+ val state = buildVodOverlayState(
+ contentType = "SERIES_EPISODE",
+ isCatchUpPlayback = false,
+ chapters = emptyList(),
+ currentPositionMs = 0L,
+ showEpisodesAction = true,
+ subtitleTrackCount = 0,
+ audioTrackCount = 0,
+ videoQualityCount = 0,
+ showExternalPlayerAction = false,
+ isCastConnected = false
+ )
+
+ assertThat(state.isVod).isTrue()
+ assertThat(state.showEpisodesAction).isTrue()
+ assertThat(state.showChapterAction).isFalse()
+ }
+
+ @Test
+ fun `cast hides chapter action while preserving navigation state`() {
+ val state = buildVodOverlayState(
+ contentType = "MOVIE",
+ isCatchUpPlayback = false,
+ chapters = listOf(chapter),
+ currentPositionMs = 30_000L,
+ showEpisodesAction = false,
+ subtitleTrackCount = 0,
+ audioTrackCount = 0,
+ videoQualityCount = 0,
+ showExternalPlayerAction = false,
+ isCastConnected = true
+ )
+
+ assertThat(state.chapters).containsExactly(chapter)
+ assertThat(state.currentChapter).isEqualTo(chapter)
+ assertThat(state.showChapterAction).isFalse()
+ }
+
+ @Test
+ fun `catch up playback does not expose vod actions`() {
+ val state = buildVodOverlayState(
+ contentType = "MOVIE",
+ isCatchUpPlayback = true,
+ chapters = listOf(chapter),
+ currentPositionMs = 30_000L,
+ showEpisodesAction = true,
+ subtitleTrackCount = 1,
+ audioTrackCount = 1,
+ videoQualityCount = 1,
+ showExternalPlayerAction = true,
+ isCastConnected = false
+ )
+
+ assertThat(state.isVod).isFalse()
+ assertThat(state.showChapterAction).isFalse()
+ assertThat(state.showEpisodesAction).isFalse()
+ assertThat(state.showSubtitleAction).isFalse()
+ assertThat(state.showAudioAction).isFalse()
+ assertThat(state.showVideoQualityAction).isFalse()
+ assertThat(state.showExternalPlayerAction).isFalse()
+ }
+
+ @Test
+ fun `live content does not expose vod actions`() {
+ val state = buildVodOverlayState(
+ contentType = "LIVE",
+ isCatchUpPlayback = false,
+ chapters = listOf(chapter),
+ currentPositionMs = 30_000L,
+ showEpisodesAction = true,
+ subtitleTrackCount = 1,
+ audioTrackCount = 1,
+ videoQualityCount = 1,
+ showExternalPlayerAction = true,
+ isCastConnected = false
+ )
+
+ assertThat(state.isVod).isFalse()
+ assertThat(state.showChapterAction).isFalse()
+ assertThat(state.showEpisodesAction).isFalse()
+ }
+
+ @Test
+ fun `negative capability counts are treated as unavailable`() {
+ val state = buildVodOverlayState(
+ contentType = "MOVIE",
+ isCatchUpPlayback = false,
+ chapters = emptyList(),
+ currentPositionMs = 0L,
+ showEpisodesAction = false,
+ subtitleTrackCount = -1,
+ audioTrackCount = -1,
+ videoQualityCount = -1,
+ showExternalPlayerAction = true,
+ isCastConnected = false
+ )
+
+ assertThat(state.showSubtitleAction).isFalse()
+ assertThat(state.showAudioAction).isFalse()
+ assertThat(state.showVideoQualityAction).isFalse()
+ assertThat(state.showExternalPlayerAction).isTrue()
+ }
+}