Files
piik/src/client/pages/ControlsPreview.tsx
T
AAA 1b9f5bd2a2 feat: add room interactions and media sharing improvements
## Problem and outcome / 问题与结果

This phase adds lightweight room interaction without making media depend on chat,
and adds Windows source-audio exclusion and optional SFU-only hosting at their
existing owners. It also corrects confirmed encoder/capture ownership and native
color-conversion defects. Host microphone commentary remains on the shared media
path; room voice and 4K are outside this phase.

## Release notes

### 中文

这是一次功能更新:Piik 加入了文字聊天与表情互动,并完善了 Windows 声音采集、画面颜色和自部署选项。

#### 新功能

- **边看边聊。** 在可移动、可缩放的聊天窗口中发送消息,或开启弹幕。停止分享后,房间中的朋友仍可继续聊天。
- **表情互动。** 点击沙发上的人物,给自己发表情,或把番茄等表情送给朋友;各端按同一事件显示动画。
- **排除应用声音。** Windows App 分享整个屏幕时,可排除一个应用及其子进程的声音,适合同时使用语音软件。所选应用退出后,来源声音停止,避免恢复为未过滤的系统声音。
- **可选的纯 SFU 模式。** 自部署管理员可让共享画面和声音统一通过媒体服务器转发。默认仍为优先 P2P;App 公网邀请保持纯 P2P。
- **Windows HDR 转 SDR。** 原生采集与缩略图自动将 HDR 来源转换为 SDR,沿用现有观看链路。

#### 修复与改进

- 修复原生颜色转换的范围与矩阵处理,避免已复现的暗部丢失、高光裁切和颜色偏差。
- 修复共享编码器切换时使用过期需求或过期帧信息的问题,保护正在观看的画面;修复换源后旧采集事件可能影响新来源的问题。
- 统一新控件、浮窗与来源选择文案,补充中英文使用说明。

#### 使用与升级

聊天与表情需要新版站点页面和 Server;声音排除需要新版页面与支持此能力的 Windows App。聊天只保留本页最近收到的消息,刷新后清空,不补发历史,且不提供端到端加密。

HDR 转换输出为 SDR,不代表端到端 HDR 或多声道支持。既有房主麦克风与立体声能力保留。

### English

This feature update brings text chat and reactions to Piik, alongside improvements to Windows audio capture, picture colors and self-hosting options.

#### New features

- **Chat while watching.** Send messages in a movable, resizable window, or enable on-screen chat. Friends can keep chatting after the host stops sharing.
- **Reactions.** Select a person on the sofa to react above yourself or send a tomato and other reactions to a friend. Participants animate the same shared event.
- **Exclude application audio.** When sharing a whole screen with Windows App, exclude one application and its child processes, useful when using a separate voice app. Source audio stops if the selected app exits, preventing a return to unfiltered system sound.
- **Optional SFU-only hosting.** Site operators can require shared video and audio to pass through the media server. P2P-first remains the default; App Public invite remains P2P-only.
- **Windows HDR-to-SDR conversion.** Native capture and source thumbnails automatically convert HDR sources to SDR for the existing viewing path.

#### Fixes and improvements

- Correct native color-conversion ranges and matrices, addressing reproduced crushed shadows, clipped highlights and color shifts.
- Correct stale demand and frame evidence during shared-encoder handoffs to protect the current picture. Prevent events from a replaced capture from affecting its successor.
- Align new controls, floating panels and source-picker copy, with updated Chinese and English guides.

#### Using this update

Chat and reactions require updated site pages and Server. Audio exclusion requires updated pages and a Windows App that supports the capability. Chat retains only recent messages received by the current page, clears on refresh and does not replay history. It is not end-to-end encrypted.

HDR conversion produces SDR; it does not add end-to-end HDR or surround sound. Existing host microphone commentary and stereo support remain available.

## Verification / 验证

- Web type checks, 78 test files / 1275 tests and production build: PASS.
- Go core tests, formatting/vet and target builds: PASS; Darwin cgo remains platform-limited on this Windows runner.
- Native build/check/color probes and synthetic HDR-to-SDR H.264/VP8 browser reception: PASS.
- Access/privacy gate, Native H.264 SFU-only media gate and local Auto/H.264 Host gate: PASS.
- Three real production pages: chat/reactions, stop/resume, idle-room join, host departure/return, offline draft recovery and invitation revocation: PASS.
- Synthetic parent/child process-audio exclusion and target lifetime: PASS; no raw audio retained.
- Windows candidate archive extraction and packaged Local startup/shutdown: PASS.
- Documentation and website build: PASS.
- [Candidate CI](https://github.com/TNTcraftHIM/Piik/actions/runs/36873766646) at
  `f26cd208d03bc0dcbe353f8cac217f993216b0a3`: Go core and race checks, Web tests,
  Server/container checks and Windows/macOS/Linux App packaging all PASS.
  Publication was skipped as intended.

The owner accepted the UI/model and bounded local walkthrough for phase closure.
Physical HDR/mixed displays, mobile keyboard,
real voice applications across output devices and macOS/Linux capture remain
unverified. Existing field reports without matched evidence are not claimed
resolved. The owner authorized v1.7.0 publication on 2026-10-02. The final
documentation-only commit records that authorization; product code matches
the verified candidate above.

## Impact / 影响

Authorized version: **v1.7.0**, matching the local release planner. Current wire
contracts remain Browser/server 23, Native control 9 and capture 7. Optional
capabilities gate new functionality; existing room/media authority remains owned
by the existing services. No new ports, message persistence, room voice or
automatic room creation on page visit. Updated README, guides, standards,
research evidence and TODO stay with the candidate phase.
2026-10-02 01:36:45 +08:00

304 lines
24 KiB
TypeScript

import { useEffect, useRef, useState } from "react";
import { Btn, Chip, NameTag, Pill, SwitchItem } from "../components/living/primitives";
import { RoomChip, RoomAdmissionBadge } from "../components/living/RoomChip";
import { CaptureSourcePicker, type NativeSourceList } from "../components/living/CaptureSourcePicker";
import { LedStrip } from "../components/living/Header";
import { StageTv } from "../components/living/Stage";
import { HostMicrophone, HostMicrophoneSettings } from "../components/living/HostMicrophone";
import { RoomInteractions } from "../components/living/RoomInteractions";
import { RoomChatOverlay, RoomChatToggle } from "../components/living/RoomChatOverlay";
import { RoomInteractionSession } from "../lib/room-interactions";
import { createOpaqueId } from "../lib/opaque-id";
import type { InteractionPayload } from "../../shared/protocol";
import { REACTION_IDS, type ReactionId } from "../../shared/room-interactions";
import { PlaybackControls } from "../components/living/PlaybackControls";
import { SharingSettings } from "../components/living/SharingSettings";
import { LauncherForm, type AppMode } from "../components/living/LauncherForm";
import { useTheaterMode } from "../components/living/use-theater-mode";
import { QualityPresets } from "../components/living/QualityPresets";
import { RoomCodeInput } from "../components/living/RoomCodeInput";
import type { QualityProfileId } from "../media/quality";
import type { loadCameraPreviews } from "../media/camera-previews";
import { Tooltip } from "../components/living/Tooltip";
import { StatusIndicator } from "../components/living/StatusIndicator";
import { deriveParticipantStatus } from "../ui/media-status";
import { Glyph } from "../ui/icons";
import { useCopy } from "../ui/copy";
import { PeoplePreview } from "./PeoplePreview";
import "./controls-preview.css";
const POSTER = `data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 360"><rect width="640" height="360" fill="#e8eee6"/><rect x="100" y="60" width="440" height="240" rx="20" fill="#fffdf6"/><path d="M100 100h440" stroke="#c9d7cf" stroke-width="2"/><circle cx="128" cy="81" r="6" fill="#ed9e65"/><circle cx="320" cy="190" r="46" fill="#65b099"/><path d="m310 191 9 9 16-22" fill="none" stroke="#fffdf6" stroke-width="7" stroke-linecap="round" stroke-linejoin="round"/></svg>')}`;
const SOURCES = { kind: "ready", processAudio: true, systemAudio: true, processAudioExclusion: true, captureBorderControl: true, sources: [
{ kind: "window", sourceId: "101", pid: 1, creationTime: "1", title: "Sketchbook" },
{ kind: "window", sourceId: "102", pid: 2, creationTime: "2", title: "A short film" },
{ kind: "window", sourceId: "104", pid: 3, creationTime: "3", title: "Voice call" },
{ kind: "display", sourceId: "103", title: "Display 1" },
] } satisfies NativeSourceList;
const previewSource = async () => POSTER;
const previewCameras: typeof loadCameraPreviews = async (_signal, publish) => publish([
{ id: "built-in", label: "Built-in camera", preview: `data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 180 320"><rect width="180" height="320" fill="#dce9e5"/><rect x="106" y="26" width="58" height="102" rx="7" fill="#fbfcf3"/><path d="M135 26v102M106 76h58" stroke="#b2cbc2" stroke-width="4"/><path d="M36 255q0-100 54-100t54 100" fill="#449986"/><circle cx="90" cy="124" r="29" fill="#449986"/><path d="M81 122v4m18-4v4" stroke="#203c3a" stroke-width="5" stroke-linecap="round"/><path d="M0 259h180v61H0" fill="#d8b898"/><rect x="142" y="241" width="21" height="23" rx="5" fill="#fff7e9"/></svg>')}` },
{ id: "usb", label: "USB Camera", preview: `data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 180"><rect width="320" height="180" fill="#e2c5a8"/><path d="M0 25h320M0 157h320" stroke="#cda987" stroke-width="2"/><rect x="53" y="27" width="163" height="124" rx="5" fill="#fffaf0" transform="rotate(-8 135 89)"/><path d="m97 112 30-35 23 25 18-12 18 19" fill="none" stroke="#689b8a" stroke-width="5" stroke-linejoin="round"/><circle cx="166" cy="57" r="11" fill="#ebbd63"/><path d="m245 64 8 77" stroke="#477b73" stroke-width="7" stroke-linecap="round"/><circle cx="266" cy="35" r="19" fill="#f9f5df"/><circle cx="266" cy="35" r="12" fill="#8c6651"/></svg>')}` },
]);
const previewMicrophones = async () => [{ id: "headset", label: "USB Headset" }, { id: "desk", label: "Desk Microphone" }];
export function ControlsPreview() {
const { t, lang, vis } = useCopy();
const en = lang === "en";
const displayPreview = new URLSearchParams(window.location.search).get("source") === "display";
const [sound, setSound] = useState(true);
const [microphone, setMicrophone] = useState(false);
const [microphoneVolume, setMicrophoneVolume] = useState(1);
const [microphoneDevice, setMicrophoneDevice] = useState("");
const [sharingSettings, setSharingSettings] = useState(false);
const [launchMode, setLaunchMode] = useState<AppMode>("link");
const [launchSite, setLaunchSite] = useState("");
const [launchPassword, setLaunchPassword] = useState("");
const [launchLan, setLaunchLan] = useState("192.0.2.10");
const [preset, setPreset] = useState<QualityProfileId>("1080p30");
const [policy, setPolicy] = useState<"open" | "private">("open");
const [roomPassword, setRoomPassword] = useState(false);
const [includeCredential, setIncludeCredential] = useState(true);
const roomLink = `https://piik.example/r/9527${includeCredential ? "#v=preview" : ""}`;
const [paused, setPaused] = useState(false);
const [passwordVisible, setPasswordVisible] = useState(false);
const [name, setName] = useState("Piik friend");
const [invalid, setInvalid] = useState(false);
const [sourceOpen, setSourceOpen] = useState(displayPreview);
const [sourceState, setSourceState] = useState<NativeSourceList["kind"] | "empty">("ready");
const [captureBorderAvailable, setCaptureBorderAvailable] = useState(true);
const [showCaptureBorder, setShowCaptureBorder] = useState(false);
const [roomCode, setRoomCode] = useState("6020");
const [feedback, setFeedback] = useState("");
const [dial, setDial] = useState("60");
const [theater, setTheater] = useState(false);
const video = useRef<HTMLVideoElement>(null);
const notify = () => setFeedback(en ? "Got it. This is a preview." : "收到,只在预览里演示。" );
return <>
<div className="cp-grid" id="control-preview">
<section id="button-preview" className="cp-card">
<header><span className="cp-number">01</span><h2>{en ? "Buttons, with a little give" : "按钮,按下去有回应。"}</h2></header>
<p>{en ? "Hover, press, or use Tab and Enter. The disabled one keeps its explanation." : "悬停、按下,或用 Tab 和回车试试。禁用时也能查看原因。"}</p>
<div className="cp-tools">
<Btn icon="cast" tone="primary" title="host.start" cap="host.start" hint="hint-share-start" onClick={notify} />
<Btn icon="stop" tone="danger" title="host.stop" cap="host.stop" hint="hint-share-stop" onClick={notify} />
<Btn icon={paused ? "play" : "pause"} title={paused ? "host.resume" : "host.pause"} cap={paused ? "host.resume" : "host.pause"}
hint={paused ? "hint-resume" : "hint-pause"} draw="preview-pause" pressed={paused} onClick={() => setPaused(!paused)} />
<Btn icon="gauge" tone="on" title="host.details" cap="host.details" hint="hint-details" pressed onClick={notify} />
<Btn icon="refresh" title="viewer.reconnect" cap="viewer.reconnect" hint="hint-reconnect" disabled />
<Btn icon="cast" title="host.starting" cap="host.starting" busy disabled hint="hint-share-start" />
</div>
<div className="cp-tools">
<Btn icon="switchSource" title="host.switchSource" hint="hint-switch-source" onClick={notify} />
<Btn icon="link" title="host.invite.copy" hint="hint-copy-invite" onClick={notify} />
<Btn icon="linkOff" title="host.invite.revoke" hint="hint-revoke-invite" onClick={notify} />
<Btn icon="network" title="host.topology" hint="hint-topology" onClick={notify} />
<Btn icon="sliders" title="host.advanced" hint="hint-advanced" onClick={notify} />
</div>
<div className="lr-host-share-controls lr-media-controls" role="group" aria-label={t("host.shareControls")}>
<HostMicrophone enabled={microphone} paused={paused} volume={microphoneVolume}
onToggle={() => setMicrophone(value => !value)} />
<Btn icon={paused ? "play" : "pause"} title={paused ? "host.resume" : "host.pause"} cap={paused ? "host.resume" : "host.pause"}
hint={paused ? "hint-resume" : "hint-pause"} onClick={() => setPaused(!paused)} />
<Btn icon="switchSource" title="host.switchSource" cap="host.switchSource" hint="hint-switch-source" onClick={notify} />
<Btn icon="sliders" cap="host.settings.button" title={sharingSettings ? "host.advanced.hide" : "host.advanced"}
hint={sharingSettings ? "hint-collapse" : "hint-advanced"} tone={sharingSettings ? "on" : undefined}
expanded={sharingSettings} controls="preview-sharing-settings" onClick={() => setSharingSettings(value => !value)} />
<Btn id="host-stop-share" icon="stop" tone="danger" title="host.stop" cap="host.stop" hint="hint-share-stop" onClick={notify} />
</div>
<SharingSettings id="preview-sharing-settings" open={sharingSettings}
presets={<QualityPresets selected={preset} onSelect={setPreset} />}
audio={<HostMicrophoneSettings deviceId={microphoneDevice} onDevice={setMicrophoneDevice} loadDevices={previewMicrophones}
native enabled={microphone} disabled={paused} volume={microphoneVolume} onVolume={setMicrophoneVolume} />}
/>
</section>
<section id="option-preview" className="cp-card">
<header><span className="cp-number">02</span><h2>{en ? "Pick, toggle, adjust" : "选一个,再拨一下。"}</h2></header>
<div className="cp-tools">
<SwitchItem checked={sound} onChange={setSound} label={t("host.sourcePicker.audioOn")} hint="hint-share-audio" />
<SwitchItem checked disabled locked onChange={() => undefined} label={t("host.advanced.route.peerOnly")}
note={t("host.advanced.route.peerOnlyRequired")} hint="hint-route-p2p-required" />
<SwitchItem checked disabled locked onChange={() => undefined} label={t("host.advanced.route.sfuOnly")}
note={t("host.advanced.route.sfuOnlyHint")} hint="hint-route-sfu" />
</div>
<div className="cp-tools">
<span className="lr-toggle" role="group" aria-label={t("host.policy")} data-selected={policy}>
{(["open", "private"] as const).map(value => <Tooltip key={value} kind={value === "open" ? "hint-admission-code" : "hint-policy-private"}
text={vis ? undefined : `${t(`host.policy.${value}`)} · ${t(value === "private" && roomPassword ? "host.policy.privatePasswordHint" : `host.policy.${value}Hint`)}`}>
<button type="button" className={policy === value ? "is-selected" : undefined} aria-pressed={policy === value}
aria-label={t(`host.policy.${value}`)} onClick={() => setPolicy(value)}>
<Glyph name={value === "open" ? "globe" : "lock"} size={19} />{vis ? null : <span className="lr-cap">{t(`host.policy.${value}`)}</span>}
</button></Tooltip>)}
</span>
<SwitchItem checked={roomPassword} onChange={setRoomPassword} label={t("host.password.set")} />
</div>
<div className="cp-tools">
<RoomAdmissionBadge policy="open" passwordEnabled={false} />
<RoomAdmissionBadge policy="private" passwordEnabled />
<RoomAdmissionBadge policy="private" passwordEnabled={false} />
</div>
<div className="lr-invite-field">
<Tooltip kind="hint-invite-link" text={roomLink} className="lr-invite-hint">
<input className="lr-invite-url" readOnly dir="ltr" value={roomLink}
aria-label={t(includeCredential ? "host.invite" : "host.invite.address")} />
</Tooltip>
<span className="lr-row-group"><Glyph name="key" size={17} />
<SwitchItem checked={includeCredential} onChange={setIncludeCredential}
label={t("host.invite.includeCredential")} note={t("host.invite.credentialHint")} hint="hint-invite-link" />
</span>
</div>
{!includeCredential && policy === "private" && !roomPassword ? <Pill icon="lock" tone="warn"
comic="hint-policy-private" label={t("host.invite.credentialRequired")} /> : null}
</section>
<section id="input-preview" className="cp-card">
<header><span className="cp-number">03</span><h2>{en ? "Names, codes and small details" : "名字、房间号,还有小细节。"}</h2></header>
<div className="cp-tools"><NameTag name={name || "Piik friend"} identity="preview-name" />
<label className="lr-input"><Glyph name="pencil" size={17} /><input value={name} aria-label={t("host.name")}
autoComplete="off" onChange={event => setName(event.target.value)} /></label>
</div>
<div className="cp-tools">
<label className="lr-input is-password"><Glyph name="key" size={17} /><input type={passwordVisible ? "text" : "password"}
placeholder={t("join.password")} aria-label={t("join.password")} aria-invalid={invalid} aria-describedby={invalid ? "preview-input-error" : undefined} autoComplete="off" />
</label>
<Btn icon={passwordVisible ? "eyeOff" : "eye"} title={passwordVisible ? "host.password.hide" : "host.password.show"}
pressed={passwordVisible} hint={passwordVisible ? "hint-password-hide" : "hint-password-show"} onClick={() => setPasswordVisible(!passwordVisible)} />
<SwitchItem checked={invalid} onChange={setInvalid} label={en ? "Show error" : "看看错误态"} />
</div>
{invalid ? <p id="preview-input-error" className="cp-input-error" role="alert">{en ? "That password did not match. Try again." : "密码没对上,再试一次。"}</p> : null}
<div className="cp-tools"><RoomChip roomId={roomCode} onReplace={() => setRoomCode(code => code === "6020" ? "2048" : "6020")} /></div>
<RoomCodeInput value={dial} onChange={setDial} />
</section>
<section id="feedback-preview" className="cp-card">
<header><span className="cp-number">04</span><h2>{en ? "Clear feedback" : "每种反馈,都说清楚。"}</h2></header>
<div className="cp-tools">
<LedStrip state="live" label={t("state.signal.connected")} comic="signal-connected" />
<LedStrip state="busy" label={t("state.signal.connecting")} comic="signal-connecting" />
<LedStrip state="off" label={t("state.signal.offline")} comic="signal-offline" />
</div>
<div className="cp-tools"><Pill icon="check" tone="live" label={t("common.copied")} comic="hint-copy-code" />
<Pill icon="lock" label={t("host.advanced.route.peerOnlyRequired")} comic="hint-route-p2p-required" />
<Pill icon="alert" tone="bad" label={t("common.copyFailed")} comic="warning" /></div>
<div className="cp-tools"><StatusIndicator status={deriveParticipantStatus({ upstream: { kind: "peer", peerId: "preview" }, mediaReady: true }, true)} />
<StatusIndicator status={deriveParticipantStatus({ upstream: { kind: "none" } }, true)} />
</div>
<p className="cp-feedback" aria-live="polite">{feedback || (en ? "Button feedback appears here." : "点一下上面的按钮,回应会出现在这里。")}</p>
</section>
</div>
<PeoplePreview />
<section id="source-preview" className="cp-section">
<header className="cp-section-head"><span className="cp-number">06</span><div><h2>{en ? "Choose a picture" : "挑一块画面。"}</h2>
<p>{en ? "The actual source selector, with sample windows and cameras. Selection only updates this preview." : "正式的画面选择器,放入了示例窗口和摄像头;选择只影响这张预览。"}</p></div></header>
<div className="cp-tools">{(["ready", "loading", "unavailable", "incompatible", "unsupported", "failed", "empty"] as const).map((value, index) => <Chip key={value}
title={value} selected={sourceState === value} onClick={() => { setSourceState(value); setSourceOpen(true); }}>
{en ? ["Available", "Loading", "App unavailable", "Update needed", "Capture unavailable", "Read failed", "Empty list"][index] : ["正常", "读取中", "App 未连接", "需要更新", "无法采集", "读取失败", "空列表"][index]}</Chip>)}
<SwitchItem checked={captureBorderAvailable} onChange={setCaptureBorderAvailable}
label={en ? "Windows border control" : "Windows 边框控制"} />
<a className="lr-btn" href="/__tooltip-preview?source=display#source-preview" onClick={() => setSourceOpen(true)}>
<Glyph name="speakerOff" size={18} />{t("host.sourcePicker.excludeAudio")}
</a>
</div>
<div className="cp-stage"><StageTv hasEntry label={t("host.sourcePicker.title")}>
<img className="cp-poster" src={POSTER} alt="" />
{sourceOpen ? <CaptureSourcePicker nativeSources={sourceState === "empty" ? { ...SOURCES, sources: [], captureBorderControl: captureBorderAvailable }
: sourceState === "ready" ? { ...SOURCES, captureBorderControl: captureBorderAvailable } : { kind: sourceState }}
initialShowCaptureBorder={showCaptureBorder}
initialTab={displayPreview ? "display" : "window"}
onBrowser={() => { setSourceOpen(false); notify(); }}
onCamera={() => { setSourceOpen(false); notify(); }}
loadCameras={previewCameras}
onNative={(_target, _audio, showBorder) => { setShowCaptureBorder(showBorder); setSourceOpen(false); notify(); }}
onPreview={previewSource} onRefresh={() => setSourceState("ready")} onCancel={() => setSourceOpen(false)} />
: <div className="cp-stage-action"><Btn icon="cast" tone="primary" title="host.start" cap="host.start" hint="hint-share-start" onClick={() => setSourceOpen(true)} /></div>}
</StageTv></div>
</section>
<section id="playback-preview" className="cp-section">
<header className="cp-section-head"><span className="cp-number">07</span><div><h2>{en ? "The playback bar" : "播放时,顺手就能找到。"}</h2>
<p>{en ? "This bar shows its waiting state. The playback test page includes a moving picture, volume and picture in picture." : "这里展示等待画面时的播放栏;动态画面、音量和小窗可进入完整播放预览试用。"}</p></div></header>
<div className="cp-stage"><StageTv label={t("playback.controls")}><video ref={video} poster={POSTER} playsInline inert />
<PlaybackControls videoRef={video} stream={null} canPlay={false} theaterMode={theater} onPlay={notify}
onToggleTheater={() => setTheater(!theater)} onReconnect={notify} reconnectAvailable />
</StageTv></div>
<p><a href="/__playback-preview">{en ? "Open the full playback preview" : "进入完整播放预览"} →</a></p>
</section>
<section id="launcher-preview" className="cp-section">
<header className="cp-section-head"><span className="cp-number">08</span><h2>{t("client.launch.title")}</h2></header>
<div className="lr-client-launch">
<LauncherForm mode={launchMode} onModeChange={setLaunchMode}
site={launchSite} onSiteChange={setLaunchSite}
lan={{ selected: launchLan, onChange: setLaunchLan, addresses: [
{ address: "192.0.2.10", name: "Wi-Fi" },
{ address: "192.0.2.20", name: "vEthernet (Example Virtual Network Adapter with a Long Name)" },
] }}
localAccessPassword={launchPassword} onLocalAccessPasswordChange={setLaunchPassword}
onSubmit={event => event.preventDefault()} />
</div>
</section>
<section id="interaction-preview" className="cp-section">
<header className="cp-section-head"><span className="cp-number">09</span><div><h2>{en ? "A little company" : "聊两句,丢个番茄。"}</h2>
<p>{en ? "Click a person for reactions. Drag or resize either window." : "点小人发表情;聊天与表情窗都能拖动、缩放。"}</p></div></header>
<InteractionControlsPreview />
</section>
</>;
}
function InteractionControlsPreview() {
const [session, setSession] = useState<RoomInteractionSession | null>(null);
const [viewer, setViewer] = useState(false);
const [crowded, setCrowded] = useState(false);
const [reaction, setReaction] = useState<ReactionId>("heart");
const [theater, setTheater] = useTheaterMode();
const video = useRef<HTMLVideoElement>(null);
const { t, lang } = useCopy();
useEffect(() => {
const sender = viewer
? { peerId: "preview-friend", role: "viewer" as const, displayName: "Piik friend" }
: { peerId: "preview-host", role: "host" as const, displayName: "Piik" };
const model = new RoomInteractionSession(message => {
queueMicrotask(() => {
if (message.type === "subscribe-room-interactions") model.receive({ type: "room-interactions-ready", serverTime: Date.now() });
if (message.type === "send-room-interaction") model.receive({ type: "room-interaction", id: createOpaqueId(),
requestId: message.requestId, occurredAt: Date.now(), sender, payload: message.payload });
});
return true;
});
model.authenticated(sender.peerId);
setSession(model);
return () => model.close();
}, [viewer]);
const receiveSample = (payload: InteractionPayload) => session?.receive({
type: "room-interaction", id: createOpaqueId(), requestId: createOpaqueId(), occurredAt: Date.now(),
sender: viewer ? { peerId: "preview-host", role: "host", displayName: "Piik" }
: { peerId: "preview-friend", role: "viewer", displayName: "Piik friend" }, payload,
});
return <><div className="cp-tools">
<SwitchItem checked={viewer} onChange={setViewer} label={lang === "en" ? "Viewer perspective" : "观众视角"} />
<SwitchItem checked={crowded} onChange={setCrowded} label={lang === "en" ? "20 viewers" : "20 位观众"} />
<button type="button" className="lr-btn" onClick={() => receiveSample({
kind: "chat", text: lang === "en" ? "I'm here. Save me a seat!" : "来了,给我留个位置!",
})}><Glyph name="chat" size={18} />{lang === "en" ? "Receive a sample message" : "模拟收到消息"}</button>
<label className="lr-input cp-reaction-select"><span>{lang === "en" ? "Reaction" : "表情"}</span>
<select value={reaction} onChange={event => setReaction(event.target.value as ReactionId)}>
{REACTION_IDS.map(id => <option key={id} value={id}>{t(`interaction.reaction.${id}`)}</option>)}
</select>
</label>
<button type="button" className="lr-btn" onClick={() => receiveSample({ kind: "reaction", reaction,
targetPeerId: viewer ? "preview-friend" : "preview-host",
})}><Glyph name="smile" size={18} />{lang === "en" ? "Receive a reaction" : "模拟收到表情"}</button>
</div><div className={theater ? "lr-room is-theater" : "cp-interaction-room"}><div className="lr-scene">
<StageTv label={t("interaction.title")}>
<video ref={video} poster={POSTER} playsInline />
<RoomChatOverlay session={session} visible />
<PlaybackControls videoRef={video} stream={null} canPlay={false} onPlay={() => {}}
theaterMode={theater} onToggleTheater={() => setTheater(value => !value)} onReconnect={() => {}} reconnectAvailable={false}
extraActions={<RoomChatToggle session={session} />} />
</StageTv><RoomInteractions session={session}
view={viewer ? "viewer" : "host"} host={{ key: "preview-host", name: "Piik", you: !viewer }} entries={
Array.from({ length: crowded ? 20 : 1 }, (_, index) => ({
key: index ? `preview-friend-${index}` : "preview-friend", name: index ? `Friend ${index + 1}` : "Piik friend",
you: viewer && index === 0,
status: deriveParticipantStatus({ upstream: { kind: "peer", peerId: "preview-host" }, mediaReady: true }, true),
}))
} /></div></div></>;
}