Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix app crashing when playback reporting fails #4362

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 56 additions & 49 deletions playback/jellyfin/src/main/kotlin/playsession/PlaySessionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.jellyfin.sdk.model.api.PlaybackStartInfo
import org.jellyfin.sdk.model.api.PlaybackStopInfo
import org.jellyfin.sdk.model.api.QueueItem
import org.jellyfin.sdk.model.extensions.inWholeTicks
import timber.log.Timber
import kotlin.math.roundToInt
import org.jellyfin.sdk.model.api.RepeatMode as SdkRepeatMode

Expand Down Expand Up @@ -70,71 +71,77 @@ class PlaySessionService(
val stream = entry.mediaStream ?: return
val item = entry.baseItem ?: return

api.playStateApi.reportPlaybackStart(
PlaybackStartInfo(
itemId = item.id,
playSessionId = stream.identifier,
playlistItemId = item.playlistItemId,
canSeek = true,
isMuted = state.volume.muted,
volumeLevel = (state.volume.volume * 100).roundToInt(),
isPaused = state.playState.value != PlayState.PLAYING,
aspectRatio = state.videoSize.value.aspectRatio.toString(),
positionTicks = withContext(Dispatchers.Main) { state.positionInfo.active.inWholeTicks },
playMethod = stream.conversionMethod.playMethod,
repeatMode = state.repeatMode.value.remoteRepeatMode,
nowPlayingQueue = getQueue(),
playbackOrder = when (state.playbackOrder.value) {
org.jellyfin.playback.core.model.PlaybackOrder.DEFAULT -> PlaybackOrder.DEFAULT
org.jellyfin.playback.core.model.PlaybackOrder.RANDOM -> PlaybackOrder.SHUFFLE
org.jellyfin.playback.core.model.PlaybackOrder.SHUFFLE -> PlaybackOrder.SHUFFLE
}
runCatching {
api.playStateApi.reportPlaybackStart(
PlaybackStartInfo(
itemId = item.id,
playSessionId = stream.identifier,
playlistItemId = item.playlistItemId,
canSeek = true,
isMuted = state.volume.muted,
volumeLevel = (state.volume.volume * 100).roundToInt(),
isPaused = state.playState.value != PlayState.PLAYING,
aspectRatio = state.videoSize.value.aspectRatio.toString(),
positionTicks = withContext(Dispatchers.Main) { state.positionInfo.active.inWholeTicks },
playMethod = stream.conversionMethod.playMethod,
repeatMode = state.repeatMode.value.remoteRepeatMode,
nowPlayingQueue = getQueue(),
playbackOrder = when (state.playbackOrder.value) {
org.jellyfin.playback.core.model.PlaybackOrder.DEFAULT -> PlaybackOrder.DEFAULT
org.jellyfin.playback.core.model.PlaybackOrder.RANDOM -> PlaybackOrder.SHUFFLE
org.jellyfin.playback.core.model.PlaybackOrder.SHUFFLE -> PlaybackOrder.SHUFFLE
}
)
)
)
}.onFailure { error -> Timber.w("Failed to send playback start event", error) }
}

private suspend fun sendStreamUpdate() {
val entry = manager.queue.entry.value ?: return
val stream = entry.mediaStream ?: return
val item = entry.baseItem ?: return

api.playStateApi.reportPlaybackProgress(
PlaybackProgressInfo(
itemId = item.id,
playSessionId = stream.identifier,
playlistItemId = item.playlistItemId,
canSeek = true,
isMuted = state.volume.muted,
volumeLevel = (state.volume.volume * 100).roundToInt(),
isPaused = state.playState.value != PlayState.PLAYING,
aspectRatio = state.videoSize.value.aspectRatio.toString(),
positionTicks = withContext(Dispatchers.Main) { state.positionInfo.active.inWholeTicks },
playMethod = stream.conversionMethod.playMethod,
repeatMode = state.repeatMode.value.remoteRepeatMode,
nowPlayingQueue = getQueue(),
playbackOrder = when (state.playbackOrder.value) {
org.jellyfin.playback.core.model.PlaybackOrder.DEFAULT -> PlaybackOrder.DEFAULT
org.jellyfin.playback.core.model.PlaybackOrder.RANDOM -> PlaybackOrder.SHUFFLE
org.jellyfin.playback.core.model.PlaybackOrder.SHUFFLE -> PlaybackOrder.SHUFFLE
}
runCatching {
api.playStateApi.reportPlaybackProgress(
PlaybackProgressInfo(
itemId = item.id,
playSessionId = stream.identifier,
playlistItemId = item.playlistItemId,
canSeek = true,
isMuted = state.volume.muted,
volumeLevel = (state.volume.volume * 100).roundToInt(),
isPaused = state.playState.value != PlayState.PLAYING,
aspectRatio = state.videoSize.value.aspectRatio.toString(),
positionTicks = withContext(Dispatchers.Main) { state.positionInfo.active.inWholeTicks },
playMethod = stream.conversionMethod.playMethod,
repeatMode = state.repeatMode.value.remoteRepeatMode,
nowPlayingQueue = getQueue(),
playbackOrder = when (state.playbackOrder.value) {
org.jellyfin.playback.core.model.PlaybackOrder.DEFAULT -> PlaybackOrder.DEFAULT
org.jellyfin.playback.core.model.PlaybackOrder.RANDOM -> PlaybackOrder.SHUFFLE
org.jellyfin.playback.core.model.PlaybackOrder.SHUFFLE -> PlaybackOrder.SHUFFLE
}
)
)
)
}.onFailure { error -> Timber.w("Failed to send playback update event", error) }
}

private suspend fun sendStreamStop() {
val entry = manager.queue.entry.value ?: return
val stream = entry.mediaStream ?: return
val item = entry.baseItem ?: return

api.playStateApi.reportPlaybackStopped(
PlaybackStopInfo(
itemId = item.id,
playSessionId = stream.identifier,
playlistItemId = item.playlistItemId,
positionTicks = withContext(Dispatchers.Main) { state.positionInfo.active.inWholeTicks },
failed = false,
nowPlayingQueue = getQueue(),
runCatching {
api.playStateApi.reportPlaybackStopped(
PlaybackStopInfo(
itemId = item.id,
playSessionId = stream.identifier,
playlistItemId = item.playlistItemId,
positionTicks = withContext(Dispatchers.Main) { state.positionInfo.active.inWholeTicks },
failed = false,
nowPlayingQueue = getQueue(),
)
)
)
}.onFailure { error -> Timber.w("Failed to send playback stop event", error) }
}
}
Loading