Purpose:
Recently, the company has assigned me a streaming media project, which includes a live broadcast function. ffmpeg was originally used for push and pull operations. But it is troublesome to frequently start commands on the server side. After consulting the information, I found that the current mainstream technologies include red5, srs, and nginx plug-ins. Among them, srs has the best performance, and the open source author has been maintaining this project and supports the webrtc protocol after version 4.0. So now I choose srs as the live broadcast technology. .
1. Build the srs server: take the centos7 server as an example
1. Download srs server
git clone -b 4.0release https://gitee.com/ossrs/srs.git
If an error is reported:
bash: git: command not found...
The error suggested is that I did not install git on Linux, perform the following steps to install git:
yum install -y git
After the installation is complete, perform step 1 again.
2. Compile, almost all operations of srs need to be operated under srs/trunk
cd srs/trunk
./configure
make
Start the server
The content of the default srs.conf file is as follows, which can be modified according to the actual situation.
./objs/srs -c conf/srs.conf
4. Determine whether srs is running normally
./etc/init.d/srs status
5. View logs
tail -f ./objs/srs.log
6. To test whether it is successful, push the stream to the srs server. You can use ffmpeg or obs.
6.1 Use ffmpeg, which takes up less resources.
ffmpeg -re -i ./doc/source.flv -c copy -f flv -y rtmp://localhost/live/livestream
6.2 Use obs to push streaming
2. OBS download address: https://obsproject.com/download
1: Live screen selection
In Source+, select the picture to be pushed. If there is a camera or camcorder, add "Video Capture Device" and then select the corresponding camera name.
I don’t have a camera here, so I choose my computer desktop live broadcast push and “monitor collection”.
2: Set up streaming server
In the lower right corner, Settings >> Streaming >> Service >> Customize.
Fill in the streaming media server address, here is: rtmp://192.168.2.246/live/
You can fill in the streaming key casually. What I filled in here is: livestream.
So the final playback address is: rtmp://192.168.2.246/live/livestream
3: Push live screen
After the configuration is completed, click "Start Streaming" to push the screen. If there is no error, it means the push is successful. At the same time, there will be relevant information below, such as CPU and so on.
在浏览器 http://192.168.2.246:8080/ 打开控制台,可以看到推送的流信息。
点击播放视频,可以看到,刚才的推送画面了。后面加flv,是因为推流拉流都是用的RTMP。
所以RTMP流的播放地址为:rtmp://192.168.2.246/live/test-livestream.flv
三、RTMP低延时配置
以上基本的直播推流拉流,配置完成。但是测试,延迟还是很大。
根据直播画面和本地时间对比,可以发现延迟差不多有6秒左右,不是很正常。RTMP流,正常延迟时间为1到3秒左右,所以还需要配置。
1:默认配置文件
由于我们以默认的配置文件启动,即srs.conf 这个配置文件。默认配置文件如下:
ubuntu@ubuntu:~/srs/trunk$ cat conf/srs.conf
# 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 on;
http_api {
enabled on;
listen 1985;
}
http_server {
enabled on;
listen 8080;
dir ./objs/nginx/html;
}
rtc_server {
enabled on;
listen 8000; # UDP port
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#config-candidate
candidate $CANDIDATE;
}
vhost __defaultVhost__ {
hls {
enabled on;
}
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:更改配置文件
根据官方文档,可以更改配置文件,低延迟配置,在vhost __ defaultVhost __ 添加以下配置。具体原理可以参考官方文档。
tcp_nodelay on;
min_latency on;
play {
gop_cache off;
queue_length 10;
mw_latency 100;
}
publish {
mr off;
}
最终配置文件为:
listen 1935;
max_connections 1000;
#srs_log_tank file;
#srs_log_file ./objs/srs.log;
daemon on;
http_api {
enabled on;
listen 1985;
}
http_server {
enabled on;
listen 8080;
dir ./objs/nginx/html;
}
rtc_server {
enabled on;
listen 8000; # UDP port
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#config-candidate
candidate $CANDIDATE;
}
vhost __defaultVhost__ {
hls {
enabled on;
}
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;
}
tcp_nodelay on;
min_latency on;
play {
gop_cache off;
queue_length 10;
mw_latency 100;
}
publish {
mr off;
}
}
3:重载配置文件测试
配置完成后,reload重载配置,完成。
./etc/init.d/srs reload
然后再次用obs推流拉流,查看效果,延迟为2秒左右,在正常延迟范围内。