12 Scanner AV Streaming on VM in Unraid
Skylar Grant edited this page 2024-07-14 16:32:53 +00:00

This article will go through my setup for hosting a scanner A/V stream from an Ubuntu Server VM.

Stack

  • UnRAID 6.12 running on an old Alienware desktop
  • Ubuntu 20.04.6 LTS Virtual Machine
  • ALSA
  • ffmpeg
  • mediamtx
  • Radioshack Pro-651 (any scanner with headphone-out should work)
  • Logitech C90 webcam

Guide

Assign devices to VM

(This is not necessary if you are using a USB sound card)

  1. In UnRAID go to Tools -> System Devices
  2. Check the box(es) for the IOMMU group(s) for your audio card
  3. Reboot the server

Set up the VM

  1. Download Ubuntu Server ISO to UnRAID server
  2. Create an Ubuntu VM in UnRAID
    -- Pass through 2-4 CPU cores for installation, can be scaled back later
    -- Choose 2048-4096MB minimum RAM, at least.
    -- 10GB vdisk should be enough, 20GB is what I used.
    -- Leave graphic card as is, you'll need VNC to complete the initial installation
    -- Assign the sound card to the VM
    -- Assign the USB webcam to the VM
  3. Start up the VM and proceed through installation of Ubuntu Server
  4. (Optional) Shutdown the VM and adjust the specs back to minimal (1G RAM, 1-2CPU cores)

Set up the Software Stack

  1. Run these commands
sudo apt update && sudo apt upgrade -y
sudo apt install ffmpeg -y
wget https://github.com/bluenviron/mediamtx/releases/download/v1.8.4/mediamtx_v1.8.4_linux_amd64.tar.gz
tar -xvf mediamtx_v1.8.4_linux_amd64.tar.gz
sudo mkdir /usr/local/share/mediamtx
sudo mv mediamtx /usr/local/bin/
sudo mv mediamtx.yml /usr/local/share/mediamtx/
  1. Edit mediamtx.yml and remove the paths: at the end, replacing with:
paths:
  scanner:
    source: publisher
    runOnInit: ffmpeg -f v4l2 -i /dev/video0 -f alsa -i hw:0 -c:v libx264 -preset veryfast -tune zerolatency -pix_fmt yuv420p -f rtsp rtsp://localhost:8554/scanner
  1. Run it with mediamtx /usr/local/share/mediamtx/mediamtx.yml and test, adjusting configuration as needed.

Daemonize the Stream

  1. Create a service file: sudo nano /etc/systemd/system/mediamtx.service
  2. Place the following in the file:
[Unit]
Description=MediaMTX Streaming Service
After=network.target

[Service]
ExecStart=mediamtx /usr/local/share/mediamtx/mediamtx.yml
Restart=always
User=nobody
Group=nogroup
WorkingDirectory=/usr/local/bin/

[Install]
WantedBy=multi-user.target
  1. Reload systemd: sudo systemctl daemon-reload
  2. Enable the service at boot: sudo systemctl enable mediamtx
  3. Start the service now: sudo service mediamtx start
  4. Check on the status: sudo service mediamtx status
  5. Get more log info: journalctl -u mediamtx

Accessing the Stream

Simply open up http://[serverFQDN/IP]:8888/scanner to view the stream, or put the HLS stream in a webpage hosted via nginx or on another machine. I used a reverse proxy to point directly at the HLS stream port.

mediamtx and ffmpeg Configuration Information

Paths Section:

  • paths: This section contains the definitions for different streaming paths.
    • scanner: This is the name of the path. You can name it whatever makes sense for your use case, such as scanner, camera, or stream.

Path Configuration:

  • source: publisher: This specifies that the stream for this path will be published by an external source. In this case, the source is the FFmpeg command specified in runOnInit.
  • runOnInit: This directive tells MediaMTX to run the specified command when the server starts. The command provided here will initialize the stream.

FFmpeg Command Explanation:

  • ffmpeg: This is the command-line tool used to handle multimedia data.
  • -f v4l2 -i /dev/video0: This tells FFmpeg to use the Video4Linux2 (v4l2) input format to capture video from the device /dev/video0, which is your USB webcam.
  • -f alsa -i hw:0: This specifies the ALSA input format to capture audio from the device hw:0.
  • -c:v libx264: This sets the video codec to H.264 using the libx264 encoder.
  • -preset veryfast: This option sets the encoding preset to veryfast, balancing compression efficiency and encoding speed.
  • -tune zerolatency: This tuning option minimizes latency, which is crucial for real-time streaming.
  • -pix_fmt yuv420p: This sets the pixel format to yuv420p, which is widely compatible with most players and devices.
  • -f rtsp rtsp://localhost:8554/scanner: This sets the output format to RTSP and specifies the RTSP URL where the stream will be available, in this case, rtsp://localhost:8554/scanner.

This configuration sets up MediaMTX to serve RTSP and HLS streams, with the scanner path specifically set up to capture and stream video and audio using FFmpeg.