commit 07/11/2025
This commit is contained in:
@@ -3,7 +3,6 @@ package web
|
||||
import StreamerOutputs
|
||||
import barix.BarixConnection
|
||||
import codes.Somecodes
|
||||
import codes.Somecodes.Companion.Generate_WAV_Header
|
||||
import codes.Somecodes.Companion.GetSensorsInfo
|
||||
import codes.Somecodes.Companion.GetUptime
|
||||
import codes.Somecodes.Companion.ListAudioFiles
|
||||
@@ -44,10 +43,9 @@ import java.nio.file.Files
|
||||
import java.time.LocalDateTime
|
||||
import codes.configKeys
|
||||
import database.QueueTable
|
||||
import io.javalin.websocket.WsContext
|
||||
import org.tinylog.Logger
|
||||
import java.io.File
|
||||
import java.io.PipedInputStream
|
||||
import java.io.PipedOutputStream
|
||||
import java.nio.file.Path
|
||||
import java.util.UUID
|
||||
|
||||
@@ -58,6 +56,7 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
lateinit var app: Javalin
|
||||
lateinit var semiauto: Javalin
|
||||
val objectmapper = jacksonObjectMapper()
|
||||
val WsContextMap = mutableMapOf<String, WsContext>()
|
||||
|
||||
private fun SendReply(context: WsMessageContext, command: String, value: String) {
|
||||
try {
|
||||
@@ -246,43 +245,47 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
before { CheckUsers(it) }
|
||||
}
|
||||
path("api") {
|
||||
//TODO gak jalan
|
||||
//TODO https://stackoverflow.com/questions/70002015/streaming-into-audio-element
|
||||
path("LiveAudio") {
|
||||
ws("/ws/{uuid}"){ws ->
|
||||
ws.onConnect {
|
||||
ctx ->
|
||||
val uuid = ctx.pathParam("uuid")
|
||||
val cookieresult = ctx.cookie("client-stream-id")
|
||||
println("Ws connected with uuid=$uuid, cookie=$cookieresult")
|
||||
WsContextMap[uuid] = ctx
|
||||
}
|
||||
|
||||
ws.onClose {
|
||||
ctx ->
|
||||
val uuid = ctx.pathParam("uuid")
|
||||
println("Ws close on uuid=$uuid")
|
||||
WsContextMap.remove(uuid)
|
||||
}
|
||||
ws.onError {
|
||||
ctx ->
|
||||
val uuid = ctx.pathParam("uuid")
|
||||
println("Ws error on uuid=$uuid")
|
||||
WsContextMap.remove(uuid)
|
||||
}
|
||||
}
|
||||
|
||||
get("Open/{broadcastzone}") { ctx ->
|
||||
val param = ctx.pathParam("broadcastzone")
|
||||
println("LiveAudio Open for zone $param")
|
||||
println("LiveAudio Open for zone $param from ${ctx.host()}" )
|
||||
if (param.isNotEmpty()) {
|
||||
val bc = Get_Barix_Connection_by_ZoneName(param)
|
||||
if (bc != null) {
|
||||
ctx.contentType("audio/mpeg")
|
||||
ctx.header("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
ctx.header("Pragma", "no-cache")
|
||||
ctx.header("Expires", "0")
|
||||
ctx.header("Transfer-Encoding", "chunked")
|
||||
ctx.header("Connection", "keep-alive")
|
||||
|
||||
val key = ctx.cookie("client-id") ?: UUID.randomUUID().toString()
|
||||
.also { ctx.cookie("client-id", it) }
|
||||
val key = ctx.cookie("client-stream-id") ?: UUID.randomUUID().toString()
|
||||
.also { ctx.cookie("client-stream-id", it) }
|
||||
|
||||
val responseStream = ctx.res().outputStream
|
||||
ctx.async {
|
||||
|
||||
val pipeIN = PipedInputStream(8192)
|
||||
val pipeOUT = PipedOutputStream(pipeIN)
|
||||
bc.AddPipeOut(key, pipeOUT)
|
||||
println("LiveAudio Open for zone $param SUCCESS")
|
||||
try{
|
||||
pipeIN.transferTo(responseStream)
|
||||
responseStream.flush()
|
||||
} catch(e: Exception){
|
||||
|
||||
} finally{
|
||||
println("Cleaning up stream for $key")
|
||||
bc.RemovePipeOut(key)
|
||||
pipeIN.close()
|
||||
}
|
||||
bc.Add_Mp3_Consumer(key){ mp3data ->
|
||||
WsContextMap[key]?.send(mp3data)
|
||||
println("Send ${mp3data.size} to $key")
|
||||
}
|
||||
|
||||
ResultMessageString(ctx, 200, key)
|
||||
} else ctx.status(400)
|
||||
.result(objectmapper.writeValueAsString(resultMessage("Broadcastzone not found")))
|
||||
} else ctx.status(400)
|
||||
@@ -292,14 +295,14 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
get("Close/{broadcastzone}") { ctx ->
|
||||
val param = ctx.pathParam("broadcastzone")
|
||||
println("LiveAudio Close for zone $param")
|
||||
val key = ctx.cookie("client-id")
|
||||
val key = ctx.cookie("client-stream-id")
|
||||
if (key != null && key.isNotEmpty()) {
|
||||
if (param.isNotEmpty()) {
|
||||
val bc = Get_Barix_Connection_by_ZoneName(param)
|
||||
if (bc != null) {
|
||||
|
||||
bc.RemovePipeOut(key)
|
||||
ctx.result(objectmapper.writeValueAsString(resultMessage("OK")))
|
||||
bc.Remove_Mp3_Consumer(key)
|
||||
WsContextMap.remove(key)
|
||||
ResultMessageString(ctx, 200, "OK")
|
||||
println("LiveAudio Close for zone $param SUCCESS")
|
||||
} else ctx.status(400)
|
||||
.result(objectmapper.writeValueAsString(resultMessage("Broadcastzone not found")))
|
||||
|
||||
Reference in New Issue
Block a user