|
| 1 | +# WHEP Client Demo |
| 2 | + |
| 3 | +这个演示程序展示了如何使用WHEP客户端连接到WHEP服务器并接收音视频流。 |
| 4 | + |
| 5 | +## 功能特性 |
| 6 | + |
| 7 | +- 连接到WHEP服务器 (默认: localhost:9090) |
| 8 | +- 接收H264视频流并解码为RGB格式 |
| 9 | +- 接收Opus音频流并解码为f32格式 |
| 10 | +- 保存前10张视频帧到 `/tmp/whep-client/` 目录 |
| 11 | +- 保存前10秒音频数据到 `/tmp/whep-client/` 目录 |
| 12 | +- 实时显示接收进度和统计信息 |
| 13 | + |
| 14 | +## 使用方法 |
| 15 | + |
| 16 | +### 1. 启动WHEP服务器 |
| 17 | + |
| 18 | +首先需要在另一个终端启动WHEP服务器: |
| 19 | + |
| 20 | +```bash |
| 21 | +cd /home/blue/Code/rust/wayshot/lib/wrtc |
| 22 | +cargo run --example whep_server2_demo |
| 23 | +``` |
| 24 | + |
| 25 | +服务器将在 `localhost:9090` 启动,并提供测试音视频流。 |
| 26 | + |
| 27 | +### 2. 运行WHEP客户端演示 |
| 28 | + |
| 29 | +在新的终端中运行客户端演示: |
| 30 | + |
| 31 | +```bash |
| 32 | +cd /home/blue/Code/rust/wayshot/lib/wrtc |
| 33 | +cargo run --example whep_client_demo |
| 34 | +``` |
| 35 | + |
| 36 | +### 3. 查看输出结果 |
| 37 | + |
| 38 | +程序将在 `/tmp/whep-client/` 目录下创建以下文件: |
| 39 | + |
| 40 | +#### 视频文件 |
| 41 | +- `frame_001_1920x1080.rgb` - 第1帧RGB数据 |
| 42 | +- `frame_001_1920x1080.txt` - 第1帧信息 (宽度、高度、数据大小) |
| 43 | +- `frame_002_1920x1080.rgb` - 第2帧RGB数据 |
| 44 | +- `frame_002_1920x1080.txt` - 第2帧信息 |
| 45 | +- ... (共10帧) |
| 46 | + |
| 47 | +#### 音频文件 |
| 48 | +- `first_10_seconds_audio.raw` - 前10秒原始音频数据 (f32格式) |
| 49 | +- `first_10_seconds_audio_info.txt` - 音频信息 (采样率、声道数、时长等) |
| 50 | + |
| 51 | +## 文件格式说明 |
| 52 | + |
| 53 | +### RGB视频帧格式 |
| 54 | +- 格式: RGB24 (每像素3字节) |
| 55 | +- 分辨率: 1920x1080 (可变) |
| 56 | +- 文件大小: 宽度 × 高度 × 3 字节 |
| 57 | + |
| 58 | +### 音频数据格式 |
| 59 | +- 格式: 32位浮点数 (little-endian) |
| 60 | +- 采样率: 48000 Hz |
| 61 | +- 声道: 2 (立体声) |
| 62 | +- 文件大小: 样本数 × 4 字节 |
| 63 | + |
| 64 | +## 查看和转换输出文件 |
| 65 | + |
| 66 | +### 转换RGB帧为图片 |
| 67 | + |
| 68 | +使用FFmpeg将RGB数据转换为PNG图片: |
| 69 | + |
| 70 | +```bash |
| 71 | +ffmpeg -f rawvideo -pixel_format rgb24 -video_size 1920x1080 \ |
| 72 | + -i /tmp/whep-client/frame_001_1920x1080.rgb \ |
| 73 | + /tmp/whep-client/frame_001.png |
| 74 | +``` |
| 75 | + |
| 76 | +### 转换音频为WAV |
| 77 | + |
| 78 | +使用FFmpeg将原始音频转换为WAV文件: |
| 79 | + |
| 80 | +```bash |
| 81 | +ffmpeg -f f32le -ar 48000 -ac 2 \ |
| 82 | + -i /tmp/whep-client/first_10_seconds_audio.raw \ |
| 83 | + /tmp/whep-client/first_10_seconds_audio.wav |
| 84 | +``` |
| 85 | + |
| 86 | +## 预期输出 |
| 87 | + |
| 88 | +程序运行时会显示类似以下的输出: |
| 89 | + |
| 90 | +``` |
| 91 | +WHEP Client Demo |
| 92 | +================ |
| 93 | +Connecting to WHEP server at: http://localhost:9090 |
| 94 | +Attempting to connect to WHEP server: http://localhost:9090 |
| 95 | +H264 video decoder initialized |
| 96 | +Opus audio decoder initialized |
| 97 | +WHEP connection established successfully |
| 98 | +Received video frame #1: 1920x1080 (6220800 bytes) |
| 99 | + Saved frame #1 to: /tmp/whep-client/frame_001_1920x1080.rgb |
| 100 | +Received audio packet #1: 48000 Hz, 960 samples (20.00ms) |
| 101 | +Progress: 1 video frames, 0.02 seconds audio |
| 102 | +... |
| 103 | +Successfully saved 10 video frames |
| 104 | +Collected 10.00 seconds of audio, saving... |
| 105 | +Saved audio data to: /tmp/whep-client/first_10_seconds_audio.raw |
| 106 | +================================================== |
| 107 | +WHEP Client Demo Summary |
| 108 | +========================= |
| 109 | +Total video frames received: 10 |
| 110 | +Total audio duration received: 10.00 seconds |
| 111 | +Output directory: /tmp/whep-client |
| 112 | +Files created: |
| 113 | + - frame_001_1920x1080.rgb (6220800 bytes) |
| 114 | + - frame_001_1920x1080.txt |
| 115 | + - frame_002_1920x1080.rgb (6220800 bytes) |
| 116 | + - ... |
| 117 | + - first_10_seconds_audio.raw (3840000 bytes) |
| 118 | + - first_10_seconds_audio_info.txt |
| 119 | +``` |
| 120 | + |
| 121 | +## 故障排除 |
| 122 | + |
| 123 | +### 连接失败 |
| 124 | +- 确保WHEP服务器正在运行在 `localhost:9090` |
| 125 | +- 检查防火墙设置 |
| 126 | +- 确保端口9090未被其他程序占用 |
| 127 | + |
| 128 | +### 编译错误 |
| 129 | +- 确保所有依赖项已正确安装 |
| 130 | +- 运行 `cargo check` 检查代码问题 |
| 131 | +- 确保在正确的目录中运行命令 |
| 132 | + |
| 133 | +### 没有收到数据 |
| 134 | +- 检查服务器是否正确启动 |
| 135 | +- 确认服务器正在发送数据 |
| 136 | +- 查看程序输出中的错误信息 |
| 137 | + |
| 138 | +## 技术实现 |
| 139 | + |
| 140 | +- **WHEP协议**: 实现了标准的WebRTC-HTTP Egress Protocol |
| 141 | +- **H264解码**: 使用OpenH264库进行Annex-B格式解码 |
| 142 | +- **Opus解码**: 使用audiopus库进行音频解码 |
| 143 | +- **YUV转换**: 实现YUV420到RGB24的实时转换 |
| 144 | +- **并发处理**: 使用tokio异步编程处理多个数据流 |
| 145 | + |
| 146 | +## 依赖项 |
| 147 | + |
| 148 | +- `tokio` - 异步运行时 |
| 149 | +- `openh264` - H264编解码器 |
| 150 | +- `audiopus` - Opus音频编解码器 |
| 151 | +- `anyhow` - 错误处理 |
| 152 | +- `env_logger` - 日志记录 |
| 153 | +- `serde_json` - JSON序列化 |
| 154 | +- `reqwest` - HTTP客户端 |
0 commit comments