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(web): compact host viewer connection details

AAA3695d2ab3cb1
diff --git "a/docs/\346\226\271\346\241\210\350\256\276\350\256\241.md" "b/docs/\346\226\271\346\241\210\350\256\276\350\256\241.md"
index 8c91a91f..9f3ae6d7 100644
--- "a/docs/\346\226\271\346\241\210\350\256\276\350\256\241.md"
+++ "b/docs/\346\226\271\346\241\210\350\256\276\350\256\241.md"
@@ -100,7 +100,7 @@ rotate、revoke 或从 public-watch 切回 private-link 都是当前 Viewer 的
Web Host 在 `authenticate` 中显式发送 `viewerPresence: true` capability,Viewer 也可显式订阅同一 participant snapshot;Native sender 不发送,因此继续只收到原有 exact server wire。两端可在 `authenticate.displayName` 发送名称,并以 `set-display-name` 更新当前会话。服务端只向已订阅的 Web participant 发送完整 `viewer-presence` snapshot,列表严格来自当前带 sessionId 的在线参与者,不含五秒媒体恢复 grace 成员;条目用 `role` discriminator 区分 Host/Viewer。加入、离线、会话替换、改名和 active route 改变都会重发快照;该只读广播不调用 connect/reconcile,也不建立任何媒体边。Host 的“在线”Viewer 总数只取 Viewer 条目数;最多两条本机媒体快照只补充当前以 Host 为 active upstream 的连接指标。
-每个 presence 条目包含 `role`、`peerId`、`displayName` 和服务端 active assignment 的精确 `upstream`:Host 为 `none`,Viewer 为 `none | peer(peerId) | sfu`。UI 默认只展示名称;房内出现同名时,仅为同名项追加 peerId 尾部至少六字符短后缀,并只在同名项后缀碰撞时逐字符有界延长至完整 peerId。卡片状态只依据本机连接或当前 Viewer 接收证据显示“已连接”,否则保持中性的“线路分配中”或具体连接状态;active assignment 本身不是连接成功证据。连接详情同样等待当前媒体证据,再显示绿色 `P2P` 或黄色 `SFU fallback`;实际 relay 另附 `TURN`,本地 relay protocol 可见时才显示 `TURN/UDP` 等实际值。独立拓扑按钮可从 authoritative assignment snapshot 展示 Host、实际存在的 SFU 节点及精确上下游,但不把计划线路当作连接成功。peerId 不因此成为授权身份或路由输入;名称、presence 与拓扑视图不改变授权、媒体或路由状态机。
+每个 presence 条目包含 `role`、`peerId`、`displayName` 和服务端 active assignment 的精确 `upstream`:Host 为 `none`,Viewer 为 `none | peer(peerId) | sfu`。UI 默认只展示名称;房内出现同名时,仅为同名项追加 peerId 尾部至少六字符短后缀,并只在同名项后缀碰撞时逐字符有界延长至完整 peerId。卡片状态只依据本机连接或当前 Viewer 接收证据显示“已连接”,否则保持中性的“线路分配中”或具体连接状态;active assignment 本身不是连接成功证据。连接详情同样等待当前媒体证据,再显示绿色 `P2P` 或黄色 `SFU fallback`;实际 relay 另附 `TURN`,本地 relay protocol 可见时才显示 `TURN/UDP` 等实际值。Host 观看者卡片采用两级指标披露:一级固定保留 route/transport、协议、帧率、分辨率、码率、丢包、RTT 和发送质量限制,codec/fmtp、编解码深指标、sender 参数与音频细节由卡片内的二级按钮按需展开;Viewer 自身的独立详情页保持完整指标。独立拓扑按钮可从 authoritative assignment snapshot 展示 Host、实际存在的 SFU 节点及精确上下游,但不把计划线路当作连接成功。peerId 不因此成为授权身份或路由输入;名称、presence 与拓扑视图不改变授权、媒体或路由状态机。
## 信令协议
diff --git a/src/client/components/StatsGrid.tsx b/src/client/components/StatsGrid.tsx
index 2ec40d2c..2bb4402a 100644
--- a/src/client/components/StatsGrid.tsx
+++ b/src/client/components/StatsGrid.tsx
@@ -1,3 +1,6 @@
+import { ChevronDown } from "lucide-react";
+import { useId, useState } from "react";
+
import type { ConnectionMetrics } from "../types";
import type { VideoSenderParameterReadback } from "../media/quality";
import { formatPacketLossPercent } from "./connection-details";
@@ -48,11 +51,15 @@ export function StatsGrid({
metrics,
direction,
senderParameters,
+ progressive = false,
}: {
metrics: ConnectionMetrics;
direction: "send" | "receive";
senderParameters?: VideoSenderParameterReadback | null;
+ progressive?: boolean;
}) {
+ const [secondaryExpanded, setSecondaryExpanded] = useState(false);
+ const secondaryId = useId();
const encoder = metrics.encoderImplementation
? `${metrics.encoderImplementation}${
metrics.powerEfficientEncoder === true
@@ -63,8 +70,8 @@ export function StatsGrid({
}`
: "未知";
- return (
- <dl className="stats-grid">
+ const primaryMetrics = (
+ <>
<Metric
label={direction === "send" ? "发送码率" : "接收码率"}
value={`${readableNumber(metrics.bitrateKbps)} kbps`}
@@ -76,14 +83,6 @@ export function StatsGrid({
label="视频丢包率"
value={formatPacketLossPercent(metrics.packetLossPercent)}
/>
- <Metric
- label={direction === "send" ? "可用上行" : "抖动"}
- value={
- direction === "send"
- ? `${readableNumber(metrics.availableOutgoingKbps)} kbps`
- : `${readableNumber(metrics.jitterMs, 1)} ms`
- }
- />
<Metric
label="传输协议"
value={metrics.iceProtocol?.toUpperCase() ?? "未知"}
@@ -99,6 +98,22 @@ export function StatsGrid({
value={`${metrics.localCandidateType ?? "?"} / ${metrics.remoteCandidateType ?? "?"}`}
title="本地 / 远端候选类型"
/>
+ {direction === "send" && (
+ <Metric label="质量状态" value={qualityReason(metrics.qualityLimitationReason)} />
+ )}
+ </>
+ );
+
+ const secondaryMetrics = (
+ <>
+ <Metric
+ label={direction === "send" ? "可用上行" : "抖动"}
+ value={
+ direction === "send"
+ ? `${readableNumber(metrics.availableOutgoingKbps)} kbps`
+ : `${readableNumber(metrics.jitterMs, 1)} ms`
+ }
+ />
<Metric label="视频 Codec" value={metrics.codec ?? "未知"} />
{metrics.codecProfile && (
<Metric label="视频 Codec profile token" value={metrics.codecProfile} />
@@ -150,7 +165,6 @@ export function StatsGrid({
label="最近区间编码/帧"
value={`${readableNumber(metrics.intervalEncodeMs, 1)} ms`}
/>
- <Metric label="质量状态" value={qualityReason(metrics.qualityLimitationReason)} />
{senderParameters && (
<>
<Metric
@@ -208,6 +222,43 @@ export function StatsGrid({
/>
</>
)}
- </dl>
+ </>
+ );
+
+ if (!progressive) {
+ return (
+ <dl className="stats-grid">
+ {primaryMetrics}
+ {secondaryMetrics}
+ </dl>
+ );
+ }
+
+ const toggleLabel = secondaryExpanded ? "收起更多连接指标" : "展开更多连接指标";
+ return (
+ <div className="stats-disclosure">
+ <dl className="stats-grid">{primaryMetrics}</dl>
+ <div className="stats-detail-actions">
+ <span>更多指标</span>
+ <button
+ className={`icon-button stats-detail-toggle${secondaryExpanded ? " is-expanded" : ""}`}
+ type="button"
+ title={toggleLabel}
+ aria-label={toggleLabel}
+ aria-expanded={secondaryExpanded}
+ aria-controls={secondaryId}
+ onClick={() => setSecondaryExpanded((current) => !current)}
+ >
+ <ChevronDown size={16} aria-hidden="true" />
+ </button>
+ </div>
+ <dl
+ id={secondaryId}
+ className="stats-grid stats-grid-secondary"
+ hidden={!secondaryExpanded}
+ >
+ {secondaryMetrics}
+ </dl>
+ </div>
);
}
diff --git a/src/client/pages/HostPage.tsx b/src/client/pages/HostPage.tsx
index 5cc0200c..a103dcb5 100644
--- a/src/client/pages/HostPage.tsx
+++ b/src/client/pages/HostPage.tsx
@@ -2216,6 +2216,7 @@ export function HostPage({ onAuthorizationRequired }: HostPageProps = {}) {
metrics={snapshot.metrics}
direction="send"
senderParameters={snapshot.senderParameters}
+ progressive
/>
)}
{showConnectionDetails &&
@@ -2226,6 +2227,7 @@ export function HostPage({ onAuthorizationRequired }: HostPageProps = {}) {
<StatsGrid
metrics={metricsFromQualityEvidence(qualityEvidence)}
direction="receive"
+ progressive
/>
</>
)}
diff --git a/src/client/styles.css b/src/client/styles.css
index c31e4476..51d5bda9 100644
--- a/src/client/styles.css
+++ b/src/client/styles.css
@@ -988,6 +988,52 @@ h3 {
font-variant-numeric: tabular-nums;
}
+.stats-disclosure {
+ min-width: 0;
+}
+
+.stats-detail-actions {
+ display: flex;
+ min-width: 0;
+ align-items: center;
+ gap: 8px;
+ margin-top: 9px;
+ padding-top: 6px;
+ border-top: 1px solid var(--border);
+ color: var(--faint);
+ font-size: 11px;
+}
+
+.stats-detail-actions span {
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.stats-detail-toggle.icon-button {
+ width: 32px;
+ height: 32px;
+ flex-basis: 32px;
+ margin-left: auto;
+}
+
+.stats-detail-toggle svg {
+ transition: transform 140ms ease;
+}
+
+.stats-detail-toggle.is-expanded svg {
+ transform: rotate(180deg);
+}
+
+.stats-grid-secondary {
+ margin-top: 10px;
+}
+
+.stats-grid-secondary[hidden] {
+ display: none;
+}
+
.metric {
min-width: 0;
}
@@ -1348,4 +1394,8 @@ h3 {
*::after {
scroll-behavior: auto !important;
}
+
+ .stats-detail-toggle svg {
+ transition: none;
+ }
}
diff --git a/tests/stats-grid.test.ts b/tests/stats-grid.test.ts
new file mode 100644
index 00000000..0e86943f
--- /dev/null
+++ b/tests/stats-grid.test.ts
@@ -0,0 +1,78 @@
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { describe, expect, it } from "vitest";
+
+import { StatsGrid } from "../src/client/components/StatsGrid.tsx";
+import {
+ EMPTY_METRICS,
+ type ConnectionMetrics,
+} from "../src/client/types.ts";
+
+const metrics = {
+ ...EMPTY_METRICS,
+ bitrateKbps: 4_200,
+ framesPerSecond: 59.8,
+ resolution: "1920x1080",
+ rttMs: 21,
+ packetLossPercent: 0.4,
+ availableOutgoingKbps: 8_100,
+ iceProtocol: "udp",
+ localCandidateType: "srflx",
+ remoteCandidateType: "host",
+ codec: "video/VP8",
+ codecParameters: "max-fs=8160",
+ audioBitrateKbps: 128,
+ audioCodec: "audio/opus",
+ encoderImplementation: "ExternalEncoder",
+ intervalEncodeMs: 3.2,
+ qualityLimitationReason: "bandwidth",
+} satisfies ConnectionMetrics;
+
+describe("StatsGrid progressive disclosure", () => {
+ it("keeps route-critical metrics above an accessible collapsed deep panel", () => {
+ const html = renderToStaticMarkup(
+ createElement(StatsGrid, {
+ metrics,
+ direction: "send",
+ progressive: true,
+ }),
+ );
+
+ for (const label of [
+ "发送码率",
+ "帧率",
+ "分辨率",
+ "RTT",
+ "视频丢包率",
+ "传输协议",
+ "候选路径",
+ "质量状态",
+ ]) {
+ expect(html).toContain(label);
+ }
+ expect(html).toContain('aria-expanded="false"');
+ expect(html).toContain('title="展开更多连接指标"');
+ const controlId = html.match(/aria-controls="([^"]+)"/)?.[1];
+ expect(controlId).toBeTruthy();
+ const panelStart = html.indexOf(`<dl id="${controlId}"`);
+ expect(panelStart).toBeGreaterThan(0);
+ expect(html.slice(panelStart, html.indexOf(">", panelStart))).toContain(
+ 'hidden=""',
+ );
+ expect(html.indexOf("质量状态")).toBeLessThan(panelStart);
+ expect(html.indexOf("视频 Codec")).toBeGreaterThan(panelStart);
+ expect(html.indexOf("音频发送码率")).toBeGreaterThan(panelStart);
+ expect(html.indexOf("最近区间编码/帧")).toBeGreaterThan(panelStart);
+ });
+
+ it("keeps the existing full grid when progressive disclosure is not requested", () => {
+ const html = renderToStaticMarkup(
+ createElement(StatsGrid, { metrics, direction: "send" }),
+ );
+
+ expect(html).toContain("视频 Codec");
+ expect(html).toContain("音频发送码率");
+ expect(html).not.toContain("更多指标");
+ expect(html).not.toContain("aria-expanded");
+ });
+});
diff --git a/tsconfig.tools.json b/tsconfig.tools.json
index 3bc13e99..2b184161 100644
--- a/tsconfig.tools.json
+++ b/tsconfig.tools.json
@@ -5,6 +5,7 @@
"lib": ["ES2023", "DOM", "DOM.Iterable"],
"module": "ESNext",
"moduleResolution": "bundler",
+ "jsx": "react-jsx",
"types": ["node", "vitest/globals"],
"skipLibCheck": true,
"allowImportingTsExtensions": true,