2021-02-20 09:05:02 -07:00
|
|
|
import { useEffect, useState } from 'react';
|
2021-02-23 00:50:51 -07:00
|
|
|
import type { CameraPosition } from '../CameraPosition';
|
|
|
|
import { sortDevices } from '../utils/FormatFilter';
|
2021-02-20 09:05:02 -07:00
|
|
|
import { Camera } from '../Camera';
|
|
|
|
import { CameraDevice, LogicalCameraDeviceType, parsePhysicalDeviceTypes, PhysicalCameraDeviceType } from '../CameraDevice';
|
|
|
|
|
2021-02-22 01:43:47 -07:00
|
|
|
type CameraDevices = {
|
|
|
|
[key in CameraPosition]: CameraDevice | undefined;
|
|
|
|
};
|
|
|
|
const DefaultCameraDevices: CameraDevices = {
|
|
|
|
back: undefined,
|
|
|
|
external: undefined,
|
|
|
|
front: undefined,
|
|
|
|
unspecified: undefined,
|
|
|
|
};
|
|
|
|
|
2021-02-20 15:20:28 -07:00
|
|
|
/**
|
2021-03-08 10:51:53 -07:00
|
|
|
* Gets the best available {@linkcode CameraDevice}. Devices with more cameras are preferred.
|
2021-02-20 15:20:28 -07:00
|
|
|
*
|
2021-03-08 10:51:53 -07:00
|
|
|
* @returns The best matching {@linkcode CameraDevice}.
|
|
|
|
* @throws {@linkcode CameraRuntimeError} if no device was found.
|
2021-02-20 15:20:28 -07:00
|
|
|
* @example
|
2021-03-09 04:02:10 -07:00
|
|
|
* ```tsx
|
2021-02-20 15:23:38 -07:00
|
|
|
* const device = useCameraDevice()
|
2021-02-20 15:20:28 -07:00
|
|
|
* // ...
|
|
|
|
* return <Camera device={device} />
|
2021-03-03 04:37:43 -07:00
|
|
|
* ```
|
2021-02-20 15:20:28 -07:00
|
|
|
*/
|
2021-02-22 01:43:47 -07:00
|
|
|
export function useCameraDevices(): CameraDevices;
|
2021-02-20 15:20:28 -07:00
|
|
|
|
|
|
|
/**
|
2021-03-08 10:51:53 -07:00
|
|
|
* Gets a {@linkcode CameraDevice} for the requested device type.
|
2021-02-20 15:20:28 -07:00
|
|
|
*
|
2021-03-03 04:37:43 -07:00
|
|
|
* @param {PhysicalCameraDeviceType | LogicalCameraDeviceType} deviceType Specifies a device type which will be used as a device filter.
|
2021-03-08 10:51:53 -07:00
|
|
|
* @returns A {@linkcode CameraDevice} for the requested device type.
|
|
|
|
* @throws {@linkcode CameraRuntimeError} if no device was found.
|
2021-02-20 15:20:28 -07:00
|
|
|
* @example
|
2021-03-09 04:02:10 -07:00
|
|
|
* ```tsx
|
2021-02-20 15:20:28 -07:00
|
|
|
* const device = useCameraDevice('wide-angle-camera')
|
|
|
|
* // ...
|
|
|
|
* return <Camera device={device} />
|
2021-03-03 04:37:43 -07:00
|
|
|
* ```
|
2021-02-20 15:20:28 -07:00
|
|
|
*/
|
2021-02-22 01:43:47 -07:00
|
|
|
export function useCameraDevices(deviceType: PhysicalCameraDeviceType | LogicalCameraDeviceType): CameraDevices;
|
2021-02-20 15:20:28 -07:00
|
|
|
|
2021-02-22 01:43:47 -07:00
|
|
|
export function useCameraDevices(deviceType?: PhysicalCameraDeviceType | LogicalCameraDeviceType): CameraDevices {
|
|
|
|
const [cameraDevices, setCameraDevices] = useState<CameraDevices>(DefaultCameraDevices);
|
2021-02-20 09:05:02 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-02-20 15:20:28 -07:00
|
|
|
let isMounted = true;
|
|
|
|
|
2021-02-20 09:05:02 -07:00
|
|
|
const loadDevice = async (): Promise<void> => {
|
2021-02-22 01:43:47 -07:00
|
|
|
let devices = await Camera.getAvailableCameraDevices();
|
2021-02-20 15:20:28 -07:00
|
|
|
if (!isMounted) return;
|
|
|
|
|
2021-02-22 01:43:47 -07:00
|
|
|
devices = devices.sort(sortDevices);
|
|
|
|
if (deviceType != null) {
|
|
|
|
devices = devices.filter((d) => {
|
2021-02-20 15:20:28 -07:00
|
|
|
const parsedType = parsePhysicalDeviceTypes(d.devices);
|
|
|
|
return parsedType === deviceType;
|
|
|
|
});
|
|
|
|
}
|
2021-02-22 01:43:47 -07:00
|
|
|
setCameraDevices({
|
|
|
|
back: devices.find((d) => d.position === 'back'),
|
|
|
|
external: devices.find((d) => d.position === 'external'),
|
|
|
|
front: devices.find((d) => d.position === 'front'),
|
|
|
|
unspecified: devices.find((d) => d.position === 'unspecified'),
|
|
|
|
});
|
2021-02-20 09:05:02 -07:00
|
|
|
};
|
|
|
|
loadDevice();
|
2021-02-20 15:20:28 -07:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
isMounted = false;
|
|
|
|
};
|
2021-02-20 09:05:02 -07:00
|
|
|
}, [deviceType]);
|
|
|
|
|
2021-02-22 01:43:47 -07:00
|
|
|
return cameraDevices;
|
2021-02-20 15:20:28 -07:00
|
|
|
}
|