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

6.4 KB
Raw Blame
import { describe, expect, it, vi } from "vitest";
import { DEFAULT_QUALITY_SETTINGS, type QualitySettings } from "../src/shared/protocol";

import {
  invalidateSenderQualityEvidence,
  senderQualityEvidenceFromSnapshot,
} from "../src/client/media/sender-quality-evidence";
import {
  NativeSenderPeer,
  shouldUseBrowserQualityCandidate,
} from "../src/client/native/native-sender-peer";
import type { NativeEdgeControl } from "../src/client/native/native-sender-edge";
import type { NativeClientEvent } from "../src/client/native/wire";
import type { PeerSnapshot } from "../src/client/types";

describe("native Host peer quality", () => {
  it.each([
    { codec: "h264", mode: "capture" },
    { codec: "vp8", mode: "capture" },
    { codec: "h264", mode: "relay" },
  ] as const)("maps $codec $mode output into sender evidence without inventing capture statistics", async ({ codec, mode }) => {
    invalidateSenderQualityEvidence();
    let listener: (event: NativeClientEvent) => void = () => undefined;
    const control: NativeEdgeControl = {
    preparePunch: vi.fn(async () => null),
    runPunch: vi.fn(async () => false),
    closePunch: vi.fn(async () => undefined),
      updateShare: vi.fn(async () => undefined),
      prepareEdge: vi.fn<NativeEdgeControl["prepareEdge"]>(async () => ({
        type: "offer",
        sdp: "v=0\r\n",
      })),
      acceptSignal: vi.fn(async () => undefined),
      closeEdge: vi.fn(async () => undefined),
      onEvent: vi.fn((next) => {
        listener = next;
        return () => {
          listener = () => undefined;
        };
      }),
    };
    const snapshots: PeerSnapshot[] = [];
    const source = mode === "relay" ? {
      connectionId: "upstream_123456", getProfile: () => DEFAULT_QUALITY_SETTINGS,
      format: () => ({ width: 1920, height: 1080 }),
    } : undefined;
    const peer = new NativeSenderPeer(
      "viewer_123456",
      "edge_12345678",
      "share_1234567",
      { iceServers: [], natPredictionStunUrls: [] },
      false,
      control,
      {
        sendSignal: () => true,
        onUpdate: (snapshot) => snapshots.push(snapshot),
      },
      codec,
      source,
    );
    expect(await peer.start()).toBe(true);
    if (source) expect(control.updateShare).toHaveBeenCalledOnce();
    else expect(control.updateShare).not.toHaveBeenCalled();
    expect(shouldUseBrowserQualityCandidate(peer, {
      childPeerId: "viewer_123456",
      connectionId: "candidate_123456",
      transport: "direct",
      qualityProbe: true,
    })).toBe(true);
    expect(shouldUseBrowserQualityCandidate(peer, {
      childPeerId: "viewer_123456",
      connectionId: "candidate_123456",
      transport: "direct",
      qualityProbe: false,
    })).toBe(false);
    expect(shouldUseBrowserQualityCandidate(undefined, {
      childPeerId: "viewer_123456",
      connectionId: "candidate_123456",
      transport: "direct",
      qualityProbe: true,
    })).toBe(false);
    listener({
      version: 10,
      type: "edge-state",
      shareId: "share_1234567",
      connectionId: "edge_12345678",
      state: "connected",
    });

    const quality = {
      version: 10 as const,
      type: "edge-quality" as const,
      shareId: "share_1234567",
      connectionId: "edge_12345678",
      sampleTimestampMs: 10_000,
      sampleWindowMs: 2_000,
      rtpStatsId: "pc_12345678",
      trackIdentifier: "screen",
      state: "degraded" as const,
      reason: "bandwidth" as const,
      intervalFramesEncoded: 60,
      framesPerSecond: 30,
      bitrateKbps: 3_000,
      availableOutgoingKbps: 1_000,
      width: 1280,
      height: 720,
    };
    listener(quality);
    expect(senderQualityEvidenceFromSnapshot(snapshots.at(-1)!, 7)?.state).toBe(
      "unknown",
    );
    listener({ ...quality, sampleTimestampMs: 12_000 });
    expect(snapshots.at(-1)?.metrics).toMatchObject({
      codec: `video/${codec.toUpperCase()}`,
      codecProfile: null,
      codecParameters: null,
      powerEfficientEncoder: source ? null : codec === "h264",
      captureWidth: source ? 1920 : null,
      captureHeight: source ? 1080 : null,
      captureFramesPerSecond: null,
      mediaSourceFramesPerSecond: null,
      intervalFramesEncoded: null,
      intervalFramesSent: 60,
      framesPerSecond: 30,
      frameWidth: 1280,
      frameHeight: 720,
    });
    expect(senderQualityEvidenceFromSnapshot(snapshots.at(-1)!, 7)).toMatchObject({
      state: "degraded",
      diagnostics: {
        reason: "bandwidth",
        bitrateKbps: 3_000,
        availableOutgoingKbps: 1_000,
        captureFramesPerSecond: null,
        mediaSourceFramesPerSecond: null,
      },
    });
    listener({ ...quality, sampleTimestampMs: 14_000, intervalFramesEncoded: 0,
      framesPerSecond: 0, width: 0, height: 0, state: "unknown", reason: null });
    expect(snapshots.at(-1)?.metrics).toMatchObject({ frameWidth: null, frameHeight: null,
      captureWidth: source ? 1920 : null, captureHeight: source ? 1080 : null });
    peer.dispose();
    expect(senderQualityEvidenceFromSnapshot(snapshots.at(-1)!, 7)).toMatchObject({
      state: "unknown",
    });
  });

  it("applies the latest relay ceiling before preparing a child", async () => {
    let profile: QualitySettings = DEFAULT_QUALITY_SETTINGS;
    const control: NativeEdgeControl = {
    preparePunch: vi.fn(async () => null),
    runPunch: vi.fn(async () => false),
    closePunch: vi.fn(async () => undefined),
      updateShare: vi.fn(async () => undefined),
      prepareEdge: vi.fn(async () => ({ type: "offer" as const, sdp: "v=0\r\n" })),
      acceptSignal: vi.fn(async () => undefined),
      closeEdge: vi.fn(async () => undefined),
      onEvent: () => () => undefined,
    };
    const peer = new NativeSenderPeer(
      "viewer_123456", "edge_12345678", "share_1234567",
      { iceServers: [] }, false, control,
      { sendSignal: () => true, onUpdate: () => undefined }, "h264",
      { connectionId: "upstream_123456", getProfile: () => profile },
    );
    profile = { ...DEFAULT_QUALITY_SETTINGS, maxFramerate: 60 };
    expect(await peer.start()).toBe(true);
    expect(control.updateShare).toHaveBeenCalledWith("share_1234567", profile);
    expect(vi.mocked(control.updateShare).mock.invocationCallOrder[0])
      .toBeLessThan(vi.mocked(control.prepareEdge).mock.invocationCallOrder[0]!);
    expect(await peer.updateCaptureProfile(DEFAULT_QUALITY_SETTINGS)).toBe(true);
    expect(control.updateShare).toHaveBeenLastCalledWith("share_1234567", DEFAULT_QUALITY_SETTINGS);
    peer.dispose();
  });
});