|
1 |
| -import React, { useCallback } from 'react'; |
| 1 | +import React, { forwardRef, useCallback, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState } from 'react'; |
| 2 | +import createElement from 'react-native-web/dist/exports/createElement'; |
| 3 | +import adapter from 'webrtc-adapter'; |
2 | 4 | import PropTypes from 'prop-types';
|
3 | 5 |
|
4 |
| -import { Text, View } from 'react-native'; |
5 |
| -import { Camera as ExpoCamera } from 'expo-camera'; |
6 |
| - |
| 6 | +import { Text, useWindowDimensions, View } from 'react-native'; |
7 | 7 | import { utils } from '@monkvision/toolkit';
|
8 |
| -import log from '../../utils/log'; |
9 |
| -import useAvailable from '../../hooks/useAvailable'; |
10 |
| -import usePermissions from '../../hooks/usePermissions'; |
11 |
| -import useWindowDimensions from '../../hooks/useWindowDimensions'; |
12 | 8 |
|
13 | 9 | import styles from './styles';
|
14 | 10 |
|
15 | 11 | const { getSize } = utils.styles;
|
16 | 12 |
|
17 |
| -export default function Camera({ |
18 |
| - children, |
19 |
| - containerStyle, |
20 |
| - onRef, |
21 |
| - ratio, |
22 |
| - style, |
23 |
| - title, |
24 |
| - ...passThroughProps |
25 |
| -}) { |
26 |
| - const available = useAvailable(); |
27 |
| - const permissions = usePermissions(); |
28 |
| - const { height: windowHeight, width: windowWidth } = useWindowDimensions(); |
29 |
| - const size = getSize(ratio, { windowHeight, windowWidth }); |
30 |
| - |
31 |
| - const handleError = useCallback((error) => { |
32 |
| - log([error], 'error'); |
| 13 | +const Video = React.forwardRef((props, ref) => createElement('video', { ...props, ref })); |
| 14 | + |
| 15 | +const tests = [ |
| 16 | +// { |
| 17 | +// label: '4K(UHD) 4:3', |
| 18 | +// width: 3840, |
| 19 | +// height: 2880, |
| 20 | +// ratio: '4:3', |
| 21 | +// }, { |
| 22 | +// label: '4K(UHD) 16:9', |
| 23 | +// width: 3840, |
| 24 | +// height: 2160, |
| 25 | +// ratio: '16:9', |
| 26 | +// }, |
| 27 | + { |
| 28 | + label: 'FHD 4:3', |
| 29 | + width: 1920, |
| 30 | + height: 1440, |
| 31 | + ratio: '4:3', |
| 32 | + }, { |
| 33 | + label: 'FHD 16:9', |
| 34 | + width: 1920, |
| 35 | + height: 1080, |
| 36 | + ratio: '16:9', |
| 37 | + }, { |
| 38 | + label: 'UXGA', |
| 39 | + width: 1600, |
| 40 | + height: 1200, |
| 41 | + ratio: '4:3', |
| 42 | + }, { |
| 43 | + label: 'HD(720p)', |
| 44 | + width: 1280, |
| 45 | + height: 720, |
| 46 | + ratio: '16:9', |
| 47 | + }, { |
| 48 | + label: 'SVGA', |
| 49 | + width: 800, |
| 50 | + height: 600, |
| 51 | + ratio: '4:3', |
| 52 | + }, { |
| 53 | + label: 'VGA', |
| 54 | + width: 640, |
| 55 | + height: 480, |
| 56 | + ratio: '4:3', |
| 57 | + }, { |
| 58 | + label: 'CIF', |
| 59 | + width: 352, |
| 60 | + height: 288, |
| 61 | + ratio: '4:3', |
| 62 | + }, { |
| 63 | + label: 'QVGA', |
| 64 | + width: 320, |
| 65 | + height: 240, |
| 66 | + ratio: '4:3', |
| 67 | + }, { |
| 68 | + label: 'QCIF', |
| 69 | + width: 176, |
| 70 | + height: 144, |
| 71 | + ratio: '4:3', |
| 72 | + }, { |
| 73 | + label: 'QQVGA', |
| 74 | + width: 160, |
| 75 | + height: 120, |
| 76 | + ratio: '4:3', |
| 77 | + }]; |
| 78 | + |
| 79 | +const { getUserMedia } = navigator.mediaDevices || navigator.mozGetUserMedia; |
| 80 | + |
| 81 | +const Camera = ({ children, containerStyle, onCameraReady, title }, ref) => { |
| 82 | + const windowDimensions = useWindowDimensions(); |
| 83 | + const videoEl = useRef(); |
| 84 | + const canvasEl = useRef(); |
| 85 | + const [candidate, setCandidate] = useState(); |
| 86 | + const [loading, setLoading] = useState(false); |
| 87 | + |
| 88 | + const { width: videoWith, height: videoHeight } = useMemo(() => { |
| 89 | + if (!candidate) { return { width: 0, height: 0 }; } |
| 90 | + return getSize(candidate.test.ratio, windowDimensions, 'number'); |
| 91 | + }, [candidate, windowDimensions]); |
| 92 | + |
| 93 | + const gum = useCallback(async (test, device) => { |
| 94 | + if (window.stream) { window.stream.getTracks().forEach((track) => track.stop()); } |
| 95 | + |
| 96 | + const OS = utils.getOS(); |
| 97 | + const facingMode = ['iOS', 'Android'].includes(OS) ? { exact: 'environment' } : 'environment'; |
| 98 | + |
| 99 | + const constraints = { |
| 100 | + audio: false, |
| 101 | + video: { |
| 102 | + facingMode, |
| 103 | + deviceId: device.deviceId ? { exact: device.deviceId } : undefined, |
| 104 | + width: { exact: test.width }, |
| 105 | + height: { exact: test.height }, |
| 106 | + }, |
| 107 | + }; |
| 108 | + |
| 109 | + let result; |
| 110 | + try { |
| 111 | + result = await getUserMedia(constraints); |
| 112 | + } catch (error) { result = { error }; } |
| 113 | + |
| 114 | + return [test, result, constraints, device]; |
| 115 | + }, []); |
| 116 | + |
| 117 | + const findBestCandidate = useCallback(async (devices) => { |
| 118 | + const testResults = devices |
| 119 | + .map((device) => tests.map(async (test) => gum(test, device))) |
| 120 | + .flat(); |
| 121 | + |
| 122 | + const [test, stream, constraint] = await testResults |
| 123 | + .reduce(async (resultA, resultB) => { |
| 124 | + const [testA, streamA] = await resultA; |
| 125 | + const [testB, streamB] = await resultB; |
| 126 | + |
| 127 | + if (streamA.error) { return resultB; } |
| 128 | + if (streamB.error) { return resultA; } |
| 129 | + |
| 130 | + return testA.width > testB.width ? resultA : resultB; |
| 131 | + }); |
| 132 | + |
| 133 | + return { |
| 134 | + test, |
| 135 | + stream, |
| 136 | + constraint, |
| 137 | + ask: `${test.width}x${test.height}`, |
| 138 | + browserVer: `${adapter.browserDetails.browser} ${adapter.browserDetails.version}`, |
| 139 | + }; |
| 140 | + }, [gum]); |
| 141 | + |
| 142 | + const findDevices = useCallback(async () => { |
| 143 | + const constraints = { audio: false, video: { facingMode: 'environment' } }; |
| 144 | + const mediaDevices = await navigator.mediaDevices.enumerateDevices(); |
| 145 | + window.stream = await getUserMedia(constraints); |
| 146 | + |
| 147 | + return mediaDevices.filter(({ kind }) => kind === 'videoinput'); |
33 | 148 | }, []);
|
34 | 149 |
|
35 |
| - if (permissions.isGranted === null) { |
36 |
| - return <View />; |
37 |
| - } |
| 150 | + useImperativeHandle( |
| 151 | + ref, |
| 152 | + () => ({ |
| 153 | + async takePicture() { |
| 154 | + if (!videoEl.current || videoEl.current?.readyState !== videoEl.current?.HAVE_ENOUGH_DATA) { |
| 155 | + throw new Error( |
| 156 | + 'ERR_CAMERA_NOT_READY', |
| 157 | + 'HTMLVideoElement does not have enough camera data to construct an image yet.', |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + canvasEl.current |
| 162 | + .getContext('2d') |
| 163 | + .drawImage(videoEl.current, 0, 0, canvasEl.current.width, canvasEl.current.height); |
| 164 | + |
| 165 | + const imageType = utils.getOS() === 'ios' ? 'image/png' : 'image/webp'; |
| 166 | + return { uri: canvasEl.current.toDataURL(imageType) }; |
| 167 | + }, |
| 168 | + async resumePreview() { |
| 169 | + if (videoEl.current) { |
| 170 | + videoEl.current.play(); |
| 171 | + } |
| 172 | + }, |
| 173 | + async pausePreview() { |
| 174 | + if (videoEl.current) { |
| 175 | + videoEl.current.pause(); |
| 176 | + } |
| 177 | + }, |
| 178 | + }), |
| 179 | + [videoEl], |
| 180 | + ); |
| 181 | + |
| 182 | + useLayoutEffect(() => { |
| 183 | + (async () => { |
| 184 | + if (videoEl && !candidate && !loading) { |
| 185 | + const video = videoEl.current; |
| 186 | + |
| 187 | + setLoading(true); |
38 | 188 |
|
39 |
| - if (permissions.isGranted === false || !available) { |
40 |
| - return <Text>No access to camera</Text>; |
41 |
| - } |
| 189 | + const canvas = canvasEl.current; |
| 190 | + const devices = await findDevices(); |
| 191 | + const bestCandidate = await findBestCandidate(devices); |
| 192 | + |
| 193 | + setCandidate(bestCandidate); |
| 194 | + |
| 195 | + if ('srcObject' in video) { |
| 196 | + video.srcObject = bestCandidate.stream; |
| 197 | + } else { |
| 198 | + video.src = window.URL.createObjectURL(bestCandidate.stream); |
| 199 | + } |
| 200 | + |
| 201 | + video.onloadedmetadata = () => { video.play(); }; |
| 202 | + |
| 203 | + canvas.width = bestCandidate.test.width; |
| 204 | + canvas.height = bestCandidate.test.height; |
| 205 | + |
| 206 | + setLoading(false); |
| 207 | + onCameraReady(); |
| 208 | + } |
| 209 | + })(); |
| 210 | + }, [candidate, findBestCandidate, findDevices, loading, onCameraReady, videoEl]); |
42 | 211 |
|
43 | 212 | return (
|
44 | 213 | <View
|
45 | 214 | accessibilityLabel="Camera container"
|
46 |
| - style={[containerStyle, size]} |
| 215 | + style={[styles.container, containerStyle]} |
47 | 216 | >
|
48 |
| - <ExpoCamera |
49 |
| - ref={onRef} |
50 |
| - ratio={ratio} |
51 |
| - onMountError={handleError} |
52 |
| - {...passThroughProps} |
53 |
| - > |
54 |
| - {children} |
55 |
| - </ExpoCamera> |
56 |
| - {title !== '' && <Text style={styles.title}>{title}</Text>} |
| 217 | + {/* eslint-disable-next-line jsx-a11y/media-has-caption */} |
| 218 | + <Video |
| 219 | + autoPlay |
| 220 | + playsInline |
| 221 | + ref={videoEl} |
| 222 | + width={videoWith} |
| 223 | + height={videoHeight} |
| 224 | + /> |
| 225 | + <canvas ref={canvasEl} /> |
| 226 | + {children} |
| 227 | + {(title !== '' && candidate) && <Text style={styles.title}>{title}</Text>} |
57 | 228 | </View>
|
58 | 229 | );
|
59 |
| -} |
| 230 | +}; |
| 231 | + |
| 232 | +export default forwardRef(Camera); |
60 | 233 |
|
61 | 234 | Camera.propTypes = {
|
62 |
| - children: PropTypes.element, |
63 | 235 | containerStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
64 |
| - onRef: PropTypes.func.isRequired, |
65 |
| - ratio: PropTypes.string.isRequired, |
| 236 | + onCameraReady: PropTypes.func.isRequired, |
66 | 237 | title: PropTypes.string,
|
67 | 238 | };
|
68 | 239 |
|
69 | 240 | Camera.defaultProps = {
|
70 |
| - children: null, |
71 | 241 | containerStyle: null,
|
72 | 242 | title: '',
|
73 | 243 | };
|
0 commit comments