commit 21/11/2025
This commit is contained in:
@@ -231,7 +231,8 @@ fun main() {
|
||||
db.Add_Log("AAS"," Application started")
|
||||
|
||||
// shutdown hook
|
||||
Runtime.getRuntime().addShutdownHook(Thread {
|
||||
Runtime.getRuntime().addShutdownHook(Thread ({
|
||||
|
||||
db.Add_Log("AAS"," Application stopping")
|
||||
Logger.info { "Shutdown hook called, stopping services..." }
|
||||
barixserver.StopTcpCommand()
|
||||
@@ -240,11 +241,12 @@ fun main() {
|
||||
web.Stop()
|
||||
udpreceiver.Stop()
|
||||
tcpreceiver.Stop()
|
||||
StreamerOutputs.values.forEach { it.close() }
|
||||
audioPlayer.Close()
|
||||
db.close()
|
||||
Logger.info { "All services stopped, exiting application." }
|
||||
ProviderRegistry.getLoggingProvider().shutdown()
|
||||
})
|
||||
},"ShutdownHook") )
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -48,9 +48,10 @@ class Mp3Encoder(val samplingrate: Int=44100, val channels: Int=1) {
|
||||
push_handle = bass.BASS_StreamCreate(samplingrate, channels, Bass.BASS_STREAM_DECODE, Pointer(-1), null)
|
||||
if (push_handle!=0){
|
||||
Logger.info{"MP3 Encoder initialized with sampling rate $samplingrate Hz and $channels channel(s)" }
|
||||
//val options = "lame -b 128 --cbr --write-xing 0 --nohist --noid3v2 - -"
|
||||
val flag = BassEnc.BASS_ENCODE_AUTOFREE or BassEnc.BASS_ENCODE_NOHEAD
|
||||
mp3_handle = bassencmp3.BASS_Encode_MP3_Start(push_handle, null, flag,proc, null)
|
||||
val options = "lame -b 128 --cbr --write-xing 0 --nohist --noid3v2 - -"
|
||||
//val flag = BassEnc.BASS_ENCODE_AUTOFREE or BassEnc.BASS_ENCODE_NOHEAD
|
||||
val flag = BassEnc.BASS_ENCODE_AUTOFREE
|
||||
mp3_handle = bassencmp3.BASS_Encode_MP3_Start(push_handle, options, flag,proc, null)
|
||||
|
||||
if (mp3_handle!=0){
|
||||
callback = cb
|
||||
@@ -100,7 +101,6 @@ class Mp3Encoder(val samplingrate: Int=44100, val channels: Int=1) {
|
||||
Logger.error{"Failed to push data to MP3 Encoder. BASS error code: $err" }
|
||||
return 0
|
||||
}
|
||||
//println("MP3 Encoder: Pushed $written bytes of PCM data.")
|
||||
return written
|
||||
}
|
||||
|
||||
@@ -122,6 +122,5 @@ class Mp3Encoder(val samplingrate: Int=44100, val channels: Int=1) {
|
||||
// auto close by BASS_ENCODE_AUTOFREE
|
||||
mp3_handle = 0
|
||||
callback = null
|
||||
println("MP3 Encoder: Stopped.")
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import java.nio.ByteBuffer
|
||||
import java.util.function.Consumer
|
||||
|
||||
@Suppress("unused")
|
||||
class BarixConnection(val index: UInt, var channel: String, val ipaddress: String, val port: Int = 5002) {
|
||||
class BarixConnection(val index: UInt, var channel: String, val ipaddress: String, val port: Int = 5002) : AutoCloseable {
|
||||
private var _bR: Int = 0
|
||||
private var _sd: Int = 0
|
||||
private var _vu: Int = 0
|
||||
@@ -26,6 +26,13 @@ class BarixConnection(val index: UInt, var channel: String, val ipaddress: Strin
|
||||
private var _tcp: Socket? = null
|
||||
private val mp3encoder = Mp3Encoder()
|
||||
private val mp3Consumer = mutableMapOf<String, Consumer<ByteArray>>()
|
||||
private val udp = DatagramSocket()
|
||||
|
||||
init {
|
||||
mp3encoder.Start { data ->
|
||||
mp3Consumer.values.forEach { it.accept(data) }
|
||||
}
|
||||
}
|
||||
|
||||
fun Add_Mp3_Consumer(key: String, cb: Consumer<ByteArray>) {
|
||||
mp3Consumer[key] = cb
|
||||
@@ -118,33 +125,26 @@ class BarixConnection(val index: UInt, var channel: String, val ipaddress: Strin
|
||||
fun SendData(data: ByteArray, cbOK: Consumer<String>, cbFail: Consumer<String>) {
|
||||
if (data.isNotEmpty()) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
DatagramSocket().use{ udp ->
|
||||
val bb = ByteBuffer.wrap(data)
|
||||
if (!mp3encoder.isStarted()) {
|
||||
mp3encoder.Start { data ->
|
||||
mp3Consumer.values.forEach { it.accept(data) }
|
||||
val bb = ByteBuffer.wrap(data)
|
||||
|
||||
while(bb.hasRemaining()){
|
||||
try {
|
||||
val chunk = ByteArray(if (bb.remaining() > maxUDPsize) maxUDPsize else bb.remaining())
|
||||
bb.get(chunk)
|
||||
while(bufferRemain<chunk.size){
|
||||
delay(10)
|
||||
}
|
||||
}
|
||||
while(bb.hasRemaining()){
|
||||
try {
|
||||
val chunk = ByteArray(if (bb.remaining() > maxUDPsize) maxUDPsize else bb.remaining())
|
||||
bb.get(chunk)
|
||||
while(bufferRemain<chunk.size){
|
||||
delay(10)
|
||||
}
|
||||
udp.send(DatagramPacket(chunk, chunk.size, inet))
|
||||
mp3encoder.PushData(chunk)
|
||||
delay(2)
|
||||
udp.send(DatagramPacket(chunk, chunk.size, inet))
|
||||
mp3encoder.PushData(chunk)
|
||||
delay(2)
|
||||
|
||||
|
||||
} catch (e: Exception) {
|
||||
cbFail.accept("SendData to $ipaddress failed, message: ${e.message}")
|
||||
return@launch
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
cbFail.accept("SendData to $ipaddress failed, message: ${e.message}")
|
||||
return@launch
|
||||
}
|
||||
mp3encoder.Stop()
|
||||
cbOK.accept("SendData to $channel ($ipaddress) succeeded, ${data.size} bytes sent")
|
||||
}
|
||||
cbOK.accept("SendData to $channel ($ipaddress) succeeded, ${data.size} bytes sent")
|
||||
}
|
||||
|
||||
} else cbFail.accept("SendData to $ipaddress failed, data is empty")
|
||||
@@ -248,4 +248,18 @@ class BarixConnection(val index: UInt, var channel: String, val ipaddress: Strin
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
try{
|
||||
udp.close()
|
||||
} catch (_ : Exception){
|
||||
Logger.error { "Failed to close udp" }
|
||||
}
|
||||
try{
|
||||
_tcp?.close()
|
||||
} catch (_ : Exception){
|
||||
Logger.error { "Failed to close tcp socket to $ipaddress" }
|
||||
}
|
||||
mp3encoder.Stop()
|
||||
}
|
||||
}
|
||||
@@ -94,11 +94,11 @@ class TCP_Android_Command_Server {
|
||||
}
|
||||
|
||||
} catch (ex: Exception) {
|
||||
logcb.accept("Failed accepting TCP Socket, Message : ${ex.message}")
|
||||
logcb.accept("Android TCP Server Failed accepting TCP Socket, Message : ${ex.message}")
|
||||
}
|
||||
|
||||
}
|
||||
logcb.accept("TCP server stopped")
|
||||
logcb.accept("Android TCP Command server stopped")
|
||||
}
|
||||
return true
|
||||
} catch (e: Exception) {
|
||||
|
||||
6
src/web/LiveListenData.kt
Normal file
6
src/web/LiveListenData.kt
Normal file
@@ -0,0 +1,6 @@
|
||||
package web
|
||||
|
||||
import barix.BarixConnection
|
||||
import io.javalin.websocket.WsContext
|
||||
|
||||
data class LiveListenData(val key: String, var bc : BarixConnection?, var ws : WsContext?)
|
||||
@@ -43,9 +43,10 @@ import java.nio.file.Files
|
||||
import java.time.LocalDateTime
|
||||
import codes.configKeys
|
||||
import database.QueueTable
|
||||
import io.javalin.websocket.WsContext
|
||||
import io.javalin.websocket.WsCloseStatus
|
||||
import org.tinylog.Logger
|
||||
import java.io.File
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.file.Path
|
||||
import java.util.UUID
|
||||
|
||||
@@ -56,7 +57,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>()
|
||||
val WsContextMap = mutableMapOf<String, LiveListenData>()
|
||||
|
||||
private fun SendReply(context: WsMessageContext, command: String, value: String) {
|
||||
try {
|
||||
@@ -247,79 +248,100 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
path("api") {
|
||||
//TODO https://stackoverflow.com/questions/70002015/streaming-into-audio-element
|
||||
path("LiveAudio") {
|
||||
post{
|
||||
val json : JsonNode = objectmapper.readTree(it.body())
|
||||
post{ ctx->
|
||||
val json : JsonNode = objectmapper.readTree(ctx.body())
|
||||
val broadcastzone = json.get("broadcastzone")?.asText("") ?: ""
|
||||
val command = json.get("command")?.asText("") ?: ""
|
||||
println("LiveAudio command=$command for zone $broadcastzone from ${it.host()}" )
|
||||
if (command == "Open" || command == "Close"){
|
||||
if (broadcastzone.isNotEmpty()){
|
||||
val bc = Get_Barix_Connection_by_ZoneName(broadcastzone)
|
||||
if (bc!=null){
|
||||
val key = ctx.cookie("client-stream-id")
|
||||
if (command == "Open"){
|
||||
// open command
|
||||
if (key!=null && key.isNotEmpty()){
|
||||
// ada connection sebelumnya, kemungkinan reconnect
|
||||
val prev = WsContextMap[key]
|
||||
if (prev!=null){
|
||||
prev.bc?.Remove_Mp3_Consumer(key)
|
||||
prev.ws?.closeSession(WsCloseStatus.NORMAL_CLOSURE, "Reopen Live Audio Stream")
|
||||
}
|
||||
WsContextMap.remove(key)
|
||||
ctx.cookie("client-stream-id", "")
|
||||
}
|
||||
val newkey = UUID.randomUUID().toString()
|
||||
.also { ctx.cookie("client-stream-id", it) }
|
||||
WsContextMap[newkey] = LiveListenData(newkey, bc, null)
|
||||
ResultMessageString(ctx, 200, newkey)
|
||||
|
||||
|
||||
} else {
|
||||
// close command
|
||||
if (key!=null && key.isNotEmpty()){
|
||||
// close connection
|
||||
val prev = WsContextMap[key]
|
||||
if (prev!=null){
|
||||
prev.bc?.Remove_Mp3_Consumer(key)
|
||||
prev.ws?.closeSession(WsCloseStatus.NORMAL_CLOSURE, "Close Live Audio Stream")
|
||||
}
|
||||
WsContextMap.remove(key)
|
||||
ctx.cookie("client-stream-id", "")
|
||||
ResultMessageString(ctx, 200, "OK")
|
||||
} else ResultMessageString(ctx, 400, "No client-id cookie found")
|
||||
}
|
||||
} else ResultMessageString(ctx, 400, "Broadcastzone not found")
|
||||
} else ResultMessageString(ctx, 400, "Invalid broadcastzone")
|
||||
} else ResultMessageString(ctx, 400, "Invalid command")
|
||||
|
||||
}
|
||||
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 from ${ctx.host()}" )
|
||||
if (param.isNotEmpty()) {
|
||||
val bc = Get_Barix_Connection_by_ZoneName(param)
|
||||
if (bc != null) {
|
||||
|
||||
val key = ctx.cookie("client-stream-id") ?: UUID.randomUUID().toString()
|
||||
.also { ctx.cookie("client-stream-id", it) }
|
||||
|
||||
bc.Add_Mp3_Consumer(key){ mp3data ->
|
||||
WsContextMap[key]?.send(mp3data)
|
||||
println("Send ${mp3data.size} to $key")
|
||||
ws("ws"){ wscontext ->
|
||||
wscontext.onConnect {
|
||||
val key = it.cookie("client-stream-id")
|
||||
if (key!=null && key.isNotEmpty()){
|
||||
val lld = WsContextMap[key]
|
||||
if (lld!=null){
|
||||
it.enableAutomaticPings()
|
||||
lld.ws = it
|
||||
lld.bc?.Add_Mp3_Consumer(key){ mp3data->
|
||||
try {
|
||||
if (it.session.isOpen){
|
||||
it.send(ByteBuffer.wrap(mp3data))
|
||||
}
|
||||
} catch (e: Exception){
|
||||
Logger.error {"Error sending LiveAudio mp3 data for key $key, Message: ${e.message}"}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
it.closeSession(WsCloseStatus.POLICY_VIOLATION, "WsContextMap key not found")
|
||||
Logger.info{"LiveAudio WebSocket connection rejected, WsContextMap key not found"}
|
||||
}
|
||||
|
||||
ResultMessageString(ctx, 200, key)
|
||||
} else ctx.status(400)
|
||||
.result(objectmapper.writeValueAsString(resultMessage("Broadcastzone not found")))
|
||||
} else ctx.status(400)
|
||||
.result(objectmapper.writeValueAsString(resultMessage("Invalid broadcastzone")))
|
||||
|
||||
}
|
||||
get("Close/{broadcastzone}") { ctx ->
|
||||
val param = ctx.pathParam("broadcastzone")
|
||||
println("LiveAudio Close for zone $param")
|
||||
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.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")))
|
||||
} else ctx.status(400)
|
||||
.result(objectmapper.writeValueAsString(resultMessage("Invalid broadcastzone")))
|
||||
} else {
|
||||
ctx.status(400)
|
||||
.result(objectmapper.writeValueAsString(resultMessage("No client-id cookie found")))
|
||||
} else {
|
||||
it.closeSession(WsCloseStatus.POLICY_VIOLATION, "Invalid client-stream-id")
|
||||
Logger.info{"LiveAudio WebSocket connection rejected, invalid client-stream-id"}
|
||||
}
|
||||
}
|
||||
wscontext.onClose {
|
||||
val key = it.cookie("client-stream-id")
|
||||
if (key!=null && key.isNotEmpty()){
|
||||
val lld = WsContextMap[key]
|
||||
lld?.bc?.Remove_Mp3_Consumer(key)
|
||||
lld?.ws?.closeSession()
|
||||
lld?.bc = null
|
||||
lld?.ws= null
|
||||
WsContextMap.remove(key)
|
||||
Logger.info{"LiveAudio WebSocket closed for key $key"}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
wscontext.onError {
|
||||
val key = it.cookie("client-stream-id")
|
||||
val msg = it.error()?.message ?: ""
|
||||
Logger.info{"LiveAudio WebSocket error for key $key, Message: $msg"}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
path("VoiceType") {
|
||||
get {
|
||||
@@ -1876,7 +1898,6 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
val language = it.pathParam("language")
|
||||
val voice = it.pathParam("voice")
|
||||
val category = it.pathParam("category")
|
||||
//println("ListSoundbank called with language=$language, voice=$voice, category=$category")
|
||||
if (ValidString(language) && Language.entries.any { lang -> lang.name == language }) {
|
||||
if (ValidString(voice) && VoiceType.entries.any { vtype -> vtype.name == voice }) {
|
||||
if (ValidString(category) && Category.entries.any { cat -> cat.name == category }) {
|
||||
@@ -1889,7 +1910,6 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
// just the filename, without path
|
||||
mm.substring(mm.lastIndexOf(File.separator) + 1)
|
||||
}
|
||||
//println("ListSoundbank result: $result")
|
||||
it.result(objectmapper.writeValueAsString(result))
|
||||
} else {
|
||||
it.status(400)
|
||||
@@ -1911,7 +1931,6 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
val category = it.pathParam("category")
|
||||
|
||||
val uploaded = it.uploadedFiles()
|
||||
println("UploadSoundbank called with language=$language, voice=$voice, category=$category, uploaded files count=${uploaded.size}")
|
||||
if (ValidString(language) && Language.entries.any { lang -> lang.name == language }) {
|
||||
if (ValidString(voice) && VoiceType.entries.any { vtype -> vtype.name == voice }) {
|
||||
if (ValidString(category) && Category.entries.any { cat -> cat.name == category }) {
|
||||
@@ -2218,7 +2237,6 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
if (db.queuetableDB.Add(qt)) {
|
||||
db.queuetableDB.Resort()
|
||||
Logger.info { "SemiAutoWeb added to queue table: $qt" }
|
||||
//println("SemiAuto added to queue table: ${objectmapper.writeValueAsString(qt)}")
|
||||
ctx.result(objectmapper.writeValueAsString(resultMessage("OK")))
|
||||
} else ResultMessageString(ctx, 500, "Failed to add to queue table")
|
||||
} else ResultMessageString(ctx, 400, "Broadcast zones cannot be empty")
|
||||
@@ -2232,10 +2250,8 @@ class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>, val
|
||||
get("/{datelog}") { ctx ->
|
||||
val datelog = ctx.pathParam("datelog")
|
||||
if (ValidDate(datelog)) {
|
||||
println("SemiAuto Get Log for date $datelog")
|
||||
db.GetLogForHtml(datelog) { loghtml ->
|
||||
val resultstring = objectmapper.writeValueAsString(loghtml)
|
||||
println("Log HTML for date $datelog: $resultstring")
|
||||
ctx.result(resultstring)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user