mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
233 lines
8.1 KiB
Kotlin
233 lines
8.1 KiB
Kotlin
package com.steamframe.fpv
|
|
|
|
import android.net.ConnectivityManager
|
|
import android.net.Network
|
|
import android.net.NetworkCapabilities
|
|
import android.net.NetworkRequest
|
|
import android.os.Bundle
|
|
import android.util.Log
|
|
import android.view.Surface
|
|
import android.view.WindowManager
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.activity.enableEdgeToEdge
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.core.view.WindowCompat
|
|
import androidx.core.view.WindowInsetsCompat
|
|
import androidx.core.view.WindowInsetsControllerCompat
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.delay
|
|
import kotlinx.coroutines.withContext
|
|
import java.util.Locale
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
private lateinit var decoder: LowLatencyVideoDecoder
|
|
private lateinit var connectivityManager: ConnectivityManager
|
|
private var ethCallback: ConnectivityManager.NetworkCallback? = null
|
|
private var nativeHandle: Long = 0L
|
|
private var currentSurface: Surface? = null
|
|
|
|
private var snapshot by mutableStateOf(FpvSnapshot())
|
|
private var osdEnabled by mutableStateOf(true)
|
|
private var dvrRecording by mutableStateOf(false)
|
|
private var codecName by mutableStateOf("h264")
|
|
private var transportName by mutableStateOf("raw")
|
|
private var pingStatus by mutableStateOf("")
|
|
|
|
private val radxaIp = "192.168.1.1"
|
|
private val videoPort = 5600
|
|
private val g29Port = 14552
|
|
private val telemetryPort = 14551
|
|
|
|
private val nativeCallback = object : NativeFpvCallback {
|
|
override fun onVideoNal(
|
|
data: ByteArray,
|
|
ptsUs: Long,
|
|
isKeyframe: Boolean,
|
|
isCodecConfig: Boolean,
|
|
) {
|
|
decoder.submitNal(data, ptsUs, isKeyframe, isCodecConfig)
|
|
}
|
|
|
|
override fun onG29Action(actionCode: Int) {
|
|
runOnUiThread {
|
|
when (actionCode) {
|
|
NativeFpvEngine.ACTION_TOGGLE_DVR -> toggleDvr()
|
|
NativeFpvEngine.ACTION_TOGGLE_OSD -> {
|
|
osdEnabled = !osdEnabled
|
|
syncNativeFlags()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
enableEdgeToEdge()
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
|
hideSystemBars()
|
|
|
|
decoder = LowLatencyVideoDecoder(applicationContext)
|
|
connectivityManager = getSystemService(ConnectivityManager::class.java)
|
|
bindEthernetNetwork()
|
|
restartNativeEngine()
|
|
|
|
setContent {
|
|
LaunchedEffect(Unit) {
|
|
while (true) {
|
|
val h = nativeHandle
|
|
if (h != 0L) {
|
|
val json = withContext(Dispatchers.IO) {
|
|
NativeFpvEngine.nativePollSnapshotJson(h)
|
|
}
|
|
val snap = FpvSnapshot.fromJson(json)
|
|
snapshot = snap
|
|
if (dvrRecording && snap.telemetry.srtLine.isNotBlank()) {
|
|
decoder.pushSrtCaption(snap.telemetry.srtLine)
|
|
}
|
|
}
|
|
delay(50L) // Rafraîchissement OSD 20 Hz
|
|
}
|
|
}
|
|
|
|
CockpitScreen(
|
|
snapshot = snapshot,
|
|
osdEnabled = osdEnabled,
|
|
dvrRecording = dvrRecording,
|
|
codec = codecName,
|
|
transport = transportName,
|
|
pingStatus = pingStatus,
|
|
onSurfaceReady = { surf ->
|
|
currentSurface = surf
|
|
decoder.setSurface(surf, codecName)
|
|
},
|
|
onToggleDvr = { toggleDvr() },
|
|
onToggleOsd = {
|
|
osdEnabled = !osdEnabled
|
|
syncNativeFlags()
|
|
},
|
|
onCycleCodec = {
|
|
codecName = if (codecName == "h264") "h265" else "h264"
|
|
decoder.setSurface(currentSurface, codecName)
|
|
restartNativeEngine()
|
|
},
|
|
onCycleTransport = {
|
|
transportName = if (transportName == "rtp") "raw" else "rtp"
|
|
restartNativeEngine()
|
|
},
|
|
onPingRadxa = { runPingRadxa() },
|
|
)
|
|
}
|
|
}
|
|
|
|
private fun hideSystemBars() {
|
|
WindowCompat.setDecorFitsSystemWindows(window, false)
|
|
val controller = WindowInsetsControllerCompat(window, window.decorView)
|
|
controller.hide(WindowInsetsCompat.Type.systemBars())
|
|
controller.systemBarsBehavior =
|
|
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
|
|
}
|
|
|
|
private fun restartNativeEngine() {
|
|
stopNativeEngine()
|
|
nativeHandle = NativeFpvEngine.nativeStart(
|
|
callback = nativeCallback,
|
|
videoPort = videoPort,
|
|
codec = codecName,
|
|
transport = transportName,
|
|
g29Port = g29Port,
|
|
telemetryPort = telemetryPort,
|
|
)
|
|
syncNativeFlags()
|
|
}
|
|
|
|
private fun stopNativeEngine() {
|
|
val h = nativeHandle
|
|
nativeHandle = 0L
|
|
if (h != 0L) {
|
|
NativeFpvEngine.nativeStop(h)
|
|
}
|
|
}
|
|
|
|
private fun syncNativeFlags() {
|
|
val h = nativeHandle
|
|
if (h != 0L) {
|
|
NativeFpvEngine.nativeSyncUiFlags(h, dvrRecording, osdEnabled)
|
|
}
|
|
}
|
|
|
|
private fun toggleDvr() {
|
|
if (dvrRecording) {
|
|
decoder.stopDvr()
|
|
dvrRecording = false
|
|
} else {
|
|
dvrRecording = decoder.startDvr()
|
|
}
|
|
syncNativeFlags()
|
|
}
|
|
|
|
private fun runPingRadxa() {
|
|
pingStatus = "Ping..."
|
|
Thread {
|
|
val rtt = NativeFpvEngine.nativePingRadxa(radxaIp, g29Port)
|
|
runOnUiThread {
|
|
pingStatus = if (rtt >= 0f) {
|
|
String.format(Locale.US, "Radxa %.1f ms", rtt)
|
|
} else {
|
|
"Radxa hors ligne"
|
|
}
|
|
}
|
|
}.start()
|
|
}
|
|
|
|
private fun bindEthernetNetwork() {
|
|
try {
|
|
// Sur Android 12+, une interface Ethernet USB sans Internet n'est pas routée par défaut :
|
|
// on force le processus sur TRANSPORT_ETHERNET pour que les sockets UDP Rust joignent 192.168.1.1 même Wi-Fi allumé.
|
|
val req = NetworkRequest.Builder()
|
|
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
|
|
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
.build()
|
|
val cb = object : ConnectivityManager.NetworkCallback() {
|
|
override fun onAvailable(network: Network) {
|
|
val ok = connectivityManager.bindProcessToNetwork(network)
|
|
Log.i("SteamFrameFPV", "Ethernet USB lié au processus (bindProcessToNetwork=$ok)")
|
|
runOnUiThread {
|
|
pingStatus = if (ok) "ETH USB OK" else "ETH bind fail"
|
|
restartNativeEngine()
|
|
}
|
|
}
|
|
|
|
override fun onLost(network: Network) {
|
|
connectivityManager.bindProcessToNetwork(null)
|
|
Log.i("SteamFrameFPV", "Ethernet USB déconnecté")
|
|
runOnUiThread {
|
|
pingStatus = "ETH déconnecté"
|
|
}
|
|
}
|
|
}
|
|
ethCallback = cb
|
|
connectivityManager.requestNetwork(req, cb)
|
|
} catch (e: Exception) {
|
|
Log.w("SteamFrameFPV", "Impossible de lier TRANSPORT_ETHERNET : ${e.message}")
|
|
}
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
ethCallback?.let {
|
|
try {
|
|
connectivityManager.unregisterNetworkCallback(it)
|
|
} catch (_: Exception) {
|
|
}
|
|
}
|
|
connectivityManager.bindProcessToNetwork(null)
|
|
stopNativeEngine()
|
|
decoder.release()
|
|
super.onDestroy()
|
|
}
|
|
}
|