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. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

13.2 KB
Raw Blame
import { parse, write } from "sdp-transform";
import { createOpaqueId } from "../lib/opaque-id";
import { debugError, debugEvent } from "../lib/debug";
import { debugRtcFailure, debugRtcStats, debugTrack, observeDebugConnection } from "../lib/debug-webrtc";
import { addRemoteIceCandidate } from "../webrtc/nat-prediction";
import { normalizeVideoOfferSdp } from "../webrtc/video-codec";
import { encodedStreams } from "./browser-encoding-output";
import {
  applyVideoCaptureProfile, configureVideoSender,
  needsStartupVideoProfile, startupVideoProfile, STARTUP_VIDEO_ENCODED_FRAMES,
  videoQualitySettingsEqual, type QualityProfile,
} from "./quality";
import { cloneSenderVideoTrack, stopSenderVideoTrack } from "./sender-video-track";
import { waitForConnectionOperation } from "../webrtc/connection-operation";

// Leave time for ordinary encoding before the outer Viewer/route deadline.
// Bound local transport setup, not frame production from a quiet source.
const LOCAL_CONNECTION_TIMEOUT_MS = 8_000;

export class BrowserEncodingProducer {
  readonly id = createOpaqueId();
  private send: RTCPeerConnection | null = null;
  private receive: RTCPeerConnection | null = null;
  private readonly releaseDebug: Array<() => void> = [];
  private input: MediaStreamTrack | null = null;
  private videoSender: RTCRtpSender | null = null;
  private video: HTMLVideoElement | null = null;
  private readonly streamAbort = new AbortController();
  private streamWriter: WritableStreamDefaultWriter<RTCEncodedVideoFrame> | null = null;
  private parameterTail = Promise.resolve();
  private disposed = false;
  private paused: boolean;
  private budget: number;
  private startupPending: boolean;
  private startupFrames: number | undefined;
  private startupTimer: ReturnType<typeof setTimeout> | undefined;
  private applied: { capture: QualityProfile; sender: QualityProfile } | null = null;

  constructor(
    readonly source: MediaStreamTrack,
    private desiredProfile: QualityProfile,
    private readonly codec: RTCRtpCodec,
    private readonly onFrame: (frame: RTCEncodedVideoFrame) => void,
    private readonly onFailure: () => void,
  ) {
    this.desiredProfile = { ...desiredProfile };
    this.budget = desiredProfile.maxBitrate;
    this.startupPending = needsStartupVideoProfile(desiredProfile);
    this.paused = !source.enabled;
  }

  get track(): MediaStreamTrack | null { return this.input; }
  get sender(): RTCRtpSender | null { return this.videoSender; }

  async start(initialBudget = this.desiredProfile.maxBitrate): Promise<void> {
    this.checkAlive();
    if (this.send) throw new Error("Browser encoding producer already started");
    this.budget = initialBudget;
    try {
      if (this.source.readyState !== "live") throw new Error("Browser encoding source ended");
      const track = this.input = cloneSenderVideoTrack(this.source, () => this.fail(), { normalizeOrientation: true });
      track.enabled = !this.paused;
      this.source.addEventListener("ended", this.fail);
      track.addEventListener("ended", this.fail);
      const send = this.send = new RTCPeerConnection({ iceServers: [], encodedInsertableStreams: true } as RTCConfiguration);
      const receive = this.receive = new RTCPeerConnection({ iceServers: [] });
      this.releaseDebug.push(observeDebugConnection(send, { producerId: this.id, role: "pool-producer" }));
      this.releaseDebug.push(observeDebugConnection(receive, { producerId: this.id, role: "pool-local-receiver" }));
      const transceiver = send.addTransceiver(track, { direction: "sendonly", streams: [new MediaStream([track])] });
      this.videoSender = transceiver.sender;
      const streams = encodedStreams(transceiver.sender), writer = streams.writable.getWriter();
      this.streamWriter = writer;
      void streams.readable.pipeTo(new WritableStream({ write: async (frame) => {
        if (this.disposed) return;
        if (this.startupPending && this.startupFrames !== undefined && !this.paused && frame.data.byteLength > 0) {
          this.startupFrames = Math.min(STARTUP_VIDEO_ENCODED_FRAMES, this.startupFrames + 1);
        }
        // Outputs clone synchronously before the local decoder consumes the original.
        this.onFrame(frame);
        await writer.write(frame);
      } }), { signal: this.streamAbort.signal }).then(this.fail, this.fail).finally(() => {
        try { writer.releaseLock(); } catch { /* Owner abort retired the writer. */ }
      });
      transceiver.setCodecPreferences([this.codec]);
      const video = this.video = document.createElement("video");
      video.autoplay = true;
      video.muted = true;
      video.playsInline = true;
      receive.ontrack = ({ track: received }) => {
        if (this.disposed) return;
        video.srcObject = new MediaStream([received]);
        void video.play().catch(this.fail);
      };
      for (const connection of [send, receive]) {
        connection.onconnectionstatechange = () => {
          if (connection.connectionState === "failed") this.fail();
          else if (send.connectionState === "connected" && receive.connectionState === "connected") {
            this.clearStartupTimer();
          }
        };
      }
      const forward = (from: RTCPeerConnection, to: RTCPeerConnection) => {
        const pending: RTCIceCandidate[] = [];
        from.onicecandidate = ({ candidate }) => {
          if (!candidate || this.disposed) return;
          if (!to.remoteDescription) pending.push(candidate);
          else void addRemoteIceCandidate(to, candidate).catch(this.fail);
        };
        return async () => {
          for (const candidate of pending.splice(0)) {
            this.checkAlive();
            await addRemoteIceCandidate(to, candidate);
          }
        };
      };
      const flushReceive = forward(send, receive), flushSend = forward(receive, send);
      this.startupTimer = setTimeout(() => {
        this.fail(new DOMException("Local encoding connection timed out", "TimeoutError"));
      }, LOCAL_CONNECTION_TIMEOUT_MS);
      await this.serialize(() => this.applyCurrent());
      await send.setLocalDescription(await send.createOffer());
      this.checkAlive();
      await receive.setRemoteDescription({ type: "offer", sdp: normalizeVideoOfferSdp(send.localDescription!.sdp) });
      await flushReceive();
      this.checkAlive();
      await receive.setLocalDescription(this.prepareLocalAnswer(await receive.createAnswer()));
      this.checkAlive();
      await send.setRemoteDescription(receive.localDescription!);
      await flushSend();
      this.checkAlive();
    } catch (error) {
      const retired = this.disposed;
      this.dispose();
      if (retired) this.checkAlive();
      throw error;
    }
  }

  update(profile: QualityProfile, budget: number): Promise<void> {
    this.desiredProfile = { ...profile };
    this.budget = budget;
    if (!needsStartupVideoProfile(profile)) this.startupPending = false;
    return this.serialize(() => this.applyCurrent());
  }

  requestKey(): Promise<void> {
    return this.serialize(async () => {
      const sender = this.requireSender();
      // Chromium's supported keyframe option is not yet in TypeScript's DOM declarations.
      const request = sender.setParameters as (parameters: RTCRtpSendParameters,
        options: { encodingOptions: Array<{ keyFrame: boolean }> }) => Promise<void>;
      await request.call(sender, sender.getParameters(), { encodingOptions: [{ keyFrame: true }] });
    // Producer failure falls back to ordinary encoding on the same connection.
    // Its retired keyframe requests must not fail that outgoing connection.
    }).catch(this.fail);
  }

  beginOutput(): void {
    // Local warmup cannot train an outgoing connection's allocation. Start the
    // existing protection only after a child actually writes this producer.
    this.startupFrames ??= 0;
  }

  async report(): Promise<RTCStatsReport> {
    this.requireSender();
    const connection = this.send!;
    const report = await waitForConnectionOperation(this.streamAbort.signal, () => connection.getStats()).catch((error) => {
      if (!this.disposed) debugRtcFailure(connection, error);
      throw error;
    });
    debugRtcStats(connection, report);
    if (!this.disposed && this.startupPending &&
      (this.startupFrames ?? 0) >= STARTUP_VIDEO_ENCODED_FRAMES) {
      this.startupPending = false;
      void this.serialize(() => this.applyCurrent()).catch(this.fail);
    }
    return report;
  }

  setPaused(paused: boolean): void {
    this.paused = paused;
    if (this.input) this.input.enabled = !paused;
  }

  dispose(): void {
    if (this.disposed) return;
    this.disposed = true;
    for (const release of this.releaseDebug) release();
    this.releaseDebug.length = 0;
    this.clearStartupTimer();
    debugEvent("encoding-pool", "producer-retired", { producerId: this.id });
    this.source.removeEventListener("ended", this.fail);
    this.input?.removeEventListener("ended", this.fail);
    stopSenderVideoTrack(this.input);
    this.streamAbort.abort();
    void this.streamWriter?.abort().catch(() => undefined);
    this.streamWriter = null;
    for (const connection of [this.send, this.receive]) {
      if (!connection) continue;
      connection.onicecandidate = connection.onconnectionstatechange = connection.ontrack = null;
      connection.close();
    }
    if (this.video) { this.video.pause(); this.video.srcObject = null; }
    this.input = this.videoSender = this.video = this.send = this.receive = null;
  }

  private readonly fail = (error?: unknown): void => {
    if (this.disposed) return;
    debugError("encoding-pool", "producer-failed", error, { producerId: this.id });
    this.dispose();
    this.onFailure();
  };

  private clearStartupTimer(): void {
    clearTimeout(this.startupTimer);
    this.startupTimer = undefined;
  }

  private checkAlive(): void {
    if (this.disposed) throw new DOMException("Browser encoding producer was retired", "AbortError");
  }

  private requireSender(): RTCRtpSender {
    this.checkAlive();
    if (!this.videoSender) throw new Error("Browser encoding producer has not started");
    return this.videoSender;
  }

  private prepareLocalAnswer(answer: RTCSessionDescriptionInit): RTCSessionDescriptionInit {
    const kbps = Math.floor(Math.min(this.budget, this.desiredProfile.maxBitrate) / 1_000);
    if (!answer.sdp) return answer;
    const session = parse(answer.sdp);
    const media = session.media.find((section) => section.type === "video" && section.port !== 0);
    if (!media) return answer;
    // Shared frames already have normalized pixels and also feed peers without
    // CVO; do not leave orientation in a local-only RTP extension.
    const cvo = media.ext?.some(extension => extension.uri === "urn:3gpp:video-orientation");
    if (cvo) media.ext = media.ext?.filter(extension => extension.uri !== "urn:3gpp:video-orientation");
    const codec = media.rtp.find((entry) => `video/${entry.codec}`.toLowerCase() === this.codec.mimeType.toLowerCase());
    if (!Number.isFinite(kbps) || kbps <= 0 || !codec) return cvo ? { ...answer, sdp: write(session) } : answer;
    const format = media.fmtp.find((entry) => entry.payload === codec.payload);
    const parameters = (format?.config ?? "").split(";").map((part) => part.trim())
      .filter((part) => part && !/^x-google-start-bitrate\s*=/i.test(part));
    parameters.push(`x-google-start-bitrate=${kbps}`);
    if (format) format.config = parameters.join(";");
    else media.fmtp.push({ payload: codec.payload, config: parameters.join(";") });
    // This same-Browser encoder already has a downstream native allocation.
    // WebRTC reads send rates from the remote answer; an offer hint is dropped.
    // Seed once, without a minimum or an external SDP/codec-identity change.
    return { ...answer, sdp: write(session) };
  }

  private async applyCurrent(): Promise<void> {
    const sender = this.requireSender(), track = this.input!;
    const profile = this.desiredProfile;
    const previous = this.applied;
    const effective = this.startupPending ? startupVideoProfile(profile) : profile;
    const parameters = { ...effective, maxBitrate: Math.min(profile.maxBitrate, this.budget) };
    const captureChanged = !previous || !videoQualitySettingsEqual(previous.capture, profile);
    const senderChanged = !previous || !videoQualitySettingsEqual(previous.sender, parameters);
    if (!captureChanged && !senderChanged) return;
    try {
      if (captureChanged) await applyVideoCaptureProfile(track, profile);
      this.checkAlive();
      if (senderChanged) await configureVideoSender(sender, parameters);
      this.checkAlive();
      this.applied = { capture: profile, sender: parameters };
      debugEvent("encoding-pool", "producer-configured", { producerId: this.id, profile, parameters });
      if (captureChanged) debugTrack(track, { producerId: this.id });
    } catch (error) {
      debugError("encoding-pool", "producer-settings-failed", error, { producerId: this.id, requested: profile, parameters });
      if (captureChanged && previous && !this.disposed) {
        await applyVideoCaptureProfile(track, previous.capture).catch(this.fail);
      }
      throw error;
    }
  }

  private serialize(operation: () => Promise<unknown>): Promise<void> {
    return this.parameterTail = this.parameterTail.catch(() => undefined).then(async () => {
      this.checkAlive();
      await operation();
      this.checkAlive();
    });
  }
}