Skip to content

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

fix(playback): route live preview handoff back to its origin surface

David Nashash8f7e0d768bd1
diff --git a/feature/playback/src/main/java/com/streamvault/feature/playback/player/PlayerLifecycleActions.kt b/feature/playback/src/main/java/com/streamvault/feature/playback/player/PlayerLifecycleActions.kt
index 905e74b1..d107f543 100644
--- a/feature/playback/src/main/java/com/streamvault/feature/playback/player/PlayerLifecycleActions.kt
+++ b/feature/playback/src/main/java/com/streamvault/feature/playback/player/PlayerLifecycleActions.kt
@@ -188,11 +188,13 @@ internal fun PlayerViewModel.cleanupAfterCleared(mainPlayerEngine: PlayerEngine)
val activeEngine = playerEngine
val channel = currentChannel.value
val streamInfo = currentResolvedStreamInfo
+ val reverseHandoffSource = playerPreviewCoordinator.consumeAdoptedHandoffSource()
val canReverseHandoff = currentContentType == ContentType.LIVE
&& !isCatchUpPlayback.value
&& activeEngine !== mainPlayerEngine
&& channel != null
&& streamInfo != null
+ && reverseHandoffSource != null
&& activeEngine.playbackState.value != PlaybackState.ERROR
if (canReverseHandoff) {
@@ -200,7 +202,7 @@ internal fun PlayerViewModel.cleanupAfterCleared(mainPlayerEngine: PlayerEngine)
channel = channel!!,
streamInfo = streamInfo!!,
engine = activeEngine,
- source = com.streamvault.feature.playback.preview.PreviewHandoffSource.HOME
+ source = reverseHandoffSource!!
)
mainPlayerEngine.resetForReuse()
} else {
diff --git a/feature/playback/src/main/java/com/streamvault/feature/playback/player/PlayerPreviewCoordinator.kt b/feature/playback/src/main/java/com/streamvault/feature/playback/player/PlayerPreviewCoordinator.kt
index bd7b7f12..4c9c17ae 100644
--- a/feature/playback/src/main/java/com/streamvault/feature/playback/player/PlayerPreviewCoordinator.kt
+++ b/feature/playback/src/main/java/com/streamvault/feature/playback/player/PlayerPreviewCoordinator.kt
@@ -19,6 +19,8 @@ class PlayerPreviewCoordinator @Inject constructor(
private val preparationCoordinator: PlayerPreparationCoordinator,
private val preferencesCoordinator: PlayerPreferencesCoordinator,
) {
+ private var adoptedHandoffSource: PreviewHandoffSource? = null
+
internal fun consumeFullscreenHandoff(
channelId: Long,
providerId: Long?
@@ -29,6 +31,20 @@ class PlayerPreviewCoordinator @Inject constructor(
handoffManager.clear(engine)
}
+ /**
+ * Returns the browse surface that handed the active engine to the fullscreen
+ * player, or null when the active engine was not adopted from a preview.
+ *
+ * Consuming resets the value so a later teardown cannot reverse-handoff a
+ * stream to a stale origin. This is what routes a Guide preview back to the
+ * Guide instead of letting the Home surface silently adopt it.
+ */
+ internal fun consumeAdoptedHandoffSource(): PreviewHandoffSource? {
+ val source = adoptedHandoffSource
+ adoptedHandoffSource = null
+ return source
+ }
+
internal fun beginReverseHandoff(
channel: Channel,
streamInfo: StreamInfo,
@@ -58,6 +74,7 @@ class PlayerPreviewCoordinator @Inject constructor(
logInfo("Skipping preview handoff for Fire TV live HLS; fullscreen will prepare a fresh session.")
clear(session.engine)
session.engine.release()
+ adoptedHandoffSource = null
return false
}
@@ -77,12 +94,14 @@ class PlayerPreviewCoordinator @Inject constructor(
if (!isCurrent()) {
engineCoordinator.switchTo(engineCoordinator.mainEngine)
adoptedEngine.release()
+ adoptedHandoffSource = null
false
} else {
onAdopted(session.streamInfo)
adoptedEngine.resetLiveHandoffGrace()
engineCoordinator.currentEngine.play()
onStarted(session.streamInfo)
+ adoptedHandoffSource = session.source
true
}
}.getOrElse {
@@ -91,6 +110,7 @@ class PlayerPreviewCoordinator @Inject constructor(
engineCoordinator.switchTo(engineCoordinator.mainEngine)
}
adoptedEngine.release()
+ adoptedHandoffSource = null
false
}
}
diff --git a/feature/playback/src/test/java/com/streamvault/feature/playback/player/PlayerPreviewCoordinatorTest.kt b/feature/playback/src/test/java/com/streamvault/feature/playback/player/PlayerPreviewCoordinatorTest.kt
new file mode 100644
index 00000000..e5414005
--- /dev/null
+++ b/feature/playback/src/test/java/com/streamvault/feature/playback/player/PlayerPreviewCoordinatorTest.kt
@@ -0,0 +1,122 @@
+package com.streamvault.feature.playback.player
+
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.domain.model.Channel
+import com.streamvault.domain.model.ContentType
+import com.streamvault.domain.model.StreamInfo
+import com.streamvault.feature.playback.preview.LivePreviewHandoffManager
+import com.streamvault.feature.playback.preview.PreviewHandoffSource
+import com.streamvault.player.PlayerEngine
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.flow.flowOf
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.resetMain
+import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.test.setMain
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.whenever
+
+@OptIn(ExperimentalCoroutinesApi::class)
+class PlayerPreviewCoordinatorTest {
+ private val testDispatcher = StandardTestDispatcher()
+ private lateinit var handoffManager: LivePreviewHandoffManager
+ private lateinit var engineCoordinator: PlayerEngineCoordinator
+ private lateinit var preparationCoordinator: PlayerPreparationCoordinator
+ private lateinit var preferencesCoordinator: PlayerPreferencesCoordinator
+
+ @Before
+ fun setUp() {
+ Dispatchers.setMain(testDispatcher)
+ handoffManager = LivePreviewHandoffManager()
+ engineCoordinator = PlayerEngineCoordinator(mock())
+ preparationCoordinator = mock()
+ preferencesCoordinator = mock()
+ whenever(preferencesCoordinator.playerMediaSessionEnabled).thenReturn(flowOf(true))
+ }
+
+ @After
+ fun tearDown() {
+ Dispatchers.resetMain()
+ }
+
+ private fun coordinator(): PlayerPreviewCoordinator = PlayerPreviewCoordinator(
+ handoffManager = handoffManager,
+ engineCoordinator = engineCoordinator,
+ preparationCoordinator = preparationCoordinator,
+ preferencesCoordinator = preferencesCoordinator
+ )
+
+ private suspend fun adopt(
+ coordinator: PlayerPreviewCoordinator,
+ channel: Channel,
+ source: PreviewHandoffSource
+ ): Boolean {
+ val previewEngine = mock<PlayerEngine>()
+ val streamInfo = StreamInfo(url = "https://example.com/live.m3u8", title = channel.name)
+ handoffManager.registerPreviewSession(
+ channel = channel,
+ streamInfo = streamInfo,
+ engine = previewEngine,
+ source = source
+ )
+ handoffManager.beginFullscreenHandoff(channel.id, previewEngine)
+ return coordinator.tryAdoptFullscreenHandoff(
+ channelId = channel.id,
+ providerId = channel.providerId,
+ contentType = ContentType.LIVE,
+ audioVideoOffsetMs = 0,
+ isCurrent = { true },
+ onAdopted = {},
+ onStarted = {}
+ )
+ }
+
+ @Test
+ fun `adopting a guide preview records guide as the reverse handoff source`() = runTest(testDispatcher) {
+ val coordinator = coordinator()
+ val channel = channel(id = 10L, providerId = 3L)
+
+ assertThat(adopt(coordinator, channel, PreviewHandoffSource.GUIDE)).isTrue()
+ assertThat(coordinator.consumeAdoptedHandoffSource()).isEqualTo(PreviewHandoffSource.GUIDE)
+ assertThat(coordinator.consumeAdoptedHandoffSource()).isNull()
+ }
+
+ @Test
+ fun `adopting a home preview records home as the reverse handoff source`() = runTest(testDispatcher) {
+ val coordinator = coordinator()
+ val channel = channel(id = 11L, providerId = 3L)
+
+ assertThat(adopt(coordinator, channel, PreviewHandoffSource.HOME)).isTrue()
+ assertThat(coordinator.consumeAdoptedHandoffSource()).isEqualTo(PreviewHandoffSource.HOME)
+ }
+
+ @Test
+ fun `preparing without a preview handoff records no reverse handoff source`() = runTest(testDispatcher) {
+ val coordinator = coordinator()
+
+ val adopted = coordinator.tryAdoptFullscreenHandoff(
+ channelId = 12L,
+ providerId = 3L,
+ contentType = ContentType.LIVE,
+ audioVideoOffsetMs = 0,
+ isCurrent = { true },
+ onAdopted = {},
+ onStarted = {}
+ )
+
+ assertThat(adopted).isFalse()
+ assertThat(coordinator.consumeAdoptedHandoffSource()).isNull()
+ }
+
+ private fun channel(id: Long, providerId: Long): Channel = Channel(
+ id = id,
+ name = "Preview $id",
+ streamUrl = "stream://$id",
+ categoryId = 1L,
+ providerId = providerId
+ )
+}