Overview
The Switcher service handles MediaMTX stream configuration and automatic slot switching for devices with dual connection slots. It monitors device health and switches to backup connections when failures are detected.Configuration
Docker Compose
Environment variables
| Variable | Description | Default |
|---|---|---|
PORT | Service port | 8080 |
STREAM_API_URL | MediaMTX API endpoint | Required |
POCKETBASE_URL | PocketBase connection URL | Required |
JOYSTICK_API_URL | Joystick service URL | Required |
JOYSTICK_API_KEY | API key for Joystick | Required |
SLOT_HEALTH_CHECK_INTERVAL | Health check interval (seconds) | 30 |
Features
Stream mode management
Manages three device modes:- live - Active streaming via MediaMTX
- off - No streaming, path removed
- auto - Automatic mode (path removed)
Automatic slot switching
Provides failover capabilities for dual-slot devices:- Health monitoring - Checks both primary and secondary slots
- Automatic failover - Switches after 2 consecutive failures
- Auto-recovery - Returns to original slot when healthy
- Seamless transition - Updates stream URLs automatically
API endpoints
Mode management
Set device mode
device- Device IDmode- One of:live,off,auto
Slot management
Switch slot manually
device- Device IDslot- Eitherprimaryorsecondary
Health monitoring
Get health status
Trigger health check
Slot switching system
How it works
- Service startup - Switcher starts health check loop
- Device discovery - Finds devices with
autoSlotSwitch=true - Health testing - Runs
slot-checkaction on both slots - Failure detection - Tracks consecutive failures
- Automatic switching - Switches after 2 failures to healthy slot
- Database update - Updates device
activeSlotfield - Stream update - PocketBase hook updates stream configuration
- Notification - Users notified of slot change
Health check process
The service runs health checks every 30 seconds (configurable):slot-check action configured in PocketBase, typically a ping or connectivity test.
Failover scenarios
Primary slot failure
- Primary slot health check fails
- Failure counter increments
- After 2 failures, check secondary slot health
- If secondary is healthy, switch to secondary
- Stream URLs updated to use secondary host
- Health checks continue on both slots
Recovery
- Original failed slot becomes healthy
- System can switch back to preferred slot
- Seamless transition with minimal interruption
Device configuration
To enable automatic slot switching:- Configure primary connection (host, phone)
- Enable “Configure Secondary Connection Slot”
- Configure secondary connection (secondSlotHost, secondSlotPhone)
- Enable “Enable automatic slot switching”
- Save configuration
MediaMTX integration
Switcher manages MediaMTX stream paths:Add device path
Delete device path
Initialization
On startup, Switcher initializes all devices in “live” mode:Monitoring and logging
The service provides detailed logging:Troubleshooting
Health checks not running
- Verify
SLOT_HEALTH_CHECK_INTERVALis set - Check switcher service logs
- Ensure devices have
autoSlotSwitchenabled
Slot switching not working
- Verify secondary slot configuration
- Check
slot-checkaction exists in database - Verify API connectivity between services
Debug commands
Performance impact
- Health checks run every 30 seconds by default
- Minimal network overhead (simple connectivity tests)
- Failover typically completes within 1-2 check cycles
- No impact on streaming during normal operation