UNPKG

@face-auth/face-id-video

Version:

Utility library for capturing photos from webcam video streams in the browser. Handles camera selection, image formatting, and output for face authentication APIs.

86 lines (85 loc) 2.88 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { FaceCameraManager } from "./faceCameraManager"; import { FacePhotoManager } from "./facePhotoManager"; export class FaceVideo { constructor(video) { this.onCameraStartedCallbacks = new Set(); this.cameraManager = new FaceCameraManager(video); this.photoManager = null; } /** * Get all video input devices. */ getDevices() { return __awaiter(this, void 0, void 0, function* () { return this.cameraManager.getDevices(); }); } /** * Start video stream with optional deviceId. */ start() { return __awaiter(this, arguments, void 0, function* (options = {}, deviceId) { return yield this.cameraManager.start(options, deviceId) .then(videoElement => { this.photoManager = new FacePhotoManager(videoElement); }) .then(() => { this.emitCameraStarted(); }); }); } /** * Stop the video stream and clean up. */ stop() { this.cameraManager.stop(); } /** * Capture the current frame and return it as Blob. */ capture(options) { return __awaiter(this, void 0, void 0, function* () { if (!this.photoManager) { return new Promise((_, reject) => { // TODO: code error reject("NO_VIDEO_INITIALIZED"); }); } return yield this.photoManager.capture(options); }); } /** * Subscribe to the camera started event. */ onCameraStarted(callback) { this.onCameraStartedCallbacks.add(callback); } /** * Unsubscribe from the camera started event. */ offCameraStarted(callback) { this.onCameraStartedCallbacks.delete(callback); } /** * Internal method to notify all subscribers. */ emitCameraStarted() { for (const cb of this.onCameraStartedCallbacks) { try { cb(); } catch (err) { console.error("Error in camera started callback", err); } } } }