Unverified Commit 2444e9d3 authored by Andrei Eres's avatar Andrei Eres Committed by GitHub
Browse files

Fix camera access checking in firefox (#84)

parent 098505cc
Pipeline #163512 passed with stages
in 1 minute and 10 seconds
......@@ -3,7 +3,32 @@ export async function checkCameraAccess() {
const permission = await navigator.permissions.query({
name: 'camera' as PermissionName,
})
return permission.state == 'granted'
return permission.state === 'granted'
} catch (error) {
// Firefox can't query 'camera' permission
if (isCameraPermissionDescriptorError(error as Error)) {
return await checkIfVideoDeviceHasLabel()
}
console.error(error)
return false
}
}
function isCameraPermissionDescriptorError(error: Error) {
return (
(error as Error).name === 'TypeError' &&
(error as Error).message.startsWith("'camera'")
)
}
// For security reasons, the `label` field is always blank
// unless the user has granted persistent permission for media device access
// @see: https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo
async function checkIfVideoDeviceHasLabel() {
try {
const devices = await navigator.mediaDevices.enumerateDevices()
return devices.some((d) => d.kind === 'videoinput' && d.label)
} catch (error) {
console.error(error)
return false
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment