2023-09-26 11:39:17 +02:00
|
|
|
import { useMemo } from 'react'
|
|
|
|
import { CameraDevice, CameraDeviceFormat } from '../CameraDevice'
|
|
|
|
import { FormatFilter, getCameraFormat } from '../devices/getCameraFormat'
|
2021-02-20 23:20:28 +01:00
|
|
|
|
|
|
|
/**
|
2023-09-21 11:20:33 +02:00
|
|
|
* Get the best matching Camera format for the given device that satisfies your requirements using a sorting filter. By default, formats are sorted by highest to lowest resolution.
|
2023-09-23 11:24:15 +02:00
|
|
|
*
|
|
|
|
* The {@linkcode filters | filters} are ranked by priority, from highest to lowest.
|
|
|
|
* This means the first item you pass will have a higher priority than the second, and so on.
|
|
|
|
*
|
2023-09-21 11:20:33 +02:00
|
|
|
* @param device The Camera Device you're currently using
|
|
|
|
* @param filter The filter you want to use. The format that matches your filter the closest will be returned
|
|
|
|
* @returns The format that matches your filter the closest.
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* const device = useCameraDevice(...)
|
2023-09-23 11:24:15 +02:00
|
|
|
* const format = useCameraFormat(device, [
|
|
|
|
* { videoResolution: { width: 3048, height: 2160 } },
|
|
|
|
* { fps: 60 }
|
|
|
|
* ])
|
2023-09-21 11:20:33 +02:00
|
|
|
* ```
|
2021-02-20 23:20:28 +01:00
|
|
|
*/
|
2023-09-23 11:24:15 +02:00
|
|
|
export function useCameraFormat(device: CameraDevice | undefined, filters: FormatFilter[]): CameraDeviceFormat | undefined {
|
2023-09-21 11:20:33 +02:00
|
|
|
const format = useMemo(() => {
|
2023-09-26 11:39:17 +02:00
|
|
|
if (device == null) return undefined
|
|
|
|
return getCameraFormat(device, filters)
|
2023-09-21 11:20:33 +02:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-09-26 11:39:17 +02:00
|
|
|
}, [device, JSON.stringify(filters)])
|
2023-09-21 11:20:33 +02:00
|
|
|
|
2023-09-26 11:39:17 +02:00
|
|
|
return format
|
2021-02-20 23:20:28 +01:00
|
|
|
}
|