File size: 6,596 Bytes
c8cad77 | 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 | import type { CSSProperties } from "react";
export const CAMERA_ROTATIONS = [0, 90, 180, 270] as const;
export type CameraRotation = (typeof CAMERA_ROTATIONS)[number];
/** Camera dimensions describe capture before rotation. */
export interface CameraRequest {
type: string;
camera_index?: number;
width: number;
height: number;
fps?: number;
fourcc?: string;
backend?: string;
rotation?: CameraRotation;
}
export interface CameraConfig extends CameraRequest {
id: string;
name: string;
device_id: string;
}
/** Only an omitted rotation defaults to zero. */
export function cameraRotation(value: unknown = 0): CameraRotation {
if (!CAMERA_ROTATIONS.some((rotation) => rotation === value)) {
throw new Error(`Camera rotation must be 0, 90, 180, or 270 degrees clockwise; received ${String(value)}.`);
}
return value as CameraRotation;
}
export function cameraRotationError(camera: Pick<CameraRequest, "rotation">): string | null {
try {
cameraRotation(camera.rotation);
return null;
} catch (error) {
return error instanceof Error ? error.message : String(error);
}
}
export function cameraConfigurationError(cameras: CameraConfig[]): string | null {
for (const camera of cameras) {
const error = cameraRotationError(camera);
if (error) return `${camera.name}: ${error}`;
}
return null;
}
/** Preserve invalid stored values so the user can correct them explicitly. */
export function loadCameraConfigs(cameras: CameraConfig[]): CameraConfig[] {
return cameras.map((camera) => cameraRotationError(camera) ? camera : normalizeCameraConfig(camera));
}
export function normalizeCameraConfig(camera: CameraConfig): CameraConfig {
return { ...camera, rotation: cameraRotation(camera.rotation) };
}
/** Remove browser/profile fields before sending a camera to a session. */
export function serializeCamera(camera: CameraRequest): CameraRequest {
return {
type: camera.type,
camera_index: camera.camera_index,
width: camera.width,
height: camera.height,
fps: camera.fps,
rotation: cameraRotation(camera.rotation),
...(camera.fourcc ? { fourcc: camera.fourcc } : {}),
...(camera.backend ? { backend: camera.backend } : {}),
};
}
export function serializeCameras(cameras: CameraConfig[]): Record<string, CameraRequest> {
return Object.fromEntries(cameras.map((camera) => [camera.name, serializeCamera(camera)]));
}
/** Quarter turns swap capture and output dimensions in both directions. */
export function rotatedDimensions(width: number, height: number, rotation: CameraRotation) {
const angle = cameraRotation(rotation);
return angle === 90 || angle === 270 ? { width: height, height: width } : { width, height };
}
/** Center the complete native rectangle inside the rotated output rectangle. */
export function cameraPreviewGeometry(width: number, height: number, rotation: CameraRotation) {
const output = rotatedDimensions(width, height, rotation);
const quarterTurn = rotation === 90 || rotation === 270;
const videoStyle: CSSProperties = {
position: "absolute",
left: "50%",
top: "50%",
width: quarterTurn ? `${(width / height) * 100}%` : "100%",
height: quarterTurn ? `${(height / width) * 100}%` : "100%",
// Crop, don't letterbox: a stream that fell back to another aspect ratio
// (see useCameraStream) should still show the recorded framing.
objectFit: "cover",
maxWidth: "none",
transform: `translate(-50%, -50%) rotate(${rotation}deg)`,
};
return { aspectRatio: `${output.width} / ${output.height}`, videoStyle };
}
export interface PhysicalCamera {
index: number;
deviceId: string;
}
/**
* The live camera for a saved one. Browser device IDs differ per browser and
* origin, so an ID saved in Firefox is unknown to Safari (or to the Vite dev
* origin); fall back to the cv2 index, which every browser shares.
*/
export function liveCameraFor<T extends PhysicalCamera>(
saved: Pick<CameraConfig, "device_id" | "camera_index">,
available: T[],
): T | undefined {
return (
(saved.device_id ? available.find((c) => c.deviceId === saved.device_id) : undefined) ??
available.find((c) => c.index === saved.camera_index)
);
}
/**
* Browser device ID to preview a saved camera with. Until the live cameras
* have been listed, try the saved ID; afterwards "" means no camera matched.
*/
export function previewDeviceId(
saved: Pick<CameraConfig, "device_id" | "camera_index">,
available: PhysicalCamera[],
): string {
if (available.length === 0) return saved.device_id;
return liveCameraFor(saved, available)?.deviceId ?? "";
}
export function physicalCameraKey(camera: PhysicalCamera): string {
return camera.deviceId ? `device:${camera.deviceId}` : `index:${camera.index}`;
}
/** Rotation follows the physical camera when a policy name is reassigned. */
export function rotationForCamera(
camera: PhysicalCamera,
configured: CameraConfig[],
overrides: Record<string, CameraRotation>,
): CameraRotation {
const saved = configured.find((item) => camera.deviceId && item.device_id === camera.deviceId)
?? configured.find((item) => item.camera_index === camera.index);
return cameraRotation(overrides[physicalCameraKey(camera)] ?? saved?.rotation);
}
/** Size each corrected camera frame within a common grid cell. */
export function recordingCameraWindows(
names: string[],
cameras: Record<string, CameraRequest>,
area: { w: number; h: number },
): Record<string, { width: number; height: number }> {
if (!names.length || area.w <= 0 || area.h <= 0) return {};
let best: Record<string, { width: number; height: number }> = {};
let bestArea = -1;
for (let columns = 1; columns <= names.length; columns++) {
const rows = Math.ceil(names.length / columns);
const cellWidth = (area.w - 12 * (columns - 1)) / columns;
const cellHeight = (area.h - 12 * (rows - 1)) / rows;
if (cellWidth <= 0 || cellHeight <= 0) continue;
let total = 0;
const windows = Object.fromEntries(names.map((name) => {
const camera = cameras[name];
const output = camera
? rotatedDimensions(camera.width, camera.height, cameraRotation(camera.rotation))
: { width: 640, height: 480 };
const aspect = output.width / output.height;
const width = Math.min(cellWidth, cellHeight * aspect);
const height = width / aspect;
total += width * height;
return [name, { width: Math.floor(width), height: Math.floor(height) }];
}));
if (total > bestArea) { best = windows; bestArea = total; }
}
return best;
}
|