File size: 21,225 Bytes
8a29801 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | /**
* Media Processing Queue
*
* Decouples media processing (HDR merge, color correction, stabilization, camera roll save)
* from the download pipeline. Downloads push items here; processing runs independently.
*/
import * as RNFS from "@dr.pogodin/react-native-fs"
import CrustModule from "@mentra/crust"
import {asgCameraApi} from "./asgCameraApi"
import {localStorageService} from "./localStorageService"
import {INVALID_DOWNLOADED_MEDIA, validateDownloadedMediaFile} from "./galleryMediaValidation"
import {reportInvalidGalleryMedia} from "./GalleryMediaIntegrityReportService"
import {useGallerySyncStore} from "../../stores/gallerySync"
import {BgTimer} from "../../utils/timers"
import {galleryTransferLedger, GalleryTransferEntry} from "./galleryTransferLedger"
import {cameraRollExportCoordinator} from "./cameraRollExportCoordinator"
const TAG = "[MediaProcessingQueue]"
export interface ProcessingItem {
/** Unique ID — capture_id (v2) or file name (v1) */
id: string
type: "photo" | "video"
/** Path to the primary downloaded file */
primaryPath: string
/** HDR bracket paths (v2 only) */
bracketPaths?: string[]
/** IMU sidecar path for video stabilization */
sidecarPath?: string
/** Thumbnail base64 data */
thumbnailData?: string
/** Capture directory for saving thumbnail */
captureDir?: string
/** Original capture timestamp */
timestamp?: number
/** Total file size */
totalSize: number
/** Video duration in ms */
duration?: number
/** Glasses model */
glassesModel?: string
/** Whether to process (lens/color/stabilization) */
shouldProcess: boolean
/** Whether to auto-save to camera roll */
shouldAutoSave: boolean
/** Pre-downloaded thumbnail path (v1 legacy sync) */
thumbnailPath?: string
/** File names to delete from glasses after processing completes */
deleteFromGlasses?: string[]
/** Negotiated gallery protocol; absent entries are inferred from the transfer ledger. */
protocolVersion?: number
}
class MediaProcessingQueue {
private queue: ProcessingItem[] = []
private isRunning = false
private aborted = false
private generation = 0 // C2: incremented on reset() to invalidate stale processLoop()
/** Add an item to the processing queue. Starts processing if not already running. */
enqueue(item: ProcessingItem): void {
this.queue.push(item)
console.log(`${TAG} Enqueued ${item.id} (${item.type}), queue size: ${this.queue.length}`)
// Only mark as processing if we'll actually process this item
if (item.shouldProcess) {
const store = useGallerySyncStore.getState()
store.onFileProcessing(item.id)
}
if (!this.isRunning) {
this.processLoop()
}
}
/** Cancel all pending processing. */
abort(): void {
this.aborted = true
this.queue = []
console.log(`${TAG} Aborted`)
}
/** Reset state for a new sync session. */
reset(): void {
this.queue = []
this.isRunning = false
this.aborted = false
this.generation++ // C2: invalidate any running processLoop
}
/** Returns true if there are items queued or currently processing. */
get hasPending(): boolean {
return this.queue.length > 0 || this.isRunning
}
/** Returns a promise that resolves when the queue is fully drained, or rejects on timeout. */
waitUntilDrained(timeoutMs: number = 4 * 60 * 60 * 1000): Promise<void> {
if (!this.hasPending) return Promise.resolve()
return new Promise((resolve, reject) => {
const deadline = Date.now() + timeoutMs
const check = () => {
if (!this.hasPending || this.aborted) {
resolve()
} else if (Date.now() >= deadline) {
console.error(`${TAG} waitUntilDrained timed out after ${timeoutMs}ms, aborting queue`)
this.abort()
reject(new Error(`Processing queue timed out after ${timeoutMs / 1000}s`))
} else {
BgTimer.setTimeout(check, 200)
}
}
check()
})
}
/** Retry durable export/ack work after an app restart without downloading media again. */
async retryPending(): Promise<{retried: number; failed: number}> {
let retried = 0
let failed = 0
for (let entry of galleryTransferLedger.pendingRecovery()) {
try {
if (entry.state === "RESTORE_PENDING") {
await asgCameraApi.restoreCapture(entry.captureId)
galleryTransferLedger.transition(entry.captureId, "DISCOVERED", {lastError: undefined})
retried++
continue
}
if (!entry.finalPath || !(await RNFS.exists(entry.finalPath))) {
throw new Error(`Recovery file is missing for ${entry.captureId}`)
}
// Rebuild a missing app index before any source acknowledgement, even when camera-roll
// saving is disabled or an export receipt was already committed before restart.
const downloadedFile = await this.recoverDownloadedFile(entry)
if (entry.state === "EXPORT_PENDING" || entry.state === "INDEXED") {
const shouldAutoSave = await cameraRollExportCoordinator.isAutoSaveEnabled()
entry = galleryTransferLedger.update(entry.captureId, {shouldAutoSave, lastError: undefined})
if (shouldAutoSave) {
if (entry.state !== "EXPORT_PENDING") {
entry = galleryTransferLedger.transition(entry.captureId, "EXPORT_PENDING")
}
const receipt = await cameraRollExportCoordinator.exportForSource(downloadedFile, entry.captureId)
entry = receipt
? galleryTransferLedger.transition(entry.captureId, "EXPORTED", {assetReceipt: receipt})
: galleryTransferLedger.transition(entry.captureId, "INDEXED", {
shouldAutoSave: false,
lastError: undefined,
})
} else if (entry.state === "EXPORT_PENDING") {
entry = galleryTransferLedger.transition(entry.captureId, "INDEXED", {
shouldAutoSave: false,
lastError: undefined,
})
}
}
entry = galleryTransferLedger.transition(entry.captureId, "ACK_PENDING")
await this.acknowledgeEntry(entry)
galleryTransferLedger.transition(entry.captureId, "TRASHED", {lastError: undefined})
retried++
} catch (error) {
galleryTransferLedger.recordFailure(entry.captureId, error)
failed++
console.warn(`${TAG} Recovery remains pending for ${entry.captureId}:`, error)
}
}
return {retried, failed}
}
private async acknowledgeEntry(entry: GalleryTransferEntry): Promise<void> {
if (entry.protocolVersion >= 3) {
await asgCameraApi.acknowledgeCapture(entry.captureId, entry.ackId)
return
}
const result = await asgCameraApi.deleteFilesFromServer(
entry.deleteFromGlasses.length > 0 ? entry.deleteFromGlasses : [entry.captureId],
)
if (result.failed.length > 0 || result.deleted.length === 0) {
throw new Error(`Glasses did not acknowledge ${entry.captureId}`)
}
}
/** Process items one at a time. */
private async processLoop(): Promise<void> {
if (this.isRunning) return
this.isRunning = true
const myGeneration = this.generation // C2: capture generation to detect reset()
while (this.queue.length > 0 && !this.aborted && this.generation === myGeneration) {
const item = this.queue.shift()!
let processFailed = false
try {
await this.processItem(item)
} catch (error) {
console.error(`${TAG} Error processing ${item.id}:`, error)
processFailed = true
const store = useGallerySyncStore.getState()
if (!store.failedFiles.includes(item.id)) {
store.onFileFailed(item.id, error instanceof Error ? error.message : String(error))
}
}
// Exit if generation changed (reset was called during processing)
if (this.generation !== myGeneration) break
const store = useGallerySyncStore.getState()
if (!processFailed) {
// Success: increment processedFiles and remove from processingFiles set.
store.onFileProcessed(item.id)
} else {
// Failure is counted exactly once above. Still clear the processingFiles entry so the UI
// does not show it as in-progress.
const current = store.processingFiles
if (current.has(item.id)) {
const newSet = new Set(current)
newSet.delete(item.id)
useGallerySyncStore.setState({processingFiles: newSet})
}
}
}
// Only clear isRunning if this loop owns the current generation
if (this.generation === myGeneration) {
this.isRunning = false
}
}
/** Process a single item through the full pipeline. */
private async processItem(item: ProcessingItem): Promise<void> {
const startTime = Date.now()
let filePathToSave = item.primaryPath
let ledgerEntry = galleryTransferLedger.get(item.id)
if (!ledgerEntry) {
ledgerEntry = galleryTransferLedger.ensureCapture(
{
capture_id: item.id,
type: item.type,
timestamp: item.timestamp || Date.now(),
total_size: item.totalSize,
files: (item.deleteFromGlasses?.length ? item.deleteFromGlasses : [item.id]).map((name, index) => ({
name,
size: index === 0 ? item.totalSize : 0,
role: index === 0 ? "primary" : "sidecar",
})),
},
item.protocolVersion || 2,
)
}
// 1. HDR merge (photos with brackets only)
if (item.shouldProcess && item.type === "photo" && item.bracketPaths && item.bracketPaths.length >= 3) {
try {
const underPath = item.bracketPaths.find((p) => p.includes("ev-2")) || item.bracketPaths[0]
const normalPath = item.bracketPaths.find((p) => p.includes("ev0")) || item.bracketPaths[1]
const overPath = item.bracketPaths.find((p) => p.includes("ev2") && !p.includes("ev-2")) || item.bracketPaths[2]
const hdrPath = item.primaryPath + ".hdr.jpg"
const hdrResult = await CrustModule.mergeHdrBrackets(underPath, normalPath, overPath, hdrPath)
if (hdrResult.success && hdrResult.outputPath) {
filePathToSave = hdrResult.outputPath
console.log(`${TAG} HDR merged ${item.id} in ${hdrResult.processingTimeMs}ms`)
}
} catch (hdrError) {
console.warn(`${TAG} HDR merge error for ${item.id}, continuing:`, hdrError)
}
}
// 2. Image processing (lens + color correction)
if (item.shouldProcess && item.type === "photo") {
try {
const processedPath = filePathToSave + ".processed.jpg"
const result = await CrustModule.processGalleryImage(filePathToSave, processedPath, {
lensCorrection: true,
colorCorrection: true,
})
if (result.success && result.outputPath) {
filePathToSave = result.outputPath
console.log(`${TAG} 🎨 Processed ${item.id} in ${result.processingTimeMs}ms`)
}
} catch (procError) {
console.warn(`${TAG} Processing error for ${item.id}, using original:`, procError)
}
}
// 3. Video stabilization + color correction
if (item.shouldProcess && item.type === "video" && item.sidecarPath) {
try {
const stabilizedPath = item.primaryPath + ".stabilized.mp4"
const result = await CrustModule.stabilizeVideo(item.primaryPath, item.sidecarPath, stabilizedPath)
if (result.success && result.outputPath) {
filePathToSave = result.outputPath
console.log(`${TAG} 📹 Stabilized ${item.id} in ${result.processingTimeMs}ms`)
}
} catch (stabError) {
console.warn(`${TAG} Stabilization error for ${item.id}, using original:`, stabError)
}
}
// 4. Save thumbnail to disk (or use pre-downloaded thumbnail from v1 sync)
let localThumbnailPath: string | undefined = item.thumbnailPath
if (item.thumbnailData && item.captureDir) {
try {
const thumbPath = `${item.captureDir}/.thumb.jpg`
const base64Data = item.thumbnailData.startsWith("data:")
? item.thumbnailData.split(",")[1]
: item.thumbnailData
await RNFS.writeFile(thumbPath, base64Data, "base64")
localThumbnailPath = thumbPath
} catch (thumbError) {
console.warn(`${TAG} Failed to save thumbnail for ${item.id}:`, thumbError)
}
}
// 5. Validate local file before persisting metadata or deleting from glasses
try {
await validateDownloadedMediaFile({
path: filePathToSave,
name: item.id,
mediaKind: item.type,
})
} catch (validationError: any) {
const reason = validationError?.message?.includes(INVALID_DOWNLOADED_MEDIA)
? validationError.message
: `${INVALID_DOWNLOADED_MEDIA}: ${item.id}`
console.error(`${TAG} Validation failed for ${item.id}: ${reason}`)
reportInvalidGalleryMedia({
name: item.id,
path: filePathToSave,
mediaKind: item.type,
stage: "processing_validation",
reason,
expectedSize: item.totalSize,
duration: item.duration,
glassesModel: item.glassesModel,
})
const store = useGallerySyncStore.getState()
store.onFileFailed(item.id, reason)
galleryTransferLedger.recordFailure(item.id, validationError)
throw new Error(reason)
}
// 6. Commit the app-local index before any export or source acknowledgement. Use the final
// on-disk output metadata for camera-roll identity: processing can change byte size, while
// recovery reconstructs this row from the same file and the transfer ledger timestamp.
const isVideo = item.type === "video"
const finalFileStat = await RNFS.stat(filePathToSave)
const finalFileSize = Number(finalFileStat.size)
const downloadedFile = localStorageService.convertToDownloadedFile(
{
name: item.id,
url: "",
download: "",
size: finalFileSize,
modified: ledgerEntry.timestamp,
is_video: isVideo,
thumbnail_data: item.thumbnailData,
duration: item.duration,
filePath: filePathToSave,
glassesModel: item.glassesModel,
},
filePathToSave,
localThumbnailPath,
item.glassesModel,
)
downloadedFile.capture_id = item.id
const shouldAutoSave = await cameraRollExportCoordinator.isAutoSaveEnabled()
try {
await localStorageService.saveDownloadedFile(downloadedFile)
ledgerEntry = galleryTransferLedger.transition(item.id, "INDEXED", {
finalPath: filePathToSave,
thumbnailPath: localThumbnailPath,
shouldAutoSave,
})
await cameraRollExportCoordinator.recordIndexed(
downloadedFile,
shouldAutoSave,
shouldAutoSave ? "SOURCE_BARRIER" : "HISTORICAL_BACKFILL",
item.id,
)
} catch (metadataError) {
console.error(`${TAG} Metadata save failed for ${item.id}; retaining local and glasses copies:`, metadataError)
galleryTransferLedger.recordFailure(item.id, metadataError)
throw metadataError
}
// 7. While enabled, export is a durable, receipted step whose failures block ack. Turning
// auto-save off safely falls back to the already-verified app-local copy and later backfill.
if (shouldAutoSave) {
galleryTransferLedger.transition(item.id, "EXPORT_PENDING")
let receipt
try {
receipt = await cameraRollExportCoordinator.exportForSource(downloadedFile, item.id)
} catch (error) {
galleryTransferLedger.recordFailure(item.id, error)
throw error
}
if (receipt) {
ledgerEntry = galleryTransferLedger.transition(item.id, "EXPORTED", {
assetReceipt: receipt,
})
console.log(`${TAG} ✅ Saved to camera roll with receipt: ${item.id}`)
} else {
ledgerEntry = galleryTransferLedger.transition(item.id, "INDEXED", {
shouldAutoSave: false,
lastError: undefined,
})
console.log(`${TAG} Automatic camera-roll saving was disabled; keeping app-local copy: ${item.id}`)
}
}
// S4: Clean up intermediate processing files
const intermediates = [
item.primaryPath + ".hdr.jpg",
item.primaryPath + ".processed.jpg",
item.primaryPath + ".stabilized.mp4",
]
for (const intermediate of intermediates) {
if (intermediate !== filePathToSave) {
RNFS.unlink(intermediate).catch(() => {}) // fire-and-forget
}
}
// 8. Update file in sync queue with local paths for gallery display
const store = useGallerySyncStore.getState()
const localFileUrl = filePathToSave.startsWith("file://") ? filePathToSave : `file://${filePathToSave}`
const localThumbUrl = localThumbnailPath
? localThumbnailPath.startsWith("file://")
? localThumbnailPath
: `file://${localThumbnailPath}`
: undefined
store.updateFileInQueue(item.id, {
name: item.id,
url: localFileUrl,
download: localFileUrl,
size: finalFileSize,
modified: ledgerEntry.timestamp,
is_video: isVideo,
filePath: filePathToSave,
mime_type: isVideo ? "video/mp4" : "image/jpeg",
thumbnail_data: item.thumbnailData,
thumbnailPath: localThumbUrl,
duration: item.duration,
})
// 9. Acknowledge only after VERIFIED -> INDEXED -> (EXPORTED). The server turns this into
// recoverable trash; failures remain ACK_PENDING and are retried without re-downloading.
const localFileExists = await RNFS.exists(filePathToSave)
const localStat = localFileExists ? await RNFS.stat(filePathToSave) : null
if (!localStat || localStat.size <= 0) {
const error = new Error(`Cannot acknowledge ${item.id}: committed local file is missing or empty`)
galleryTransferLedger.recordFailure(item.id, error)
throw error
}
ledgerEntry = galleryTransferLedger.transition(item.id, "ACK_PENDING")
try {
await this.acknowledgeEntry(ledgerEntry)
galleryTransferLedger.transition(item.id, "TRASHED", {lastError: undefined})
} catch (ackError) {
galleryTransferLedger.recordFailure(item.id, ackError)
throw ackError
}
const elapsed = Date.now() - startTime
console.log(`${TAG} ✅ Finished ${item.id} in ${elapsed}ms`)
}
/** Rebuild a missing app-local index row from the durable transfer ledger. */
private async recoverDownloadedFile(entry: GalleryTransferEntry) {
if (!entry.finalPath) throw new Error(`Recovery file is missing for ${entry.captureId}`)
// The transfer ledger is the durable source of truth for a committed generation. A local
// index row can survive an interrupted replacement and still point at an older/missing path,
// or contain the pre-processing size. Only reuse it when its path and identity still match the
// ledger's committed file; otherwise rebuild the row before export or source acknowledgement.
const finalPath = entry.finalPath.replace(/^file:\/\//, "")
const indexed = await localStorageService.getDownloadedFile(entry.captureId)
if (indexed) {
try {
const indexedPath = indexed.filePath?.replace(/^file:\/\//, "")
if (indexedPath === finalPath && (await RNFS.exists(indexedPath))) {
const indexedStat = await RNFS.stat(indexedPath)
const indexedSize = Number(indexedStat.size)
if (indexedSize > 0 && indexed.size === indexedSize && indexed.modified === entry.timestamp) {
return indexed
}
}
} catch (error) {
console.warn(`${TAG} Ignoring stale local index for ${entry.captureId}:`, error)
}
}
const stat = await RNFS.stat(finalPath)
const finalSize = Number(stat.size)
if (!Number.isFinite(finalSize) || finalSize <= 0) {
throw new Error(`Recovery file is missing or empty for ${entry.captureId}`)
}
const recovered = localStorageService.convertToDownloadedFile(
{
name: entry.captureId,
url: "",
download: "",
size: finalSize,
modified: entry.timestamp,
mime_type: entry.captureType === "video" ? "video/mp4" : "image/jpeg",
is_video: entry.captureType === "video",
filePath: finalPath,
},
finalPath,
entry.thumbnailPath,
)
recovered.capture_id = entry.captureId
recovered.assetReceipt = entry.assetReceipt
await localStorageService.saveDownloadedFile(recovered)
return recovered
}
}
export const mediaProcessingQueue = new MediaProcessingQueue()
|