Skip to content

fix: Close CameraSession if the View is removed #2174

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

Merged
merged 4 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ class CameraView(context: Context) :
super.onDetachedFromWindow()
}

fun destroy() {
cameraSession.close()
}

fun update() {
Log.i(TAG, "Updating CameraSession...")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class CameraViewManager : ViewGroupManager<CameraView>() {

override fun getName(): String = TAG

override fun onDropViewInstance(view: CameraView) {
view.destroy()
super.onDropViewInstance(view)
}

@ReactProp(name = "cameraId")
fun setCameraId(view: CameraView, cameraId: String) {
view.cameraId = cameraId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
codeScannerOutput?.close()
codeScannerOutput = null

configuration = null
isRunning = false
}

Expand Down Expand Up @@ -363,16 +364,16 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
}

private fun configureCaptureRequest(config: CameraConfiguration) {
val device = cameraDevice ?: throw NoCameraDeviceError()
val captureSession = captureSession ?: throw CameraNotReadyError()

if (!config.isActive) {
// TODO: Do we want to do stopRepeating() or entirely destroy the session?
// If the Camera is not active, we don't do anything.
captureSession.stopRepeating()
captureSession?.stopRepeating()
return
}

val device = cameraDevice ?: throw NoCameraDeviceError()
val captureSession = captureSession ?: throw CameraNotReadyError()

val previewOutput = previewOutput
if (previewOutput == null) {
Log.w(TAG, "Preview Output is null, aborting...")
Expand Down