$ git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
$ sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
$ wget http://nginx.org/download/nginx-1.15.9.tar.gz
$ tar -xf nginx-1.15.9.tar.gz
$ cd nginx-1.15.9
$ ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
$ make -j 1
$ sudo make install
The default location for nginx conf is /usr/local/nginx/conf/nginx.conf
or /etc/nginx/nginx.conf
Replace the existing one with the one below:
worker_processes auto;
events {
worker_connections 1024;
}
# RTMP configuration
rtmp {
server {
listen 1935; # Listen on standard RTMP port
chunk_size 4000;
application show {
live on;
# Turn on HLS
hls on;
hls_path /mnt/hls/;
hls_fragment 3;
hls_playlist_length 60;
# disable consuming the stream from nginx as rtmp
deny play all;
}
}
}
http {
sendfile off;
tcp_nopush on;
aio on;
directio 512;
default_type application/octet-stream;
server {
listen 8080;
location / {
# Disable cache
add_header 'Cache-Control' 'no-cache';
# CORS setup
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
# allow CORS preflight requests
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
types {
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /mnt/;
}
}
}
Test the configuration file
$ /usr/local/nginx/sbin/nginx -t
Start nginx in the background
$ /usr/local/nginx/sbin/nginx
nginx accepts rtmp stream as input. For a proper HLS stream the video codec should be x264
and audio codec aac/mp3/ac3
most commonly being aac
.
if you have an existing rtmp stream in the correct codec, you can skip ffmpeg and tell nginx to pull the stream directly. In order to do so add a pull directive under application
section in nginx.conf
like so:
application show {
live on;
pull rtmp://example.com:4567/sports/channel3 live=1;
# to change the local stream name use this syntax: ... live=1 name=ch3;
# other directives...
# hls_...
}
To achieve the stream encoding and muxing we will use the almighty ffmpeg
.
To install ffmpeg using PPA run these commands
$ sudo add-apt-repository ppa:mc3man/trusty-media
$ sudo apt-get update
$ sudo apt-get install ffmpeg
Stream file example-vid.mp4
$ ffmpeg -re -i example-vid.mp4 -vcodec libx264 -vprofile baseline -g 30 -acodec aac -strict -2 -f flv rtmp://localhost/show/stream
Once streaming is up. We need to make sure Node.js server is distributing the load.
Assuming Node.js latest LTS is installed on the machine. or use nvm
to install Node.js
$ https://github.com/deluxor/nodejs-nginx-rtmp-balancer.git
$ cd nodejs-nginx-rtmp-balancer
$ cd caller
$ npm install
$ screen -dmS node node app.js
$ cd receiver
$ npm install
$ screen -dmS node node app.js
Open caller/config.json
in an editor and update the following:
"load_balancer_address": "53.53.53.10", //replace this with tht public IP of your server
"load_balancer_key": "1e06725f825882d0636caa877c1bcf368c8fabad" // use a long secure key here to avoid forgery
Open receiver/config.json
in and editor and update the following:
"port": 3040,
"key": "1e06725f825882d0636caa877c1bcf368c8fabad", // use the same secure key you have used in `caller/config.json`
That's all. The Node.js receiver and caller are running on respective ports.
http://51.15.59.160:3030/servers
http://51.15.59.160:3030/stats