Files
WebSpeak-client-for-TeamSpeak/web/src/voice/screen-share.ts
T

811 lines
38 KiB
TypeScript

import { reactive, ref } from "vue";
import { parseScreenShareStream, parseScreenShareViewers, type ServerMessage } from "../../../src/shared/server-messages.js";
import { normalizeScreenShareIceServers, type ScreenShareIceServer, type ScreenShareClientMessage, type ScreenShareStreamDescription as ScreenShareStream, type ScreenSharePeerSignal as ScreenShareSignal } from "../../../src/shared/screen-share.js";
const SCREEN_SHARE_NEGOTIATION_TIMEOUT_MS = 15_000;
export interface ScreenShareOutputSettings {
maxWidth?: number;
maxHeight?: number;
maxFrameRate?: number;
}
export interface ScreenShareCaptureStats {
width: number | null;
height: number | null;
frameRate: number | null;
}
export interface ScreenSharePeerStats {
peerId: string;
role: "owner" | "viewer";
direction: "outbound" | "inbound";
connectionState: string;
iceConnectionState: string;
codec: string | null;
candidateType: string | null;
width: number | null;
height: number | null;
frameRate: number | null;
bitrateKbps: number | null;
packetsLost: number | null;
packetsTotal: number | null;
lossPercent: number | null;
framesDropped: number | null;
jitterMs: number | null;
roundTripTimeMs: number | null;
availableOutgoingBitrateKbps: number | null;
qualityLimitationReason: string | null;
}
export interface ScreenShareWebRtcStats {
updatedAt: number | null;
capture: ScreenShareCaptureStats | null;
peers: ScreenSharePeerStats[];
}
interface ScreenShareTransport {
isOpen(): boolean;
send(message: ScreenShareClientMessage): void;
}
/** Owns one session's capture tracks, peers, signaling and diagnostics. */
export function createScreenShareController(transport: ScreenShareTransport) {
let screenShareIceServers: RTCIceServer[] = normalizeScreenShareIceServers();
const screenShareStreams = reactive<ScreenShareStream[]>([]);
const screenShareActive = ref(false);
const screenShareStarting = ref(false);
const screenShareActiveStreamId = ref("");
const screenShareViewing = ref(false);
const screenShareViewingStreamId = ref("");
const screenShareRemoteStream = ref<MediaStream | null>(null);
const screenShareError = ref("");
const screenShareErrorCode = ref("");
const screenShareRemoteVolume = ref(1);
let screenShareLocalStream: MediaStream | null = null;
// These are encoder/output limits. The display track itself must keep the
// source resolution so selecting a high-resolution desktop or game window
// never changes that source before capture.
let screenShareOutputSettings: ScreenShareOutputSettings | null = null;
const screenSharePeers = new Map<string, RTCPeerConnection>();
const screenSharePeerRoles = new Map<string, "owner" | "viewer">();
const screenShareWebRtcStats = reactive<ScreenShareWebRtcStats>({ updatedAt: null, capture: null, peers: [] });
const screenShareStatsPrevious = new Map<string, { sampledAt: number; bytes: number | null; frames: number | null }>();
let screenShareStatsTimer: ReturnType<typeof setInterval> | null = null;
let screenShareStatsCollecting = false;
let screenShareStatsGeneration = 0;
const screenSharePendingIce = new Map<string, RTCIceCandidateInit[]>();
const screenSharePeerStreams = new Map<string, MediaStream>();
const screenSharePeerTimers = new Map<string, ReturnType<typeof setTimeout>>();
let screenShareRequestSequence = 0;
let screenSharePendingStartId = "";
let screenShareStartCancelled = false;
let screenShareStartGeneration = 0;
function sendScreenShareMessage(message: ScreenShareClientMessage): void {
if (transport.isOpen()) transport.send(message);
}
type ScreenShareStatsRecord = Record<string, unknown>;
function screenShareStatsRecord(value: unknown): ScreenShareStatsRecord {
return value && typeof value === "object" ? value as ScreenShareStatsRecord : {};
}
function screenShareStatsNumber(stats: ScreenShareStatsRecord | undefined, key: string): number | null {
const value = stats?.[key];
return typeof value === "number" && Number.isFinite(value) ? value : null;
}
function screenShareStatsString(stats: ScreenShareStatsRecord | undefined, key: string): string | null {
const value = stats?.[key];
return typeof value === "string" && value ? value : null;
}
function screenShareVideoStatsKind(stats: ScreenShareStatsRecord): string {
return screenShareStatsString(stats, "kind") ?? screenShareStatsString(stats, "mediaType") ?? "";
}
function screenShareStatsCapture(): ScreenShareCaptureStats | null {
const track = screenShareLocalStream?.getVideoTracks()[0];
if (!track) return null;
const settings = track.getSettings();
return {
width: typeof settings.width === "number" ? settings.width : null,
height: typeof settings.height === "number" ? settings.height : null,
frameRate: typeof settings.frameRate === "number" ? settings.frameRate : null,
};
}
async function collectScreenSharePeerStats(peerId: string, peer: RTCPeerConnection, role: "owner" | "viewer"): Promise<ScreenSharePeerStats | null> {
try {
const report = await peer.getStats();
if (!isCurrentPeer(peerId, peer)) return null;
const records = new Map<string, ScreenShareStatsRecord>();
let mediaStats: ScreenShareStatsRecord | undefined;
let remoteInboundStats: ScreenShareStatsRecord | undefined;
let trackStats: ScreenShareStatsRecord | undefined;
let candidatePairStats: ScreenShareStatsRecord | undefined;
report.forEach((raw) => {
const stats = screenShareStatsRecord(raw);
const id = screenShareStatsString(stats, "id");
if (id) records.set(id, stats);
const type = screenShareStatsString(stats, "type");
const kind = screenShareVideoStatsKind(stats);
if (type === "outbound-rtp" && kind === "video" && role === "owner") mediaStats = stats;
if (type === "inbound-rtp" && kind === "video" && role === "viewer") mediaStats = stats;
if (type === "remote-inbound-rtp" && kind === "video" && role === "owner") remoteInboundStats = stats;
if (type === "track" && kind === "video") trackStats = stats;
if (type === "candidate-pair" && (stats.selected === true || stats.nominated === true || screenShareStatsString(stats, "state") === "succeeded")) candidatePairStats = stats;
});
const codecId = screenShareStatsString(mediaStats, "codecId");
const codecStats = codecId ? records.get(codecId) : undefined;
const localCandidateId = screenShareStatsString(candidatePairStats, "localCandidateId");
const localCandidate = localCandidateId ? records.get(localCandidateId) : undefined;
const remoteCandidateId = screenShareStatsString(candidatePairStats, "remoteCandidateId");
const remoteCandidate = remoteCandidateId ? records.get(remoteCandidateId) : undefined;
const remoteStats = role === "owner" ? remoteInboundStats : undefined;
const frames = screenShareStatsNumber(mediaStats, role === "owner" ? "framesEncoded" : "framesDecoded")
?? screenShareStatsNumber(mediaStats, role === "owner" ? "framesSent" : "framesReceived");
const bytes = screenShareStatsNumber(mediaStats, role === "owner" ? "bytesSent" : "bytesReceived");
const now = performance.now();
const previous = screenShareStatsPrevious.get(peerId);
const elapsedMs = previous ? now - previous.sampledAt : 0;
const derivedFrameRate = previous && elapsedMs >= 250 && frames !== null && previous.frames !== null
? Math.max(0, ((frames - previous.frames) * 1_000) / elapsedMs)
: null;
const derivedBitrateKbps = previous && elapsedMs >= 250 && bytes !== null && previous.bytes !== null
? Math.max(0, ((bytes - previous.bytes) * 8) / elapsedMs)
: null;
screenShareStatsPrevious.set(peerId, { sampledAt: now, bytes, frames });
const packetsLost = screenShareStatsNumber(remoteStats ?? mediaStats, "packetsLost");
const packetsTransferred = screenShareStatsNumber(mediaStats, role === "owner" ? "packetsSent" : "packetsReceived");
const packetsTotal = packetsTransferred === null || packetsLost === null ? null : packetsTransferred + packetsLost;
const lossPercent = packetsTotal && packetsTotal > 0 && packetsLost !== null ? (packetsLost / packetsTotal) * 100 : null;
const currentRoundTripTime = screenShareStatsNumber(remoteStats, "roundTripTime") ?? screenShareStatsNumber(candidatePairStats, "currentRoundTripTime");
const jitter = screenShareStatsNumber(remoteStats ?? mediaStats, "jitter");
const directFrameRate = screenShareStatsNumber(mediaStats, "framesPerSecond") ?? screenShareStatsNumber(trackStats, "framesPerSecond");
const directBitrateKbps = screenShareStatsNumber(mediaStats, "bitrate") !== null ? (screenShareStatsNumber(mediaStats, "bitrate") as number) / 1_000 : null;
return {
peerId,
role,
direction: role === "owner" ? "outbound" : "inbound",
connectionState: peer.connectionState,
iceConnectionState: peer.iceConnectionState,
codec: screenShareStatsString(codecStats, "mimeType"),
candidateType: screenShareStatsString(localCandidate, "candidateType") ?? screenShareStatsString(remoteCandidate, "candidateType"),
width: screenShareStatsNumber(mediaStats, "frameWidth") ?? screenShareStatsNumber(trackStats, "frameWidth"),
height: screenShareStatsNumber(mediaStats, "frameHeight") ?? screenShareStatsNumber(trackStats, "frameHeight"),
frameRate: directFrameRate !== null && directFrameRate > 0 ? directFrameRate : derivedFrameRate,
bitrateKbps: directBitrateKbps ?? derivedBitrateKbps,
packetsLost,
packetsTotal,
lossPercent,
framesDropped: screenShareStatsNumber(mediaStats, "framesDropped") ?? screenShareStatsNumber(trackStats, "framesDropped"),
jitterMs: jitter === null ? null : jitter * 1_000,
roundTripTimeMs: currentRoundTripTime === null ? null : currentRoundTripTime * 1_000,
availableOutgoingBitrateKbps: screenShareStatsNumber(candidatePairStats, "availableOutgoingBitrate") === null
? null
: (screenShareStatsNumber(candidatePairStats, "availableOutgoingBitrate") as number) / 1_000,
qualityLimitationReason: screenShareStatsString(mediaStats, "qualityLimitationReason"),
};
} catch {
return null;
}
}
async function collectScreenShareWebRtcStats(): Promise<void> {
if (screenShareStatsCollecting || !screenSharePeers.size) return;
screenShareStatsCollecting = true;
const generation = screenShareStatsGeneration;
try {
const peers = await Promise.all([...screenSharePeers.entries()].map(async ([peerId, peer]) => {
const role = screenSharePeerRoles.get(peerId) ?? "viewer";
return collectScreenSharePeerStats(peerId, peer, role);
}));
if (generation !== screenShareStatsGeneration) return;
screenShareWebRtcStats.capture = screenShareStatsCapture();
screenShareWebRtcStats.peers = peers.filter((stats): stats is ScreenSharePeerStats => stats !== null);
screenShareWebRtcStats.updatedAt = Date.now();
} finally {
if (generation === screenShareStatsGeneration) screenShareStatsCollecting = false;
}
}
function startScreenShareStatsPolling(): void {
if (screenShareStatsTimer) return;
void collectScreenShareWebRtcStats();
screenShareStatsTimer = setInterval(() => { void collectScreenShareWebRtcStats(); }, 1_000);
}
function stopScreenShareStatsPolling(): void {
screenShareStatsGeneration++;
screenShareStatsCollecting = false;
if (screenShareStatsTimer) {
clearInterval(screenShareStatsTimer);
screenShareStatsTimer = null;
}
screenShareStatsPrevious.clear();
screenShareWebRtcStats.updatedAt = null;
screenShareWebRtcStats.capture = null;
screenShareWebRtcStats.peers = [];
}
function clearScreenSharePeerTimer(peerId: string): void {
const timer = screenSharePeerTimers.get(peerId);
if (!timer) return;
clearTimeout(timer);
screenSharePeerTimers.delete(peerId);
}
function armScreenSharePeerTimer(peerId: string): void {
if (!screenSharePeers.has(peerId)) return;
clearScreenSharePeerTimer(peerId);
screenSharePeerTimers.set(peerId, setTimeout(() => {
screenSharePeerTimers.delete(peerId);
if (!screenSharePeers.has(peerId)) return;
failScreenSharePeer(peerId, "屏幕共享直连协商超时,请确认双方网络允许浏览器直连");
}, SCREEN_SHARE_NEGOTIATION_TIMEOUT_MS));
}
function closeScreenSharePeer(peerId: string): void {
clearScreenSharePeerTimer(peerId);
const peer = screenSharePeers.get(peerId);
screenSharePeers.delete(peerId);
screenSharePeerRoles.delete(peerId);
screenShareStatsPrevious.delete(peerId);
screenSharePendingIce.delete(peerId);
screenSharePeerStreams.get(peerId)?.getTracks().forEach(track => track.stop());
screenSharePeerStreams.delete(peerId);
if (peer) {
peer.onicecandidate = null;
peer.ontrack = null;
peer.onconnectionstatechange = null;
}
try { peer?.close(); } catch { /* closing an already closed peer is harmless */ }
if (!screenSharePeers.size) stopScreenShareStatsPolling();
}
function closeAllScreenSharePeers(): void {
for (const peerId of [...screenSharePeers.keys()]) closeScreenSharePeer(peerId);
for (const timer of screenSharePeerTimers.values()) clearTimeout(timer);
screenSharePeerTimers.clear();
screenSharePendingIce.clear();
stopScreenShareStatsPolling();
}
function isCurrentPeer(peerId: string, peer: RTCPeerConnection): boolean {
return transport.isOpen() && screenSharePeers.get(peerId) === peer;
}
function setScreenShareP2PError(message = "直连 P2P 失败,当前网络无法建立浏览器之间的直接连接") {
screenShareErrorCode.value = "";
screenShareError.value = message;
}
function failScreenSharePeer(peerId: string, message?: string): void {
const viewingStream = screenShareStreams.find((stream) => stream.streamId === screenShareViewingStreamId.value && stream.ownerPeerId === peerId);
if (viewingStream) sendScreenShareMessage({ type: "screenShareLeave", streamId: viewingStream.streamId });
closeScreenSharePeer(peerId);
if (viewingStream) {
screenShareViewing.value = false;
screenShareViewingStreamId.value = "";
screenShareRemoteStream.value = null;
}
setScreenShareP2PError(message);
}
function createScreenSharePeer(streamId: string, peerId: string, role: "owner" | "viewer"): RTCPeerConnection {
const existing = screenSharePeers.get(peerId);
if (existing) return existing;
// STUN discovers server-reflexive candidates; it does not carry media.
// TURN is accepted only when explicitly configured by the deployment, and
// would use that external TURN service rather than the WebSpeak gateway.
const peer = new RTCPeerConnection({ iceServers: screenShareIceServers });
screenSharePeers.set(peerId, peer);
screenSharePeerRoles.set(peerId, role);
startScreenShareStatsPolling();
if (role === "owner") {
for (const track of screenShareLocalStream?.getTracks() ?? []) {
const sender = peer.addTrack(track, screenShareLocalStream!);
if (track.kind === "video") void configureScreenShareVideoSender(sender, track);
}
} else {
peer.addTransceiver("video", { direction: "recvonly" });
const stream = screenShareStreams.find((candidate) => candidate.streamId === streamId);
if (stream?.audio) peer.addTransceiver("audio", { direction: "recvonly" });
}
preferScreenShareCodecs(peer);
peer.onicecandidate = (event) => {
if (!isCurrentPeer(peerId, peer) || !event.candidate) return;
const candidate = event.candidate;
sendScreenShareMessage({
type: "screenShareSignal",
streamId,
targetPeerId: peerId,
signal: {
kind: "iceCandidate",
candidate: candidate.candidate,
sdpMid: candidate.sdpMid,
sdpMLineIndex: candidate.sdpMLineIndex,
},
});
};
peer.ontrack = (event) => {
if (!isCurrentPeer(peerId, peer) || role !== "viewer") return;
clearScreenSharePeerTimer(peerId);
const remote = event.streams[0] ?? screenSharePeerStreams.get(peerId) ?? new MediaStream();
if (!event.streams[0]) remote.addTrack(event.track);
screenSharePeerStreams.set(peerId, remote);
screenShareRemoteStream.value = remote;
screenShareViewing.value = true;
};
peer.onconnectionstatechange = () => {
if (!isCurrentPeer(peerId, peer)) return;
// `completed` belongs to RTCIceConnectionState, not the aggregate
// RTCPeerConnection.connectionState. Treating it as a connection state
// both trips the type checker and can hide the actual failed/closed
// transitions we need to handle here.
if (peer.connectionState === "connected") {
clearScreenSharePeerTimer(peerId);
} else if (peer.connectionState === "failed") {
failScreenSharePeer(peerId);
}
if (peer.connectionState === "closed" && screenSharePeers.get(peerId) === peer) closeScreenSharePeer(peerId);
};
return peer;
}
async function configureScreenShareVideoSender(sender: RTCRtpSender, track: MediaStreamTrack): Promise<void> {
if (!sender.setParameters || track.kind !== "video") return;
try {
const settings = track.getSettings();
const requested = screenShareOutputSettings;
const sourceWidth = typeof settings.width === "number" && settings.width > 0 ? settings.width : null;
const sourceHeight = typeof settings.height === "number" && settings.height > 0 ? settings.height : null;
const targetWidth = requested?.maxWidth ?? sourceWidth;
const targetHeight = requested?.maxHeight ?? sourceHeight;
const scaleResolutionDownBy = sourceWidth && sourceHeight && targetWidth && targetHeight
? Math.max(1, sourceWidth / targetWidth, sourceHeight / targetHeight)
: 1;
const sourceFrameRate = typeof settings.frameRate === "number" && settings.frameRate > 0 ? settings.frameRate : null;
const targetFrameRate = requested?.maxFrameRate
? Math.max(1, Math.min(requested.maxFrameRate, sourceFrameRate ?? requested.maxFrameRate))
: sourceFrameRate;
const parameters = sender.getParameters();
const encodings = parameters.encodings?.length ? parameters.encodings : [{}];
const firstEncoding = { ...encodings[0] };
if (scaleResolutionDownBy > 1.01) firstEncoding.scaleResolutionDownBy = scaleResolutionDownBy;
if (targetFrameRate) firstEncoding.maxFramerate = targetFrameRate;
parameters.encodings = [firstEncoding, ...encodings.slice(1)];
// Prefer keeping motion smooth and let the encoder reduce detail/resolution
// before it throws away large numbers of frames under pressure.
parameters.degradationPreference = "maintain-framerate";
await sender.setParameters(parameters);
} catch {
// Older browsers may reject one of the optional sender parameters. The
// track remains usable and the diagnostics panel still exposes the real
// negotiated frame rate and dimensions.
}
}
function preferScreenShareCodecs(peer: RTCPeerConnection): void {
const transceiver = peer.getTransceivers().find((candidate) => candidate.sender.track?.kind === "video" || candidate.receiver.track?.kind === "video");
const capabilities = typeof RTCRtpReceiver !== "undefined" ? RTCRtpReceiver.getCapabilities?.("video") : null;
if (!transceiver?.setCodecPreferences || !capabilities?.codecs?.length) return;
const vp8 = capabilities.codecs.filter((codec) => codec.mimeType.toLowerCase() === "video/vp8");
if (!vp8.length) return;
const remaining = capabilities.codecs.filter((codec) => codec.mimeType.toLowerCase() !== "video/vp8");
try { transceiver.setCodecPreferences([...vp8, ...remaining]); } catch { /* older browsers may reject codec preference changes */ }
}
async function flushScreenShareCandidates(peerId: string, peer: RTCPeerConnection): Promise<void> {
const pending = screenSharePendingIce.get(peerId) ?? [];
screenSharePendingIce.delete(peerId);
for (const candidate of pending) {
if (!isCurrentPeer(peerId, peer)) return;
try { await peer.addIceCandidate(candidate); } catch { /* an obsolete candidate can be ignored */ }
}
}
async function startScreenShareViewer(stream: ScreenShareStream): Promise<void> {
closeAllScreenSharePeers();
screenShareRemoteStream.value = null;
screenShareViewing.value = true;
screenShareViewingStreamId.value = stream.streamId;
screenShareErrorCode.value = "";
screenShareError.value = "";
const peer = createScreenSharePeer(stream.streamId, stream.ownerPeerId, "viewer");
if (stream.source === "teamspeak") {
armScreenSharePeerTimer(stream.ownerPeerId);
return;
}
try {
const offer = await peer.createOffer();
if (!isCurrentPeer(stream.ownerPeerId, peer)) return;
await peer.setLocalDescription(offer);
if (!isCurrentPeer(stream.ownerPeerId, peer)) return;
armScreenSharePeerTimer(stream.ownerPeerId);
sendScreenShareMessage({
type: "screenShareSignal",
streamId: stream.streamId,
targetPeerId: stream.ownerPeerId,
signal: { kind: "offer", sdp: peer.localDescription?.sdp ?? offer.sdp ?? "" },
});
} catch {
if (isCurrentPeer(stream.ownerPeerId, peer)) failScreenSharePeer(stream.ownerPeerId, "无法创建屏幕共享直连请求,请重试");
}
}
async function startNativeScreenShareViewer(streamId: string, peerId: string): Promise<void> {
const stream = screenShareStreams.find((candidate) => candidate.streamId === streamId);
if (!stream || stream.source !== "browser" || screenShareActiveStreamId.value !== streamId || stream.ownerPeerId !== screenShareLocalPeerId()) return;
closeScreenSharePeer(peerId);
const peer = createScreenSharePeer(streamId, peerId, "owner");
try {
const offer = await peer.createOffer();
if (!isCurrentPeer(peerId, peer)) return;
await peer.setLocalDescription(offer);
if (!isCurrentPeer(peerId, peer)) return;
armScreenSharePeerTimer(peerId);
sendScreenShareMessage({
type: "screenShareSignal",
streamId,
targetPeerId: peerId,
signal: { kind: "offer", sdp: peer.localDescription?.sdp ?? offer.sdp ?? "" },
});
} catch {
if (isCurrentPeer(peerId, peer)) failScreenSharePeer(peerId, "无法为 TeamSpeak 观看端创建屏幕共享直连");
}
}
async function handleScreenShareSignal(streamId: string, fromPeerId: string, signal: ScreenShareSignal): Promise<void> {
const stream = screenShareStreams.find((candidate) => candidate.streamId === streamId);
if (!stream) return;
if (signal.kind === "close") {
closeScreenSharePeer(fromPeerId);
if (screenShareViewingStreamId.value === streamId) {
screenShareViewing.value = false;
screenShareViewingStreamId.value = "";
screenShareRemoteStream.value = null;
}
return;
}
if (signal.kind === "iceCandidate") {
if (!signal.candidate) return;
const peer = screenSharePeers.get(fromPeerId);
const candidate: RTCIceCandidateInit = {
candidate: signal.candidate,
...(signal.sdpMid !== undefined ? { sdpMid: signal.sdpMid } : {}),
...(signal.sdpMLineIndex !== undefined ? { sdpMLineIndex: signal.sdpMLineIndex } : {}),
};
if (!peer?.remoteDescription) {
screenSharePendingIce.set(fromPeerId, [...(screenSharePendingIce.get(fromPeerId) ?? []), candidate]);
return;
}
try { await peer.addIceCandidate(candidate); } catch { /* stale ICE is not fatal */ }
return;
}
if (stream.source === "teamspeak" && screenShareViewingStreamId.value === streamId && fromPeerId === stream.ownerPeerId && signal.kind === "offer") {
const peer = screenSharePeers.get(fromPeerId) ?? createScreenSharePeer(streamId, fromPeerId, "viewer");
if (!signal.sdp) return;
armScreenSharePeerTimer(fromPeerId);
try {
await peer.setRemoteDescription({ type: "offer", sdp: signal.sdp });
if (!isCurrentPeer(fromPeerId, peer)) return;
await flushScreenShareCandidates(fromPeerId, peer);
if (!isCurrentPeer(fromPeerId, peer)) return;
const answer = await peer.createAnswer();
if (!isCurrentPeer(fromPeerId, peer)) return;
await peer.setLocalDescription(answer);
if (!isCurrentPeer(fromPeerId, peer)) return;
sendScreenShareMessage({ type: "screenShareSignal", streamId, targetPeerId: fromPeerId, signal: { kind: "answer", sdp: answer.sdp ?? "" } });
} catch {
if (isCurrentPeer(fromPeerId, peer)) failScreenSharePeer(fromPeerId, "无法回复 TeamSpeak 屏幕共享的直连请求");
}
return;
}
if (stream.source === "browser" && screenShareActiveStreamId.value === streamId && stream.ownerPeerId === screenShareLocalPeerId() && fromPeerId.startsWith("ts-viewer-") && signal.kind === "answer") {
const peer = screenSharePeers.get(fromPeerId);
if (!peer || !signal.sdp) return;
try {
await peer.setRemoteDescription({ type: "answer", sdp: signal.sdp });
if (!isCurrentPeer(fromPeerId, peer)) return;
await flushScreenShareCandidates(fromPeerId, peer);
if (!isCurrentPeer(fromPeerId, peer)) return;
} catch {
if (isCurrentPeer(fromPeerId, peer)) failScreenSharePeer(fromPeerId, "TeamSpeak 观看端无法完成屏幕共享直连协商");
}
return;
}
if (stream.source === "browser" && stream.ownerPeerId === fromPeerId && signal.kind === "answer") {
const peer = screenSharePeers.get(fromPeerId);
if (!peer || !signal.sdp) return;
try {
await peer.setRemoteDescription({ type: "answer", sdp: signal.sdp });
if (!isCurrentPeer(fromPeerId, peer)) return;
await flushScreenShareCandidates(fromPeerId, peer);
if (!isCurrentPeer(fromPeerId, peer)) return;
} catch {
if (isCurrentPeer(fromPeerId, peer)) failScreenSharePeer(fromPeerId, "观看端无法完成屏幕共享直连协商");
}
return;
}
if (screenShareActiveStreamId.value === streamId && stream.ownerPeerId === screenShareLocalPeerId()) {
const peer = screenSharePeers.get(fromPeerId) ?? createScreenSharePeer(streamId, fromPeerId, "owner");
if (signal.kind !== "offer" || !signal.sdp) return;
armScreenSharePeerTimer(fromPeerId);
try {
await peer.setRemoteDescription({ type: "offer", sdp: signal.sdp });
if (!isCurrentPeer(fromPeerId, peer)) return;
await flushScreenShareCandidates(fromPeerId, peer);
if (!isCurrentPeer(fromPeerId, peer)) return;
const answer = await peer.createAnswer();
if (!isCurrentPeer(fromPeerId, peer)) return;
await peer.setLocalDescription(answer);
if (!isCurrentPeer(fromPeerId, peer)) return;
sendScreenShareMessage({ type: "screenShareSignal", streamId, targetPeerId: fromPeerId, signal: { kind: "answer", sdp: answer.sdp ?? "" } });
} catch {
if (isCurrentPeer(fromPeerId, peer)) failScreenSharePeer(fromPeerId, "共享端无法完成观看者的直连协商");
}
}
}
// The owner peer id is generated by the gateway and is returned in the
// screenShareStarted event; the active stream's owner id is therefore the
// only stable local-owner marker available to the browser.
function screenShareLocalPeerId(): string {
const active = screenShareStreams.find((stream) => stream.streamId === screenShareActiveStreamId.value);
return active?.ownerPeerId ?? "";
}
async function startScreenShare(audio = true, settings?: ScreenShareOutputSettings): Promise<void> {
if (!transport.isOpen() || screenShareActive.value || screenShareStarting.value) return;
if (!navigator.mediaDevices?.getDisplayMedia) {
screenShareError.value = "当前浏览器不支持屏幕共享";
return;
}
screenShareErrorCode.value = "";
screenShareError.value = "";
const startGeneration = ++screenShareStartGeneration;
screenShareStarting.value = true;
screenShareStartCancelled = false;
screenShareOutputSettings = settings ?? null;
let acquiredStream: MediaStream | null = null;
try {
const stream = acquiredStream = await navigator.mediaDevices.getDisplayMedia({
// Capture the selected surface at its native browser-provided size.
// Output resolution/FPS are applied later on each RTCRtpSender so the
// user's desktop or application window is never resized or sampled at
// the output limit.
video: true,
audio,
// These are preferences: when the selected surface is a window, ask
// for that window's audio; when it is a monitor, allow system audio.
// The browser/OS may still return no audio or ignore the preference.
systemAudio: "include",
windowAudio: "window",
selfBrowserSurface: "exclude",
} as unknown as DisplayMediaStreamOptions);
if (startGeneration !== screenShareStartGeneration || !screenShareStarting.value || screenShareStartCancelled) {
stream.getTracks().forEach((track) => track.stop());
return;
}
const videoTrack = stream.getVideoTracks()[0];
if (!videoTrack) throw new Error("NO_VIDEO_TRACK");
const displaySurface = videoTrack.getSettings().displaySurface;
if ("contentHint" in videoTrack) videoTrack.contentHint = displaySurface === "browser" ? "detail" : "motion";
screenShareLocalStream = stream;
screenShareRequestSequence = (screenShareRequestSequence + 1) % 1_000_000;
screenSharePendingStartId = `screen-start-${screenShareRequestSequence}`;
for (const track of stream.getTracks()) track.addEventListener("ended", () => {
if (screenShareLocalStream === stream) stopScreenShare();
}, { once: true });
sendScreenShareMessage({ type: "screenShareStart", requestId: screenSharePendingStartId, audio: stream.getAudioTracks().length > 0, name: "我的屏幕" });
} catch (error: unknown) {
acquiredStream?.getTracks().forEach(track => track.stop());
if (startGeneration !== screenShareStartGeneration) return;
screenShareLocalStream = null;
screenShareOutputSettings = null;
screenShareStarting.value = false;
screenSharePendingStartId = "";
screenShareStartCancelled = false;
if (error instanceof DOMException && error.name === "NotAllowedError") screenShareError.value = "你取消了屏幕共享或浏览器未授予权限";
else screenShareError.value = "无法开始屏幕共享,请检查浏览器权限";
}
}
function stopScreenShare(): void {
screenShareStartGeneration += 1;
if (screenShareStarting.value) screenShareStartCancelled = true;
if (screenShareActive.value && screenShareActiveStreamId.value) sendScreenShareMessage({ type: "screenShareStop", streamId: screenShareActiveStreamId.value });
closeAllScreenSharePeers();
screenShareLocalStream?.getTracks().forEach((track) => track.stop());
screenShareLocalStream = null;
screenShareOutputSettings = null;
screenShareStarting.value = false;
screenSharePendingStartId = "";
screenShareStartCancelled = false;
screenShareActive.value = false;
screenShareActiveStreamId.value = "";
}
function joinScreenShare(streamId: string): void {
screenShareErrorCode.value = "";
screenShareError.value = "";
if (screenShareViewingStreamId.value && screenShareViewingStreamId.value !== streamId) leaveScreenShare();
screenShareRequestSequence = (screenShareRequestSequence + 1) % 1_000_000;
sendScreenShareMessage({ type: "screenShareJoin", streamId, requestId: `screen-join-${screenShareRequestSequence}` });
}
function leaveScreenShare(): void {
if (screenShareViewingStreamId.value) sendScreenShareMessage({ type: "screenShareLeave", streamId: screenShareViewingStreamId.value });
closeAllScreenSharePeers();
screenShareViewing.value = false;
screenShareViewingStreamId.value = "";
screenShareRemoteStream.value = null;
}
function stopScreenShareTransport(sendStop: boolean): void {
screenShareStartGeneration += 1;
if (sendStop && screenShareActive.value && screenShareActiveStreamId.value) sendScreenShareMessage({ type: "screenShareStop", streamId: screenShareActiveStreamId.value });
if (screenShareStarting.value) screenShareStartCancelled = true;
closeAllScreenSharePeers();
screenShareLocalStream?.getTracks().forEach((track) => track.stop());
screenShareLocalStream = null;
screenShareOutputSettings = null;
screenShareStarting.value = false;
screenSharePendingStartId = "";
screenShareStartCancelled = false;
screenShareActive.value = false;
screenShareActiveStreamId.value = "";
screenShareViewing.value = false;
screenShareViewingStreamId.value = "";
screenShareRemoteStream.value = null;
screenShareStreams.length = 0;
}
function upsertScreenShareStream(raw: unknown): ScreenShareStream | null {
const stream = parseScreenShareStream(raw);
if (!stream) return null;
const index = screenShareStreams.findIndex((candidate) => candidate.streamId === stream.streamId);
if (index >= 0) screenShareStreams.splice(index, 1, stream);
else screenShareStreams.push(stream);
return stream;
}
function handleMessage(msg: ServerMessage): boolean {
switch (msg.type) {
case "screenShareList":
screenShareStreams.length = 0;
if (Array.isArray(msg.streams)) for (const raw of msg.streams) upsertScreenShareStream(raw);
break;
case "screenShareStarted": {
const stream = upsertScreenShareStream(msg.stream);
if (!stream) break;
if (msg.owner === true) {
const requestId = typeof msg.requestId === "string" ? msg.requestId : "";
const isCurrentStart = Boolean(screenSharePendingStartId) && requestId === screenSharePendingStartId && !screenShareStartCancelled;
if (!isCurrentStart) {
sendScreenShareMessage({ type: "screenShareStop", streamId: stream.streamId });
const staleIndex = screenShareStreams.findIndex((candidate) => candidate.streamId === stream.streamId);
if (staleIndex >= 0) screenShareStreams.splice(staleIndex, 1);
break;
}
screenSharePendingStartId = "";
screenShareStarting.value = false;
screenShareActive.value = true;
screenShareActiveStreamId.value = stream.streamId;
}
break;
}
case "screenShareViewerCount": {
const stream = screenShareStreams.find((candidate) => candidate.streamId === String(msg.streamId || ""));
if (stream) {
if (typeof msg.viewerCount === "number") stream.viewerCount = Math.max(0, Math.floor(msg.viewerCount));
if (Array.isArray(msg.viewers)) stream.viewers = parseScreenShareViewers(msg.viewers);
}
break;
}
case "screenShareStopped": {
const streamId = String(msg.streamId || "");
const index = screenShareStreams.findIndex((candidate) => candidate.streamId === streamId);
if (index >= 0) screenShareStreams.splice(index, 1);
if (screenShareActiveStreamId.value === streamId) {
closeAllScreenSharePeers();
screenShareStarting.value = false;
screenSharePendingStartId = "";
screenShareStartCancelled = false;
screenShareActive.value = false;
screenShareActiveStreamId.value = "";
screenShareLocalStream?.getTracks().forEach((track) => track.stop());
screenShareLocalStream = null;
screenShareOutputSettings = null;
}
if (screenShareViewingStreamId.value === streamId) {
closeAllScreenSharePeers();
screenShareViewing.value = false;
screenShareViewingStreamId.value = "";
screenShareRemoteStream.value = null;
}
break;
}
case "screenShareJoined": {
const stream = upsertScreenShareStream(msg.stream);
if (!stream) break;
void startScreenShareViewer(stream);
break;
}
case "screenShareNativeViewerJoined":
if (typeof msg.streamId === "string" && typeof msg.viewerPeerId === "string") {
void startNativeScreenShareViewer(msg.streamId, msg.viewerPeerId);
}
break;
case "screenShareSignal":
if (typeof msg.streamId === "string" && typeof msg.fromPeerId === "string" && msg.signal) {
void handleScreenShareSignal(msg.streamId, msg.fromPeerId, msg.signal as ScreenShareSignal);
}
break;
case "screenShareViewerLeft":
if (typeof msg.viewerPeerId === "string") closeScreenSharePeer(msg.viewerPeerId);
break;
case "screenShareLeft":
if (screenShareViewingStreamId.value === String(msg.streamId || "")) leaveScreenShare();
break;
case "screenShareError":
screenShareErrorCode.value = typeof msg.code === "string" ? msg.code : "";
screenShareError.value = String(msg.message || "屏幕共享操作失败");
if (screenShareStarting.value) {
screenShareStarting.value = false;
screenSharePendingStartId = "";
screenShareStartCancelled = false;
screenShareLocalStream?.getTracks().forEach((track) => track.stop());
screenShareLocalStream = null;
screenShareOutputSettings = null;
}
if (screenShareViewing.value) {
if (screenShareViewingStreamId.value) sendScreenShareMessage({ type: "screenShareLeave", streamId: screenShareViewingStreamId.value });
closeAllScreenSharePeers();
screenShareViewing.value = false;
screenShareViewingStreamId.value = "";
screenShareRemoteStream.value = null;
}
break;
default: return false;
}
return true;
}
return {
api: {
screenShareStreams,
screenShareActive,
screenShareStarting,
screenShareActiveStreamId,
screenShareViewing,
screenShareViewingStreamId,
screenShareRemoteStream,
screenShareError,
screenShareErrorCode,
screenShareRemoteVolume,
screenShareWebRtcStats,
startScreenShare,
stopScreenShare,
joinScreenShare,
leaveScreenShare,
},
handleMessage,
setIceServers(servers?: ScreenShareIceServer[]): void {
screenShareIceServers = normalizeScreenShareIceServers(servers);
},
refreshStreams(): void { sendScreenShareMessage({ type: "screenShareList" }); },
stopTransport: stopScreenShareTransport,
};
}