SRT 协议
SRT(Secure Reliable Transport)是由 Haivision 开发的开源低延迟传输协议。它在不可靠的网络环境中提供安全、可靠的数据传输,特别适合远程连线、广电级采集等对抗网络抖动要求较高的场景。
| 属性 | 值 |
|---|---|
| 默认端口 | 6000 |
| 传输层 | UDP(SRT 协议层) |
| 推流 | ✅ 支持 |
| 拉流 | ✅ 支持 |
| 延迟 | 120ms+(可配置) |
| Feature Flag | srt |
Feature 启用
Section titled “Feature 启用”[features]srt = ["dep:plugin-srt", "plugin-srt/srt", "srt-tokio"]srt: enable: true listen_addr: ":6000" latency_ms: 120 max_bandwidth: 0 passphrase: null| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
enable | bool | true | 是否启用 SRT 插件 |
listen_addr | string | ":6000" | 监听地址 |
latency_ms | u32 | 120 | 传输延迟(毫秒) |
max_bandwidth | u64 | 0 | 最大带宽(字节/秒,0=不限) |
passphrase | Option | null | 加密密码(10-79 字符) |
latency_ms 是 SRT 的核心参数,控制发送端到接收端的缓冲延迟:
- 低延迟场景(局域网):
60-120ms - 城市级传输:
120-500ms - 跨国传输:
500-2000ms
SRT 支持 AES 加密,通过 passphrase 配置:
srt: passphrase: "my-secret-key-12345"密码长度必须在 10-79 字符之间。推流端和拉流端必须使用相同的密码。
使用 FFmpeg 推流
Section titled “使用 FFmpeg 推流”# 基本推流ffmpeg -re -i input.mp4 -c copy -f mpegts "srt://localhost:6000?streamid=publish/live/test"
# 带加密的推流ffmpeg -re -i input.mp4 -c copy -f mpegts \ "srt://localhost:6000?streamid=publish/live/test&passphrase=my-secret-key-12345"
# 指定延迟参数ffmpeg -re -i input.mp4 -c copy -f mpegts \ "srt://localhost:6000?streamid=publish/live/test&latency=200000"
# 摄像头采集推流ffmpeg -f v4l2 -i /dev/video0 \ -c:v libx264 -preset ultrafast -tune zerolatency \ -c:a aac -b:a 128k \ -f mpegts "srt://localhost:6000?streamid=publish/live/camera"使用 OBS 推流
Section titled “使用 OBS 推流”- 进入 设置 → 直播
- 服务选择 自定义
- 服务器填写:
srt://your-server:6000?streamid=publish/live/test - 点击 开始推流
StreamID 格式
Section titled “StreamID 格式”SRT 通过 streamid 参数区分推流和拉流:
- 推流:
streamid=publish/{streamPath} - 拉流:
streamid=subscribe/{streamPath}或streamid=play/{streamPath}
使用 FFplay 拉流
Section titled “使用 FFplay 拉流”ffplay "srt://localhost:6000?streamid=subscribe/live/test"使用 FFmpeg 录制
Section titled “使用 FFmpeg 录制”ffmpeg -i "srt://localhost:6000?streamid=subscribe/live/test" -c copy output.mp4使用 VLC 拉流
Section titled “使用 VLC 拉流”- 打开 VLC,选择 媒体 → 打开网络串流
- 输入 URL:
srt://localhost:6000?streamid=subscribe/live/test - 点击 播放
SRT 推入的流可以通过其他协议播放:
# SRT 推流ffmpeg -re -i input.mp4 -c copy -f mpegts \ "srt://localhost:6000?streamid=publish/live/test"
# 通过 RTMP 拉流ffplay rtmp://localhost:1935/live/test
# 通过 HTTP-FLV 拉流ffplay http://localhost:8080/flv/live/test.flv
# 通过 HLS 拉流ffplay http://localhost:8080/hls/live/test/index.m3u8SRT vs RTMP
Section titled “SRT vs RTMP”| 特性 | SRT | RTMP |
|---|---|---|
| 传输层 | UDP | TCP |
| 延迟 | 可配置(120ms+) | 1-3 秒 |
| 弱网抗性 | 强(FEC + ARQ) | 弱 |
| 加密 | AES 加密 | 无内置加密 |
| 防火墙友好 | 一般(UDP) | 好(TCP) |
| 工具支持 | FFmpeg/OBS/vMix | 广泛 |
SRT 在弱网环境下的表现远优于 RTMP,推荐在以下场景使用 SRT 替代 RTMP:
- 跨公网远程连线
- 移动网络推流
- 网络质量不稳定的环境
联系我们
微信公众号:不卡科技
腾讯频道:流媒体技术
QQ 频道:p0qq0crz08
QQ 群:751639168