Skip to content

Commit b9a3aec

Browse files
committed
Add challengeOption parameter and remove error codes
1 parent 8e6c9d5 commit b9a3aec

File tree

8 files changed

+53
-54
lines changed

8 files changed

+53
-54
lines changed

HostApp/HostApp/Views/ExampleLivenessView.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct ExampleLivenessView: View {
2323
FaceLivenessDetectorView(
2424
sessionID: viewModel.sessionID,
2525
region: "us-east-1",
26-
cameraPosition: .back,
26+
challengeOption: .faceMovementAndLightChallenge,
2727
isPresented: Binding(
2828
get: { viewModel.presentationState == .liveness },
2929
set: { _ in }
@@ -49,8 +49,6 @@ struct ExampleLivenessView: View {
4949
viewModel.presentationState = .error(.invalidSignature)
5050
case .failure(.cameraNotAvailable):
5151
viewModel.presentationState = .error(.cameraNotAvailable)
52-
case .failure(.invalidCameraPositionSelected):
53-
viewModel.presentationState = .error(.invalidCameraPositionSelected)
5452
default:
5553
viewModel.presentationState = .liveness
5654
}
@@ -83,8 +81,6 @@ struct ExampleLivenessView: View {
8381
LivenessCheckErrorContentView.invalidSignature
8482
case .cameraNotAvailable:
8583
LivenessCheckErrorContentView.cameraNotAvailable
86-
case . invalidCameraPositionSelected:
87-
LivenessCheckErrorContentView.invalidCameraPositionSelected
8884
default:
8985
LivenessCheckErrorContentView.unexpected
9086
}

Sources/FaceLiveness/AV/LivenessCaptureDevice.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,3 @@ struct LivenessCaptureDevice {
3737
}
3838
}
3939
}
40-
41-
public enum LivenessCaptureDevicePosition {
42-
case front
43-
case back
44-
}

Sources/FaceLiveness/Views/GetReadyPage/CameraPreviewViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class CameraPreviewViewModel: NSObject, ObservableObject {
1616
@Published var buffer: CVPixelBuffer?
1717

1818
var previewCaptureSession: LivenessCaptureSession?
19-
let cameraPosition: LivenessCaptureDevicePosition
19+
let cameraPosition: LivenessCamera
2020

21-
init(cameraPosition: LivenessCaptureDevicePosition) {
21+
init(cameraPosition: LivenessCamera) {
2222
self.cameraPosition = cameraPosition
2323

2424
super.init()

Sources/FaceLiveness/Views/GetReadyPage/GetReadyPageView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ struct GetReadyPageView: View {
1212
let beginCheckButtonDisabled: Bool
1313
let onBegin: () -> Void
1414
let challenge: Challenge
15-
let cameraPosition: LivenessCaptureDevicePosition
15+
let cameraPosition: LivenessCamera
1616

1717
init(
1818
onBegin: @escaping () -> Void,
1919
beginCheckButtonDisabled: Bool = false,
2020
challenge: Challenge,
21-
cameraPosition: LivenessCaptureDevicePosition
21+
cameraPosition: LivenessCamera
2222
) {
2323
self.onBegin = onBegin
2424
self.beginCheckButtonDisabled = beginCheckButtonDisabled

Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionError.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,7 @@ public struct FaceLivenessDetectionError: Error, Equatable {
129129
public static let cameraNotAvailable = FaceLivenessDetectionError(
130130
code: 18,
131131
message: "The camera is not available.",
132-
recoverySuggestion: "There might be a hardware issue or the selected camera is not available."
133-
)
134-
135-
public static let invalidCameraPositionSelected = FaceLivenessDetectionError(
136-
code: 19,
137-
message: "The camera position selected is incompatible with the liveness challenge type requested.",
138-
recoverySuggestion: "Please ensure the camera position is supported for the liveness challenge type requested."
132+
recoverySuggestion: "There might be a hardware issue."
139133
)
140134

141135
public static func == (lhs: FaceLivenessDetectionError, rhs: FaceLivenessDetectionError) -> Bool {

Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionView.swift

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct FaceLivenessDetectorView: View {
2020
@State var displayingCameraPermissionsNeededAlert = false
2121

2222
let disableStartView: Bool
23-
let cameraPosition: LivenessCaptureDevicePosition
23+
let cameraPosition: LivenessCamera
2424
let onCompletion: (Result<Void, FaceLivenessDetectionError>) -> Void
2525

2626
let sessionTask: Task<FaceLivenessSession, Error>
@@ -30,13 +30,13 @@ public struct FaceLivenessDetectorView: View {
3030
credentialsProvider: AWSCredentialsProvider? = nil,
3131
region: String,
3232
disableStartView: Bool = false,
33-
cameraPosition: LivenessCaptureDevicePosition = .front,
33+
challengeOption: ChallengeOption,
3434
isPresented: Binding<Bool>,
3535
onCompletion: @escaping (Result<Void, FaceLivenessDetectionError>) -> Void
3636
) {
3737
self.disableStartView = disableStartView
3838
self._isPresented = isPresented
39-
self.cameraPosition = cameraPosition
39+
self.cameraPosition = challengeOption.camera
4040
self.onCompletion = onCompletion
4141

4242
self.sessionTask = Task {
@@ -82,7 +82,8 @@ public struct FaceLivenessDetectorView: View {
8282
closeButtonAction: { onCompletion(.failure(.userCancelled)) },
8383
sessionID: sessionID,
8484
isPreviewScreenEnabled: !disableStartView,
85-
cameraPosition: cameraPosition
85+
cameraPosition: challengeOption.camera,
86+
challengeOption: challengeOption
8687
)
8788
)
8889
}
@@ -92,15 +93,15 @@ public struct FaceLivenessDetectorView: View {
9293
credentialsProvider: AWSCredentialsProvider? = nil,
9394
region: String,
9495
disableStartView: Bool = false,
95-
cameraPosition: LivenessCaptureDevicePosition,
96+
challengeOption: ChallengeOption,
9697
isPresented: Binding<Bool>,
9798
onCompletion: @escaping (Result<Void, FaceLivenessDetectionError>) -> Void,
9899
captureSession: LivenessCaptureSession
99100
) {
100101
self.disableStartView = disableStartView
101102
self._isPresented = isPresented
102103
self.onCompletion = onCompletion
103-
self.cameraPosition = cameraPosition
104+
self.cameraPosition = challengeOption.camera
104105

105106
self.sessionTask = Task {
106107
let session = try await AWSPredictionsPlugin.startFaceLivenessSession(
@@ -125,7 +126,8 @@ public struct FaceLivenessDetectorView: View {
125126
closeButtonAction: { onCompletion(.failure(.userCancelled)) },
126127
sessionID: sessionID,
127128
isPreviewScreenEnabled: !disableStartView,
128-
cameraPosition: cameraPosition
129+
cameraPosition: challengeOption.camera,
130+
challengeOption: challengeOption
129131
)
130132
)
131133
}
@@ -243,8 +245,6 @@ public struct FaceLivenessDetectorView: View {
243245
return .socketClosed
244246
case .cameraNotAvailable:
245247
return .cameraNotAvailable
246-
case .invalidCameraPositionSelecteed:
247-
return .invalidCameraPositionSelected
248248
default:
249249
return .cameraPermissionDenied
250250
}
@@ -285,7 +285,7 @@ public struct FaceLivenessDetectorView: View {
285285
enum DisplayState: Equatable {
286286
case awaitingChallengeType
287287
case awaitingLivenessSession(Challenge)
288-
case displayingGetReadyView(Challenge, LivenessCaptureDevicePosition)
288+
case displayingGetReadyView(Challenge, LivenessCamera)
289289
case displayingLiveness
290290
case awaitingCameraPermission
291291

@@ -340,3 +340,33 @@ private func map(detectionCompletion: @escaping (Result<Void, FaceLivenessDetect
340340
}
341341
}
342342
}
343+
344+
enum LivenessCamera {
345+
case front
346+
case back
347+
}
348+
349+
public struct ChallengeOption {
350+
let challenge: Challenge
351+
let camera: LivenessCamera
352+
353+
init(challenge: Challenge, camera: LivenessCamera) {
354+
self.challenge = challenge
355+
self.camera = camera
356+
}
357+
358+
public static let faceMovementAndLightChallenge = Self.init(
359+
challenge: .init(version: "2.0.0",
360+
type: .faceMovementAndLightChallenge),
361+
camera: .front)
362+
363+
public static let faceMovementChallengeWithFrontCamera = Self.init(
364+
challenge: .init(version: "1.0.0",
365+
type: .faceMovementAndLightChallenge),
366+
camera: .front)
367+
368+
public static let faceMovementChallengeWithBackCamera = Self.init(
369+
challenge: .init(version: "1.0.0",
370+
type: .faceMovementAndLightChallenge),
371+
camera: .back)
372+
}

Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel.swift

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class FaceLivenessDetectionViewModel: ObservableObject {
4444
var initialClientEvent: InitialClientEvent?
4545
var faceMatchedTimestamp: UInt64?
4646
var noFitStartTime: Date?
47-
let cameraPosition: LivenessCaptureDevicePosition
47+
let cameraPosition: LivenessCamera
48+
let challengeOption: ChallengeOption
4849

4950
static var attemptCount: Int = 0
5051
static var attemptIdTimeStamp: Date = Date()
@@ -66,7 +67,8 @@ class FaceLivenessDetectionViewModel: ObservableObject {
6667
closeButtonAction: @escaping () -> Void,
6768
sessionID: String,
6869
isPreviewScreenEnabled: Bool,
69-
cameraPosition: LivenessCaptureDevicePosition
70+
cameraPosition: LivenessCamera,
71+
challengeOption: ChallengeOption
7072
) {
7173
self.closeButtonAction = closeButtonAction
7274
self.videoChunker = videoChunker
@@ -77,6 +79,7 @@ class FaceLivenessDetectionViewModel: ObservableObject {
7779
self.faceInOvalMatching = faceInOvalMatching
7880
self.isPreviewScreenEnabled = isPreviewScreenEnabled
7981
self.cameraPosition = cameraPosition
82+
self.challengeOption = challengeOption
8083

8184
self.closeButtonAction = { [weak self] in
8285
guard let self else { return }
@@ -127,18 +130,7 @@ class FaceLivenessDetectionViewModel: ObservableObject {
127130
livenessService?.register(
128131
listener: { [weak self] _challenge in
129132
self?.challenge = _challenge
130-
guard _challenge.type == .faceMovementAndLightChallenge,
131-
self?.cameraPosition == .back else {
132-
onChallengeTypeReceived(_challenge)
133-
return
134-
}
135-
136-
// incompatible camera position with challenge type
137-
// return error
138-
DispatchQueue.main.async {
139-
self?.livenessState
140-
.unrecoverableStateEncountered(.invalidCameraPositionSelecteed)
141-
}
133+
onChallengeTypeReceived(_challenge)
142134
},
143135
on: .challenge)
144136
}
@@ -217,14 +209,7 @@ class FaceLivenessDetectionViewModel: ObservableObject {
217209
try livenessService?.initializeLivenessStream(
218210
withSessionID: sessionID,
219211
userAgent: UserAgentValues.standard().userAgentString,
220-
challenges: FaceLivenessSession.supportedChallenges,
221-
222-
// light challenge
223-
// challenges: [.init(version: "2.0.0", type: .faceMovementAndLightChallenge)],
224-
225-
// no light challenge
226-
// challenges: [.init(version: "1.0.0", type: .faceMovementChallenge)],
227-
212+
challenges: [challengeOption.challenge],
228213
options: .init(
229214
attemptCount: Self.attemptCount,
230215
preCheckViewEnabled: isPreviewScreenEnabled)

Sources/FaceLiveness/Views/Liveness/LivenessStateMachine.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ struct LivenessStateMachine {
166166
static let socketClosed = LivenessError(code: 6, webSocketCloseCode: .normalClosure)
167167
static let viewResignation = LivenessError(code: 8, webSocketCloseCode: .viewClosure)
168168
static let cameraNotAvailable = LivenessError(code: 9, webSocketCloseCode: .unexpectedRuntimeError)
169-
static let invalidCameraPositionSelecteed = LivenessError(code: 10, webSocketCloseCode: .unexpectedRuntimeError)
170169

171170
static func == (lhs: LivenessError, rhs: LivenessError) -> Bool {
172171
lhs.code == rhs.code

0 commit comments

Comments
 (0)