Skip to content

fix(text): release subtitle cues during live playback - #5223

Open
dsilhavy wants to merge 13 commits into
developmentfrom
fix/text-cue-memory-leak
Open

dsilhavy wants to merge 13 commits into
developmentfrom
fix/text-cue-memory-leak

Conversation

@dsilhavy

@dsilhavy dsilhavy commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Since v5.1.0 (#4812), every parsed subtitle cue stays in memory during live playback. Deleted cues were never removed from the cue interval tree, and the live text buffer was never pruned.

Fixes #5222

Changes

  • Deleted cues are also removed from the interval tree.
  • Live text is pruned like audio and video while subtitles are enabled. Static streams are unchanged.
  • Embedded CEA-608 cues are released only on live video pruning. Embedded and sideloaded cues are kept when switching to a fragmented track.
  • Unit tests for every case. Functional tests for live text pruning, seeking back into pruned subtitles and track switches.

Known limitation

In live streams, a CEA-608 caption that started in the segment before a DVR seek target is not shown after seeking back into pruned video, as in v4. Keeping all cues of the DVR window would bring the leak back.

Verification

  • npm test: 4528 passed, 0 failed.
  • Functional, Chrome, text.json: new tests pass; the pruning and switch tests fail with the fixes reverted.
  • Live TTML and CEA-608: cue objects stay flat (30 and 66) instead of growing by 1–2 per second.

_deleteTrackCues removed cues from the native TextTrack and activeCues
but kept them in allCues. The cue window re-added them, and on live
streams every parsed cue stayed in memory. Remove cues in the deleted
range from allCues too, and use the same range check for activeCues.

Refs #5222
pruneBuffer skipped text completely, so for fragmented subtitles in
live streams the text buffer and its cues were never removed. Prune
text like audio and video for dynamic streams. Static streams keep the
previous behaviour.

Fixes #5222
@dsilhavy dsilhavy added this to the 5.3.1 milestone Nov 26, 2026
@dsilhavy dsilhavy moved this to In Progress in dash.js Version 5.3.1 Nov 26, 2026
Now that deleted cues are released for good, callers that delete too
much cause visible caption loss:

- onVideoBufferCleared ran for audio and text buffer clears as well,
  so an audio track switch removed CEA-608 cues of still buffered
  video. Only handle video buffer clears.
- remove() without a range on an empty buffer passed NaN boundaries,
  which delete every cue of the track. Return early instead.

Deleting all cues of a track now clears the interval tree directly.

Refs #5222
The interval tree query excludes the range boundaries, so a
zero-duration cue on a boundary was not a candidate and stayed in
allCues. Widen the query slightly; cueInRange still decides which
cues are deleted.

Refs #5222
Pruning live text while subtitles were disabled updated the text
buffer level every pruning interval and overwrote the disabled state
in the metrics. Only prune text of dynamic streams while text is
enabled.

Refs #5222
Deleted cues are now released for good, so only delete them where the
data is loaded again or the memory would otherwise grow:

- Switching from an embedded CEA-608 track to a fragmented track no
  longer deletes the embedded cues. They come from video segments
  that are not appended again.
- Video buffer clears delete embedded cues only in dynamic streams.
  In static streams they are bounded by the content duration, and a
  caption that started in an earlier segment is not rebuilt after a
  seek back.

deleteAllTextTracks empties the interval trees at once instead of
removing every cue before discarding them.

Refs #5222
A non-fragmented subtitle file is not appended again once it is
cached, so deleting its cues on a switch to a fragmented track lost
them for good. Only delete the cues of a previous fragmented,
non-embedded track, which is loaded again when selected.

Refs #5222
- Deleted cues are exited and not entered again in manual cue
  processing, also when all cues of a track are deleted.
- TextSourceBuffer.remove() updates the buffered ranges and deletes
  the cues of the removed range.
- The static-stream text pruning test enables text, so the disabled
  text guard no longer masks it.

Refs #5222
@dsilhavy dsilhavy changed the title Fix/text cue memory leak fix(text): release subtitle cues during live playback Nov 27, 2026
- live-text-pruning: the text buffer of live streams is pruned.
- seek-back-text: subtitles are shown again after seeking back into a
  pruned range.
- switch-text-keeps-cues: cues of an embedded track are back right
  after switching to a fragmented track and back.

The tests run on all vectors that include text/* and skip where the
stream has no suitable track or no subtitles at the checked position.

Refs #5222
A cue dispatched in manual rendering mode is not necessarily in the native TextTrack, e.g. when it was dispatched on track selection while paused. Deleting it from the interval tree skipped CUE_EXIT, so the application kept showing it. Exit the dispatched cues of the track within the deleted range before releasing them.
The seek-back test now requires a buffer removal covering the reference range and waits for the seek to complete. The switch test only expects cues that were loaded before the switch and overlap the range after switching back, so sparse subtitles no longer cause false failures.
Any cue in the range after switching back could be a newly parsed one, so the test could pass although the kept track's cues were deleted. Wait for one of the cues recorded before the switch instead.
A parse error removed the whole buffered range of the fragmented track.
Cue deletion now also releases the cues from the interval tree, so the
correctly parsed subtitles of that range were lost until a seek. Only
remove the failed sample from the buffered ranges and keep the cues.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Subtitle cues are never released during live playback (memory leak since v5.1.0)

1 participant