Spaces:
Sleeping
Sleeping
File size: 5,743 Bytes
16a84ba |
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 |
import {
LitElement,
html,
css,
} from 'https://cdn.jsdelivr.net/gh/lit/dist@3/core/lit-core.min.js';
class VideoRecorder extends LitElement {
static styles = css`
:host {
display: block;
}
.video-container {
position: relative;
width: 100%;
max-width: 640px;
}
video {
width: 100%;
height: auto;
background: #000;
}
`;
static properties = {
dataEvent: {type: String},
stateChangeEvent: {type: String},
state: {type: String},
recordEvent: {type: String},
isRecording: {type: Boolean},
enabled: {type: Boolean},
quality: {type: Number},
fps: {type: Number},
showPreview: {type: Boolean},
};
constructor() {
super();
this.debug = false;
this.mediaStream = null;
this.isStreaming = false;
this.isRecording = false;
this.isInitializing = false;
this.enabled = false;
this.quality = 0.8; // JPEG quality
this.fps = 2; // Frames per second
this.showPreview = true; // Enable preview by default
// Setup canvas and video elements
this.video = document.createElement('video');
this.video.setAttribute('playsinline', ''); // Better mobile support
this.video.setAttribute('autoplay', '');
this.video.setAttribute('muted', '');
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
this.captureInterval = null;
this.onGeminiLiveStarted = (e) => {
if (this.isRecording) {
this.startStreaming();
}
};
this.onGeminiLiveStopped = (e) => {
this.stop();
};
}
connectedCallback() {
super.connectedCallback();
window.addEventListener(
'gemini-live-api-started',
this.onGeminiLiveStarted,
);
window.addEventListener(
'gemini-live-api-stopped',
this.onGeminiLiveStopped,
);
}
disconnectedCallback() {
super.disconnectedCallback();
this.stop();
window.removeEventListener(
'gemini-live-api-started',
this.onGeminiLiveStarted,
);
window.removeEventListener(
'gemini-live-api-stopped',
this.onGeminiLiveStopped,
);
}
firstUpdated() {
if (this.state !== 'disabled') {
this.startStreaming();
}
}
updated(changedProperties) {
if (changedProperties.has('enabled')) {
if (this.enabled) {
this.startStreaming();
} else {
this.stop();
}
}
}
async startStreaming() {
if (this.state === 'disabled') {
this.dispatchEvent(new MesopEvent(this.stateChangeEvent, 'initializing'));
}
this.isInitializing = true;
const initialized = await this.initialize();
this.isInitializing = false;
if (initialized) {
this.isRecording = true;
this.dispatchEvent(new MesopEvent(this.stateChangeEvent, 'recording'));
this.start();
}
}
async initialize() {
try {
this.mediaStream = await navigator.mediaDevices.getUserMedia({
video: {
width: {ideal: 1280},
height: {ideal: 720},
},
});
this.video.srcObject = this.mediaStream;
// Wait for video to be ready
await new Promise((resolve) => {
this.video.onloadedmetadata = () => {
this.canvas.width = this.video.videoWidth;
this.canvas.height = this.video.videoHeight;
resolve();
};
});
await this.video.play();
// Request a redraw to show the video preview
this.requestUpdate();
return true;
} catch (error) {
this.error('Error accessing webcam:', error);
return false;
}
}
captureFrame() {
if (!this.mediaStream) {
this.error('Webcam not started');
return null;
}
// Draw current video frame to canvas
this.ctx.drawImage(this.video, 0, 0);
// Convert to JPEG and base64 encode
const base64Data = this.canvas.toDataURL('image/jpeg', this.quality);
// Remove the data URL prefix to get just the base64 data
return base64Data.replace('data:image/jpeg;base64,', '');
}
start() {
this.isStreaming = true;
// Start capturing frames at specified FPS
const intervalMs = 1000 / this.fps;
this.captureInterval = setInterval(() => {
const base64Frame = this.captureFrame();
if (base64Frame) {
this.dispatchEvent(
new MesopEvent(this.dataEvent, {
data: base64Frame,
}),
);
this.dispatchEvent(
new CustomEvent('video-input-received', {
detail: {data: base64Frame},
// Allow event to cross shadow DOM boundaries (both need to be true)
bubbles: true,
composed: true,
}),
);
}
}, intervalMs);
return true;
}
stop() {
this.isStreaming = false;
this.isRecording = false;
this.dispatchEvent(new MesopEvent(this.stateChangeEvent, 'disabled'));
if (this.captureInterval) {
clearInterval(this.captureInterval);
this.captureInterval = null;
}
if (this.mediaStream) {
this.mediaStream.getTracks().forEach((track) => track.stop());
this.mediaStream = null;
}
// Clear video source
if (this.video.srcObject) {
this.video.srcObject = null;
}
}
render() {
return html`
<div class="video-container">
${this.showPreview
? html`<video
.srcObject="${this.mediaStream}"
playsinline
autoplay
muted
></video>`
: null}
</div>
`;
}
error(...args) {
if (this.debug) {
console.error(...args);
}
}
}
customElements.define('video-recorder', VideoRecorder);
|