WebRTC 协议
WebRTC 是实现浏览器端超低延迟音视频通信的标准技术。Monibuca V6 基于 WHIP(WebRTC-HTTP Ingestion Protocol)和 WHEP(WebRTC-HTTP Egress Protocol)标准实现推流和拉流,底层使用 rustrtc 引擎提供完整的 ICE/DTLS/SRTP 支持。
| 属性 | 值 |
|---|---|
| 信令传输 | HTTP(WHIP/WHEP) |
| 媒体传输 | UDP(ICE/DTLS/SRTP) |
| 推流 | ✅ 支持(WHIP) |
| 拉流 | ✅ 支持(WHEP) |
| 延迟 | < 500ms |
| Feature Flag | webrtc |
Feature 启用
Section titled “Feature 启用”[features]webrtc = ["dep:plugin-webrtc"]webrtc: enable: true port_range: "udp:9000-9100" pli_interval: 2 public_ip: "" enable_cors: true cors_origins: - "*" ice_servers: - urls: - "stun:stun.l.google.com:19302"| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
enable | bool | true | 是否启用 WebRTC 插件 |
port_range | string | "udp:9000-9100" | UDP 端口范围 |
pli_interval | u64 | 2 | PLI(关键帧请求)间隔(秒) |
public_ip | string | "" | 公网 IP(NAT 穿透时使用) |
enable_cors | bool | true | 是否启用 CORS |
cors_origins | Vec | ["*"] | 允许的跨域来源 |
ice_servers | Vec | [] | ICE 服务器列表 |
ICE 服务器配置
Section titled “ICE 服务器配置”ice_servers: # STUN 服务器(NAT 穿透用) - urls: - "stun:stun.l.google.com:19302"
# TURN 服务器(中继用) - urls: - "turn:turn.example.com:3478" username: "user" credential: "pass"| 类型 | 编码 | 说明 |
|---|---|---|
| 视频 | H.264 | 广泛支持 |
| 视频 | H.265 | 部分浏览器支持 |
| 音频 | Opus | WebRTC 标准音频编码 |
WHIP 推流
Section titled “WHIP 推流”API 端点
Section titled “API 端点”POST /webrtc/whip/{streamPath}Content-Type: application/sdp请求体为 SDP Offer,响应返回 SDP Answer。
使用 FFmpeg 推流
Section titled “使用 FFmpeg 推流”FFmpeg 6.1+ 支持 WHIP 推流:
ffmpeg -re -i input.mp4 \ -c:v libx264 -preset ultrafast -tune zerolatency \ -c:a libopus \ -f whip "http://localhost:8080/webrtc/whip/live/test"使用 OBS 推流
Section titled “使用 OBS 推流”OBS 30+ 内置 WHIP 支持:
- 进入 设置 → 直播
- 服务选择 WHIP
- 服务器填写:
http://your-server:8080/webrtc/whip/live/test - 点击 开始推流
WHEP 拉流
Section titled “WHEP 拉流”API 端点
Section titled “API 端点”POST /webrtc/whep/{streamPath}Content-Type: application/sdp请求体为 SDP Offer,响应返回 SDP Answer。
浏览器拉流示例
Section titled “浏览器拉流示例”async function play(streamPath) { const pc = new RTCPeerConnection({ iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] });
pc.addTransceiver('video', { direction: 'recvonly' }); pc.addTransceiver('audio', { direction: 'recvonly' });
pc.ontrack = (event) => { document.getElementById('player').srcObject = event.streams[0]; };
const offer = await pc.createOffer(); await pc.setLocalDescription(offer);
const response = await fetch(`/webrtc/whep/${streamPath}`, { method: 'POST', headers: { 'Content-Type': 'application/sdp' }, body: offer.sdp, });
const answer = await response.text(); await pc.setRemoteDescription({ type: 'answer', sdp: answer });}
play('live/test');内置测试页面
Section titled “内置测试页面”Monibuca 提供内置的 WebRTC 测试页面,方便快速验证功能:
| 页面 | URL | 说明 |
|---|---|---|
| 推流测试 | /webrtc/test/publish | 使用摄像头/屏幕推流 |
| 拉流测试 | /webrtc/test/subscribe | 播放已有的 WebRTC 流 |
在浏览器中直接访问即可使用。
NAT 穿透
Section titled “NAT 穿透”在有 NAT 的网络环境下,需要正确配置以确保 WebRTC 连通:
方案一:配置公网 IP
Section titled “方案一:配置公网 IP”webrtc: public_ip: "203.0.113.1" # 服务器公网 IP方案二:配置 STUN/TURN 服务器
Section titled “方案二:配置 STUN/TURN 服务器”webrtc: ice_servers: - urls: - "stun:stun.l.google.com:19302" - urls: - "turn:turn.example.com:3478" username: "user" credential: "pass"方案三:端口映射
Section titled “方案三:端口映射”确保 port_range 中配置的 UDP 端口范围在防火墙上已开放:
# 开放 UDP 端口范围sudo ufw allow 9000:9100/udpWebRTC 推流后,可以通过其他协议拉取:
# WebRTC WHIP 推流后...
# 通过 RTMP 拉流(Opus 自动转码为 AAC)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.m3u8联系我们
微信公众号:不卡科技
腾讯频道:流媒体技术
QQ 频道:p0qq0crz08
QQ 群:751639168