Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

20.8 KB
Raw Blame
import type {
  IceConfig,
  PreparedRouteCandidate,
  SignalPayload,
} from "../../shared/protocol";
import type { QualityProfile } from "../media/quality";
import { debugError, debugEvent } from "../lib/debug";
import { BrowserEncodingPool } from "../media/browser-encoding-pool";
import type { PeerSnapshot } from "../types";
import {
  HostPeer,
  type HostMediaPeer,
  type HostPeerEvents,
} from "./host-peer";
import { MAX_ENDPOINT_MEDIA_CHILDREN } from "./media-assignment";
import {
  automaticVideoCodecPreference,
  type BrowserVideoCodecPreference,
  VP8_ONLY_VIDEO_CODEC,
} from "./video-codec";
import { preferredVideoCodecForTrack } from "./video-codec-preflight";

export interface ViewerRelayEvents {
  onInputChannel?: (channel: RTCDataChannel, connection: RTCPeerConnection) => void;
  sendSignal: (peerId: string, payload: SignalPayload) => boolean;
  onUpdate?: (snapshot: PeerSnapshot | null) => void;
  onSenderUpdate?: (snapshot: PeerSnapshot, revision: number | null) => void;
  onPreparedChildFailed?: (revision: number, connectionId: string) => void;
  onPreparedChildConnected?: (revision: number, connectionId: string) => boolean;
}
interface PreparedChild {
  revision: number;
  childPeerId: string;
  candidate: PreparedRouteCandidate;
  peer: HostMediaPeer;
  failed: boolean;
  connectedSent: boolean;
  replacesConnectionId: string | null;
}

export interface ViewerRelayPeerFactory {
  requiresStream: boolean;
  create(
    childPeerId: string,
    connectionId: string | undefined,
    events: ViewerRelayPeerEvents,
    candidate: PreparedRouteCandidate | null,
  ): HostMediaPeer | null;
}

export interface ViewerRelayPeerEvents {
  sendSignal: (peerId: string, payload: SignalPayload) => boolean;
  onUpdate: (snapshot: PeerSnapshot) => void;
}

export class ViewerRelay {
  private childPeerIds: string[] = [];
  private stream: MediaStream | null = null;
  private readonly peers = new Map<string, HostMediaPeer>();
  private readonly videoPool = new BrowserEncodingPool();
  private readonly snapshots = new Map<string, PeerSnapshot>();
  private preparedChild: PreparedChild | null = null;
  private preparedRevision: number | null = null;
  private preparedCandidate: PreparedRouteCandidate | null = null;
  private plannedChildPeerIds: string[] = [];
  private syncQueue = Promise.resolve();
  private videoCodec: BrowserVideoCodecPreference = VP8_ONLY_VIDEO_CODEC;
  private codecProbeTrack: MediaStreamTrack | null = null;
  private codecProbeAbort: AbortController | null = null;
  private codecProbePromise: Promise<void> | null = null;
  private codecProbeSettled = false;
  private disposed = false;

  constructor(
    private iceConfig: IceConfig,
    private desiredProfile: QualityProfile,
    private readonly events: ViewerRelayEvents,
    private maxMediaEdges = MAX_ENDPOINT_MEDIA_CHILDREN,
    private readonly natPredictionEnabled = false,
    private readonly peerFactory: ViewerRelayPeerFactory | null = null,
  ) {}

  getSnapshot(childPeerId?: string): PeerSnapshot | null {
    const peerId =
      childPeerId ??
      this.childPeerIds.find((candidate) => this.snapshots.has(candidate));
    const snapshot = peerId ? this.snapshots.get(peerId) : undefined;
    return snapshot
      ? { ...snapshot, metrics: { ...snapshot.metrics } }
      : null;
  }

  prepareChild(
    revision: number,
    candidate: PreparedRouteCandidate,
    plannedChildPeerIds: readonly string[],
  ): boolean {
    const stream = this.stream;
    const planned = [...new Set(plannedChildPeerIds)];
    let refusal: string | null = null;
    if (this.disposed) refusal = "disposed";
    else if (!stream && (this.peerFactory?.requiresStream ?? true)) refusal = "no-stream";
    else if (candidate.transport === "sfu") refusal = "sfu-transport";
    else if (!planned.includes(candidate.childPeerId)) refusal = "child-unplanned";
    else if (planned.length !== plannedChildPeerIds.length) refusal = "plan-duplicate";
    else if (planned.length < this.childPeerIds.length) refusal = "plan-shrinks";
    else if (planned.length > this.childPeerIds.length + 1) refusal = "plan-skips";
    else if (planned.length > this.maxMediaEdges) refusal = "over-capacity";
    else if (this.childPeerIds.some((peerId) => !planned.includes(peerId))) refusal = "drops-current";
    if (refusal !== null) {
      debugEvent("webrtc", "relay-prepare-refused", {
        revision,
        childPeerId: candidate.childPeerId,
        reason: refusal,
      });
      this.discardPreparedChild();
      return false;
    }
    if (
      this.preparedRevision === revision &&
      sameCandidate(this.preparedCandidate, candidate) &&
      samePeerIds(this.plannedChildPeerIds, planned)
    ) {
      return this.preparedChild ? !this.preparedChild.failed : true;
    }

    this.discardPreparedChild();
    this.preparedRevision = revision;
    this.preparedCandidate = { ...candidate };
    this.plannedChildPeerIds = planned;
    this.startPreparedChildIfReady();
    return true;
  }

  activateChildren(revision: number, childPeerIds: readonly string[]): void {
    const prepared = this.preparedChild;
    const extendsActive =
      childPeerIds.length === this.childPeerIds.length + 1 &&
      this.childPeerIds.every((peerId) => childPeerIds.includes(peerId));
    const replacedPeer = prepared
      ? this.peers.get(prepared.childPeerId) ?? null
      : null;
    const replacesActive =
      replacedPeer?.connectionId === prepared?.replacesConnectionId;
    if (
      prepared?.revision === revision &&
      !prepared.failed &&
      prepared.candidate.connectionId === prepared.peer.connectionId &&
      childPeerIds.includes(prepared.childPeerId) &&
      (extendsActive || replacesActive)
    ) {
      this.preparedChild = null;
      this.preparedRevision = null;
      this.preparedCandidate = null;
      this.plannedChildPeerIds = [];
      this.peers.set(prepared.childPeerId, prepared.peer);
      replacedPeer?.dispose();
      this.snapshots.set(prepared.childPeerId, prepared.peer.getSnapshot());
    } else {
      this.discardPreparedChild();
    }
    this.reconcileChildren(childPeerIds);
  }

  discardPreparedChild(): void {
    this.preparedRevision = null;
    this.preparedCandidate = null;
    this.plannedChildPeerIds = [];
    this.discardPreparedPeer();
    this.startCodecProbe();
  }

  private discardPreparedPeer(): void {
    const prepared = this.preparedChild;
    this.preparedChild = null;
    prepared?.peer.dispose();
  }

  setChildren(childPeerIds: readonly string[]): void {
    this.reconcileChildren(childPeerIds);
  }

  resyncSignaling(): void {
    // Authoritative reconciliation recreates negotiations whose signals may be lost.
    for (const [childPeerId, peer] of this.peers) {
      if (!peer.isConnected()) this.disposePeer(childPeerId);
    }
  }

  updateCapacity(maxMediaEdges: number): void {
    if (this.disposed || this.maxMediaEdges === maxMediaEdges) {
      return;
    }
    this.maxMediaEdges = maxMediaEdges;
    if (this.plannedChildPeerIds.length > maxMediaEdges) {
      this.discardPreparedChild();
    }
    this.reconcileChildren(this.childPeerIds);
  }

  private reconcileChildren(childPeerIds: readonly string[]): void {
    if (this.disposed) {
      return;
    }
    const nextChildPeerIds = [...new Set(childPeerIds)].slice(
      0,
      this.maxMediaEdges,
    );
    for (const childPeerId of this.childPeerIds) {
      if (!nextChildPeerIds.includes(childPeerId)) {
        this.disposePeer(childPeerId);
      }
    }
    this.childPeerIds = nextChildPeerIds;
    if (this.childPeerIds.length > 0) {
      this.cancelCodecProbe();
    }
    for (const childPeerId of this.childPeerIds) {
      if (this.stream || this.peerFactory?.requiresStream === false) {
        this.startPeer(childPeerId, this.stream);
      }
    }
    this.startCodecProbe();
  }

  setStream(stream: MediaStream): void {
    if (this.disposed) {
      return;
    }
    const nextVideoTrack = stream.getVideoTracks()[0] ?? null;
    if (!this.peerFactory && nextVideoTrack !== this.codecProbeTrack) {
      this.cancelCodecProbe();
      this.codecProbeTrack = nextVideoTrack;
      if (!this.codecProbeSettled) {
        this.videoCodec = VP8_ONLY_VIDEO_CODEC;
      }
    }
    const preparedPeer = this.preparedChild?.peer ?? null;
    this.stream = stream;
    this.syncQueue = this.syncQueue
      .then(() => this.syncStream(stream, preparedPeer))
      .catch(() => undefined);
    this.startCodecProbe();
  }

  updateProfile(profile: QualityProfile): Promise<boolean> {
    if (this.disposed) {
      return Promise.resolve(false);
    }
    this.desiredProfile = profile;
    const result = this.syncQueue.then(() => this.syncProfile(profile));
    this.syncQueue = result.then(
      () => undefined,
      () => undefined,
    );
    return result.catch(() => false);
  }

  getSignalRouteRevision(
    fromPeerId: string,
    connectionId: string,
    activeRevision: number,
  ): number {
    const prepared = this.preparedChild;
    return prepared?.childPeerId === fromPeerId &&
      !prepared.failed &&
      prepared.peer.connectionId === connectionId
      ? prepared.revision
      : activeRevision;
  }

  async acceptSignal(
    fromPeerId: string,
    payload: SignalPayload,
    routeRevision: number,
  ): Promise<boolean> {
    const prepared = this.preparedChild;
    if (
      prepared?.revision === routeRevision &&
      !prepared.failed &&
      prepared.childPeerId === fromPeerId &&
      prepared.peer.connectionId === payload.connectionId
    ) {
      await prepared.peer.acceptSignal(payload);
      return true;
    }
    const peer = this.peers.get(fromPeerId);
    if (
      !this.childPeerIds.includes(fromPeerId) ||
      !peer ||
      peer.connectionId !== payload.connectionId
    ) {
      return false;
    }
    await peer.acceptSignal(payload);
    return true;
  }

  async recover(
    fromPeerId: string,
    connectionId: string,
    rebuild: boolean,
  ): Promise<void> {
    const stream = this.stream;
    const peer = this.peers.get(fromPeerId);
    if (
      this.disposed ||
      (!stream && (this.peerFactory?.requiresStream ?? true)) ||
      !this.childPeerIds.includes(fromPeerId)
    ) {
      return;
    }
    if (rebuild) {
      if (peer && peer.connectionId !== connectionId) {
        return;
      }
      this.disposePeer(fromPeerId);
      this.startPeer(fromPeerId, stream);
      return;
    }
    if (!peer || peer.connectionId !== connectionId) {
      return;
    }
    if (await peer.restartIce()) {
      return;
    }
    if (this.peers.get(fromPeerId) === peer) {
      this.disposePeer(fromPeerId);
      this.startPeer(fromPeerId, this.stream);
    }
  }

  updateIceConfig(iceConfig: IceConfig): void {
    this.iceConfig = iceConfig;
    for (const peer of this.peers.values()) {
      peer.updateIceConfig(iceConfig);
    }
    this.preparedChild?.peer.updateIceConfig(iceConfig);
  }

  stop(): void {
    this.cancelCodecProbe();
    this.codecProbeTrack = null;
    this.codecProbeSettled = false;
    this.videoCodec = VP8_ONLY_VIDEO_CODEC;
    this.stream = null;
    this.discardPreparedChild();
    this.disposePeers();
  }

  dispose(): void {
    if (this.disposed) {
      return;
    }
    this.disposed = true;
    this.cancelCodecProbe();
    this.codecProbeTrack = null;
    this.stream = null;
    this.discardPreparedChild();
    this.childPeerIds = [];
    this.disposePeers();
    this.videoPool.dispose();
  }

  private startPreparedChild(
    revision: number,
    candidate: PreparedRouteCandidate,
    stream: MediaStream | null,
  ): void {
    let peer: HostMediaPeer;
    try {
      peer = this.createPeer(
        candidate.childPeerId,
        stream,
        candidate.connectionId,
        candidate,
      );
    } catch (error) {
      debugError("webrtc", "relay-prepare-failed", error, {
        revision,
        connectionId: candidate.connectionId,
        stage: "create",
      });
      this.discardPreparedChild();
      this.events.onPreparedChildFailed?.(revision, candidate.connectionId);
      return;
    }
    this.preparedChild = {
      revision,
      childPeerId: candidate.childPeerId,
      candidate: { ...candidate },
      peer,
      failed: false,
      connectedSent: false,
      replacesConnectionId:
        this.peers.get(candidate.childPeerId)?.connectionId ?? null,
    };
    void peer
      .start()
      .catch((error) => {
        debugError("webrtc", "relay-prepare-failed", error, {
          revision,
          connectionId: candidate.connectionId,
          stage: "start",
        });
        return false;
      })
      .then((started) => {
        if (!started && this.preparedChild?.peer === peer) {
          this.failPreparedChild(peer);
        }
      });
  }

  private startPreparedChildIfReady(): void {
    const revision = this.preparedRevision;
    const candidate = this.preparedCandidate;
    if (
      this.disposed ||
      this.codecProbePromise ||
      this.preparedChild ||
      revision === null ||
      candidate?.transport !== "direct"
    ) {
      return;
    }
    this.startPreparedChild(revision, candidate, this.stream);
  }

  private async syncStream(
    stream: MediaStream,
    preparedPeer: HostMediaPeer | null,
  ): Promise<void> {
    if (this.disposed || this.stream !== stream) {
      return;
    }
    if (
      preparedPeer &&
      this.preparedChild?.peer === preparedPeer &&
      !(await this.replacePeerStream(preparedPeer, stream)) &&
      this.stream === stream
    ) {
      this.failPreparedChild(preparedPeer);
    }
    if (this.disposed || this.stream !== stream) {
      return;
    }
    await Promise.all(
      this.childPeerIds.map((childPeerId) =>
        this.syncPeerStream(childPeerId, stream),
      ),
    );
  }

  private async syncPeerStream(
    childPeerId: string,
    stream: MediaStream,
  ): Promise<void> {
    const peer = this.peers.get(childPeerId);
    if (!peer) {
      this.startPeer(childPeerId, stream);
      return;
    }

    const replaced = await this.replacePeerStream(peer, stream);
    if (
      replaced ||
      this.disposed ||
      this.peers.get(childPeerId) !== peer ||
      this.stream !== stream ||
      !this.childPeerIds.includes(childPeerId)
    ) {
      return;
    }
    this.disposePeer(childPeerId);
    this.startPeer(childPeerId, stream);
  }

  private async syncProfile(profile: QualityProfile): Promise<boolean> {
    if (this.disposed || this.desiredProfile !== profile) {
      return false;
    }
    const prepared = this.preparedChild;
    const peers = [
      ...this.peers.values(),
      ...(prepared ? [prepared.peer] : []),
    ];
    if (peers.length === 0) {
      return true;
    }
    const results = await Promise.all(
      peers.map(async (peer) =>
        (this.peers.get(peer.peerId) !== peer &&
          this.preparedChild?.peer !== peer) ||
        (await peer.updateProfile(this.desiredProfile)),
      ),
    );
    return this.desiredProfile === profile && results.every(Boolean);
  }

  private async replacePeerStream(
    peer: HostMediaPeer,
    stream: MediaStream,
  ): Promise<boolean> {
    try {
      return await peer.replaceStream(stream);
    } catch {
      return false;
    }
  }

  private async startPeer(
    childPeerId: string,
    stream: MediaStream | null,
    attempt = 0,
  ): Promise<void> {
    if (
      this.disposed ||
      this.peers.has(childPeerId) ||
      this.stream !== stream ||
      !this.childPeerIds.includes(childPeerId)
    ) {
      return;
    }

    let peer: HostMediaPeer | undefined;
    try {
      peer = this.createPeer(childPeerId, stream);
      this.peers.set(childPeerId, peer);
      if (await peer.start()) return;
    } catch (error) {
      debugError("webrtc", "relay-start-failed", error, { connectionId: peer?.connectionId });
    }
    if (this.peers.get(childPeerId) !== peer) return;
    if (peer) this.disposePeer(childPeerId);
    if (
      attempt < 1 &&
      !this.disposed &&
      this.stream === stream &&
      this.childPeerIds.includes(childPeerId)
    ) {
      window.setTimeout(() => {
        void this.startPeer(childPeerId, stream, attempt + 1);
      }, 500);
    }
  }

  private createPeer(
    childPeerId: string,
    stream: MediaStream | null,
    connectionId?: string,
    candidate: PreparedRouteCandidate | null = null,
  ): HostMediaPeer {
    let peer: HostMediaPeer;
    const peerEvents: HostPeerEvents = {
      onInputChannel: this.events.onInputChannel,
      sendSignal: (targetPeerId, payload) =>
        !this.disposed &&
        (this.peers.get(childPeerId) === peer ||
          this.preparedChild?.peer === peer) &&
        childPeerId === targetPeerId
          ? this.events.sendSignal(targetPeerId, payload)
          : false,
      onUpdate: (snapshot) => {
        if (this.preparedChild?.peer === peer) {
          const prepared = this.preparedChild;
          if (
            !prepared.failed && !prepared.connectedSent &&
            snapshot.connectionState === "connected" &&
            snapshot.connectionId === prepared.candidate.connectionId &&
            snapshot.peerId === prepared.childPeerId
          ) {
            prepared.connectedSent = this.events.onPreparedChildConnected?.(
              prepared.revision, snapshot.connectionId,
            ) ?? false;
          }
          if (snapshot.connectionState === "failed") {
            this.failPreparedChild(peer);
          }
          this.events.onSenderUpdate?.(
            snapshot,
            this.preparedChild?.revision ?? null,
          );
          return;
        }
        if (
          !this.disposed &&
          this.peers.get(childPeerId) === peer &&
          this.childPeerIds.includes(childPeerId) &&
          snapshot.peerId === childPeerId &&
          snapshot.connectionId === peer.connectionId
        ) {
          this.snapshots.set(childPeerId, {
            ...snapshot,
            metrics: { ...snapshot.metrics },
          });
          this.events.onSenderUpdate?.(snapshot, null);
          this.events.onUpdate?.(this.getSnapshot());
        }
      },
    };
    if (this.peerFactory) {
      const nativePeer = this.peerFactory.create(
        childPeerId,
        connectionId,
        peerEvents,
        candidate,
      );
      if (nativePeer) return (peer = nativePeer);
    }
    peer = new HostPeer(
      childPeerId,
      this.iceConfig,
      stream!,
      this.desiredProfile,
      peerEvents,
      this.videoCodec,
      connectionId,
      this.natPredictionEnabled,
      candidate?.qualityProbe ? null : this.videoPool,
    );
    return peer;
  }

  private startCodecProbe(): void {
    const track = this.codecProbeTrack;
    if (
      this.disposed ||
      this.peerFactory ||
      this.codecProbeSettled ||
      this.codecProbeAbort ||
      this.codecProbePromise ||
      !track ||
      track.readyState === "ended" ||
      this.childPeerIds.length > 0 ||
      this.preparedChild
    ) {
      return;
    }
    const controller = new AbortController();
    this.codecProbeAbort = controller;
    const promise = preferredVideoCodecForTrack(
      track,
      this.desiredProfile,
      controller.signal,
    )
      .then((codec) => {
        if (
          this.codecProbeAbort !== controller ||
          controller.signal.aborted ||
          this.codecProbeTrack !== track ||
          this.disposed
        ) {
          return;
        }
        this.videoCodec = automaticVideoCodecPreference(codec);
        this.codecProbeSettled = true;
        this.codecProbeAbort = null;
      })
      .finally(() => {
        if (this.codecProbePromise === promise) {
          this.codecProbePromise = null;
          this.startCodecProbe();
          this.startPreparedChildIfReady();
        }
      });
    this.codecProbePromise = promise;
  }

  private cancelCodecProbe(): void {
    const controller = this.codecProbeAbort;
    this.codecProbeAbort = null;
    controller?.abort();
  }

  private failPreparedChild(peer: HostMediaPeer): void {
    const prepared = this.preparedChild;
    if (prepared?.peer !== peer || prepared.failed) {
      return;
    }
    prepared.failed = true;
    peer.dispose();
    this.events.onPreparedChildFailed?.(
      prepared.revision,
      prepared.candidate.connectionId,
    );
  }

  private disposePeers(): void {
    for (const childPeerId of [...this.peers.keys()]) {
      this.disposePeer(childPeerId);
    }
  }

  private disposePeer(childPeerId: string): void {
    const peer = this.peers.get(childPeerId);
    this.peers.delete(childPeerId);
    this.snapshots.delete(childPeerId);
    this.events.onUpdate?.(this.getSnapshot());
    peer?.dispose();
  }

}

function sameCandidate(
  left: PreparedRouteCandidate | null,
  right: PreparedRouteCandidate,
): boolean {
  return (
    left?.childPeerId === right.childPeerId &&
    left.connectionId === right.connectionId &&
    left.transport === right.transport
  );
}

function samePeerIds(left: readonly string[], right: readonly string[]): boolean {
  return (
    left.length === right.length &&
    left.every((peerId, index) => peerId === right[index])
  );
}