Update frontend/js/video-manager.js
Browse files- frontend/js/video-manager.js +26 -96
frontend/js/video-manager.js
CHANGED
|
@@ -1,97 +1,27 @@
|
|
| 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 |
-
this.onImageData = onImageData;
|
| 26 |
-
|
| 27 |
-
try {
|
| 28 |
-
// Acquire video stream based on mode
|
| 29 |
-
if (mode === 'camera') {
|
| 30 |
-
this.videoStream = await navigator.mediaDevices.getUserMedia({
|
| 31 |
-
video: { width: { ideal: 320 }, height: { ideal: 240 } }
|
| 32 |
-
});
|
| 33 |
-
} else {
|
| 34 |
-
this.videoStream = await navigator.mediaDevices.getDisplayMedia({
|
| 35 |
-
video: { width: { ideal: 1920 }, height: { ideal: 1080 } }
|
| 36 |
-
});
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
// Set video source
|
| 40 |
-
this.videoElement.srcObject = this.videoStream;
|
| 41 |
-
|
| 42 |
-
// Periodically capture and send frames
|
| 43 |
-
this.videoInterval = setInterval(() => {
|
| 44 |
-
this.captureAndSendFrame();
|
| 45 |
-
}, 1000); // Send a frame every second
|
| 46 |
-
|
| 47 |
-
} catch (error) {
|
| 48 |
-
console.error('Error initializing video capture:', error);
|
| 49 |
-
throw new Error(`Video capture failed: ${error.message}`);
|
| 50 |
-
}
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
/**
|
| 54 |
-
* Stop video capture and release resources
|
| 55 |
-
*/
|
| 56 |
-
stopCapture() {
|
| 57 |
-
// Clear capture interval
|
| 58 |
-
if (this.videoInterval) {
|
| 59 |
-
clearInterval(this.videoInterval);
|
| 60 |
-
this.videoInterval = null;
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
// Stop all video tracks
|
| 64 |
-
if (this.videoStream) {
|
| 65 |
-
this.videoStream.getTracks().forEach(track => track.stop());
|
| 66 |
-
this.videoStream = null;
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
// Clear video source
|
| 70 |
-
if (this.videoElement) {
|
| 71 |
-
this.videoElement.srcObject = null;
|
| 72 |
-
}
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
/**
|
| 76 |
-
* Capture current video frame and send through callback
|
| 77 |
-
* @private
|
| 78 |
-
*/
|
| 79 |
-
captureAndSendFrame() {
|
| 80 |
-
if (!this.videoStream || !this.onImageData) return;
|
| 81 |
-
|
| 82 |
-
const ctx = this.canvasElement.getContext('2d');
|
| 83 |
-
|
| 84 |
-
// Set canvas size to match video dimensions
|
| 85 |
-
this.canvasElement.width = this.videoElement.videoWidth;
|
| 86 |
-
this.canvasElement.height = this.videoElement.videoHeight;
|
| 87 |
-
|
| 88 |
-
// Draw video frame to canvas
|
| 89 |
-
ctx.drawImage(this.videoElement, 0, 0, this.videoElement.videoWidth, this.videoElement.videoHeight);
|
| 90 |
-
|
| 91 |
-
// Get JPEG image as base64
|
| 92 |
-
const base64Image = this.canvasElement.toDataURL('image/jpeg').split(',')[1];
|
| 93 |
-
|
| 94 |
-
// Send through callback
|
| 95 |
-
this.onImageData(base64Image);
|
| 96 |
-
}
|
| 97 |
-
}
|
|
|
|
| 1 |
+
// In your initEventListeners method
|
| 2 |
+
this.switchCameraBtn = document.getElementById('switchCameraBtn');
|
| 3 |
+
if (this.switchCameraBtn) {
|
| 4 |
+
this.switchCameraBtn.addEventListener('click', () => this.switchCamera());
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
// Add this method to your GeminiApp class
|
| 8 |
+
async switchCamera() {
|
| 9 |
+
if (this.videoManager && this.currentMode === 'camera') {
|
| 10 |
+
const success = await this.videoManager.switchCamera();
|
| 11 |
+
if (success) {
|
| 12 |
+
console.log('Camera switched successfully');
|
| 13 |
+
} else {
|
| 14 |
+
this.uiController.showError('Failed to switch camera');
|
| 15 |
}
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// In startStream, show the switch button when camera mode is active
|
| 20 |
+
if (mode === 'camera') {
|
| 21 |
+
this.switchCameraBtn.style.display = 'inline-block';
|
| 22 |
+
} else {
|
| 23 |
+
this.switchCameraBtn.style.display = 'none';
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// In stopStream, hide the switch button
|
| 27 |
+
this.switchCameraBtn.style.display = 'none';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|