RTMP Streaming Server

RTMP Overwiew

Live video streaming is something becoming more and more popular on the Internet and platforms like Twitch or YouTube provide a infrastructure to consume endless hours of live and recorded media. While these services provide a basic solution for free, they hold contents generally behind advertisements without a paid subscription. To have full control over your contents and thanks to open-source solutions it is easy to configure an own live streaming server that uses the RTMP protocol.

Requirements

Setting up the Server

1 . Log into the instance via SSH

2 . Update the apt sources lists and upgrade the software already installed on the instance:

apt update && apt upgrade -y

3 . All required packages for the basic server configuration are available via APT. Install nginx and the required packages:

apt install build-essential libpcre3 libpcre3-dev libssl-dev nginx libnginx-mod-rtmp ffmpeg -y

4 . Open the Nginx configuration file /etc/nginx/nginx.conf in a text editor:

nano /etc/nginx/nginx.conf

And add the following lines at the end of the configuration file:

rtmp {
        server {
                listen 1935;
                chunk_size 4096;
                notify_method get;

                application live {
                             on_publish http://localhost/auth;
                             live on;
                             #Set this to "record off" if you don't want to save a copy of your broadcasts
                             record all;
                             # The directory in which the recordings will be stored
                             record_path /var/www/html/recordings;
                             record_unique on;
                }
        }
}

This sets up the live streaming server as well as recording of the streams. These will be stored in the directory /var/www/html/recordings of the instance.

5 . Create the directory for recordings and make it writeable to the web server software:

mkdir -p /var/www/html/recordings
chown -R www-data:www-data /var/www/html/recordings/

7 . Open the file /etc/nginx/sites-enabled/default in a text editor and add a location block to the server configuration:

nano /etc/nginx/sites-enabled/default

libnginx-mod-rtmp does not support authentication by default. To avoid that anybody knowing the stream key may broadcast media, copy / paste the following content into the server configuration block, under the server_name block, to setup a basic authentication mechanism. It will ask for a password when streaming. If the password is not correct, the user will see a 401 - Unauthorized message:

location /auth {
        if ($arg_pwd = 'a_secret_password') {
            return 200;
            }
            return 401;
}

Replace a_secret_password with a secret password of your choice which authenticates against the server for broadcasting streams.

8 . Restart the Nginx web server:

systemctl restart nginx.service

Configuring the OBS Client

To broadcast a stream from a local computer to the streaming server, a broadcast system is required. Download OBS Studio, an open source broadcasting solution, which is available for Linux, Mac OS and Windows.

1 . In the Controls section of the Interface, click on Settings to enter the OBS configuration interface:

OBS

2 . Enter the Stream tab and enter the Information about your streaming instance: Server:

  • Service: Custom
  • Server: rtmp://<instance_ip>/live
  • Stream Key: your_stream?pwd=a_secret_password (replace your_stream with a custom name of your stream and a_secret_password with the password you have set in the Nginx configuration)

OBS

Save the configuration and setup your scene within OBS Studio.

3 . When ready, start broadcasting to your instance by clicking on Start Streaming in the Controls section of OBS.

Connecting to the Stream

The stream can be viewed in your favourite media player, for example VLC media player.

1 . Start VLC and click on Open Media

2 . Click on the Network tab and enter the URL of your Stream:

  • URL: rtmp://<instance_ip>/live/<your_stream>

OBS

3 . Click on Open and your stream will be visible in the media player.

Setting up Multistreaming

It is also possible to rebroadcast a stream to platforms like Youtube, Facebook or Twitch to stream on multiple platforms at the same time.

1 . Open the Nginx configuration file /etc/nginx/nginx.conf in a text editor:

nano /etc/nginx/nginx.conf

2 . Edit the file as required for the different streaming services that you want to use:

rtmp {
        server {
                listen 1935;
                chunk_size 4096;
                notify_method get;

                application live {
                             on_publish http://localhost/auth;
                             live on;
                             record all;
                             record_path /var/www/html/recordings;
                             record_unique on;
                             # Define the applications to which the stream will be pushed, comment them out to disable the ones not needed:
                             push rtmp://localhost/twitch;
                             push rtmp://localhost/facebook;
                }

                      # Twitch Stream Application
                      application twitch {
                          live on;
                          record off;

                          # Only allow localhost to publish
                          allow publish 127.0.0.1;
                          deny publish all;

                          # Push URL with the Twitch stream key
                          push rtmp://live-cdg.twitch.tv/app/<twitch_stream_key>;
                      }

                      # Facebook Stream Application  
                      application facebook {
                          live on;
                          record off;

                          #Only allow localhost to publish
                          allow publish 127.0.0.1;
                          deny publish all;

                          # Push URL with the Facebook stream key
                          push rtmps://live-api-s.facebook.com:443/rtmp/<facebook_stream_key>;

                      }
                  }
}

It is possible to add additional applications in the same way as the examples above.

3 . Restart Nginx to activate the new configuration:

systemctl restart nginx.service

4 . Start broadcasting from OBS. You can now either view your stream via a media player like VLC, but also from broadcasting platforms like twitch:

Twitch Interface

Discover a New Cloud Experience

Deploy SSD Cloud Servers in seconds.