Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 1x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 88x 85x 88x 88x 29x 29x 29x 29x 29x 88x 3x 3x 3x 3x 3x 3x 88x 38x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 38x 38x 31x 3x 3x 28x 28x 28x 28x 28x 28x 38x 38x 6x 6x 6x 6x 6x 6x 88x 88x 1x 88x 2x 2x 1x 1x 1x 1x 1x 88x 6x 6x 5x 5x 4x 4x 1x 88x 33x 32x 32x 32x 33x 88x 88x 88x 88x 35x 35x 35x 32x 32x 3x 88x 33x 33x 33x 33x 33x 33x 33x 88x 84x | import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
/**
* `useLayoutEffect` where there is a DOM, `useEffect` where there is not.
*
* Written out rather than imported from `@usefy/use-isomorphic-layout-effect`
* so this package keeps its zero-dependency promise — four lines is a smaller
* cost than a dependency in every consumer's tree.
*/
const useIsomorphicLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
import {
UserMediaError,
type UseUserMediaOptions,
type UseUserMediaReturn,
type UserMediaStatus,
} from "./types";
import {
activeDeviceIdOf,
isEnumerationSupported,
isUserMediaSupported,
stopStream,
supportsTorch,
toUserMediaError,
withVideoPreferences,
} from "./utils";
/**
* Acquire and manage a camera or microphone stream.
*
* `getUserMedia` looks like a one-line API and is not: the stream has to be
* released on unmount or the camera light stays on, permission has to be
* granted before device labels are even visible, switching cameras means
* tearing down and re-acquiring, and every browser words its failures
* differently. This packages all of that, including the parts that are only
* obvious after they have gone wrong in front of a user.
*
* @example
* ```tsx
* const camera = useUserMedia({ facingMode: "environment" });
*
* return camera.stream
* ? <video ref={(el) => el && (el.srcObject = camera.stream)} autoPlay playsInline muted />
* : <button onClick={camera.start}>Start camera</button>;
* ```
*/
export function useUserMedia(options: UseUserMediaOptions = {}): UseUserMediaReturn {
const {
constraints = { video: true },
autoStart = false,
facingMode,
deviceId,
onStream,
onError,
} = options;
const [stream, setStream] = useState<MediaStream | null>(null);
const [status, setStatus] = useState<UserMediaStatus>(() =>
isUserMediaSupported() ? "idle" : "unsupported",
);
const [error, setError] = useState<UserMediaError | null>(null);
const [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
const [torch, setTorchState] = useState(false);
// Every mutable value the async acquisition path reads lives in a ref, so a
// request in flight is never confused by a re-render — and so the effect that
// owns teardown does not re-run when a callback identity changes.
const streamRef = useRef<MediaStream | null>(null);
const requestRef = useRef(0);
const mountedRef = useRef(true);
const optionsRef = useRef({ constraints, facingMode, deviceId, onStream, onError });
// Assigned in an effect, not during render: a render React discards must not
// mutate a ref. A layout effect rather than a passive one, so the value is in
// place before any other effect — including this hook's own acquisition — runs.
useIsomorphicLayoutEffect(() => {
optionsRef.current = { constraints, facingMode, deviceId, onStream, onError };
});
const isSupported = isUserMediaSupported();
/** Enumerate video inputs. Labels only appear after a grant, so this re-runs. */
const refreshDevices = useCallback(async () => {
Iif (!isEnumerationSupported()) return;
try {
const all = await navigator.mediaDevices.enumerateDevices();
Iif (!mountedRef.current) return;
setDevices(all.filter((device) => device.kind === "videoinput"));
} catch {
// Enumeration is a convenience; a browser that refuses it should not
// take the stream down with it.
}
}, []);
const stop = useCallback(() => {
// Bump the request id so any acquisition still in flight discards its
// result instead of quietly re-opening the camera after `stop()`.
requestRef.current++;
stopStream(streamRef.current);
streamRef.current = null;
setStream(null);
setTorchState(false);
setStatus((previous) => (previous === "unsupported" ? previous : "idle"));
}, []);
const acquire = useCallback(
async (overrides: { deviceId?: string; facingMode?: "user" | "environment" } = {}) => {
if (!isUserMediaSupported()) {
const unsupported = new UserMediaError(
"unsupported",
"Media capture is unavailable here. It needs a secure context (HTTPS or " +
"localhost) and a browser with mediaDevices.",
);
Eif (mountedRef.current) {
setStatus("unsupported");
setError(unsupported);
}
optionsRef.current.onError?.(unsupported);
return null;
}
const request = ++requestRef.current;
setStatus("prompting");
setError(null);
const current = optionsRef.current;
const requested = withVideoPreferences(current.constraints, {
deviceId: overrides.deviceId ?? current.deviceId,
facingMode: overrides.facingMode ?? current.facingMode,
});
try {
const next = await navigator.mediaDevices.getUserMedia(requested);
// A newer request (or a `stop()`) superseded this one while the
// permission sheet was open. Releasing immediately is the whole point:
// this is the stream nobody is going to render, and leaving it running
// is a camera light with no UI attached to it.
if (request !== requestRef.current || !mountedRef.current) {
stopStream(next);
return null;
}
stopStream(streamRef.current);
streamRef.current = next;
setStream(next);
setStatus("granted");
setTorchState(false);
current.onStream?.(next);
void refreshDevices();
return next;
} catch (caught) {
const failure = toUserMediaError(caught);
Eif (request === requestRef.current && mountedRef.current) {
setStatus(failure.reason === "denied" ? "denied" : "error");
setError(failure);
}
current.onError?.(failure);
return null;
}
},
[refreshDevices],
);
const start = useCallback(() => acquire(), [acquire]);
const selectDevice = useCallback(
(nextDeviceId: string) => acquire({ deviceId: nextDeviceId }),
[acquire],
);
const switchDevice = useCallback(async () => {
const list = devices.length > 0 ? devices : [];
if (list.length < 2) {
// Nothing to switch to. Re-acquiring anyway would flash the preview for
// no reason, so this is a no-op that keeps the current stream.
return streamRef.current;
}
const active = activeDeviceIdOf(streamRef.current);
const index = list.findIndex((device) => device.deviceId === active);
const next = list[(index + 1) % list.length]!;
return acquire({ deviceId: next.deviceId });
}, [acquire, devices]);
const setTorch = useCallback(async (on: boolean) => {
const track = streamRef.current?.getVideoTracks()[0];
if (!track || !supportsTorch(streamRef.current)) return false;
try {
// `torch` is not in the standard `MediaTrackConstraintSet` — it comes from
// the Image Capture draft — so TypeScript's DOM types do not know it,
// even though every browser that implements the LED accepts it exactly
// here. The double cast is the honest way to say "the platform's types
// are behind the platform".
await track.applyConstraints({
advanced: [{ torch: on }],
} as unknown as MediaTrackConstraints);
Eif (mountedRef.current) setTorchState(on);
return true;
} catch {
// Some devices advertise torch support and then refuse — often because
// the camera is in a mode that cannot drive the LED.
return false;
}
}, []);
// Device list changes (a webcam plugged in, a phone rotated into a dock).
useEffect(() => {
if (!isEnumerationSupported()) return;
const target = navigator.mediaDevices;
const handler = (): void => void refreshDevices();
target.addEventListener?.("devicechange", handler);
return () => target.removeEventListener?.("devicechange", handler);
}, [refreshDevices]);
// Re-acquire when the caller changes which camera it wants.
//
// The first run is tracked with its own flag rather than by comparing against
// an empty string: `""` is a perfectly legitimate value (no `deviceId`, no
// `facingMode`), and using it as the sentinel meant a consumer who mounted
// with neither and then set one got no re-acquisition at all — a flip-camera
// button that silently did nothing on its first press.
const firstWantRef = useRef(true);
const wantedRef = useRef<string>("");
const wanted = `${deviceId ?? ""}|${facingMode ?? ""}`;
useEffect(() => {
const previous = wantedRef.current;
wantedRef.current = wanted;
if (firstWantRef.current) {
firstWantRef.current = false;
return;
}
if (previous !== wanted && streamRef.current) void acquire();
}, [wanted, acquire]);
useEffect(() => {
mountedRef.current = true;
if (autoStart) void acquire();
return () => {
mountedRef.current = false;
// The one guarantee that matters most: no track survives this component.
// StrictMode mounts twice, so this runs twice — `stopStream` is
// deliberately safe to call on an already-stopped stream.
requestRef.current++;
stopStream(streamRef.current);
streamRef.current = null;
};
// `acquire` is stable and `autoStart` is read once, on purpose: a change to
// either must not tear down a live camera.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Memoized so the object's identity only changes when something in it does.
// Without this, every consumer's `useCallback([camera])` and `useEffect`
// churns on every render of the component holding the hook — which, for a
// scanner re-rendering on each decoded frame, means listeners being torn down
// and re-added twelve times a second.
return useMemo(
() => ({
stream,
status,
error,
isSupported,
start,
stop,
devices,
activeDeviceId: activeDeviceIdOf(stream),
selectDevice,
switchDevice,
isTorchSupported: supportsTorch(stream),
torch,
setTorch,
}),
[stream, status, error, isSupported, start, stop, devices, selectDevice, switchDevice, torch, setTorch],
);
}
|