Skip to content
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 @@ -54,9 +54,13 @@ import java.util.Date
import java.util.concurrent.Executors
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus

internal typealias OnMuxedSegment = (bytes: ByteArray, timestamp: Long) -> Unit
internal typealias OnChallengeComplete = () -> Unit
Expand All @@ -82,6 +86,7 @@ internal class LivenessCoordinator(

private val attemptCounter = AttemptCounter()
private val analysisExecutor = Executors.newSingleThreadExecutor()
private val coordinatorScope = MainScope() + CoroutineName("LivenessCoordinator")

val livenessState = LivenessState(
sessionId = sessionId,
Expand Down Expand Up @@ -153,7 +158,7 @@ internal class LivenessCoordinator(
}

private fun launchCamera(camera: Camera) {
MainScope().launch {
coordinatorScope.launch {
delay(5_000)
if (!previewTextureView.hasReceivedUpdate) {
val faceLivenessException = FaceLivenessDetectionException(
Expand All @@ -163,7 +168,7 @@ internal class LivenessCoordinator(
processSessionError(faceLivenessException, true)
}
}
MainScope().launch {
coordinatorScope.launch {
getCameraProvider(context).apply {
if (lifecycleOwner.lifecycle.currentState != Lifecycle.State.DESTROYED) {
unbindAll()
Expand Down Expand Up @@ -241,7 +246,8 @@ internal class LivenessCoordinator(
FaceLivenessDetectionException.UnsupportedChallengeTypeException(throwable = error) to true
else -> FaceLivenessDetectionException(
error.message ?: "Unknown error.",
error.recoverySuggestion, error
error.recoverySuggestion,
error
) to false
}
processSessionError(faceLivenessException, shouldStopLivenessSession)
Expand All @@ -250,10 +256,8 @@ internal class LivenessCoordinator(
}

private fun unbindCamera(context: Context) {
MainScope().launch {
getCameraProvider(context).apply {
unbindAll()
}
coordinatorScope.launch(NonCancellable) {
getCameraProvider(context).unbindAll()
}
}

Expand Down Expand Up @@ -294,44 +298,40 @@ internal class LivenessCoordinator(

fun processLivenessCheckComplete() {
livenessState.onLivenessChallengeComplete()
stopEncoder { livenessState.onFullChallengeComplete() }
coordinatorScope.launch {
encoder.stop()
livenessState.onFullChallengeComplete()
}
}

private fun processFinalEventsSent() {
unbindCamera(context)
}

private fun stopEncoder(onComplete: () -> Unit) {
encoder.stop {
MainScope().launch {
onComplete()
}
}
}

/**
* This is only called when onDispose is triggered from FaceLivenessDetector view.
* If we begin calling destroy in other places, we should ensure we are still tracking the proper error code.
*/
fun destroy(context: Context) {
// Destroy all resources so a new coordinator can safely be created
encoder.stop {
coordinatorScope.launch(NonCancellable) {
encoder.stop()
encoder.destroy()
}
val webSocketCloseCode = if (!disconnectEventReceived) WebSocketCloseCode.DISPOSED else null
livenessState.onDestroy(true, webSocketCloseCode)
unbindCamera(context)
analysisExecutor.shutdown()
coordinatorScope.cancel()
}

private suspend fun getCameraProvider(context: Context): ProcessCameraProvider =
suspendCoroutine { continuation ->
ProcessCameraProvider.getInstance(context).also { cameraProvider ->
cameraProvider.addListener({
continuation.resume(cameraProvider.get())
}, ContextCompat.getMainExecutor(context))
}
private suspend fun getCameraProvider(context: Context): ProcessCameraProvider = suspendCoroutine { continuation ->
ProcessCameraProvider.getInstance(context).also { cameraProvider ->
cameraProvider.addListener({
continuation.resume(cameraProvider.get())
}, ContextCompat.getMainExecutor(context))
}
}

companion object {
const val TARGET_FPS_MIN = 24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import androidx.annotation.WorkerThread
import com.amplifyframework.core.Amplify
import com.amplifyframework.ui.liveness.util.isKeyFrame
import java.io.File
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

internal class LivenessVideoEncoder private constructor(
width: Int,
Expand Down Expand Up @@ -212,19 +214,19 @@ internal class LivenessVideoEncoder private constructor(
}
}

fun stop(onComplete: () -> Unit) {
suspend fun stop() = suspendCoroutine { continuation ->
encoderHandler.post {
encoding = false
livenessMuxer?.stop()
livenessMuxer = null
if (LOGGING_ENABLED) {
Log.i(TAG, "Stopping encoder")
}
onComplete()
continuation.resume(Unit)
}
}

fun destroy() {
suspend fun destroy() = suspendCoroutine { continuation ->
encoderHandler.post {
if (LOGGING_ENABLED) {
Log.i(TAG, "Destroying encoder")
Expand All @@ -241,6 +243,8 @@ internal class LivenessVideoEncoder private constructor(
// may already be stopped
}
encoder.release()

continuation.resume(Unit)
}
}
}
2 changes: 1 addition & 1 deletion samples/liveness/app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/build
/buildNative
**/awsconfiguration.json
**/amplifyconfiguration.json
**/amplifyconfiguration**.json