Skip to content

fix: reset fragmentation state on seek to prevent invalid fragment ranges#548

Open
lideen wants to merge 1 commit intogpac:mainfrom
lideen:fix/invalid-fragmentation-state-on-seek
Open

fix: reset fragmentation state on seek to prevent invalid fragment ranges#548
lideen wants to merge 1 commit intogpac:mainfrom
lideen:fix/invalid-fragmentation-state-on-seek

Conversation

@lideen
Copy link
Contributor

@lideen lideen commented Feb 23, 2026

Description

After upgrading this library from 0.5.6 to the current version 2.3.0, I was intermittently seeing errors where ISOFile.createFragment() was called with an invalid sample range (sampleEnd < sampleStart), especially around seek operations.

Root cause: seek() updated trak.nextSample, but per-track fragmentation/extraction state was not reset.
So after a seek, state could be out of sync (for example, stale lastFragmentSampleNumber from before seek vs new nextSample after seek).

This could cause:

  • Invalid fragment ranges (sampleEnd < sampleStart) after backward seeks
  • Fragment windows incorrectly spanning pre-seek and post-seek samples after forward seeks
  • Unnecessary/incorrect fetch offsets (requesting earlier data than needed)
  • Segment buffer contamination across seek boundaries

Solution

On each track seek (seekTrack), reset seek-sensitive per-track state:

  • set lastFragmentSampleNumber to nextSample
  • set lastSegmentSampleNumber to nextSample
  • reset accumulatedSize to 0
  • clear segmentStream
  • clear pending extracted samples for extraction mode

Also added a defensive guard in createFragment:

  • if sampleEnd < sampleStart, skip fragment creation safely and warn

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