In the previous article, I guided you through setting up a livestream system using nginx-rtmp. However, in practice, the latency was quite high, falling short of my livestreaming requirements. So I looked for another platform with lower latency, which is SRS.
Table of contents
Open Table of contents
Prerequisites and usage
- Ubuntu server 20.04
- OBS (Free and open source software for video recording and live streaming)
- SRS (SRS is a simple, high efficiency and realtime video server, supports RTMP, WebRTC, HLS, HTTP-FLV, SRT, MPEG-DASH and GB28181)
Step 1: Install SRS
-
Update the system:
sudo apt update sudo apt upgrade -y
-
Install the necessary dependencies:
sudo apt install build-essential automake tclsh cmake unzip pkg-config libssl-dev libpq-dev -y
-
Download and compile SRS:
git clone -b develop https://github.com/ossrs/srs.git cd srs/trunk ./configure make && sudo make install
Step 2: Configure SRS
-
Edit the SRS configuration file: Here there are a lot of configurations for srs that you can see in the
conf
folder. When building srs, it will receive thesrs.conf
file as default config file. We will edit this config file as suggested by srs low latency.sudo apt install nano mv ./conf/srs.conf ./conf/srs.old.conf nano ./conf/srs.conf
Here is config:
listen 1935; max_connections 1000; daemon on; #srs_log_tank console; srs_log_tank file; srs_log_file ./objs/srs.log; http_server { enabled on; listen 8080; dir ./objs/nginx/html; } http_api { enabled on; listen 1985; } stats { network 0; } 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__ { tcp_nodelay on; min_latency on; play { gop_cache off; queue_length 10; mw_latency 100; } publish { mr off; } http_hooks { # http callback when you need notify stream start and stop to your server #enabled on; #on_publish http://localhost:4000/api/livestream/onplay; #on_unpublish http://localhost:4000/api/livestream/onplay; #on_play http://localhost:4000/api/livestream/onplay; #on_stop http://localhost:4000/api/livestream/onplay; } rtc { enabled on; # @see https: //ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtmp-to-rtc rtmp_to_rtc on; # @see https: //ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtc-to-rtmp rtc_to_rtmp on; } http_remux { enabled on; mount [vhost]/[app]/[stream].flv; } }
-
Save and exit the editor:
Step 3: Start SRS
- Compile and link SRS to systemctl:
We will compile and link srs.service
All file will link from folder srs to./configure && make && sudo make install && sudo ln -sf /usr/local/srs/etc/init.d/srs /etc/init.d/srs && sudo cp -f /usr/local/srs/usr/lib/systemd/system/srs.service /usr/lib/systemd/system/srs.service && sudo systemctl daemon-reload && sudo systemctl enable srs
/usr/local/srs
. - Start SRS service
sudo systemctl start srs
- Check SRS status:
sudo systemctl status srs
Step 4: Live Stream
To start live streaming, you can use OBS (Open Broadcaster Software) or any streaming software that supports RTMP.
-
Install OBS:
- Download and install OBS from the OBS Project website.
-
Configure OBS:
- Open OBS, go to
Settings
->Stream
. - Select
Service
asCustom...
. - Enter
Server
asrtmp://localhost/live/
. - Enter
Stream Key
as any string you want, for example:stream
.
- Open OBS, go to
Step 5: Check the Live Stream
Open a vlc and enter the URL:
- RTMP (by VLC): rtmp://localhost/live/livestream
- H5(HTTP-FLV): http://localhost:8080/live/livestream.flv
- H5(HLS): http://localhost:8080/live/livestream.m3u8
If you are streaming, you will see your video playing. With this, you have completed the setup of a live streaming server with SRS.