Files
piik/internal/app/nativecontrol/session_test.go
T
AAA d76e94eb71 feat(app): improve capture controls and sharing feedback
## Problem and outcome / 问题与结果

Windows App users can now choose whether to show the capture border. The same border owner handles source previews and active capture; unsupported or denied requests keep normal capture available. The source picker distinguishes an absent/incompatible App, unavailable capture capability, failed enumeration and a genuinely empty list. Chrome's pending local-access permission no longer holds optional Viewer reception.

This phase also reduces tooltip obstruction and contains a failed Linux encoder output within its own branch. Browser/server v23, Native control v9 and capture v7 remain compatible. Closes #401.

## Release notes

### 中文

这是一次分享体验更新,重点改进 Windows 采集边框、App 选源反馈和提示浮窗,并修复 Linux 采集输出的故障隔离。

- **采集边框可选。** Windows App 选源器新增「显示采集边框」,位于声音开关左侧,默认关闭。支持的系统上,来源预览也会隐藏边框。
- **找不到窗口时,提示更明确。** 区分 App 未连接、版本不兼容、采集不可用和读取失败;授权后可以刷新列表重试。改善 Chrome 首次访问时的观看连接,避免本机授权等待拖慢入座。
- **提示浮窗更顺手。** 统一 500 毫秒悬停延迟,减少对画面和控件的遮挡,改善触屏长按和关闭操作;只用于说明的控件使用普通鼠标光标。
- **Linux 分享更稳。** 修复单路编码输出失败可能连带中断其他输出的问题。
- **排查文档更完整。** 补充 App 来源列表、Windows 10 黄框及浏览器 WebRTC 设置的排查说明。

使用新的采集边框开关,请更新 Windows App 并刷新已打开的网页。Windows 10 没有对应的系统接口,边框仍由系统显示;文档中心提供可选处理方法。系统权限或其他采集会话也可能要求保留边框。

### English

This sharing update improves Windows capture borders, App source selection and tooltips, and fixes failure isolation between Linux capture outputs.

- **Choose whether to show the capture border.** The Windows App source picker adds a **Show capture border** switch to the left of audio, off by default. Source previews also hide the border on supported systems.
- **Clearer source-selection feedback.** The picker distinguishes an unavailable or incompatible App, unsupported capture and a failed source list. Refresh after granting access to retry. Pending local-access permission in Chrome no longer delays Browser reception for Viewers.
- **Less intrusive tooltips.** A consistent 500 ms hover delay, improved placement and touch-hold behavior make guidance easier to read and dismiss. Help-only controls use the ordinary cursor.
- **More resilient Linux sharing.** A failed encoder output no longer interrupts healthy sibling outputs.
- **More useful troubleshooting.** The documentation covers App source lists, Windows 10 capture borders and browser WebRTC settings.

Update the Windows App and refresh open pages to use the capture-border switch. Windows 10 lacks the corresponding system API and retains its system border; an optional procedure is available in the documentation center. System permission or another capture session may also require the border to remain visible.

## Verification / 验证

- `npm run check`: TypeScript, 998 tests and production Web build passed.
- `npm run check:go`: Go formatting/tests/vet/target builds and Windows native capture checks passed. macOS cgo builds require their native CI runner.
- Documentation, repository hygiene and website build passed.
- Real Chrome 152 profiles: first-use local permission, recovery after consent and source-list refresh; optional Viewer discovery before/after consent; App Local without cross-address-space consent.
- Public invitation gate: link creation, remote page and WebSocket access, App exit and link retirement passed.
- Windows 11 border on/off, source preview and concurrent-capture checks have physical evidence. Tooltip hover, keyboard, touch-hold, dismissal and narrow-layout checks passed.
- [Candidate CI](https://github.com/TNTcraftHIM/Piik/actions/runs/35157993032) passed at `1d159831`: Server/container, Windows/Linux/macOS packages, Go race checks and frontend validation. Publication was skipped.
- Local Windows ZIP passed packaged startup, capture probing, embedded Web, helper identity/notices and clean-shutdown checks.
- Windows H.264 and VP8 Host gates passed live/paused settings, background recovery, source replacement, source-end/restart, decoded Viewer frames and complete resource cleanup.
- Final `0b370e6a` differs from the CI candidate only in TODO/status closure. Full Web/Go/native checks and website build passed again. The access/privacy gate passed credential-leak and cleanup checks.

## Impact / 影响

Compatible feature update for **v1.5.0**. No wire or storage version change. Older Apps retain their existing capture behavior through capability negotiation. Publication and matching deployments follow the release workflow.

The independent Windows x86 experiment is excluded. Physical 32-bit Windows, macOS/Linux capture, unsupported Windows border control, and actual border-permission denial remain outside this candidate's acceptance evidence. The original missing-window, public-link timeout and URL-handler crash reports still need affected-machine diagnostics.
2026-09-17 08:29:15 +08:00

499 lines
19 KiB
Go

package nativecontrol
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"io"
"log/slog"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"testing"
"time"
"github.com/TNTcraftHIM/Piik/internal/app/loopback"
"github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
"github.com/TNTcraftHIM/Piik/internal/app/nativehost"
)
func TestMain(tests *testing.M) {
if marker := os.Getenv("PIIK_SOURCE_PROBE_FIXTURE"); marker != "" {
if err := os.WriteFile(marker, nil, 0600); err != nil {
os.Exit(1)
}
time.Sleep(10 * time.Second)
os.Exit(0)
}
if directory := os.Getenv("PIIK_QUIET_CAPTURE_FIXTURE"); directory != "" {
runQuietCaptureFixture(directory)
os.Exit(0)
}
os.Exit(tests.Run())
}
func runQuietCaptureFixture(directory string) {
arguments, _ := json.Marshal(os.Args[1:])
if err := os.WriteFile(filepath.Join(directory, "arguments.json"), arguments, 0600); err != nil {
os.Exit(1)
}
var state nativehost.CaptureState
state.State, state.Codec = "starting", "vp8"
var adapter uint32
state.AdapterIndex = &adapter
state.AdapterName, state.AdapterIdentity, state.EncoderName, state.EncoderIdentity = "fixture", "fixture", "fixture", "fixture"
number := func(value string) uint32 { parsed, _ := strconv.ParseUint(value, 10, 32); return uint32(parsed) }
for index := 1; index < len(os.Args)-1; index++ {
switch os.Args[index] {
case "--width":
state.Width = number(os.Args[index+1])
case "--height":
state.Height = number(os.Args[index+1])
case "--fps":
state.FPS = number(os.Args[index+1])
case "--output":
state.Outputs = append(state.Outputs, nativecapture.OutputProfile{Width: number(os.Args[index+1]),
Height: number(os.Args[index+2]), Framerate: number(os.Args[index+3]), Bitrate: number(os.Args[index+4])})
}
}
write := func(kind nativecapture.FrameKind, payload []byte, timestamp uint64) {
var header [32]byte
copy(header[:], "SMED")
header[4], header[5] = 2, byte(kind)
if kind == nativecapture.FrameBegin {
binary.BigEndian.PutUint64(header[8:16], timestamp)
binary.BigEndian.PutUint64(header[16:24], 333_333)
}
binary.BigEndian.PutUint32(header[28:32], uint32(len(payload)))
_, _ = os.Stdout.Write(header[:])
_, _ = os.Stdout.Write(payload)
}
status := func() { payload, _ := json.Marshal(state); write(nativecapture.FrameStatus, payload, 0) }
status()
closed := make(chan struct{})
go func() { _, _ = io.Copy(io.Discard, os.Stdin); close(closed) }()
if state.Width == 1280 {
_ = os.WriteFile(filepath.Join(directory, "prepared"), nil, 0600)
ticker := time.NewTicker(20 * time.Millisecond)
defer ticker.Stop()
var input uint64
for {
select {
case <-closed:
return
case <-ticker.C:
value, _ := os.ReadFile(filepath.Join(directory, "release"))
if string(value) == "active" {
write(nativecapture.FrameBegin, nil, input+333_333)
state.State = "active"
status()
<-closed
return
}
if string(value) == "input" {
input += 333_333
write(nativecapture.FrameBegin, nil, input)
}
}
}
}
state.State = "active"
status()
<-closed
}
func TestCaptureBorderPreferenceFollowsSourceAndProfile(t *testing.T) {
for _, supported := range []bool{false, true} {
t.Run(strconv.FormatBool(supported), func(t *testing.T) {
directory := t.TempDir()
t.Setenv("PIIK_QUIET_CAPTURE_FIXTURE", directory)
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}
session := New(executable, nativecapture.Capabilities{VideoCapture: true, SoftwareVP8: true, CaptureBorderControl: supported}, false)
t.Cleanup(func() { _ = session.Close() })
handle := func(request any) {
t.Helper()
payload, err := json.Marshal(request)
if err != nil {
t.Fatal(err)
}
value, err := session.Handle(t.Context(), payload)
if _, failed := value.(requestFailedResponse); err != nil || failed {
t.Fatalf("capture request failed: %v, %#v", err, value)
}
}
assertBorder := func(expected bool) {
t.Helper()
data, err := os.ReadFile(filepath.Join(directory, "arguments.json"))
var arguments []string
if err != nil || json.Unmarshal(data, &arguments) != nil {
t.Fatalf("capture arguments missing: %v", err)
}
if slices.Contains(arguments, "--show-capture-border") != expected {
t.Fatalf("capture border intent lost: %v", arguments)
}
}
start := startShareRequest{Version: 9, ID: "request_start", Type: "start-share", ShareID: "share_123456",
Source: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"},
ShowCaptureBorder: true, EdgeCapacity: 1, Codec: "vp8",
Profile: qualitySettings{Resolution: "1080p", MaxFramerate: 30, MaxBitrate: 5_000_000, DegradationPreference: "balanced"}}
handle(start)
assertBorder(supported)
profile := start.Profile
profile.Resolution = "480p"
handle(updateShareRequest{Version: 9, ID: "request_update", Type: "update-share", ShareID: start.ShareID, Profile: profile})
session.mu.Lock()
done := session.updateDone
session.mu.Unlock()
if done != nil {
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatal("profile update did not settle")
}
}
assertBorder(supported)
for _, show := range []bool{false, true} {
handle(replaceShareSourceRequest{Version: 9, ID: "request_replace", Type: "replace-share-source", ShareID: start.ShareID,
Source: nativecapture.CaptureTarget{Kind: "display", SourceID: "2", Title: "Other fixture"}, ShowCaptureBorder: show})
assertBorder(show && supported)
}
handle(stopShareRequest{Version: 9, ID: "request_stop", Type: "stop-share", ShareID: start.ShareID})
})
}
}
func TestQuietHostUpdatePreservesControlAndCancelsCleanly(t *testing.T) {
for _, outcome := range []string{"complete", "stop", "disconnect", "first-input-timeout", "backpressure-stop"} {
t.Run(outcome, func(t *testing.T) {
var trace bytes.Buffer
if outcome == "complete" {
previous := slog.Default()
t.Cleanup(func() { slog.SetDefault(previous) })
slog.SetDefault(slog.New(slog.NewJSONHandler(&trace, &slog.HandlerOptions{Level: slog.LevelDebug})))
}
directory := t.TempDir()
t.Setenv("PIIK_QUIET_CAPTURE_FIXTURE", directory)
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}
session := New(executable, nativecapture.Capabilities{SoftwareVP8: true}, false)
t.Cleanup(func() { _ = session.Close() })
handle := func(payload string) any {
t.Helper()
value, err := session.Handle(t.Context(), []byte(payload))
if err != nil {
t.Fatal(err)
}
return value
}
const start = `{"version":9,"id":"request_start","type":"start-share","shareId":"share_123456","source":{"kind":"display","sourceId":"1","title":"Fixture"},"audio":false,"adapterIndex":0,"encoderIndex":0,"edgeCapacity":1,"codec":"vp8","profile":{"resolution":"1080p","maxFramerate":30,"maxBitrate":5000000,"degradationPreference":"balanced"}}`
handle(start)
const update = `{"version":9,"id":"request_update","type":"update-share","shareId":"share_123456","profile":{"resolution":"720p","maxFramerate":30,"maxBitrate":3000000,"degradationPreference":"balanced"}}`
if value := handle(update); value != nil {
t.Fatalf("Host update did not defer: %#v", value)
}
session.mu.Lock()
done := session.updateDone
session.mu.Unlock()
deadline := time.Now().Add(2 * time.Second)
for {
if _, err = os.Stat(filepath.Join(directory, "prepared")); err == nil {
break
}
if time.Now().After(deadline) {
t.Fatal("replacement did not reach its starting state")
}
time.Sleep(10 * time.Millisecond)
}
for _, payload := range []string{
strings.Replace(update, "request_update", "request_duplicate", 1),
`{"version":9,"id":"request_replace","type":"replace-share-source","shareId":"share_123456","source":{"kind":"display","sourceId":"1","title":"Fixture"},"audio":false,"adapterIndex":0,"encoderIndex":0}`,
} {
if _, ok := handle(payload).(requestFailedResponse); !ok {
t.Fatal("conflicting capture mutation was not rejected")
}
}
for _, payload := range []string{
`{"version":9,"id":"request_pause","type":"pause-share","shareId":"share_123456","paused":true}`,
`{"version":9,"id":"request_ice","type":"edge-candidate","shareId":"share_123456","connectionId":"retired_edge","candidate":null}`,
} {
started := time.Now()
handle(payload)
if time.Since(started) > time.Second {
t.Fatal("quality preparation blocked control")
}
}
switch outcome {
case "complete":
select {
case <-done:
t.Fatal("quiet source update ended before a frame arrived")
case <-time.After(5200 * time.Millisecond):
}
if err = os.WriteFile(filepath.Join(directory, "release"), []byte("active"), 0600); err != nil {
t.Fatal(err)
}
case "first-input-timeout":
if err = os.WriteFile(filepath.Join(directory, "release"), []byte("input"), 0600); err != nil {
t.Fatal(err)
}
case "backpressure-stop":
fill:
for {
select {
case session.events <- struct{}{}:
default:
break fill
}
}
if err = os.WriteFile(filepath.Join(directory, "release"), []byte("active"), 0600); err != nil {
t.Fatal(err)
}
case "stop":
handle(`{"version":9,"id":"request_stop","type":"stop-share","shareId":"share_123456"}`)
if _, ok := handle(strings.ReplaceAll(start, "share_123456", "share_restart")).(shareStartedResponse); !ok {
t.Fatal("stopped update blocked a new share")
}
case "disconnect":
if err = session.Close(); err != nil {
t.Fatal(err)
}
}
select {
case <-done:
case <-time.After(6 * time.Second):
t.Fatal("update did not finish after input or cancellation")
}
if outcome == "disconnect" {
return
}
if outcome == "backpressure-stop" {
stopped := make(chan any, 1)
go func() {
value, err := session.Handle(t.Context(), []byte(`{"version":9,"id":"request_stop","type":"stop-share","shareId":"share_123456"}`))
if err != nil {
stopped <- err
} else {
stopped <- value
}
}()
select {
case value := <-stopped:
if _, ok := value.(responseEnvelope); !ok {
t.Fatalf("backpressured stop failed: %#v", value)
}
case <-time.After(2 * time.Second):
t.Fatal("completion backpressure blocked stop")
}
return
}
ctx, cancel := context.WithTimeout(t.Context(), time.Second)
defer cancel()
responseFor := func(id string) any {
for {
select {
case value := <-session.Events():
if response, ok := value.(shareUpdatedResponse); ok && response.ID == id {
return value
}
if response, ok := value.(requestFailedResponse); ok && response.ID == id {
return value
}
case <-ctx.Done():
t.Fatal("deferred update lost its response")
}
}
}
_, success := responseFor("request_update").(shareUpdatedResponse)
if success != (outcome == "complete") {
t.Fatal("deferred response did not match capture outcome")
}
if outcome == "complete" {
if value := handle(strings.Replace(update, "request_update", "request_followup", 1)); value != nil {
t.Fatal("acknowledged update still blocked its successor")
}
if _, ok := responseFor("request_followup").(shareUpdatedResponse); !ok {
t.Fatal("later update could not complete")
}
_ = session.Close()
var started, ended int
decoder := json.NewDecoder(&trace)
for decoder.More() {
var record map[string]any
if err := decoder.Decode(&record); err != nil {
t.Fatal(err)
}
if record["requestId"] != "request_update" {
continue
}
switch record["event"] {
case "native-request-started":
started++
case "native-request-ended":
ended++
if record["durationMs"].(float64) < 5_000 || record["failed"] != false || record["share"] == "share_123456" {
t.Fatalf("asynchronous request lost duration, outcome or private identity handling: %+v", record)
}
}
}
if started != 1 || ended != 1 {
t.Fatalf("async update logged %d starts and %d completions", started, ended)
}
}
})
}
}
func TestSTUNURLsUseTheCurrentBoundedWire(t *testing.T) {
servers, err := pionICEServers([]iceServer{{URLs: []string{
"stun:example.test:3478",
"stun:example.test:3479",
}}})
if err != nil || len(servers) != 1 || len(servers[0].URLs) != 2 {
t.Fatalf("servers = %+v, %v", servers, err)
}
for _, value := range []string{
"turn:example.com:3478",
"stun:user@example.com:3478",
"stun:example.com:0",
"stun:example.com/path",
} {
if validSTUNURL(value) {
t.Fatalf("invalid STUN URL accepted: %s", value)
}
}
}
func TestControlMessagesRejectUnknownFieldsAndStaleVersions(t *testing.T) {
session := New("missing-capture-process", nativecapture.Capabilities{}, false)
t.Cleanup(func() { _ = session.Close() })
for _, payload := range []string{
`{"version":9,"id":"request_sources","type":"list-sources","extra":true}`,
`{"version":4,"id":"request_sources","type":"list-sources"}`,
`{"version":5,"id":"short","type":"stop-share","shareId":"share_123456"}`,
} {
if _, err := session.Handle(t.Context(), []byte(payload)); err == nil {
t.Fatalf("invalid control message accepted: %s", payload)
}
}
}
func TestResponseKeepsTheRequestIdentity(t *testing.T) {
value := response(requestEnvelope{
Version: loopback.ProtocolVersion,
ID: "request_123456",
Type: "stop-share",
}, "share-stopped")
if value.Version != loopback.ProtocolVersion || value.ID != "request_123456" ||
value.Type != "share-stopped" {
t.Fatalf("response = %+v", value)
}
failed := operationFailure(requestEnvelope{
Version: loopback.ProtocolVersion, ID: "request_update", Type: "update-share",
})
if failed.Version != loopback.ProtocolVersion || failed.ID != "request_update" ||
failed.Type != "request-failed" || failed.Code != "operation-failed" {
t.Fatalf("operation failure lost its request identity: %+v", failed)
}
}
func TestPrepareLocalEdgeOwnsOneStrictRequestShape(t *testing.T) {
session := New("missing-capture-process", nativecapture.Capabilities{}, false)
t.Cleanup(func() { _ = session.Close() })
valid := `{"version":9,"id":"request_local_edge","type":"prepare-local-edge","shareId":"share_123456","connectionId":"edge_1234567"}`
assertOperationFailure(t, session, valid)
invalid := `{"version":9,"id":"request_local_edge","type":"prepare-local-edge","shareId":"share_123456","connectionId":"edge_1234567","iceServers":[]}`
if _, err := session.Handle(t.Context(), []byte(invalid)); err == nil || err.Error() != "native prepare-local-edge request is invalid" {
t.Fatalf("extended local-edge request was accepted: %v", err)
}
}
func TestPreviewFailureReturnsAnAdvisoryResponse(t *testing.T) {
session := New("missing-capture-process", nativecapture.Capabilities{}, false)
t.Cleanup(func() { _ = session.Close() })
value, err := session.Handle(t.Context(), []byte(
`{"version":9,"id":"request_preview","type":"source-preview","source":{"kind":"display","sourceId":"65537","title":"Display 1"}}`,
))
if err != nil {
t.Fatal(err)
}
response, ok := value.(sourcePreviewResponse)
if !ok || response.Data != "" || response.SourceKey != "display:65537" {
t.Fatalf("preview response = %#v", value)
}
}
func TestSourceEnumerationFailureDoesNotCloseControl(t *testing.T) {
session := New("missing-capture-process", nativecapture.Capabilities{}, false)
t.Cleanup(func() { _ = session.Close() })
value, err := session.Handle(t.Context(), []byte(
`{"version":9,"id":"request_sources","type":"list-sources"}`,
))
if err != nil || value != operationFailure(requestEnvelope{
Version: loopback.ProtocolVersion, ID: "request_sources", Type: "list-sources",
}) {
t.Fatalf("source list failure ended its control session: %#v, %v", value, err)
}
if _, err := session.Handle(t.Context(), []byte(
`{"version":9,"id":"request_options","type":"capture-options"}`,
)); err != nil {
t.Fatalf("control was unusable after source enumeration failed: %v", err)
}
}
func TestQualitySettingsMapOnceIntoNativeMedia(t *testing.T) {
settings := qualitySettings{
Resolution: "1440p", MaxFramerate: 60, MaxBitrate: 12_000_000,
DegradationPreference: "maintain-framerate",
ScreenAudioQuality: "very-high",
}
profile := nativeQualityProfile(settings)
if !validQualitySettings(settings) || profile.Video.Width != 2560 ||
profile.Video.Height != 1440 || profile.Video.Framerate != 60 ||
profile.Video.Bitrate != 12_000_000 ||
profile.Video.Preference != "maintain-framerate" ||
profile.AudioBitrate != 192_000 {
t.Fatalf("native profile = %+v", profile)
}
settings.Resolution = "2160p"
if validQualitySettings(settings) {
t.Fatal("unsupported resolution was accepted")
}
}
func TestUpdateShareRequiresTheCurrentStrictProfile(t *testing.T) {
session := New("missing-capture-process", nativecapture.Capabilities{}, false)
t.Cleanup(func() { _ = session.Close() })
valid := `{"version":9,"id":"request_update","type":"update-share","shareId":"share_123456","profile":{"resolution":"1080p","maxFramerate":30,"maxBitrate":5000000,"degradationPreference":"balanced","screenAudioQuality":"music"}}`
assertOperationFailure(t, session, valid)
invalid := `{"version":9,"id":"request_update","type":"update-share","shareId":"share_123456","profile":{"resolution":"1080p","maxFramerate":30,"maxBitrate":5000000,"degradationPreference":"balanced","screenAudioQuality":"music","extra":true}}`
if _, err := session.Handle(t.Context(), []byte(invalid)); err == nil ||
err.Error() != "native update-share request is invalid" {
t.Fatalf("extended update was accepted: %v", err)
}
}
func TestReplaceShareSourceKeepsOneStrictTargetShape(t *testing.T) {
session := New("missing-capture-process", nativecapture.Capabilities{}, false)
t.Cleanup(func() { _ = session.Close() })
valid := `{"version":9,"id":"request_source","type":"replace-share-source","shareId":"share_123456","source":{"kind":"picker","sourceId":"1","title":"Portal"},"audio":false,"adapterIndex":0,"encoderIndex":0}`
assertOperationFailure(t, session, valid)
invalid := `{"version":9,"id":"request_source","type":"replace-share-source","shareId":"share_123456","source":{"kind":"picker","sourceId":"1","title":"Portal"},"audio":false,"adapterIndex":0,"encoderIndex":0,"extra":true}`
if _, err := session.Handle(t.Context(), []byte(invalid)); err == nil ||
err.Error() != "native replace-share-source request is invalid" {
t.Fatalf("extended source replacement was accepted: %v", err)
}
invalid = strings.Replace(valid, `"kind":"picker"`, `"kind":"unknown"`, 1)
if _, err := session.Handle(t.Context(), []byte(invalid)); err == nil ||
err.Error() != "native replace-share-source request is invalid" {
t.Fatalf("malformed target was treated as an operational failure: %v", err)
}
}