Skip to content

fix(android): treat ERROR_SOURCE_INACTIVE as a successful finish (don't discard a valid file)#4042

Open
radko93 wants to merge 1 commit into
mrousavy:mainfrom
radko93:fix/android-source-inactive-keep-recording
Open

fix(android): treat ERROR_SOURCE_INACTIVE as a successful finish (don't discard a valid file)#4042
radko93 wants to merge 1 commit into
mrousavy:mainfrom
radko93:fix/android-source-inactive-keep-recording

Conversation

@radko93

@radko93 radko93 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Problem

On Android, when a recording finalizes with VideoRecordEvent.Finalize.ERROR_SOURCE_INACTIVE, HybridVideoRecorder reports it via onRecordingError, so callers treat the recording as failed and discard it.

But ERROR_SOURCE_INACTIVE is not a no-output error. CameraX finalizes a valid, playable file (containing every frame produced before the source went inactive) before delivering the Finalize event. This commonly fires when the host Activity is stopped/destroyed mid-recording during normal app lifecycle (backgrounding, deep-link relaunch, config change) — so the user did record usable footage, and it's being thrown away.

Evidence it produces a usable file

Per the CameraX VideoRecordEvent.Finalize reference:

  • ERROR_SOURCE_INACTIVE: "The recording failed because the source becomes inactive and stops sending frames. One case is that if camera is closed due to lifecycle stopped, the active recording will be finalized with this error, and the output will be generated, containing the frames produced before camera closing."
  • ERROR_NO_VALID_DATA: "… The application will need to clean up the output file, such as deleting the file."

So ERROR_SOURCE_INACTIVE ⇒ usable file; ERROR_NO_VALID_DATA ⇒ no usable file.

Precedent

expo-camera treats ERROR_SOURCE_INACTIVE as a success and resolves with event.outputResults.outputUri — see ExpoCameraView.kt L382-L394. This change brings VisionCamera in line with that contract.

The intent to keep the file even predates the Nitro rewrite. VisionCamera v4's VideoRecordEvent+toCameraError.kt L13-L20 grouped this code under a comment that reads "errors where the recording still gets saved (so we can probably ignore them)" — but mapped it to NoDataError(cause) (wasVideoRecorded = false), so CameraSession+Video.kt L67-L76 still took the fatal branch and dropped the file. The Nitro rewrite collapsed the wasVideoRecorded distinction, so the file is lost either way — this PR realizes the originally-intended behavior.

Change

Map ERROR_SOURCE_INACTIVE to a successful finish (RecordingFinishedReason.STOPPED). ERROR_NO_VALID_DATA and other no-output errors stay on the error path.

Open question: reuse STOPPED, or would you prefer a dedicated RecordingFinishedReason.SOURCE_INACTIVE so callers can distinguish a lifecycle-truncated recording from a clean stop while still receiving the file? Happy to adjust.

Testing

ERROR_SOURCE_INACTIVE is delivered by CameraX when the frame source dies, which is impractical to trigger deterministically in the unit harness (needs a real camera/lifecycle teardown). Verified manually on devices (Android 12–16): backgrounding to destroy the Activity mid-recording previously hit onRecordingError with a valid file left on disk; with this change the file is delivered via onRecordingFinished. Happy to add a harness test if you can point me at the right seam to inject a Finalize event with a given error code.

bun run lint-kotlin produces no diff.

CameraX finalizes a valid file on ERROR_SOURCE_INACTIVE (the frames recorded
before the source went inactive — e.g. the Activity is stopped/destroyed
mid-recording), unlike ERROR_NO_VALID_DATA. Routing it to onRecordingError
discards usable footage, so map it to onRecordingFinished(STOPPED) instead,
matching expo-camera's handling. ERROR_NO_VALID_DATA stays fatal.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 24, 2026

Copy link
Copy Markdown

@radko93 is attempting to deploy a commit to the Margelo Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant