fix(camera): isolate stalled diagnostic modes and gate preview surfaces
This commit is contained in:
@@ -44,9 +44,11 @@ class CameraDiagnosticsActivity: ComponentActivity() {
|
||||
AndroidView(factory={context->SurfaceView(context).apply {
|
||||
holder.addCallback(object: SurfaceHolder.Callback {
|
||||
override fun surfaceCreated(holder: SurfaceHolder) {
|
||||
readyHolder=holder;startIfReady()
|
||||
readyHolder=holder
|
||||
}
|
||||
override fun surfaceChanged(holder: SurfaceHolder,format: Int,width: Int,height: Int) {
|
||||
if(width>0 && height>0) window.decorView.post {startIfReady()}
|
||||
}
|
||||
override fun surfaceChanged(holder: SurfaceHolder,format: Int,width: Int,height: Int) {}
|
||||
override fun surfaceDestroyed(holder: SurfaceHolder) {runner?.cancel()}
|
||||
})
|
||||
}},modifier=Modifier.fillMaxWidth().height(220.dp))
|
||||
@@ -82,8 +84,9 @@ class CameraDiagnosticsActivity: ComponentActivity() {
|
||||
}
|
||||
private fun startIfReady() {
|
||||
val holder=readyHolder ?: return
|
||||
if(!holder.surface.isValid || holder.isCreating || holder.surfaceFrame.width()<=0 || holder.surfaceFrame.height()<=0) return
|
||||
if(runner==null && foreground) {
|
||||
runner=CameraProbeController(this,this,holder,::sendReport);runner!!.start()
|
||||
runner=CameraProbeController(this,this,holder,done=::sendReport);runner!!.start()
|
||||
}
|
||||
}
|
||||
override fun onResume() {super.onResume();hideSystemBars(window);foreground=true;startIfReady()}
|
||||
|
||||
@@ -48,6 +48,9 @@ data class DiagnosticReportInfo(
|
||||
val cameraProbeStage: CameraProbeStage=CameraProbeStage.IDLE,
|
||||
val cameraProbeEntryFailure: CameraProbeFailure=CameraProbeFailure.NONE,
|
||||
val cameraProbes: List<CameraProbeResult> = emptyList(),
|
||||
val cameraProbeIsolated: Boolean=false,
|
||||
val cameraProbeRunLimitMs: Int=8000,val cameraProbeCloseLimitMs: Int=8000,
|
||||
val cameraProbeExitLimitMs: Int=0,
|
||||
val cameraAnalysis: CameraAnalysisSnapshot = CameraAnalysisSnapshot(),
|
||||
val faceTransmission: FaceTransmissionSnapshot = FaceTransmissionSnapshot(),
|
||||
val presence: PresenceDiagnosticsSnapshot = PresenceDiagnosticsSnapshot(),
|
||||
@@ -91,6 +94,10 @@ object DiagnosticReportUpload {
|
||||
.put("native_operation",r.nativeOperation.name.lowercase()).put("native_call_state",r.nativeCallState.name.lowercase())
|
||||
.put("native_call_started_ms",r.nativeCallStartedMs).put("native_call_returned_ms",r.nativeCallReturnedMs)
|
||||
.put("initial_failure",r.initialFailure.name.lowercase())
|
||||
.put("worker_pid",r.workerPid).put("process_stop_required",r.processStopRequired)
|
||||
.put("process_exit_confirmed",r.processExitConfirmed)
|
||||
.put("surface_valid_before_attach",r.surfaceValidBeforeAttach).put("surface_valid_before_start",r.surfaceValidBeforeStart)
|
||||
.put("surface_creating_at_attach",r.surfaceCreatingAtAttach).put("surface_width",r.surfaceWidth).put("surface_height",r.surfaceHeight)
|
||||
.put("frame_observation",if(r.mode==CameraProbeMode.CAMERA1_DEFAULT_PREVIEW) "not_sampled" else "callback_counted")
|
||||
.put("mode",r.mode.name.lowercase()).put("failure",r.failure.name.lowercase())
|
||||
.put("started_ms",r.startedMs).put("finished_ms",r.finishedMs).put("bound_ms",r.boundMs)
|
||||
@@ -106,7 +113,9 @@ object DiagnosticReportUpload {
|
||||
.put("closed_ms",r.closedMs).put("close_confirmed",r.closeConfirmed).put("preview_trace",previewTrace(r.previewTrace)))}
|
||||
return JSONObject().put("stage",info.cameraProbeStage.name.lowercase())
|
||||
.put("entry_failure",info.cameraProbeEntryFailure.name.lowercase()).put("max_tests",CameraProbeMode.entries.size)
|
||||
.put("run_limit_ms",8000).put("close_limit_ms",8000).put("results",rows)
|
||||
.put("process_isolated",info.cameraProbeIsolated)
|
||||
.put("run_limit_ms",info.cameraProbeRunLimitMs).put("close_limit_ms",info.cameraProbeCloseLimitMs)
|
||||
.put("process_exit_limit_ms",info.cameraProbeExitLimitMs).put("results",rows)
|
||||
}
|
||||
|
||||
/** Result arrives on an OkHttp worker (or immediately if setup fails). */
|
||||
|
||||
@@ -29,7 +29,8 @@ import java.util.concurrent.atomic.AtomicLong
|
||||
@androidx.annotation.OptIn(markerClass=[androidx.camera.camera2.interop.ExperimentalCamera2Interop::class])
|
||||
@Suppress("DEPRECATION")
|
||||
class CameraProbeController(private val context: Context,private val owner: LifecycleOwner,
|
||||
private val holder: SurfaceHolder,private val done: ()->Unit) {
|
||||
private val holder: SurfaceHolder,private val config: CameraProbeConfig=CameraProbeConfig(),
|
||||
private val onSnapshot: (CameraProbeResult)->Unit={},private val done: ()->Unit) {
|
||||
private val main=Handler(Looper.getMainLooper())
|
||||
private var generation=0L
|
||||
@Volatile private var active: Run?=null
|
||||
@@ -42,10 +43,10 @@ class CameraProbeController(private val context: Context,private val owner: Life
|
||||
private val watchTick=object: Runnable { override fun run() {
|
||||
if(notified.get()) return
|
||||
val run=active
|
||||
if(run!=null && !run.retired && SystemClock.elapsedRealtime()-run.result.startedMs>=8_000) {
|
||||
if(run!=null && !run.retired && SystemClock.elapsedRealtime()-run.result.startedMs>=config.runLimitMs) {
|
||||
finishRun(run)
|
||||
}
|
||||
if(run!=null && run.retired && closeAt>=0 && SystemClock.elapsedRealtime()-closeAt>=8_000) {
|
||||
if(run!=null && run.retired && closeAt>=0 && SystemClock.elapsedRealtime()-closeAt>=config.closeLimitMs) {
|
||||
// Still works if CameraX unbind or the camera worker is blocked.
|
||||
run.update {it.copy(failure=CameraProbeFailure.CLOSE_TIMEOUT,initialFailure=if(it.initialFailure==CameraProbeFailure.NONE) CameraProbeFailure.CLOSE_TIMEOUT else it.initialFailure)}
|
||||
publish(run);event(run,"close_watchdog_blocked");finish(true)
|
||||
@@ -82,10 +83,10 @@ class CameraProbeController(private val context: Context,private val owner: Life
|
||||
if(context.checkSelfPermission(Manifest.permission.CAMERA)!=PackageManager.PERMISSION_GRANTED) {
|
||||
CameraProbeStore.entryFailure=CameraProbeFailure.PERMISSION;finish(false);return
|
||||
}
|
||||
if(cancelled || nextIndex>=CameraProbeMode.entries.size) {finish(false);return}
|
||||
if(cancelled || nextIndex>=config.modes.size) {finish(false);return}
|
||||
val run=synchronized(progressLock) {
|
||||
if(notified.get() || cancelled) return
|
||||
Run(++generation,CameraProbeMode.entries[nextIndex++]).also {
|
||||
Run(++generation,config.modes[nextIndex++]).also {
|
||||
active=it;closeAt=-1;CameraProbeStore.stage=CameraProbeStage.RUNNING
|
||||
}
|
||||
}
|
||||
@@ -106,6 +107,9 @@ class CameraProbeController(private val context: Context,private val owner: Life
|
||||
private fun startLegacy(run: Run) {
|
||||
run.worker.post {
|
||||
if(run.retired) return@post
|
||||
if(!holder.surface.isValid || holder.isCreating || holder.surfaceFrame.width()<=0 || holder.surfaceFrame.height()<=0) {
|
||||
error(run,CameraProbeFailure.SURFACE);return@post
|
||||
}
|
||||
var camera: Camera?=null
|
||||
var failure=CameraProbeFailure.OPEN
|
||||
try {
|
||||
@@ -134,6 +138,10 @@ class CameraProbeController(private val context: Context,private val owner: Life
|
||||
nativeCall(run,CameraNativeOperation.SET_ERROR_CALLBACK) {
|
||||
camera.setErrorCallback {_,_->error(run,CameraProbeFailure.CAMERA_ERROR)}
|
||||
}
|
||||
val surfaceReady=holder.surface.isValid && !holder.isCreating && holder.surfaceFrame.width()>0 && holder.surfaceFrame.height()>0
|
||||
run.update {it.copy(surfaceValidBeforeAttach=surfaceReady,surfaceCreatingAtAttach=holder.isCreating,
|
||||
surfaceWidth=holder.surfaceFrame.width(),surfaceHeight=holder.surfaceFrame.height())}
|
||||
if(!surfaceReady) {error(run,CameraProbeFailure.SURFACE);return@post}
|
||||
nativeCall(run,CameraNativeOperation.ATTACH_SURFACE) {camera.setPreviewDisplay(holder)}
|
||||
if(!baseline) {
|
||||
val expected=actual.width*actual.height*3/2
|
||||
@@ -150,6 +158,8 @@ class CameraProbeController(private val context: Context,private val owner: Life
|
||||
nativeCall(run,CameraNativeOperation.ADD_BUFFERS) {repeat(3) {camera.addCallbackBuffer(ByteArray(expected))}}
|
||||
}
|
||||
if(run.retired) return@post
|
||||
run.update {it.copy(surfaceValidBeforeStart=holder.surface.isValid && !holder.isCreating)}
|
||||
if(!run.result.surfaceValidBeforeStart) {error(run,CameraProbeFailure.SURFACE);return@post}
|
||||
nativeCall(run,CameraNativeOperation.START_PREVIEW) {camera.startPreview()}
|
||||
main.post {if(valid(run)) {
|
||||
val now=SystemClock.elapsedRealtime()
|
||||
@@ -272,7 +282,7 @@ class CameraProbeController(private val context: Context,private val owner: Life
|
||||
if(!run.retired && context.checkSelfPermission(Manifest.permission.CAMERA)!=PackageManager.PERMISSION_GRANTED) {
|
||||
cancelled=true;close(run,CameraProbeFailure.PERMISSION)
|
||||
}
|
||||
if(!run.retired && now-run.result.startedMs>=8_000) finishRun(run)
|
||||
if(!run.retired && now-run.result.startedMs>=config.runLimitMs) finishRun(run)
|
||||
if(run.retired) {
|
||||
val safe=probeCloseSafe(run.mode.legacy,run.result.boundMs>=0,
|
||||
run.result.nativeState==CameraNativeState.CLOSED,run.result.surfaceCreated,
|
||||
@@ -282,7 +292,7 @@ class CameraProbeController(private val context: Context,private val owner: Life
|
||||
run.update {it.copy(closedMs=now,closeConfirmed=true)};publish(run);event(run,"close_confirmed")
|
||||
detach(run);active=null;main.removeCallbacks(this);next();return
|
||||
}
|
||||
if(now-closeAt>=8_000) {
|
||||
if(now-closeAt>=config.closeLimitMs) {
|
||||
run.update {it.copy(failure=CameraProbeFailure.CLOSE_TIMEOUT,initialFailure=if(it.initialFailure==CameraProbeFailure.NONE) CameraProbeFailure.CLOSE_TIMEOUT else it.initialFailure)};publish(run);event(run,"close_blocked")
|
||||
// Retain producer-owned Surface/blocked worker; never force release or start next API.
|
||||
run.observer?.let {run.states?.removeObserver(it)}
|
||||
@@ -341,6 +351,7 @@ class CameraProbeController(private val context: Context,private val owner: Life
|
||||
frameHeight=if(run.height>0) run.height else it.frameHeight,callbackBytes=run.bytes,
|
||||
previewTrace=run.sink?.trace() ?: it.previewTrace)}
|
||||
CameraProbeStore.put(run.result)
|
||||
onSnapshot(run.result)
|
||||
}
|
||||
private fun event(run: Run,event: String) {
|
||||
val msg="camera_probe event=$event mode=${run.mode.name.lowercase()} elapsed_ms=${SystemClock.elapsedRealtime()-run.result.startedMs} frames=${run.frames.get()} native=${run.result.nativeState.name.lowercase()} failure=${run.result.failure.name.lowercase()} operation=${run.result.nativeOperation.name.lowercase()} call_state=${run.result.nativeCallState.name.lowercase()}"
|
||||
@@ -355,6 +366,9 @@ class CameraProbeController(private val context: Context,private val owner: Life
|
||||
main.removeCallbacks(tick);watchdog.removeCallbacks(watchTick);watchThread.quitSafely()
|
||||
done()
|
||||
}
|
||||
fun endSampling() {
|
||||
active?.let { if(!it.retired && !notified.get()) finishRun(it) }
|
||||
}
|
||||
fun cancel() {
|
||||
cancelled=true
|
||||
val run=active
|
||||
|
||||
@@ -14,7 +14,17 @@ enum class CameraNativeOperation { NONE, ENUMERATE, OPEN, GET_PARAMETERS, SET_PA
|
||||
enum class CameraNativeCallState { NONE, PENDING, RETURNED, THREW }
|
||||
enum class CameraProbeStage { IDLE, RUNNING, CLOSING, DONE, BLOCKED }
|
||||
enum class CameraProbeFailure { NONE, PERMISSION, NO_FRONT, UNSUPPORTED_SIZE, PROVIDER, BIND, OPEN,
|
||||
START, CAMERA_ERROR, SURFACE, NATIVE_TIMEOUT, FRAME_TIMEOUT, CLOSE_TIMEOUT, CANCELLED }
|
||||
START, CAMERA_ERROR, SURFACE, NATIVE_TIMEOUT, FRAME_TIMEOUT, CLOSE_TIMEOUT, CANCELLED,
|
||||
WORKER_START_TIMEOUT, WORKER_DIED, PROCESS_EXIT_TIMEOUT }
|
||||
data class CameraProbeConfig(
|
||||
val modes: List<CameraProbeMode> = CameraProbeMode.entries.toList(),
|
||||
val runLimitMs: Long = 8_000, val closeLimitMs: Long = 8_000,
|
||||
) {
|
||||
init {
|
||||
require(modes.isNotEmpty() && modes.size <= 7 && modes.distinct().size == modes.size)
|
||||
require(runLimitMs in 1_000..8_000 && closeLimitMs in 500..8_000)
|
||||
}
|
||||
}
|
||||
data class CameraProbeResult(val mode: CameraProbeMode,val startedMs: Long,
|
||||
val finishedMs: Long=-1,val closeStartedMs: Long=-1,val closeReturnedMs: Long=-1,
|
||||
val closedMs: Long=-1,val firstFrameMs: Long=-1,val frames: Int=0,val analysisReturned: Int=0,
|
||||
@@ -30,7 +40,11 @@ data class CameraProbeResult(val mode: CameraProbeMode,val startedMs: Long,
|
||||
val nativeOperation: CameraNativeOperation=CameraNativeOperation.NONE,
|
||||
val nativeCallState: CameraNativeCallState=CameraNativeCallState.NONE,
|
||||
val nativeCallStartedMs: Long=-1,val nativeCallReturnedMs: Long=-1,
|
||||
val previewTrace: CameraPreviewTrace=CameraPreviewTrace(),val failure: CameraProbeFailure=CameraProbeFailure.NONE)
|
||||
val previewTrace: CameraPreviewTrace=CameraPreviewTrace(),val failure: CameraProbeFailure=CameraProbeFailure.NONE,
|
||||
val workerPid: Int=-1,val processStopRequired: Boolean=false,val processExitConfirmed: Boolean=false,
|
||||
val surfaceValidBeforeAttach: Boolean=false,val surfaceValidBeforeStart: Boolean=false,
|
||||
val surfaceCreatingAtAttach: Boolean=false,val surfaceWidth: Int=0,val surfaceHeight: Int=0,
|
||||
) : java.io.Serializable
|
||||
|
||||
/** One foreground suite per process at a time. A timed-out close keeps normal camera inhibited. */
|
||||
object CameraProbeStore {
|
||||
|
||||
@@ -114,7 +114,7 @@ class FaceTransmissionDiagnostics {
|
||||
/** Counters are recorded before potentially blocking GL calls, sampled by a separate watchdog. */
|
||||
data class CameraPreviewTrace(val available: Int=0,val latchStarted: Int=0,val latchCompleted: Int=0,
|
||||
val firstAvailableMs: Long=-1,val latchStartedMs: Long=-1,val latchCompletedMs: Long=-1,
|
||||
val heartbeatMs: Long=-1,val resultAcks: Int=0,val resultCode: Int=-1,val closed: Boolean=false)
|
||||
val heartbeatMs: Long=-1,val resultAcks: Int=0,val resultCode: Int=-1,val closed: Boolean=false) : java.io.Serializable
|
||||
class CameraPreviewMetrics {
|
||||
private var value=CameraPreviewTrace()
|
||||
@Synchronized fun available(now: Long): Boolean { val first=value.available==0
|
||||
|
||||
Reference in New Issue
Block a user