Streaming to Twitch and PeerTube simultaneously using nginx on Oracle cloud
November 22, 2021 [Programming, Tech]Simulcasting RTMP using NGINX
I want people to be able to watch my Matrix and Rust live coding streams using free software, so I'd like to simulcast to PeerTube as well as Twitch.
This is possible using NGINX and its RTMP module. It does involve building NGINX from source, but I actually found that reasonably easy to do.
Why Oracle cloud?
I would never recommend using Oracle for anything, but they do provide up to two virtual machines in their cloud for free, and the one I am using has been consistently available with very good connectivity, in a London data centre since I set it up several months ago.
So, we are making our lives more difficult by trying to do this on Oracle Linux, which is a derivative of RHEL.
Building NGINX and its RTMP module on Oracle Linux
I ran these commands on my Oracle cloud instance (running Oracle Linux):
sudo yum install git pcre-devel openssl-devel mkdir nginx cd nginx wget http://nginx.org/download/nginx-1.21.4.tar.gz git clone https://github.com/arut/nginx-rtmp-module.git cd nginx-1.21.4 ./configure --add-module=../nginx-rtmp-module/ make sudo make install
After all this NGINX was installed to /usr/local/nginx/.
Creating the NGINX config file for RTMP simulcasting
Next I edited the NGINX config file by typing:
sudo nano /usr/local/nginx/conf/nginx.conf
And pasted in this config at the bottom of the file:
rtmp { server { listen 2036; chunk_size 4096; application live { live on; record off; push rtmp://live.twitch.tv/app/live_INSERT_TWITCH_STREAM_KEY; push rtmp://diode.zone/live/INSERT_PEERTUBE_STREAM_KEY; } } }
Notice that you will need to get your Twitch stream key from Twitch -> Creator Dashboard -> Settings -> Stream, then Copy next to the Primary Stream Key.
To get a PeerTube stream ID, you will need to go to your PeerTube page and click Publish, then Go Live, choose your channel and choose Go Live. Note that if you want the streams to record and be available later, you have to create a new stream key each time you start a stream, and change it in nginx.conf.
If you use a different PeerTube server (I use diode.zone) then you'll need to change the server name in the config file above too.
Make sure your config file is saved with the right URLs in it.
Opening ports
To send RTMP traffic to my server, I needed to open the right port to the Oracle cloud instance. That involved creating an ingress rule, and adding a firewall rule.
Creating an ingress rule
In the web interface, I went to the menu in the top left, clicked Compute, then Instances.
I clicked on my instance's name, then I clicked on the name of the subnet in the details (on the right).
I clicked on Default security list for..., then Add Ingress Rules.
I made an ingress rule with Source Type=CIDR, Source CIDR=0.0.0.0/0, IP Protocol=TCP, Source Port Range=(blank, meaning all), Destination Port Range=2036
Adding a firewall rule
Then I ssh'd into the machine and ran these commands to create a firewall rule allowing the traffic:
sudo firewall-cmd --zone=public --permanent --add-port=2036/tcp sudo firewall-cmd --reload
Stop and Start NGINX
After creating the config file and opening the right port, I needed to start NGINX.
Every time I change the config file, I need to restart it.
If it's already running, I stop it with:
sudo /usr/local/nginx/sbin/nginx -s stop
and then I start it up again with
sudo /usr/local/nginx/sbin/nginx
I can check whether it's happy by looking at the log files, for example to see any errors:
less /usr/local/nginx/logs/error.log
Starting the stream
Now I go into OBS and go to File -> Settings -> Stream and choose the type as Custom, and the Server as rtmp://1.1.1.1:2036/live. (But instead of 1.1.1.1 I put the public IP address of my instance, which I found by clicking the name of the instance in the Oracle cloud management console.)