We run a small relay in front of SRS so late viewers can start decoding immediately: cache the FLV header (metadata + AVC/AAC sequence headers) plus the current GOP, then replay both to anyone who joins mid-stream.
It worked for months. Then a second live room went permanently black — with a valid H.264 stream, valid audio, no console errors.
The header replayed fine. The problem was the GOP cache.
FLV is a tag stream: each tag has an 11-byte header with a 24-bit size field. Our GOP buffer was appended from arbitrary io.Writer chunks, so it often started in the middle of a tag. The scanner then read a random byte as a tag type and a random 24-bit number as a length, misaligned forever, and replayed raw bytes that looked nothing like video to the decoder.
The fix is two lines of discipline:
- Seed the GOP buffer from the header buffer. The header buffer is only marked "done" after a complete tag, so it always ends on a boundary.
- Only replay the GOP buffer when its first tag is a keyframe video tag. Otherwise skip the replay and wait for the next IDR.
Lesson: any cache of a binary container format has to start on a record boundary. "It works on my first stream" is not evidence — the chunk boundary just happened to land somewhere else that time.
Top comments (0)