Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

17.3 KB
Raw Blame
import { CollabIceCandidateType } from '../types/multiplayer';
import {
  ChunkReassembler,
  CollabSendOptions,
  DataChannelSendQueue,
  ICollabTransport,
  encodeTransportFrames,
  isEphemeralWireBinary,
  queryPeerConnectionIceCandidateType,
} from './collabTransport';
import {
  buildIceServers,
  WEBRTC_DATACHANNEL_LABEL,
  WEBRTC_EPHEMERAL_DATACHANNEL_LABEL,
  WebRtcTurnConfig,
} from './webrtcHostHub';
import {
  createSignalingClient,
  ISignalingClient,
  SignalingEnvelope,
} from './webrtcSignaling';

export interface WebRtcGuestClientOptions {
  roomHash: string;
  peerId: string;
  turnConfig?: WebRtcTurnConfig;
  customSignalingUrl?: string;
  signalingClient?: ISignalingClient;
  connectTimeoutMs?: number;
  bufferedAmountLowThreshold?: number;
  ephemeralSoftLimit?: number;
  reliableHighWaterMark?: number;
  reliableQueueHardLimit?: number;
}

/**
 * WebRTC Star-Topology Guest Client.
 *
 * Exchanges SDP offer/answer & ICE via the pluggable zero-cost signaling client,
 * then opens dual RTCDataChannels to the Host Hub:
 *  - `inkstudio-collab`: ordered reliable channel for tiles, masks, layers, strokes, and chat
 *  - `inkstudio-collab-ephemeral`: optional unordered unreliable channel (`maxRetransmits: 0`)
 *    for low-latency cursors and follow-camera updates (falling back seamlessly to reliable
 *    if the remote peer only supports a single channel).
 */
export class WebRtcGuestClient implements ICollabTransport {
  public readonly kind = 'webrtc-guest' as const;

  public readonly roomHash: string;
  private readonly peerId: string;
  private readonly iceServers: RTCIceServer[];
  private readonly connectTimeoutMs: number;
  private readonly signaling: ISignalingClient;
  private readonly sender: DataChannelSendQueue;

  private pc: RTCPeerConnection | null = null;
  private dc: RTCDataChannel | null = null;
  private ephemeralDc: RTCDataChannel | null = null;
  private reassembler = new ChunkReassembler();
  private ephemeralReassembler = new ChunkReassembler({ maxInflight: 16, timeoutMs: 5_000 });
  private remoteDescSet = false;
  private pendingIce: RTCIceCandidateInit[] = [];
  private localIceCandidates: NonNullable<SignalingEnvelope['candidate']>[] = [];
  private hostSignalingPeerId: string | null = null;

  private textHandlers = new Set<(text: string) => void>();
  private binaryHandlers = new Set<(data: ArrayBuffer) => void>();
  private closeHandlers = new Set<(reason?: string) => void>();
  private errorHandlers = new Set<(err: Error | Event) => void>();

  private unsubSignaling: (() => void) | null = null;
  private offerRetryTimer: ReturnType<typeof setInterval> | null = null;
  private signalingCloseTimer: ReturnType<typeof setTimeout> | null = null;
  private disconnectTimer: ReturnType<typeof setTimeout> | null = null;
  private nextMsgId = 1;
  private closed = false;
  private hasNotifiedClose = false;

  constructor(options: WebRtcGuestClientOptions) {
    this.roomHash = options.roomHash;
    this.peerId = options.peerId;
    this.iceServers = buildIceServers(options.turnConfig);
    this.connectTimeoutMs = options.connectTimeoutMs ?? 20_000;
    this.sender = new DataChannelSendQueue({
      bufferedAmountLowThreshold: options.bufferedAmountLowThreshold,
      ephemeralSoftLimit: options.ephemeralSoftLimit,
      reliableHighWaterMark: options.reliableHighWaterMark,
      reliableQueueHardLimit: options.reliableQueueHardLimit,
      onOverflow: (reason) => {
        this.emitCloseOnce(reason);
      },
    });
    this.signaling =
      options.signalingClient ??
      createSignalingClient({
        roomHash: options.roomHash,
        peerId: options.peerId,
        customSignalingUrl: options.customSignalingUrl,
      });
  }

  private allocMessageId = (): number => {
    this.nextMsgId = (this.nextMsgId + 1) >>> 0;
    if (this.nextMsgId === 0) this.nextMsgId = 1;
    return this.nextMsgId;
  };

  public async connect(): Promise<void> {
    if (this.closed) {
      throw new Error('WebRTC Guest Client already closed');
    }
    if (typeof RTCPeerConnection === 'undefined') {
      throw new Error('WebRTC is not supported in this browser/WebView');
    }

    await this.signaling.connect();

    return new Promise<void>((resolve, reject) => {
      let settled = false;

      const clearTimers = () => {
        if (timeoutId !== null) {
          clearTimeout(timeoutId);
          timeoutId = null;
        }
        if (this.offerRetryTimer !== null) {
          clearInterval(this.offerRetryTimer);
          this.offerRetryTimer = null;
        }
      };

      const settleResolve = () => {
        if (settled) return;
        settled = true;
        clearTimers();
        // Close signaling shortly after DataChannel opens (zero ongoing signaling traffic)
        this.signalingCloseTimer = setTimeout(() => {
          this.unsubSignaling?.();
          this.unsubSignaling = null;
          this.signaling.close();
        }, 3_000);
        resolve();
      };

      const settleReject = (err: Error) => {
        if (settled) return;
        settled = true;
        clearTimers();
        this.closeInternal(false);
        reject(err);
      };

      let timeoutId: ReturnType<typeof setTimeout> | null = setTimeout(() => {
        settleReject(
          new Error(
            'Could not reach the room host. Verify the Room Code/Secret and ensure the host tablet is online.'
          )
        );
      }, this.connectTimeoutMs);

      const pc = new RTCPeerConnection({ iceServers: this.iceServers });
      this.pc = pc;

      const wireOnMessage = (reassembler: ChunkReassembler) => async (msgEv: MessageEvent) => {
        if (this.closed) return;
        try {
          const rawBuf =
            msgEv.data instanceof ArrayBuffer
              ? msgEv.data
              : msgEv.data instanceof Blob
                ? await msgEv.data.arrayBuffer()
                : null;
          if (!rawBuf) return;

          const assembled = reassembler.pushFrame(rawBuf);
          if (!assembled) return;

          if (assembled.kind === 'text') {
            this.textHandlers.forEach((h) => h(assembled.text));
          } else {
            this.binaryHandlers.forEach((h) => h(assembled.data));
          }
        } catch {
          // drop malformed frame
        }
      };

      // Open optional unordered/unreliable ephemeral channel first so legacy single-channel
      // hosts/mocks that only retain the latest channel still bind to the reliable channel.
      let ephemeralDc: RTCDataChannel | null = null;
      try {
        const candidate = pc.createDataChannel(WEBRTC_EPHEMERAL_DATACHANNEL_LABEL, {
          ordered: false,
          maxRetransmits: 0,
        });
        if (candidate) {
          candidate.binaryType = 'arraybuffer';
          ephemeralDc = candidate;
        }
      } catch {
        ephemeralDc = null;
      }

      const dc = pc.createDataChannel(WEBRTC_DATACHANNEL_LABEL, { ordered: true });
      dc.binaryType = 'arraybuffer';
      this.dc = dc;
      this.sender.attachReliableChannel(dc);

      if (ephemeralDc && ephemeralDc !== dc) {
        this.ephemeralDc = ephemeralDc;
        this.sender.attachEphemeralChannel(ephemeralDc);
        ephemeralDc.onmessage = wireOnMessage(this.ephemeralReassembler);

        // Ephemeral channel failures/closes never tear down the session — fall back to reliable
        const detachEphemeral = () => {
          if (this.ephemeralDc === ephemeralDc) {
            this.ephemeralDc = null;
            this.sender.detachEphemeralChannel();
            this.ephemeralReassembler.clear();
          }
        };
        ephemeralDc.onclose = detachEphemeral;
        ephemeralDc.onerror = detachEphemeral;
      }

      dc.onopen = () => {
        settleResolve();
      };

      dc.onmessage = wireOnMessage(this.reassembler);

      dc.onclose = () => {
        if (!settled) {
          settleReject(new Error('Host closed the connection before handshake completed'));
        } else {
          this.emitCloseOnce('Host disconnected from the P2P room');
        }
      };

      dc.onerror = (ev) => {
        this.errorHandlers.forEach((h) => h(ev));
      };

      pc.onicecandidate = (ev) => {
        if (this.closed || !ev.candidate) return;
        const json = ev.candidate.toJSON ? ev.candidate.toJSON() : undefined;
        const candidateStr = json?.candidate ?? ev.candidate.candidate ?? '';
        if (!candidateStr) return;
        const candidateInit: NonNullable<SignalingEnvelope['candidate']> = {
          candidate: candidateStr,
          sdpMid: json?.sdpMid ?? ev.candidate.sdpMid ?? null,
          sdpMLineIndex: json?.sdpMLineIndex ?? ev.candidate.sdpMLineIndex ?? null,
          ...(json?.usernameFragment != null ? { usernameFragment: json.usernameFragment } : {}),
        };
        if (this.localIceCandidates.length < 64) {
          this.localIceCandidates.push(candidateInit);
        }
        this.signaling.send({
          type: 'ice',
          fromPeerId: this.peerId,
          ...(this.hostSignalingPeerId ? { toPeerId: this.hostSignalingPeerId } : {}),
          candidate: candidateInit,
        });
      };

      pc.onconnectionstatechange = () => {
        if (pc.connectionState === 'connected') {
          if (this.disconnectTimer !== null) {
            clearTimeout(this.disconnectTimer);
            this.disconnectTimer = null;
          }
          return;
        }
        if (pc.connectionState === 'failed' || pc.connectionState === 'closed') {
          if (!settled) {
            settleReject(
              new Error(
                'WebRTC peer connection failed (strict firewall/NAT). Try configuring an optional TURN server in Settings.'
              )
            );
          } else {
            this.emitCloseOnce('Host disconnected from the P2P room');
          }
        } else if (pc.connectionState === 'disconnected' && settled) {
          if (this.disconnectTimer === null) {
            this.disconnectTimer = setTimeout(() => {
              this.disconnectTimer = null;
              if (!this.closed && this.pc?.connectionState === 'disconnected') {
                this.emitCloseOnce('Host disconnected from the P2P room');
              }
            }, 5_000);
          }
        }
      };

      this.unsubSignaling = this.signaling.onMessage((env: SignalingEnvelope) => {
        void this.handleSignalingEnvelope(env, settleReject);
      });

      // Create & broadcast offer (with periodic retry carrying live gathered SDP & ICE until answer arrives)
      pc.createOffer()
        .then(async (offer) => {
          if (this.closed) return;
          await pc.setLocalDescription(offer);
          const initialSdp = pc.localDescription?.sdp || offer.sdp;
          if (!initialSdp) {
            settleReject(new Error('Failed to generate WebRTC SDP offer'));
            return;
          }

          const sendOffer = () => {
            if (this.closed || this.remoteDescSet) return;
            const liveSdp = pc.localDescription?.sdp || initialSdp;
            this.signaling.send({
              type: 'offer',
              fromPeerId: this.peerId,
              sdp: liveSdp,
              sdpType: 'offer',
            });
            for (const cand of this.localIceCandidates) {
              this.signaling.send({
                type: 'ice',
                fromPeerId: this.peerId,
                candidate: cand,
              });
            }
          };

          sendOffer();
          this.offerRetryTimer = setInterval(sendOffer, 2_500);
        })
        .catch((err) => {
          settleReject(err instanceof Error ? err : new Error('Failed to create WebRTC offer'));
        });
    });
  }

  private async handleSignalingEnvelope(
    env: SignalingEnvelope,
    onFatalError: (err: Error) => void
  ): Promise<void> {
    if (this.closed || !this.pc) return;

    if (env.type === 'room-full') {
      onFatalError(new Error(env.reason || 'Room is full (maximum 8 guests)'));
      return;
    }

    if (env.type === 'bye' && env.fromPeerId === this.hostSignalingPeerId) {
      this.emitCloseOnce(env.reason || 'Host closed the P2P room');
      return;
    }

    if (env.type === 'answer' && env.sdp && !this.remoteDescSet) {
      this.hostSignalingPeerId = env.fromPeerId;
      if (this.offerRetryTimer !== null) {
        clearInterval(this.offerRetryTimer);
        this.offerRetryTimer = null;
      }
      try {
        await this.pc.setRemoteDescription({ type: 'answer', sdp: env.sdp });
        this.remoteDescSet = true;
        for (const cand of this.pendingIce.splice(0)) {
          try {
            await this.pc.addIceCandidate(cand);
          } catch {
            // ignore stale candidate
          }
        }
        // Replay any local ICE candidates gathered before we knew hostSignalingPeerId
        for (const localCand of this.localIceCandidates) {
          this.signaling.send({
            type: 'ice',
            fromPeerId: this.peerId,
            toPeerId: this.hostSignalingPeerId,
            candidate: localCand,
          });
        }
      } catch (err) {
        onFatalError(
          err instanceof Error ? err : new Error('Failed to apply WebRTC answer from host')
        );
      }
      return;
    }

    if (env.type === 'ice' && env.candidate) {
      if (!this.remoteDescSet) {
        if (this.pendingIce.length < 64) {
          this.pendingIce.push(env.candidate);
        }
        return;
      }
      try {
        await this.pc.addIceCandidate(env.candidate);
      } catch {
        // ignore invalid candidate
      }
    }
  }

  private emitCloseOnce(reason?: string): void {
    if (this.hasNotifiedClose) return;
    this.hasNotifiedClose = true;
    this.closeInternal(false);
    this.closeHandlers.forEach((h) => h(reason));
  }

  public isOpen(): boolean {
    return !this.closed && this.dc !== null && this.dc.readyState === 'open';
  }

  public hasOpenEphemeralChannel(): boolean {
    return !this.closed && this.sender.hasOpenEphemeralChannel();
  }

  public getBufferedAmount(): number {
    return this.closed ? 0 : this.sender.getTotalBufferedAmount();
  }

  public getDroppedEphemeralCount(): number {
    return this.sender.getDroppedEphemeralCount();
  }

  public async getActiveIceCandidateType(): Promise<CollabIceCandidateType | null> {
    if (this.closed) return null;
    return queryPeerConnectionIceCandidateType(this.pc);
  }

  public sendText(text: string, options?: CollabSendOptions): void {
    if (!this.isOpen()) return;
    try {
      const frames = encodeTransportFrames('text', text, this.allocMessageId());
      this.sender.sendFrames(frames, Boolean(options?.ephemeral));
    } catch {
      // ignore encode failure on oversized payload
    }
  }

  public sendBinary(data: ArrayBuffer | Uint8Array, options?: CollabSendOptions): void {
    if (!this.isOpen()) return;
    try {
      const ephemeral = options?.ephemeral ?? isEphemeralWireBinary(data);
      const frames = encodeTransportFrames('binary', data, this.allocMessageId());
      this.sender.sendFrames(frames, ephemeral);
    } catch {
      // ignore encode failure on oversized payload
    }
  }

  public onTextMessage(handler: (text: string) => void): () => void {
    this.textHandlers.add(handler);
    return () => this.textHandlers.delete(handler);
  }

  public onBinaryMessage(handler: (data: ArrayBuffer) => void): () => void {
    this.binaryHandlers.add(handler);
    return () => this.binaryHandlers.delete(handler);
  }

  public onClose(handler: (reason?: string) => void): () => void {
    this.closeHandlers.add(handler);
    return () => this.closeHandlers.delete(handler);
  }

  public onError(handler: (err: Error | Event) => void): () => void {
    this.errorHandlers.add(handler);
    return () => this.errorHandlers.delete(handler);
  }

  private closeInternal(sendBye: boolean): void {
    if (this.closed) return;
    this.closed = true;

    if (this.offerRetryTimer !== null) {
      clearInterval(this.offerRetryTimer);
      this.offerRetryTimer = null;
    }
    if (this.signalingCloseTimer !== null) {
      clearTimeout(this.signalingCloseTimer);
      this.signalingCloseTimer = null;
    }
    if (this.disconnectTimer !== null) {
      clearTimeout(this.disconnectTimer);
      this.disconnectTimer = null;
    }
    if (sendBye) {
      try {
        this.signaling.send({
          type: 'bye',
          fromPeerId: this.peerId,
          ...(this.hostSignalingPeerId ? { toPeerId: this.hostSignalingPeerId } : {}),
        });
      } catch {
        // ignore
      }
    }

    this.unsubSignaling?.();
    this.unsubSignaling = null;
    this.signaling.close();
    this.sender.dispose();
    this.reassembler.clear();
    this.ephemeralReassembler.clear();

    try {
      if (this.ephemeralDc) {
        this.ephemeralDc.onopen = null;
        this.ephemeralDc.onmessage = null;
        this.ephemeralDc.onclose = null;
        this.ephemeralDc.onerror = null;
        this.ephemeralDc.close();
      }
    } catch {
      // ignore
    }
    this.ephemeralDc = null;

    try {
      if (this.dc) {
        this.dc.onopen = null;
        this.dc.onmessage = null;
        this.dc.onclose = null;
        this.dc.onerror = null;
        this.dc.close();
      }
    } catch {
      // ignore
    }
    this.dc = null;

    try {
      if (this.pc) {
        this.pc.onicecandidate = null;
        this.pc.onconnectionstatechange = null;
        this.pc.close();
      }
    } catch {
      // ignore
    }
    this.pc = null;
  }

  public close(): void {
    this.closeInternal(true);
  }
}