首次上传
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
node_modules/
|
||||||
164
README.md
Normal file
164
README.md
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
# Trae Video Server
|
||||||
|
|
||||||
|
一个基于Electron的专业视频采集和流媒体服务程序,支持实时视频预览和网络流媒体分发。
|
||||||
|
|
||||||
|
## 功能特性
|
||||||
|
|
||||||
|
- 🎥 **设备管理**: 自动检测并列出所有可用的视频采集设备
|
||||||
|
- 📺 **实时预览**: 在应用程序内显示选定设备的实时视频画面
|
||||||
|
- 🌐 **流媒体服务**: 提供HTTP流媒体服务,支持网页端实时观看
|
||||||
|
- 🎨 **现代UI**: 美观的用户界面,支持响应式设计
|
||||||
|
- ⚡ **高性能**: 优化的视频处理和网络传输
|
||||||
|
|
||||||
|
## 系统要求
|
||||||
|
|
||||||
|
- Node.js 16.0 或更高版本
|
||||||
|
- Windows 10/11, macOS 10.14+, 或 Linux
|
||||||
|
- 至少一个可用的视频采集设备(摄像头)
|
||||||
|
|
||||||
|
## 安装和运行
|
||||||
|
|
||||||
|
### 1. 安装依赖
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 启动应用程序
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 生产模式
|
||||||
|
npm start
|
||||||
|
|
||||||
|
# 开发模式(包含开发者工具)
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 构建可执行文件
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
## 使用说明
|
||||||
|
|
||||||
|
### 主要功能
|
||||||
|
|
||||||
|
1. **设备选择**
|
||||||
|
- 启动应用后,程序会自动检测所有可用的视频设备
|
||||||
|
- 在下拉菜单中选择要使用的摄像头
|
||||||
|
- 点击"刷新设备"按钮可重新扫描设备
|
||||||
|
|
||||||
|
2. **开始预览**
|
||||||
|
- 选择设备后,点击"开始预览"按钮
|
||||||
|
- 应用程序下方会显示实时视频画面
|
||||||
|
- 同时启动流媒体服务器
|
||||||
|
|
||||||
|
3. **网络流媒体**
|
||||||
|
- 预览启动后,可通过 `http://localhost:3000/preview` 访问网页版预览
|
||||||
|
- 其他设备可通过网络访问此地址观看实时视频
|
||||||
|
- 支持多客户端同时连接
|
||||||
|
|
||||||
|
4. **停止预览**
|
||||||
|
- 点击"停止预览"按钮结束视频采集
|
||||||
|
- 自动释放设备资源和网络连接
|
||||||
|
|
||||||
|
### 网络访问
|
||||||
|
|
||||||
|
流媒体服务默认运行在端口3000上,提供以下接口:
|
||||||
|
|
||||||
|
- **网页预览**: `http://localhost:3000/preview`
|
||||||
|
- **Socket.IO连接**: 用于实时视频帧传输
|
||||||
|
|
||||||
|
## 技术架构
|
||||||
|
|
||||||
|
### 核心技术栈
|
||||||
|
|
||||||
|
- **Electron**: 跨平台桌面应用框架
|
||||||
|
- **Express**: Web服务器框架
|
||||||
|
- **Socket.IO**: 实时双向通信
|
||||||
|
- **WebRTC**: 视频采集和处理
|
||||||
|
- **HTML5 Canvas**: 视频帧处理
|
||||||
|
|
||||||
|
### 项目结构
|
||||||
|
|
||||||
|
```
|
||||||
|
trae-video-server/
|
||||||
|
├── main.js # Electron主进程
|
||||||
|
├── index.html # 主界面
|
||||||
|
├── package.json # 项目配置
|
||||||
|
├── assets/ # 资源文件
|
||||||
|
│ └── icon.svg # 应用图标
|
||||||
|
├── public/ # 静态资源
|
||||||
|
│ └── preview.html # 网页预览界面
|
||||||
|
└── README.md # 项目文档
|
||||||
|
```
|
||||||
|
|
||||||
|
### 工作流程
|
||||||
|
|
||||||
|
1. **设备检测**: 使用 `navigator.mediaDevices.enumerateDevices()` 获取设备列表
|
||||||
|
2. **视频采集**: 通过 `getUserMedia()` API 获取视频流
|
||||||
|
3. **帧处理**: 使用Canvas将视频帧转换为图像数据
|
||||||
|
4. **网络传输**: 通过Socket.IO将帧数据广播给所有连接的客户端
|
||||||
|
5. **实时显示**: 客户端接收帧数据并实时更新显示
|
||||||
|
|
||||||
|
## 配置选项
|
||||||
|
|
||||||
|
### 视频参数
|
||||||
|
|
||||||
|
默认视频配置(可在代码中修改):
|
||||||
|
- 分辨率: 1280x720
|
||||||
|
- 帧率: 30 FPS(网络传输)
|
||||||
|
- 图像格式: JPEG
|
||||||
|
- 压缩质量: 80%
|
||||||
|
|
||||||
|
### 网络配置
|
||||||
|
|
||||||
|
- 默认端口: 3000
|
||||||
|
- CORS: 允许所有来源
|
||||||
|
- Socket.IO: 支持跨域连接
|
||||||
|
|
||||||
|
## 故障排除
|
||||||
|
|
||||||
|
### 常见问题
|
||||||
|
|
||||||
|
1. **设备检测失败**
|
||||||
|
- 确保摄像头已正确连接
|
||||||
|
- 检查设备权限设置
|
||||||
|
- 重启应用程序
|
||||||
|
|
||||||
|
2. **预览无法启动**
|
||||||
|
- 确保设备未被其他应用占用
|
||||||
|
- 检查浏览器权限设置
|
||||||
|
- 尝试选择不同的设备
|
||||||
|
|
||||||
|
3. **网络连接问题**
|
||||||
|
- 检查防火墙设置
|
||||||
|
- 确保端口3000未被占用
|
||||||
|
- 验证网络连接状态
|
||||||
|
|
||||||
|
### 调试模式
|
||||||
|
|
||||||
|
使用开发模式启动应用以查看详细日志:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
## 许可证
|
||||||
|
|
||||||
|
MIT License - 详见 LICENSE 文件
|
||||||
|
|
||||||
|
## 贡献
|
||||||
|
|
||||||
|
欢迎提交Issue和Pull Request来改进这个项目。
|
||||||
|
|
||||||
|
## 联系方式
|
||||||
|
|
||||||
|
如有问题或建议,请通过以下方式联系:
|
||||||
|
- 创建GitHub Issue
|
||||||
|
- 发送邮件至项目维护者
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Trae Video Server** - 专业的视频采集与流媒体解决方案
|
||||||
34
assets/icon.svg
Normal file
34
assets/icon.svg
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<svg width="64" height="64" viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<defs>
|
||||||
|
<linearGradient id="bg" x1="0%" y1="0%" x2="100%" y2="100%">
|
||||||
|
<stop offset="0%" style="stop-color:#4facfe;stop-opacity:1" />
|
||||||
|
<stop offset="100%" style="stop-color:#00f2fe;stop-opacity:1" />
|
||||||
|
</linearGradient>
|
||||||
|
<linearGradient id="camera" x1="0%" y1="0%" x2="100%" y2="100%">
|
||||||
|
<stop offset="0%" style="stop-color:#ffffff;stop-opacity:1" />
|
||||||
|
<stop offset="100%" style="stop-color:#f0f0f0;stop-opacity:1" />
|
||||||
|
</linearGradient>
|
||||||
|
</defs>
|
||||||
|
|
||||||
|
<!-- Background circle -->
|
||||||
|
<circle cx="32" cy="32" r="30" fill="url(#bg)" stroke="#2196f3" stroke-width="2"/>
|
||||||
|
|
||||||
|
<!-- Camera body -->
|
||||||
|
<rect x="16" y="22" width="32" height="20" rx="4" fill="url(#camera)" stroke="#333" stroke-width="1"/>
|
||||||
|
|
||||||
|
<!-- Camera lens -->
|
||||||
|
<circle cx="32" cy="32" r="8" fill="#333" stroke="#666" stroke-width="1"/>
|
||||||
|
<circle cx="32" cy="32" r="5" fill="#1976d2"/>
|
||||||
|
<circle cx="30" cy="30" r="2" fill="#64b5f6" opacity="0.7"/>
|
||||||
|
|
||||||
|
<!-- Camera flash -->
|
||||||
|
<rect x="42" y="24" width="4" height="3" rx="1" fill="#ffd54f"/>
|
||||||
|
|
||||||
|
<!-- Recording indicator -->
|
||||||
|
<circle cx="20" cy="26" r="2" fill="#f44336"/>
|
||||||
|
|
||||||
|
<!-- Streaming waves -->
|
||||||
|
<path d="M 8 16 Q 12 12 16 16" stroke="#4caf50" stroke-width="2" fill="none" opacity="0.8"/>
|
||||||
|
<path d="M 6 12 Q 12 6 18 12" stroke="#4caf50" stroke-width="2" fill="none" opacity="0.6"/>
|
||||||
|
<path d="M 4 8 Q 12 0 20 8" stroke="#4caf50" stroke-width="2" fill="none" opacity="0.4"/>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 1.5 KiB |
500
index.html
Normal file
500
index.html
Normal file
@@ -0,0 +1,500 @@
|
|||||||
|
<!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, #667eea 0%, #764ba2 100%);
|
||||||
|
min-height: 100vh;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
max-width: 1000px;
|
||||||
|
margin: 0 auto;
|
||||||
|
background: white;
|
||||||
|
border-radius: 15px;
|
||||||
|
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header {
|
||||||
|
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
||||||
|
color: white;
|
||||||
|
padding: 30px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header h1 {
|
||||||
|
font-size: 2.5em;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
font-weight: 300;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header p {
|
||||||
|
font-size: 1.1em;
|
||||||
|
opacity: 0.9;
|
||||||
|
}
|
||||||
|
|
||||||
|
.content {
|
||||||
|
padding: 40px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.device-section {
|
||||||
|
margin-bottom: 40px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section-title {
|
||||||
|
font-size: 1.5em;
|
||||||
|
color: #333;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section-title::before {
|
||||||
|
content: '';
|
||||||
|
width: 4px;
|
||||||
|
height: 24px;
|
||||||
|
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
||||||
|
margin-right: 15px;
|
||||||
|
border-radius: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.device-controls {
|
||||||
|
display: flex;
|
||||||
|
gap: 15px;
|
||||||
|
align-items: center;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 300px;
|
||||||
|
padding: 12px 15px;
|
||||||
|
border: 2px solid #e1e5e9;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 16px;
|
||||||
|
background: white;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
select:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: #4facfe;
|
||||||
|
box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn {
|
||||||
|
padding: 12px 24px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary {
|
||||||
|
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary:hover {
|
||||||
|
transform: translateY(-2px);
|
||||||
|
box-shadow: 0 10px 20px rgba(79, 172, 254, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-secondary {
|
||||||
|
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
|
||||||
|
color: #8b4513;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-secondary:hover {
|
||||||
|
transform: translateY(-2px);
|
||||||
|
box-shadow: 0 10px 20px rgba(252, 182, 159, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-danger {
|
||||||
|
background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
|
||||||
|
color: #8b0000;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-danger:hover {
|
||||||
|
transform: translateY(-2px);
|
||||||
|
box-shadow: 0 10px 20px rgba(255, 154, 158, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn:disabled {
|
||||||
|
opacity: 0.6;
|
||||||
|
cursor: not-allowed;
|
||||||
|
transform: none !important;
|
||||||
|
box-shadow: none !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status {
|
||||||
|
padding: 15px;
|
||||||
|
border-radius: 8px;
|
||||||
|
margin: 20px 0;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status.success {
|
||||||
|
background: #d4edda;
|
||||||
|
color: #155724;
|
||||||
|
border: 1px solid #c3e6cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status.error {
|
||||||
|
background: #f8d7da;
|
||||||
|
color: #721c24;
|
||||||
|
border: 1px solid #f5c6cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status.info {
|
||||||
|
background: #d1ecf1;
|
||||||
|
color: #0c5460;
|
||||||
|
border: 1px solid #bee5eb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preview-section {
|
||||||
|
margin-top: 40px;
|
||||||
|
padding-top: 40px;
|
||||||
|
border-top: 2px solid #f0f0f0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preview-container {
|
||||||
|
background: #f8f9fa;
|
||||||
|
border-radius: 12px;
|
||||||
|
padding: 30px;
|
||||||
|
text-align: center;
|
||||||
|
min-height: 400px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preview-placeholder {
|
||||||
|
color: #6c757d;
|
||||||
|
font-size: 1.2em;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
video {
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 400px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stream-info {
|
||||||
|
margin-top: 20px;
|
||||||
|
padding: 20px;
|
||||||
|
background: #e3f2fd;
|
||||||
|
border-radius: 8px;
|
||||||
|
border-left: 4px solid #2196f3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stream-info h3 {
|
||||||
|
color: #1976d2;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stream-info a {
|
||||||
|
color: #1976d2;
|
||||||
|
text-decoration: none;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stream-info a:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loading {
|
||||||
|
display: inline-block;
|
||||||
|
width: 20px;
|
||||||
|
height: 20px;
|
||||||
|
border: 3px solid #f3f3f3;
|
||||||
|
border-top: 3px solid #4facfe;
|
||||||
|
border-radius: 50%;
|
||||||
|
animation: spin 1s linear infinite;
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes spin {
|
||||||
|
0% { transform: rotate(0deg); }
|
||||||
|
100% { transform: rotate(360deg); }
|
||||||
|
}
|
||||||
|
|
||||||
|
.hidden {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="header">
|
||||||
|
<h1>🎥 Trae Video Server</h1>
|
||||||
|
<p>专业视频采集与流媒体服务平台</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="content">
|
||||||
|
<div class="device-section">
|
||||||
|
<h2 class="section-title">📹 视频设备管理</h2>
|
||||||
|
|
||||||
|
<div class="device-controls">
|
||||||
|
<select id="deviceSelect">
|
||||||
|
<option value="">正在加载设备列表...</option>
|
||||||
|
</select>
|
||||||
|
<button id="refreshBtn" class="btn btn-secondary">🔄 刷新设备</button>
|
||||||
|
<button id="startBtn" class="btn btn-primary" disabled>▶️ 开始预览</button>
|
||||||
|
<button id="stopBtn" class="btn btn-danger hidden">⏹️ 停止预览</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="status" class="status hidden"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="preview-section">
|
||||||
|
<h2 class="section-title">📺 实时预览</h2>
|
||||||
|
|
||||||
|
<div class="preview-container">
|
||||||
|
<div id="previewPlaceholder" class="preview-placeholder">
|
||||||
|
<p>📷 选择视频设备并点击"开始预览"来查看实时画面</p>
|
||||||
|
</div>
|
||||||
|
<video id="previewVideo" class="hidden" autoplay muted></video>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="streamInfo" class="stream-info hidden">
|
||||||
|
<h3>🌐 流媒体服务信息</h3>
|
||||||
|
<p>网页预览地址: <a id="streamUrl" href="#" target="_blank">http://localhost:3000/preview</a></p>
|
||||||
|
<p>其他设备可以通过此链接访问实时视频流</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const { ipcRenderer } = require('electron');
|
||||||
|
|
||||||
|
let currentStream = null;
|
||||||
|
let isStreaming = false;
|
||||||
|
|
||||||
|
// DOM元素
|
||||||
|
const deviceSelect = document.getElementById('deviceSelect');
|
||||||
|
const refreshBtn = document.getElementById('refreshBtn');
|
||||||
|
const startBtn = document.getElementById('startBtn');
|
||||||
|
const stopBtn = document.getElementById('stopBtn');
|
||||||
|
const status = document.getElementById('status');
|
||||||
|
const previewVideo = document.getElementById('previewVideo');
|
||||||
|
const previewPlaceholder = document.getElementById('previewPlaceholder');
|
||||||
|
const streamInfo = document.getElementById('streamInfo');
|
||||||
|
const streamUrl = document.getElementById('streamUrl');
|
||||||
|
|
||||||
|
// 显示状态消息
|
||||||
|
function showStatus(message, type = 'info') {
|
||||||
|
status.textContent = message;
|
||||||
|
status.className = `status ${type}`;
|
||||||
|
status.classList.remove('hidden');
|
||||||
|
|
||||||
|
if (type === 'success' || type === 'error') {
|
||||||
|
setTimeout(() => {
|
||||||
|
status.classList.add('hidden');
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载设备列表
|
||||||
|
async function loadDevices() {
|
||||||
|
try {
|
||||||
|
showStatus('正在加载设备列表...', 'info');
|
||||||
|
|
||||||
|
// 请求媒体权限
|
||||||
|
await navigator.mediaDevices.getUserMedia({ video: true });
|
||||||
|
|
||||||
|
const devices = await navigator.mediaDevices.enumerateDevices();
|
||||||
|
const videoDevices = devices.filter(device => device.kind === 'videoinput');
|
||||||
|
|
||||||
|
deviceSelect.innerHTML = '';
|
||||||
|
|
||||||
|
if (videoDevices.length === 0) {
|
||||||
|
deviceSelect.innerHTML = '<option value="">未找到视频设备</option>';
|
||||||
|
showStatus('未找到可用的视频设备', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceSelect.innerHTML = '<option value="">请选择视频设备</option>';
|
||||||
|
videoDevices.forEach(device => {
|
||||||
|
const option = document.createElement('option');
|
||||||
|
option.value = device.deviceId;
|
||||||
|
option.textContent = device.label || `摄像头 ${device.deviceId.slice(0, 8)}...`;
|
||||||
|
deviceSelect.appendChild(option);
|
||||||
|
});
|
||||||
|
|
||||||
|
showStatus(`成功加载 ${videoDevices.length} 个视频设备`, 'success');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('加载设备失败:', error);
|
||||||
|
deviceSelect.innerHTML = '<option value="">加载设备失败</option>';
|
||||||
|
showStatus('加载设备失败: ' + error.message, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始预览
|
||||||
|
async function startPreview() {
|
||||||
|
const deviceId = deviceSelect.value;
|
||||||
|
if (!deviceId) {
|
||||||
|
showStatus('请先选择一个视频设备', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
startBtn.disabled = true;
|
||||||
|
startBtn.innerHTML = '<span class="loading"></span>启动中...';
|
||||||
|
showStatus('正在启动预览...', 'info');
|
||||||
|
|
||||||
|
// 获取视频流
|
||||||
|
const constraints = {
|
||||||
|
video: {
|
||||||
|
deviceId: { exact: deviceId },
|
||||||
|
width: { ideal: 1280 },
|
||||||
|
height: { ideal: 720 }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
currentStream = await navigator.mediaDevices.getUserMedia(constraints);
|
||||||
|
previewVideo.srcObject = currentStream;
|
||||||
|
|
||||||
|
// 显示预览视频
|
||||||
|
previewPlaceholder.classList.add('hidden');
|
||||||
|
previewVideo.classList.remove('hidden');
|
||||||
|
|
||||||
|
// 启动流媒体服务
|
||||||
|
const result = await ipcRenderer.invoke('start-preview', deviceId);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
isStreaming = true;
|
||||||
|
startBtn.classList.add('hidden');
|
||||||
|
stopBtn.classList.remove('hidden');
|
||||||
|
deviceSelect.disabled = true;
|
||||||
|
refreshBtn.disabled = true;
|
||||||
|
|
||||||
|
// 显示流媒体信息
|
||||||
|
streamUrl.href = result.streamUrl;
|
||||||
|
streamInfo.classList.remove('hidden');
|
||||||
|
|
||||||
|
showStatus('预览已启动,流媒体服务已就绪', 'success');
|
||||||
|
|
||||||
|
// 开始广播视频帧
|
||||||
|
startBroadcasting();
|
||||||
|
} else {
|
||||||
|
throw new Error(result.message);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('启动预览失败:', error);
|
||||||
|
showStatus('启动预览失败: ' + error.message, 'error');
|
||||||
|
|
||||||
|
if (currentStream) {
|
||||||
|
currentStream.getTracks().forEach(track => track.stop());
|
||||||
|
currentStream = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
previewVideo.classList.add('hidden');
|
||||||
|
previewPlaceholder.classList.remove('hidden');
|
||||||
|
} finally {
|
||||||
|
startBtn.disabled = false;
|
||||||
|
startBtn.innerHTML = '▶️ 开始预览';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止预览
|
||||||
|
async function stopPreview() {
|
||||||
|
try {
|
||||||
|
showStatus('正在停止预览...', 'info');
|
||||||
|
|
||||||
|
// 停止视频流
|
||||||
|
if (currentStream) {
|
||||||
|
currentStream.getTracks().forEach(track => track.stop());
|
||||||
|
currentStream = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止流媒体服务
|
||||||
|
await ipcRenderer.invoke('stop-preview');
|
||||||
|
|
||||||
|
// 重置UI
|
||||||
|
isStreaming = false;
|
||||||
|
previewVideo.classList.add('hidden');
|
||||||
|
previewPlaceholder.classList.remove('hidden');
|
||||||
|
streamInfo.classList.add('hidden');
|
||||||
|
|
||||||
|
startBtn.classList.remove('hidden');
|
||||||
|
stopBtn.classList.add('hidden');
|
||||||
|
deviceSelect.disabled = false;
|
||||||
|
refreshBtn.disabled = false;
|
||||||
|
|
||||||
|
showStatus('预览已停止', 'success');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('停止预览失败:', error);
|
||||||
|
showStatus('停止预览失败: ' + error.message, 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 广播视频帧
|
||||||
|
function startBroadcasting() {
|
||||||
|
if (!currentStream || !isStreaming) return;
|
||||||
|
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
|
function captureFrame() {
|
||||||
|
if (!isStreaming || !currentStream) return;
|
||||||
|
|
||||||
|
const video = previewVideo;
|
||||||
|
if (video.videoWidth && video.videoHeight) {
|
||||||
|
canvas.width = video.videoWidth;
|
||||||
|
canvas.height = video.videoHeight;
|
||||||
|
ctx.drawImage(video, 0, 0);
|
||||||
|
|
||||||
|
// 将帧数据发送到主进程
|
||||||
|
const frameData = canvas.toDataURL('image/jpeg', 0.8);
|
||||||
|
ipcRenderer.send('broadcast-frame', frameData);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isStreaming) {
|
||||||
|
setTimeout(captureFrame, 33); // 30 FPS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待视频加载完成后开始捕获
|
||||||
|
previewVideo.addEventListener('loadedmetadata', () => {
|
||||||
|
setTimeout(captureFrame, 1000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 事件监听器
|
||||||
|
deviceSelect.addEventListener('change', () => {
|
||||||
|
startBtn.disabled = !deviceSelect.value;
|
||||||
|
});
|
||||||
|
|
||||||
|
refreshBtn.addEventListener('click', loadDevices);
|
||||||
|
startBtn.addEventListener('click', startPreview);
|
||||||
|
stopBtn.addEventListener('click', stopPreview);
|
||||||
|
|
||||||
|
// 页面加载完成后初始化
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
loadDevices();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
143
main.js
Normal file
143
main.js
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
const { app, BrowserWindow, ipcMain } = require('electron');
|
||||||
|
const path = require('path');
|
||||||
|
const express = require('express');
|
||||||
|
const http = require('http');
|
||||||
|
const socketIo = require('socket.io');
|
||||||
|
const cors = require('cors');
|
||||||
|
|
||||||
|
let mainWindow;
|
||||||
|
let streamingServer;
|
||||||
|
let io;
|
||||||
|
|
||||||
|
// 创建主窗口
|
||||||
|
function createWindow() {
|
||||||
|
mainWindow = new BrowserWindow({
|
||||||
|
width: 1200,
|
||||||
|
height: 800,
|
||||||
|
webPreferences: {
|
||||||
|
nodeIntegration: true,
|
||||||
|
contextIsolation: false,
|
||||||
|
enableRemoteModule: true,
|
||||||
|
webSecurity: false
|
||||||
|
},
|
||||||
|
icon: path.join(__dirname, 'assets', 'icon.svg'),
|
||||||
|
title: 'Trae Video Server'
|
||||||
|
});
|
||||||
|
|
||||||
|
mainWindow.loadFile('index.html');
|
||||||
|
|
||||||
|
// 开发模式下打开开发者工具
|
||||||
|
if (process.argv.includes('--dev')) {
|
||||||
|
mainWindow.webContents.openDevTools();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建流媒体服务器
|
||||||
|
function createStreamingServer() {
|
||||||
|
const app = express();
|
||||||
|
app.use(cors());
|
||||||
|
app.use(express.static(path.join(__dirname, 'public')));
|
||||||
|
|
||||||
|
const server = http.createServer(app);
|
||||||
|
io = socketIo(server, {
|
||||||
|
cors: {
|
||||||
|
origin: "*",
|
||||||
|
methods: ["GET", "POST"]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 提供网页预览接口
|
||||||
|
app.get('/preview', (req, res) => {
|
||||||
|
res.sendFile(path.join(__dirname, 'public', 'preview.html'));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Socket.IO连接处理
|
||||||
|
io.on('connection', (socket) => {
|
||||||
|
console.log('客户端连接到流媒体服务');
|
||||||
|
|
||||||
|
socket.on('disconnect', () => {
|
||||||
|
console.log('客户端断开连接');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const PORT = 3000;
|
||||||
|
server.listen(PORT, () => {
|
||||||
|
console.log(`流媒体服务器运行在 http://localhost:${PORT}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取视频设备列表
|
||||||
|
ipcMain.handle('get-video-devices', async () => {
|
||||||
|
try {
|
||||||
|
const devices = await navigator.mediaDevices.enumerateDevices();
|
||||||
|
const videoDevices = devices.filter(device => device.kind === 'videoinput');
|
||||||
|
return videoDevices.map(device => ({
|
||||||
|
deviceId: device.deviceId,
|
||||||
|
label: device.label || `摄像头 ${device.deviceId.slice(0, 8)}...`
|
||||||
|
}));
|
||||||
|
} catch (error) {
|
||||||
|
console.error('获取设备列表失败:', error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 开始预览
|
||||||
|
ipcMain.handle('start-preview', async (event, deviceId) => {
|
||||||
|
try {
|
||||||
|
// 如果流媒体服务器还没启动,则启动它
|
||||||
|
if (!streamingServer) {
|
||||||
|
streamingServer = createStreamingServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
streamUrl: 'http://localhost:3000/preview',
|
||||||
|
message: '预览已启动,流媒体服务已就绪'
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('启动预览失败:', error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: '启动预览失败: ' + error.message
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 停止预览
|
||||||
|
ipcMain.handle('stop-preview', async () => {
|
||||||
|
try {
|
||||||
|
if (io) {
|
||||||
|
io.emit('stop-stream');
|
||||||
|
}
|
||||||
|
return { success: true, message: '预览已停止' };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('停止预览失败:', error);
|
||||||
|
return { success: false, message: '停止预览失败: ' + error.message };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 广播视频帧到所有连接的客户端
|
||||||
|
ipcMain.on('broadcast-frame', (event, frameData) => {
|
||||||
|
if (io) {
|
||||||
|
io.emit('video-frame', frameData);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.whenReady().then(createWindow);
|
||||||
|
|
||||||
|
app.on('window-all-closed', () => {
|
||||||
|
if (streamingServer) {
|
||||||
|
streamingServer.close();
|
||||||
|
}
|
||||||
|
if (process.platform !== 'darwin') {
|
||||||
|
app.quit();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.on('activate', () => {
|
||||||
|
if (BrowserWindow.getAllWindows().length === 0) {
|
||||||
|
createWindow();
|
||||||
|
}
|
||||||
|
});
|
||||||
12751
package-lock.json
generated
Normal file
12751
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
42
package.json
Normal file
42
package.json
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
{
|
||||||
|
"name": "trae-video-server",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Electron视频采集和流媒体服务程序",
|
||||||
|
"main": "main.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "electron .",
|
||||||
|
"dev": "electron . --dev",
|
||||||
|
"build": "electron-builder"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"electron",
|
||||||
|
"video",
|
||||||
|
"streaming",
|
||||||
|
"capture"
|
||||||
|
],
|
||||||
|
"author": "Trae AI",
|
||||||
|
"license": "MIT",
|
||||||
|
"devDependencies": {
|
||||||
|
"electron": "^27.3.11",
|
||||||
|
"electron-builder": "^24.6.4"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"socket.io": "^4.7.2"
|
||||||
|
},
|
||||||
|
"build": {
|
||||||
|
"appId": "com.trae.videoserver",
|
||||||
|
"productName": "Trae Video Server",
|
||||||
|
"directories": {
|
||||||
|
"output": "dist"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"**/*",
|
||||||
|
"!node_modules/**/*",
|
||||||
|
"node_modules/express/**/*",
|
||||||
|
"node_modules/socket.io/**/*",
|
||||||
|
"node_modules/cors/**/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
333
public/preview.html
Normal file
333
public/preview.html
Normal file
@@ -0,0 +1,333 @@
|
|||||||
|
<!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>
|
||||||
Reference in New Issue
Block a user