Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
#10. Added try catch to createCaptureSession. Then ensuring the camer…
Browse files Browse the repository at this point in the history
…a is released properly and report the error through the listener.
  • Loading branch information
alistairsykes committed Apr 8, 2019
1 parent 80df518 commit 8bee734
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,33 +118,40 @@ internal class Camera2Source(
surfaces: List<Surface>,
listener: OnCameraReadyListener?
) {
cameraDevice?.createCaptureSession(
surfaces, object : CameraCaptureSession.StateCallback() {
override fun onConfigured(session: CameraCaptureSession) {
if (cameraDevice == null) return
@Suppress("UnsafeCallOnNullableType")
val builder = cameraDevice!!.createCaptureRequest(
CameraDevice.TEMPLATE_PREVIEW
)
val autoFocus = selectBestAutoFocus()
if (autoFocus != null) {
builder.set(CaptureRequest.CONTROL_AF_MODE, autoFocus)
try {
cameraDevice?.createCaptureSession(
surfaces, object : CameraCaptureSession.StateCallback() {
override fun onConfigured(session: CameraCaptureSession) {
if (cameraDevice == null) return
@Suppress("UnsafeCallOnNullableType")
val builder = cameraDevice!!.createCaptureRequest(
CameraDevice.TEMPLATE_PREVIEW
)
val autoFocus = selectBestAutoFocus()
if (autoFocus != null) {
builder.set(CaptureRequest.CONTROL_AF_MODE, autoFocus)
}
for (surface in surfaces) {
builder.addTarget(surface)
}
session.setRepeatingRequest(builder.build(), null, null)
}
for (surface in surfaces) {
builder.addTarget(surface)

override fun onConfigureFailed(session: CameraCaptureSession) {
release()
val exception = CameraSessionException()
Timber.e(exception, "Error creating camera session")
listener?.onCameraFailure(exception)
}
session.setRepeatingRequest(builder.build(), null, null)
}

override fun onConfigureFailed(session: CameraCaptureSession) {
release()
val exception = CameraSessionException()
Timber.e(exception, "Error creating camera session")
listener?.onCameraFailure(exception)
}
},
null
)
},
null
)
} catch (e: android.hardware.camera2.CameraAccessException) {
release()
val exception = CameraAccessException()
Timber.e(exception, "Error creating camera session")
listener?.onCameraFailure(exception)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ class CameraServiceException : CameraException()
* @see android.hardware.camera2.CameraCaptureSession.StateCallback.onConfigureFailed
*/
class CameraSessionException : CameraException()

/**
* If the camera device is no longer connected or has encountered a fatal error
*
* @see android.hardware.camera2.CameraDevice.createCaptureSession
*/
class CameraAccessException : CameraException()
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.argumentCaptor
import com.nhaarman.mockitokotlin2.doNothing
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.doThrow
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
Expand Down Expand Up @@ -360,6 +361,23 @@ internal class Camera2SourceTest {
verify(listener).onCameraFailure(any<CameraSessionException>())
}

@Test
fun cameraDevice_configurationException__createCaptureSession__release_callsListener() {
// GIVEN
cameraSource.cameraDevice = cameraDevice
whenever(cameraDevice.createCaptureSession(any(), any(), anyOrNull()))
.doThrow(mock<android.hardware.camera2.CameraAccessException>())

// WHEN
val surfaces = listOf<Surface>(mock(), mock())
val listener = mock<OnCameraReadyListener>()
cameraSource.createCaptureSession(surfaces, listener)

// THEN
verify(cameraSource).release()
verify(listener).onCameraFailure(any<CameraAccessException>())
}

@Test
fun sizesWithAboveAndBelow_minWidth__chooseOutputSize__lowestAboveMin() {
// GIVEN
Expand Down

0 comments on commit 8bee734

Please sign in to comment.