publish: stop the encoder from shrinking the picture

Every resolution above 360p was effectively a lie. The capture ran at the
requested size, the bitrate cap was applied, and the encoder then scaled
the picture down by 3x anyway and never recovered.

The cause is degradationPreference, which we never set. Its default for a
track hinted "motion" - which is every preset except Presentation - is to
protect the frame rate and sacrifice resolution. For a screen share that
is backwards: the user picked 1080p, so 1080p is what to keep.

Measured on a 1440x1080 capture with a 6000 kbps cap, 12 s each:

    default             480x270  @20  limit=bandwidth  ~214 kbps
    maintain-framerate  480x270  @20  limit=bandwidth  ~212 kbps
    balanced            480x270  @15  limit=bandwidth  ~169 kbps
    maintain-resolution 1440x810 @19  limit=none       ~717 kbps

Note the bitrate column: the shrunken variants were not short of budget,
they had 6000 kbps and used 214. Setting contentHint "detail" fixes it
too, which is why Presentation was the one preset that looked right.

Audio senders are left alone; degradationPreference is video-only.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
MoepchiandClaude Opus 5 committed 2026-09-11 05:26:33 +00:00
1 parent b505d68f56
commit 686458807a
1 file changed
+24 -9
+24 -9
View File
@@ -298,7 +298,7 @@ export class StreamPublisher {
try {
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
await this.applyBitrates(pc);
await this.applySendParameters(pc);
this.opts.send({
type: "respondJoinStream",
streamId: this.streamId,
@@ -381,27 +381,42 @@ export class StreamPublisher {
}
/**
* Caps each sender at the configured bitrate.
* Caps each sender at the configured bitrate and pins the resolution.
*
* `setupstream`'s `bitrate` is only what the stream *advertises* - the
* server does not even echo our value back unchanged, and it certainly does
* not reach the encoder. The actual limit is a sender parameter, and the
* encodings it lives on only exist once a local description is set.
*
* `degradationPreference` matters just as much. Its default for a track
* hinted "motion" is to protect the frame rate and shrink the picture, which
* for a screen share is exactly backwards: the user picked 1080p, so 1080p is
* the thing to keep. Measured on a 1440x1080 capture at a 6000 kbps cap:
*
* default / maintain-framerate / balanced -> 480x270, a 3x downscale
* maintain-resolution -> 1440x810, no downscale
*
* Without this the encoder collapses within seconds of the connection coming
* up and never recovers, and every resolution setting above 360p is a lie.
*/
private async applyBitrates(pc: RTCPeerConnection): Promise<void> {
private async applySendParameters(pc: RTCPeerConnection): Promise<void> {
const options = this.options;
if (!options) return;
for (const sender of pc.getSenders()) {
const kbps = sender.track?.kind === "audio" ? options.audioBitrateKbps : options.videoBitrateKbps;
if (!sender.track || kbps <= 0) continue;
if (!sender.track) continue;
const video = sender.track.kind === "video";
const kbps = video ? options.videoBitrateKbps : options.audioBitrateKbps;
const params = sender.getParameters();
if (!params.encodings || params.encodings.length === 0) params.encodings = [{}];
for (const encoding of params.encodings) encoding.maxBitrate = kbps * 1000;
if (video) params.degradationPreference = "maintain-resolution";
if (kbps > 0) {
if (!params.encodings || params.encodings.length === 0) params.encodings = [{}];
for (const encoding of params.encodings) encoding.maxBitrate = kbps * 1000;
}
try {
await sender.setParameters(params);
} catch (err) {
// Not fatal - the stream still runs, just uncapped.
console.warn("[publish] could not apply the bitrate cap:", describe(err));
// Not fatal - the stream still runs, just uncapped and free to shrink.
console.warn("[publish] could not apply the send parameters:", describe(err));
}
}
}