sudo su
# Stop services listen 80/443 port
#/opt/local/etc/init.d/openresty stop
source ~/env.sh
source /root/.acme.sh/acme.sh.env
# Only have IP v6 public Address
acme.sh --issue -d blog.zhenglei.net --standalone --listen-v6
Suppose there are two linux box, a local box and a remote box, the local box unable to access some URL, due to the firewall limitation, while the remote box have no limitation.
Now we config a exit node, to bypass the firewall of the local linux box.
Remore linux: Enable IP forward & Add exit-node:
sudo sed -i “s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/” /etc/sysctl.conf
sudo sed -i “s/#net.ipv6.conf.all.forwarding=1/net.ipv6.conf.all.forwarding=1/” /etc/sysctl.conf
sudo sysctl -p /etc/sysctl.conf
sudo tailscale up --advertise-exit-node
Tailscale admin webpage: Approve the exit node
Local linux: Use exit-node:
sudo sed -i "s/#net.ipv4.conf.all.rp_filter=1/net.ipv4.conf.all.rp_filter=2/" /etc/sysctl.conf
sudo sysctl -p /etc/sysctl.conf
sudo tailscale up --accept-routes --exit-node-allow-lan-access --exit-node=RemoteIP
Where RemoteIP is the vpn ip of remote node, say 100.113.189.45
Note: Have been verified on Redmi-6/MiUi 14, and openresty + RTMP Module + Linux
We saw how to setup a streaming server with MistServer in this post, let’s see how to do the same with nginx.
nginx, pronounced “engine X“, is a web server that can also be used as a reverse proxy, load balancer, mail proxy, HTTP cache and, why not, RTMP server. It is free and open-source software, released under the terms of the 2-clause BSD license.
For the purpose of this trial, we will see how to install and configure the server on a Raspberry Pi board running Raspberry Pi OS Lite.
Install nginx with RTMP support
First, we must install the server and an add-on module that will allow it to handle the RTMP protocol. sudo apt install nginxsudo apt install libnginx-mod-rtmp
After the installation is complete, we should be able to reach the welcome page simply by entering the IP address of the server in our favorite browser, http://192.168.1.18/ for us.
Configure the RTMP server
The way nginx and its modules work is determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the directory /etc/nginx. For details, please check out the Beginner’s Guide and other resources available in the nginx documentation.
To enable the RTMP protocol, edit the configuration file sudo nano /etc/nginx/nginx.conf
then add these few lines at the very end # protocol imap; # proxy on; # } #} rtmp { server { listen 1935; application live { live on; hls on; hls_path /tmp/hls; } } }
finally save the file and restart the server so that the new configuration will be loaded sudo nginx -s reload
In this example, we are configuring the RTMP server to listen on the port 1935 (the default RTMP port), and to handle an application named live. This application has the live mode (one-to-many broadcasting) enabled. The HLS output is also enabled, the playlist and the fragments will be saved in /tmp/hls (if the directory does not exist it will be created).
The complete reference about the available RTMP directives can be found here.
Configure the HTTP server
We need to configure the HTTP server so that it can access the files in /tmp/hls for clients to play HLS. nginx uses the so called Server Blocks to serve multiple sites in parallel, let’s change the configuration of the default one sudo nano /etc/nginx/sites-enabled/default
by adding a new location entry according to the documentation location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } location /hls { types { application/vnd.apple.mpegurl m3u8; } root /tmp; add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; } # pass PHP scripts to FastCGI server #
then save the file and restart the server once again sudo nginx -s reload
Configure the app
From CamON Live Streaming app settings, enable the Live streaming adapter and configure it
in he Server field, specify the RTMP URL for the application we configured, rtmp://192.168.1.18/live in this example
in the Stream field enter a streaming key of your choice, let’s use spynet
TIP: the streaming key will be used by nginx as the base name for the HLS files
To start the stream use the arrow icon in the bottom-right corner of the main screen. By tapping on it a countdown will be shown, at the end of which the device will connect to nginx.
TIP: during the countdown, tap on the arrow again if you wish to abort
Let’s see it in action
To verify that everything is working as expected, we can use VLC as the client to see the nginx broadcast.
It is possible to see the HLS output using the URL http://192.168.1.18/hls/spynet.m3u8, where hls is the location we configured for the HTTP server to find the files and spynet is the streaming key we have chosen.
It is also possible to see the RTMPoutput using the URL rtmp://192.168.1.18/live/spynet, where live is the name of the application we configured and spynet is the streaming key.
Embed the player
For a better user experience, we may want to embed the player in our web page, This way the broadcast will be available with no extra effort. As the player, Video.js is a good choice to see the HLS broadcast.
Let’s create our index.html page in /var/www/html sudo nano /var/www/html/index.html
with the following HTML code
TIP: the key point is to set the correct source, src=”/hls/spynet.m3u8″, as described above
<!DOCTYPE html> <html lang=”en”><head><link href=”https://vjs.zencdn.net/7.17.0/video-js.css” rel=”stylesheet” /><!– If you’d like to support IE8 (for Video.js versions prior to v7) –><script src=”https://vjs.zencdn.net/ie8/1.1.2/videojs-ie8.min.js”></script></head><body><h1>My nginx streaming server</h1><video id=”my-video” class=”video-js” controls preload=”auto” width=”640″ height=”360″ data-setup=”{}” ><source src=”/hls/spynet.m3u8″ type=”application/vnd.apple.mpegurl m3u8″ /><p class=”vjs-no-js”> To view this video please enable JavaScript, and consider upgrading to a web browser that <a href=”https://videojs.com/html5-video-support/” target=”_blank”> supports HTML5 video </a></p></video><script src=”https://vjs.zencdn.net/7.17.0/video.js”></script></body></html>
After the file has been saved (no need to restart the server), by navigating to address of the server, http://192.168.1.18/, we can see the new homepage in action
Some small tweaks
Since we are planning to broadcast our video over the Internet, we should make the server publicly reachable. To keep it simple, we should consider setting up port forwarding and the Dynamic DNS as described in this post.
In summary, if the router supports the UPnP protocol, we can use the command line utility upnpc to forward the HTTP port directly from the server. If not, we can manually configure the router. sudo apt install miniupnpcupnpc -a server_ipserver_portexternal_port tcpupnpc -a 192.168.1.18 80 8282 tcp
This way the server will be reachable from anywhere at http://public_ip_address:8282/ or http://myserver.dyndns.org:8282/.
As discussed, HLS is continuously writes files to disk while updating the playlist and the fragments. This consumes resources and can dramatically reduce the life of the SD card used by the server as storage. A better solution is to use a ramdisk to temporary store those files.
Examine the available memory to find out how much we can use. free -h
Examine the typical HLS disk usage to find out how much memory we expect to need. sudo du -sh /tmp/hls/
Create a folder where to mount the ramdisk. sudo mkdir -p /mnt/ramdisk
Add an entry to fstab to configure the ramdisk (50M is enough for this example). sudo nano /etc/fstabproc /proc proc defaults 0 0 PARTUUID=4b551375-01 /boot vfat defaults 0 2 PARTUUID=4b551375-02 / ext4 defaults,noatime 0 1 tmpfs /mnt/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=50M 0 0
Change the nginx configuration so that HLS files will be saved in /mnt/ramdisk/hls instead of in /tmp/hls. sudo nano /etc/nginx/nginx.confrtmp { server { listen 1935; application live { live on; hls on; hls_path /mnt/ramdisk/hls; } } }
Change the nginx configuration so that the HTTP server will know where to find the HLS files. sudo nano /etc/nginx/sites-enabled/default location /hls { types { application/vnd.apple.mpegurl m3u8; } root /mnt/ramdisk; add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; }
#audio_output { # type “fifo” # name “my_fifo” # path “/tmp/mpd.fifo” # format “44100:16:2” #}
# creative usb card #audio_output { # type “alsa” # name “MP3” # device “hw:CARD=MP3,DEV=0” # mixer_type “software” # optional #}
#audio_output { # type “alsa” # name “TerraTec” # device “iec958:CARD=Device,DEV=0” # mixer_type “software” # optional #}
#audio_output { # type “alsa” # name “C-Media” # device “iec958:CARD=Set,DEV=0” # mixer_type “hardware” # optional #}
# Via Winner AD-86D amplifer audio_output { type “alsa” name “Audio” device “hw:CARD=AUDIO,DEV=0” mixer_type “software” }
# via Jack Daemon #audio_output { # type “jack” # name “My Jack Output” # mixer_type “software” #}
# # An example of an OSS output: # #audio_output { # type “oss” # name “My OSS Device” ## device “/dev/dsp” # optional ## mixer_type “hardware” # optional ## mixer_device “/dev/mixer” # optional ## mixer_control “PCM” # optional #} # # An example of a shout output (for streaming to Icecast): # #audio_output { # type “shout” # encoder “vorbis” # optional # name “My Shout Stream” # host “localhost” # port “8000” # mount “/mpd.ogg” # password “hackme” # quality “5.0” # bitrate “128” # format “44100:16:1” ## protocol “icecast2” # optional ## user “source” # optional ## description “My Stream Description” # optional ## url “http://example.com” # optional ## genre “jazz” # optional ## public “no” # optional ## timeout “2” # optional ## mixer_type “software” # optional #} # # An example of a recorder output: # #audio_output { # type “recorder” # name “My recorder” # encoder “vorbis” # optional, vorbis or lame # path “/opt/local/run/mpd/recorder/mpd.ogg” ## quality “5.0” # do not define if bitrate is defined # bitrate “128” # do not define if quality is defined # format “44100:16:1” #} # # An example of a httpd output (built-in HTTP streaming server): # #audio_output { # type “httpd” # name “My HTTP Stream” # encoder “vorbis” # optional, vorbis or lame # port “8000” # bind_to_address “0.0.0.0” # optional, IPv4 or IPv6 ## quality “5.0” # do not define if bitrate is defined # bitrate “128” # do not define if quality is defined # format “44100:16:1” # max_clients “0” # optional 0=no limit #} # # An example of a pulseaudio output (streaming to a remote pulseaudio server) # #audio_output { # type “pulse” # name “My Pulse Output” ## server “remote_server” # optional ## sink “remote_server_sink” # optional #} # # An example of a winmm output (Windows multimedia API). # #audio_output { # type “winmm” # name “My WinMM output” ## device “Digital Audio (S/PDIF) (High Definition Audio Device)” # optional # or ## device “0” # optional ## mixer_type “hardware” # optional #} # # An example of an openal output. # #audio_output { # type “openal” # name “My OpenAL output” ## device “Digital Audio (S/PDIF) (High Definition Audio Device)” # optional #} # # An example of an sndio output. # #audio_output { # type “sndio” # name “sndio output” # mixer_type “hardware” #} # # An example of an OS X output: # #audio_output { # type “osx” # name “My OS X Device” ## device “Built-in Output” # optional ## channel_map “-1,-1,0,1” # optional #} # ## Example “pipe” output: # #audio_output { # type “pipe” # name “my pipe” # command “aplay -f cd 2>/dev/null” ## Or if you’re want to use AudioCompress # command “AudioCompress -m | aplay -f cd 2>/dev/null” ## Or to send raw PCM stream through PCM: # command “nc example.org 8765” # format “44100:16:2” #} # ## An example of a null output (for no audio output): # #audio_output { # type “null” # name “My Null Output” # mixer_type “none” # optional #} # ###############################################################################
# Normalization automatic volume adjustments ################################## # # This setting specifies the type of ReplayGain to use. This setting can have # the argument “off”, “album”, “track” or “auto”. “auto” is a special mode that # chooses between “track” and “album” depending on the current state of # random playback. If random playback is enabled then “track” mode is used. # See <http://www.replaygain.org> for more details about ReplayGain. # This setting is off by default. # #replaygain “album” # # This setting sets the pre-amp used for files that have ReplayGain tags. By # default this setting is disabled. # #replaygain_preamp “0” # # This setting sets the pre-amp used for files that do NOT have ReplayGain tags. # By default this setting is disabled. # #replaygain_missing_preamp “0” # # This setting enables or disables ReplayGain limiting. # MPD calculates actual amplification based on the ReplayGain tags # and replaygain_preamp / replaygain_missing_preamp setting. # If replaygain_limit is enabled MPD will never amplify audio signal # above its original level. If replaygain_limit is disabled such amplification # might occur. By default this setting is enabled. # #replaygain_limit “yes” # # This setting enables on-the-fly normalization volume adjustment. This will # result in the volume of all playing audio to be adjusted so the output has # equal “loudness”. This setting is disabled by default. # #volume_normalization “no” # ###############################################################################
# Character Encoding ########################################################## # # If file or directory names do not display correctly for your locale then you # may need to modify this setting. # #filesystem_charset “UTF-8” # ###############################################################################