Skip to content

Commit 36f1ac1

Browse files
authored
fix(liveness): Detect failed camera opening (#205)
1 parent 10719e6 commit 36f1ac1

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

liveness/src/main/java/com/amplifyframework/ui/liveness/camera/LivenessCoordinator.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import java.util.concurrent.Executors
5050
import kotlin.coroutines.resume
5151
import kotlin.coroutines.suspendCoroutine
5252
import kotlinx.coroutines.MainScope
53+
import kotlinx.coroutines.delay
5354
import kotlinx.coroutines.launch
5455

5556
internal typealias OnMuxedSegment = (bytes: ByteArray, timestamp: Long) -> Unit
@@ -137,6 +138,16 @@ internal class LivenessCoordinator(
137138
private var disconnectEventReceived = false
138139

139140
init {
141+
MainScope().launch {
142+
delay(5_000)
143+
if (!previewTextureView.hasReceivedUpdate) {
144+
val faceLivenessException = FaceLivenessDetectionException(
145+
"The camera failed to open within the allowed time limit.",
146+
"Ensure the camera is available to use and that no other apps are using it."
147+
)
148+
processSessionError(faceLivenessException, true)
149+
}
150+
}
140151
MainScope().launch {
141152
getCameraProvider(context).apply {
142153
if (lifecycleOwner.lifecycle.currentState != Lifecycle.State.DESTROYED) {

liveness/src/main/java/com/amplifyframework/ui/liveness/camera/PreviewTextureView.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal class PreviewTextureView(
3131
) : TextureView(context) {
3232

3333
private var surface: Surface? = null
34+
internal var hasReceivedUpdate = false
3435

3536
init {
3637
surfaceTextureListener = object : SurfaceTextureListener {
@@ -48,7 +49,11 @@ internal class PreviewTextureView(
4849
}
4950
}
5051

51-
override fun onSurfaceTextureUpdated(surfaceTexture: SurfaceTexture) {}
52+
override fun onSurfaceTextureUpdated(surfaceTexture: SurfaceTexture) {
53+
if (!hasReceivedUpdate) {
54+
hasReceivedUpdate = true
55+
}
56+
}
5257

5358
override fun onSurfaceTextureSizeChanged(
5459
surfaceTexture: SurfaceTexture,

0 commit comments

Comments
 (0)