-
-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate WebSockets to new SocketApi from SDK
- Loading branch information
1 parent
c0875f0
commit a9aee39
Showing
7 changed files
with
183 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
app/src/main/java/org/jellyfin/androidtv/data/eventhandling/SocketListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package org.jellyfin.androidtv.data.eventhandling | ||
|
||
import android.content.Context | ||
import android.widget.Toast | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import org.jellyfin.androidtv.TvApp | ||
import org.jellyfin.androidtv.data.model.DataRefreshService | ||
import org.jellyfin.androidtv.ui.playback.MediaManager | ||
import org.jellyfin.androidtv.util.apiclient.PlaybackHelper | ||
import org.jellyfin.apiclient.model.entities.MediaType | ||
import org.jellyfin.sdk.api.client.ApiClient | ||
import org.jellyfin.sdk.api.client.extensions.sessionApi | ||
import org.jellyfin.sdk.api.client.extensions.socket | ||
import org.jellyfin.sdk.api.client.extensions.userLibraryApi | ||
import org.jellyfin.sdk.api.sockets.SocketInstance | ||
import org.jellyfin.sdk.api.sockets.addGeneralCommandsListener | ||
import org.jellyfin.sdk.api.sockets.addListener | ||
import org.jellyfin.sdk.model.api.GeneralCommandType | ||
import org.jellyfin.sdk.model.api.LibraryUpdateInfo | ||
import org.jellyfin.sdk.model.api.PlaystateCommand | ||
import org.jellyfin.sdk.model.extensions.getValue | ||
import org.jellyfin.sdk.model.serializer.toUUIDOrNull | ||
import org.jellyfin.sdk.model.socket.LibraryChangedMessage | ||
import org.jellyfin.sdk.model.socket.PlayMessage | ||
import org.jellyfin.sdk.model.socket.PlayStateMessage | ||
import timber.log.Timber | ||
import java.util.* | ||
|
||
class SocketListener( | ||
private val context: Context, | ||
private val api: ApiClient, | ||
private val dataRefreshService: DataRefreshService, | ||
private val mediaManager: MediaManager, | ||
) { | ||
private var socketInstance: SocketInstance? = null | ||
|
||
suspend fun updateSession() { | ||
api.sessionApi.postCapabilities( | ||
playableMediaTypes = listOf(MediaType.Video, MediaType.Audio), | ||
supportsMediaControl = true, | ||
supportedCommands = listOf( | ||
GeneralCommandType.DISPLAY_MESSAGE, | ||
GeneralCommandType.SEND_STRING, | ||
), | ||
) | ||
|
||
if (socketInstance != null) socketInstance?.updateCredentials() | ||
else socketInstance = createInstance() | ||
} | ||
|
||
private fun createInstance() = api.socket.createInstance().apply { | ||
// Library | ||
addListener<LibraryChangedMessage> { message -> onLibraryChanged(message.info) } | ||
|
||
// Media playback | ||
addListener<PlayMessage> { message -> onPlayMessage(message) } | ||
addListener<PlayStateMessage> { message -> onPlayStateMessage(message) } | ||
|
||
// General commands | ||
addGeneralCommandsListener(setOf(GeneralCommandType.DISPLAY_CONTENT)) { message -> | ||
val itemId by message | ||
val itemUuid = itemId?.toUUIDOrNull() | ||
|
||
if (itemUuid != null) onDisplayContent(itemUuid) | ||
} | ||
addGeneralCommandsListener(setOf(GeneralCommandType.DISPLAY_MESSAGE, GeneralCommandType.SEND_STRING)) { message -> | ||
val header by message | ||
val text by message | ||
val string by message | ||
|
||
onDisplayMessage(header, text ?: string) | ||
} | ||
} | ||
|
||
private fun onLibraryChanged(info: LibraryUpdateInfo) { | ||
Timber.d(buildString { | ||
append("Library changed.") | ||
append(" added=", info.itemsAdded!!.size) | ||
append(" removed=", info.itemsRemoved!!.size) | ||
append(" updated=", info.itemsUpdated!!.size) | ||
}) | ||
|
||
if (info.itemsAdded!!.any() || info.itemsRemoved!!.any()) | ||
dataRefreshService.lastLibraryChange = System.currentTimeMillis() | ||
} | ||
|
||
private fun onPlayMessage(message: PlayMessage) { | ||
val itemId = message.request.itemIds?.firstOrNull() ?: return | ||
|
||
PlaybackHelper.retrieveAndPlay( | ||
itemId.toString(), | ||
false, | ||
message.request.startPositionTicks, | ||
context | ||
) | ||
} | ||
|
||
private fun onPlayStateMessage(message: PlayStateMessage) = socketInstance?.launch(Dispatchers.Main) { | ||
Timber.i("Received PlayStateMessage with command ${message.request.command}") | ||
|
||
val playbackController = TvApp.getApplication()!!.playbackController | ||
when (message.request.command) { | ||
PlaystateCommand.STOP -> { | ||
if (mediaManager.isAudioPlayerInitialized) mediaManager.stopAudio(true) | ||
else playbackController?.endPlayback(true) | ||
} | ||
PlaystateCommand.PAUSE, | ||
PlaystateCommand.UNPAUSE, | ||
PlaystateCommand.PLAY_PAUSE -> { | ||
if (mediaManager.isAudioPlayerInitialized) mediaManager.playPauseAudio() | ||
else playbackController?.playPause() | ||
} | ||
PlaystateCommand.NEXT_TRACK -> { | ||
if (mediaManager.isAudioPlayerInitialized) mediaManager.nextAudioItem() | ||
else playbackController?.next() | ||
} | ||
PlaystateCommand.PREVIOUS_TRACK -> { | ||
if (mediaManager.isAudioPlayerInitialized) mediaManager.prevAudioItem() | ||
else playbackController?.prev() | ||
} | ||
PlaystateCommand.SEEK -> { | ||
if (mediaManager.isAudioPlayerInitialized) return@launch | ||
val pos = (message.request.seekPositionTicks ?: 0) / 10000 | ||
playbackController?.seek(pos) | ||
} | ||
PlaystateCommand.REWIND -> { | ||
// FIXME get value from displayprefs | ||
if (!mediaManager.isAudioPlayerInitialized) playbackController?.skip(-11000) | ||
} | ||
PlaystateCommand.FAST_FORWARD -> { | ||
// FIXME get value from displayprefs | ||
if (!mediaManager.isAudioPlayerInitialized) playbackController?.skip(30000) | ||
} | ||
} | ||
} | ||
|
||
private fun onDisplayContent(itemId: UUID) { | ||
val playbackController = TvApp.getApplication()?.playbackController | ||
|
||
if (playbackController?.isPlaying == true || playbackController?.isPaused == true) { | ||
Timber.i("Not launching $itemId: playback in progress") | ||
return | ||
} | ||
|
||
Timber.i("Launching $itemId") | ||
|
||
socketInstance?.launch { | ||
val item by api.userLibraryApi.getItem(itemId = itemId) | ||
// FIXME: Add an ItemLauncher function that accepts SDK BaseItemDto | ||
// Add "GeneralCommandType.DISPLAY_CONTENT" to supportedCommands to enable | ||
// ItemLauncher.launch(item) | ||
} | ||
} | ||
|
||
private fun onDisplayMessage(header: String?, text: String?) { | ||
val toastMessage = buildString { | ||
if (!header.isNullOrBlank()) append(header, ": ") | ||
append(text) | ||
} | ||
|
||
runBlocking(Dispatchers.Main) { | ||
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
} |
Oops, something went wrong.