Skip to content

add: experimental binary stream transport via Source Workers (Deezer first) - #261

Draft
1Lucas1apk wants to merge 11 commits into
devfrom
exp/source-worker-stream-transport
Draft

1Lucas1apk wants to merge 11 commits into
devfrom
exp/source-worker-stream-transport

Conversation

@1Lucas1apk

Copy link
Copy Markdown
Member

Note

Draft / Experimental: Isolates heavy source stream fetching and decryption inside Source Workers, delivering audio back to Players over dedicated Named Pipes / Unix Domain Sockets instead of Node.js IPC.

Why

Even with URL resolution running in Source Workers, the playback process still ends up doing the heavy HTTP streaming, retries, and decryption inside the Player event loop. When multiple streams stall, reconnect, or decrypt at the same time, that I/O and crypto work fights directly with active audio playback.

The goal here is to move that heavy stream production out to the Source Worker threads without touching the Player audio pipeline (decoding, filters, fading, and Opus encoding stay where they are). Deezer (src/sources/deezer.ts) is the first source wired into this transport because of its continuous HTTP streaming, aligned-boundary CDN retries, and Blowfish-CBC decryption, while the StreamTransport layer (src/playback/streamTransport.ts) is kept generic so any other heavy source can use the same transport later.

Delegation & Transport

Instead of passing chunks over IPC/JSON/Base64, each stream gets its own dedicated socket connection with end-to-end backpressure. The playback worker registers a one-time streamId and securityToken on its local StreamTransportServer, strips non-serializable callbacks from additionalData, and delegates startStreamTransport over the Master Command Socket (src/workers/main.ts) to SourceWorkerManager, which picks the least-loaded Source Worker thread (src/workers/source.ts). That thread opens the upstream stream, connects to the Player's Named Pipe or Unix Domain Socket, validates the binary handshake, and pumps framed records in the background while pausing whenever the Player's reserve buffer hits its high watermark. On finish, stop, or native seek, cancelStreamTransport tears down the active producer in the Source Worker.

Component / Parameter Value Role
Transport socket Named Pipe (Windows) / Unix Domain Socket (POSIX) Per-stream dedicated channel, zero Node.js IPC
Handshake 56 bytes (Magic, Version, Type, Flags, 16B streamId, 32B securityToken) Single-use auth via crypto.timingSafeEqual
Frame header 5 bytes (Type + 4B big-endian Length) Raw binary DATA, END, and ERROR records
Reserve buffer (default) 64 KB low / 192 KB target / 384 KB high (128 KB backpressure cap) Absorbs worker/network jitter without unbounded RAM

Current bottlenecks

startStreamTransport returns { started: true } as soon as the stream starts pumping in the background (client.run()), so SourceWorkerManager's workerLoads counter drops immediately and doesn't yet account for long-lived activeTransports when scheduling heavy loadTracks or playlist parsing on the same threads. On native seeks, closing the old pipe and opening a new one is fast locally, so the main latency left is the upstream CDN Range warm-up on the new offset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant