Skip to content

fix(subtitles): bound how far a PGS sidecar reads ahead - #528

Merged
Quick104 merged 1 commit into
mainfrom
fix/pgs-sup-memory
Oct 8, 2026
Merged

Quick104 merged 1 commit into
mainfrom
fix/pgs-sup-memory

Conversation

@Quick104

@Quick104 Quick104 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: changes #323 C4; changes #324 C4

When the server sends an embedded PGS subtitle as a .sup sidecar (remux and transcode sessions), the app reads the whole file the moment the track is mounted and keeps every decoded caption image in memory until playback reaches it. For a feature film that is tens of MB of Java heap. On a phone emulator with a 192 MB heap, selecting the English PGS track on a 33 Mbps remux crashed the app with an OutOfMemoryError on the ExoPlayer playback thread, or left it stuck at startup with the heap at 189 MB. The TV emulator survived the same track at 159 MB, close to the same limit.

This change bounds how far a PGS sidecar reads ahead of the playhead, so it holds a few minutes of captions instead of the whole film.

Approach

NonGatingSidecarPeriod keeps a sidecar out of the MergingMediaSource loading gate by re-continuing its ProgressiveMediaPeriod every time it parks at its 1 MiB check interval, with no limit. It now takes an optional read-ahead limit. When the delegate parks and its queued cues are already more than that far past the playhead, the wrapper leaves it parked. It continues the delegate once the playhead closes the gap, checked from discardBuffer (called on the playing period every tick) and reevaluateBuffer. A seek clears the hold and restarts the read as before, and the check is skipped until the delegate has prepared.

Only the PGS sidecar sets a limit (two minutes, in SiloPlayerFactory). Text sidecars still load in one go, so their behaviour does not change. Phone and TV share this code.

Validation

  • Unit tests: android-shared 1,700, androidApp 812, androidTvApp 1,236, 0 failures. New SidecarSubtitleMediaSourceTest cases cover holding, resuming from both playback callbacks, seeks, finished and unprepared delegates, and the unlimited text case.
  • Builds: :androidApp:assembleDebug and :androidTvApp:assembleDebug pass.
  • Phone emulator against a dev server: the 33 Mbps title now plays with PGS captions on screen at a flat 140 MB (126 MB with subtitles off). On a 2.5 Mbps title, live heap after a forced GC went from 140 MB (all 1,600 caption sets read within 40 s) to 122 MB. After a seek the sidecar parked about two minutes ahead, resumed when playback caught up, and parked again. No crashes in about 10 minutes of playback.
  • Not run: a physical low-memory device.

Evidence

Evidence: https://evidence.siloserver.org/r/silo-android/pgs-sidecar-memory/

Risks

  • A held sidecar keeps its HTTP request parked for minutes at a time (1 MiB of PGS can cover several minutes of film). Media3 already parks progressive loads at the same boundaries, and if the server drops the idle connection, Media3's load-error retry reopens it at the same byte offset. I did not observe a dropped connection in testing.
  • If the sidecar has to restart while it is held (a track remount), it rereads from the nearest indexed point and drops the queued cues. Captions still appear; they are fetched again.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code, run from T3 Code
  • Tool(s): Claude Code
  • Model(s): claude-opus-5-5
  • Involvement: Fully AI-generated; human review pending
  • Adversarial review: an independent Claude Code reviewer agent (claude-opus-5-5) reviewed the diff for Media3 threading and loader-state bugs. It found none it was confident about. I acted on two suggestions: the read-ahead check now waits until the delegate has prepared, and the seek test now proves the hold starts over.

🤖 Generated with Claude Code

Note

Bound PGS sidecar subtitle reads to 120s ahead of playback

  • Adds a maxLookaheadUs parameter to SidecarSubtitleMediaSource, defaulting to C.TIME_UNSET (unbounded). The player factory sets it to 120,000,000 µs for PGS sidecars only.
  • NonGatingSidecarPeriod now tracks prepared and held-ahead state. When the delegate's buffered position is more than the lookahead past the playback floor, loading kicks are suppressed. The hold clears on seeks, and playback ticks or buffer reevaluations resume loading once the gap closes. Unprepared delegates and C.TIME_END_OF_SOURCE are never held.
  • Risk: PGS sidecars previously buffered the whole file; they now buffer at most ~120s ahead of playback. Check SiloPlayerFactory in SiloPlayerFactory.kt if a different lookahead is needed.

Macroscope summarized c27bae2.

A PGS subtitle sent as a .sup sidecar (the remux and transcode route) was
read from start to finish as soon as it was mounted, and every caption
image it decoded stayed in the sample queue until playback reached it.
For a feature film that is tens of MB of Java heap; a phone with a 192 MB
heap ran out of memory, or stalled at startup, as soon as the track was
selected.

The sidecar wrapper keeps re-continuing its loader so a sidecar never
gates the video. It now leaves the loader parked once its queued cues are
more than two minutes past the playhead, and continues it as playback
catches up (from the playback ticks) or after a seek. Only the PGS sidecar
sets a limit; text sidecars still load in one go.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-08T23:07:45.010791Z c27bae2 PR opened
🔒 Security Review ✅ Completed 2026-10-08T23:07:24.838579Z c27bae2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@silo-kody

silo-kody Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@coderabbitai

coderabbitai Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f106c3d6-59d5-4d33-b7be-759897f06fc9
📥 Commits

Reviewing files that changed from the base of the PR and between ab86a85 and c27bae2.

📒 Files selected for processing (3)
  • android-shared/src/androidMain/kotlin/org/siloserver/silo/common/player/SiloPlayerFactory.kt
  • android-shared/src/androidMain/kotlin/org/siloserver/silo/common/player/subtitle/SidecarSubtitleMediaSource.kt
  • android-shared/src/androidUnitTest/kotlin/org/siloserver/silo/common/player/subtitle/SidecarSubtitleMediaSourceTest.kt

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The player factory applies a 120-second lookahead to PGS sidecar subtitles. The sidecar period pauses loading when its buffered position exceeds that limit relative to the playback floor, then resumes when playback catches up. Other subtitle types retain unbounded loading by default.

Changes

Sidecar subtitle lookahead

Layer / File(s) Summary
Lookahead configuration
android-shared/src/androidMain/kotlin/org/siloserver/silo/common/player/subtitle/SidecarSubtitleMediaSource.kt, android-shared/src/androidMain/kotlin/org/siloserver/silo/common/player/SiloPlayerFactory.kt
SidecarSubtitleMediaSource accepts an optional lookahead limit and passes it to the wrapped period. The factory sets a 120-second limit for PGS and leaves other subtitle types unbounded.
Hold and resume loading
android-shared/src/androidMain/kotlin/org/siloserver/silo/common/player/subtitle/SidecarSubtitleMediaSource.kt
The period holds loading when buffered data exceeds the configured limit beyond the playback floor. It checks for resumption during buffer reevaluation, discard, track selection, and after seeks.
Lookahead behavior tests
android-shared/src/androidUnitTest/kotlin/org/siloserver/silo/common/player/subtitle/SidecarSubtitleMediaSourceTest.kt
Tests cover holding and resuming loading, seeking, preparation and end-of-source behavior, and the unbounded default. The fake period now reports a configurable buffered position.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant SiloMediaSourceFactory
  participant NonGatingSidecarPeriod
  participant Delegate
  SiloMediaSourceFactory->>NonGatingSidecarPeriod: Configure the sidecar lookahead
  NonGatingSidecarPeriod->>Delegate: Request continuation when within the limit
  NonGatingSidecarPeriod->>NonGatingSidecarPeriod: Hold continuation when buffered data exceeds the limit
  NonGatingSidecarPeriod->>Delegate: Resume after playback catches up
Loading

Merge Risk: ⚪ Minimal · up to c27ba

The PGS read-ahead bound appears mergeable after normal checks. The investigated track-reselection path does not leave subtitles permanently stalled.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 11.54% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 26 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the main change: limiting PGS sidecar subtitle read-ahead.
Description check Passed The description is directly related to the changeset and explains the problem, implementation, validation, risks, and affected subtitle types.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Quick104 Quick104 added priority: P1 Core-path bug; painful workaround — next after P0 impact: playback Can't play, stalls, or wrong media labels Oct 8, 2026 — with Cursor
@Quick104
Quick104 merged commit def0532 into main Oct 8, 2026
13 of 15 checks passed
@Quick104
Quick104 deleted the fix/pgs-sup-memory branch October 8, 2026 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

impact: playback Can't play, stalls, or wrong media priority: P1 Core-path bug; painful workaround — next after P0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant