Files
Piik/internal/app/loopback/server_test.go
T
AAA 89a03e6470 fix(app): improve capture selection and media recovery (#433)
## Problem and outcome / 问题与结果

This maintenance phase fixes failures around native capture startup, room reconnection and media handoffs. Capture mutations stay cancellable, relayed-content failures remain request-scoped, and failed media connections return to the existing route controller. Windows Auto codec selection measures delivered cadence instead of timer-sensitive per-frame work estimates.

The phase preserves P2P-first routing, one committed graph and one route operation. It adds no signaling replay mechanism, changes no wire/schema version, and includes no interaction prototype or 4K feature work.

## Release notes

### 中文

Piik v1.6.8 是一次稳定性修复,重点改善 Windows 原生采集、断线恢复和分享过程中的控制响应。

#### 修复与改善

- **更准确地选择编码器。** Windows 的自动模式按实际出帧表现评估 H.264,并正确区分启动预热和持续性能不足,减少计时误差导致的不必要 VP8 回退。硬件编码事件改为异步等待。
- **改善断线与线路切换。** 房间信令短暂断开后保留观众成员身份 20 秒;修复线路切换、SFU 订阅恢复及声音重协商卡住的问题。失效连接及时交回现有路由控制器处理,原生 SFU 发送端不再把短暂断连直接判为终止。
- **保持 App 控制响应。** 开始分享和更换来源时仍可取消操作;合法消息突发不再直接关闭控制连接。中继信令的单条内容错误不会连带结束整场原生分享。
- **修正设备列表与配置保存。** 来源或麦克风数量达到上限时保留可用列表;配置替换失败时保留原有设置。
- **改善紧凑布局和辅助功能。** 调整手机横屏下的画面与播控布局、画质预设标签和长邀请链接显示,修正部分提示与辅助功能语义。
- **加强站点访问保护。** 站点登录 Cookie 使用独立随机签名密钥,应用自身阻止被其他页面嵌套。发布说明也会在合并前检查。

#### 升级说明

通信协议和持久化格式保持不变。设置了站点密码时,App 或 Server 重启后需要重新输入站点密码;持久化房间和邀请授权不受这项 Cookie 调整影响。Piik 操作页面不再支持嵌入 iframe。

### English

Piik v1.6.8 is a stability update focused on Windows native capture, connection recovery and responsive sharing controls.

#### Fixes and improvements

- **More accurate codec selection.** Windows Auto mode evaluates H.264 using delivered frames and distinguishes startup warmup from sustained performance limits, reducing unnecessary VP8 fallback caused by timing noise. Hardware encoder events now use asynchronous waiting.
- **Improved reconnection and route handoffs.** Viewer membership survives brief signaling outages for 20 seconds. Fixes cover route activation, SFU subscription recovery and stalled audio renegotiation. Failed connections return to the existing route controller, and the native SFU publisher no longer treats a transient disconnect as immediate termination.
- **Responsive App controls.** Starting a share and replacing its source remain cancellable. Valid message bursts no longer close the control connection, and an invalid relayed payload cannot terminate the entire native sharing session.
- **Device lists and saved settings.** Source and microphone enumeration retain a usable list at their limits. Failed configuration replacement preserves the previous settings.
- **Compact layouts and accessibility.** Improved picture and playback-control layout on landscape phones, quality preset labels and long invitation links, with corrected tooltip and accessibility semantics.
- **Stronger site-access protection.** Login cookies use an independent random signing key, and the application prevents embedding by other pages. Release notes are also checked before merge.

#### Upgrade notes

Wire protocols and persistent formats are unchanged. If a site password is configured, restarting the App or Server requires entering it again; this cookie change does not revoke persisted rooms or invitation grants. Piik operation pages can no longer be embedded in an iframe.

## Verification / 验证

- `npm run check`: passed, 76 test files / 1,171 tests, TypeScript and production Web build.
- `npm run check:go-core`: passed; full Go/native checks also passed during integration of the capture changes. macOS cgo builds remain native-runner checks.
- Windows native Host gates: Auto, H.264 and VP8 passed during the phase; final control changes rechecked with Auto, including live/paused quality, source replacement, failure retirement and subsequent sharing.
- Embedded SFU browser VP8 recovery: passed with production Host/Viewer route owners.
- Embedded SFU native H.264, two rooms: passed, including recovered decoded video/audio, independent room retirement and resource cleanup.
- Documentation, repository hygiene and diff checks: passed. Final follow-up changes only align operations documentation with the PR note check.
- Go race checks could not run locally because no cgo/GCC toolchain is available; the existing Linux CI race job remains required. AMD/Intel hardware, broader device combinations, public-NAT field reports and long endurance are not established by these local checks.

## Impact / 影响

Compatible patch release after v1.6.7. The cookie and frame-protection effects are disclosed in the public upgrade notes. No automatic retry budget, room-code admission policy, room lifetime or media topology is replaced. Reports without matched diagnostics and unmeasured native performance costs remain in the TODO ledger.
2026-09-29 08:44:17 +08:00

582 lines
19 KiB
Go

package loopback
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
)
const testOrigin = "https://share.example.com"
func TestStartServesHealthAndStrictControlHandshake(t *testing.T) {
expectedMedia := NativeMediaCapabilities{
Video: true, ProcessAudio: false, SystemAudio: true, HardwareH264: true,
}
server := startTestServerWithOptions(t, Options{
AllowedOrigins: []string{testOrigin},
NativeMedia: expectedMedia,
})
endpoint := server.Endpoint()
var health Health
response := doHealthRequest(t, endpoint, endpoint.Host, testOrigin)
if response.StatusCode != http.StatusOK {
t.Fatalf("health status = %d", response.StatusCode)
}
if err := json.NewDecoder(response.Body).Decode(&health); err != nil {
t.Fatal(err)
}
response.Body.Close()
if health.Protocol != ProtocolVersion || health.Service != ServiceName ||
health.Port != endpoint.Port || health.InstanceToken != endpoint.InstanceToken ||
health.NativeMedia != expectedMedia {
t.Fatalf("health = %+v", health)
}
connection := dialControl(t, endpoint, endpoint.InstanceToken, testOrigin)
defer connection.CloseNow()
writeControl(t, connection, requestJSON("request_hello", "hello"))
var ready controlMessage
readControl(t, connection, &ready)
if ready.Type != "ready" || ready.ID != "request_hello" {
t.Fatalf("ready = %+v", ready)
}
writeControl(t, connection, requestJSON("request_ping", "ping"))
var pong controlMessage
readControl(t, connection, &pong)
if pong.Type != "pong" || pong.ID != "request_ping" {
t.Fatalf("pong = %+v", pong)
}
}
func TestHealthRejectsUnexpectedOriginAndHost(t *testing.T) {
server := startTestServer(t, testOrigin)
endpoint := server.Endpoint()
response := doHealthRequest(t, endpoint, endpoint.Host, "https://other.example")
response.Body.Close()
if response.StatusCode != http.StatusForbidden {
t.Fatalf("bad origin status = %d", response.StatusCode)
}
response = doHealthRequest(t, endpoint, "127.0.0.2:"+strconv.Itoa(endpoint.Port), testOrigin)
response.Body.Close()
if response.StatusCode != http.StatusNotFound {
t.Fatalf("bad host status = %d", response.StatusCode)
}
}
func TestHealthAcceptsTheLocalHostAlias(t *testing.T) {
server := startTestServer(t, testOrigin)
endpoint := server.Endpoint()
response := doHealthRequest(t, endpoint,
"localhost:"+strconv.Itoa(endpoint.Port),
"http://localhost:"+strconv.Itoa(endpoint.Port),
)
response.Body.Close()
if response.StatusCode != http.StatusOK {
t.Fatalf("localhost health status = %d", response.StatusCode)
}
}
func TestPresentationUpdatesOnlyLanguageWithoutClaimingMediaControl(t *testing.T) {
languages := make(chan string, 3)
var server *Server
server = startTestServerWithOptions(t, Options{
AllowedOrigins: []string{testOrigin},
Presentation: func(language string) {
server.SetAllowedOrigins([]string{testOrigin})
languages <- language
},
})
endpoint := server.Endpoint()
connection := dialControl(t, endpoint, endpoint.InstanceToken, testOrigin)
defer connection.CloseNow()
writeControl(t, connection, requestJSON("request_hello", "hello"))
var ready controlMessage
readControl(t, connection, &ready)
client := &http.Client{Timeout: time.Second}
for _, input := range []struct {
method, path, origin, body, language string
status int
}{
{http.MethodPost, "/presentation", testOrigin, `{"language":"zh"}`, "zh", http.StatusNoContent},
{http.MethodPost, "/presentation", testOrigin, `{"language":"en"}`, "en", http.StatusNoContent},
{http.MethodPost, "/presentation", endpoint.URL, `{"language":"vis"}`, "vis", http.StatusNoContent},
{http.MethodOptions, "/presentation", testOrigin, "", "", http.StatusNoContent},
{http.MethodPost, "/presentation", "https://other.example", `{"language":"en"}`, "", http.StatusForbidden},
{http.MethodOptions, "/presentation", "https://other.example", "", "", http.StatusForbidden},
{http.MethodOptions, "/health", testOrigin, "", "", http.StatusNoContent},
{http.MethodOptions, "/health", "https://other.example", "", "", http.StatusForbidden},
{http.MethodGet, "/presentation", testOrigin, "", "", http.StatusMethodNotAllowed},
{http.MethodPost, "/presentation", testOrigin, `{"language":"other"}`, "", http.StatusBadRequest},
{http.MethodPost, "/presentation", testOrigin, `{"language":"en","capture":true}`, "", http.StatusBadRequest},
{http.MethodPost, "/presentation", testOrigin, `{"language":"en"}{}`, "", http.StatusBadRequest},
{http.MethodPost, "/presentation", testOrigin, strings.Repeat(" ", 257) + `{"language":"en"}`, "", http.StatusBadRequest},
} {
request, err := http.NewRequest(input.method, endpoint.URL+input.path, strings.NewReader(input.body))
if err != nil {
t.Fatal(err)
}
request.Header.Set("Origin", input.origin)
request.Header.Set("Content-Type", "application/json")
if input.method == http.MethodOptions {
request.Header.Set("Access-Control-Request-Private-Network", "true")
}
response, err := client.Do(request)
if err != nil {
t.Fatal(err)
}
response.Body.Close()
if response.StatusCode != input.status {
t.Fatalf("%s %s status = %d, want %d", input.method, input.path, response.StatusCode, input.status)
}
if input.method == http.MethodOptions && input.status == http.StatusNoContent &&
(response.Header.Get("Access-Control-Allow-Origin") != testOrigin ||
response.Header.Get("Access-Control-Allow-Private-Network") != "true" ||
(input.path == "/presentation" && (response.Header.Get("Access-Control-Allow-Methods") != "POST" ||
response.Header.Get("Access-Control-Allow-Headers") != "Content-Type")) ||
(input.path == "/health" && response.Header.Get("Access-Control-Allow-Methods") != "GET")) {
t.Fatal("presentation preflight did not preserve its bounded origin/method scope")
}
select {
case language := <-languages:
if language != input.language {
t.Fatalf("unexpected presentation callback: %q", language)
}
default:
if input.language != "" {
t.Fatal("presentation callback missing")
}
}
}
writeControl(t, connection, requestJSON("request_ping", "ping"))
var pong controlMessage
readControl(t, connection, &pong)
if pong.Type != "pong" {
t.Fatal("presentation changed the active media control")
}
}
func TestControlSessionsAreBoundedAndRetireIndependently(t *testing.T) {
created := make(chan *testControlSession, maxControlSessions)
server := startTestServerWithOptions(t, Options{
AllowedOrigins: []string{testOrigin},
NewControl: func() ControlSession {
session := &testControlSession{events: make(chan any), closed: make(chan struct{})}
created <- session
return session
},
})
endpoint := server.Endpoint()
var connections []*websocket.Conn
var sessions []*testControlSession
for index := 0; index < maxControlSessions; index++ {
connection := dialControl(t, endpoint, endpoint.InstanceToken, testOrigin)
defer connection.CloseNow()
writeControl(t, connection, requestJSON("request_hello", "hello"))
var ready controlMessage
readControl(t, connection, &ready)
connections = append(connections, connection)
select {
case session := <-created:
sessions = append(sessions, session)
case <-time.After(time.Second):
t.Fatal("accepted control did not acquire its own session")
}
}
extra, response, err := websocket.Dial(context.Background(), websocketURL(endpoint)+"/control", &websocket.DialOptions{
HTTPHeader: http.Header{"Origin": []string{testOrigin}},
Subprotocols: []string{ControlSubprotocol + "." + endpoint.InstanceToken},
})
if extra != nil {
extra.CloseNow()
}
if err == nil || response == nil || response.StatusCode != http.StatusConflict {
t.Fatalf("excess control dial: conn=%v response=%v err=%v", extra, response, err)
}
_ = response.Body.Close()
_ = connections[0].CloseNow()
select {
case <-sessions[0].closed:
case <-time.After(time.Second):
t.Fatal("closed room retained its native session")
}
writeControl(t, connections[1], requestJSON("request_ping", "ping"))
var pong controlMessage
readControl(t, connections[1], &pong)
if pong.Type != "pong" {
t.Fatal("closing one room affected its sibling control")
}
if err = server.Close(); err != nil {
t.Fatal(err)
}
for _, session := range sessions {
select {
case <-session.closed:
default:
t.Fatal("App close returned before native session cleanup")
}
}
}
func TestControlSessionSharesOneBoundedSocketForResponsesAndEvents(t *testing.T) {
extension := &testControlSession{
events: make(chan any, 1),
closed: make(chan struct{}),
}
server := startTestServerWithOptions(t, Options{
AllowedOrigins: []string{testOrigin},
NewControl: func() ControlSession {
return extension
},
})
connection := dialControl(t, server.Endpoint(), server.Endpoint().InstanceToken, testOrigin)
writeControl(t, connection, requestJSON("request_hello", "hello"))
var ready controlMessage
readControl(t, connection, &ready)
// The old owner's event was queued while valid, but retired before delivery.
current := true
retired := ControlEvent{Value: controlMessage{Version: ProtocolVersion, Type: "retired-event"},
Current: func() bool { return current }}
current = false
extension.events <- retired
extension.events <- ControlEvent{
Value: controlMessage{Version: ProtocolVersion, Type: "current-event"},
Current: func() bool { return true },
}
var currentEvent controlMessage
readControl(t, connection, &currentEvent)
if currentEvent.Type != "current-event" {
t.Fatalf("retired event escaped the delivery fence: %+v", currentEvent)
}
extension.events <- controlMessage{
Version: ProtocolVersion, ID: "event_123456", Type: "extension-event",
}
writeControl(t, connection, requestJSON("request_extension", "extension"))
received := map[string]bool{}
for len(received) < 2 {
var message controlMessage
readControl(t, connection, &message)
received[message.Type] = true
}
if !received["extension-event"] || !received["extension-response"] {
t.Fatalf("control messages = %+v", received)
}
connection.CloseNow()
select {
case <-extension.closed:
case <-time.After(time.Second):
t.Fatal("control extension was not closed with its socket")
}
}
func TestControlClosesTheSocketWhenItsReaderRejectsAFrame(t *testing.T) {
server := startTestServer(t, testOrigin)
connection := dialControl(t, server.Endpoint(), server.Endpoint().InstanceToken, testOrigin)
defer connection.CloseNow()
writeControl(t, connection, requestJSON("request_hello", "hello"))
var ready controlMessage
readControl(t, connection, &ready)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := connection.Write(ctx, websocket.MessageBinary, []byte("invalid")); err != nil {
t.Fatal(err)
}
if _, _, err := connection.Read(ctx); err == nil || ctx.Err() != nil {
t.Fatalf("server did not close its rejected control socket: %v", err)
}
}
func TestControlCloseCancelsAHandlerWaitingForPicker(t *testing.T) {
extension := &blockingControlSession{
startedSignal: make(chan struct{}),
closedSignal: make(chan struct{}),
}
server := startTestServerWithOptions(t, Options{
AllowedOrigins: []string{testOrigin},
NewControl: func() ControlSession {
return extension
},
})
connection := dialControl(t, server.Endpoint(), server.Endpoint().InstanceToken, testOrigin)
writeControl(t, connection, requestJSON("request_hello", "hello"))
var ready controlMessage
readControl(t, connection, &ready)
writeControl(t, connection, requestJSON("request_extension", "extension"))
select {
case <-extension.startedSignal:
case <-time.After(time.Second):
t.Fatal("control handler did not start")
}
connection.CloseNow()
select {
case <-extension.closedSignal:
case <-time.After(time.Second):
t.Fatal("closing the control socket did not release the picker handler")
}
deadline := time.Now().Add(time.Second)
for {
second, response, err := websocket.Dial(context.Background(),
websocketURL(server.Endpoint())+"/control", &websocket.DialOptions{
HTTPHeader: http.Header{"Origin": []string{testOrigin}},
Subprotocols: []string{ControlSubprotocol + "." + server.Endpoint().InstanceToken},
})
if err == nil {
second.CloseNow()
return
}
if response != nil && response.Body != nil {
response.Body.Close()
}
if time.Now().After(deadline) {
t.Fatalf("control session was not released: %v", err)
}
time.Sleep(10 * time.Millisecond)
}
}
func TestControlSessionProcessesARequestBurstInOrder(t *testing.T) {
extension := &testControlSession{
events: make(chan any, 1),
closed: make(chan struct{}),
}
server := startTestServerWithOptions(t, Options{
AllowedOrigins: []string{testOrigin},
NewControl: func() ControlSession {
return extension
},
})
connection := dialControl(t, server.Endpoint(), server.Endpoint().InstanceToken, testOrigin)
defer connection.CloseNow()
writeControl(t, connection, requestJSON("request_hello", "hello"))
var ready controlMessage
readControl(t, connection, &ready)
const burst = controlQueueSize * 2
for index := 0; index < burst; index++ {
writeControl(t, connection, requestJSON(
fmt.Sprintf("request_extension_%d", index), "extension",
))
}
seen := make(map[string]bool, burst)
for len(seen) < burst {
var response controlMessage
readControl(t, connection, &response)
seen[response.ID] = true
}
for index := 0; index < burst; index++ {
if !seen[fmt.Sprintf("request_extension_%d", index)] {
t.Fatalf("request %d was not processed", index)
}
}
}
func TestDeferredResponseKeepsControlResponsiveAndRequestIdentity(t *testing.T) {
extension := &testControlSession{events: make(chan any, 1), closed: make(chan struct{})}
server := startTestServerWithOptions(t, Options{AllowedOrigins: []string{testOrigin},
NewControl: func() ControlSession { return extension }})
connection := dialControl(t, server.Endpoint(), server.Endpoint().InstanceToken, testOrigin)
defer connection.CloseNow()
writeControl(t, connection, requestJSON("request_hello", "hello"))
var value controlMessage
readControl(t, connection, &value)
writeControl(t, connection, requestJSON("request_pending", "deferred"))
writeControl(t, connection, requestJSON("request_ping", "ping"))
readControl(t, connection, &value)
if value.Type != "pong" || value.ID != "request_ping" {
t.Fatalf("pending operation blocked control: %+v", value)
}
extension.events <- controlMessage{Version: ProtocolVersion, ID: "request_pending", Type: "extension-response"}
readControl(t, connection, &value)
if value.Type != "extension-response" || value.ID != "request_pending" {
t.Fatalf("deferred response lost its request identity: %+v", value)
}
}
func TestStartAlwaysBindsToIPv4Loopback(t *testing.T) {
server := startTestServer(t, testOrigin)
host, _, err := net.SplitHostPort(server.Endpoint().Host)
if err != nil || host != "127.0.0.1" {
t.Fatalf("helper host = %q, %v", host, err)
}
}
func TestCloseStopsTheControlSession(t *testing.T) {
server := startTestServer(t, testOrigin)
endpoint := server.Endpoint()
connection := dialControl(t, endpoint, endpoint.InstanceToken, testOrigin)
if err := server.Close(); err != nil {
t.Fatal(err)
}
readContext, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if _, _, err := connection.Read(readContext); err == nil {
t.Fatal("control connection remained open after server close")
}
connection.CloseNow()
}
func TestUnexpectedServeFailureIsReported(t *testing.T) {
server := startTestServer(t, testOrigin)
if err := server.listener.Close(); err != nil {
t.Fatal(err)
}
select {
case err := <-server.Done():
if err == nil {
t.Fatal("unexpected listener close was reported as a clean shutdown")
}
case <-time.After(time.Second):
t.Fatal("serve failure was not reported")
}
}
func startTestServer(t *testing.T, origin string) *Server {
t.Helper()
return startTestServerWithOptions(t, Options{AllowedOrigins: []string{origin}})
}
func startTestServerWithOptions(t *testing.T, options Options) *Server {
t.Helper()
port := freePort(t)
options.PortStart = port
options.PortEnd = port
server, err := Start(context.Background(), options)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = server.Close() })
return server
}
func freePort(t *testing.T) int {
t.Helper()
listener, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener.Close()
return listener.Addr().(*net.TCPAddr).Port
}
func doHealthRequest(t *testing.T, endpoint Endpoint, host, origin string) *http.Response {
t.Helper()
request, err := http.NewRequest(http.MethodGet, endpoint.URL+"/health", nil)
if err != nil {
t.Fatal(err)
}
request.Host = host
if origin != "" {
request.Header.Set("Origin", origin)
}
response, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatal(err)
}
return response
}
func dialControl(t *testing.T, endpoint Endpoint, token, origin string) *websocket.Conn {
t.Helper()
connection, _, err := websocket.Dial(context.Background(), websocketURL(endpoint)+"/control", &websocket.DialOptions{
HTTPHeader: http.Header{"Origin": []string{origin}},
Subprotocols: []string{ControlSubprotocol + "." + token},
})
if err != nil {
t.Fatal(err)
}
return connection
}
func websocketURL(endpoint Endpoint) string {
return strings.Replace(endpoint.URL, "http://", "ws://", 1)
}
func writeControl(t *testing.T, connection *websocket.Conn, value map[string]any) {
t.Helper()
if err := wsjson.Write(context.Background(), connection, value); err != nil {
t.Fatal(err)
}
}
func readControl(t *testing.T, connection *websocket.Conn, value any) {
t.Helper()
contextWithDeadline, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := wsjson.Read(contextWithDeadline, connection, value); err != nil {
t.Fatal(err)
}
}
func requestJSON(id, messageType string) map[string]any {
return map[string]any{
"version": ProtocolVersion,
"id": id,
"type": messageType,
}
}
type testControlSession struct {
events chan any
closed chan struct{}
}
type blockingControlSession struct {
started sync.Once
closed sync.Once
startedSignal chan struct{}
closedSignal chan struct{}
}
func (session *blockingControlSession) Handle(ctx context.Context, _ []byte) (any, error) {
session.started.Do(func() { close(session.startedSignal) })
<-ctx.Done()
return nil, ctx.Err()
}
func (session *blockingControlSession) Events() <-chan any {
return nil
}
func (session *blockingControlSession) Close() error {
session.closed.Do(func() { close(session.closedSignal) })
return nil
}
func (session *testControlSession) Handle(_ context.Context, payload []byte) (any, error) {
message, err := decodeEnvelope(payload)
if err == nil && message.Type == "deferred" {
return nil, nil
}
if err != nil || message.Type != "extension" {
return nil, errors.New("unexpected extension request")
}
return controlMessage{
Version: ProtocolVersion, ID: message.ID, Type: "extension-response",
}, nil
}
func (session *testControlSession) Events() <-chan any {
return session.events
}
func (session *testControlSession) Close() error {
select {
case <-session.closed:
default:
close(session.closed)
}
return nil
}