File size: 3,852 Bytes
3216a1f | 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 | /**
* recorder.js - MediaRecorder wrapper for video capture.
*
* Handles browser webcam access via getUserMedia, records 5-10 second
* video blobs using the MediaRecorder API, and provides start/stop controls.
*/
class FaceRecorder {
constructor() {
this.stream = null;
this.mediaRecorder = null;
this.chunks = [];
this.isRecording = false;
this.timerInterval = null;
this.startTime = 0;
this.maxDuration = 10000; // 10 seconds max
this.minDuration = 5000; // 5 seconds min
}
/**
* Initialize webcam stream and attach to video element.
* @param {HTMLVideoElement} videoElement
*/
async initCamera(videoElement) {
try {
this.stream = await navigator.mediaDevices.getUserMedia({
video: { width: { ideal: 640 }, height: { ideal: 480 }, facingMode: 'user' },
audio: false,
});
videoElement.srcObject = this.stream;
await videoElement.play();
return true;
} catch (err) {
console.error('[Recorder] Camera access denied:', err);
return false;
}
}
/**
* Start recording video from the webcam stream.
* @param {Function} onTimerUpdate - Callback with formatted time string.
* @returns {Promise} Resolves when recording starts.
*/
startRecording(onTimerUpdate) {
if (!this.stream) throw new Error('Camera not initialized');
if (this.isRecording) return;
this.chunks = [];
const mimeType = MediaRecorder.isTypeSupported('video/webm;codecs=vp9')
? 'video/webm;codecs=vp9'
: 'video/webm';
this.mediaRecorder = new MediaRecorder(this.stream, { mimeType });
this.mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) this.chunks.push(e.data);
};
this.mediaRecorder.start(100); // Collect data every 100ms
this.isRecording = true;
this.startTime = Date.now();
// Timer update
this.timerInterval = setInterval(() => {
const elapsed = Date.now() - this.startTime;
const secs = Math.floor(elapsed / 1000);
const mins = Math.floor(secs / 60);
const display = `${mins}:${String(secs % 60).padStart(2, '0')}`;
if (onTimerUpdate) onTimerUpdate(display);
// Auto-stop at max duration
if (elapsed >= this.maxDuration) {
this.stopRecording();
}
}, 100);
}
/**
* Stop recording and return the video blob.
* @returns {Promise<Blob>} The recorded video blob.
*/
stopRecording() {
return new Promise((resolve) => {
if (!this.isRecording || !this.mediaRecorder) {
resolve(null);
return;
}
clearInterval(this.timerInterval);
this.mediaRecorder.onstop = () => {
const blob = new Blob(this.chunks, { type: 'video/webm' });
this.isRecording = false;
this.chunks = [];
resolve(blob);
};
this.mediaRecorder.stop();
});
}
/**
* Check if recording duration meets minimum requirement.
*/
hasMinDuration() {
if (!this.isRecording) return false;
return (Date.now() - this.startTime) >= this.minDuration;
}
/**
* Stop the camera stream entirely.
*/
stopCamera() {
if (this.stream) {
this.stream.getTracks().forEach(track => track.stop());
this.stream = null;
}
}
/**
* Get the current stream for reuse.
*/
getStream() {
return this.stream;
}
}
// Export globally
window.FaceRecorder = FaceRecorder;
|