mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
feat(android): add fpv-android-ffi JNI bridge and Galaxy Tab S8 Ultra Compose/MediaCodec app
This commit is contained in:
1 parent
16d05785fb
commit
6ccdb82f89
50 files changed
+2962
-1
No files matched your search
@@ -0,0 +1,183 @@
|
||||
package com.steamframe.fpv
|
||||
|
||||
import android.os.Bundle
|
||||
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 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("rtp")
|
||||
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)
|
||||
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()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
stopNativeEngine()
|
||||
decoder.release()
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user