SRS
本指南基于 SRS 7.0 release 版本,从源码编译安装为 systemd 服务。
环境准备(Ubuntu 22.04+ 推荐)
bash
sudo apt update && sudo apt upgrade -y
sudo apt install -y git gcc g++ build-essential make cmake autoconf libtool pkg-config libssl-dev libsrt-dev libffmpeg-dev zlib1g-dev libpcre3-dev zlib1g-dev lsof net-tools curl wget logrotate下载 7.0 release 源码(推荐 release tarball,避免 develop 分支):
bash
# https://github.com/ossrs/srs/srs-7.0release
tar -xzf srs-7.0release.tar.gz
cd srs-7.0release/trunk2. 源码编译
SRS v6.0 默认已开启多数功能
bash
# --single-thread=off 关闭单线程
# --sanitizer=off 关闭调试内存
./configure --prefix=/server/srs --single-thread=off --sanitizer=off
make -j$(nproc)
sudo make install验证二进制:
bash
/server/srs/objs/srs -vsystemctl
Ubuntu20使用systemctl管理服务,我们在init.d的基础上新增了systemctl的配置:
bash
sudo ln -sf /server/srs/etc/init.d/srs /etc/init.d/srs
sudo cp -f /server/srs/usr/lib/systemd/system/srs.service /etc/systemd/system/srs.service
sudo systemctl daemon-reload
udo systemctl enable srs验证服务
shell
sudo ss -tulnp | grep srs端口说明
| 协议 | 端口 | 需求 | 说明 |
|---|---|---|---|
| TCP | 1935 | 必要 | RTMP 推流 / 拉流(OBS、FFmpeg 默认端口) |
| TCP | 1443 | 必要 | RTMPS(加密 RTMP,默认关闭需手动开启) |
| TCP | 1985 | 必要 | HTTP API |
| TCP | 1990 | 必要 | HTTPS API(对应 1985 加密版) |
| TCP | 8081 | 必要 | HTTP Server 默认为8080 改为8081 |
| TCP | 8088 | 必要 | HTTPS 流媒体(对应 8080 加密版,HTTPS-FLV/HTTPS-HLS) |
| UDP | 8000 | 必要 | WebRTC 媒体 |
扩展协议端口(按需开启)
1. SRT(可靠低延迟传输)
- 默认端口:9000 UDP
- 场景:安防设备、远距离弱网推流
2. RTSP
- 默认端口:554 TCP
- 场景:摄像头 RTSP 接入、国标 GB28181 对接
3. MPEG-TS 转推端口
| 端口 | 协议 | 作用 |
|---|---|---|
| 8935 | UDP | UDP-MPEGTS 推流接入 |
| 8936 | TCP | HTTP-FLV 反向推流接入(流转换器) |
4. WebRTC 备用端口
- 10080 UDP:部分配置用作 WebRTC 备选媒体端口,应对 UDP 8000 被封禁场景
- 可配置 TCP 端口:WebRTC over TCP 兜底传输
SRS 集群 / 源站代理端口(Origin Cluster)
| 默认端口 | 协议 | 代理对应原生端口 |
|---|---|---|
| 11935 | TCP | 代理 RTMP 1935 |
| 11985 | TCP | 代理 HTTP API 1985 |
| 18080 | TCP | 代理 HTTP 8080 |
| 18000 | UDP | 代理 WebRTC 8000 |
| 20080 | UDP | 代理 SRT 9000 |
防火墙 / 安全组放行最小端口清单
普通直播(RTMP + HTTP-FLV + HLS)
TCP:1935, 1985, 8080
默认8080 改为8081
带 WebRTC 低延迟直播
TCP:1935,1985,8080
UDP:8000
全功能部署(加密 + SRT+RTSP)
TCP:1935,1985,8080,1990,8088,1443,554
UDP:8000,9000,8935
九、防火墙与安全放行
9.1 Firewalld(CentOS)
默认8080 改为8081
bash
firewall-cmd --permanent --add-port=1935/tcp --add-port=1985/tcp --add-port=8080/tcp --add-port=1990/tcp --add-port=8088/tcp --add-port=1443/tcp --add-port=554/tcp
firewall-cmd --permanent --add-port=8000/udp --add-port=9000/udp --add-port=8935/udp
firewall-cmd --reload9.2 UFW(Ubuntu)
bash
sudo ufw allow 1935/tcp
sudo ufw allow 1985/tcp
# 默认8080 改为8081
sudo ufw allow 8081/tcp
sudo ufw allow 1990/tcp
sudo ufw allow 8088/tcp
sudo ufw allow 1443/tcp
sudo ufw allow 554/tcp
sudo ufw allow 8000/udp
sudo ufw allow 9000/udp
sudo ufw allow 8935/udp
sudo ufw reload
3. 配置文件准备(/server/srs/conf/srs.conf)
编辑 /server/srs/conf/srs.conf
nginx
# main config for srs.
# @see full.conf for detail config.
max_connections 1000;
srs_log_tank file;
srs_log_file ./logs/srs.log;
daemon on;
rtmp {
listen [::]:1935;
}
rtmps {
enabled on;
listen [::]:1443;
key /certs/cert.key;
cert /certs/cert.crt;
}
http_api {
enabled on;
listen [::]:1985;
auth {
# whether enable the HTTP AUTH.
# Overwrite by env SRS_HTTP_API_AUTH_ENABLED
# default: off
enabled on;
# The username of Basic authentication:
# Overwrite by env SRS_HTTP_API_AUTH_USERNAME
username xxxx;
# The password of Basic authentication:
# Overwrite by env SRS_HTTP_API_AUTH_PASSWORD
password xxxx;
}
https {
# Whether enable HTTPS API.
# default: off
enabled on;
# The listen endpoint for HTTPS API.
# default: 1990
listen [::]:1990;
# The SSL private key file, generated by:
# openssl genrsa -out server.key 2048
# default: ./conf/server.key
key /certs/cert.key;
# The SSL public cert file, generated by:
# openssl req -new -x509 -key server.key -out server.crt -days 3650 -subj "/C=CA/ST=Toronto/L=Toronto/O=Me/OU=Me/CN=ossrs.io"
# default: ./conf/server.crt
cert /certs/cert.crt;
}
}
http_server {
enabled on;
listen [::]:8081;
dir ./objs/nginx/html;
https {
# Whether enable HTTPS Streaming.
# Overwrite by env SRS_HTTP_SERVER_HTTPS_ENABLED
# default: off
enabled on;
# The listen endpoint for HTTPS Streaming.
# Overwrite by env SRS_HTTP_SERVER_HTTPS_LISTEN
# default: 8088
listen [::]:8088;
# The SSL private key file, generated by:
# openssl genrsa -out server.key 2048
# Overwrite by env SRS_HTTP_SERVER_HTTPS_KEY
# default: ./conf/server.key
key /certs/cert.key;
# The SSL public cert file, generated by:
# openssl req -new -x509 -key server.key -out server.crt -days 3650 -subj "/C=CA/ST=Toronto/L=Toronto/O=Me/OU=Me/CN=ossrs.io"
# Overwrite by env SRS_HTTP_SERVER_HTTPS_CERT
# default: ./conf/server.crt
cert /certs/cert.crt;
}
}
rtc_server {
enabled on;
listen [::]:8000; # UDP port
# @see https://ossrs.io/lts/en-us/docs/v7/doc/webrtc#config-candidate
# candidate $CANDIDATE;
# 支持 域名 ipv4 ipv6
candidate xxxx.com;
}
vhost __defaultVhost__ {
hls {
enabled on;
}
http_remux {
enabled on;
mount [vhost]/[app]/[stream].flv;
}
rtc {
enabled on;
# @see https://ossrs.io/lts/en-us/docs/v7/doc/webrtc#rtmp-to-rtc
rtmp_to_rtc off;
# @see https://ossrs.io/lts/en-us/docs/v7/doc/webrtc#rtc-to-rtmp
rtc_to_rtmp off;
}
play{
gop_cache_max_frames 2500;
}
# 增加SRT
srt {
enabled on;
srt_to_rtmp on;
}
# ==================== DVR 录制 ====================
dvr {
enabled on;
dvr_path ./objs/nginx/html/record/[app]/[stream].[timestamp].mp4;
dvr_plan segment;
dvr_duration 60;
dvr_wait_keyframe on;
}
http_hooks {
enabled on;
on_dvr http://xxxx.com:8080/api/srs/hooks/dvr;
}
}日志查看:
bash
journalctl -u srs -f
tail -f /server/srs/log/srs.logSRS 录制配置指南
picar-server 提供录制 API,底层调用 SRS Raw DVR API;完成文件经 on_dvr 回调写入 MySQL。
1. SRS vhost 配置示例
nginx
vhost __defaultVhost__ {
dvr {
enabled on;
dvr_path ./objs/nginx/html/record/[app]/[stream].[timestamp].mp4;
dvr_plan segment;
dvr_duration 60;
dvr_wait_keyframe on;
}
http_hooks {
enabled on;
on_dvr http://x.qolome.com:8080/api/srs/hooks/dvr;
}
}
http_api {
enabled on;
listen 1985;
raw_api {
enabled on;
allow_update on;
}
}