DEV Community

Cao Wen
Cao Wen

Posted on

HTTP-FLV freezes after a reconnect — the FLV header is the reason

Symptom first, because it's the part that wastes days: your HTTP-FLV stream plays fine, the network blips for a second, the player reconnects — and then the picture freezes permanently. Not a rebuffer. Not an error event.buffered stops growing,currentTime stops advancing, and only a full page reload fixes it.

Everything looks healthy from the outside. Bytes are still arriving. The connection is open. The player just refuses to move.

What actually happened

An HTTP-FLV connection is not a continuation of the previous one. Every new connection begins with:

FLV header (9 bytes) + sequence header + the last GOP from the server's gop cache

That is a complete, independently decodable prefix — but its timestamps start before the ones already in yourSourceBuffer . When you append it as if it were the next chunk of the old stream, the demuxer sees a second FLV header and a timestamp going backwards. Some players throw. Most just stop advancing, which is worse, because nothing in your logs looks like an error.

The fix: treat reconnect as a generation change

Don't patch the stream. Replace it:

function onReconnect() {
gen++; // new generation id
resetPrefixCache(); // drop FLV header / GOP parts
player.unload(); // destroy the MSE state
connect(url); // reconnect, take the fresh prefix as-is
}

Two details matter more than the code:

  1. Downstream consumers need the same treatment. If anything relays your bytes (a peer, a worker, a proxy), it has to forward a generation marker followed by the full prefix — not mid-stream bytes. Otherwise the downstream keeps feeding old chunks into a new decoder and you get artifacts that look like corruption.

  2. Drop queued bytes on reconnect, not on error. The trap is that send queues look healthy while the pipeline is already dead. Clear the queue when the generation changes; the prefix replay will refill it.

The cost

Destroying and rebuilding the buffer costs roughly 200–400ms of black frame. That feels bad until you compare it to the alternative: a viewer staring at a frozen image until they reload the page, with no indication anything is wrong.

For a live commerce stream, a 300ms blip during a network hiccup is invisible. A permanent freeze during a product demo is the whole session.

What I'd check first

If you see this in the wild, log three things on reconnect: the FLV header offset where the new connection started, the first timestamp after reconnect, and whether it's lower than the last one you appended. That one comparison tells you whether you're looking at a generation problem or something else entirely.

I build a live selling widget for Shopify/WooCommerce stores, which is where most of these reconnect cases showed up: https://durianlive.com

Top comments (0)