fix: Fix getting triple devices with useCameraDevice (#2074)

This commit is contained in:
Marc Rousavy 2023-10-24 15:48:39 +02:00 committed by GitHub
parent 8d65d1d53c
commit 40268c4a05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,8 +36,6 @@ export interface DeviceFilter {
* ``` * ```
*/ */
export function getCameraDevice(devices: CameraDevice[], position: CameraPosition, filter: DeviceFilter = {}): CameraDevice | undefined { export function getCameraDevice(devices: CameraDevice[], position: CameraPosition, filter: DeviceFilter = {}): CameraDevice | undefined {
const explicitlyWantsNonWideAngle = filter.physicalDevices != null && !filter.physicalDevices.includes('wide-angle-camera')
const filtered = devices.filter((d) => d.position === position) const filtered = devices.filter((d) => d.position === position)
let bestDevice = filtered[0] let bestDevice = filtered[0]
@ -52,19 +50,10 @@ export function getCameraDevice(devices: CameraDevice[], position: CameraPositio
if (bestDevice.hardwareLevel === 'full') leftPoints += 4 if (bestDevice.hardwareLevel === 'full') leftPoints += 4
if (device.hardwareLevel === 'full') rightPoints += 4 if (device.hardwareLevel === 'full') rightPoints += 4
if (!explicitlyWantsNonWideAngle) { if (filter.physicalDevices != null) {
// prefer wide-angle-camera as a default // user did pass a physical device filter, two possible scenarios:
if (bestDevice.physicalDevices.includes('wide-angle-camera')) leftPoints += 2
if (device.physicalDevices.includes('wide-angle-camera')) rightPoints += 2
// if we have more than one device, we rank it lower. we only want a simple camera
if (bestDevice.physicalDevices.length > device.physicalDevices.length) leftPoints -= 1
if (device.physicalDevices.length > bestDevice.physicalDevices.length) rightPoints -= 1
}
// compare devices. two possible scenarios:
// 1. user wants all cameras ([ultra-wide, wide, tele]) to zoom. prefer those devices that have all 3 cameras. // 1. user wants all cameras ([ultra-wide, wide, tele]) to zoom. prefer those devices that have all 3 cameras.
// 2. user wants only one ([wide]) for faster performance. prefer those devices that only have one camera, if they have more, we rank them lower. // 2. user wants only one ([wide]) for faster performance. prefer those devices that only have one camera, if they have more, we rank them lower.
if (filter.physicalDevices != null) {
for (const d of bestDevice.physicalDevices) { for (const d of bestDevice.physicalDevices) {
if (filter.physicalDevices.includes(d)) leftPoints += 1 if (filter.physicalDevices.includes(d)) leftPoints += 1
else leftPoints -= 1 else leftPoints -= 1
@ -73,6 +62,13 @@ export function getCameraDevice(devices: CameraDevice[], position: CameraPositio
if (filter.physicalDevices.includes(d)) rightPoints += 1 if (filter.physicalDevices.includes(d)) rightPoints += 1
else rightPoints -= 1 else rightPoints -= 1
} }
} else {
// user did not pass a physical device filter. prefer wide-angle-camera as a default
if (bestDevice.physicalDevices.includes('wide-angle-camera')) leftPoints += 2
if (device.physicalDevices.includes('wide-angle-camera')) rightPoints += 2
// if we have more than one device, we rank it lower. we only want a simple camera
if (bestDevice.physicalDevices.length > device.physicalDevices.length) leftPoints -= 1
if (device.physicalDevices.length > bestDevice.physicalDevices.length) rightPoints -= 1
} }
if (rightPoints > leftPoints) bestDevice = device if (rightPoints > leftPoints) bestDevice = device