Music & Minecraft Part 4: The Sniper Daemon, Streams Watchdog, and Self-Healing Telemetry
Music & Minecraft Part 4: The Sniper Daemon, Streams Watchdog, and Self-Healing Telemetry
To move beyond my old PowerShell scripts and solve the nightmare of transient stream death specifically the endless rotation of expired authentication tokens, dropped sockets, and dead YouTube live containers I designed a fully autonomous, data-driven Python fleet running natively alongside mpd on the Gigabyte Brix.
As a DBA, I built the system around three core architectural principles: separation of concerns, state persistence, and aggressive self-healing telemetry.
1. Decoupled Data Architecture (Master vs. Slave)
The system strictly separates content definition from stream execution:
- Master Files (Source of Truth): High-level, human-readable configuration manifests containing station lineups, metadata, and routing directives. These files remain completely isolated from the active player.
- Slave Playlists (Execution Output): Compiled
.m3uplaylists generated automatically by the Python fleet and consumed directly bympd. If an output playlist gets corrupted or stale, it is simply re-compiled from the Master without losing configuration history.
2. Three-Tier Operational Strategy
To keep CPU and network overhead minimal while avoiding aggressive API rate-limiting or unnecessary invocations, the fleet divides streams into three operational bands:
- Completely Stable Tier: Standard Icecast and Shoutcast internet radio streams (BBC, LBC, global stations) that run continuously without active polling.
- Unstable / Dynamic Tier: Streams powered by
yt-dlp(such as YouTube feeds and BBC Sounds) where underlying containers, manifests, or authentication tokens require dynamic resolution, as well as RSS feeds that remain stable overall while seamlessly picking up newly added episodes. - The High-Churn "RAPID" Tier (
#BRIX-RAPID): Volatile, highly dynamic feeds (such as live Twitch broadcasts and rolling YouTube streams) that frequently drop offline or change stream URIs. This tier is the dedicated hunting ground for the Sniper and Watchdog daemons, actively tracking real-time online/offline states.
3. The Python Fleet & Persistence Engine
Four specialized Python scripts and background daemons form the active telemetry and compilation suite:
The Master Generator (refresh_streams.py)
Compiles Master definitions into active Slave .m3u playlists using resolvers like yt-dlp.
- Disk-Read Persistence Patch (
#phash): Before writing new files, the generator inspects existing Slave playlists on disk using unique stream hashes (#phash). This ensures that routine playlist regenerations never overwrite active state titles, online uptimes, or dropout timestamps.
The Streams Watchdog (Healing Volatile YouTube Channels)
One of the trickiest parts of running a terminal audio setup is dealing with 24/7 rolling YouTube streams—like continuous live news feeds (e.g., Sky News) or 24/7 synthwave and lo-fi music channels hosted on YouTube. While they claim to be "24/7," YouTube's underlying streaming tokens, container endpoints, and manifest URLs constantly shift behind the scenes, causing standard static links to break after a few hours.
The Streams Watchdog actively monitors these volatile URLs. Instead of letting them die silently into static or throw a fatal player error, the watchdog continuously re-resolves the live stream endpoints in the background, updating the active stream pointers on the fly so these pseudo-permanent YouTube channels actually behave like reliable, un-interruptible broadcast feeds.
The Sniper Daemon (stream_sniper.py)
Operates as a fast, lightweight background sentinel, continuously sweeping the #BRIX-RAPID frequency band to audit real-time carrier status (is_live).
- Kill & Re-acquire: If a stream drops off the airwaves, the sniper instantly snips the dead carrier from the active list, routes playback to a local fallback audio track, and holds the position. The exact millisecond the carrier pulses live again, the sniper re-acquires the feed and patches it back into the live stream seamlessly.
- Precision Temporal State Tracking:
- 🟢 ONLINE State: Maintains persistent, active uptime tracking while streams are healthy.
- 🔴 OFFLINE - HOLDING RX State: Stamps exact ISO 8601 dropout timestamps (e.g.,
// 🔴 OFFLINE - HOLDING RX (2026-10-21T17:48:00Z)) when a stream dies, freezing the timestamp on disk so dropout logs survive routine system refreshes. - 🟡 CARRIER - ACQUIRING SB State: Tags newly discovered or unverified streams until the sniper locks onto their true carrier frequency.
The MPD Watchdog & Audio Guardrail
A dedicated companion daemon running alongside mpd. If an underlying stream thread hangs, a network socket drops, or audio processing stutters, the watchdog automatically resets the player daemon state and resumes playback without requiring human intervention.
The Ultimate Home-Lab Audio Engine
What started as a £60 eBay Gigabyte Brix bought for a fun weekend project has evolved into a headless, enterprise-grade, self-healing broadcast matrix.
Zero app bloat. Zero smart-speaker frustration. Just pure, resilient terminal audio backed by custom Python telemetry, automated YouTube URL healing, and the finest sci-fi atmosphere in the galaxy.
Comments
Post a Comment