Monday, 22 July 2024

Low latency WebRTC streaming from HDMI source via zerotier

In order to achieve low latency WebRTC streaming over the Internet I found MediaMTX.

I installed it on my Raspberry Pi 4 on latest 64 bit Bookworm image as follows.

jordana@pi4g:~ $ mkdir mediamtx && cd mediamtx
jordana@pi4g:~/mediamtx $ wget https://github.com/bluenviron/mediamtx/releases/download/v1.8.4/mediamtx_v1.8.4_linux_arm64v8.tar.gz
jordana@pi4g:~/mediamtx $ tar -xvzf mediamtx_v1.8.4_linux_arm64v8.tar.gz 
jordana@pi4g:~/mediamtx $ nano mediamtx.yml
webrtcIPsFromInterfaces: no
webrtcAdditionalHosts: [zerotier_IP_of_Raspberry]
paths:
  cam:
    runOnInit: ffmpeg -f v4l2 -pix_fmt yuv420p -s:v 1280x720 -i /dev/video0 -f alsa -i hw:3,0 -filter:v fps=50 -c:v libx264 -c:a libopus -async 50 -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH
    runOnInitRestart: yes
jordana@pi4g:~/mediamtx $ ./mediamtx 

In order to find out the audio configuration hw:[card],[device] of the USB capture device I used arecord tool.

jordana@pi4g:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 3: MS2109 [MS2109], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0

To stream WebRTC from the other side of a zerotier connection I simply had to use a browser to load this URL.

http://zerotier_IP_of_Raspberry:8889/cam/

I had to use Chromium from Crostini to have a stream within a minute but once it is up&running the latency and quality is fantastic!

UPDATE

After a few days of working stable the need arose to make a service out of it as described in the MediaMTX GitHub.

jordana@pi4g:~/mediamtx $ sudo mv mediamtx /usr/local/bin/
jordana@pi4g:~/mediamtx $ sudo mv mediamtx.yml /usr/local/etc/
jordana@pi4g:~ $ sudo tee /etc/systemd/system/mediamtx.service >/dev/null << EOF
[Unit]
Wants=network.target
[Service]
ExecStart=/usr/local/bin/mediamtx /usr/local/etc/mediamtx.yml
[Install]
WantedBy=multi-user.target
EOF
jordana@pi4g:~ $ sudo systemctl daemon-reload
jordana@pi4g:~ $ sudo systemctl enable mediamtx
jordana@pi4g:~ $ sudo systemctl start mediamtx

No comments:

Post a Comment