Give the Fly App GO 4-style playback, flight records, Find My Drone and live streaming
Playback: the flight screen's playback button opens the album over the live view, so a video tapped there plays on the aircraft into the feed with a transport bar. Long press selects files to download or delete (delete asks first); one file opens in a viewer with the camera's preview. Leaving the album puts the camera back in its capture mode. Flight records: every flight is recorded from take-off to landing (1 Hz track, max height / distance / speed, distance flown, battery used) and kept on the phone. When it lands while signed in, it is uploaded to the PilotVault logbook through the existing /api/drones/auto and /api/flights endpoints as an automatic-path entry with the track as GeoJSON. Simulator flights are never uploaded; flights recorded while signed out retry on sign-in. Flight logs lists them with their upload state and a track map, and the profile stats come from real flights. Find My Drone: the aircraft's last fix is kept across lost links and restarts, and shown against the phone's own GPS with distance, direction and a hand-off to the maps app. Live streaming: a new LiveStreamBridge streams the main camera to an RTMP address set in Settings > General, with a LIVE pill on the flight screen that turns amber when the uplink falls behind and stops on tap. Pre-flight checklist (manual checks plus the aircraft's live status) and an in-app Academy with six short guides that work offline. None of this has run on hardware yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e02bb0e5cd
commit
91ca150a35
28 files changed
+3747
-158
No files matched your search
@@ -114,6 +114,7 @@ class DjiSdkBridge(
|
||||
VirtualStickBridge(ctx),
|
||||
AirSenseBridge(ctx),
|
||||
FlyZoneBridge(ctx),
|
||||
LiveStreamBridge(ctx),
|
||||
)
|
||||
|
||||
init {
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package com.pilotvault.flyapp
|
||||
|
||||
import dji.sdk.sdkmanager.DJISDKManager
|
||||
import dji.sdk.sdkmanager.LiveStreamManager
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
/**
|
||||
* RTMP live streaming of the primary video feed (plan P9, GO 4's "Live
|
||||
* streaming platform"), through the SDK's app-level [LiveStreamManager].
|
||||
*
|
||||
* Methods: `startLiveStream {url, audio}` (resolves once the stream is up, or
|
||||
* fails with the SDK's result code), `stopLiveStream`, `getLiveStreamState`.
|
||||
* Every change, and once a second while streaming, it emits
|
||||
*
|
||||
* ```
|
||||
* liveStream: {streaming, status: STREAMING | STOPPED | ERROR | null, url,
|
||||
* videoKbps, audioKbps, fps, seconds, cacheSize, error}
|
||||
* ```
|
||||
*
|
||||
* `startStream` blocks on a latch inside the SDK, so starting and stopping run
|
||||
* on a single worker thread.
|
||||
*/
|
||||
class LiveStreamBridge(private val ctx: BridgeCtx) : SubBridge {
|
||||
|
||||
private val worker = Executors.newSingleThreadExecutor()
|
||||
private var listening = false
|
||||
private var lastStatus: Int? = null
|
||||
private var lastError: String? = null
|
||||
private var polling = false
|
||||
|
||||
private val statusListener = LiveStreamManager.OnLiveChangeListener { status ->
|
||||
lastStatus = status
|
||||
ctx.mainHandler.post {
|
||||
publish()
|
||||
if (status == LiveStreamManager.STATUS_STREAMING) startPolling()
|
||||
}
|
||||
}
|
||||
|
||||
private val errorListener = LiveStreamManager.OnLiveErrorStatusListener { code, message ->
|
||||
lastError = message?.takeIf { it.isNotBlank() } ?: "Error $code"
|
||||
ctx.mainHandler.post { publish() }
|
||||
}
|
||||
|
||||
private val poll = object : Runnable {
|
||||
override fun run() {
|
||||
if (!polling) return
|
||||
publish()
|
||||
if (manager()?.isStreaming == true) {
|
||||
ctx.mainHandler.postDelayed(this, POLL_MS)
|
||||
} else {
|
||||
polling = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun manager(): LiveStreamManager? = try {
|
||||
DJISDKManager.getInstance().liveStreamManager
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
|
||||
override fun handle(call: MethodCall, result: MethodChannel.Result): Boolean {
|
||||
when (call.method) {
|
||||
"startLiveStream" -> start(call.argument<String>("url") ?: "", call.argument<Boolean>("audio") ?: false, result)
|
||||
"stopLiveStream" -> stop(result)
|
||||
"getLiveStreamState" -> {
|
||||
val m = manager()
|
||||
ctx.mainHandler.post { result.success(if (m == null) null else state(m)) }
|
||||
}
|
||||
else -> return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun listen(m: LiveStreamManager) {
|
||||
if (listening) return
|
||||
m.registerListener(statusListener)
|
||||
m.addLiveErrorStatusListener(errorListener)
|
||||
listening = true
|
||||
}
|
||||
|
||||
private fun start(url: String, audio: Boolean, result: MethodChannel.Result) {
|
||||
val m = manager() ?: run { ctx.fail(result, "Live streaming is not available until the SDK is registered"); return }
|
||||
if (!url.startsWith("rtmp://") && !url.startsWith("rtmps://")) {
|
||||
ctx.fail(result, "The stream address must start with rtmp://")
|
||||
return
|
||||
}
|
||||
listen(m)
|
||||
lastError = null
|
||||
worker.execute {
|
||||
try {
|
||||
if (m.isStreaming) m.stopStream()
|
||||
m.setLiveUrl(url)
|
||||
m.setAudioStreamingEnabled(audio)
|
||||
val code = m.startStream()
|
||||
m.setStartTime()
|
||||
ctx.mainHandler.post {
|
||||
publish()
|
||||
if (code == 0) {
|
||||
startPolling()
|
||||
result.success(null)
|
||||
} else {
|
||||
result.error("DJI_ERROR", lastError ?: "The stream did not start (code $code)", code)
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
ctx.mainHandler.post { result.error("DJI_ERROR", e.message ?: "The stream did not start", null) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun stop(result: MethodChannel.Result) {
|
||||
val m = manager() ?: run { ctx.mainHandler.post { result.success(null) }; return }
|
||||
worker.execute {
|
||||
try {
|
||||
m.stopStream()
|
||||
} catch (_: Throwable) {
|
||||
// Already stopped.
|
||||
}
|
||||
ctx.mainHandler.post {
|
||||
polling = false
|
||||
publish()
|
||||
result.success(null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startPolling() {
|
||||
if (polling) return
|
||||
polling = true
|
||||
ctx.mainHandler.postDelayed(poll, POLL_MS)
|
||||
}
|
||||
|
||||
private fun publish() {
|
||||
val m = manager() ?: return
|
||||
ctx.emit(mapOf("type" to "liveStream") + state(m))
|
||||
}
|
||||
|
||||
private fun state(m: LiveStreamManager): Map<String, Any?> {
|
||||
val streaming = try { m.isStreaming } catch (_: Throwable) { false }
|
||||
return mapOf(
|
||||
"streaming" to streaming,
|
||||
"status" to when (lastStatus) {
|
||||
LiveStreamManager.STATUS_STREAMING -> "STREAMING"
|
||||
LiveStreamManager.STATUS_STOP -> "STOPPED"
|
||||
null -> null
|
||||
else -> "ERROR"
|
||||
},
|
||||
"url" to try { m.liveUrl } catch (_: Throwable) { null },
|
||||
"videoKbps" to if (streaming) m.liveVideoBitRate else 0,
|
||||
"audioKbps" to if (streaming) m.liveAudioBitRate else 0,
|
||||
"fps" to if (streaming) m.liveVideoFps.toDouble() else 0.0,
|
||||
"seconds" to if (streaming) m.streamTime else 0L,
|
||||
"cacheSize" to if (streaming) m.liveVideoCacheSize else 0,
|
||||
"error" to lastError,
|
||||
)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val POLL_MS = 1000L
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.pilotvault.flyapp
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import dji.common.camera.SettingsDefinitions
|
||||
import dji.common.error.DJICameraError
|
||||
import dji.common.error.DJIError
|
||||
import dji.common.util.CommonCallbacks
|
||||
import dji.sdk.media.DownloadListener
|
||||
@@ -13,31 +14,77 @@ import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
|
||||
/**
|
||||
* On-drone media via the SDK [MediaManager]: list the SD card, fetch thumbnails,
|
||||
* and download originals. Emits `mediaList` (the file catalogue) and
|
||||
* `mediaDownload` (per-file progress). Thumbnails/downloads are written to the
|
||||
* app's cache / files dirs and their paths handed back to Dart.
|
||||
* On-drone media via the SDK [MediaManager]: list the SD card, fetch thumbnails
|
||||
* and previews, download originals, delete, and play videos on the aircraft
|
||||
* (plan P9's in-FPV playback). Emits `mediaList` (the file catalogue),
|
||||
* `mediaDownload` (per-file progress) and `mediaPlayback` (the aircraft's video
|
||||
* player: `{index, status: PLAYING | PAUSED | STOPPED | UNKNOWN, position}`, s).
|
||||
* Thumbnails / previews / downloads are written to the app's cache / files dirs
|
||||
* and their paths handed back to Dart.
|
||||
*
|
||||
* Files are addressed by their index in the last refreshed list; a delete
|
||||
* refreshes the list, so Dart must re-read indices from the next `mediaList`.
|
||||
* Cached images are named by [cacheKey], which survives that re-indexing.
|
||||
*/
|
||||
class MediaBridge(private val ctx: BridgeCtx) : SubBridge {
|
||||
|
||||
// Snapshot of the last-refreshed file list; Dart addresses files by index.
|
||||
private var files: List<MediaFile> = emptyList()
|
||||
|
||||
// The manager the playback listener is attached to, so it is added once.
|
||||
private var listening: MediaManager? = null
|
||||
|
||||
private val playbackListener = MediaManager.VideoPlaybackStateListener { state ->
|
||||
val playing = state?.playingMediaFile
|
||||
val values = mapOf(
|
||||
"type" to "mediaPlayback",
|
||||
"index" to (playing?.let { files.indexOf(it) } ?: -1),
|
||||
"status" to (state?.playbackStatus?.name ?: "UNKNOWN"),
|
||||
"position" to (state?.playingPosition?.toDouble() ?: 0.0),
|
||||
)
|
||||
ctx.mainHandler.post { ctx.emit(values) }
|
||||
}
|
||||
|
||||
private fun manager(): MediaManager? = ctx.aircraft()?.camera?.mediaManager
|
||||
|
||||
override fun handle(call: MethodCall, result: MethodChannel.Result): Boolean {
|
||||
when (call.method) {
|
||||
"refreshMediaList" -> refresh(result)
|
||||
"fetchThumbnail" -> fetchThumbnail(call.argument<Int>("index") ?: -1, result)
|
||||
"fetchMediaPreview" -> fetchPreview(call.argument<Int>("index") ?: -1, result)
|
||||
"downloadMedia" -> download(call.argument<Int>("index") ?: -1, result)
|
||||
"deleteMedia" -> delete(call.argument<List<Int>>("indices") ?: emptyList(), result)
|
||||
"playMediaVideo" -> play(call.argument<Int>("index") ?: -1, result)
|
||||
"pauseMediaVideo" -> withManager(result) { it.pause(ctx.completion(result)) }
|
||||
"resumeMediaVideo" -> withManager(result) { it.resume(ctx.completion(result)) }
|
||||
"stopMediaVideo" -> withManager(result) { it.stop(ctx.completion(result)) }
|
||||
"seekMediaVideo" -> withManager(result) {
|
||||
it.moveToPosition((call.argument<Double>("seconds") ?: 0.0).toFloat(), ctx.completion(result))
|
||||
}
|
||||
else -> return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
listening?.removeMediaUpdatedVideoPlaybackStateListener(playbackListener)
|
||||
listening = null
|
||||
files = emptyList()
|
||||
}
|
||||
|
||||
private fun withManager(result: MethodChannel.Result, block: (MediaManager) -> Unit) {
|
||||
val mgr = manager() ?: run { ctx.fail(result, "Media manager not supported"); return }
|
||||
block(mgr)
|
||||
}
|
||||
|
||||
private fun refresh(result: MethodChannel.Result) {
|
||||
val camera = ctx.aircraft()?.camera ?: run { ctx.fail(result, "No camera connected"); return }
|
||||
val mgr = camera.mediaManager ?: run { ctx.fail(result, "Media manager not supported"); return }
|
||||
if (listening !== mgr) {
|
||||
listening?.removeMediaUpdatedVideoPlaybackStateListener(playbackListener)
|
||||
mgr.addMediaUpdatedVideoPlaybackStateListener(playbackListener)
|
||||
listening = mgr
|
||||
}
|
||||
|
||||
// The media manager only serves files while the camera is in download mode.
|
||||
camera.setMode(
|
||||
@@ -57,7 +104,7 @@ class MediaBridge(private val ctx: BridgeCtx) : SubBridge {
|
||||
return
|
||||
}
|
||||
files = mgr.sdCardFileListSnapshot ?: emptyList()
|
||||
emitList()
|
||||
emitList(mgr)
|
||||
ctx.mainHandler.post { result.success(files.size) }
|
||||
}
|
||||
},
|
||||
@@ -67,20 +114,42 @@ class MediaBridge(private val ctx: BridgeCtx) : SubBridge {
|
||||
)
|
||||
}
|
||||
|
||||
private fun emitList() {
|
||||
private fun cacheKey(f: MediaFile): String =
|
||||
"${f.fileName}_${f.timeCreated}".replace(Regex("[^A-Za-z0-9._-]"), "_")
|
||||
|
||||
private fun emitList(mgr: MediaManager) {
|
||||
val list = files.mapIndexed { i, f ->
|
||||
val type = f.mediaType
|
||||
val isVideo = type == MediaFile.MediaType.MP4 || type == MediaFile.MediaType.MOV
|
||||
mapOf(
|
||||
"index" to i,
|
||||
"key" to cacheKey(f),
|
||||
"fileName" to f.fileName,
|
||||
"type" to if (isVideo) "video" else "photo",
|
||||
"mediaType" to type?.name,
|
||||
"durationSeconds" to f.durationInSeconds.toInt(),
|
||||
"sizeBytes" to f.fileSize,
|
||||
"createdMs" to f.timeCreated,
|
||||
"resolution" to f.resolution?.name,
|
||||
)
|
||||
}
|
||||
ctx.emit(mapOf("type" to "mediaList", "files" to list))
|
||||
val supportsPlayback = try { mgr.isVideoPlaybackSupported } catch (_: Throwable) { false }
|
||||
ctx.emit(mapOf("type" to "mediaList", "files" to list, "videoPlayback" to supportsPlayback))
|
||||
}
|
||||
|
||||
/** Writes [bmp] to the cache as [name] and answers [result] with its path. */
|
||||
private fun saveBitmap(bmp: Bitmap?, name: String, result: MethodChannel.Result) {
|
||||
if (bmp == null) {
|
||||
ctx.mainHandler.post { result.success(null) }
|
||||
return
|
||||
}
|
||||
val out = File(ctx.appContext.cacheDir, name)
|
||||
try {
|
||||
FileOutputStream(out).use { bmp.compress(Bitmap.CompressFormat.JPEG, 90, it) }
|
||||
ctx.mainHandler.post { result.success(out.absolutePath) }
|
||||
} catch (e: Exception) {
|
||||
ctx.mainHandler.post { result.error("IO_ERROR", e.message, null) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun fetchThumbnail(index: Int, result: MethodChannel.Result) {
|
||||
@@ -91,22 +160,60 @@ class MediaBridge(private val ctx: BridgeCtx) : SubBridge {
|
||||
ctx.mainHandler.post { result.error("DJI_ERROR", err.description, null) }
|
||||
return
|
||||
}
|
||||
val bmp: Bitmap? = file.thumbnail
|
||||
if (bmp == null) {
|
||||
ctx.mainHandler.post { result.success(null) }
|
||||
return
|
||||
}
|
||||
val out = File(ctx.appContext.cacheDir, "thumb_$index.png")
|
||||
try {
|
||||
FileOutputStream(out).use { bmp.compress(Bitmap.CompressFormat.PNG, 90, it) }
|
||||
ctx.mainHandler.post { result.success(out.absolutePath) }
|
||||
} catch (e: Exception) {
|
||||
ctx.mainHandler.post { result.error("IO_ERROR", e.message, null) }
|
||||
}
|
||||
saveBitmap(file.thumbnail, "thumb_${cacheKey(file)}.jpg", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** The screen-sized preview the camera keeps beside each photo (and a still for videos). */
|
||||
private fun fetchPreview(index: Int, result: MethodChannel.Result) {
|
||||
val file = files.getOrNull(index) ?: run { ctx.fail(result, "No such media"); return }
|
||||
file.fetchPreview(object : CommonCallbacks.CompletionCallback<DJIError> {
|
||||
override fun onResult(err: DJIError?) {
|
||||
if (err != null) {
|
||||
ctx.mainHandler.post { result.error("DJI_ERROR", err.description, null) }
|
||||
return
|
||||
}
|
||||
saveBitmap(file.preview, "preview_${cacheKey(file)}.jpg", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun delete(indices: List<Int>, result: MethodChannel.Result) {
|
||||
val mgr = manager() ?: run { ctx.fail(result, "Media manager not supported"); return }
|
||||
val targets = indices.mapNotNull { files.getOrNull(it) }
|
||||
if (targets.isEmpty()) {
|
||||
ctx.fail(result, "No such media")
|
||||
return
|
||||
}
|
||||
mgr.deleteFiles(
|
||||
targets,
|
||||
object : CommonCallbacks.CompletionCallbackWithTwoParam<List<MediaFile>, DJICameraError> {
|
||||
override fun onSuccess(failed: List<MediaFile>?, error: DJICameraError?) {
|
||||
val failedCount = failed?.size ?: 0
|
||||
// The snapshot no longer matches the card: re-list before answering.
|
||||
files = mgr.sdCardFileListSnapshot ?: files.filter { it !in targets || failed?.contains(it) == true }
|
||||
emitList(mgr)
|
||||
ctx.mainHandler.post { result.success(failedCount) }
|
||||
}
|
||||
|
||||
override fun onFailure(error: DJIError?) {
|
||||
ctx.mainHandler.post { result.error("DJI_ERROR", error?.description ?: "Delete failed", null) }
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private fun play(index: Int, result: MethodChannel.Result) {
|
||||
val mgr = manager() ?: run { ctx.fail(result, "Media manager not supported"); return }
|
||||
val file = files.getOrNull(index) ?: run { ctx.fail(result, "No such media"); return }
|
||||
if (!mgr.isVideoPlaybackSupported) {
|
||||
ctx.fail(result, "This aircraft cannot play videos itself; download it instead")
|
||||
return
|
||||
}
|
||||
mgr.playVideoMediaFile(file, ctx.completion(result))
|
||||
}
|
||||
|
||||
private fun download(index: Int, result: MethodChannel.Result) {
|
||||
val file = files.getOrNull(index) ?: run { ctx.fail(result, "No such media"); return }
|
||||
val destDir = ctx.appContext.getExternalFilesDir(null) ?: ctx.appContext.filesDir
|
||||
|
||||
Reference in new issue
Block a user