nginx, pronounced "engine X", is a web server that can also be used as a reverse proxy, load balancer, mail proxy, HTTP cache and, why not, RTMP server. It is free and open-source software, released under the terms of the 2-clause BSD license.
For the purpose of this trial, we will see how to install and configure the server on a Raspberry Pi board running Raspberry Pi OS Lite.
Install nginx with RTMP support
First, we must install the server and an add-on module that will allow it to handle the RTMP protocol.
After the installation is complete, we should be able to reach the welcome page simply by entering the IP address of the server in our favorite browser, http://192.168.1.18/ for us.
Configure the RTMP server
The way nginx and its modules work is determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the directory /etc/nginx. For details, please check out the Beginner’s Guide and other resources available in the nginx documentation.
To enable the RTMP protocol, edit the configuration file
then add these few lines at the very end
finally save the file and restart the server so that the new configuration will be loaded
In this example, we are configuring the RTMP server to listen on the port 1935 (the default RTMP port), and to handle an application named live. This application has the live mode (one-to-many broadcasting) enabled. The HLS output is also enabled, the playlist and the fragments will be saved in /tmp/hls (if the directory does not exist it will be created).
The complete reference about the available RTMP directives can be found here.
Configure the HTTP server
We need to configure the HTTP server so that it can access the files in /tmp/hls for clients to play HLS. nginx uses the so called Server Blocks to serve multiple sites in parallel, let's change the configuration of the default one
then save the file and restart the server once again
Configure the app
From CamON Live Streaming app settings, enable the Live streaming adapter and configure it
- in he Server field, specify the RTMP URL for the application we configured, rtmp://192.168.1.18/live in this example
- in the Stream field enter a streaming key of your choice, let's use spynet
TIP: the streaming key will be used by nginx as the base name for the HLS files
To start the stream use the arrow icon in the bottom-right corner of the main screen. By tapping on it a countdown will be shown, at the end of which the device will connect to nginx.
TIP: during the countdown, tap on the arrow again if you wish to abort
Let's see it in action
To verify that everything is working as expected, we can use VLC as the client to see the nginx broadcast.
It is possible to see the HLS output using the URL http://192.168.1.18/hls/spynet.m3u8, where hls is the location we configured for the HTTP server to find the files and spynet is the streaming key we have chosen.
It is also possible to see the RTMP output using the URL rtmp://192.168.1.18/live/spynet, where live is the name of the application we configured and spynet is the streaming key.
Embed the player
For a better user experience, we may want to embed the player in our web page, This way the broadcast will be available with no extra effort. As the player, Video.js is a good choice to see the HLS broadcast.
Let's create our index.html page in /var/www/html
with the following HTML code
TIP: the key point is to set the correct source, src="/hls/spynet.m3u8", as described above
After the file has been saved (no need to restart the server), by navigating to address of the server, http://192.168.1.18/, we can see the new homepage in action
Some small tweaks
Since we are planning to broadcast our video over the Internet, we should make the server publicly reachable. To keep it simple, we should consider setting up port forwarding and the Dynamic DNS as described in this post.
This way the server will be reachable from anywhere at http://public_ip_address:8282/ or http://myserver.dyndns.org:8282/.
As discussed, HLS is continuously writes files to disk while updating the playlist and the fragments. This consumes resources and can dramatically reduce the life of the SD card used by the server as storage. A better solution is to use a ramdisk to temporary store those files.
Examine the available memory to find out how much we can use.
Examine the typical HLS disk usage to find out how much memory we expect to need.
Create a folder where to mount the ramdisk.
Add an entry to fstab to configure the ramdisk (50M is enough for this example).
Reboot the server.
Verify that the ramdisk was mounted.
Change the nginx configuration so that HLS files will be saved in /mnt/ramdisk/hls instead of in /tmp/hls.
Change the nginx configuration so that the HTTP server will know where to find the HLS files.
Restart the server.
Verify that the ramdisk is now used.
Your server should now run much smoother!