Files
piik/internal/app/nativehost/session_test.go
T
AAA 6cc6863294 fix(media): recover H264 selection and isolate rejected ICE candidates
## Problem and outcome / 问题与结果

Windows native H.264 startup trusted the first enumerated hardware encoder. A failing GPU/driver could end sharing even when another encoder worked. The shared capture/relay selector now tries hardware candidates within its existing startup budget, verifies actual H.264 output, and retains the selected backend for later source/quality changes. Explicit H.264 never silently becomes VP8.

A browser ICE candidate rejected with `OperationError` also aborted the remaining queue and could prevent the Viewer from answering. The shared candidate receiver now records that rejection and continues; description/lifecycle errors still propagate, and transport state and the existing route deadline retain failure authority.

The launcher no longer prefills the Demo address. Site mode and its explicit origin authorization remain unchanged.

## Release notes

### 中文

这是一次兼容性修复更新,改善 Windows App 的 H.264 分享启动和浏览器的媒体连接建立。

- 首选硬件编码器无法工作时,尝试其他可用的 H.264 硬件编码器;手动选择 H.264 时仍保持该编码格式。
- 修复单条被拒绝的 ICE 候选中断后续候选处理、导致连接建立失败的问题。
- App 的已有站点地址不再预填 Demo;已保存的地址保持不变。

协议和配置格式不变,可直接升级。缺少双方诊断报告的现场连接失败仍待核实。

### English

This maintenance update improves H.264 sharing startup in the Windows App and media connection setup in browsers.

- Try other available H.264 hardware encoders when the preferred encoder cannot work. Manually selected H.264 remains H.264.
- Fix a rejected ICE candidate interrupting subsequent candidate processing and preventing connection setup.
- Stop prefilling the Demo address in App Site mode while preserving saved addresses.

Protocol and configuration formats are unchanged; no migration is required. Reported connection failures without matched diagnostics remain unconfirmed.

## Verification / 验证

- `npm run check`: 74 test files / 1,057 tests, type-check and Web build passed.
- Documentation and repository hygiene checks passed.
- `npm run check:go`: formatting, Go tests/vet, Windows/Linux core builds and Windows native regressions/compile passed. Darwin cgo packaging belongs to the native macOS CI runner.
- Real hardware: a preferred AMD encoder failed activation; the selector reached a working NVIDIA H.264 encoder without changing codec. Local native Host gate passed decoded Viewer frames, live quality changes, source replacement and cleanup.
- Browser Host/Viewer queue regressions fail before the ICE repair and pass afterward. Chrome 152 rejected an injected mismatched media identifier, then connected using valid candidates.
- External-host public invitation HTTP/WebSocket gate and native H.264 RTP delivery gate passed. The remote Pion gate establishes RTP receipt, not physical remote decoding or endurance.

## Impact / 影响

Patch release v1.6.1; no wire, persistence, route-policy or dependency changes. Browser/App/Server public compatibility is preserved. Shared media/routing owners and scoped research/TODO are updated; unknown reporter causes remain explicit.

No claim of universal H.264 driver recovery, improved NAT success on every network, or a fix for provider HTTP 1033. Site-mode redesign and 4K remain outside this phase.
2026-09-22 08:34:28 +08:00

340 lines
13 KiB
Go

package nativehost
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"os"
"strings"
"testing"
"time"
"github.com/TNTcraftHIM/Piik/internal/app/mediaedge"
"github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
)
func TestMain(tests *testing.M) {
if os.Getenv("PIIK_NATIVEHOST_PIPE_FIXTURE") == "backend" {
for _, key := range []string{"PIIK_CAPTURE_STARTING", "PIIK_CAPTURE_ACTIVE"} {
writeCaptureFixtureFrame(nativecapture.Frame{Kind: nativecapture.FrameStatus, Data: []byte(os.Getenv(key))})
}
_, _ = io.Copy(io.Discard, os.Stdin)
os.Exit(0)
}
if os.Getenv("PIIK_NATIVEHOST_PIPE_FIXTURE") == "audio-recovery" {
runAudioRecoveryCapture()
os.Exit(0)
}
if os.Getenv("PIIK_NATIVEHOST_PIPE_FIXTURE") == "echo" {
_, _ = io.Copy(os.Stdout, os.Stdin)
os.Exit(0)
}
if os.Getenv("PIIK_NATIVEHOST_PIPE_FIXTURE") == "1" {
_, _ = io.Copy(io.Discard, os.Stdin)
os.Exit(0)
}
os.Exit(tests.Run())
}
func TestCaptureCommitWaitsForReaderMetadataOrTermination(t *testing.T) {
t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "1")
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}
for _, mode := range []string{"installed", "reader-failed", "closed"} {
t.Run(mode, func(t *testing.T) {
check := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
engine, err := mediaedge.NewEngine(mediaedge.EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})
check(err)
defer engine.Close()
options := nativecapture.VideoOptions{Target: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"}, Codec: "vp8",
Profile: nativecapture.VideoProfile{Width: 1280, Height: 720, Framerate: 15, Bitrate: 2_000_000, Preference: "balanced"}, OutputGroups: 1}
previous, err := nativecapture.StartVideo(ctx, executable, options)
check(err)
defer previous.Close()
if len(previous.Outputs()) != 3 {
t.Fatal("capture did not retain its independent output group")
}
source, err := engine.NewSource("vp8", 1, 2, nil)
check(err)
defer source.Close()
check(configureCaptureOutputs(source, previous.Outputs()))
publication, err := engine.NewPublication(source, mediaedge.EdgeOptions{ConnectionID: "metadata-fixture"})
check(err)
session := &Session{ctx: ctx, cancel: cancel, engine: engine, source: source, stream: previous,
edgeCapacity: 1, ready: make(chan error, 1), done: make(chan error, 1), events: func(context.Context, Event) {}}
options.Profile.Width, options.Profile.Height = 854, 480
replacement, err := nativecapture.StartVideo(ctx, executable, options)
check(err)
defer replacement.Close()
committed := make(chan error, 1)
adapter, encoder := uint32(1), uint32(2)
go func() {
session.updateMu.Lock()
err := session.commitCapture(options, QualityProfile{Video: options.Profile, AudioBitrate: 64_000}, replacement,
CaptureState{AdapterIndex: &adapter, EncoderIndex: &encoder}, nil, false)
session.updateMu.Unlock()
committed <- err
}()
select {
case <-previous.Done():
case <-time.After(5 * time.Second):
t.Fatal("old fixture did not exit")
}
select {
case err := <-committed:
t.Fatalf("process exit acknowledged metadata before reader installation: %v", err)
default:
}
if len(publication.Media().Layers) != 2 || publication.Media().Layers[1].Width != 1280 {
t.Fatal("held reader unexpectedly changed source metadata")
}
if mode == "installed" {
// Hold the reader at its handoff, then run the same installation it uses.
check(session.installCapture(replacement))
select {
case err = <-committed:
check(err)
case <-time.After(time.Second):
t.Fatal("installed metadata did not acknowledge update")
}
media := publication.Media()
if session.videoOptions.AdapterIndex != adapter || session.videoOptions.EncoderIndex != encoder {
t.Fatal("capture replacement discarded its selected hardware backend")
}
if media.Layers[1].Width != 854 || media.Layers[1].Height != 480 || media.Layers[1].Bitrate != 2_000_000 {
t.Fatalf("acknowledged stale metadata: %+v", media)
}
return
}
go session.run()
closed := make(chan error, 1)
if mode == "closed" {
go func() { closed <- session.Close() }()
} else {
check(replacement.Close())
}
select {
case err = <-committed:
if err == nil {
t.Fatal("terminated reader acknowledged an uninstalled stream")
}
case <-time.After(5 * time.Second):
t.Fatal("capture update remained latched after termination")
}
if mode == "closed" {
select {
case err = <-closed:
check(err)
case <-time.After(5 * time.Second):
t.Fatal("Close deadlocked behind the capture update")
}
} else {
select {
case err = <-session.Done():
if err == nil {
t.Fatal("unexpected reader death did not fail the share")
}
case <-time.After(5 * time.Second):
t.Fatal("reader death did not end the share")
}
}
})
}
}
func TestCaptureProfileRetainsSelectedBackendFromStarting(t *testing.T) {
t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "backend")
t.Setenv("PIIK_CAPTURE_STARTING", string(captureStatePayload(t,
`{"state":"starting","hardwareOnly":true,"codec":"h264","adapterIndex":1,"adapterName":"GPU","adapterIdentity":"0:1","encoderIndex":2,"encoderName":"H264","encoderIdentity":"encoder"}`)))
t.Setenv("PIIK_CAPTURE_ACTIVE", string(captureStatePayload(t,
`{"state":"active","hardwareOnly":true,"codec":"h264","profileLevelId":"42c01f","width":1280,"height":720,"fps":30}`)))
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}
profile := nativecapture.VideoProfile{Width: 1280, Height: 720, Framerate: 30, Bitrate: 3_000_000, Preference: "balanced"}
stream, err := nativecapture.StartVideo(t.Context(), executable, nativecapture.VideoOptions{
Target: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"}, Codec: "h264", Profile: profile, OutputGroups: 4,
})
if err != nil {
t.Fatal(err)
}
defer stream.Close()
state, err := waitForCaptureProfile(t.Context(), stream, profile, "h264", false, false)
if err != nil {
t.Fatal(err)
}
if state.AdapterIndex == nil || *state.AdapterIndex != 1 || state.EncoderIndex == nil || *state.EncoderIndex != 2 {
t.Fatalf("active profile lost the actual hardware selection: %+v", state)
}
}
func TestOutputPlanPreservesOriginalStartupAndIndependentActivation(t *testing.T) {
t.Setenv("PIIK_NATIVEHOST_PIPE_FIXTURE", "echo")
executable, err := os.Executable()
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
stream, err := nativecapture.StartVideo(ctx, executable, nativecapture.VideoOptions{
Target: nativecapture.CaptureTarget{Kind: "display", SourceID: "1", Title: "Fixture"}, Codec: "vp8",
Profile: nativecapture.VideoProfile{Width: 1280, Height: 720, Framerate: 15,
Bitrate: 2_000_000, Preference: "balanced"}, OutputGroups: 2,
})
if err != nil {
t.Fatal(err)
}
defer stream.Close()
plan := mediaedge.OutputPlan{Active: []bool{false, true, true, false}, Bitrates: []uint32{0, 0, 250_000, 0}}
if err = applyOutputPlan(stream, plan, false); err != nil {
t.Fatal(err)
}
plan.RecoveryLayers = []int{2}
if err = applyOutputPlan(stream, plan, true); err != nil {
t.Fatal(err)
}
clear(plan.Active)
plan.RecoveryLayers = nil
if err = applyOutputPlan(stream, plan, true); err != nil {
t.Fatal(err)
}
for _, command := range []string{"B 2 250000", "K 2", "A 0 0", "A 2 1", "A 1 0", "A 2 0"} {
frame, readErr := stream.Read()
if readErr != nil || frame.Kind != nativecapture.FrameControl || string(frame.Data) != command {
t.Fatalf("expected control %q, received %+v, %v", command, frame, readErr)
}
}
}
func TestCaptureStateKeepsStartingAndActiveContractsDistinct(t *testing.T) {
starting, err := decodeCaptureState(captureStatePayload(t,
`{"state":"starting","hardwareOnly":true,"codec":"h264","adapterIndex":0,"adapterName":"GPU","adapterIdentity":"0:1","encoderIndex":0,"encoderName":"H264","encoderIdentity":"encoder"}`,
))
if err != nil || starting.State != "starting" || starting.AdapterIndex == nil {
t.Fatalf("starting = %+v, %v", starting, err)
}
active, err := decodeCaptureState(captureStatePayload(t,
`{"state":"active","hardwareOnly":true,"codec":"h264","profileLevelId":"42c01f","width":1280,"height":720,"fps":30}`,
))
if err != nil || active.State != "active" || active.ProfileLevelID != "42c01f" {
t.Fatalf("active = %+v, %v", active, err)
}
for _, payload := range []string{
`{"state":"starting","hardwareOnly":false,"codec":"vp8","adapterIndex":0,"adapterName":"GPU","adapterIdentity":"0:1","encoderName":"libvpx VP8","encoderIdentity":"libvpx/v1.17.0"}`,
`{"state":"active","hardwareOnly":false,"codec":"vp8","width":1280,"height":720,"fps":30}`,
`{"state":"active","hardwareOnly":true,"codec":"h264","profileLevelId":"42c01e","width":854,"height":480,"fps":15}`,
`{"state":"active","hardwareOnly":true,"codec":"h264","profileLevelId":"42c033","width":2560,"height":1440,"fps":60,"restoreToken":"portal-token"}`,
} {
if _, err = decodeCaptureState(captureStatePayload(t, payload)); err != nil {
t.Fatalf("valid profile state was rejected: %s: %v", payload, err)
}
}
}
func TestCaptureStateRejectsUnknownOrUnattributedState(t *testing.T) {
for _, payload := range []string{
`{"state":"active","hardwareOnly":true,"codec":"vp8","width":1280,"height":720,"fps":30}`,
`{"state":"active","hardwareOnly":false,"codec":"vp8","profileLevelId":"42c01f","width":1280,"height":720,"fps":30}`,
`{"state":"active","hardwareOnly":false}`,
`{"state":"ready","hardwareOnly":true}`,
`{"state":"active","hardwareOnly":true,"codec":"h264","extra":true}`,
} {
if _, err := decodeCaptureState(captureStatePayload(t, payload)); err == nil {
t.Fatalf("invalid state was accepted: %s", payload)
}
}
}
func TestCaptureStateUsesOriginalSlotAndBoundsPhysicalGroups(t *testing.T) {
state, err := decodeCaptureState(captureStatePayload(t,
`{"state":"active","hardwareOnly":false,"codec":"vp8","width":1280,"height":720,"fps":30}`))
if err != nil || len(state.Outputs) != 6 || state.Outputs[5] != state.Outputs[0] {
t.Fatalf("independent output groups = %+v, %v", state, err)
}
wrongOriginal := state
wrongOriginal.Width = state.Outputs[5].Width
tooMany := state
tooMany.Outputs = append(tooMany.Outputs, tooMany.Outputs[0])
for _, invalid := range []CaptureState{wrongOriginal, tooMany} {
payload, err := json.Marshal(invalid)
if err != nil {
t.Fatal(err)
}
if _, err = decodeCaptureState(payload); err == nil {
t.Fatal("invalid original slot or output count was accepted")
}
}
}
func captureStatePayload(t *testing.T, payload string) []byte {
t.Helper()
var fields map[string]json.RawMessage
if err := json.Unmarshal([]byte(payload), &fields); err != nil {
t.Fatal(err)
}
profile := nativecapture.VideoProfile{Width: 1280, Height: 720, Framerate: 30, Bitrate: 3_000_000}
for key, field := range map[string]*uint32{"width": &profile.Width, "height": &profile.Height, "fps": &profile.Framerate} {
if value, ok := fields[key]; ok {
if err := json.Unmarshal(value, field); err != nil {
t.Fatal(err)
}
}
}
slots := nativecapture.ScreenShareOutputs(profile)
for range 4 {
slots = append(slots, slots[0])
}
outputs, err := json.Marshal(slots)
if err != nil {
t.Fatal(err)
}
fields["outputs"] = outputs
result, err := json.Marshal(fields)
if err != nil {
t.Fatal(err)
}
return result
}
func TestCandidateForRetiredEdgeIsIgnored(t *testing.T) {
session := &Session{}
if err := session.AddCandidate("retired-edge", nil); err != nil {
t.Fatalf("stale candidate stopped the session: %v", err)
}
}
func TestCaptureProfileFailurePreservesCauseWithoutCredentials(t *testing.T) {
previous := slog.Default()
t.Cleanup(func() { slog.SetDefault(previous) })
var output bytes.Buffer
slog.SetDefault(slog.New(slog.NewJSONHandler(&output, &slog.HandlerOptions{Level: slog.LevelDebug})))
failure := errors.New("profile failed: hresult=0x887a0005 token=private-capture-token")
profile := nativecapture.VideoProfile{Width: 1280, Height: 720, Framerate: 30, Bitrate: 3_000_000}
if captureProfileFailure(t.Context(), profile, "wait-timeout", failure) != failure {
t.Fatal("diagnostic changed the failure result")
}
var record map[string]any
if err := json.Unmarshal(output.Bytes(), &record); err != nil {
t.Fatal(err)
}
if record["event"] != "capture-profile-rejected" || record["stage"] != "wait-timeout" ||
record["width"] != float64(1280) || record["height"] != float64(720) ||
record["fps"] != float64(30) || record["bitrate"] != float64(3_000_000) ||
!strings.Contains(output.String(), "0x887a0005") || strings.Contains(output.String(), "private-capture-token") {
t.Fatalf("profile diagnostic changed or leaked: %s", output.String())
}
}