Files
traepro-videoserver/public/preview.html
2025-07-24 22:34:02 +08:00

333 lines
9.4 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>视频流预览 - Trae Video Server</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
border-radius: 15px;
box-shadow: 0 20px 40px rgba(0,0,0,0.2);
overflow: hidden;
max-width: 900px;
width: 100%;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
text-align: center;
}
.header h1 {
font-size: 2.2em;
margin-bottom: 10px;
font-weight: 300;
}
.header p {
font-size: 1.1em;
opacity: 0.9;
}
.content {
padding: 40px;
text-align: center;
}
.video-container {
background: #f8f9fa;
border-radius: 12px;
padding: 30px;
margin-bottom: 30px;
min-height: 400px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.placeholder {
color: #6c757d;
font-size: 1.3em;
margin-bottom: 20px;
}
.placeholder-icon {
font-size: 4em;
margin-bottom: 20px;
opacity: 0.5;
}
#videoStream {
max-width: 100%;
max-height: 500px;
border-radius: 8px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.status {
padding: 15px;
border-radius: 8px;
margin: 20px 0;
font-weight: 500;
}
.status.connected {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status.disconnected {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.status.connecting {
background: #fff3cd;
color: #856404;
border: 1px solid #ffeaa7;
}
.info-panel {
background: #e3f2fd;
border-radius: 8px;
padding: 20px;
border-left: 4px solid #2196f3;
text-align: left;
}
.info-panel h3 {
color: #1976d2;
margin-bottom: 15px;
}
.info-item {
margin-bottom: 10px;
display: flex;
justify-content: space-between;
align-items: center;
}
.info-label {
font-weight: 600;
color: #333;
}
.info-value {
color: #666;
font-family: monospace;
}
.loading {
display: inline-block;
width: 20px;
height: 20px;
border: 3px solid #f3f3f3;
border-top: 3px solid #667eea;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-right: 10px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.hidden {
display: none;
}
@media (max-width: 768px) {
.container {
margin: 10px;
}
.header {
padding: 20px;
}
.header h1 {
font-size: 1.8em;
}
.content {
padding: 20px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🌐 视频流预览</h1>
<p>实时视频流媒体服务</p>
</div>
<div class="content">
<div class="video-container">
<div id="placeholder" class="placeholder">
<div class="placeholder-icon">📹</div>
<p>正在连接视频流...</p>
</div>
<img id="videoStream" class="hidden" alt="视频流">
</div>
<div id="status" class="status connecting">
<span class="loading"></span>正在连接到服务器...
</div>
<div class="info-panel">
<h3>📊 连接信息</h3>
<div class="info-item">
<span class="info-label">服务器地址:</span>
<span class="info-value" id="serverUrl">localhost:3000</span>
</div>
<div class="info-item">
<span class="info-label">连接状态:</span>
<span class="info-value" id="connectionStatus">连接中...</span>
</div>
<div class="info-item">
<span class="info-label">最后更新:</span>
<span class="info-value" id="lastUpdate">--</span>
</div>
<div class="info-item">
<span class="info-label">帧率:</span>
<span class="info-value" id="frameRate">-- FPS</span>
</div>
</div>
</div>
</div>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
const videoStream = document.getElementById('videoStream');
const placeholder = document.getElementById('placeholder');
const status = document.getElementById('status');
const connectionStatus = document.getElementById('connectionStatus');
const lastUpdate = document.getElementById('lastUpdate');
const frameRate = document.getElementById('frameRate');
let frameCount = 0;
let lastFrameTime = Date.now();
let fpsInterval;
// 更新状态显示
function updateStatus(message, type) {
status.textContent = message;
status.className = `status ${type}`;
connectionStatus.textContent = message;
}
// 更新帧率显示
function updateFrameRate() {
const now = Date.now();
const elapsed = now - lastFrameTime;
if (elapsed >= 1000) {
const fps = Math.round((frameCount * 1000) / elapsed);
frameRate.textContent = `${fps} FPS`;
frameCount = 0;
lastFrameTime = now;
}
}
// Socket连接事件
socket.on('connect', () => {
console.log('已连接到服务器');
updateStatus('已连接到服务器', 'connected');
// 开始计算帧率
fpsInterval = setInterval(updateFrameRate, 1000);
});
socket.on('disconnect', () => {
console.log('与服务器断开连接');
updateStatus('与服务器断开连接', 'disconnected');
// 隐藏视频,显示占位符
videoStream.classList.add('hidden');
placeholder.classList.remove('hidden');
placeholder.innerHTML = `
<div class="placeholder-icon">⚠️</div>
<p>连接已断开,请检查服务器状态</p>
`;
// 停止帧率计算
if (fpsInterval) {
clearInterval(fpsInterval);
frameRate.textContent = '-- FPS';
}
});
// 接收视频帧
socket.on('video-frame', (frameData) => {
frameCount++;
// 显示视频帧
videoStream.src = frameData;
videoStream.classList.remove('hidden');
placeholder.classList.add('hidden');
// 更新最后更新时间
const now = new Date();
lastUpdate.textContent = now.toLocaleTimeString();
});
// 停止流
socket.on('stop-stream', () => {
console.log('视频流已停止');
updateStatus('视频流已停止', 'disconnected');
videoStream.classList.add('hidden');
placeholder.classList.remove('hidden');
placeholder.innerHTML = `
<div class="placeholder-icon">⏹️</div>
<p>视频流已停止</p>
`;
if (fpsInterval) {
clearInterval(fpsInterval);
frameRate.textContent = '-- FPS';
}
});
// 连接错误处理
socket.on('connect_error', (error) => {
console.error('连接错误:', error);
updateStatus('连接失败', 'disconnected');
});
// 页面卸载时清理
window.addEventListener('beforeunload', () => {
if (fpsInterval) {
clearInterval(fpsInterval);
}
socket.disconnect();
});
</script>
</body>
</html>