Skip to content

Commit

Permalink
Add repeat_threshold_ms param to player.seekToPrevious, improve Serve…
Browse files Browse the repository at this point in the history
…rActions
  • Loading branch information
toasterofbread committed Nov 14, 2024
1 parent f5337bf commit 6d8ea1b
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import okio.FileSystem
import okio.Path.Companion.toPath
import dev.toastbits.spms.socketapi.shared.SpMsPlayerEvent
import dev.toastbits.spms.player.VideoInfoProvider
import dev.toastbits.spms.player.shouldRepeatOnSeekToPrevious
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsPlayerRepeatMode
import dev.toastbits.spms.socketapi.shared.SpMsPlayerState
import dev.toastbits.spms.PLATFORM
import gen.libmpv.LibMpv
import kotlin.math.roundToInt
import kotlin.time.*
import kotlin.time.Duration

private const val URL_PREFIX: String = "spmp://"

Expand Down Expand Up @@ -128,12 +130,18 @@ abstract class MpvClientImpl(
return false
}

override fun seekToPrevious(): Boolean {
if (runCommand("playlist-prev", check_result = false) == 0) {
onEvent(SpMsPlayerEvent.ItemTransition(current_item_index))
override fun seekToPrevious(repeat_threshold: Duration?): Boolean {
if (shouldRepeatOnSeekToPrevious(repeat_threshold)) {
seekToTime(0)
return true
}
return false
else {
if (runCommand("playlist-prev", check_result = false) == 0) {
onEvent(SpMsPlayerEvent.ItemTransition(current_item_index))
return true
}
return false
}
}

override fun setRepeatMode(repeat_mode: SpMsPlayerRepeatMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dev.toastbits.spms.player
import dev.toastbits.spms.socketapi.shared.SpMsPlayerEvent
import dev.toastbits.spms.socketapi.shared.SpMsPlayerRepeatMode
import dev.toastbits.spms.socketapi.shared.SpMsPlayerState
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

data class PlayerStreamInfo(
val url: String,
Expand Down Expand Up @@ -32,7 +34,7 @@ interface Player {
fun seekToTime(position_ms: Long)
fun seekToItem(index: Int, position_ms: Long = 0)
fun seekToNext(): Boolean
fun seekToPrevious(): Boolean
fun seekToPrevious(repeat_threshold: Duration? = null): Boolean
fun setRepeatMode(repeat_mode: SpMsPlayerRepeatMode)

fun getItem(): String?
Expand All @@ -45,3 +47,10 @@ interface Player {

fun setVolume(value: Double)
}

fun Player.shouldRepeatOnSeekToPrevious(repeat_threshold: Duration?): Boolean =
repeat_threshold != null
&& (
repeat_threshold == Duration.ZERO
|| current_position_ms.milliseconds >= repeat_threshold
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dev.toastbits.spms.socketapi.shared.SpMsPlayerState
import dev.toastbits.spms.player.*
import dev.toastbits.spms.ReentrantLock
import kotlin.time.*
import kotlin.time.Duration

abstract class HeadlessPlayer(private val enable_logging: Boolean = true): Player {
protected abstract fun getCachedItemDuration(item_id: String): Duration?
Expand Down Expand Up @@ -260,18 +261,23 @@ abstract class HeadlessPlayer(private val enable_logging: Boolean = true): Playe
}
}

override fun seekToPrevious(): Boolean {
override fun seekToPrevious(repeat_threshold: Duration?): Boolean {
withLock {
log("seekToPrevious()")
if (current_item_index == 0) {

if (shouldRepeatOnSeekToPrevious(repeat_threshold)) {
seekToTime(0)
return true
}
else if (current_item_index == 0) {
return false
}
else {
performSeekToItem(current_item_index - 1)
onItemTransition(current_item_index)

performSeekToItem(current_item_index - 1)

onItemTransition(current_item_index)

return true
return true
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ sealed class ServerAction(

companion object {
private val ALL: List<ServerAction> = listOf(
ServerActionGetStatus(),
ServerActionGetClients(),
ServerActionGetProperty(),

ServerActionPause(),
ServerActionPlay(),
ServerActionPlayPause(),

ServerActionSeekToItem(),
ServerActionSeekToNext(),
ServerActionSeekToPrevious(),
ServerActionSeekToTime(),
ServerActionSetRepeatMode(),

ServerActionAddItem(),
ServerActionMoveItem(),
ServerActionRemoveItem(),
ServerActionClearQueue(),

ServerActionReadyToPlay()
ServerActionGetStatus,
ServerActionGetClients,
ServerActionGetProperty,

ServerActionPause,
ServerActionPlay,
ServerActionPlayPause,

ServerActionSeekToItem,
ServerActionSeekToNext,
ServerActionSeekToPrevious,
ServerActionSeekToTime,
ServerActionSetRepeatMode,

ServerActionAddItem,
ServerActionMoveItem,
ServerActionRemoveItem,
ServerActionClearQueue,

ServerActionReadyToPlay
)

fun getAll(): List<ServerAction> = ALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonPrimitive
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsClientID
import dev.toastbits.spms.player.Player

class ServerActionAddItem: ServerAction(
object ServerActionAddItem: ServerAction(
identifier = "addItem",
name = { server_actions.add_item_name },
help = { server_actions.add_item_help },
Expand All @@ -29,7 +30,12 @@ class ServerActionAddItem: ServerAction(
val item_id: String = context.getParameterValue("item_id")!!.jsonPrimitive.content
val index: Int = context.getParameterValue("index")?.jsonPrimitive?.int ?: -1

server.player.addItem(item_id, index)
execute(server.player, item_id, index)

return null
}

fun execute(player: Player, item_id: String, index: Int) {
player.addItem(item_id, index)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package dev.toastbits.spms.socketapi.server
import kotlinx.serialization.json.JsonElement
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsClientID
import dev.toastbits.spms.player.Player

class ServerActionClearQueue: ServerAction(
object ServerActionClearQueue: ServerAction(
identifier = "clearQueue",
name = { server_actions.clear_queue_name },
help = { server_actions.clear_queue_help },
parameters = emptyList()
) {
override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? {
server.player.clearQueue()
execute(server.player)
return null
}

fun execute(player: Player) {
player.clearQueue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.encodeToJsonElement
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsClientID
import dev.toastbits.spms.socketapi.shared.SpMsClientInfo

class ServerActionGetClients: ServerAction(
object ServerActionGetClients: ServerAction(
identifier = "clients",
name = { server_actions.clients_name },
help = { server_actions.clients_help },
parameters = emptyList()
) {
override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement {
val clients = server.getClients(client)
val clients: List<SpMsClientInfo> = execute(server, client)
return Json.encodeToJsonElement(clients)
}

fun execute(server: SpMs, client: SpMsClientID): List<SpMsClientInfo> {
return server.getClients(client)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.serialization.json.JsonElement
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsClientID

class ServerActionGetProperty: ServerAction(
object ServerActionGetProperty: ServerAction(
identifier = "getProperty",
name = { "TODO" },
help = { "TODO" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ import dev.toastbits.spms.socketapi.shared.SpMsPlayerState
import dev.toastbits.spms.getCacheDir
import dev.toastbits.spms.localisation.SpMsLocalisation
import dev.toastbits.spms.PLATFORM
import dev.toastbits.spms.player.Player
import kotlin.time.Duration

class ServerActionGetStatus: ServerAction(
object ServerActionGetStatus: ServerAction(
identifier = "status",
name = { server_actions.status_name },
help = { server_actions.status_help },
parameters = emptyList()
) {
override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement {
return Json.encodeToJsonElement(server.player.getCurrentStateJson())
return execute(server.player)
}

fun execute(player: Player): JsonElement {
return Json.encodeToJsonElement(player.getCurrentStateJson())
}

override fun formatResult(result: JsonElement, localisation: SpMsLocalisation): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonPrimitive
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsClientID
import dev.toastbits.spms.player.Player

class ServerActionMoveItem: ServerAction(
object ServerActionMoveItem: ServerAction(
identifier = "moveItem",
name = { server_actions.move_item_name },
help = { server_actions.move_item_help },
Expand All @@ -29,7 +30,12 @@ class ServerActionMoveItem: ServerAction(
val from: Int = context.getParameterValue("from")!!.jsonPrimitive.int
val to: Int = context.getParameterValue("to")!!.jsonPrimitive.int

server.player.moveItem(from, to)
execute(server.player, from, to)

return null
}

fun execute(player: Player, from: Int, to: Int) {
player.moveItem(from, to)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package dev.toastbits.spms.socketapi.server
import kotlinx.serialization.json.JsonElement
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsClientID
import dev.toastbits.spms.player.Player

class ServerActionPause: ServerAction(
object ServerActionPause: ServerAction(
name = { server_actions.pause_name },
identifier = "pause",
help = { server_actions.pause_help },
parameters = emptyList()
) {
override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? {
server.player.pause()
execute(server.player)
return null
}

fun execute(player: Player) {
player.pause()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package dev.toastbits.spms.socketapi.server
import kotlinx.serialization.json.JsonElement
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsClientID
import dev.toastbits.spms.player.Player

class ServerActionPlay: ServerAction(
object ServerActionPlay: ServerAction(
identifier = "play",
name = { server_actions.play_name },
help = { server_actions.play_help },
parameters = emptyList()
) {
override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? {
server.player.play()
execute(server.player)
return null
}

fun execute(player: Player) {
player.play()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package dev.toastbits.spms.socketapi.server
import kotlinx.serialization.json.JsonElement
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsClientID
import dev.toastbits.spms.player.Player

class ServerActionPlayPause: ServerAction(
object ServerActionPlayPause: ServerAction(
identifier = "playPause",
name = { server_actions.play_pause_name },
help = { server_actions.play_pause_help },
parameters = emptyList()
) {
override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? {
server.player.playPause()
execute(server.player)
return null
}

fun execute(player: Player) {
player.playPause()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import kotlinx.serialization.json.long
import dev.toastbits.spms.server.SpMs
import dev.toastbits.spms.socketapi.shared.SpMsClientID
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

class ServerActionReadyToPlay: ServerAction(
object ServerActionReadyToPlay: ServerAction(
identifier = "readyToPlay",
name = { server_actions.ready_to_play_name },
help = { server_actions.ready_to_play_help },
Expand All @@ -35,14 +36,21 @@ class ServerActionReadyToPlay: ServerAction(
)
) {
override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? {
val item_index: Int = context.getParameterValue("item_index")!!.jsonPrimitive.int
val item_id: String = context.getParameterValue("item_id")!!.jsonPrimitive.content
val item_duration: Duration = context.getParameterValue("item_duration_ms")!!.jsonPrimitive.long.milliseconds

execute(server, client, item_index, item_id, item_duration)

return null
}

fun execute(server: SpMs, client: SpMsClientID, item_index: Int, item_id: String, item_duration: Duration) {
server.onClientReadyToPlay(
client,
context.getParameterValue("item_index")!!.jsonPrimitive.int,
context.getParameterValue("item_id")!!.jsonPrimitive.content,
with (Duration) {
context.getParameterValue("item_duration_ms")!!.jsonPrimitive.long.milliseconds
}
item_index,
item_id,
item_duration
)
return null
}
}
Loading

0 comments on commit 6d8ea1b

Please sign in to comment.