Skip to content

Commit

Permalink
Handle new handshake format in player
Browse files Browse the repository at this point in the history
  • Loading branch information
toasterofbread committed Dec 18, 2023
1 parent 32e06a4 commit 7ad3932
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 57 deletions.
39 changes: 11 additions & 28 deletions src/nativeMain/kotlin/cinterop/mpv/MpvCommandInterface.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
package cinterop.mpv

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.encodeToJsonElement
import spms.player.Player
import spms.server.SpMsServerState

@Serializable
data class ServerStatusData(
val queue: List<String>,
val state: Player.State,
val is_playing: Boolean,
val current_item_index: Int,
val current_position_ms: Long,
val duration_ms: Long,
val repeat_mode: Player.RepeatMode,
val volume: Double
)

fun Player.getCurrentStateJson(): JsonElement =
Json.encodeToJsonElement(
ServerStatusData(
(0 until item_count).map { getItem(it) ?: "" },
state,
is_playing,
current_item_index,
current_position_ms,
duration_ms,
repeat_mode,
volume
)
fun Player.getCurrentStateJson(): SpMsServerState =
SpMsServerState(
(0 until item_count).map { getItem(it) ?: "" },
state,
is_playing,
current_item_index,
current_position_ms.toInt(),
duration_ms.toInt(),
repeat_mode,
volume.toFloat()
)
24 changes: 5 additions & 19 deletions src/nativeMain/kotlin/spms/client/player/PlayerClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import spms.localisation.loc
import spms.player.Player
import spms.player.PlayerEvent
import spms.player.StreamProviderServer
import spms.server.PlayerOptions
import spms.server.SpMsClientHandshake
import spms.server.SpMsClientType
import spms.server.*
import kotlin.system.getTimeMillis

private const val SERVER_REPLY_TIMEOUT_MS: Long = 10000
Expand All @@ -37,18 +35,6 @@ private const val POLL_INTERVAL: Long = 100
private fun getClientName(): String =
"SpMs Player Client"

@Serializable
private data class ServerState(
val queue: List<String>,
val state: Player.State,
val is_playing: Boolean,
val current_item_index: Int,
val current_position_ms: Int,
val duration_ms: Int,
val repeat_mode: Player.RepeatMode,
val volume: Float
)

private abstract class PlayerImpl(headless: Boolean = true): MpvClientImpl(headless) {
override fun onEvent(event: PlayerEvent, clientless: Boolean) {
if (event.type == PlayerEvent.Type.READY_TO_PLAY) {
Expand All @@ -58,7 +44,7 @@ private abstract class PlayerImpl(headless: Boolean = true): MpvClientImpl(headl

abstract fun onReadyToPlay()

fun applyServerState(state: ServerState) {
fun applyServerState(state: SpMsServerState) {
clearQueue()
for (item in state.queue) {
addItem(item, -1)
Expand Down Expand Up @@ -188,14 +174,14 @@ class PlayerClient private constructor(): Command(
override fun idToUrl(item_id: String): String = stream_url + item_id
}

val state: ServerState = Json.decodeFromString(reply.first())
player.applyServerState(state)
val server_handshake: SpMsServerHandshake = Json.decodeFromString(reply.first())
player.applyServerState(server_handshake.server_state)

if (player_options.mute_on_start) {
player.setVolume(0.0)
}

log("Initial state: $state")
log("Initial state: ${server_handshake.server_state}")

runBlocking {
val message: MutableList<String> = mutableListOf()
Expand Down
16 changes: 14 additions & 2 deletions src/nativeMain/kotlin/spms/server/SpMsClient.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package spms.server

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import spms.localisation.Language
import spms.player.Player

typealias SpMsClientID = Int

Expand All @@ -25,7 +25,19 @@ data class SpMsServerHandshake(
val name: String,
val device_name: String,
val spms_commit_hash: String,
val server_state: JsonElement
val server_state: SpMsServerState
)

@Serializable
data class SpMsServerState(
val queue: List<String>,
val state: Player.State,
val is_playing: Boolean,
val current_item_index: Int,
val current_position_ms: Int,
val duration_ms: Int,
val repeat_mode: Player.RepeatMode,
val volume: Float
)

@Serializable
Expand Down
11 changes: 3 additions & 8 deletions src/nativeMain/kotlin/spms/serveraction/ServerActionGetStatus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.*
import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath
Expand All @@ -39,14 +34,14 @@ class ServerActionGetStatus: ServerAction(
parameters = emptyList()
) {
override fun execute(server: SpMs, context: ActionContext): JsonElement {
return server.player.getCurrentStateJson()
return Json.encodeToJsonElement(server.player.getCurrentStateJson())
}

private val cache_files: MutableMap<String, JsonElement> = mutableMapOf()

private fun loadCacheFiles() {
cache_files.clear()
val files = FileSystem.SYSTEM.listOrNull(getCacheDir()) ?: return
val files: List<Path> = FileSystem.SYSTEM.listOrNull(getCacheDir()) ?: return
for (file in files) {
val content: String = FileSystem.SYSTEM.read(file) { readUtf8() }
cache_files[file.name.removeSuffix(".json")] = Json.parseToJsonElement(content)
Expand Down

0 comments on commit 7ad3932

Please sign in to comment.