Video Streaming & MediaMTX Architecture
📌 What is this for? (Quick Summary for New Interns)​
In Simple Words:
When operating a high-speed vehicle remotely, you need to see what is in front of the vehicle in real time.
If video lags by 5 or 10 seconds (like standard YouTube or Twitch streams), it would be far too late to stop the pod in an emergency.This subsystem solves that problem:
- It takes live video from cameras on the vehicle and streams it to our web dashboards in under 300 milliseconds (a fraction of a second).
- If a camera gets unplugged or broken, it automatically switches to a colorful test screen so the operator knows the connection is working, but the camera is disconnected.
1. How Video Reaches Your Browser​
2. Why We Don't Use Standard Video Settings​
Normal security cameras only send a full picture ("keyframe") once every 10 to 15 seconds to save internet bandwidth.
If you open a web browser to watch the stream, the browser has to sit there doing nothing, waiting up to 15 seconds for the next keyframe before it can show you anything. That causes spinning loading wheels and black screens.
To fix this, in stream_service/app.py, we configured FFmpeg with specific low-latency settings:
cmd = [
'ffmpeg', '-re',
...,
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-tune', 'zerolatency',
'-profile:v', 'baseline', # Simple format that every laptop and tablet can play
'-pix_fmt', 'yuv420p',
'-g', '15', # Sends a full picture every 1 second (15 frames at 15 FPS)
'-bf', '0', # Zero delay frames: displays immediately without buffering
...,
'-f', 'rtsp',
'-rtsp_transport', 'tcp',
target_rtsp
]
The 3 Golden Rules We Followed:​
-g 15(1 Full Picture Every Second): When someone opens the dashboard, they see video within 1 second instead of waiting 15 seconds.-bf 0(Zero Delay Frames): Disables predictive frames that require the computer to wait for future frames before showing the current one.-profile:v baseline: Ensures video plays on old laptops, iPhones, Androids, or control tablets without hardware decoding errors.
3. Automatic Test Screen If Camera Unplugs​
If someone trips over an Ethernet cable on the test track and the camera disconnects:
- The
stream_serviceprobes the camera IP address every few seconds. - If the connection fails, it doesn't crash. It seamlessly switches to generating a colorful SMPTE test pattern with an embedded clock timestamp.
- As soon as the cable is plugged back in, it detects the camera and switches back to live video automatically without needing to restart any services.
4. Implementation & Deployment Reference​
For a complete, copy-paste-ready deployment guide covering:
- AWS EC2 instance launch, Security Group ports, and Route 53 DNS.
- Complete
/etc/mediamtx/mediamtx.ymlconfiguration. - Complete
/etc/systemd/system/mediamtx.servicedefinition. - Complete Raspberry Pi
app.pystream forwarder script with socket checks and SMPTE fallback. - Complete Raspberry Pi
abc-stream.servicesystemd file.
👉 See the Camera Setup & MediaMTX Streaming Guide.