Skip to content

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

Preview handoff source disambiguation

a62a1a6327682
diff --git a/app/src/main/java/com/streamvault/app/player/LivePreviewHandoffManager.kt b/app/src/main/java/com/streamvault/app/player/LivePreviewHandoffManager.kt
index c9f3018b..727c71d5 100644
--- a/app/src/main/java/com/streamvault/app/player/LivePreviewHandoffManager.kt
+++ b/app/src/main/java/com/streamvault/app/player/LivePreviewHandoffManager.kt
@@ -10,27 +10,49 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
+enum class PreviewHandoffSource { HOME, GUIDE }
+
@Singleton
class LivePreviewHandoffManager @Inject constructor() {
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
private var pendingReleaseJob: Job? = null
private var session: LivePreviewHandoffSession? = null
+ private var pendingReverseReleaseJob: Job? = null
+ private val _reverseSessionFlow = MutableStateFlow<LivePreviewHandoffSession?>(null)
+ val reverseSessionFlow: StateFlow<LivePreviewHandoffSession?> = _reverseSessionFlow.asStateFlow()
+ private val reverseSession: LivePreviewHandoffSession?
+ get() = _reverseSessionFlow.value
fun registerPreviewSession(
channel: Channel,
streamInfo: StreamInfo,
- engine: PlayerEngine
+ engine: PlayerEngine,
+ source: PreviewHandoffSource = PreviewHandoffSource.HOME
+ ) {
+ registerPreviewSession(channel.id, channel.providerId, streamInfo, engine, source)
+ }
+
+ fun registerPreviewSession(
+ channelId: Long,
+ providerId: Long,
+ streamInfo: StreamInfo,
+ engine: PlayerEngine,
+ source: PreviewHandoffSource = PreviewHandoffSource.HOME
) {
val previous = session
pendingReleaseJob?.cancel()
session = LivePreviewHandoffSession(
engine = engine,
- channelId = channel.id,
- providerId = channel.providerId,
+ channelId = channelId,
+ providerId = providerId,
streamInfo = streamInfo,
- pendingFullscreen = false
+ pendingFullscreen = false,
+ source = source
)
if (previous != null && previous.engine !== engine) {
previous.engine.release()
@@ -73,12 +95,46 @@ class LivePreviewHandoffManager @Inject constructor() {
session = null
}
+ fun beginReverseHandoff(
+ channel: Channel,
+ streamInfo: StreamInfo,
+ engine: PlayerEngine,
+ source: PreviewHandoffSource = PreviewHandoffSource.HOME
+ ) {
+ pendingReverseReleaseJob?.cancel()
+ _reverseSessionFlow.value = LivePreviewHandoffSession(
+ engine = engine,
+ channelId = channel.id,
+ providerId = channel.providerId,
+ streamInfo = streamInfo,
+ pendingFullscreen = false,
+ source = source
+ )
+ pendingReverseReleaseJob = scope.launch {
+ delay(PENDING_FULLSCREEN_TIMEOUT_MS)
+ val stale = _reverseSessionFlow.value
+ if (stale != null) {
+ _reverseSessionFlow.value = null
+ stale.engine.release()
+ }
+ }
+ }
+
+ fun consumeReverseHandoff(forSource: PreviewHandoffSource? = null): LivePreviewHandoffSession? {
+ val s = _reverseSessionFlow.value ?: return null
+ if (forSource != null && s.source != forSource) return null
+ pendingReverseReleaseJob?.cancel()
+ _reverseSessionFlow.value = null
+ return s
+ }
+
data class LivePreviewHandoffSession(
val engine: PlayerEngine,
val channelId: Long,
val providerId: Long,
val streamInfo: StreamInfo,
val pendingFullscreen: Boolean,
+ val source: PreviewHandoffSource = PreviewHandoffSource.HOME,
val updatedAtMs: Long = System.currentTimeMillis()
)
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/home/HomeViewModel.kt b/app/src/main/java/com/streamvault/app/ui/screens/home/HomeViewModel.kt
index d1274ba0..a4b9b62f 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/home/HomeViewModel.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/home/HomeViewModel.kt
@@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.streamvault.app.di.AuxiliaryPlayerEngine
import com.streamvault.app.player.LivePreviewHandoffManager
+import com.streamvault.app.player.PreviewHandoffSource
import com.streamvault.app.plugins.StreamVaultPluginManager
import com.streamvault.app.tvinput.TvInputChannelSyncManager
import com.streamvault.app.ui.screens.multiview.MultiViewManager
@@ -118,6 +119,13 @@ class HomeViewModel @Inject constructor(
init {
loadAllProviders()
+ viewModelScope.launch {
+ livePreviewHandoffManager.reverseSessionFlow.collect { session ->
+ if (session != null && session.source == PreviewHandoffSource.HOME) {
+ resumePreviewFromHandoff()
+ }
+ }
+ }
viewModelScope.launch {
combine(
multiViewManager.slots,
@@ -1057,7 +1065,8 @@ class HomeViewModel @Inject constructor(
livePreviewHandoffManager.registerPreviewSession(
channel = channel,
streamInfo = preparedStreamInfo,
- engine = engine
+ engine = engine,
+ source = PreviewHandoffSource.HOME
)
scheduleAdaptivePreviewReprime(
previewVersion = previewVersion,
@@ -1128,6 +1137,50 @@ class HomeViewModel @Inject constructor(
return true
}
+ fun resumePreviewFromHandoff() {
+ val session = livePreviewHandoffManager.consumeReverseHandoff(PreviewHandoffSource.HOME) ?: return
+ val engine = session.engine
+ previewSessionVersion++
+ val version = previewSessionVersion
+ previewPlaybackJob?.cancel()
+ previewErrorJob?.cancel()
+ previewPlaybackJob = null
+ previewErrorJob = null
+ previewPlayerEngine?.stop()
+ previewPlayerEngine?.release()
+ previewPlayerEngine = engine
+ // Restore auxiliary-engine defaults that the fullscreen handoff flipped.
+ (engine as? com.streamvault.player.Media3PlayerEngine)?.let {
+ it.enableMediaSession = false
+ it.bypassAudioFocus = true
+ }
+ engine.play()
+ // Re-register the forward-handoff slot so a subsequent click can open fullscreen again.
+ livePreviewHandoffManager.registerPreviewSession(
+ channelId = session.channelId,
+ providerId = session.providerId,
+ streamInfo = session.streamInfo,
+ engine = engine,
+ source = PreviewHandoffSource.HOME
+ )
+ _uiState.update {
+ it.copy(
+ previewChannelId = session.channelId,
+ previewPlayerEngine = engine,
+ isPreviewLoading = false,
+ previewErrorMessage = null
+ )
+ }
+ previewPlaybackJob = viewModelScope.launch {
+ engine.playbackState.collectLatest { state ->
+ if (!isActivePreviewSession(version, session.channelId)) return@collectLatest
+ if (state == PlaybackState.ERROR && _uiState.value.previewErrorMessage == null) {
+ _uiState.update { it.copy(previewErrorMessage = appContext.getString(R.string.live_preview_failed)) }
+ }
+ }
+ }
+ }
+
fun clearPreview() {
if (previewPlayerEngine == null) return
previewSessionVersion++
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerLifecycleActions.kt b/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerLifecycleActions.kt
index 78242761..fe726654 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerLifecycleActions.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerLifecycleActions.kt
@@ -3,6 +3,7 @@ package com.streamvault.app.ui.screens.player
import androidx.lifecycle.viewModelScope
import com.streamvault.domain.model.ContentType
import com.streamvault.domain.model.ProviderType
+import com.streamvault.player.PlaybackState
import com.streamvault.player.PlayerEngine
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@@ -175,11 +176,32 @@ internal fun PlayerViewModel.cleanupAfterCleared(mainPlayerEngine: PlayerEngine)
inFlightThumbnailPreloadKey = null
lastCompletedThumbnailPreloadKey = null
seekThumbnailProvider.clearCache()
- livePreviewHandoffManager.clear(playerEngine)
- if (playerEngine === mainPlayerEngine) {
+
+ val activeEngine = playerEngine
+ val channel = currentChannel.value
+ val streamInfo = currentResolvedStreamInfo
+ val canReverseHandoff = currentContentType == ContentType.LIVE
+ && !isCatchUpPlayback.value
+ && activeEngine !== mainPlayerEngine
+ && channel != null
+ && streamInfo != null
+ && activeEngine.playbackState.value != PlaybackState.ERROR
+
+ if (canReverseHandoff) {
+ livePreviewHandoffManager.beginReverseHandoff(
+ channel = channel!!,
+ streamInfo = streamInfo!!,
+ engine = activeEngine,
+ source = adoptedHandoffSource ?: com.streamvault.app.player.PreviewHandoffSource.HOME
+ )
mainPlayerEngine.resetForReuse()
} else {
- playerEngine.release()
- mainPlayerEngine.resetForReuse()
+ livePreviewHandoffManager.clear(activeEngine)
+ if (activeEngine === mainPlayerEngine) {
+ mainPlayerEngine.resetForReuse()
+ } else {
+ activeEngine.release()
+ mainPlayerEngine.resetForReuse()
+ }
}
}
\ No newline at end of file
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerViewModel.kt b/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerViewModel.kt
index 8ce5dff0..a4dd3071 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerViewModel.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerViewModel.kt
@@ -250,6 +250,7 @@ class PlayerViewModel @Inject constructor(
internal var currentArtworkUrl: String? = null
internal var currentResolvedPlaybackUrl: String = ""
internal var currentResolvedStreamInfo: StreamInfo? = null
+ internal var adoptedHandoffSource: com.streamvault.app.player.PreviewHandoffSource? = null
internal var pendingCatchUpUrls: List<String> = emptyList()
internal var channelNumberingMode: ChannelNumberingMode = ChannelNumberingMode.GROUP
set(value) {
@@ -1075,6 +1076,7 @@ class PlayerViewModel @Inject constructor(
providerId = providerId.takeIf { it > 0L }
) ?: return false
+ adoptedHandoffSource = session.source
val adoptedEngine = session.engine
return runCatching {
val shouldRenewAdoptedPreview = shouldRenewAdoptedPreviewOnFullscreen(