20221014版本日志

正文摘要

正文

版本日志

SRS 流媒体框架的搭建与配置

一、 SRS流媒体框架是什么

  • SRS是一个简单高效的实时视频服务器,支持RTMP、WebRTC、HLS、HTTP-FLV、SRT、GB28181等多种实时流媒体协议。

  • 简单的单节点架构,适用于大多数场景
    简单的单点架构

  • 这是典型的源站和边缘集群的架构,适用于需要高并发的场景
    源站和边缘集群的架构

二、 安装及配置SRS流媒体框架

1.docker模式安装

1.1 推荐使用Docker启动SRS,这是最简单也是最方便的方式。

1
docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \ registry.cn-hangzhou.aliyuncs.com/ossrs/srs:4 ./objs/srs -c conf/docker.conf

1.2 使用FFmpeg的Docker推流到本机。

1
docker run --network host --rm registry.cn-hangzhou.aliyuncs.com/ossrs/srs:encoder

1.3 或者使用 FFmpeg(点击下载) 或 OBS(点击下载) 推流。

1
ffmpeg -re -i ./doc/source.flv -c copy -f flv rtmp://localhost/live/livestream

实例文件./doc/source.flv在SRS的源代码目录中有。

1
2
3
4
5
# 打开下面的页面播放流(若SRS不在本机,请将localhost更换成服务器IP):

# RTMP (by VLC): rtmp://localhost/live/livestream
# H5(HTTP-FLV): http://localhost:8080/live/livestream.flv
# H5(HLS): http://localhost:8080/live/livestream.m3u8

1.4 你可以推拉多路流到SRS,不需要特殊的设置,按照前面的步骤运行SRS后,改变推拉流的URL就可以。

1
2
3
4
5
6
7
8
9
# rtmp://ip/live/livesteam
# rtmp://ip/live/livesteamN
# rtmp://ip/liveN/livestreamN
# srt://ip:10080?streamid=#!::r=anyM/streamN,m=publish
# webrtc://localhost/anyM/streamN
# http://ip:8080/anyM/streamN.flv
# http://ip:8080/anyM/streamN.m3u8
# https://ip:8080/anyM/streamN.flv
# https://ip:8080/anyM/streamN.m3u8

2.源码安装方式

2.1 新建文件夹并下载源码

1
2
3
mkdir mediaServer

git clone -b 4.0release https://gitee.com/ossrs/srs.git

2.2 编译,注意需要切换到srs/trunk目录:

1
2
3
4
5
cd srs/trunk

./configure

make

2.3 修改并优化服务 文本开启的 端口 1935 1985 8081

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# main config for srs.
# @see full.conf for detail config.

listen 1935;
max_connections 1000;
srs_log_tank file;
srs_log_file ./objs/srs.log;
daemon off;

http_api {
enabled on;
listen 1985;
}
http_server {
enabled on;
listen 8081;
dir ./objs/nginx/html;
}
rtc_server {
enabled on;
listen 8001; # UDP port
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#config-candidate
candidate $CANDIDATE;
}
vhost __defaultVhost__ {
tcp_nodelay on;
min_latency on;

play {
gop_cache off;
queue_length 10;
mw_latency 100;
}

publish {
mr off;
}

hls {
enabled on;
hls_fragment 5;
hls_window 5;
hls_path ./objs/nginx/html;
hls_m3u8_file [app]/[stream].m3u8;
hls_ts_file [app]/[stream]/[2006][01][02]/[15][04][05].ts;
hls_dispose 86400;
}
http_remux {
enabled on;
mount [vhost]/[app]/[stream].flv;
}
rtc {
enabled on;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtmp-to-rtc
rtmp_to_rtc off;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtc-to-rtmp
rtc_to_rtmp off;
}
}

2.4 启动服务

1
./objs/srs -c conf/srs.conf

2.5 检查SRS是否成功启动,可以打开 http://localhost:8081/ ,或者执行命令

1
2
3
4
5
6
7
8
9
10
11
12
# 查看SRS的状态
./etc/init.d/srs status

# 或者看SRS的日志
tail -n 30 -f ./objs/srs.log

# 例如,下面的命令显示SRS正在运行
MB0:trunk $ ./etc/init.d/srs status
SRS(pid 90408) is running. [ OK ]

MB0:trunk $ tail -n 30 -f ./objs/srs.log
[2021-08-13 10:30:36.634][Trace][90408][12c97232] Hybrid cpu=0.00%,0MB, cid=1,1, timer=61,0,0, clock=0,22,25,0,0,0,0,1,0