<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Liu Lantao</title>
    <description>The latest articles on DEV Community by Liu Lantao (@lax).</description>
    <link>https://dev.to/lax</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F30859%2F186f51bd-c8e1-4790-9418-59c8267ea0d9.jpeg</url>
      <title>DEV Community: Liu Lantao</title>
      <link>https://dev.to/lax</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lax"/>
    <language>en</language>
    <item>
      <title>RTMP/RTMPS relay with stunnel</title>
      <dc:creator>Liu Lantao</dc:creator>
      <pubDate>Thu, 04 Apr 2019 07:01:46 +0000</pubDate>
      <link>https://dev.to/lax/rtmps-relay-with-stunnel-12d3</link>
      <guid>https://dev.to/lax/rtmps-relay-with-stunnel-12d3</guid>
      <description>&lt;p&gt;On May 1st, 2019, Facebook Live will &lt;a href="https://developers.facebook.com/docs/graph-api/changelog/breaking-changes/#live-api-4-24"&gt;deprecate RTMP&lt;/a&gt; from Live API and GoLive page, and enforce using RTMPS.&lt;/p&gt;




&lt;p&gt;I'm relaying the stream through &lt;a href="https://github.com/arut/nginx-rtmp-module"&gt;nginx-rtmp-module&lt;/a&gt; to different platforms such as FB Live, YouTube Live, and Twitch.&lt;br&gt;
Unfortunately &lt;code&gt;nginx-rtmp-module&lt;/code&gt; doesn't support replaying to an &lt;code&gt;rtmps://&lt;/code&gt; address, and the feature is not supposed be added in a recent release.&lt;br&gt;
There's an issue &lt;a href="https://github.com/arut/nginx-rtmp-module/issues/1397"&gt;nginx-rtmp-module#1397&lt;/a&gt; discussing about this.&lt;/p&gt;

&lt;p&gt;RTMPS, which is RTMP over a TLS/SSL connection.&lt;br&gt;
We can use a tunnel software to establish an SSL connection with the server and pass RTMP streams over the connection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.stunnel.org"&gt;Stunnel&lt;/a&gt; is a proxy designed to add TLS encryption functionality to existing clients and servers without any changes in the programs' code.&lt;br&gt;
Stunnel uses the OpenSSL library for cryptography, so it supports whatever cryptographic algorithms are compiled into the library.&lt;/p&gt;

&lt;p&gt;We can install &lt;code&gt;stunnel&lt;/code&gt; with the package manager on a modern OS.&lt;/p&gt;



&lt;p&gt;My current setup without &lt;code&gt;stunnel&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{OBS/FFmpeg/...} -&amp;gt; rtmp://nginx-rtmp-server:1935 -&amp;gt; rtmp://live-api-s.facebook.com:80
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;stunnel&lt;/code&gt;, the setup will be like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{OBS/FFmpeg/...} -&amp;gt; rtmp://nginx-rtmp-server:1935 -&amp;gt; rtmp://stunnel:19350 -&amp;gt; rtmps://live-api-s.facebook.com:443
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;stunnel&lt;/code&gt; is running on port &lt;code&gt;:19350&lt;/code&gt; because nginx is already listening on port &lt;code&gt;:1935&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;I'm installing &lt;code&gt;stunnel&lt;/code&gt; on &lt;code&gt;Debian 9 - stretch&lt;/code&gt; with &lt;code&gt;root&lt;/code&gt; user. You need to switch &lt;code&gt;root&lt;/code&gt; user or use &lt;code&gt;sudo&lt;/code&gt; for every commands listed below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Install stunnel
apt install stunnel4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a minimal configuration of stunnel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Stunnel basic config
cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/stunnel/stunnel.conf 
setuid = stunnel4
setgid = stunnel4
pid=/tmp/stunnel.pid
output = /var/log/stunnel4/stunnel.log
include = /etc/stunnel/conf.d
EOF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable tunnel in &lt;code&gt;/etc/default/stunnel4&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ENABLE=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add a tunnel for Facebook Live address&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# RTMP -&amp;gt; RTMPS tunnel
cat &amp;lt;&amp;lt;EOF &amp;gt; /etc/stunnel/conf.d/fb.conf 
[fb-live]
client = yes
accept = 127.0.0.1:19350
connect = live-api-s.facebook.com:443
verifyChain = no
EOF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;* Please note you need to enable &lt;code&gt;verifyChain = yes&lt;/code&gt; to prevent MITM attacks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;systemctl restart stunnel4 &amp;amp;&amp;amp; systemctl status stunnel4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;We can run a &lt;code&gt;ffmpeg&lt;/code&gt; command to test pushing stream, target to local stunnel port &lt;code&gt;:19350&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ffmpeg -re -i /path/to/video.flv -c:v libx264 -c:a aac -f flv rtmps://127.0.0.1:19350/rtmp/&amp;lt;facebook-live-stream-key&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the tunnel is ready to work!&lt;/p&gt;

&lt;p&gt;Let's replace the URL to local stunnel port within nginx rtmp config.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Change this line
push rtmp://live-api-s.facebook.com:80/rtmp/&amp;lt;facebook-live-stream-key&amp;gt;;
# to
push rtmp://127.0.0.1:19350/rtmp/&amp;lt;facebook-live-stream-key&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All done!&lt;/p&gt;




&lt;p&gt;Lax - Freelancer, System engineer, Tech-Writer&lt;br&gt;
You can find me on Twitter &lt;a href="https://twitter.com/liulantao"&gt;@liulantao&lt;/a&gt; or GitHub &lt;a href="https://github.com/Lax"&gt;@Lax&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
