Skip to content

feat(publish): encode while enabled, with an onDemand opt-in - #7143

Merged
kixelated merged 6 commits into
mainfrom
quest/m1/eager-encode/js
Oct 29, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m1/eager-encode/js

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Finishes quest/m1/eager-encode/js.md, part of the always-on encoding epic from moq-dev/moq#7101.

Summary

@moq/publish audio and video encoders now run whenever their rendition is enabled, with or without a subscriber. Their groups sit in the track's cache, where a late SUBSCRIBE can reach them within its maxDelay, or a FETCH within maxAge. A new onDemand option on Publish.Broadcast brings back the old demand gate for every rendition.

  • The root gate (js/publish/src/broadcast.ts): the broadcast hands each rendition its track whenever the track is open. With onDemand, it also waits for a subscriber, as before. A closed track now clears rendition.track through served.closed; before, that happened through demand.
  • Epochs: losing demand no longer ends an epoch, since the audio #cut() and the video teardown discontinuity both follow the track, which now stays put. Disabling still ends one. With onDemand, the last subscriber leaving still ends one.
  • Video keyframe on join: a join only forces a keyframe when demand wants a group past every group already written (wanted > max(answered, track.largest())). The old check (answered === undefined || wanted > answered) would have forced a keyframe on the first join to a track whose groups were all encoded before anyone subscribed, even when the cached group already served it. A join past the refresh ratio still gets a fresh keyframe.
  • Bandwidth: reservations still follow demand. Each encoder reserves only while a subscriber is attached, using a new watched() computed in internal.ts that changes only when demand appears or goes away. So an unwatched rendition claims nothing and encodes at its configured bitrate, as the epic decided.
  • Decoder identity: a track name's identity is now locked when the network broadcast starts, not at the first announce. An eager encoder caches groups before then, so a codec change while unannounced now takes a fresh name (video/1) instead of leaving the old codec's groups under the new catalog config. This replaces the "identity changes before the first announce keep the catalog ID" behavior from moq-dev/moq#5552. That test is now a regression test that writes a group before announcing.
  • Docs: doc/lib/js/publish.md (the Encoding section and the example comment) and js/publish/README.md describe both modes. Code comments no longer call it a "demand gate".
  • Quests: deletes js.md and its links. (The line-reference updates to push-to-talk/reenable.md were dropped: moq-dev/moq#7163 finished and deleted that quest.)

Public API: the default behavior changes, and BroadcastInput.onDemand: Getter<boolean> is new (API report updated). Wire: none.

Tests

  • An enabled video encoder with no subscriber writes groups, and a late subscriber with a covering maxDelay reads groups 0 and 1 from before it arrived, with no forced keyframe. With onDemand, nothing is encoded until it subscribes, and its arrival opens group 0.
  • The same for audio, plus: the last subscriber leaving writes no discontinuity by default, and writes one with onDemand.
  • A join that a written group serves forces no keyframe. I checked that this test and the late-subscriber test fail against the old keyframe condition.
  • An unwatched rendition encodes at its configured bitrate, follows the grant while subscribed, and goes back once unsubscribed.
  • Broadcast: the track is handed over without a subscriber by default. With onDemand, it is handed over only while subscribed, and the broadcast keeps the same open track across the gap.
  • Three tests that assumed the demand gate were updated: an active that is now already true, a replacement track handed over at once, and a recovered encoder active on its fresh track. The two encoder-level "demand gap" tests are renamed "on-demand gap".

Local: sh/js/workspace.py check and test on the changed packages and their dependents (publish, room, doc, browser harnesses) pass. So do biome, tsc, the API report check and remark on the docs.

Choices made without asking

  • onDemand is a reactive Getter<boolean>, like the broadcast's other inputs, so it can be toggled at runtime.
  • <moq-publish> gets no on-demand attribute. The quest scopes the option to the broadcast, and push-to-talk/mute is editing element.ts in parallel. As a result, element users get always-on with no way to opt out.
  • Text renditions (broadcast.text()) follow the same gate, so a caption writer gets its track without a subscriber too, unless onDemand is set.
  • I stopped reserving bandwidth while nobody watches, instead of holding an idle reservation and ignoring its grant. That matches the old behavior exactly and needs no new allocator logic.
  • No version bump. The next @moq/publish release should be a minor bump, because the default behavior changes.

Suggested follow-ups

  • An on-demand attribute on <moq-publish>, if element users need the opt-in. Worth doing after the mute quest lands.
  • @moq/room publishes through Publish.Broadcast, so a joined room now encodes the camera and microphone while nobody watches. That is intended by the epic, but the room docs may want to say so. That falls under the epic's own docs work.

🤖 Generated with Claude Code

(Written by Claude Opus 5.5)

kixelated and others added 3 commits October 28, 2026 15:04
@moq/publish audio and video encoders now run whenever their rendition is
enabled, with or without a subscriber, so their groups wait in the track's
cache for a late SUBSCRIBE's maxDelay or a FETCH to reach. The broadcast
hands each rendition its track while the track is open; `onDemand: true`
on the broadcast restores the old gate for every rendition.

- Losing demand no longer ends an epoch; disabling still does.
- Video forces a keyframe on join only when demand wants a group past every
  one written, so a join served by a group cached before any subscriber
  arrived forces none.
- Bandwidth reservations stay keyed on demand: an unwatched rendition
  claims nothing and encodes at its configured bitrate.

Finishes quest/m1/eager-encode/js.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title @moq/publish encodes while enabled feat(publish): encode while enabled, with an onDemand opt-in Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 22:35
@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 2 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d7c76241-a80d-4c51-90b7-4d989dfc9849
Commits

Reviewing files that changed from the base of the PR and between 97387af and b509d05.

Files selected for processing: 14
  • doc/lib/js/publish.md
  • js/publish/README.md
  • js/publish/api/index.api.md
  • js/publish/src/audio/encoder.test.ts
  • js/publish/src/audio/encoder.ts
  • js/publish/src/broadcast.test.ts
  • js/publish/src/broadcast.ts
  • js/publish/src/internal.ts
  • js/publish/src/rendition.ts
  • js/publish/src/video/encoder.test.ts
  • js/publish/src/video/encoder.ts
  • quest/m1/eager-encode/README.md
  • quest/m1/eager-encode/js.md
  • quest/m1/eager-encode/raw-audio.md
  • Autofix · 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-29T04:46:04.116287Z 0e34efc New commits
ℹ️ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fcf3157763

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread js/publish/src/broadcast.ts Resolved

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: fcf3157.

Confirmed the existing P1 decoder-identity finding, rather than duplicating it inline: broadcast.ts:468–471 now exposes tracks before announce, but :250–256 locks/replaces identity only after #served is initialized at :398. With announce:false, a codec/config change can therefore leave cached media under the new catalog's incompatible decoder identity. Lock identity when eager media can first be written and add a pre-announcement codec-change/late-replay regression test.

Otherwise the direction is coherent: one broadcast-level onDemand switch is simpler than separate encoder gates, and demand-scoped reservations avoid charging idle eager renditions against the live bandwidth budget. The cached-group-aware keyframe condition preserves refresh requests without forcing every join to cut.

Verification: static review of changed production code/tests and surrounding catalog identity, encoder lifecycle, track demand, and signal cleanup semantics. No browser, codec, or test execution.

kixelated and others added 2 commits October 28, 2026 21:44
# Conflicts:
#	quest/m1/push-to-talk/reenable.md
An eager encoder caches groups on its track before the first announce, so
fixing a name's identity only at announce let a pre-announce codec change
reuse the name, and a late subscriber or fetch read the old codec's groups
under the new catalog config. Lock identities when the network broadcast
starts instead.

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

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)
Reviewed commit: 0e34efc

The previously confirmed P1 decoder-identity finding is addressed: js/publish/src/broadcast.ts now seeds the identity map when the network broadcast starts, before eager tracks are handed over. Pre-announcement identity changes therefore use the existing replacement-track path instead of relabeling cached groups. The updated regression checks that a bitrate-only change keeps the track, a codec change closes it, and the catalog names a fresh track with sequence zero.

No new actionable bugs found in this update. Moving the identity boundary is the right focused fix and reuses existing lifecycle logic rather than adding a separate eager-mode mechanism.

Verification: base-aware comparison against the prior reviewed head, static inspection of the fix, catalog folding, track replacement, and changed test. No tests, browser playback, codec execution, or late-fetch integration run.

(Written by review (OpenAI))

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merge summary

  • Merged main. Kept the deletion of quest/m1/push-to-talk/reenable.md, which moq-dev/moq#7163 removed when that quest finished, so this PR's line-reference edits to it are dropped. fix(js): re-enabling an audio encoder reuses its Opus probe #7163's Opus probe cache in js/publish/src/audio/encoder.ts merged cleanly with this PR's demand-gate removal: it only touches #runConfig, and the encoder's bandwidth reservation still follows watched(). The js/publish tests pass (289), as do workspace.py check and test on the changed packages.
  • Fixed the P1 decoder-identity finding in 0e34efc. Identity is now locked when the network broadcast starts, not at the first announce, so a codec change while unannounced takes a fresh track name instead of leaving eagerly cached groups under the new config. The pre-announce test from moq-dev/moq#5552 is now a regression test for this, and it fails without the fix. The automated review of that head found nothing new.
  • Dropped the empty ## Required heading in quest/m1/eager-encode/raw-audio.md. Main finished its audio.md blocker and this PR finishes js.md, so raw-audio is now unblocked.
  • Release: the next @moq/publish release needs a minor bump, because the default changes to encoding while enabled and onDemand is new. Not bumped here.

(Written by Claude Opus 5.5)

@kixelated
kixelated added this pull request to the merge queue Oct 29, 2026
Merged via the queue into main with commit 991804a Oct 29, 2026
11 checks passed
@kixelated
kixelated deleted the quest/m1/eager-encode/js branch October 29, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant