Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
Add DISABLE_MPV build flag
Browse files Browse the repository at this point in the history
  • Loading branch information
toasterofbread committed May 16, 2024
1 parent a9f7ddc commit 81f5353
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 95 deletions.
80 changes: 66 additions & 14 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.jetbrains.kotlin.gradle.targets.js.binaryen.BinaryenRootPlugin.Compan
import org.jetbrains.kotlin.gradle.tasks.CompileUsingKotlinDaemon
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile

val FLAG_LINK_STATIC: String = "linkStatic"
val GENERATED_FILE_PREFIX: String = "// Generated on build in build.gradle.kts\n"

plugins {
Expand All @@ -24,11 +23,20 @@ repositories {
maven("https://jitpack.io")
}

val platform_specific_files: List<String> = listOf(
"cinterop/indicator/TrayIndicatorImpl.kt",
"spms/Platform.kt",
"spms/server/SpMsMediaSession.kt"
)
val PLATFORM_SPECIFIC_FILES: List<String> =
listOf(
"cinterop/indicator/TrayIndicatorImpl.kt",
"spms/Platform.kt",
"spms/server/SpMsMediaSession.kt"
)

enum class BuildFlag {
DISABLE_MPV,
LINK_STATIC;

fun isEnabled(project: Project): Boolean =
project.hasProperty(name)
}

enum class Arch {
X86_64, ARM64;
Expand Down Expand Up @@ -146,6 +154,18 @@ enum class CinteropLibraries {
else -> true
}

fun includeInProject(project: Project): Boolean =
when (this) {
LIBMPV -> !BuildFlag.DISABLE_MPV.isEnabled(project)
else -> true
}

fun getDependentFiles(): List<String> =
when (this) {
LIBMPV -> listOf("cinterop/mpv/LibMpvClient.kt", "cinterop/mpv/MpvClientEventLoop.kt")
else -> emptyList()
}

fun getBinaryDependencies(platform: Platform): List<String> =
when (this) {
LIBMPV ->
Expand Down Expand Up @@ -323,14 +343,14 @@ fun KotlinMultiplatformExtension.configureKotlinTarget(platform: Platform) {
Platform.OSX_ARM -> macosArm64(platform.identifier)
}

val static: Boolean = project.hasProperty(FLAG_LINK_STATIC)
val static: Boolean = BuildFlag.LINK_STATIC.isEnabled(project)
val deps_directory: File = platform.getNativeDependenciesDir(project)

native_target.apply {
compilations.getByName("main") {
cinterops {
for (library in CinteropLibraries.values()) {
if (!library.includeOnPlatform(platform)) {
if (!library.includeOnPlatform(platform) || !library.includeInProject(project)) {
continue
}

Expand All @@ -354,7 +374,7 @@ fun KotlinMultiplatformExtension.configureKotlinTarget(platform: Platform) {
}

for (library in CinteropLibraries.values()) {
if (!library.includeOnPlatform(platform)) {
if (!library.includeOnPlatform(platform) || !library.includeInProject(project)) {
continue
}
library.configureExecutable(platform, static, this, deps_directory)
Expand Down Expand Up @@ -436,20 +456,51 @@ tasks.register("bundleIcon") {
}
}


for (platform in Platform.supported) {
val print_target_task by tasks.register("printTarget${platform.identifier}") {
doLast {
println("Building spmp-server for target $platform")
}
}

val configure_platform_files_task by tasks.register("configurePlatformFiles${platform.identifier}") {
fun String.getFile(suffix: String? = null): File =
project.file("src/commonMain/kotlin/" + if (suffix == null) this.replace(".kt", ".gen.kt") else (this + suffix))

val configure_library_dependent_files_task by tasks.register("configureLibraryDependentFiles${platform.identifier}") {
outputs.upToDateWhen { false }

fun String.getFile(suffix: String? = null): File =
project.file("src/commonMain/kotlin/" + if (suffix == null) this.replace(".kt", ".gen.kt") else (this + suffix))
for (library in CinteropLibraries.values()) {
for (path in library.getDependentFiles()) {
outputs.file(path.getFile())
inputs.file(path.getFile(".enabled"))
inputs.file(path.getFile(".disabled"))
}
}

doLast {
for (library in CinteropLibraries.values()) {
val enabled: Boolean = library.includeOnPlatform(platform) && library.includeInProject(project)

for (path in library.getDependentFiles()) {
val out_file: File = path.getFile()
val in_file: File = path.getFile(if (enabled) ".enabled" else ".disabled")

out_file.writer().use { writer ->
writer.write(GENERATED_FILE_PREFIX)

in_file.reader().use { reader ->
reader.transferTo(writer)
}
}
}
}
}
}
val configure_platform_files_task by tasks.register("configurePlatformFiles${platform.identifier}") {
outputs.upToDateWhen { false }

for (path in platform_specific_files) {
for (path in PLATFORM_SPECIFIC_FILES) {
outputs.file(path.getFile())

val input_files: List<File> =
Expand All @@ -462,7 +513,7 @@ for (platform in Platform.supported) {
}

doLast {
for (path in platform_specific_files) {
for (path in PLATFORM_SPECIFIC_FILES) {
val out_file: File = path.getFile()

for (platform_file in listOf(
Expand Down Expand Up @@ -494,6 +545,7 @@ for (platform in Platform.supported) {
dependsOn(print_target_task)
dependsOn("bundleIcon")
dependsOn(configure_platform_files_task)
dependsOn(configure_library_dependent_files_task)
}

val check_dependencies_task by tasks.register("checkDependencies${platform.identifier}") {
Expand Down
35 changes: 35 additions & 0 deletions src/commonMain/kotlin/cinterop/mpv/LibMpvClient.kt.disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cinterop.mpv

import spms.player.Player
import kotlinx.cinterop.CPrimitiveVar
import kotlinx.cinterop.MemScope
import kotlin.reflect.KClass

typealias MpvFormat = Unit

class mpv_event {
val event_id: Int get() = throw NotImplementedError()
}

abstract class LibMpvClient(val headless: Boolean = true): Player {
companion object {
fun isAvailable(): Boolean = false
}

init {
throw NotImplementedError()
}

override fun release() { throw NotImplementedError() }

protected fun runCommand(name: String, vararg args: Any?, check_result: Boolean = true): Int = throw NotImplementedError()
protected inline fun <reified V> getProperty(name: String): V = throw NotImplementedError()
protected inline fun <reified T: Any> setProperty(name: String, value: T) { throw NotImplementedError() }
protected fun observeProperty(name: String, cls: KClass<*>) { throw NotImplementedError() }
protected inline fun <reified T> MemScope.getPointerOf(v: T? = null): CPrimitiveVar = throw NotImplementedError()
protected fun getFormatOf(cls: KClass<*>): MpvFormat = throw NotImplementedError()
protected fun waitForEvent(): mpv_event? = throw NotImplementedError()
protected fun requestLogMessages() { throw NotImplementedError() }
protected fun addHook(name: String, priority: Int = 0) { throw NotImplementedError() }
protected fun continueHook(id: ULong) { throw NotImplementedError() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import libmpv.mpv_format as MpvFormat

@OptIn(ExperimentalForeignApi::class)
abstract class LibMpvClient(val headless: Boolean = true): Player {
companion object {
fun isAvailable(): Boolean = true
}

protected val ctx: CPointer<MpvHandle>

init {
ctx = mpv_create_client(null, null)
?: throw NullPointerException("Creating MPV client failed")
?: throw NullPointerException("Creating mpv client failed")

memScoped {
val vid: BooleanVar = alloc()
Expand All @@ -31,7 +35,7 @@ abstract class LibMpvClient(val headless: Boolean = true): Player {
}

val init_result: Int = mpv_initialize(ctx)
check(init_result == 0) { "Initialising MPV client failed ($init_result)" }
check(init_result == 0) { "Initialising mpv client failed ($init_result)" }
}

override fun release() {
Expand Down Expand Up @@ -124,7 +128,7 @@ abstract class LibMpvClient(val headless: Boolean = true): Player {
else -> throw NotImplementedError(cls.toString())
}

protected fun waitForEvent(): mpv_event? =
internal fun waitForEvent(): mpv_event? =
mpv_wait_event(ctx, -1.0)?.pointed

protected fun requestLogMessages() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cinterop.mpv

internal suspend fun MpvClientImpl.eventLoop() {
throw NotImplementedError()
}
70 changes: 70 additions & 0 deletions src/commonMain/kotlin/cinterop/mpv/MpvClientEventLoop.kt.enabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cinterop.mpv

import libmpv.*
import kotlinx.coroutines.*
import kotlinx.serialization.json.JsonPrimitive
import spms.server.SpMs
import spms.socketapi.shared.SpMsPlayerEvent
import cinterop.utils.safeToKString
import kotlinx.cinterop.*

internal suspend fun MpvClientImpl.eventLoop() = withContext(Dispatchers.IO) {
while (true) {
val event: mpv_event? = waitForEvent()

when (event?.event_id) {
MPV_EVENT_START_FILE -> {
val data: mpv_event_start_file = event.data.pointedAs()
if (data.playlist_entry_id.toInt() != current_item_playlist_id) {
continue
}

onEvent(SpMsPlayerEvent.ItemTransition(current_item_index), clientless = true)
}
MPV_EVENT_PLAYBACK_RESTART -> {
onEvent(SpMsPlayerEvent.PropertyChanged("state", JsonPrimitive(state.ordinal)), clientless = true)
onEvent(SpMsPlayerEvent.PropertyChanged("duration_ms", JsonPrimitive(duration_ms)), clientless = true)
onEvent(SpMsPlayerEvent.SeekedToTime(current_position_ms), clientless = true)
}
MPV_EVENT_END_FILE -> {
onEvent(SpMsPlayerEvent.PropertyChanged("state", JsonPrimitive(state.ordinal)), clientless = true)
}
MPV_EVENT_FILE_LOADED -> {
onEvent(SpMsPlayerEvent.ReadyToPlay(), clientless = true)

song_initial_seek_position_ms?.also { position_ms ->
seekToTime(position_ms)
song_initial_seek_position_ms = null
}
}
MPV_EVENT_SHUTDOWN -> {
onShutdown()
}
MPV_EVENT_PROPERTY_CHANGE -> {
val data: mpv_event_property = event.data.pointedAs()

when (data.name?.safeToKString()) {
"core-idle" -> {
val playing: Boolean = !data.data.pointedAs<BooleanVar>().value
onEvent(SpMsPlayerEvent.PropertyChanged("is_playing", JsonPrimitive(playing)), clientless = true)
}
}
}

MPV_EVENT_HOOK -> {
val data: mpv_event_hook = event.data.pointedAs()
launch {
onMpvHook(data.name?.safeToKString(), data.id)
}
}

MPV_EVENT_LOG_MESSAGE -> {
if (SpMs.logging_enabled) {
val message: mpv_event_log_message = event.data.pointedAs()
SpMs.log("From mpv (${message.prefix?.safeToKString()}): ${message.text?.safeToKString()}")
}
}
}
}
}

Loading

0 comments on commit 81f5353

Please sign in to comment.