From 686458807ab3c5eb5f7c2c41bfc52dab8589f587 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Thu, 10 Sep 2026 21:02:52 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- web/src/publish.ts | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/web/src/publish.ts b/web/src/publish.ts index 390f8fc..28eeba4 100644 --- a/web/src/publish.ts +++ b/web/src/publish.ts @@ -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 { + private async applySendParameters(pc: RTCPeerConnection): Promise { 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)); } } }