Skip to content

fix(playback): choose the remux container instead of always writing MP4 - #153

Merged
JonahMMay merged 1 commit into
mainfrom
fix/remux-container-negotiation
Jul 30, 2026
Merged

JonahMMay merged 1 commit into
mainfrom
fix/remux-container-negotiation

Conversation

@JonahMMay

Copy link
Copy Markdown

What

The progressive remux passed a literal "mp4" as its output format at every call site — stream.go, jellycompat, and the proxy — so the client's declared containers list had no effect on what it received. A Matroska source played by a client that declares mkv was still rewritten into fragmented MP4.

RemuxOutputContainer now keeps the source container when the client declared it, and falls back to MP4 otherwise.

Why it matters

Rewriting into MP4 isn't free:

  • MP4 keeps its index in a moov box that a normal write patches at the end, which a pipe cannot do — so streaming MP4 means fragmenting it.
  • Several codecs a remux copies are spec-legal in MP4 but unreliable in practice: AC-3, E-AC-3, DTS and TrueHD. That's already acknowledged in the codebase — remuxAudioCopySafe refuses to treat a bare codec claim as evidence for them.

Matroska has neither problem: it streams unfragmented and carries those codecs unambiguously. #151 made AC-3 copy into the remux container on a plain claim, so without this change the common path lands AC-3 in fragmented MP4 — exactly the combination flagged above.

One rule, both cases

Why the session remuxes Source container declared? Result
Client can't play the container No MP4 — what that session needed anyway
Container fine, needs a specific audio track mapped Yes Source container kept

Trusting the declared list adds no new trust: Resolve already serves that exact container byte-for-byte via direct play on the same claim (containerOK, Case 1). A client that over-claims a container is already broken there, and this doesn't make it more so.

Verification

Not just arg assertions — the real file. Remuxing the AV1/TrueHD test source into Matroska with the AC-3 track mapped:

  • 22.7 MB written to a pipe, exit=0, no stderr
  • probes as av1 video + ac3 / channels=6 / 5.1(side) audio — both copied, surround intact

ffmpeg -h encoder=ac3 and container behavior were checked against the binary in our own image rather than documentation.

Notes

  • movflags are emitted for MP4 only — fragmentation is an MP4 workaround, not a property of piped delivery.
  • The container is carried on the session, recipe card and token claims so a reconstruct after a restart doesn't switch containers mid-stream; the proxy reads it from the claims so the same token yields the same container wherever it's served.
  • An empty value keeps the MP4 default, so existing sessions and older tokens behave exactly as before.
  • webm is deliberately excluded: it's a Matroska subset that can't carry AC-3 or H.264, so choosing it would turn a stream copy into a re-encode.

Pre-existing failures unrelated to this change (both fail at main too): TestServeDirectPlayChangedEntityRejectsOldIfRange, TestHandleReplanPlaybackV3SeekFailureRecoveryNeverChangesMediaVersion.

🤖 Generated with Claude Code

The progressive remux passed a literal "mp4" as its output format at every
call site -- stream.go, jellycompat, and the proxy -- so the client's declared
`containers` list had no effect on what it received. A Matroska source played by
a client that declares mkv was still rewritten into fragmented MP4.

That rewrite is not free. MP4 keeps its index in a moov box that a normal write
patches at the end, which a pipe cannot do, so streaming MP4 means fragmenting
it. And several codecs a remux copies are spec-legal in MP4 but unreliable in
practice: AC-3, E-AC-3, DTS and TrueHD all sit in that category, which is why
remuxAudioCopySafe already refuses to treat a bare codec claim as evidence for
them. Matroska has neither problem -- it streams unfragmented and carries those
codecs unambiguously.

RemuxOutputContainer keeps the source container when the client declared it and
falls back to MP4 otherwise. One rule covers both reasons a session remuxes: a
client that cannot play the container never declared it, so it still gets MP4 --
which is what that session needed -- while a client that can play it but needs a
specific audio track mapped keeps the container it already demuxes.

Trusting the declared list adds no new trust. Resolve already serves that exact
container byte-for-byte via direct play on the same claim (containerOK, Case 1),
so a client that over-claims a container is already broken there and this does
not make it more so.

Verified against the real file rather than only in arg assertions: remuxing the
AV1/TrueHD test source into Matroska with the AC-3 track mapped writes 22.7 MB
to a pipe, exit 0, and probes as av1 video + ac3 5.1(side) audio -- both copied,
surround intact. The same session as MP4 lands AC-3 in fragmented MP4, the
combination flagged above.

movflags are now emitted for MP4 only, since fragmentation is an MP4 workaround
rather than a property of piped delivery. The container is carried on the
session, recipe card and token claims so a reconstruct after a restart does not
switch containers mid-stream, and the proxy reads it from the claims so the same
token yields the same container wherever it is served. An empty value keeps the
MP4 default, so existing sessions and older tokens behave exactly as before.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@cursor

cursor Bot commented Jul 30, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@JonahMMay, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 6 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: d2d07aec-600e-4532-831e-52dd7d3937d5

📥 Commits

Reviewing files that changed from the base of the PR and between 3b44741 and 1f3d96b.

📒 Files selected for processing (10)
  • internal/api/handlers/playback.go
  • internal/api/handlers/stream.go
  • internal/playback/recipecard.go
  • internal/playback/remux.go
  • internal/playback/remux_container.go
  • internal/playback/remux_container_test.go
  • internal/playback/session.go
  • internal/playback/transcode_manager.go
  • internal/proxy/server.go
  • internal/streamtoken/token.go

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

@JonahMMay
JonahMMay merged commit 7303326 into main Jul 30, 2026
12 checks passed
@JonahMMay
JonahMMay deleted the fix/remux-container-negotiation branch July 30, 2026 04:39
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