ExternalPlayer.desktop.kt
composeApp/src/desktopMain/kotlin/eu/friedcutlet/yokan/services/media/player/ExternalPlayer.desktop.kt
1package eu.friedcutlet.yokan.services.media.player23import eu.friedcutlet.yokan.services.logging.AppLogger4import java.io.File5import java.util.Locale67/** Ordered launchers per OS: mpv first (takes headers), then other players, then the default handler. */8private fun desktopLaunchers(url: String, headers: Map<String, String>): List<Pair<ExternalPlayerChoice, List<String>>> {9 val os = System.getProperty("os.name").orEmpty().lowercase(Locale.US)10 val mpv = ExternalPlayerChoice("mpv", "mpv") to (listOf("mpv") + mpvHeaderArgs(headers) + url)11 return when {12 "mac" in os || "darwin" in os -> listOf(13 mpv,14 ExternalPlayerChoice("iina", "IINA") to listOf("/Applications/IINA.app/Contents/MacOS/iina-cli", url),15 ExternalPlayerChoice("vlc", "VLC") to listOf("/Applications/VLC.app/Contents/MacOS/VLC", url),16 ExternalPlayerChoice("system", "System default") to listOf("open", url),17 )18 "win" in os -> listOf(19 mpv,20 ExternalPlayerChoice("mpv.exe", "mpv.exe") to (listOf("mpv.exe") + mpvHeaderArgs(headers) + url),21 ExternalPlayerChoice("system", "System default") to listOf("cmd", "/c", "start", "", url),22 )23 else -> listOf( // assume Linux / *BSD24 mpv,25 ExternalPlayerChoice("vlc", "VLC") to listOf("vlc", url),26 ExternalPlayerChoice("system", "System default") to listOf("xdg-open", url),27 )28 }29}3031/** Players whose executable exists (absolute path or on PATH), plus the system default handler. */32actual fun externalPlayerChoices(): List<ExternalPlayerChoice> =33 desktopLaunchers("", emptyMap()).filter { (choice, command) -> choice.id == "system" || isExecutable(command.first()) }34 .map { it.first }3536private fun isExecutable(command: String): Boolean {37 if (File(command).isAbsolute) return File(command).canExecute()38 return System.getenv("PATH").orEmpty().split(File.pathSeparator).any { File(it, command).canExecute() }39}4041actual fun launchExternalPlayer(url: String, headers: Map<String, String>, playerId: String): Boolean {42 if (url.isBlank()) return false43 val candidates = desktopLaunchers(url, headers).filter { playerId.isBlank() || it.first.id == playerId }44 for ((_, cmd) in candidates) {45 val started = runCatching {46 // Nobody reads the player's console output; an undrained pipe fills47 // up and stalls the player, so discard it and close its stdin.48 ProcessBuilder(cmd)49 .redirectErrorStream(true)50 .redirectOutput(ProcessBuilder.Redirect.DISCARD)51 .start()52 .outputStream53 .close()54 AppLogger.info("[player] handed to external launcher ${cmd.first()}")55 true56 }.getOrDefault(false)57 if (started) return true58 }59 AppLogger.warn("[player] no external launcher succeeded for url")60 return false61}6263private fun mpvHeaderArgs(headers: Map<String, String>): List<String> {64 if (headers.isEmpty()) return emptyList()65 // mpv accepts `--http-header-fields=Header1: v1,Header2: v2`. Commas in66 // header values would break the parser, but standard streaming headers67 // (User-Agent, Referer, Origin, Cookie) never legitimately contain them.68 val joined = headers.entries.joinToString(",") { "${it.key}: ${it.value}" }69 return listOf("--http-header-fields=$joined")70}71