Skip to content
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

fix(liveness): Fix oval render when switching cameras #5954

Merged
merged 9 commits into from
Oct 31, 2024

Conversation

esauerbo
Copy link
Contributor

@esauerbo esauerbo commented Oct 24, 2024

Description of changes

  • Fix for an issue where the oval renders in the wrong location when switching cameras to unblock data collection
  • Add an event listener to make sure video metadata (which includes video.videoHeight and video.videoWidth) is loaded before attempting to draw the oval
  • Remove drawStaticOval from state machine because we already call it in the LivenessCameraModule. Calling from multiple places caused weird/unpredictable behavior

Issue #, if available

Description of how you validated changes

Checklist

  • Have read the Pull Request Guidelines
  • PR description included
  • yarn test passes and tests are updated/added
  • PR title and commit messages follow conventional commit syntax
  • If this change should result in a version bump, changeset added (This can be done after creating the PR.) This does not apply to changes made to docs, e2e, examples, or other private packages.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@esauerbo esauerbo requested a review from a team as a code owner October 24, 2024 20:14
Copy link

changeset-bot bot commented Oct 24, 2024

⚠️ No Changeset found

Latest commit: 61308ec

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

jordanvn
jordanvn previously approved these changes Oct 24, 2024
Copy link
Member

@jordanvn jordanvn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me

Comment on lines 284 to 289
if (isMetadataLoaded) {
send({
type: 'UPDATE_DEVICE_AND_STREAM',
data: { newDeviceId, newStream },
});
}
Copy link
Member

@calebpollman calebpollman Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this function run if setIsMetaDataLoaded is set to false in the same closure on line 275?

Copy link
Contributor Author

@esauerbo esauerbo Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is updated now so it just sets IsMetaDataLoaded to false. I took out the step where it draws the oval from the state machine since we're already drawing it in the camera component.

Comment on lines 167 to 171
canvasRef?.current &&
videoRef?.current &&
videoStream &&
isStartView &&
isMetadataLoaded
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concerned abt fragility/maintainability with so many boolean values used for a condition

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah fair, maybe we can extract them into one variable for readability?


React.useLayoutEffect(() => {
if (isCameraReady) {
if (isCameraReady && isMetadataLoaded) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using isMetadataLoaded to gate behavior in three different side effects creates complexity in readability and refactoring, can we isolate some of this behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be hard to do without a lot of refactoring. But I was actually able to remove this particular gate

@@ -258,7 +269,9 @@ describe('LivenessCameraModule', () => {
/>
);
const videoEl = screen.getByTestId('video');
videoEl.dispatchEvent(new Event('canplay'));
await waitFor(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for fixing these!

reesscot
reesscot previously approved these changes Oct 30, 2024
calebpollman
calebpollman previously approved these changes Oct 31, 2024
Comment on lines -172 to 180
if (
e.matches &&
canvasRef?.current &&
videoRef?.current &&
videoStream &&
isStartView
) {
drawStaticOval(canvasRef.current, videoRef.current, videoStream);
if (e.matches && shouldDrawOval) {
drawStaticOval(canvasRef.current, videoRef.current!, videoStream);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup!

Comment on lines 198 to 199
canvasRef,
videoRef,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to track the refs in the dependency array, their references maintain stability since we access .current inside the body of the hook

Suggested change
canvasRef,
videoRef,

Copy link
Contributor Author

@esauerbo esauerbo Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed canvasRef but react gets mad if we take out videoRef, I think because the ref itself might change since it comes from the useMediaStreamInVideo hook

const { videoRef, videoWidth, videoHeight } = useMediaStreamInVideo(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh got it. For sure out of scope for this PR (and tbh think it will be a non-trivial change) but we should refactor to keep videoRef scoped to wherever it's used directly. Passing refs around leads to unexpected behaviors

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah makes sense I'll add that to our backlog. It definitely made this like 10x harder to fix

calebpollman
calebpollman previously approved these changes Oct 31, 2024
reesscot
reesscot previously approved these changes Oct 31, 2024
@esauerbo esauerbo merged commit abbf118 into liveness/alpha Oct 31, 2024
32 checks passed
@esauerbo esauerbo deleted the fix-oval-render branch October 31, 2024 20:38
esauerbo added a commit that referenced this pull request Nov 1, 2024
* chore(liveness): allow selecting all cameras, allow camera selection … (#5833)

* chore(liveness): allow selecting all cameras, allow camera selection on mobile

* fix(liveness): fix camera select showing up with one camera, fix camera changing weirdness with hair check screen (#5845)

* fix(liveness): Only apply transform style on user-facing video (#5953)

* fix(liveness): Fix oval render when switching cameras  (#5954)

---------

Co-authored-by: thaddmt <68032955+thaddmt@users.noreply.github.com>
Co-authored-by: Caleb Pollman <cpollman@amazon.com>
Co-authored-by: Emma Sauerborn <70536670+esauerbo@users.noreply.github.com>
Co-authored-by: Scott Rees <6165315+reesscot@users.noreply.github.com>
@ayush987goyal ayush987goyal mentioned this pull request Nov 6, 2024
5 tasks
esauerbo added a commit that referenced this pull request Nov 6, 2024
* chore(liveness): allow selecting all cameras, allow camera selection … (#5833)

* chore(liveness): allow selecting all cameras, allow camera selection on mobile

* only show on mobile with facemovement challenge only

* relax e2e tests on alpha

* make more reaadable

* fix

* update names

* refactor, add tests

* Update packages/react-liveness/src/components/FaceLivenessDetector/LivenessCheck/LivenessCameraModule.tsx

Co-authored-by: Caleb Pollman <cpollman@amazon.com>

* refactor

---------

Co-authored-by: Caleb Pollman <cpollman@amazon.com>

* fix(liveness): fix camera select showing up with one camera, fix camera changing weirdness with hair check screen (#5845)

* fix(liveness): Only apply transform style on user-facing video (#5953)

Co-authored-by: Scott Rees <6165315+reesscot@users.noreply.github.com>

* fix(liveness): Fix oval render when switching cameras  (#5954)

* chore(liveness): update video constraints for selected camera (#6009)

---------

Co-authored-by: thaddmt <68032955+thaddmt@users.noreply.github.com>
Co-authored-by: Caleb Pollman <cpollman@amazon.com>
Co-authored-by: Emma Sauerborn <70536670+esauerbo@users.noreply.github.com>
Co-authored-by: Scott Rees <6165315+reesscot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants