Files
piik/internal/app/remoteinput/platform_windows_test.go
T
AAA 815f1a07c4 fix(media): stabilize sharing recovery and remote input (#513)
## Problem and outcome

Connection recovery and packet repair could leave stale pictures, missing sound or unreliable remote input. This maintenance release repairs media/route ownership, H.264 negotiation, voice forwarding and Windows input recovery, and makes shared mobile controls and popovers easier to use.

## Release notes
**Piik, now in your pocket.**

Piik App supports Android screen sharing, with touch controls for a friend's computer.

**手机,也能开播。**

Piik App 支持 Android 屏幕分享,也能用手机控制朋友的电脑。

### English

Piik v2.5.1 improves sharing recovery, voice playback and remote control, with clearer mobile controls. It keeps the existing 2.x compatibility baseline.

- Improve H.264 selection and picture recovery after reconnecting, changing sources or recovering lost packets; keep a working share when a replacement source fails.
- Keep audio and room voice continuous, and prevent duplicate sound when switching playback windows.
- Improve Windows input recovery and release remote-control permissions when their connection ends.
- Keep mobile controls together and popovers usable after layout changes or in short viewports.
- Open the appropriate Android update download, including the Gitee ZIP containing the APK. Self-hosted SFU servers refresh public addresses for new connections without a restart.
- Add macOS source-thumbnail support.

Update the App and self-hosted site together to receive all fixes. Browser settings and existing room data are retained.

### 中文

Piik v2.5.1 改善分享恢复、语音播放与远程控制的稳定性,让手机操作更顺手,沿用现有 2.x 兼容基线。

- 改善 H.264 编码选择,以及重连、切换分享源和丢包后的画面恢复;新分享源切换失败时保留正常分享。
- 保持声音与房间语音连续,避免切换播放窗口时出现双重声音。
- 改善 Windows 输入恢复,并在连接结束时释放远程控制授权。
- 手机控件保持成组,浮层在布局变化或可见高度较小时仍可操作。
- Android 更新打开对应下载入口,Gitee 提供内含 APK 的 ZIP;自部署 SFU 为新连接刷新公网地址,无需重启服务。
- 补齐 macOS 选源缩略图支持。

请一起更新 App 与自部署站点,以获得全部修复。浏览器设置与已有房间数据保留。
## Validation

- Full frontend typecheck, test and production-build acceptance; current Windows App/Server packaging, extracted-file checks and exact served JavaScript verified locally.
- Existing Go, vulnerability, native media and controlled-network results retained at their tested revisions. The final production delta from the fully checked candidate is the shared short-viewport popover correction.
- The diagnostic report supplied for #502 informed the Windows input fixes. #500 sharing is confirmed working; its missing source previews are implemented. Original-device follow-up remains separate from this release's verified local fixes.

## Compatibility and scope

Preserves the published 2.x Browser/server, Native control, capture and SQLite contracts. No 3.0 or iOS implementation or external contributor patch is included. Related: #500, #502, #510, #512.
2026-10-31 16:34:12 -04:00

174 lines
5.5 KiB
Go

//go:build windows
package remoteinput
import (
"context"
"errors"
"runtime"
"strings"
"syscall"
"testing"
"unsafe"
"golang.org/x/sys/windows"
)
func TestWindowsInputRecoversItsDesktopAndPendingRelease(t *testing.T) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
backend, err := newPlatformSink(false)
if err != nil {
t.Fatal(err)
}
sink := backend.(*windowsSink)
active, _, _ := user32.NewProc("OpenInputDesktop").Call(0, 0, 0x81)
if active == 0 {
t.Skip("no accessible input desktop")
}
defer user32.NewProc("CloseDesktop").Call(active)
activeName, ok := desktopName(active)
if !ok || activeName != sink.desktop {
t.Skip("test is not on the input desktop")
}
down, _, _ := user32.NewProc("GetAsyncKeyState").Call(1)
if down&0x8000 != 0 {
t.Skip("local mouse button is held")
}
thread, _, _ := user32.NewProc("GetThreadDesktop").Call(uintptr(windows.GetCurrentThreadId()))
name, _ := windows.UTF16PtrFromString(t.Name())
other, _, err := user32.NewProc("CreateDesktopW").Call(uintptr(unsafe.Pointer(name)), 0, 0, 0, 0x83, 0)
if other == 0 {
t.Fatalf("create isolated thread desktop: %v", err)
}
defer func() {
if restored, _, err := user32.NewProc("SetThreadDesktop").Call(thread); restored == 0 {
t.Errorf("restore test thread desktop: %v", err)
}
user32.NewProc("CloseDesktop").Call(other)
}()
if bound, _, err := user32.NewProc("SetThreadDesktop").Call(other); bound == 0 {
t.Fatalf("bind isolated thread: %v", err)
}
// Never switch the visible desktop. Only this test thread leaves it.
probe := winInput{Mouse: mouseInput{Flags: 1}}
restricted := &windowsSink{desktop: windows.UTF16PtrToString(name)}
if err := restricted.inject(probe); err == nil {
t.Fatal("input escaped the backend's desktop")
}
if err := sink.inject(probe); err != nil {
t.Fatalf("input did not recover to its own active desktop: %v", err)
}
current, _, _ := user32.NewProc("GetThreadDesktop").Call(uintptr(windows.GetCurrentThreadId()))
currentName, _ := desktopName(current)
if currentName != restricted.desktop {
t.Fatal("injection changed the caller's desktop")
}
// A previously failed release must use the same recovery as fresh input.
sink.buttons[0] = true
m := &Manager{sink: sink, pendingButtons: map[int]bool{0: true}}
if err := m.retryReleases(); err != nil || len(m.pendingButtons) != 0 || len(sink.buttons) != 0 {
t.Fatalf("release debt did not recover: %v", err)
}
if err := sink.Apply(bounds{}, inputEvent{Type: "relative"}); err != nil {
t.Fatalf("input remained blocked after cleanup: %v", err)
}
}
func TestRejectedWindowsInputDiagnostics(t *testing.T) {
for _, want := range []syscall.Errno{0, windows.ERROR_ACCESS_DENIED} {
err := rejectedWindowsInput(0, 1, want)
var code syscall.Errno
if !errors.As(err, &code) || code != want {
t.Fatalf("Windows rejection lost its system code: %v", err)
}
for _, fact := range []string{"Windows accepted 0 of 1", "input desktop accessible=", "input desktop matches thread=", "foreground present=", "foreground higher integrity="} {
if !strings.Contains(err.Error(), fact) {
t.Fatalf("Windows rejection lost %q: %v", fact, err)
}
}
}
if level, ok := tokenIntegrity(windows.GetCurrentProcessToken()); !ok || level == 0 {
t.Fatalf("current process integrity was not observed: %d %v", level, ok)
}
}
func TestNativeStopShortcutRegistrationLifetime(t *testing.T) {
// Reserve an available chord through the OS API without injecting input.
var reserved func()
var key int
for key = 1; key <= 11; key++ {
var err error
reserved, err = watchStopShortcut(t.Context(), key, func() {})
if err == nil {
break
}
}
if reserved == nil {
t.Fatal("no test shortcut is available")
}
t.Cleanup(reserved)
m, err := New(t.Context(), false)
if err != nil || m == nil {
t.Fatalf("hotkey collision disabled the input backend: %v", err)
}
t.Cleanup(m.Close)
m.stopKey = key
state, err := m.ReadStopShortcut()
if err != nil || state.Available {
t.Fatalf("reserved chord was reported usable: %+v, %v", state, err)
}
reserved()
state, err = m.ReadStopShortcut()
if err != nil || !state.Available {
t.Fatalf("released chord was not available: %+v, %v", state, err)
}
ctx, cancel := context.WithCancel(t.Context())
stop, err := watchStopShortcut(ctx, key, func() {})
if err != nil {
t.Fatal(err)
}
cancel()
stop()
stop, err = watchStopShortcut(t.Context(), key, func() {})
if err != nil {
t.Fatalf("cancelled watcher retained its chord: %v", err)
}
stop()
}
func TestCommittedTextPackets(t *testing.T) {
inputs := textInputs("A中😀")
units := []uint16{0x41, 0x4e2d, 0xd83d, 0xde00}
if len(inputs) != 2*len(units) {
t.Fatalf("text packets: %d", len(inputs))
}
for i, input := range inputs {
key := (*keyboardInput)(unsafe.Pointer(&input.Mouse))
if input.Kind != 1 || key.VK != 0 || key.Scan != units[i/2] || key.Flags != uint32(4+2*(i%2)) {
t.Fatalf("packet %d: %+v", i, key)
}
}
}
func TestMouseButtonPackets(t *testing.T) {
for button, flags := range []uint32{2, 0x20, 8, 0x80, 0x80} {
for _, down := range []bool{true, false} {
input, err := buttonInput(button, down)
wantFlags, wantData := flags, uint32(0)
if !down {
wantFlags <<= 1
}
if button >= 3 {
wantData = 1 << (button - 3)
}
if err != nil || input.Kind != 0 || input.Mouse.Flags != wantFlags || input.Mouse.Data != wantData {
t.Fatalf("button %d down=%v: %+v, %v", button, down, input, err)
}
}
}
if _, err := buttonInput(5, true); err == nil {
t.Fatal("unknown button accepted")
}
}