change flask to fastapi
This commit is contained in:
@ -3,7 +3,9 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Beacon Tracker - Live Stream</title>
|
||||
<title>Beacon Tracker - Live Stream (FastAPI)</title>
|
||||
<!-- JSMpeg for H.264/MPEG-TS decoding -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/jsmpeg@0.1.0/jsmpeg.min.js"></script>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
@ -90,24 +92,29 @@
|
||||
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
#videoFrame {
|
||||
#videoFrame, #videoCanvas {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.hidden {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
.loading-overlay {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: rgba(0, 0, 0, 0.8);
|
||||
background: rgba(0, 0, 0, 0.7);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
color: white;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.loading-overlay.hidden {
|
||||
@ -115,17 +122,18 @@
|
||||
}
|
||||
|
||||
.spinner {
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
border: 4px solid rgba(255, 255, 255, 0.3);
|
||||
border-top-color: white;
|
||||
border-radius: 50%;
|
||||
border-top: 4px solid white;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
animation: spin 1s linear infinite;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.info-grid {
|
||||
@ -137,15 +145,16 @@
|
||||
|
||||
.info-card {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
padding: 15px;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
color: white;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.info-label {
|
||||
font-size: 0.85rem;
|
||||
opacity: 0.9;
|
||||
margin-bottom: 5px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.info-value {
|
||||
@ -153,12 +162,61 @@
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.controls {
|
||||
margin-top: 20px;
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.controls button, .stream-selector button {
|
||||
padding: 12px 24px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.controls button {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.stream-selector {
|
||||
margin-top: 20px;
|
||||
padding: 15px;
|
||||
background: #f0f0f0;
|
||||
border-radius: 8px;
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.stream-selector button {
|
||||
background: white;
|
||||
color: #333;
|
||||
border: 2px solid #ddd;
|
||||
}
|
||||
|
||||
.stream-selector button.active {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.controls button:hover, .stream-selector button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.error-message {
|
||||
margin-top: 15px;
|
||||
padding: 15px;
|
||||
background: #e74c3c;
|
||||
color: white;
|
||||
padding: 15px;
|
||||
border-radius: 10px;
|
||||
margin-top: 15px;
|
||||
border-radius: 8px;
|
||||
display: none;
|
||||
}
|
||||
|
||||
@ -166,43 +224,40 @@
|
||||
display: block;
|
||||
}
|
||||
|
||||
.controls {
|
||||
margin-top: 20px;
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
.perf-badge {
|
||||
display: inline-block;
|
||||
padding: 4px 12px;
|
||||
border-radius: 12px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 700;
|
||||
margin-left: 10px;
|
||||
}
|
||||
|
||||
button {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
.perf-badge.sse {
|
||||
background: #f39c12;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 12px 24px;
|
||||
}
|
||||
|
||||
.perf-badge.websocket {
|
||||
background: #27ae60;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.tech-badge {
|
||||
display: inline-block;
|
||||
padding: 4px 10px;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s, box-shadow 0.2s;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
|
||||
}
|
||||
|
||||
button:active {
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
font-size: 0.75rem;
|
||||
background: #3498db;
|
||||
color: white;
|
||||
margin-left: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<header>
|
||||
<h1>🎯 Beacon Tracker - Live Stream</h1>
|
||||
<h1>🎯 Beacon Tracker - Live Stream <span class="tech-badge">FastAPI</span></h1>
|
||||
<div class="status-bar">
|
||||
<div class="status-item">
|
||||
<div class="status-indicator" id="connectionStatus"></div>
|
||||
@ -214,12 +269,28 @@
|
||||
<div class="status-item">
|
||||
<span class="status-text">Кадр: <strong id="frameCounter">0</strong></span>
|
||||
</div>
|
||||
<div class="status-item">
|
||||
<span class="status-text">Метод: <strong id="streamMethod">-</strong></span>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="main-content">
|
||||
<div class="stream-selector">
|
||||
<span style="font-weight: 600;">Выбрать метод передачи:</span>
|
||||
<button id="btnWebSocket" onclick="switchToWebSocket()" class="active">
|
||||
WebSocket (H.264) <span class="perf-badge websocket">Быстрее</span>
|
||||
</button>
|
||||
<button id="btnSSE" onclick="switchToSSE()">
|
||||
SSE (JPEG) <span class="perf-badge sse">Совместимость</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="video-container">
|
||||
<img id="videoFrame" alt="Video stream">
|
||||
<!-- Canvas for WebSocket/JSMpeg streaming -->
|
||||
<canvas id="videoCanvas"></canvas>
|
||||
<!-- Image for SSE/JPEG streaming -->
|
||||
<img id="videoFrame" class="hidden" alt="Video stream">
|
||||
<div class="loading-overlay" id="loadingOverlay">
|
||||
<div class="spinner"></div>
|
||||
<p>Ожидание данных...</p>
|
||||
@ -232,7 +303,7 @@
|
||||
<div class="info-value" id="resolution">-</div>
|
||||
</div>
|
||||
<div class="info-card">
|
||||
<div class="info-label">Размер кадра</div>
|
||||
<div class="info-label">Размер/Битрейт</div>
|
||||
<div class="info-value" id="frameSize">-</div>
|
||||
</div>
|
||||
<div class="info-card">
|
||||
@ -240,8 +311,8 @@
|
||||
<div class="info-value" id="latency">-</div>
|
||||
</div>
|
||||
<div class="info-card">
|
||||
<div class="info-label">Битрейт</div>
|
||||
<div class="info-value" id="bitrate">-</div>
|
||||
<div class="info-label">Потребление памяти</div>
|
||||
<div class="info-value" id="memoryUsage">-</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -255,14 +326,193 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let eventSource = null;
|
||||
let frameCount = 0;
|
||||
let lastFrameTime = Date.now();
|
||||
let fps = 0;
|
||||
let totalBytes = 0;
|
||||
let lastBitrateCalc = Date.now();
|
||||
// Stream mode: 'websocket' or 'sse'
|
||||
let streamMode = 'websocket';
|
||||
|
||||
// SSE variables
|
||||
let eventSource = null;
|
||||
let sseFrameCount = 0;
|
||||
let sseLastFrameTime = Date.now();
|
||||
let sseTotalBytes = 0;
|
||||
let sseLastBitrateCalc = Date.now();
|
||||
|
||||
// WebSocket variables
|
||||
let websocket = null;
|
||||
let player = null;
|
||||
let wsFrameCount = 0;
|
||||
let wsLastFrameTime = Date.now();
|
||||
let reconnectAttempts = 0;
|
||||
const MAX_RECONNECT_ATTEMPTS = 5;
|
||||
|
||||
// Common variables
|
||||
let fps = 0;
|
||||
let statsInterval = null;
|
||||
|
||||
function updateMemoryUsage() {
|
||||
if (performance.memory) {
|
||||
const memMB = (performance.memory.usedJSHeapSize / 1024 / 1024).toFixed(1);
|
||||
document.getElementById('memoryUsage').textContent = `${memMB} MB`;
|
||||
} else {
|
||||
document.getElementById('memoryUsage').textContent = 'N/A';
|
||||
}
|
||||
}
|
||||
|
||||
function switchToWebSocket() {
|
||||
if (streamMode === 'websocket') return;
|
||||
|
||||
streamMode = 'websocket';
|
||||
document.getElementById('btnWebSocket').classList.add('active');
|
||||
document.getElementById('btnSSE').classList.remove('active');
|
||||
|
||||
disconnectSSE();
|
||||
connectWebSocket();
|
||||
}
|
||||
|
||||
function switchToSSE() {
|
||||
if (streamMode === 'sse') return;
|
||||
|
||||
streamMode = 'sse';
|
||||
document.getElementById('btnSSE').classList.add('active');
|
||||
document.getElementById('btnWebSocket').classList.remove('active');
|
||||
|
||||
disconnectWebSocket();
|
||||
connectSSE();
|
||||
}
|
||||
|
||||
function connectWebSocket() {
|
||||
document.getElementById('streamMethod').textContent = 'WebSocket (H.264)';
|
||||
document.getElementById('videoCanvas').classList.remove('hidden');
|
||||
document.getElementById('videoFrame').classList.add('hidden');
|
||||
|
||||
console.log('Connecting to WebSocket video stream...');
|
||||
|
||||
// Build WebSocket URL
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const wsUrl = `${protocol}//${window.location.host}/ws/video`;
|
||||
|
||||
// Connect to native WebSocket
|
||||
websocket = new WebSocket(wsUrl);
|
||||
websocket.binaryType = 'arraybuffer';
|
||||
|
||||
websocket.onopen = () => {
|
||||
console.log('WebSocket connected');
|
||||
updateConnectionStatus(true);
|
||||
hideError();
|
||||
reconnectAttempts = 0;
|
||||
|
||||
// Initialize JSMpeg player
|
||||
const canvas = document.getElementById('videoCanvas');
|
||||
player = new JSMpeg.Player(null, {
|
||||
canvas: canvas,
|
||||
disableGl: false,
|
||||
disableWebAssembly: false,
|
||||
preserveDrawingBuffer: false,
|
||||
progressive: true,
|
||||
throttled: true,
|
||||
chunkSize: 1024 * 32,
|
||||
onVideoDecode: (decoder, time) => {
|
||||
// Update FPS counter
|
||||
const now = Date.now();
|
||||
const timeDiff = (now - wsLastFrameTime) / 1000;
|
||||
if (timeDiff > 0.1) { // Update every 100ms
|
||||
fps = Math.round(1 / timeDiff);
|
||||
document.getElementById('fpsCounter').textContent = fps;
|
||||
wsFrameCount++;
|
||||
document.getElementById('frameCounter').textContent = wsFrameCount;
|
||||
}
|
||||
wsLastFrameTime = now;
|
||||
|
||||
// Update resolution
|
||||
if (decoder.width && decoder.height) {
|
||||
document.getElementById('resolution').textContent =
|
||||
`${decoder.width}×${decoder.height}`;
|
||||
}
|
||||
|
||||
// Hide loading overlay on first frame
|
||||
document.getElementById('loadingOverlay').classList.add('hidden');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
websocket.onmessage = (event) => {
|
||||
if (player && event.data instanceof ArrayBuffer) {
|
||||
const uint8Array = new Uint8Array(event.data);
|
||||
|
||||
if (!player.source) {
|
||||
// Create source buffer if needed
|
||||
player.source = {
|
||||
write: function(data) {
|
||||
if (player.demuxer && player.demuxer.write) {
|
||||
player.demuxer.write(data);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Feed data to JSMpeg
|
||||
if (player.demuxer && player.demuxer.write) {
|
||||
player.demuxer.write(uint8Array);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
websocket.onerror = (error) => {
|
||||
console.error('WebSocket error:', error);
|
||||
updateConnectionStatus(false);
|
||||
showError('Ошибка WebSocket соединения');
|
||||
};
|
||||
|
||||
websocket.onclose = () => {
|
||||
console.log('WebSocket disconnected');
|
||||
updateConnectionStatus(false);
|
||||
|
||||
// Auto-reconnect
|
||||
if (streamMode === 'websocket' && reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
|
||||
reconnectAttempts++;
|
||||
console.log(`Attempting to reconnect (${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})...`);
|
||||
setTimeout(connectWebSocket, 2000);
|
||||
} else if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
|
||||
showError('Потеряно соединение. Нажмите "Переподключиться"');
|
||||
}
|
||||
};
|
||||
|
||||
// Start memory monitoring
|
||||
if (!statsInterval) {
|
||||
statsInterval = setInterval(() => {
|
||||
updateMemoryUsage();
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
// Send periodic ping to keep connection alive
|
||||
const pingInterval = setInterval(() => {
|
||||
if (websocket && websocket.readyState === WebSocket.OPEN) {
|
||||
websocket.send('ping');
|
||||
} else {
|
||||
clearInterval(pingInterval);
|
||||
}
|
||||
}, 30000); // Every 30 seconds
|
||||
}
|
||||
|
||||
function disconnectWebSocket() {
|
||||
if (websocket) {
|
||||
websocket.close();
|
||||
websocket = null;
|
||||
}
|
||||
if (player) {
|
||||
player.destroy();
|
||||
player = null;
|
||||
}
|
||||
if (statsInterval) {
|
||||
clearInterval(statsInterval);
|
||||
statsInterval = null;
|
||||
}
|
||||
}
|
||||
|
||||
function connectSSE() {
|
||||
document.getElementById('streamMethod').textContent = 'SSE (JPEG)';
|
||||
document.getElementById('videoCanvas').classList.add('hidden');
|
||||
document.getElementById('videoFrame').classList.remove('hidden');
|
||||
|
||||
function connect() {
|
||||
if (eventSource) {
|
||||
eventSource.close();
|
||||
}
|
||||
@ -291,17 +541,17 @@
|
||||
document.getElementById('loadingOverlay').classList.add('hidden');
|
||||
|
||||
// Update counters
|
||||
frameCount++;
|
||||
sseFrameCount++;
|
||||
document.getElementById('frameCounter').textContent = data.frame_number;
|
||||
|
||||
// Calculate FPS
|
||||
const now = Date.now();
|
||||
const timeDiff = (now - lastFrameTime) / 1000;
|
||||
const timeDiff = (now - sseLastFrameTime) / 1000;
|
||||
if (timeDiff > 0) {
|
||||
fps = Math.round(1 / timeDiff);
|
||||
document.getElementById('fpsCounter').textContent = fps;
|
||||
}
|
||||
lastFrameTime = now;
|
||||
sseLastFrameTime = now;
|
||||
|
||||
// Update resolution
|
||||
document.getElementById('resolution').textContent = `${data.width}×${data.height}`;
|
||||
@ -316,15 +566,17 @@
|
||||
document.getElementById('latency').textContent = `${latencyMs} ms`;
|
||||
|
||||
// Calculate bitrate
|
||||
totalBytes += data.data_size;
|
||||
const bitrateTime = (now - lastBitrateCalc) / 1000;
|
||||
sseTotalBytes += data.data_size;
|
||||
const bitrateTime = (now - sseLastBitrateCalc) / 1000;
|
||||
if (bitrateTime >= 1.0) {
|
||||
const bitrate = (totalBytes * 8 / bitrateTime / 1000000).toFixed(2);
|
||||
document.getElementById('bitrate').textContent = `${bitrate} Mbps`;
|
||||
totalBytes = 0;
|
||||
lastBitrateCalc = now;
|
||||
const bitrate = (sseTotalBytes * 8 / bitrateTime / 1000000).toFixed(2);
|
||||
document.getElementById('frameSize').textContent = `${bitrate} Mbps`;
|
||||
sseTotalBytes = 0;
|
||||
sseLastBitrateCalc = now;
|
||||
}
|
||||
|
||||
updateMemoryUsage();
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error processing frame:', error);
|
||||
showError('Ошибка обработки кадра: ' + error.message);
|
||||
@ -336,6 +588,24 @@
|
||||
updateConnectionStatus(false);
|
||||
showError('Потеряно соединение с сервером');
|
||||
};
|
||||
|
||||
// Start memory monitoring
|
||||
if (!statsInterval) {
|
||||
statsInterval = setInterval(() => {
|
||||
updateMemoryUsage();
|
||||
}, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
function disconnectSSE() {
|
||||
if (eventSource) {
|
||||
eventSource.close();
|
||||
eventSource = null;
|
||||
}
|
||||
if (statsInterval) {
|
||||
clearInterval(statsInterval);
|
||||
statsInterval = null;
|
||||
}
|
||||
}
|
||||
|
||||
function updateConnectionStatus(connected) {
|
||||
@ -364,19 +634,45 @@
|
||||
|
||||
function reconnect() {
|
||||
document.getElementById('loadingOverlay').classList.remove('hidden');
|
||||
connect();
|
||||
reconnectAttempts = 0;
|
||||
if (streamMode === 'websocket') {
|
||||
disconnectWebSocket();
|
||||
connectWebSocket();
|
||||
} else {
|
||||
disconnectSSE();
|
||||
connectSSE();
|
||||
}
|
||||
}
|
||||
|
||||
function takeScreenshot() {
|
||||
const img = document.getElementById('videoFrame');
|
||||
let dataUrl;
|
||||
|
||||
if (streamMode === 'websocket') {
|
||||
const canvas = document.getElementById('videoCanvas');
|
||||
dataUrl = canvas.toDataURL('image/jpeg', 0.95);
|
||||
} else {
|
||||
const img = document.getElementById('videoFrame');
|
||||
dataUrl = img.src;
|
||||
}
|
||||
|
||||
const link = document.createElement('a');
|
||||
link.download = `beacon_tracker_${Date.now()}.jpg`;
|
||||
link.href = img.src;
|
||||
link.href = dataUrl;
|
||||
link.click();
|
||||
}
|
||||
|
||||
// Connect on page load
|
||||
connect();
|
||||
// Connect on page load with default WebSocket mode
|
||||
if (streamMode === 'websocket') {
|
||||
connectWebSocket();
|
||||
} else {
|
||||
connectSSE();
|
||||
}
|
||||
|
||||
// Cleanup on page unload
|
||||
window.addEventListener('beforeunload', () => {
|
||||
disconnectWebSocket();
|
||||
disconnectSSE();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user