Nginx: instalasi RTMP

From OnnoWiki
Jump to navigation Jump to search

Sumber: https://obsproject.com/forum/resources/how-to-set-up-your-own-private-rtmp-server-using-nginx.50/


Tulisan ini akan membahas konfigurasi dasar RTMP server di Linux. Dengan kemampuan tersebut kita dapat lebih mengontrol stream kita, membuka kemungkinan orang untuk men-stream kita, kita ingin menstream ke banyak lokasi dll.

Beberapa hal yang dapat dilakukan oleh RTMP server yang mungkin kita inginkan:

  • Stream ke banyak kanal
  • Import stream orang lain ke stream kita.

Langkah yang perlu dilakukan ..


Step 1: Siapkan Box Server

Believe it or not, RTMP is actually extremely light on system resources. Essentially it just grabs data from the input and forwards it on to the output, simple data transfer. Don't believe me? My RTMP server for a long time was a Raspberry Pi, a $35 mini-computer, sitting under my desk, and it was capable of hosting at least 3 simultaneous streams, and I never even stressed it to see how many more it could handle. So I assure you, even a cheap old box would suffice.

If you don't have your own box, a VPS can also work. I recommend Linode as a provider, if you can afford it. This is what I am currently using, and thus far it has worked out well. Just make sure you have enough bandwidth...remember that bandwidth usage will be (the size of a stream) * (the number of people uploading + the number of people downloading). So when I have 2 streamers stream to my server, and I download both of them, I can chew up 10GB of bandwidth in 2 hours.

I recommend using Ubuntu for the server software for the sake of ease, but you can obviously use whatever you want. As long as you get the dependencies for nginx somewhere besides apt, you can follow this guide just fine.

Note to Windows users: This guide focuses on using Linux. If you want to use Windows, you can find Windows binaries for nginx with the RTMP module already included here: http://nginx-win.ecsds.eu/

If you are hosting your server in your home, you will have to forward TCP port 1935 to the box...this varies by router, so look up how to set up port forwarding for your router. Also, I recommend using a service like DynDNS to overcome dynamic IP issues that come up with residential hosting.

Step 2: Installing nginx with RTMP module

Log into your box, and make sure you have the necessary tools to build nginx using the following command:

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

Now a bit of info about nginx (pronounced "engine-X"). nginx is an extremely lightweight web server, but someone wrote a RTMP module for it, so it can host RTMP streams too. However, to add the RTMP module, we have to compile nginx from source rather than use the apt package. Don't worry, it's really easy. Just follow these instructions. :)

Download nginx source code

cd /usr/local/src
wget http://nginx.org/download/nginx-1.7.4.tar.gz
wget http://nginx.org/download/nginx-1.6.1.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

buka

cd /usr/loca/src
tar -zxvf nginx-1.6.1.tar.gz
unzip master.zip


Compile

cd /usr/local/src/nginx-1.6.1
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
make
sudo make install

And nginx is installed! By default it installs to /usr/local/nginx, so to start the server run the following command:

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

And to test to make sure nginx is running, point your browser to http://<your server ip>/ and you should get the "Welcome to nginx!" page.

Step 3: Configuring nginx to use RTMP

Open your config file, located by default at /usr/local/nginx/conf/nginx.conf and add the following at the very end of the file:

Code | Analyze

rtmp {
        server {
                listen 1935;
                chunk_size 4096; 

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

This is an extremely basic configuration with a "live" application that simply forwards the RTMP stream on to whoever requests it. You can play with it some more later. Here's the whole configuration guide, which shows you how to forward streams to other places (such as Twitch), save recordings of uploads, output stats, etc.

Restart nginx with:

   $ sudo /usr/local/nginx/sbin/nginx -s stop
   $ sudo /usr/local/nginx/sbin/nginx

Step 4: Testing!

Your server should now be ready to accept RTMP streams! Let's try it out.

Create a new profile in OBS, and change your Broadcast Settings thusly:

   Streaming Service: Custom
   Server: rtmp://<your server ip>/live
   Play Path/Stream Key: test

You may be wondering where that play path "test" came from. Well, we just made it up, just now. You can basically make up any play path and stream to it, and put that path into an RTMP player, and it will play back. For simple purposes, authentication isn't necessary in my experience. However, authentication IS supported...instructions coming soon!

You should now be able to start streaming to your server. If you hit "Start Streaming" and don't get an error from OBS, that's a good sign.

So how do you watch it? Personally, I have been using vMix to play back RTMP streams and incorporate them into my own stream. If you just want to play back the stream that's being uploaded, you can do so with VLC 2.1.0 (which, as of this writing, is in pre-release, and you can get from the nightlies page). Just Open a Newtork Stream and enter in rtmp://<your server ip>/live/test as the URL. If it all worked right, then you should now be seeing your stream in VLC!

You now have a working RTMP server! Congrats!

What now?

OBS currently doesn't have a way (yet) to import RTMP streams directly. You can capture a VLC window, I suppose, with Window Capture. Once VLC 2.1.0 is released, Faruton will be able to add RTMP support to his video source plugin.

You can also use your RTMP server to forward to other streaming services and channels! Underneath the "record off;" line in your nginx.conf, add the following:

   push rtmp://<other streaming service rtmp url>/<stream key>

And any stream streamed to that application will be forwarded on to the other service, as well as being served up from the server! You can add multiple "pushes" to forward the stream to multiple locations.