The editor could clear 2048 quote characters although escaping exceeded the Native 4096-byte command budget. Measure the complete command before sending and retain oversized text. Core, Native and Browser use the 8192-byte UTF-8 carrier bound; independent queue budgets keep their own names. Static Browser values are compared with compiled Core, with no new WASM hot-path dependency. Real WebCrypto boundary probes, Native compilation and all 2521 Web tests pass.
1134 lines
55 KiB
TypeScript
1134 lines
55 KiB
TypeScript
import { data_frame_bytes, data_queue_frames } from "../../../prototype/channel-browser/pkg/piik_channel_browser";
|
|
import { inputFrameFits } from "../remote-control/protocol";
|
|
import { LinearCandidateAdmission } from "../webrtc/linear-ice-candidate";
|
|
import { addRemoteIceCandidate } from "../webrtc/remote-ice-candidate";
|
|
import { collectTrackConnectionMetrics, decodedVideoFrameSamples, type StatsAccumulator } from "../webrtc/stats";
|
|
import { debugTrack, observeDebugConnection } from "../lib/debug-webrtc";
|
|
import type { ConnectionMetrics } from "../types";
|
|
import { preferOpusStereoReceive } from "../webrtc/opus-receive-sdp";
|
|
import { applyVideoCodecPreference, normalizeVideoOfferSdp, type BrowserVideoCodecPreference } from "../webrtc/video-codec";
|
|
import { DEFAULT_ROOM_VOICE_BITRATE, type ChannelPublication, type ChannelSignal } from "./protocol";
|
|
import { applyVideoCaptureProfile, configureAudioSender, configureVideoSender, screenAudioBitrate, screenContentHint, screenShareEncodings, videoQualitySettingsEqual, type VideoSenderProfile } from "../media/quality";
|
|
import { BrowserEncodingOutput, supportsBrowserEncoding } from "../media/browser-encoding-output";
|
|
import type { BrowserEncodingPool, BrowserPooledSender } from "../media/browser-encoding-pool";
|
|
import { cloneSenderVideoTrack, senderCaptureTrack, stopSenderVideoTrack } from "../media/sender-video-track";
|
|
import { waitForConnectionOperation } from "../webrtc/connection-operation";
|
|
|
|
const TRANSPORT_PREPARE_MS = 20_000;
|
|
const INPUT_SEND_BUFFER_BYTES = 8192;
|
|
const DATA_SEND_BUFFER_BYTES = 65536;
|
|
|
|
export interface OutgoingMedia {
|
|
kind: ChannelPublication["kind"];
|
|
stream: MediaStream | null;
|
|
profile?: VideoSenderProfile;
|
|
audioBitrate?: number;
|
|
voiceBitrate?: number;
|
|
paused?: boolean;
|
|
codec?: BrowserVideoCodecPreference;
|
|
}
|
|
|
|
interface SenderBinding {
|
|
sender: RTCRtpSender;
|
|
source: MediaStreamTrack;
|
|
lifetime: AbortController;
|
|
encoded?: BrowserEncodingOutput;
|
|
pooled?: BrowserPooledSender | null;
|
|
}
|
|
|
|
interface OutgoingBinding {
|
|
kind: ChannelPublication["kind"];
|
|
stream: MediaStream;
|
|
senders: SenderBinding[];
|
|
failed: boolean;
|
|
paused: boolean;
|
|
profile?: VideoSenderProfile;
|
|
audioBitrate?: number;
|
|
rejectedTracks?: MediaStreamTrack[];
|
|
replacement?: MediaStreamTrack[];
|
|
metrics?: Map<string, StatsAccumulator>;
|
|
slots?: MediaSlots;
|
|
}
|
|
|
|
interface MediaSlot {
|
|
sender: RTCRtpSender;
|
|
transceiver: RTCRtpTransceiver;
|
|
encoded?: BrowserEncodingOutput | null;
|
|
}
|
|
|
|
interface MediaSlots {
|
|
key: string;
|
|
stream: MediaStream;
|
|
senders: MediaSlot[];
|
|
}
|
|
|
|
interface TransportOptions {
|
|
identity?: { sessionId: string; pairId: string; peerId: string };
|
|
polite: boolean;
|
|
iceServers: RTCIceServer[];
|
|
canReceiveVideoLayers?: boolean;
|
|
videoPool?: BrowserEncodingPool;
|
|
send(payload: ChannelSignal): void;
|
|
receive(flowId: string, stream: MediaStream): void;
|
|
retire?(flowId: string): void;
|
|
outgoingFailed?(flowId: string): void;
|
|
connected(): void;
|
|
disconnected?(): void;
|
|
roomData(ready: boolean): void;
|
|
failed(resourcesReleased: boolean): void;
|
|
data?(kind: "control" | "chunk", data: string | Uint8Array): void;
|
|
dataState?(ready: boolean): void;
|
|
bulkClosed?(): void;
|
|
input?(channel: RTCDataChannel | null): void;
|
|
}
|
|
|
|
/** One physical pair and its media slots. Flows lease senders, never source tracks. */
|
|
export class ChannelTransport {
|
|
readonly pc: RTCPeerConnection;
|
|
private sending = new Map<string, OutgoingBinding>();
|
|
private mediaSlots = new Set<MediaSlots>();
|
|
private pausedEncodings = new WeakMap<RTCRtpSender, boolean[]>();
|
|
private stableSenders: Map<RTCRtpSender, MediaStreamTrack | null> | null = null;
|
|
private remoteIds = new Map<string, string>();
|
|
private frameBaselines = new Map<string, ReadonlyMap<string, number | null> | null>();
|
|
private receiving = new Map<string, MediaStream>();
|
|
private applyingRemote = false;
|
|
private ignoreOffer = false;
|
|
private pendingOffer: { payload: ChannelSignal; senders: Map<RTCRtpSender, MediaStreamTrack | null> } | null = null;
|
|
private localOfferFailed = false;
|
|
private answer: ChannelSignal | null = null;
|
|
private candidates: NonNullable<ChannelSignal["candidate"]>[] = [];
|
|
private linearCandidates: LinearCandidateAdmission | null = new LinearCandidateAdmission();
|
|
private operations: Promise<void> = Promise.resolve();
|
|
private deadline: ReturnType<typeof setTimeout> | null;
|
|
private established = false;
|
|
private closed = false;
|
|
private releaseDebug: () => void;
|
|
private rooms = new Set<string>();
|
|
private channel: RTCDataChannel | null = null;
|
|
private bulk: RTCDataChannel | null = null;
|
|
private input: RTCDataChannel | null = null;
|
|
private inputEnabled = false;
|
|
private writers = new Map<"control" | "chunk", ChannelWriter>();
|
|
private dataLifetime = new AbortController();
|
|
private roomDataFailed = false;
|
|
|
|
outgoingMetrics(report: RTCStatsReport, flowIds: ReadonlySet<string>): Array<{ flowId: string; metrics: ConnectionMetrics }> {
|
|
return [...this.sending].flatMap(([flowId, binding]) => {
|
|
if (!flowIds.has(flowId)) return [];
|
|
const video = binding.senders.find(value => value.source.kind === "video");
|
|
const track = binding.senders.find(value => value.source.kind === (binding.kind === "screen" ? "video" : "audio"))?.sender.track;
|
|
if (!track) return [];
|
|
// Picture counters belong to the producer while this sender uses its clock.
|
|
if (video && track !== video.encoded?.track) {
|
|
debugTrack(video.source, { flowId, role: "source", senderTrackId: track.id });
|
|
debugTrack(senderCaptureTrack(track), { flowId, role: "sender-input", senderTrackId: track.id });
|
|
}
|
|
const audio = binding.senders.find(value => value.source.kind === "audio")?.sender.track;
|
|
return collectTrackConnectionMetrics(report, "send", binding.metrics ??= new Map(), {
|
|
kind: binding.kind === "screen" ? "video" : "audio", trackIdentifier: track.id,
|
|
audioTrackIdentifier: audio?.id ?? null,
|
|
}, this.pc).map(metrics => ({ flowId, metrics: video?.pooled?.metrics(metrics) ?? metrics }));
|
|
});
|
|
}
|
|
|
|
constructor(private options: TransportOptions) {
|
|
this.pc = new RTCPeerConnection({ iceServers: options.iceServers, bundlePolicy: "max-bundle" });
|
|
const transport = this;
|
|
this.releaseDebug = observeDebugConnection(this.pc, { role: "channel-peer", ...options.identity,
|
|
get outgoing() { return [...transport.sending].map(([flowId, output]) => ({ flowId,
|
|
trackIds: output.senders.flatMap(({ sender }) => sender.track ? [sender.track.id] : []) })); },
|
|
get incoming() { return [...transport.receiving].flatMap(([streamId, stream]) => {
|
|
const flowId = transport.remoteIds.get(streamId);
|
|
return flowId ? [{ flowId, trackIds: stream.getTracks().map(track => track.id) }] : [];
|
|
}); },
|
|
});
|
|
this.deadline = setTimeout(() => this.fail(), TRANSPORT_PREPARE_MS);
|
|
this.pc.onicecandidate = ({ candidate }) => {
|
|
if (candidate) options.send({ candidate: candidate.toJSON() });
|
|
};
|
|
this.pc.onnegotiationneeded = () => { if (!this.localOfferFailed) this.offer(); };
|
|
this.pc.oniceconnectionstatechange = () => {
|
|
if (["connected", "completed"].includes(this.pc.iceConnectionState)) this.linearCandidates = null;
|
|
};
|
|
this.pc.onconnectionstatechange = () => {
|
|
if (this.pc.connectionState === "connected") {
|
|
if (!this.established) {
|
|
this.established = true;
|
|
this.clearDeadline();
|
|
}
|
|
if (this.inputEnabled) {
|
|
if (this.input) options.input?.(this.input);
|
|
else this.reopenInput();
|
|
}
|
|
if (!this.rooms.size || this.dataLifetime.signal.aborted) options.dataState?.(false);
|
|
else this.reportFileDataReady();
|
|
options.connected();
|
|
this.reportRoomDataReady();
|
|
this.syncDeadline();
|
|
} else if (this.pc.connectionState === "failed") this.fail();
|
|
else if (this.pc.connectionState === "disconnected") {
|
|
options.input?.(null);
|
|
options.disconnected?.();
|
|
} else if (this.pc.connectionState === "closed") options.input?.(null);
|
|
};
|
|
this.pc.ondatachannel = ({ channel }) => {
|
|
if (this.closed || ["failed", "closed"].includes(this.pc.connectionState)) { channel.close(); return; }
|
|
if (channel.label === "piik-input") {
|
|
if (this.input) channel.close(); else this.installInput(channel);
|
|
return;
|
|
}
|
|
// DCEP can precede this endpoint's room projection on a media carrier.
|
|
// Keep at most one physical channel per lane; room leases admit its data.
|
|
if ((channel.label !== "piik-room" && channel.label !== "piik-file") || (channel.label === "piik-room" ? this.channel : this.bulk)) { channel.close(); return; }
|
|
this.installChannel(channel);
|
|
};
|
|
this.pc.ontrack = ({ streams }) => {
|
|
for (const stream of streams) {
|
|
const flowId = this.remoteIds.get(stream.id);
|
|
// Replayed SDP can introduce a receiver before a current flow claims its stream.
|
|
if (!flowId && !this.applyingRemote) continue;
|
|
this.receiving.set(stream.id, stream);
|
|
if (flowId && !this.applyingRemote) options.receive(flowId, stream);
|
|
}
|
|
};
|
|
}
|
|
|
|
/** Input follows selected screens, independently of room data leases. */
|
|
setInputEnabled(enabled: boolean, probe: boolean): void {
|
|
if (this.closed) return;
|
|
const changed = this.inputEnabled !== enabled;
|
|
this.inputEnabled = enabled;
|
|
// An empty pair needs an SCTP offer to start ICE. Input delivery still requires a selected screen.
|
|
if ((enabled || probe) && !this.input && !this.options.polite && !["failed", "closed"].includes(this.pc.connectionState)) {
|
|
let channel: RTCDataChannel;
|
|
try { channel = this.pc.createDataChannel("piik-input", { ordered: true }); }
|
|
catch (error) {
|
|
// Once established, the pair can recover without a new data channel.
|
|
if (this.established && error instanceof DOMException && error.name === "OperationError") {
|
|
console.warn("Could not create the screen input channel", error);
|
|
return;
|
|
}
|
|
throw error;
|
|
}
|
|
this.installInput(channel);
|
|
this.retryOffer();
|
|
} else if (changed) this.options.input?.(enabled ? this.input : null);
|
|
}
|
|
|
|
private installInput(channel: RTCDataChannel): void {
|
|
this.input = channel;
|
|
const current = () => !this.closed && this.input === channel;
|
|
let opened = channel.readyState === "open";
|
|
channel.addEventListener("open", () => {
|
|
opened = true;
|
|
if (current() && this.inputEnabled) this.options.input?.(channel);
|
|
});
|
|
const lost = () => {
|
|
if (!current()) return;
|
|
this.input = null;
|
|
this.options.input?.(null);
|
|
channel.close();
|
|
if (opened) queueMicrotask(() => this.reopenInput());
|
|
};
|
|
channel.addEventListener("close", lost);
|
|
channel.addEventListener("error", lost);
|
|
if (this.inputEnabled) this.options.input?.(channel);
|
|
}
|
|
|
|
private reopenInput(): void {
|
|
if (!this.inputEnabled || this.pc.connectionState !== "connected") return;
|
|
try { this.setInputEnabled(true, false); }
|
|
catch (error) { console.warn("Could not reopen the screen input channel", error); }
|
|
}
|
|
|
|
sendInput(frame: string): boolean {
|
|
const channel = this.input;
|
|
if (!this.inputEnabled || channel?.readyState !== "open" || this.closed || this.pc.connectionState !== "connected") return false;
|
|
if (!inputFrameFits(frame) || channel.bufferedAmount > INPUT_SEND_BUFFER_BYTES) { channel.close(); return false; }
|
|
try { channel.send(frame); return true; }
|
|
catch { channel.close(); return false; }
|
|
}
|
|
|
|
/** Room leases share one reliable ordered channel on this physical pair. */
|
|
setRooms(rooms: readonly string[]): void {
|
|
if (this.closed) return;
|
|
const added = rooms.some(room => !this.rooms.has(room));
|
|
const changed = added || rooms.length !== this.rooms.size;
|
|
this.rooms = new Set(rooms);
|
|
if (changed && !rooms.length) this.closeData();
|
|
if (added) {
|
|
this.roomDataFailed = false;
|
|
if (this.dataLifetime.signal.aborted) this.dataLifetime = new AbortController();
|
|
}
|
|
if (!rooms.length || this.dataLifetime.signal.aborted) { this.syncDeadline(); return; }
|
|
try {
|
|
if (!this.channel && !this.options.polite) {
|
|
this.installChannel(this.pc.createDataChannel("piik-room", { ordered: true }));
|
|
}
|
|
this.ensureBulkChannel();
|
|
} catch (error) {
|
|
if (!this.established || !(error instanceof DOMException) || error.name !== "OperationError") throw error;
|
|
console.warn("Could not create the room data channels", error);
|
|
this.failRoomData();
|
|
return;
|
|
}
|
|
this.reportRoomDataReady();
|
|
this.syncDeadline();
|
|
if (changed) this.retryOffer();
|
|
}
|
|
|
|
isRoomDataReady(): boolean {
|
|
return !this.closed && !this.roomDataFailed && this.rooms.size > 0
|
|
&& this.channel?.readyState === "open" && this.pc.connectionState === "connected";
|
|
}
|
|
|
|
isFileDataReady(roomId?: string): boolean {
|
|
return (roomId === undefined || this.rooms.has(roomId)) && this.isRoomDataReady() && this.bulk?.readyState === "open";
|
|
}
|
|
|
|
private reportFileDataReady(): void {
|
|
if (this.isFileDataReady()) this.options.dataState?.(true);
|
|
}
|
|
|
|
private ensureBulkChannel(): void {
|
|
if (!this.bulk && !this.options.polite) this.installChannel(this.pc.createDataChannel("piik-file", { ordered: true }));
|
|
}
|
|
|
|
private writer(kind: "control" | "chunk"): ChannelWriter {
|
|
let writer = this.writers.get(kind);
|
|
if (!writer) {
|
|
writer = new ChannelWriter(kind);
|
|
this.writers.set(kind, writer);
|
|
}
|
|
return writer;
|
|
}
|
|
|
|
private installChannel(channel: RTCDataChannel): void {
|
|
const bulk = channel.label === "piik-file";
|
|
if (bulk) this.bulk = channel; else this.channel = channel;
|
|
const kind = bulk ? "chunk" : "control";
|
|
const writer = this.writer(kind);
|
|
writer.install(channel);
|
|
const current = () => !this.closed && (bulk ? this.bulk : this.channel) === channel;
|
|
channel.onopen = () => {
|
|
if (!current()) return;
|
|
this.reportRoomDataReady();
|
|
this.reportFileDataReady();
|
|
this.syncDeadline();
|
|
};
|
|
const failed = () => {
|
|
if (!current()) return;
|
|
writer.close();
|
|
this.writers.delete(kind);
|
|
if (bulk) {
|
|
this.bulk = null;
|
|
channel.close();
|
|
if (!["failed", "closed"].includes(this.pc.connectionState)) this.options.bulkClosed?.();
|
|
return;
|
|
}
|
|
this.channel = null;
|
|
this.failRoomData();
|
|
channel.close();
|
|
};
|
|
channel.onclose = failed;
|
|
channel.onerror = failed;
|
|
channel.binaryType = "arraybuffer";
|
|
channel.onmessage = ({ data }) => {
|
|
if (!current()) return;
|
|
if (bulk && this.rooms.size && !this.dataLifetime.signal.aborted
|
|
&& data instanceof ArrayBuffer && data.byteLength <= writer.maxFrameBytes) this.options.data?.("chunk", new Uint8Array(data));
|
|
// Core may defer control from this pair until the room grant and plan arrive.
|
|
else if (!bulk && typeof data === "string" && new TextEncoder().encode(data).length <= writer.maxFrameBytes) this.options.data?.("control", data);
|
|
};
|
|
this.reportRoomDataReady();
|
|
this.syncDeadline();
|
|
}
|
|
|
|
cancelChunks(request: string): void { this.writers.get("chunk")?.cancel(request); }
|
|
|
|
async sendData(kind: "control" | "chunk", data: string | Uint8Array, allowed?: () => boolean, request?: string): Promise<void> {
|
|
const lifetime = this.dataLifetime.signal;
|
|
if (!this.rooms.size || lifetime.aborted) throw new Error("peer_data_unavailable");
|
|
if (kind === "chunk") this.ensureBulkChannel();
|
|
try { await this.writer(kind).send(data, allowed, request); }
|
|
catch (error) {
|
|
if (lifetime.aborted || ["failed", "closed"].includes(this.pc.connectionState)) throw new Error("peer_data_unavailable");
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
private reportRoomDataReady(): void {
|
|
if (this.isRoomDataReady()) this.options.roomData(true);
|
|
}
|
|
|
|
private failRoomData(): void {
|
|
if (!this.rooms.size || this.roomDataFailed) return;
|
|
this.roomDataFailed = true;
|
|
this.closeData();
|
|
this.options.roomData(false);
|
|
this.syncDeadline();
|
|
}
|
|
|
|
private closeData(): void {
|
|
this.dataLifetime.abort();
|
|
for (const writer of this.writers.values()) writer.close();
|
|
this.writers.clear();
|
|
const channels = [this.channel, this.bulk];
|
|
this.channel = this.bulk = null;
|
|
for (const channel of channels) channel?.close();
|
|
this.options.dataState?.(false);
|
|
}
|
|
|
|
private syncDeadline(): void {
|
|
if (!this.established || this.closed) return;
|
|
if (!this.rooms.size || this.roomDataFailed || this.channel?.readyState === "open") {
|
|
this.clearDeadline();
|
|
} else this.deadline ??= setTimeout(() => this.failRoomData(), TRANSPORT_PREPARE_MS);
|
|
}
|
|
|
|
setOutgoing(streams: ReadonlyMap<string, OutgoingMedia>, resourcesReleased = false): boolean {
|
|
if (this.closed) return false;
|
|
if (resourcesReleased || [...this.sending].some(([id, output]) => !output.failed && !streams.has(id)) || [...streams].some(([id, { stream }]) => {
|
|
const output = this.sending.get(id);
|
|
if (output?.failed) return false;
|
|
const previous = output?.rejectedTracks ?? output?.stream.getTracks();
|
|
const tracks = stream?.getTracks() ?? [];
|
|
return !previous || previous.length !== tracks.length || tracks.some(track => !previous.includes(track));
|
|
})) this.retryOffer();
|
|
let released = false;
|
|
for (const [id, previous] of this.sending) {
|
|
if (streams.has(id)) continue;
|
|
released ||= previous.senders.length > 0;
|
|
for (const sender of previous.senders) this.removeSender(sender);
|
|
for (const track of previous.stream.getTracks()) previous.stream.removeTrack(track);
|
|
this.sending.delete(id);
|
|
}
|
|
for (const [id, { kind, stream, profile, audioBitrate, codec, voiceBitrate, paused = false }] of streams) {
|
|
let output = this.sending.get(id);
|
|
if (!output) {
|
|
output = { kind, stream: new MediaStream(), senders: [], failed: false, paused };
|
|
this.sending.set(id, output);
|
|
}
|
|
if (output.failed) continue;
|
|
if (profile && (!output.profile || !videoQualitySettingsEqual(output.profile, profile))) {
|
|
output.profile = profile;
|
|
const owner = output;
|
|
for (const sender of owner.senders) if (sender.source.kind === "video") {
|
|
this.enqueue(() => this.applyVideoProfile(id, owner, sender, profile));
|
|
}
|
|
}
|
|
if (output.paused !== paused) {
|
|
output.paused = paused;
|
|
const owner = output;
|
|
for (const sender of owner.senders) {
|
|
if (sender.sender.track && sender.sender.track !== sender.source) sender.sender.track.enabled = !paused;
|
|
sender.encoded?.setPaused(paused);
|
|
sender.pooled?.setPaused(paused);
|
|
this.enqueue(() => this.applyPaused(id, owner, sender));
|
|
}
|
|
}
|
|
const bitrate = kind === "voice" ? voiceBitrate ?? DEFAULT_ROOM_VOICE_BITRATE : screenAudioBitrate(audioBitrate);
|
|
if (output.audioBitrate !== bitrate) {
|
|
output.audioBitrate = bitrate;
|
|
const owner = output;
|
|
for (const sender of owner.senders) if (sender.source.kind === "audio") {
|
|
this.enqueue(() => this.applyAudioBitrate(id, owner, sender));
|
|
}
|
|
}
|
|
const tracks = stream?.getTracks() ?? [];
|
|
if (output.replacement || output.senders.some(sender => tracks.some(track =>
|
|
track.kind === sender.source.kind && track !== sender.source))) {
|
|
if (output.replacement?.length === tracks.length && tracks.every(track => output.replacement!.includes(track))) continue;
|
|
const owner = output;
|
|
owner.replacement = tracks;
|
|
this.enqueue(async () => {
|
|
if (this.sending.get(id) !== owner || owner.replacement !== tracks || owner.failed) return;
|
|
try {
|
|
const retained: SenderBinding[] = [];
|
|
for (const track of tracks) {
|
|
const sender = owner.senders.find(sender => sender.source.kind === track.kind && !retained.includes(sender));
|
|
if (sender) {
|
|
if (sender.source !== track) await this.replaceSource(id, owner, sender, track);
|
|
if (this.closed || this.sending.get(id) !== owner) return;
|
|
retained.push(sender);
|
|
} else {
|
|
owner.stream.addTrack(track);
|
|
const added = this.addSender(id, owner, track, codec);
|
|
retained.push(added);
|
|
owner.senders.push(added);
|
|
if (track.kind === "audio") {
|
|
await this.applyAudioBitrate(id, owner, added);
|
|
}
|
|
if (owner.paused) await this.applyPaused(id, owner, added);
|
|
}
|
|
}
|
|
for (const sender of owner.senders) if (!retained.includes(sender)) this.removeSender(sender);
|
|
owner.senders = retained;
|
|
for (const previous of owner.stream.getTracks()) if (!tracks.includes(previous)) owner.stream.removeTrack(previous);
|
|
for (const track of tracks) if (!owner.stream.getTracks().includes(track)) owner.stream.addTrack(track);
|
|
} catch (error) {
|
|
if (!this.closed && this.sending.get(id) === owner && owner.replacement === tracks) {
|
|
this.failOutput(id, owner);
|
|
console.warn("Channel source replacement failed", { flowId: id }, error);
|
|
}
|
|
} finally { if (owner.replacement === tracks) owner.replacement = undefined; }
|
|
});
|
|
continue;
|
|
}
|
|
if (output.rejectedTracks?.length === tracks.length && tracks.every(track => output.rejectedTracks!.includes(track))) continue;
|
|
output.rejectedTracks = undefined;
|
|
const previous = [...output.senders];
|
|
let attached = true;
|
|
for (const track of tracks) {
|
|
if (!output.senders.some(sender => sender.source === track)) {
|
|
try {
|
|
output.stream.addTrack(track);
|
|
const video = kind === "screen" && track.kind === "video";
|
|
const sender = this.addSender(id, output, track, codec);
|
|
output.senders.push(sender);
|
|
if (track.kind === "audio") {
|
|
const owner = output;
|
|
this.enqueue(() => this.applyAudioBitrate(id, owner, sender));
|
|
}
|
|
if (video && profile) {
|
|
const owner = output;
|
|
this.enqueue(() => this.applyVideoProfile(id, owner, sender, profile));
|
|
}
|
|
if (output.paused) {
|
|
const owner = output;
|
|
this.enqueue(() => this.applyPaused(id, owner, sender));
|
|
}
|
|
} catch (error) {
|
|
attached = false;
|
|
const live = previous.filter(sender => sender.sender.track?.readyState === "live");
|
|
const retained = live.some(sender => sender.source.kind === (kind === "screen" ? "video" : "audio")) ? live : [];
|
|
for (const sender of output.senders) if (!retained.includes(sender)) this.removeSender(sender);
|
|
output.senders = retained;
|
|
for (const track of output.stream.getTracks()) {
|
|
if (!retained.some(sender => sender.source === track)) output.stream.removeTrack(track);
|
|
}
|
|
console.warn("Channel media sender attach failed", { flowId: id, kind: track.kind }, error);
|
|
output.rejectedTracks = [...tracks];
|
|
output.failed = retained.length === 0;
|
|
if (output.failed) this.options.outgoingFailed?.(id);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (attached) output.senders = output.senders.filter(sender => {
|
|
if (tracks.includes(sender.source)) return true;
|
|
output.stream.removeTrack(sender.source);
|
|
this.removeSender(sender);
|
|
released = true;
|
|
return false;
|
|
});
|
|
}
|
|
return released;
|
|
}
|
|
|
|
private ownsSender(id: string, owner: OutgoingBinding, binding: SenderBinding): boolean {
|
|
return !this.closed && this.sending.get(id) === owner && !owner.failed && owner.senders.includes(binding);
|
|
}
|
|
|
|
private failOutput(id: string, owner: OutgoingBinding): void {
|
|
if (this.closed || this.sending.get(id) !== owner || owner.failed) return;
|
|
owner.failed = true;
|
|
for (const binding of owner.senders) this.removeSender(binding);
|
|
owner.senders = [];
|
|
for (const track of owner.stream.getTracks()) owner.stream.removeTrack(track);
|
|
this.options.outgoingFailed?.(id);
|
|
}
|
|
|
|
private cloneVideo(id: string, owner: OutgoingBinding, source: MediaStreamTrack): MediaStreamTrack {
|
|
const hint = screenContentHint(owner.profile?.degradationPreference, owner.profile?.displaySource);
|
|
const track = cloneSenderVideoTrack(source, hint, ended => {
|
|
if (owner.senders.some(binding => binding.sender.track === ended)) this.failOutput(id, owner);
|
|
});
|
|
track.enabled = !owner.paused;
|
|
return track;
|
|
}
|
|
|
|
private commitSenderTrack(sender: RTCRtpSender): void {
|
|
if (this.stableSenders?.has(sender)) this.stableSenders.set(sender, sender.track);
|
|
if (this.pendingOffer?.senders.has(sender)) this.pendingOffer.senders.set(sender, sender.track);
|
|
}
|
|
|
|
private async replaceSource(id: string, owner: OutgoingBinding, binding: SenderBinding, source: MediaStreamTrack): Promise<void> {
|
|
const previous = binding.sender.track;
|
|
const isolated = !!binding.encoded || previous !== binding.source;
|
|
const next = isolated ? this.cloneVideo(id, owner, source) : source;
|
|
let retained = false;
|
|
try {
|
|
await waitForConnectionOperation(binding.lifetime.signal, () => binding.sender.replaceTrack(next));
|
|
if (!this.ownsSender(id, owner, binding)) return;
|
|
const pooled = binding.pooled;
|
|
binding.pooled = undefined;
|
|
binding.source = source;
|
|
retained = true;
|
|
pooled?.dispose();
|
|
if (isolated && next.readyState === "ended") throw new Error("Replacement video track ended");
|
|
this.attachPool(id, owner, binding);
|
|
if (source.kind === "video" && owner.profile) await this.applyVideoProfile(id, owner, binding, owner.profile);
|
|
if (!this.ownsSender(id, owner, binding)) return;
|
|
this.commitSenderTrack(binding.sender);
|
|
} finally {
|
|
if (isolated) {
|
|
if (!retained) stopSenderVideoTrack(next);
|
|
if ((retained || !this.ownsSender(id, owner, binding)) && previous !== next && previous !== binding.encoded?.track) stopSenderVideoTrack(previous);
|
|
}
|
|
}
|
|
}
|
|
|
|
private async applyAudioBitrate(id: string, owner: OutgoingBinding, binding: SenderBinding): Promise<void> {
|
|
if (!this.ownsSender(id, owner, binding)) return;
|
|
const sender = binding.sender;
|
|
const parameters = sender.getParameters();
|
|
if (!parameters.encodings.length) return;
|
|
try { await waitForConnectionOperation(binding.lifetime.signal, () => configureAudioSender(sender, owner.audioBitrate!)); }
|
|
catch (error) { if (this.ownsSender(id, owner, binding)) console.warn("Channel audio bitrate could not be applied", { flowId: id }, error); }
|
|
}
|
|
|
|
private addSender(id: string, owner: OutgoingBinding, source: MediaStreamTrack, codec?: BrowserVideoCodecPreference, allowPool = true): SenderBinding {
|
|
const key = JSON.stringify([owner.kind, codec?.primary ?? null, codec?.vp8Fallback ?? false,
|
|
owner.kind === "screen" ? this.options.canReceiveVideoLayers ? ["q", "h"] : [null] : []]);
|
|
if (!owner.slots || !owner.senders.length && owner.slots.key !== key) {
|
|
const slots = [...this.mediaSlots].find(slots => slots.key === key
|
|
&& ![...this.sending.values()].some(output => output.slots === slots))
|
|
?? { key, stream: new MediaStream(), senders: [] };
|
|
this.mediaSlots.add(slots);
|
|
const tracks = owner.stream.getTracks();
|
|
for (const track of tracks) owner.stream.removeTrack(track);
|
|
owner.slots = slots;
|
|
owner.stream = slots.stream;
|
|
for (const track of tracks) owner.stream.addTrack(track);
|
|
}
|
|
const slots = owner.slots;
|
|
let slot = slots.senders.find(slot => slot.transceiver.receiver.track.kind === source.kind
|
|
&& slot.transceiver.direction !== "stopped" && !slot.sender.track
|
|
&& ![...this.sending.values()].some(output => output.senders.some(binding => binding.sender === slot.sender)));
|
|
const reused = !!slot;
|
|
const video = owner.kind === "screen" && source.kind === "video";
|
|
const profile = owner.profile;
|
|
const layered = this.options.canReceiveVideoLayers, count = layered ? 2 : 1;
|
|
const pooled = video && allowPool && !layered && profile && this.options.videoPool && supportsBrowserEncoding();
|
|
const track = video && !reused ? this.cloneVideo(id, owner, source) : source;
|
|
const encodings = video && profile ? screenShareEncodings(profile, source.getSettings(), count, count)
|
|
: layered ? [{ rid: "q", scaleResolutionDownBy: 2 }, { rid: "h", scaleResolutionDownBy: 1 }] : [{}];
|
|
if (!layered) delete encodings[0].rid;
|
|
let binding: SenderBinding | undefined;
|
|
try {
|
|
const transceiver = slot?.transceiver ?? this.pc.addTransceiver(track, { direction: "sendrecv", streams: [owner.stream],
|
|
...(video ? { sendEncodings: encodings } : {}) });
|
|
if (!slot) { slot = { sender: transceiver.sender, transceiver }; slots.senders.push(slot); }
|
|
binding = { sender: transceiver.sender, source, lifetime: new AbortController() };
|
|
if (video && codec && !applyVideoCodecPreference(transceiver, codec)) throw new Error("video_codec_unsupported");
|
|
if (pooled && slot.encoded === undefined) {
|
|
// Attach synchronously while this sender's encoded stream is still claimable.
|
|
// The physical PC also carries ordinary audio, receive tracks and Native flows.
|
|
try {
|
|
const sender = binding.sender;
|
|
const current = () => [...this.sending].find(([, output]) => output.senders.some(binding => binding.sender === sender));
|
|
slot.encoded = new BrowserEncodingOutput(sender, () => {
|
|
this.mediaSlots.delete(slots);
|
|
const flow = current();
|
|
if (flow) this.failOutput(flow[0], flow[1]);
|
|
}, { get flowId() { return current()?.[0]; } });
|
|
} catch (error) {
|
|
// A refused claim may already have intercepted this sender. Recreate
|
|
// only this optional optimization's new sender with ordinary media.
|
|
this.removeSender(binding);
|
|
binding = undefined;
|
|
slots.senders.splice(slots.senders.indexOf(slot), 1);
|
|
if (!reused) transceiver.stop();
|
|
console.warn("Channel encoding pool unavailable for sender", { flowId: id }, error);
|
|
return this.addSender(id, owner, source, codec, false);
|
|
}
|
|
} else if (!allowPool && slot.encoded === undefined) slot.encoded = null;
|
|
binding.encoded = slot.encoded ?? undefined;
|
|
if (reused) {
|
|
const active = binding;
|
|
transceiver.direction = "sendrecv";
|
|
active.sender.setStreams(owner.stream);
|
|
this.enqueue(async () => {
|
|
if (!this.ownsSender(id, owner, active)) return;
|
|
const track = video ? this.cloneVideo(id, owner, source) : source;
|
|
try {
|
|
await waitForConnectionOperation(active.lifetime.signal, () => active.sender.replaceTrack(track));
|
|
if (!this.ownsSender(id, owner, active)) return;
|
|
if (video && track.readyState === "ended") throw new Error("Replacement video track ended");
|
|
this.commitSenderTrack(active.sender);
|
|
this.attachPool(id, owner, active);
|
|
active.encoded?.setPaused(owner.paused);
|
|
if (video && profile) await this.applyVideoProfile(id, owner, active, profile);
|
|
await this.applyPaused(id, owner, active);
|
|
// Lease replacement may restore the old direction before the
|
|
// Browser raises negotiationneeded; its manifest still changed.
|
|
this.offer();
|
|
} catch (error) {
|
|
if (this.ownsSender(id, owner, active)) {
|
|
owner.rejectedTracks = owner.stream.getTracks();
|
|
const retained = owner.senders.filter(binding => binding !== active && binding.sender.track?.readyState === "live");
|
|
if (retained.some(binding => binding.source.kind === (owner.kind === "screen" ? "video" : "audio"))) {
|
|
for (const binding of owner.senders) if (!retained.includes(binding)) this.removeSender(binding);
|
|
owner.senders = retained;
|
|
for (const track of owner.stream.getTracks()) if (!retained.some(binding => binding.source === track)) owner.stream.removeTrack(track);
|
|
} else this.failOutput(id, owner);
|
|
console.warn("Channel media slot could not be rebound", { flowId: id }, error);
|
|
}
|
|
} finally {
|
|
if (video && !this.ownsSender(id, owner, active)) stopSenderVideoTrack(track);
|
|
}
|
|
});
|
|
} else if (binding.encoded) {
|
|
binding.encoded.setPaused(owner.paused);
|
|
this.attachPool(id, owner, binding);
|
|
}
|
|
return binding;
|
|
} catch (error) {
|
|
if (binding) this.removeSender(binding);
|
|
else if (video && !reused) stopSenderVideoTrack(track);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/** Pool handoffs use the same queue as SDP, without turning a failed handoff into a pair failure. */
|
|
private senderOperation<T>(operation: () => Promise<T>): Promise<T> {
|
|
const work = this.operations.then(operation);
|
|
this.operations = work.then(() => {}, () => {});
|
|
return work;
|
|
}
|
|
|
|
private attachPool(id: string, owner: OutgoingBinding, binding: SenderBinding): void {
|
|
const encoded = binding.encoded, pool = this.options.videoPool;
|
|
if (!encoded || !pool || !owner.profile) return;
|
|
const source = binding.source, sender = binding.sender;
|
|
let member: BrowserPooledSender | null = null;
|
|
const owns = () => this.ownsSender(id, owner, binding) && binding.source === source && binding.pooled === member;
|
|
const requestKey = () => this.senderOperation(async () => {
|
|
if (!owns()) return;
|
|
const request = sender.setParameters as (parameters: RTCRtpSendParameters,
|
|
options: { encodingOptions: Array<{ keyFrame: boolean }> }) => Promise<void>;
|
|
await waitForConnectionOperation(binding.lifetime.signal,
|
|
() => request.call(sender, sender.getParameters(), { encodingOptions: [{ keyFrame: true }] }));
|
|
});
|
|
member = pool.create(source, sender, this.pc, owner.profile, encoded,
|
|
() => this.senderOperation(async () => {
|
|
if (!owns()) return false;
|
|
const previous = sender.track!;
|
|
const next = member?.carrierScale() !== undefined ? encoded.track
|
|
: previous === encoded.track ? this.cloneVideo(id, owner, source) : previous;
|
|
try {
|
|
next.enabled = !owner.paused;
|
|
if (next !== encoded.track) await waitForConnectionOperation(binding.lifetime.signal, () => applyVideoCaptureProfile(next, owner.profile!));
|
|
if (!owns()) return false;
|
|
if (next !== previous) await waitForConnectionOperation(binding.lifetime.signal, () => sender.replaceTrack(next));
|
|
const applied = await waitForConnectionOperation(binding.lifetime.signal, () => configureVideoSender(sender, owner.profile!, member?.carrierScale()));
|
|
if (applied.mismatches.length) throw new Error("capture_quality_unsupported");
|
|
if (!owns()) return false;
|
|
if (next.readyState === "ended") throw new Error("Replacement video track ended");
|
|
this.commitSenderTrack(sender);
|
|
if (previous !== next && previous !== encoded.track) stopSenderVideoTrack(previous);
|
|
owner.metrics = undefined;
|
|
return true;
|
|
} catch (error) {
|
|
if (owns() && sender.track !== previous) {
|
|
try { await waitForConnectionOperation(binding.lifetime.signal, () => sender.replaceTrack(previous)); }
|
|
catch { if (owns()) this.failOutput(id, owner); }
|
|
}
|
|
if (owns()) console.warn("Channel pooled carrier could not be applied", { flowId: id }, error);
|
|
return false;
|
|
} finally {
|
|
if (next !== encoded.track && next !== previous && (!owns() || sender.track !== next)) stopSenderVideoTrack(next);
|
|
if (!owns() && previous !== encoded.track) stopSenderVideoTrack(previous);
|
|
}
|
|
}), requestKey, () => { if (owns()) this.failOutput(id, owner); });
|
|
binding.pooled = member;
|
|
if (!member) encoded.passthrough(requestKey);
|
|
member?.setPaused(owner.paused);
|
|
}
|
|
|
|
private async applyPaused(id: string, owner: OutgoingBinding, binding: SenderBinding): Promise<void> {
|
|
if (!this.ownsSender(id, owner, binding)) return;
|
|
const sender = binding.sender;
|
|
const paused = owner.paused;
|
|
binding.encoded?.setPaused(paused);
|
|
binding.pooled?.setPaused(paused);
|
|
if (sender.track && sender.track !== binding.source) sender.track.enabled = !paused;
|
|
try {
|
|
const parameters = sender.getParameters();
|
|
if (!parameters.encodings.length) return;
|
|
let active = this.pausedEncodings.get(sender);
|
|
if (paused && !active) {
|
|
active = parameters.encodings.map(encoding => encoding.active ?? true);
|
|
this.pausedEncodings.set(sender, active);
|
|
}
|
|
if (!active) return;
|
|
parameters.encodings.forEach((encoding, index) => { encoding.active = !paused && (active![index] ?? true); });
|
|
await waitForConnectionOperation(binding.lifetime.signal, () => sender.setParameters(parameters));
|
|
if (!paused) this.pausedEncodings.delete(sender);
|
|
} catch (error) { if (this.ownsSender(id, owner, binding)) console.warn("Channel sender pause could not be applied", { flowId: id }, error); }
|
|
}
|
|
|
|
private async applyVideoProfile(id: string, owner: OutgoingBinding, binding: SenderBinding, profile: VideoSenderProfile): Promise<void> {
|
|
const current = () => this.ownsSender(id, owner, binding);
|
|
if (!current()) return;
|
|
const sender = binding.sender;
|
|
if (!sender.getParameters().encodings.length) return;
|
|
try {
|
|
// Automatic projection owns no applied-setting acknowledgement. Pool
|
|
// completion may need a carrier handoff on this same sender queue.
|
|
void binding.pooled?.updateProfile(profile).catch(error => {
|
|
if (current()) console.warn("Channel source quality parameters could not be applied", { flowId: id }, error);
|
|
});
|
|
const applied = await waitForConnectionOperation(binding.lifetime.signal,
|
|
() => configureVideoSender(sender, profile, binding.pooled?.carrierScale()));
|
|
if (current() && applied.mismatches.length) {
|
|
console.warn("Channel source quality parameters were not fully applied", { flowId: id, parameters: applied.mismatches });
|
|
}
|
|
} catch (error) {
|
|
if (current()) console.warn("Channel source quality parameters could not be applied", { flowId: id }, error);
|
|
}
|
|
}
|
|
|
|
/** An explicit source edit shares the negotiation queue, but cannot fail the pair. */
|
|
async configureVideo(id: string, settings: VideoSenderProfile): Promise<void> {
|
|
const owner = this.sending.get(id);
|
|
const profile = { ...settings, displaySource: owner?.profile?.displaySource };
|
|
const bindings = await this.senderOperation(async () => {
|
|
if (this.closed || !owner || this.sending.get(id) !== owner || owner.failed) return [];
|
|
return owner.senders.filter(binding => binding.source.kind === "video" && binding.sender.getParameters().encodings.length);
|
|
});
|
|
// A producer replacement commits through configureCarrier on the sender
|
|
// queue. Await its acceptance outside that queue, then apply transport caps.
|
|
await Promise.all(bindings.map(binding => binding.pooled?.updateProfile(profile)));
|
|
return this.senderOperation(async () => {
|
|
if (this.closed || !owner || this.sending.get(id) !== owner || owner.failed) return;
|
|
for (const binding of bindings) if (this.ownsSender(id, owner, binding)) {
|
|
const applied = await waitForConnectionOperation(binding.lifetime.signal,
|
|
() => configureVideoSender(binding.sender, profile, binding.pooled?.carrierScale()));
|
|
if (applied.mismatches.length) throw new Error("capture_quality_unsupported");
|
|
}
|
|
if (this.sending.get(id) === owner) owner.profile = profile;
|
|
});
|
|
}
|
|
|
|
configureAudio(id: string, bitrate: number): Promise<void> {
|
|
const owner = this.sending.get(id);
|
|
return this.senderOperation(async () => {
|
|
if (this.closed || !owner || this.sending.get(id) !== owner || owner.failed) return;
|
|
for (const binding of owner.senders) if (binding.source.kind === "audio" && binding.sender.getParameters().encodings.length) {
|
|
await waitForConnectionOperation(binding.lifetime.signal,
|
|
() => configureAudioSender(binding.sender, bitrate));
|
|
}
|
|
if (this.sending.get(id) === owner) owner.audioBitrate = bitrate;
|
|
});
|
|
}
|
|
|
|
private removeSender(binding: SenderBinding): void {
|
|
const { sender, encoded, pooled } = binding;
|
|
binding.lifetime.abort();
|
|
encoded?.setPaused(true);
|
|
binding.pooled = undefined;
|
|
pooled?.dispose();
|
|
encoded?.passthrough(async () => {});
|
|
if (sender.track !== binding.source && sender.track !== encoded?.track) stopSenderVideoTrack(sender.track);
|
|
this.stableSenders?.delete(sender);
|
|
this.pendingOffer?.senders.delete(sender);
|
|
if (!this.closed) this.pc.removeTrack(sender);
|
|
}
|
|
|
|
private retryOffer(): void {
|
|
if (!this.localOfferFailed && ![...this.sending.values()].some(output => !output.failed && output.rejectedTracks)) return;
|
|
this.localOfferFailed = false;
|
|
for (const output of this.sending.values()) output.rejectedTracks = undefined;
|
|
this.offer();
|
|
}
|
|
|
|
private offer(): void {
|
|
this.enqueue(async () => {
|
|
if (this.localOfferFailed || this.pc.signalingState !== "stable" || this.pendingOffer) return;
|
|
const senders = new Map([...this.sending.values()].flatMap(output => output.senders.map(({ sender }) => [sender, sender.track] as const)));
|
|
const tracks = this.outgoingTracks();
|
|
const channel = this.channel, bulk = this.bulk;
|
|
try {
|
|
const offer = preferOpusStereoReceive(await this.pc.createOffer());
|
|
await this.pc.setLocalDescription({ ...offer, sdp: normalizeVideoOfferSdp(offer.sdp!) });
|
|
} catch (error) {
|
|
if (!this.stableSenders || this.pendingOffer || this.pc.signalingState !== "stable"
|
|
|| this.closed || ["failed", "closed"].includes(this.pc.connectionState)) throw error;
|
|
this.localOfferFailed = true;
|
|
const reverted: string[] = [], failed: string[] = [];
|
|
for (const [id, output] of this.sending) {
|
|
const rejected = output.senders.filter(({ sender }) => senders.has(sender) && senders.get(sender) === sender.track
|
|
&& this.stableSenders!.get(sender) !== sender.track);
|
|
if (!rejected.length) continue;
|
|
reverted.push(id);
|
|
output.rejectedTracks = output.stream.getTracks();
|
|
for (const sender of rejected) {
|
|
output.stream.removeTrack(sender.source);
|
|
this.removeSender(sender);
|
|
}
|
|
output.senders = output.senders.filter(sender => !rejected.includes(sender));
|
|
if (!output.senders.some(({ sender }) => sender.track?.readyState === "live" && sender.track.kind === (output.kind === "screen" ? "video" : "audio"))) {
|
|
output.failed = true;
|
|
failed.push(id);
|
|
}
|
|
}
|
|
console.warn("Channel local offer failed", { flows: reverted }, error);
|
|
for (const id of failed) this.options.outgoingFailed?.(id);
|
|
if ((this.channel && this.channel !== channel) || (this.bulk && this.bulk !== bulk)
|
|
|| [...this.sending.values()].some(output => output.senders.some(({ sender }) => !senders.has(sender) || senders.get(sender) !== sender.track))) this.retryOffer();
|
|
return;
|
|
}
|
|
if (!this.closed) this.sendDescription(senders, tracks);
|
|
});
|
|
}
|
|
|
|
private confirmSenders(senders: ReadonlyMap<RTCRtpSender, MediaStreamTrack | null>): void {
|
|
this.stableSenders = new Map(this.pc.getTransceivers()
|
|
.filter(transceiver => ["sendonly", "sendrecv"].includes(transceiver.currentDirection ?? "")
|
|
&& senders.has(transceiver.sender) && senders.get(transceiver.sender) === transceiver.sender.track)
|
|
.map(transceiver => [transceiver.sender, transceiver.sender.track]));
|
|
for (const [id, owner] of this.sending) for (const sender of owner.senders) if (this.stableSenders.has(sender.sender)) {
|
|
if (sender.source.kind === "audio") this.enqueue(() => this.applyAudioBitrate(id, owner, sender));
|
|
if (sender.source.kind === "video" && owner.profile) this.enqueue(() => this.applyVideoProfile(id, owner, sender, owner.profile!));
|
|
this.enqueue(() => this.applyPaused(id, owner, sender));
|
|
}
|
|
}
|
|
|
|
hasRemoteStream(flowId: string, stream: MediaStream): boolean {
|
|
return !this.applyingRemote && this.remoteIds.get(stream.id) === flowId && this.receiving.get(stream.id) === stream;
|
|
}
|
|
|
|
frameBaseline(flowId: string): ReadonlyMap<string, number | null> | null {
|
|
return this.frameBaselines.get(flowId) ?? null;
|
|
}
|
|
|
|
signal(payload: ChannelSignal): void {
|
|
this.enqueue(async () => {
|
|
if (payload.resync) { this.resendOffer(); return; }
|
|
if (payload.description) {
|
|
const description = payload.description;
|
|
if (description.type === "answer" && (!this.pendingOffer || payload.exchangeId !== this.pendingOffer.payload.exchangeId)) return;
|
|
if (description.type === "offer" && this.answer && payload.exchangeId === this.answer.exchangeId) { this.replayDescription(this.answer); return; }
|
|
const collision = description.type === "offer" && this.pc.signalingState !== "stable";
|
|
this.ignoreOffer = !this.options.polite && collision;
|
|
if (this.ignoreOffer) return;
|
|
const previousIds = this.remoteIds;
|
|
const offered = this.pendingOffer;
|
|
const remoteIds = new Map((payload.tracks ?? []).map(track => [track.streamId, track.flowId]));
|
|
const flows = new Set(remoteIds.values());
|
|
const added = [...flows].filter(id => !this.frameBaselines.has(id));
|
|
if (added.length) {
|
|
let baseline: ReadonlyMap<string, number | null> | null = null;
|
|
try {
|
|
const samples = decodedVideoFrameSamples(await this.pc.getStats());
|
|
baseline = new Map(samples.map(({ id, framesDecoded }) => [id, framesDecoded]));
|
|
} catch { /* An unknown baseline requires observed progress, not transport failure. */ }
|
|
if (this.closed) return;
|
|
for (const id of added) this.frameBaselines.set(id, baseline);
|
|
}
|
|
this.remoteIds = remoteIds;
|
|
// SRD can remove a stream's old video before adding its replacement.
|
|
this.applyingRemote = true;
|
|
try { await this.pc.setRemoteDescription(description); }
|
|
finally { this.applyingRemote = false; }
|
|
if (this.closed) return;
|
|
if (description.type === "answer") this.confirmSenders(offered!.senders);
|
|
if (description.type === "offer" || description.type === "answer") this.pendingOffer = null;
|
|
for (const [streamId, stream] of this.receiving) {
|
|
const flowId = this.remoteIds.get(streamId);
|
|
if (flowId) this.options.receive(flowId, stream);
|
|
else if (!stream.getTracks().some(track => this.pc.getReceivers().some(receiver => receiver.track === track))) this.receiving.delete(streamId);
|
|
}
|
|
for (const id of this.frameBaselines.keys()) if (!flows.has(id)) this.frameBaselines.delete(id);
|
|
for (const id of new Set(previousIds.values())) {
|
|
if (!flows.has(id)) this.options.retire?.(id);
|
|
}
|
|
for (const candidate of this.candidates.splice(0)) await this.addCandidate(candidate);
|
|
if (description.type === "offer") {
|
|
const senders = new Map([...this.sending.values()].flatMap(output => output.senders.map(({ sender }) => [sender, sender.track] as const)));
|
|
const tracks = this.outgoingTracks();
|
|
await this.pc.setLocalDescription(preferOpusStereoReceive(await this.pc.createAnswer()));
|
|
if (!this.closed) { this.confirmSenders(senders); this.sendDescription(senders, tracks, payload.exchangeId!); }
|
|
this.offerChangedTracks(tracks);
|
|
} else this.offerChangedTracks(offered!.payload.tracks!);
|
|
} else if (payload.candidate && !this.ignoreOffer) {
|
|
if (this.pc.remoteDescription) await this.addCandidate(payload.candidate);
|
|
else if (this.candidates.length < 128) this.candidates.push(payload.candidate);
|
|
}
|
|
});
|
|
}
|
|
|
|
private async addCandidate(candidate: NonNullable<ChannelSignal["candidate"]>): Promise<void> {
|
|
if (this.closed || candidate.origin === "linear" && !this.linearCandidates?.admit(this.pc, candidate)) return;
|
|
const { origin, ...ice } = candidate;
|
|
await addRemoteIceCandidate(this.pc, { ...ice, candidate: ice.candidate ?? "" }, origin);
|
|
}
|
|
|
|
resync(): void {
|
|
this.enqueue(async () => {
|
|
if (this.roomDataFailed) this.options.roomData(false);
|
|
else this.reportRoomDataReady();
|
|
for (const [id, output] of this.sending) if (output.failed) this.options.outgoingFailed?.(id);
|
|
this.options.send({ resync: true });
|
|
this.resendOffer();
|
|
});
|
|
}
|
|
|
|
private resendOffer(): void {
|
|
if (this.pendingOffer) this.replayDescription(this.pendingOffer.payload);
|
|
}
|
|
|
|
private replayDescription(payload: ChannelSignal): void {
|
|
// SDP completes its original exchange; only current flow bindings may travel with it.
|
|
this.options.send({ ...payload, tracks: payload.tracks?.filter(track => {
|
|
const output = this.sending.get(track.flowId);
|
|
return output && !output.failed && output.stream.id === track.streamId;
|
|
}) });
|
|
}
|
|
|
|
private outgoingTracks(): NonNullable<ChannelSignal["tracks"]> {
|
|
return [...this.sending].filter(([, output]) => !output.failed)
|
|
.map(([flowId, { stream }]) => ({ flowId, streamId: stream.id }));
|
|
}
|
|
|
|
private offerChangedTracks(tracks: NonNullable<ChannelSignal["tracks"]>): void {
|
|
const current = this.outgoingTracks();
|
|
if (current.length !== tracks.length || current.some(track => !tracks.some(previous =>
|
|
previous.flowId === track.flowId && previous.streamId === track.streamId))) this.offer();
|
|
}
|
|
|
|
private sendDescription(senders: Map<RTCRtpSender, MediaStreamTrack | null>, tracks: NonNullable<ChannelSignal["tracks"]>, exchangeId: string = crypto.randomUUID()): void {
|
|
const description = this.pc.localDescription;
|
|
if (!description || (description.type !== "offer" && description.type !== "answer")) return;
|
|
const payload = { exchangeId, description: { type: description.type, sdp: description.sdp }, tracks };
|
|
if (description.type === "offer") this.pendingOffer = { payload, senders };
|
|
else if (description.type === "answer") this.answer = payload;
|
|
this.replayDescription(payload);
|
|
}
|
|
|
|
private enqueue(action: () => Promise<void>): void {
|
|
this.operations = this.operations.then(async () => { if (!this.closed) await action(); })
|
|
.catch(error => { if (!this.closed) { console.warn("Channel media negotiation failed", error); this.fail(); } });
|
|
}
|
|
|
|
private clearDeadline(): void {
|
|
if (this.deadline !== null) clearTimeout(this.deadline);
|
|
this.deadline = null;
|
|
}
|
|
|
|
private fail(): void {
|
|
if (this.closed) return;
|
|
this.failRoomData();
|
|
this.options.failed(this.close());
|
|
}
|
|
|
|
close(): boolean {
|
|
if (this.closed) return false;
|
|
const released = [...this.sending.values()].some(output => output.senders.length > 0);
|
|
this.closed = true;
|
|
this.releaseDebug();
|
|
this.closeData();
|
|
this.options.input?.(null);
|
|
this.clearDeadline();
|
|
this.pc.onicecandidate = null;
|
|
this.pc.ontrack = null;
|
|
this.pc.onnegotiationneeded = null;
|
|
this.pc.oniceconnectionstatechange = null;
|
|
this.pc.onconnectionstatechange = null;
|
|
this.pc.ondatachannel = null;
|
|
this.input?.close();
|
|
for (const owner of this.sending.values()) for (const binding of owner.senders) this.removeSender(binding);
|
|
for (const slots of this.mediaSlots) for (const slot of slots.senders) slot.encoded?.dispose();
|
|
this.mediaSlots.clear();
|
|
// Closing ends received tracks, and a video still showing an ended track paints a black frame.
|
|
// Emptied streams leave their consumers on the last frame, as a withdrawn remote track does.
|
|
for (const stream of this.receiving.values()) for (const track of stream.getTracks()) stream.removeTrack(track);
|
|
this.pc.close();
|
|
this.sending.clear();
|
|
this.receiving.clear();
|
|
this.frameBaselines.clear();
|
|
this.pendingOffer = null;
|
|
this.stableSenders = null;
|
|
this.answer = null;
|
|
return released;
|
|
}
|
|
}
|
|
|
|
/** One bounded writer per SCTP stream; producers own their application work. */
|
|
class ChannelWriter {
|
|
private channel?: RTCDataChannel;
|
|
private closed = false;
|
|
private resume?: ReturnType<typeof setTimeout>;
|
|
private readonly pending = new Set<{
|
|
data: string | Uint8Array; size: number; allowed?: () => boolean; request?: string;
|
|
resolve: () => void; reject: (error: unknown) => void;
|
|
}>();
|
|
readonly maxFrameBytes: number;
|
|
private readonly maxQueuedFrames: number;
|
|
|
|
constructor(private kind: "control" | "chunk") {
|
|
this.maxFrameBytes = data_frame_bytes(kind === "chunk");
|
|
this.maxQueuedFrames = data_queue_frames(kind === "chunk");
|
|
}
|
|
install(channel: RTCDataChannel): void {
|
|
this.channel = channel;
|
|
channel.bufferedAmountLowThreshold = DATA_SEND_BUFFER_BYTES / 2;
|
|
channel.addEventListener("open", this.drain);
|
|
channel.addEventListener("bufferedamountlow", this.drain);
|
|
channel.addEventListener("closing", this.close);
|
|
this.drain();
|
|
}
|
|
send(data: string | Uint8Array, allowed?: () => boolean, request?: string): Promise<void> {
|
|
const size = typeof data === "string" ? new TextEncoder().encode(data).length : data.byteLength;
|
|
if (size > this.maxFrameBytes) return Promise.reject(new Error(this.kind === "control" ? "control_frame_too_large" : "file_frame_too_large"));
|
|
if (this.closed) return Promise.reject(new Error("data_channel_unavailable"));
|
|
if (this.pending.size >= this.maxQueuedFrames) return Promise.reject(new Error("data_channel_busy"));
|
|
return new Promise((resolve, reject) => {
|
|
this.pending.add({ data, size, allowed, request, resolve, reject });
|
|
this.drain();
|
|
});
|
|
}
|
|
cancel(request: string): void {
|
|
for (const frame of this.pending) {
|
|
if (frame.request !== request) continue;
|
|
this.pending.delete(frame);
|
|
frame.resolve();
|
|
}
|
|
this.drain();
|
|
}
|
|
private drain = (): void => {
|
|
const channel = this.channel;
|
|
if (channel?.readyState !== "open" || this.resume !== undefined) return;
|
|
for (const frame of this.pending) {
|
|
if (channel.bufferedAmount + frame.size > DATA_SEND_BUFFER_BYTES) break;
|
|
this.pending.delete(frame);
|
|
try {
|
|
if (frame.allowed && !frame.allowed()) {
|
|
frame.reject(new Error("room_unavailable"));
|
|
continue;
|
|
}
|
|
if (typeof frame.data === "string") channel.send(frame.data);
|
|
else channel.send(frame.data as Uint8Array<ArrayBuffer>);
|
|
frame.resolve();
|
|
} catch (error) {
|
|
frame.reject(error);
|
|
// Application owners retire the failed response in their promise handlers.
|
|
this.resume = setTimeout(() => { this.resume = undefined; this.drain(); }, 0);
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
close = (): void => {
|
|
this.closed = true;
|
|
clearTimeout(this.resume);
|
|
this.channel?.removeEventListener("open", this.drain);
|
|
this.channel?.removeEventListener("bufferedamountlow", this.drain);
|
|
this.channel?.removeEventListener("closing", this.close);
|
|
for (const frame of this.pending) frame.reject(new Error("data_channel_unavailable"));
|
|
this.pending.clear();
|
|
};
|
|
}
|