Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create persistent CaptureSession to avoid any blackscreen issues or errors #2494

Merged
merged 60 commits into from
Feb 6, 2024

Conversation

mrousavy
Copy link
Owner

@mrousavy mrousavy commented Feb 1, 2024

What

Rewrites the CameraSession to use a PersistentCameraCaptureSession instead of a CameraDevice and CameraCaptureSession combination to avoid the need of dynamically creating and dropping CameraCaptureSession instances as it is currently.

Before:

private var device: CameraDevice? = null
private var session: CameraCaptureSession? = null
// ^ both could get destroyed by Android, go `null`, go out of sync, etc...

After:

private var session: PersistentCameraCaptureSession
// ^ is always there, I can just set what I need and it internally figures it out

In VisionCamera's CameraSession, we have the following dependency graph:

graph TD;

isActive[isActive: Boolean]-->run;
id[cameraId: String]-->device(cameraDevice: CameraDevice);
options[cameraConfig: CameraConfig]-->request[captureRequest: CaptureRequest];
id-->request;
device-->session(cameraSession: CameraCaptureSession);
outputs["outputs: List<SurfaceOutput>"]-->session;

request-->run["start() / stop()"];
session-->run;

Loading

Important to note is that CameraDevice and CameraCaptureSession are asynchronous resources, meaning two things:

  1. Creating them takes time and is async (suspend) (which is fine)
  2. They can be closed or disconnected by the OS at any point, in which we need to also update their dependant values and tear down the camera session with a chance of rebuilding it again later.

See #2495 for more details

Changes

  • Creates PersistentCameraCaptureSession which can auto-recover devices in a persistent class. This makes the code much simpler and safer
  • Uses PersistentCameraCaptureSession instead of CameraDevice/CameraCaptureSession
  • Uses abortCaptures() when tearing down the session so that completion callbacks are called
  • Skips configuration if preview is null

Tested on

Related issues

Copy link

vercel bot commented Feb 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-native-vision-camera ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 6, 2024 1:19pm

@mrousavy mrousavy force-pushed the feat/custom-session branch from 08b0f7e to c8380ff Compare February 1, 2024 10:56
@mrousavy mrousavy force-pushed the feat/custom-session branch from c8380ff to d5e14ea Compare February 1, 2024 11:44
@mrousavy mrousavy changed the title feat: Create custom CaptureSession wrapper feat: Create custom CaptureSession wrapper: PersistentCameraCaptureSession Feb 1, 2024
@mrousavy mrousavy changed the title feat: Create custom CaptureSession wrapper: PersistentCameraCaptureSession feat: Create persistent CaptureSession to avoid any blackscreen issues or errors Feb 1, 2024
@mrousavy mrousavy marked this pull request as ready for review February 6, 2024 12:48
@mrousavy
Copy link
Owner Author

mrousavy commented Feb 6, 2024

holy shit I think it works

@mrousavy mrousavy merged commit 5acc64e into main Feb 6, 2024
11 of 12 checks passed
@mrousavy mrousavy deleted the feat/custom-session branch February 6, 2024 13:19
isaaccolson pushed a commit to isaaccolson/deliveries-mobile that referenced this pull request Oct 30, 2024
…ues or errors (mrousavy#2494)

* feat: Create custom `CaptureSession` wrapper

* Create `PersistentCameraCaptureSession`

* Update VideoStabilizationMode.kt

* Create RepeatingRequest.kt

* Update CaptureSession.kt

* Delete CaptureSession.kt

* Update PersistentCameraCaptureSession.kt

* Update PersistentCameraCaptureSession.kt

* fix: Add `isRepeating`

* Update CameraSession.kt

* Make `SurfaceOutput` not `Closable` anymore

* Update PersistentCameraCaptureSession.kt

* Stub out the rest

* Format

* Set `isRunning` properly

* Close previous outputs

* onError callback

* Format

* Started/Stopped

* Update CameraPage.tsx

* Add `isValid`

* Log `isActive`

* Add `tryAbortCaptures`

* Configure()

* Try?

* Add `didDestroyFromOutside`

* Disable FP for testing

* fix: Call `super.onAttachedToWindow` first

* Hm

* Update CameraSession.kt

* Update PersistentCameraCaptureSession.kt

* Try catch `didDestroyFromOutside`

* Update PersistentCameraCaptureSession.kt

* Session can only be active with a preview

* Update PersistentCameraCaptureSession.kt

* Throw `no-outputs` if needed

* Update logs

* fix: Check for CAMERA permission

* fix: Close session when opening a new device

* perf: Make everything `by lazy` in CameraDeviceDetails

* Update CameraDeviceDetails.kt

* Update PersistentCameraCaptureSession.kt

* Update PersistentCameraCaptureSession.kt

* Move

* Update Podfile.lock

* Implement `capture()`

* Format

* fix: Fix orientation not being applied

* fix: Fix `isMirrored`

* fix: Fix getting size

* fix: Close `Surface` in `VideoPipeline`

* Format

* fix: Fix `VideoPipeline` not properly destroying itself

* Use FP again

* Update CameraConfiguration.kt

* Rename

* Clean up

* Format

* Update CameraConfiguration.kt

* fix: Don't stop repeating request when capturing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment