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

feat(native): expose hardware preference status

AAA37a18c88db48
diff --git a/native/sender/internal/app/server.go b/native/sender/internal/app/server.go
index 68ee1a90..f209c97d 100644
--- a/native/sender/internal/app/server.go
+++ b/native/sender/internal/app/server.go
@@ -28,7 +28,7 @@ const (
mediaConfigTimeout = 5 * time.Second
)
-//go:embed ui/index.html ui/app.js ui/styles.css
+//go:embed ui/index.html ui/app.js ui/hardware-status.js ui/styles.css
var uiFiles embed.FS
type App struct {
@@ -133,6 +133,8 @@ func (app *App) ServeHTTP(response http.ResponseWriter, request *http.Request) {
app.serveAsset(response, request, "ui/index.html", "text/html; charset=utf-8")
case "/app.js":
app.serveAsset(response, request, "ui/app.js", "text/javascript; charset=utf-8")
+ case "/hardware-status.js":
+ app.serveAsset(response, request, "ui/hardware-status.js", "text/javascript; charset=utf-8")
case "/styles.css":
app.serveAsset(response, request, "ui/styles.css", "text/css; charset=utf-8")
case "/api/start":
diff --git a/native/sender/internal/app/ui/app.js b/native/sender/internal/app/ui/app.js
index a1c110ef..4394b6b8 100644
--- a/native/sender/internal/app/ui/app.js
+++ b/native/sender/internal/app/ui/app.js
@@ -1,3 +1,9 @@
+import {
+ HARDWARE_PREFERENCE,
+ HARDWARE_STATUS,
+ retainsHardwarePreference,
+} from "./hardware-status.js";
+
const WIDTH = 1280;
const HEIGHT = 720;
const FPS = 30;
@@ -171,18 +177,22 @@ async function startEncoder(generation) {
framerate: FPS,
latencyMode: "realtime",
bitrateMode: "variable",
- hardwareAcceleration: "prefer-hardware",
+ hardwareAcceleration: HARDWARE_PREFERENCE,
};
let support = await VideoEncoder.isConfigSupported(preferred);
assertCurrentStart(generation, mediaSocket);
- metrics.hardwarePreference = "requested";
- if (!support.supported) {
+ if (!retainsHardwarePreference(support)) {
const fallback = { ...preferred };
delete fallback.hardwareAcceleration;
support = await VideoEncoder.isConfigSupported(fallback);
assertCurrentStart(generation, mediaSocket);
- metrics.hardwarePreference = "not accepted by capability check";
+ metrics.hardwarePreference = support.supported
+ ? HARDWARE_STATUS.FALLBACK
+ : HARDWARE_STATUS.UNSUPPORTED;
+ } else {
+ metrics.hardwarePreference = HARDWARE_STATUS.PREFERENCE_ACCEPTED;
}
+ metrics.hardwareEvidence = HARDWARE_STATUS.UNVERIFIED;
if (!support.supported) throw new Error("此设备不支持 VP8 720p30 编码");
let activeEncoder;
activeEncoder = new VideoEncoder({
@@ -445,7 +455,7 @@ function updateDiagnostics() {
const height = captureSettings?.height ?? "未知";
const rate = captureSettings?.frameRate ?? "未知";
elements.captureDiagnostic.textContent = `${width} × ${height} · ${rate} FPS`;
- elements.encoderDiagnostic.textContent = `VP8 · 3 Mbps 上限 · 单对象 · 硬件偏好 ${metrics.hardwarePreference}`;
+ elements.encoderDiagnostic.textContent = `VP8 · 3 Mbps 上限 · 单对象 · 硬件偏好 ${hardwarePreferenceLabel(metrics.hardwarePreference)} · 硬件证据 ${hardwareEvidenceLabel(metrics.hardwareEvidence)}`;
elements.bridgeDiagnostic.textContent = `读取 ${metrics.framesRead} · 提交 ${metrics.framesSubmitted} · 输出 ${metrics.encoderOutputs} · 编码队列丢弃 ${metrics.encoderQueueDrops} · 编码队列峰值 ${metrics.encoderQueuePeak} 帧 · socket 峰值 ${metrics.socketBufferedPeak} B`;
const helper = nativeDiagnostics?.media;
const queue = helper?.queue;
@@ -503,10 +513,24 @@ function emptyMetrics() {
encoderQueueDrops: 0,
encoderQueuePeak: 0,
socketBufferedPeak: 0,
- hardwarePreference: "未请求",
+ hardwarePreference: HARDWARE_STATUS.UNREQUESTED,
+ hardwareEvidence: HARDWARE_STATUS.UNVERIFIED,
};
}
+function hardwarePreferenceLabel(value) {
+ return {
+ [HARDWARE_STATUS.UNREQUESTED]: "未请求",
+ [HARDWARE_STATUS.PREFERENCE_ACCEPTED]: "偏好已接受",
+ [HARDWARE_STATUS.FALLBACK]: "已回退",
+ [HARDWARE_STATUS.UNSUPPORTED]: "不支持",
+ }[value] || "未知";
+}
+
+function hardwareEvidenceLabel(value) {
+ return value === HARDWARE_STATUS.UNVERIFIED ? "未验证" : "未知";
+}
+
function readableError(error, fallback) {
return error instanceof Error && error.message ? error.message : fallback;
}
diff --git a/native/sender/internal/app/ui/hardware-status.d.ts b/native/sender/internal/app/ui/hardware-status.d.ts
new file mode 100644
index 00000000..09e80644
--- /dev/null
+++ b/native/sender/internal/app/ui/hardware-status.d.ts
@@ -0,0 +1,11 @@
+export declare const HARDWARE_PREFERENCE: "prefer-hardware";
+
+export declare const HARDWARE_STATUS: Readonly<{
+ UNREQUESTED: "unrequested";
+ PREFERENCE_ACCEPTED: "preference-accepted";
+ FALLBACK: "fallback";
+ UNSUPPORTED: "unsupported";
+ UNVERIFIED: "hardware-unverified";
+}>;
+
+export declare function retainsHardwarePreference(support: unknown): boolean;
diff --git a/native/sender/internal/app/ui/hardware-status.js b/native/sender/internal/app/ui/hardware-status.js
new file mode 100644
index 00000000..84a59fa6
--- /dev/null
+++ b/native/sender/internal/app/ui/hardware-status.js
@@ -0,0 +1,14 @@
+export const HARDWARE_PREFERENCE = "prefer-hardware";
+
+export const HARDWARE_STATUS = Object.freeze({
+ UNREQUESTED: "unrequested",
+ PREFERENCE_ACCEPTED: "preference-accepted",
+ FALLBACK: "fallback",
+ UNSUPPORTED: "unsupported",
+ UNVERIFIED: "hardware-unverified",
+});
+
+export function retainsHardwarePreference(support) {
+ return support?.supported === true &&
+ support.config?.hardwareAcceleration === HARDWARE_PREFERENCE;
+}
diff --git a/tests/native-hardware-status.test.ts b/tests/native-hardware-status.test.ts
new file mode 100644
index 00000000..09ff2a9d
--- /dev/null
+++ b/tests/native-hardware-status.test.ts
@@ -0,0 +1,44 @@
+import { readFileSync } from "node:fs";
+import { join } from "node:path";
+
+import { describe, expect, it } from "vitest";
+
+import {
+ HARDWARE_PREFERENCE,
+ retainsHardwarePreference,
+} from "../native/sender/internal/app/ui/hardware-status.js";
+
+const senderUI = readFileSync(
+ join(import.meta.dirname, "../native/sender/internal/app/ui/app.js"),
+ "utf8",
+);
+const senderServer = readFileSync(
+ join(import.meta.dirname, "../native/sender/internal/app/server.go"),
+ "utf8",
+);
+
+describe("native sender hardware preference contract", () => {
+ it("accepts only a retained prefer-hardware capability result", () => {
+ expect(retainsHardwarePreference({
+ supported: true,
+ config: { hardwareAcceleration: HARDWARE_PREFERENCE },
+ })).toBe(true);
+ expect(retainsHardwarePreference({ supported: true, config: {} })).toBe(false);
+ expect(retainsHardwarePreference({ supported: false, config: {
+ hardwareAcceleration: HARDWARE_PREFERENCE,
+ } })).toBe(false);
+ expect(retainsHardwarePreference(null)).toBe(false);
+ });
+
+ it("keeps VP8, one encoder, and an explicit unverified hardware boundary", () => {
+ expect(senderUI).toContain('codec: "vp8"');
+ expect(senderUI).toContain("encoderInstances: 1");
+ expect(senderUI).toContain("HARDWARE_STATUS.PREFERENCE_ACCEPTED");
+ expect(senderUI).toContain("HARDWARE_STATUS.FALLBACK");
+ expect(senderUI).toContain("HARDWARE_STATUS.UNVERIFIED");
+ expect(senderUI).toContain("delete fallback.hardwareAcceleration");
+ expect(senderUI).not.toContain('hardwareAcceleration: "prefer-software"');
+ expect(senderServer).toContain("ui/hardware-status.js");
+ expect(senderServer).toContain("/hardware-status.js");
+ });
+});