Nginx: Basic Live Video Streaming Server

From OnnoWiki
Revision as of 09:34, 11 May 2020 by Onnowpurbo (talk | contribs)
Jump to navigation Jump to search

Sumber: https://opensource.com/article/19/1/basic-live-video-streaming-server

Yang perlu di pikirkan dalam instalasi live streaming video server

  • Stream quality: mau HD atau SD?
  • Viewership: jumlah pemirsa?
  • Storage: mau di rekam atau tidak?
  • Access: apakah private atau open?

Spec Minimal Server:

  • 4GB RAM
  • 20G harddisk
  • 1 core i7

Disini digunakan Real-Time Messaging Protocol (RTMP) untuk menangani audio + video streaming. Alternatif lain adalah WebRTC.

Setting Server

Instalasi nginx

sudo apt update
sudo apt upgrade (optional)
sudo apt -y install nginx

Instalasi RTMP agar nginx dapat menangani media stream:

sudo add-apt-repository universe
sudo apt install libnginx-mod-rtmp

Tambahkan konfigurasi di nginx untuk menangani RTMP,

sudo vi /etc/nginx/nginx.conf

Tambahkan di paling bawah,

rtmp {
        server {
                listen 1935;
                chunk_size 4096; 

                application live {
                        live on;
                        record off;
                }
        }
}

Di save konfigurasi-nya.

Restart nginx

sudo systemctl restart nginx
/etc/init.d/nginx restart




Setting up a BSD server

If you're of the "beastie" persuasion, getting a streaming server up and running is also devilishly easy.

Head on over to the FreeBSD website and download the latest release. Fire up the FreeBSD installer on your computer or virtual machine and go through the initial steps and choose settings that best match your environment. Since this is a server, you'll likely want to set some static network settings.

After the installer finishes and your system reboots, you should have a shiny new FreeBSD system. Like any other freshly installed system, you'll likely want to get everything updated (from this step forward, make sure you're logged in as root):

pkg update
pkg upgrade

I install Nano for editing configuration files:

pkg install nano

This streaming server will use the very powerful and versatile Nginx web server. You can build Nginx using the excellent ports system that FreeBSD boasts.

First, update your ports tree:

portsnap fetch
portsnap extract

Browse to the Nginx ports directory:

cd /usr/ports/www/nginx

And begin building Nginx by running:

make install

You'll see a screen asking what modules to include in your Nginx build. For this project, you'll need to add the RTMP module. Scroll down until the RTMP module is selected and press Space. Then Press Enter to proceed with the rest of the build and installation.

Once Nginx has finished installing, it's time to configure it for streaming purposes.

First, add an entry into /etc/rc.conf to ensure the Nginx server starts when your system boots:

nano /etc/rc.conf

Add this text to the file:

nginx_enable="YES"
stream-server_streamingconfig.png

Nginx configuration

Next, create a webroot directory from where Nginx will serve its content. I call mine stream:

cd /usr/local/www/
mkdir stream
chmod -R 755 stream/

Now that you have created your stream directory, configure Nginx by editing its configuration file:

nano /usr/local/etc/nginx/nginx.conf

Load your streaming modules at the top of the file:

load_module /usr/local/libexec/nginx/ngx_stream_module.so;
load_module /usr/local/libexec/nginx/ngx_rtmp_module.so;

stream-server_modules.png Loading streaming modules

Under the Server section, change the webroot location to match the one you created earlier:

Location / {
root /usr/local/www/stream
}

stream-server_webroot.png Changing webroot location

And finally, add your RTMP settings so Nginx will know how to handle your media streams:

rtmp {
        server {
                listen 1935;
                chunk_size 4096; 

                application live {
                        live on;
                        record off;
                }
        }
}

Save the config. In Nano, you can do this by pressing Ctrl+X, Y, and then Enter.

As you can see, this is a very minimal config that will create a working streaming server. Later, you'll add to this config, but this will provide you with a great starting point.

However, before you can begin your first stream, you'll need to restart Nginx with its new config:

service nginx restart

Set up your streaming software

Broadcasting with OBS

Now that your server is ready to accept your video streams, it's time to set up your streaming software. This tutorial uses the powerful and open source Open Broadcast Studio (OBS).

Head over to the OBS website and find the build for your operating system and install it. Once OBS launches, you should see a first-time-run wizard that will help you configure OBS with the settings that best fit your hardware.

stream-server_autoconfig.png OBS auto-configuration wizard

OBS isn't capturing anything because you haven't supplied it with a source. For this tutorial, you'll just capture your desktop for the stream. Simply click the + button under Source, choose Screen Capture, and select which desktop you want to capture.

Click OK, and you should see OBS mirroring your desktop.

Now it's time to send your newly configured video stream to your server. In OBS, click File > Settings. Click on the Stream section, and set Stream Type to Custom Streaming Server.

In the URL box, enter the prefix rtmp:// followed the IP address of your streaming server followed by /live. For example,

rtmp://IP-ADDRESS/live.

Next, you'll probably want to enter a Stream key—a special identifier required to view your stream. Enter whatever key you want (and can remember) in the Stream key box.

stream-server_streamkey.png Stream key setup

Click Apply and then OK.

Now that OBS is configured to send your stream to your server, you can start your first stream. Click Start Streaming.

If everything worked, you should see the button change to Stop Streaming and some bandwidth metrics will appear at the bottom of OBS.

stream-server_metrics.png OBS stream metrics

If you receive an error, double-check Stream Settings in OBS for misspellings. If everything looks good, there could be another issue preventing it from working.

Viewing your stream

A live video isn't much good if no one is watching it, so be your first viewer!

There are a multitude of open source media players that support RTMP, but the most well-known is probably VLC media player.

After you install and launch VLC, open your stream by clicking on Media > Open Network Stream. Enter the path to your stream, adding the Stream Key you set up in OBS, then click Play. For example,

rtmp://IP-ADDRESS/live/SECRET-KEY.

You should now be viewing your very own live video stream!

stream-server_livevideo.png Live video in VLC

Where to go next?

This is a very simple setup that will get you off the ground. Here are two other features you likely will want to use.

   Limit access: The next step you might want to take is to limit access to your server, as the default setup allows anyone to stream to and from the server. There are a variety of ways to set this up, such as an operating system firewall, .htaccess file, or even using the built-in access controls in the RTMP module.
   Record streams: This simple Nginx configuration will only stream and won't save your videos, but this is easy to add. In the Nginx config, under the RTMP section, set up the recording options and the location where you want to save your videos. Make sure the path you set exists and Nginx is able to write to it.
application live {
             live on;
             record all;
             record_path /var/www/html/recordings;
             record_unique on;
}

The world of live streaming is constantly evolving, and if you're interested in more advanced uses, there are lots of other great resources you can find floating around the internet. Good luck and happy streaming!




Referensi

Pranala Menarik