fix(screen-share): keep the announced viewer count in step with the roster

Two smaller gaps left over from removing the inline avatars.

The announced viewerCount summed the viewer entry ids and the native viewer
ids, while the roster silently dropped ids whose participant had vanished
from the entry map. A client that disappears without passing through the
leave path therefore inflated the count for as long as the stream lived.
Prune those ids, and derive the count in the same call that builds the
roster so the two can no longer disagree.

The legacy inline-avatar path capped each entry at 128 KiB but placed no
bound on the roster total, which still allowed 64 x 128 KiB in a single
broadcast. Bound the total as well; entries past the budget fall back to
the client-side member lookup like any other viewer without an avatar.

Both behaviours are covered by new tests: a dropped entry no longer
inflates the count, and eight 128 KiB avatars are trimmed to the budget
(measured at 1048576 bytes without the bound).
This commit is contained in:
LainHE committed 2026-10-16 09:50:26 +08:00
1 parent bfa26da9eb
commit bcc78177c2
5 files changed
+89 -7

No files matched your search

+8
View File
@@ -78,6 +78,14 @@ export type ScreenSharePeerSignal =
kind: "close";
};
/**
* Ceiling on the total inline avatar bytes a client accepts from a legacy
* gateway for one viewer roster. Current gateways send client ids instead of
* avatars, so this bounds only the compatibility path; a per-entry limit alone
* would still admit 64 x 128 KiB in a single broadcast.
*/
export const SCREEN_SHARE_LEGACY_AVATAR_BUDGET = 256 * 1024;
export interface ScreenShareViewerDescription {
peerId: string;
nickname: string;
+22
View File
@@ -1,6 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import { parseServerMessage, parseScreenShareStream, parseVoiceAudioBridgeStats, type ServerMessage } from "./server-messages.js";
import { SCREEN_SHARE_LEGACY_AVATAR_BUDGET } from "./screen-share.js";
const stream = {
streamId: "stream-1", source: "browser" as const, ownerPeerId: "owner", ownerClientId: 1,
@@ -127,3 +128,24 @@ test("screen share viewers carry a client id and still accept a legacy inline av
assert.equal(viewers[2]?.clientId, undefined);
assert.equal(viewers[3]?.clientId, undefined);
});
test("legacy inline avatars are bounded in total, not merely per entry", () => {
const avatar = `data:image/png;base64,${"A".repeat(128 * 1024 - 22)}`;
const message = parseServerMessage({
type: "screenShareList",
streams: [{
streamId: "s", ownerPeerId: "owner",
viewers: Array.from({ length: 8 }, (_, index) => ({ peerId: `v${index}`, nickname: `n${index}`, avatar })),
}],
});
assert.ok(message?.type === "screenShareList");
const viewers = message.streams[0]?.viewers ?? [];
assert.equal(viewers.length, 8, "entries are kept even when their avatar is dropped");
const accepted = viewers.reduce((total, viewer) => total + (viewer.avatar?.length ?? 0), 0);
assert.ok(
accepted <= SCREEN_SHARE_LEGACY_AVATAR_BUDGET,
`accepted avatar bytes must stay within the roster budget, got ${accepted}`,
);
assert.ok(accepted > 0, "the legacy path still renders the leading avatars");
assert.ok(viewers.some(viewer => viewer.avatar === undefined), "entries past the budget must fall back to a local lookup");
});
+16 -6
View File
@@ -1,5 +1,5 @@
import type { ChannelMember, ChannelInfo, ServerEvent, VoiceAudioBridgeStats } from "./voice-models.js";
import { normalizeScreenShareIceServers, parseScreenShareSignal, type ScreenShareIceServer, type ScreenSharePeerSignal, type ScreenShareStreamDescription, type ScreenShareViewerDescription } from "./screen-share.js";
import { normalizeScreenShareIceServers, parseScreenShareSignal, SCREEN_SHARE_LEGACY_AVATAR_BUDGET, type ScreenShareIceServer, type ScreenSharePeerSignal, type ScreenShareStreamDescription, type ScreenShareViewerDescription } from "./screen-share.js";
import { normalizeVoiceStunServer } from "./voice-ice.js";
import { isSessionDescription, type SessionDescription } from "./webrtc.js";
@@ -100,12 +100,22 @@ export function parseVoiceAudioBridgeStats(value: unknown): VoiceAudioBridgeStat
export function parseScreenShareViewers(raw: unknown): ScreenShareViewerDescription[] {
if (!Array.isArray(raw)) return [];
let avatarBudget = SCREEN_SHARE_LEGACY_AVATAR_BUDGET;
return raw.filter((viewer): viewer is RecordValue & { peerId: string; nickname: string } => isRecord(viewer) && text(viewer.peerId) && text(viewer.nickname))
.slice(0, 64).map(viewer => ({
peerId: viewer.peerId.slice(0, 128), nickname: viewer.nickname.slice(0, 120),
...(clientId(viewer.clientId) ? { clientId: viewer.clientId } : {}),
...(text(viewer.avatar) && viewer.avatar.length <= 128 * 1024 ? { avatar: viewer.avatar } : {}),
}));
.slice(0, 64).map(viewer => {
// Client ids replaced inline avatars, so this only serves older gateways.
// Bound the roster's total avatar bytes, not just each entry: the
// per-entry cap alone still allows 64 x 128 KiB per broadcast.
const avatar = text(viewer.avatar) && viewer.avatar.length <= 128 * 1024 && viewer.avatar.length <= avatarBudget
? viewer.avatar
: undefined;
if (avatar) avatarBudget -= avatar.length;
return {
peerId: viewer.peerId.slice(0, 128), nickname: viewer.nickname.slice(0, 120),
...(clientId(viewer.clientId) ? { clientId: viewer.clientId } : {}),
...(avatar ? { avatar } : {}),
};
});
}
/** Defaults preserve stream descriptions sent by older gateway versions. */