Spaces:
Running
Running
File size: 9,657 Bytes
8876673 |
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
class ScreenStreamApp {
constructor() {
this.thumbnailInterval = null;
this.durationInterval = null;
this.isRecording = false;
this.init();
}
init() {
this.bindEvents();
this.loadRecordings();
this.checkRecordingStatus();
}
bindEvents() {
document.getElementById('startBtn').addEventListener('click', () => this.startRecording());
document.getElementById('stopBtn').addEventListener('click', () => this.stopRecording());
}
async checkRecordingStatus() {
try {
const response = await fetch('/duration');
if (response.ok) {
this.isRecording = true;
this.updateUIForRecording();
this.startLivePreview();
this.startDurationTimer();
} else {
this.isRecording = false;
this.updateUIForIdle();
this.stopLivePreview();
this.stopDurationTimer();
}
} catch (error) {
console.error('Error checking recording status:', error);
this.isRecording = false;
this.updateUIForIdle();
}
}
async startRecording() {
const filename = document.getElementById('filename').value.trim() || 'recording_' + Date.now();
const format = document.getElementById('format').value;
try {
const response = await fetch(`/start/${filename}?format=${format}`, {
method: 'POST'
});
if (response.status === 204) {
this.isRecording = true;
this.updateUIForRecording();
this.startLivePreview();
this.startDurationTimer();
this.showNotification('Recording started successfully!', 'success');
} else {
this.showNotification('Failed to start recording', 'error');
}
} catch (error) {
console.error('Error starting recording:', error);
this.showNotification('Error starting recording', 'error');
}
}
async stopRecording() {
try {
const response = await fetch('/stop', {
method: 'POST'
});
if (response.status === 204) {
this.isRecording = false;
this.updateUIForIdle();
this.stopLivePreview();
this.stopDurationTimer();
this.showNotification('Recording stopped successfully!', 'success');
this.loadRecordings();
} else {
this.showNotification('Failed to stop recording', 'error');
}
} catch (error) {
console.error('Error stopping recording:', error);
this.showNotification('Error stopping recording', 'error');
}
}
updateUIForRecording() {
document.getElementById('startBtn').disabled = true;
document.getElementById('stopBtn').disabled = false;
document.getElementById('statusDot').classList.remove('hidden');
document.getElementById('statusText').textContent = 'Recording';
document.getElementById('thumbnail').classList.remove('hidden');
document.getElementById('noFeed').classList.add('hidden');
}
updateUIForIdle() {
document.getElementById('startBtn').disabled = false;
document.getElementById('stopBtn').disabled = true;
document.getElementById('statusDot').classList.add('hidden');
document.getElementById('statusText').textContent = 'Not Recording';
document.getElementById('thumbnail').classList.add('hidden');
document.getElementById('noFeed').classList.remove('hidden');
document.getElementById('duration').textContent = '';
}
startLivePreview() {
this.stopLivePreview();
this.updateThumbnail();
this.thumbnailInterval = setInterval(() => this.updateThumbnail(), 1000);
}
stopLivePreview() {
if (this.thumbnailInterval) {
clearInterval(this.thumbnailInterval);
this.thumbnailInterval = null;
}
}
async updateThumbnail() {
const thumbnail = document.getElementById('thumbnail');
try {
const response = await fetch('/thumbnail');
if (response.ok) {
const blob = await response.blob();
const url = URL.createObjectURL(blob);
thumbnail.src = url + '?t=' + Date.now();
}
} catch (error) {
console.error('Error updating thumbnail:', error);
}
}
startDurationTimer() {
this.stopDurationTimer();
this.updateDuration();
this.durationInterval = setInterval(() => this.updateDuration(), 1000);
}
stopDurationTimer() {
if (this.durationInterval) {
clearInterval(this.durationInterval);
this.durationInterval = null;
}
}
async updateDuration() {
try {
const response = await fetch('/duration');
if (response.ok) {
const duration = await response.text();
document.getElementById('duration').textContent = this.formatDuration(parseInt(duration));
}
} catch (error) {
console.error('Error updating duration:', error);
}
}
formatDuration(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}
async loadRecordings() {
try {
const response = await fetch('/videos');
const videos = await response.json();
this.renderRecordings(videos);
} catch (error) {
console.error('Error loading recordings:', error);
}
}
renderRecordings(videos) {
const container = document.getElementById('recordingsList');
if (videos.length === 0) {
container.innerHTML = `
<div class="text-center py-8 text-gray-500">
<i data-feather="folder" class="w-12 h-12 mx-auto mb-4"></i>
<p>No recordings found</p>
</div>
`;
return;
}
container.innerHTML = videos.map(video => `
<div class="flex items-center justify-between p-4 border border-gray-200 rounded-lg hover:border-primary transition-colors">
<div class="flex items-center gap-3">
<i data-feather="video" class="text-primary"></i>
<div>
<h3 class="font-semibold text-gray-800">${video}</h3>
<p class="text-sm text-gray-500">Click to download</p>
</div>
</div>
<div class="flex gap-2">
<a href="/recording/${video}"
class="bg-primary hover:bg-blue-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors flex items-center gap-1">
<i data-feather="download"></i>
Download
</a>
<button onclick="app.deleteRecording('${video}')"
class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors flex items-center gap-1">
<i data-feather="trash-2"></i>
Delete
</button>
</div>
</div>
`).join('');
feather.replace();
}
async deleteRecording(filename) {
if (!confirm(`Are you sure you want to delete "${filename}"?`)) {
return;
}
try {
const response = await fetch(`/recording/${filename}`, {
method: 'DELETE'
});
if (response.status === 204) {
this.showNotification('Recording deleted successfully!', 'success');
this.loadRecordings();
} else {
this.showNotification('Failed to delete recording', 'error');
}
} catch (error) {
console.error('Error deleting recording:', error);
this.showNotification('Error deleting recording', 'error');
}
}
showNotification(message, type = 'info') {
// Create notification element
const notification = document.createElement('div');
notification.className = `fixed top-4 right-4 z-50 p-4 rounded-lg shadow-lg transform transition-transform duration-300 translate-x-full ${
type === 'success' ? 'bg-secondary' :
type === 'error' ? 'bg-red-500' :
'bg-primary'
} text-white font-medium`;
notification.textContent = message;
document.body.appendChild(notification);
// Animate in
setTimeout(() => {
notification.classList.remove('translate-x-full');
}, 100);
// Remove after delay
setTimeout(() => {
notification.classList.add('translate-x-full');
setTimeout(() => {
document.body.removeChild(notification);
}, 300);
}, 3000);
}
}
// Initialize the app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
window.app = new ScreenStreamApp();
}); |