From bcc78177c29fc0aaa0c96a4e47fa80a752b06e07 Mon Sep 17 00:00:00 2001 From: LainHE <36128030+LainHE@users.noreply.github.com> Date: Fri, 16 Oct 2026 09:50:26 +0800 Subject: [PATCH] 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). --- src/server/screen-share-coordinator.test.ts | 28 +++++++++++++++++++++ src/server/screen-share-coordinator.ts | 16 +++++++++++- src/shared/screen-share.ts | 8 ++++++ src/shared/server-messages.test.ts | 22 ++++++++++++++++ src/shared/server-messages.ts | 22 +++++++++++----- 5 files changed, 89 insertions(+), 7 deletions(-) diff --git a/src/server/screen-share-coordinator.test.ts b/src/server/screen-share-coordinator.test.ts index cecf40a..5cd6653 100644 --- a/src/server/screen-share-coordinator.test.ts +++ b/src/server/screen-share-coordinator.test.ts @@ -392,3 +392,31 @@ test("the viewer roster references members by client id instead of inlining avat assert.equal("avatar" in viewer, false, "the roster must not inline avatars"); } }); + +test("a viewer whose entry disappeared no longer inflates the announced count", () => { + const f = fixture(); + const owner = f.participant("owner", 1); + const streamId = f.start(owner); + const live = f.participant("live", 2); + const gone = f.participant("gone", 3); + f.handle(live, { type: "screenShareJoin", streamId }); + f.handle(gone, { type: "screenShareJoin", streamId }); + + const before = f.messages + .filter(({ to, message }) => to === owner.id && message.type === "screenShareViewerCount") + .at(-1)?.message; + assert.ok(before?.type === "screenShareViewerCount"); + assert.equal(before.viewerCount, 2); + + // The entry disappears without passing through the coordinator's leave path. + f.entries.delete(gone.id); + const later = f.participant("later", 4); + f.handle(later, { type: "screenShareJoin", streamId }); + + const after = f.messages + .filter(({ to, message }) => to === owner.id && message.type === "screenShareViewerCount") + .at(-1)?.message; + assert.ok(after?.type === "screenShareViewerCount"); + assert.equal(after.viewers?.length, 2, "only viewers that still exist are listed"); + assert.equal(after.viewerCount, 2, "the announced count must match the listed viewers"); +}); diff --git a/src/server/screen-share-coordinator.ts b/src/server/screen-share-coordinator.ts index b13a481..8ebe3d6 100644 --- a/src/server/screen-share-coordinator.ts +++ b/src/server/screen-share-coordinator.ts @@ -255,15 +255,29 @@ export class ScreenShareCoordinator { return { type: "screenShareViewerCount", streamId: stream.streamId, - viewerCount: stream.viewerCount, + // Derive the count from the same call as the roster so the announced + // number can never disagree with the entries actually listed. + viewerCount: this.screenShareViewerCount(stream), viewers: this.describeScreenViewers(stream), }; } private screenShareViewerCount(stream: ScreenStreamRecord): number { + this.pruneStaleViewers(stream); return stream.viewerEntryIds.size + stream.nativeViewerClids.size; } + /** + * Drop viewer entries whose participant is gone. A client can disappear from + * the entry map without passing through the leave path, and a lingering id + * inflates the announced count while the roster silently omits it. + */ + private pruneStaleViewers(stream: ScreenStreamRecord): void { + for (const entryId of [...stream.viewerEntryIds.keys()]) { + if (!this.entries.has(entryId)) stream.viewerEntryIds.delete(entryId); + } + } + private broadcastScreenMessage(stream: ScreenStreamRecord, message: ServerMessage, excludeEntryId?: string): void { for (const candidate of this.entries.values()) { if (candidate.id === excludeEntryId || candidate.target && teamSpeakTargetKey(candidate.target) !== stream.targetKey) continue; diff --git a/src/shared/screen-share.ts b/src/shared/screen-share.ts index cfe41dc..7d01a15 100644 --- a/src/shared/screen-share.ts +++ b/src/shared/screen-share.ts @@ -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; diff --git a/src/shared/server-messages.test.ts b/src/shared/server-messages.test.ts index 037f264..13d3a8d 100644 --- a/src/shared/server-messages.test.ts +++ b/src/shared/server-messages.test.ts @@ -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"); +}); diff --git a/src/shared/server-messages.ts b/src/shared/server-messages.ts index f957014..f7c05d9 100644 --- a/src/shared/server-messages.ts +++ b/src/shared/server-messages.ts @@ -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. */