fix(camera): recover browser preview after permission is granted#41
Open
cn0303 wants to merge 1 commit into
Open
fix(camera): recover browser preview after permission is granted#41cn0303 wants to merge 1 commit into
cn0303 wants to merge 1 commit into
Conversation
The camera preview video element made a single getUserMedia attempt. On first use it can fail before the page has been granted camera permission (the grant is driven separately by useAvailableCameras' probe) or while the device is briefly held right after that probe releases it. Because the deviceId does not change once permission lands, the hook stayed stuck on the error state until a full page reload -- visible on the calibration and recording camera previews as a failed preview / "No browser match". Retry the stream a few times with backoff, and re-attempt when the media device list changes (granting permission fires a devicechange event) but only while in the error state, so a healthy stream is never torn down. The preview now recovers on its own without a reload.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The camera preview (
useCameraStream) makes a singlegetUserMediaattempt. On firstuse that attempt can fail because:
useAvailableCameras' probegetUserMedia), orNotReadableError).Because the bound
deviceIddoesn't change once permission lands, the hook stays stuck in theerror state until a full page reload. This is visible today on:
both showing
Preview failed/No browser matchon the first visit, only fixed by reloading.Steps to reproduce
Fix
NotReadableError/AbortError, i.e. the deviceis briefly held) are retried, with exponential backoff (300ms → 600ms → 1200ms → ...).
Permanent-at-this-instant errors; permission denial (
NotAllowedError), missing device(
NotFoundError), unsatisfiable constraints (OverconstrainedError) — are not spin-retried.devicechangewhich can follow a permission grant or a device-exposure change (the browser doesn't guarantee
timing) and only while already in the error state, so a healthy stream is never torn down by an
unrelated device change (e.g. plugging in a microphone).
The preview now recovers on its own, no reload needed. Behaviour while
pausedis unchanged(stream is still released for cv2 during recording).
Testing
after granting, no reload.
npx tsc --noEmitclean; ESLint clean on the changed file (note: the repo's fullnpm run lintalready reports pre-existing errors/warnings unrelated to this change);
npm run buildsucceeds.