Skip to content

Sync to the first packet when the stream doesn't start with a sync byte - #79

Open
hsdfat wants to merge 1 commit into
asticode:masterfrom
hsdfat:fix-demuxer-initial-sync-71
Open

hsdfat wants to merge 1 commit into
asticode:masterfrom
hsdfat:fix-demuxer-initial-sync-71

Conversation

@hsdfat

@hsdfat hsdfat commented Sep 26, 2026

Copy link
Copy Markdown

When a demuxer starts reading a stream that is already transmitting (e.g. joining a live UDP multicast), the first byte is usually not a sync byte, and the demuxer fails right away with ErrPacketMustStartWithASyncByte, both with packet size auto detection and with DemuxerOptPacketSize.

This syncs to the first packet instead: it looks for a sync byte followed by another one exactly one packet later (188, 192 or 204 bytes) and drops the bytes before it. It covers the bufio.Reader (peek + discard), io.Seeker (seek) and plain reader paths, as well as a fixed packet size. Streams that start with a sync byte behave as before, and ErrPacketMustStartWithASyncByte is still returned when no packet can be found. This only handles the start of the stream, not resyncing after data loss mid-stream.

For the repro you asked for, the tests cut 1, 100 and 187 bytes off the front of testdata/ts/silent_audio.ts and check that the demuxer returns the same packets as the untruncated file from the first complete one on. They fail on master.

Fixes #71

… start with a sync byte

When joining a live stream (e.g. UDP multicast) the first byte is usually
not a sync byte, and both packet size auto detection and a packet size
forced with DemuxerOptPacketSize failed with ErrPacketMustStartWithASyncByte.

Look for the first sync byte that is followed by another one exactly one
packet later, and start demuxing from there. Streams that already start
with a sync byte take the same path as before.

Fixes asticode#71
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.

Demuxer fails when reading a stream that was already transmitting

1 participant