Files
42_150/串流轉發.md
Timmy ce4f9f19bd 調校 MediaMTX 抗閃爍:加大寫入佇列、強制 RTSP 走 TCP
實況偶發「閃一下」,查 journalctl 為應用層問題(非網路):
- reader is too slow → 加 writeQueueSize: 1024(預設 512)吸收瞬時卡頓
- UDP decode errors → rtspTransports: [tcp] 杜絕 UDP 掉包花屏
.103 線上設定已同步並重啟驗證;另補上 init 產生的 CLAUDE.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 20:54:28 +08:00

91 lines
4.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 用轉發伺服器突破「單一 RTSP 連線」限制
## 為什麼需要
TL-IPC42EW-4 同時只接受極少的 RTSP 連線(實測只剩 1 個空檔)。多個觀看端直連會撞 `406 Not Acceptable`
**解法**架一台轉發restream / proxy伺服器。它只用**那 1 條** RTSP 去拉流一次,再對外複製成任意數量的 RTSP / HLS / WebRTC給無限個觀看端。
```
攝影機 ──(1 條 RTSP)──> 轉發伺服器 ──┬─> 觀看端 A (VLC/RTSP)
├─> 觀看端 B (瀏覽器/HLS)
├─> 觀看端 C (手機/WebRTC)
└─> ... 不限數量
```
## 方案一MediaMTX推薦單一執行檔、免寫程式
### 安裝macOS
```sh
brew install mediamtx
# 或到 https://github.com/bluenviron/mediamtx/releases 下載對應平台的 binary
```
### 啟動
用本 repo 附的 [`mediamtx.yml`](./mediamtx.yml)
```sh
mediamtx ./mediamtx.yml
```
### 觀看(把 `<本機IP>` 換成跑 MediaMTX 那台的 IP
| 協定 | 網址 | 用途 |
|------|------|------|
| RTSP | `rtsp://<本機IP>:8554/cam` | VLC、ffplay、NVR |
| HLS | `http://<本機IP>:8888/cam` | 瀏覽器直接看(延遲較高) |
| WebRTC | `http://<本機IP>:8889/cam` | 瀏覽器低延遲 |
`/cam` = 主碼流(H.265 1080p)`/cam-sub` = 子碼流(H.264 704×576)。
`sourceOnDemand: yes` 的好處:沒人看時 MediaMTX 不連攝影機,會把那唯一的連線槽還回去;有人看才連,且多人看也只佔 1 條。
> HLS 不支援 H.265,瀏覽器用 HLS 看請改走 `/cam-sub`(H.264)WebRTC 視瀏覽器而定。VLC/ffplay 走 RTSP 則 H.265 沒問題。
## 方案二ffmpeg 直接轉推(已有 RTMP/HLS 伺服器時)
```sh
# 拉一次,轉推到你既有的 RTMP 伺服器(如 nginx-rtmp
ffmpeg -rtsp_transport tcp -i rtsp://192.168.42.150:554/stream1 \
-c copy -f flv rtmp://<你的伺服器>/live/cam
```
`-c copy` 不轉碼、不吃 CPU。要產生 HLS 切片也可 `-f hls output.m3u8`
## 已部署CT103 / 192.168.42.103
MediaMTX v1.19.1 已實際架在 `192.168.42.103`Ubuntu 24.04systemd 服務 `mediamtx`,設定檔 `/usr/local/etc/mediamtx.yml`,內容同本 repo 的 [`mediamtx.yml`](./mediamtx.yml))。
**觀看網址**
**觀看網址**
| 串流 | RTSPVLC/ffplay | HLS瀏覽器 | WebRTC低延遲 |
|------|------|------|------|
| 主碼流 H.265 1080p | `rtsp://192.168.42.103:8554/cam` | `http://192.168.42.103:8888/cam`HLS 不支援 H.265 | `http://192.168.42.103:8889/cam` |
| 子碼流 H.264 704×576 | `rtsp://192.168.42.103:8554/cam-sub` | `http://192.168.42.103:8888/cam-sub` | `http://192.168.42.103:8889/cam-sub` |
**驗證結果**
- 主碼流 → HEVC 1920×1080@15fps + PCM A-law 音訊 ✓
- 子碼流 → H.264 704×576@15fps + PCM A-law 音訊 ✓
- **6 條連線同時看主碼流6/6 全部成功**(攝影機端只佔 1 條406 問題解除)。
⚠️ 此 MediaMTX 為全開放HTTP API 9997 與認證皆關閉),任何能連到 .103 的人都能觀看/推流。對外或不信任網段請開啟認證。
### 抗閃爍調校2026-06-22
實況曾偶發「閃一下」,查 `journalctl -u mediamtx` 為應用層而非網路問題,已在 [`mediamtx.yml`](./mediamtx.yml) 加兩個全域設定(.103 已同步並重啟驗證):
- `writeQueueSize: 1024`(預設 512— 觀看端瞬時吃不下 1080p H.265 時原本佇列塞爆會直接丟幀log: `reader is too slow, discarding N frames``i/o timeout` 斷線);加大佇列吸收瞬時卡頓。
- `rtspTransports: [tcp]` — 強制 RTSP 只走 TCP杜絕 UDP 掉包造成的 H.265 花屏log: UDP session `decode errors` 後秒斷)。只影響 RTSPHLS/WebRTC 不受影響。
弱端(手機/wifi建議直接看子碼流 `cam-sub`(H.264 704×576),碼率低不會塞。
## 選哪個
- 只是想多人/多端看 → **MediaMTX**,三行設定搞定,支援 RTSP/HLS/WebRTC 全包。
- 已經有 RTMP/HLS 基礎設施、想接進去 → ffmpeg `-c copy` 轉推。