什么是 SRS WebRTC?
SRS (Simple Realtime Server) 是一个支持 WebRTC 流媒体的开源媒体服务器。它允许你建立基于 WebRTC 的低延迟直播和实时通信应用。
快速开始
1. 安装 SRS
bash
# 使用 Docker 快速安装
docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \-p 8000:8000/udp -p 10080:10080/udp ossrs/srs:5 \./objs/srs -c conf/rtc.conf
或者从源码编译:
bash
git clone https://github.com/ossrs/srs
cd srs/trunk
./configure --with-ffmpeg --with-ssl --with-hls --with-rtc \--with-http-callback --with-http-api --with-http-server
make
2. 基本配置 (conf/rtc.conf)
nginx
listen 1935;
max_connections 1000;
daemon off;
srs_log_tank console;rtc_server {enabled on;listen 8000; # UDP端口candidate $CANDIDATE; # 你的服务器IP或域名
}http_api {enabled on;listen 1985;
}http_server {enabled on;listen 8080;dir ./objs/nginx/html;
}
3. 启动 SRS
bash
./objs/srs -c conf/rtc.conf
WebRTC 推流与播放
推流 (Publisher)
-
使用 WebRTC 推流 URL 格式:
text
webrtc://your-server-ip/live/streamid
-
可以使用官方 Web 演示页面:
text
http://your-server-ip:8080/players/rtc_publisher.html
播放 (Player)
-
WebRTC 播放 URL 格式:
text
webrtc://your-server-ip/live/streamid
-
官方 Web 播放演示:
text
http://your-server-ip:8080/players/rtc_player.html
进阶配置
1. 设置 TURN 服务器 (穿越 NAT)
nginx
rtc_server {# ... 其他配置 ...turn_server {enabled on;listen 3478;# 必须配置域名或IPcandidate $CANDIDATE;realm ossrs.net;credential ossrs;}
}
2. 与其它协议转换
SRS 支持 WebRTC 与其他协议互转:
-
WebRTC → RTMP
-
WebRTC → HLS
-
RTMP → WebRTC
常见问题
-
ICE 连接失败:
-
确保配置了正确的
candidate
IP -
检查防火墙开放了 UDP 端口 (默认 8000)
-
-
延迟高:
-
确保使用 WebRTC 而不是 HLS 或 HTTP-FLV
-
检查网络状况
-