From 496da49dd5ce4a3bfc4b5df84ce5c7f7bdbcf504 Mon Sep 17 00:00:00 2001 From: Luke Hoersten Date: Sat, 20 Jun 2026 10:30:24 -0500 Subject: "paint the latest, drop the rest" — FIONREAD skip + Scrypted backpressure-blind + lwIP TCP window bump MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three changes that work together to make the stream "always paint what's freshest, never sit on a stale frame": #1 — Firmware FIONREAD skip in stream_server Right after the body of frame N comes off the wire (and before we unlock the decoder + spend ~6ms on decode + paint), check the kernel receive buffer with ioctl(FIONREAD). If at least one more header (8 bytes) is queued, frame N is no longer the freshest possible — skip its decode + paint and loop back to read frame N+1. The TCP recv cost is unavoidable (bytes still have to cross the wire) but the decoder + paint cost is saved on every superseded frame. Glass-to-glass latency on the latest frame drops by however many frames had backed up. #2 — Scrypted: keep writing past kernel-buffer backpressure Previously: when sock.write() returned false we dropped the next ffmpeg frame at source. New: we keep writing through. Node buffers internally; under our load (~16 MB/s ffmpeg → ~5-7 MB/s firmware) the buffer rarely exceeds a frame or two. With the firmware now silently skipping decode on backed-up frames (#1), excess frames get shed for free on the device side. Scrypted's job is just to hand the firmware the freshest bytes as fast as possible. socketBackpressured is still tracked for the diagnostic log. #3 — lwIP TCP window bump (revisiting earlier regression) The previous attempt at LWIP_TCP_WND_DEFAULT=32k regressed under HTTP because every /frame opened a fresh socket and we paid the slow-start cost repeatedly. The streaming pivot eliminated that: the socket is long-lived, slow-start runs exactly once, then we ride the full window for the rest of the session. Bumping to 65535 (max for stock lwIP), SND_BUF to match, RECVMBOX to 16, SACK on. Expected: recv throughput ceiling moves up from ~5.3 MB/s, which directly raises the fps ceiling (recv is currently 37ms of the 43ms per-frame total). --- scrypted/scrypted-viewport.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'scrypted/scrypted-viewport.ts') diff --git a/scrypted/scrypted-viewport.ts b/scrypted/scrypted-viewport.ts index f25f557..fddfd28 100644 --- a/scrypted/scrypted-viewport.ts +++ b/scrypted/scrypted-viewport.ts @@ -6,7 +6,7 @@ // short git hash of the commit that added this constant — if the // hash in the log doesn't match the HEAD this file came from, the // Scrypted Script editor is still on stale code. -const SCRIPT_VERSION = "521de7e"; +const SCRIPT_VERSION = "pending"; // // Architecture // ------------ @@ -947,12 +947,15 @@ class ScryptedViewportProvider extends ScryptedDeviceBase workBuf = workBuf.subarray(eoi + 2); if (frame.length < 4 || frame[0] !== 0xff || frame[1] !== 0xd8) continue; - // Drop if the socket isn't connected yet (initial - // open) or if the kernel send buffer is full - // (firmware can't ingest as fast as ffmpeg emits). - // Frame is gone forever — TCP doesn't queue what - // we don't write. - if (!socketReady || socketBackpressured) { + // Drop only when the socket isn't connected yet + // (initial-open race) — once it's up we just keep + // writing. Node buffers internally if the kernel + // send buffer is full; under 16 MB/s ffmpeg output + // and ~5-7 MB/s firmware ingest the buffer rarely + // exceeds a frame or two. The firmware's FIONREAD- + // skip ensures it always paints the LATEST queued + // frame, so any backlog gets shed there for free. + if (!socketReady) { droppedFrames++; continue; } @@ -969,7 +972,11 @@ class ScryptedViewportProvider extends ScryptedDeviceBase if (writeLatencies.length > 200) writeLatencies.shift(); bytesSent += 8 + frame.length; sentFrames++; + // Track but don't gate on backpressure — the metric + // is still useful as a "kernel buffer was full" + // indicator for diagnostics. if (!ok) socketBackpressured = true; + else socketBackpressured = false; } }); -- cgit v1.2.3