Just save an article from https://medium.com/@yenthanh/setup-a-rtmp-livestream-server-in-15-minutes-with-srs-1b0046c77267
Livestreaming? Nowaday this is a new trend, livestream industry is growing rapidly and unstoppable, many big players have joined this game e.g. Facebook, Instagram,…
So do you want to join this game too? To implement a livestreaming system in production for a livestream app with millions of people is not easy, but if you just want to create a simple livestreaming server which can support like 5k people, it will be not a big problem. Actually it just takes you some simple steps.
First, I think you wanna to know some basics about livestreaming tech. There are many technic for livestreaming, some of the most commons are HLS, DASH, RTMP, RTP/RTSP and WebRTC.
Each of the technic have their strengths and weaknesses:
- HLS and DASH which are widely supported on iOS and Android are easier for player and device hardware, easier for scaling through a CDN, but they have very high latency, usually 30 seconds which I can’t consider ‘live’ stream. I used to reduce HLS’s latency to 6 seconds but still too much, if you want to create a livestream for a tv show which doesn’t strictly require ‘realtime’ so HLS/DASH is an approriate choice.
- RTMP/RTSP are better for livestreaming in ‘live’ meaning. We can easily reduce the latency of RTMP or RTSP to around 1 second with just some simple setup and a good connection to the server, many streaming apps are using RTMP protocol nowaday. The only and biggest weakness of RTMP is it requires Flash and is not easily to be integrated into a website
- WebRTC is something called the future for livestreaming, it is a peer-to-peer protocol which can reach ‘realtime’ latency for livestreaming(under 1 second). The weakness of WebRTC is it is hard when we need scaling, currently it is just approriate livestreaming in online meeting which require a small value of peers. There are many solution to overcome this, such as a hybrid solution combining WebRTC for input and RTMP/HLS/DASH for output.
So let’s get started with RTMP livestreaming
There are couples of libraries for setup livestreaming server, one of the most simple and advanced is SRS (https://github.com/ossrs/srs). This library is widely used for many livestreaming apps nowaday. We will setup our server with this library in some simple step:
1. Go to our server and Download SRS:
$ cd ~ && git clone https://github.com/ossrs/srs && cd srs/trunk
2. Build SRS:
$ ./configure && make
3. Run SRS:
$ ./objs/srs -c conf/srs.conf
Ok, if you can see srs running, everything seems to be fine. Let’s test our server:
- Download and install OBS application (https://obsproject.com) which is a free app for pushing livestreaming to RTMP livestream server.
- After you have started OBS, go to Menu File -> Setting. In Setting window, select Stream.
- In the URL section, put the rtmp link to our server: rtmp://<your_server_ip>/app (e.g. rtmp://126.96.36.199/app)
- In the Stream key section, put: live (or any stream key you like)
- Click OK to save our setting.
- In the Sources section of OBS window, you can add some source to the screen such as Media Source (a video file), Window Capture (record computer screen), Video Capture Device (your webcam).
- Now everything is ready, press Start Streaming, in the right-down corner, if you see a green status then we have succesfully push our stream onto our server. You can take a look at the server’s terminal (where we used to run the SRS before) to see some log.
- Open a RTMP player, such as https://www.hlsplayer.net/rtmp-player. Input our livestream link, which is rtmp://<your_server_ip>/app/live,if everything is ok, you can see the livestream we are pushing from OBS now. Cheers!
Some Advanced setup
You can open the config file we have run for SRS (conf/srs.conf). There are many other config template files for running with SRS, you can also try to open them and understand the configuration, it is quite easy.
There are still many works until we can finish our livestream server such as setup security (with token or IP restriction), reduce latency, output to HLS with addaptive bitrate. All of those can be configurated in SRS. You can take a look at SRS’ WIKI to learn more advanced things.
If you need any help or any question, please comment, I will try to help with my best. Cheers!