Blog

Install and Configure NGINX with RTMP Protocol for Live Streaming

NGINX / RTMP Protocol

Install and Configure NGINX with RTMP Protocol for Live Streaming

First thing is, lets walk through about the RTMP Protocol, what is the purpose have to use this protocol in the IT industries.

The Real-Time Messaging Protocol (RTMP) is a proprietary protocol developed by Macromedia (now owned by Adobe) for streaming audio, video, and data over the Internet. It’s commonly used in scenarios where live streaming and real-time interaction are essential, such as in live broadcasting, online gaming, and live auctions.

Key Features:

  1. Low Latency: RTMP is designed to minimize latency, which is crucial for live streaming applications.
  2. Support for Various Media: RTMP can handle multiple types of media, including audio, video, and data streams.
  3. Adaptability: It supports both live streams and on-demand content.
  4. Interactive Applications: RTMP is suitable for interactive applications, such as live chat or live commenting during a broadcast.

How RTMP Works:

  • Connection Establishment: RTMP works over a persistent TCP connection. When a client wants to stream content, it first establishes a connection to an RTMP server.
  • Handshake: After the connection is established, a handshake process ensures that both client and server are ready for data transmission.
  • Data Transmission: The client sends media data to the server, which can then distribute it to other clients or broadcast it to viewers.
  • Chunks and Messages: RTMP data is divided into chunks and messages to optimize the transmission and ensure smooth streaming.

Use Cases:

  • Live Streaming: Used by platforms like Twitch and Facebook Live to stream live video content to viewers.
  • Interactive Applications: Utilized in webinars, online tutoring, and live auctions where real-time interaction is required.
  • Online Gaming: Helps in streaming game play with low latency, crucial for competitive gaming

Installation:

Next we go to the Installation part in an Amazon Linux EC2 instance,

2. Upgrade the CentOS packges for benefit from getting the latest software, including new security patches, and all the upgraded technology that comes with latest version.

$ sudo yum update

Fixed the error while upgrading the package:

Error: Failed to download metadata for repo ‘AppStream’: Cannot prepare internal mirrorlist: No URLs in mirrorlist

3. Installed vim and wget packages in CentOS instance that’s required for edit and download the files from the public

$ sudo yum install vim wget git

$ sudo yum groupinstall "Development Tools"

Fixed the error while installing the Development Tools

Error: Failed to download metadata for repo ‘appstream’: Cannot prepare internal mirrorlist: No URLs in mirrorlist

4. Download the NGINX sever and rtmp_module from the repository using the git command

$ sudo git clone https://github.com/arut/nginx-rtmp-module.git

$ sudo git clone https://github.com/nginx/nginx.git

5. Install and configure NGINX server with rtmp_module using the commands below,

$ sudo ./auto/configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --add-module=../nginx-rtmp-module

Once you have installed without any you will see the output like,

Configuration summary
+ using system PCRE2 library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

6. Then execute make and make install commands,

$ make

$ sudo make install

Once the installation has completed, check your nginx Version:

$ sudo /usr/local/nginx/sbin/nginx -V

Just take a copy the nginx.conf original file to the different name: nginx.conf_original, then copy and paste the following lines and save it.

$ sudo vim /usr/local/nginx/nginx.conf

The nginx configurations are below,

user nobody;
worker_processes 5;
pid /run/nginx.pid;
events {
  worker_connections 1024;
}

rtmp_auto_push on;
## RTMP configuration
rtmp {
  server {
    listen 1935;
    chunk_size 4000;
      application cloudishsoft {
        live on;
        interleave on;
        hls on;
        hls_path /tmp/cloudishsoft/hls;
        hls_fragment 15s;
        dash on;
        dash_path /tmp/cloudishsoft/dash;
        dash_fragment 15s;
        record all;
        record_path /tmp/cloudishsoft/rec;
        record_suffix -%d-%b-%y-%T.mp4;
        record_unique on;
        record_append on;
        recorder audio {
        record audio;
        record_suffix .audio.flv;
        }
      }
  }
}

## HTTP configuration
http {
default_type application/octet-stream;
server {
listen 80;
location / {
  root /tmp/cloudishsoft;
  add_header 'Access-Control-Allow-Origin' '*';
  add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  }
  types {
    application/vnd.apple.mepgurl m3u8;
    video/mp2t ts;
    text/html html;
    application/dash+xml mpd;
    }
  }
}

8. Once you have configured the NGINX.CONF file have to make sure that any errors in the configuration using the command below,

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

the output should be looks like,

nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful

9. Start the nginx server using the command below,

$ sudo /usr/local/nginx/sbin/nginx

the nginx has started,

nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful

10. Make sure that the NGINX is running successfully,

$ sudo ps aux | grep nginx

output,

root 41658 0.0 0.0 49896 3008 ? Ss 09:35 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 41659 0.0 0.2 82056 9192 ? S 09:35 0:00 nginx: worker process
nobody 41660 0.0 0.2 82056 9192 ? S 09:35 0:00 nginx: worker process
nobody 41661 0.0 0.1 81792 6908 ? S 09:35 0:00 nginx: cache manager process

Make sure that the ports are listening both RTMP and web server ports using the command below,

$ netstat -anplt

Although RTMP is still widely used, especially in live streaming, its usage is declining in favor of more modern protocols like HLS, DASH (Dynamic Adaptive Streaming over HTTP), and WebRTC, which offer better support for adaptive streaming and compatibility with various devices and browsers. However, RTMP remains an important protocol for specific low-latency streaming applications and legacy systems.

Spread the love

Leave your thought here

Your email address will not be published. Required fields are marked *