Skip to content

Instantly share code, notes, and snippets.

@aitchkhan
Last active March 3, 2023 00:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aitchkhan/929200045e676521cfab968d642017d3 to your computer and use it in GitHub Desktop.
Save aitchkhan/929200045e676521cfab968d642017d3 to your computer and use it in GitHub Desktop.

1. Compile nginx with rtmp module

Clone nginx-rtmp-module

$ git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git

Install nginx dependencies

$ sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

Download nginx

$ wget http://nginx.org/download/nginx-1.15.9.tar.gz
$ tar -xf nginx-1.15.9.tar.gz
$ cd nginx-1.15.9

Compile nginx

$ ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
$ make -j 1
$ sudo make install

2. Create nginx configuration file

complete nginx.conf

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/;
        }
    }
}

3. Start nginx

Test the configuration file

$ /usr/local/nginx/sbin/nginx -t

Start nginx in the background

$ /usr/local/nginx/sbin/nginx

4. Pushing live stream to nginx using rtmp

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.

Options 1: From existing rtmp stream already in h264

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_...
}

Options 2: From local webcam/different rtmp/file

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

5. Clone nodejs-nginx-rtmp-balancer

$ 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

6. Update Node.js app config

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment