Files
piik/internal/app/nativehost/input_transport_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

278 lines
8.9 KiB
Go

//go:build windows
package nativehost
import (
"context"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"encoding/json"
"net"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/TNTcraftHIM/Piik/internal/app/mediaedge"
"github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
"github.com/TNTcraftHIM/Piik/internal/app/remoteinput"
"github.com/TNTcraftHIM/Piik/internal/media/forwarding"
"github.com/TNTcraftHIM/Piik/internal/media/inputroute"
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
"github.com/pion/ice/v4"
"github.com/pion/webrtc/v4"
)
type inputLossSocket struct {
net.PacketConn
drop atomic.Bool
}
func (s *inputLossSocket) WriteTo(p []byte, addr net.Addr) (int, error) {
if s.drop.Load() {
return len(p), nil
}
return s.PacketConn.WriteTo(p, addr)
}
func (s *inputLossSocket) ReadFrom(p []byte) (int, net.Addr, error) {
for {
n, addr, err := s.PacketConn.ReadFrom(p)
if err != nil || !s.drop.Load() {
return n, addr, err
}
}
}
// Real ICE/DTLS/SCTP and the production permission owner. Approving control
// does not inject input; no key, button or pointer event is sent by this fixture.
func TestInputTransportLossRetiresOnlyItsPreviousApproval(t *testing.T) {
for _, kind := range []string{"edge", "publication"} {
t.Run(kind, func(t *testing.T) { checkInputTransportOwner(t, kind) })
}
}
func checkInputTransportOwner(t *testing.T, kind string) {
ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
defer cancel()
check := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
wait := func(check func() bool, what string) {
t.Helper()
for !check() {
select {
case <-ctx.Done():
t.Fatal("timeout: " + what)
case <-time.After(10 * time.Millisecond):
}
}
}
engine, err := mediaedge.NewEngine(mediaedge.EngineOptions{BindAddress: "127.0.0.1:0", IncludeLoopback: true})
check(err)
defer engine.Close()
source, err := engine.NewSource("vp8", 1, 1, nil)
check(err)
defer source.Close()
check(source.SetFormat(0, 320, 180))
check(source.ConfigureOutputs([]uint32{300000}))
manager, err := remoteinput.New(ctx, false)
check(err)
defer manager.Close()
input := manager.NewSession()
input.SetTarget(nativecapture.CaptureTarget{Kind: "display", SourceID: "1"}, nil)
policy, err := input.BeginPolicy(ctx, "ask", "", false)
check(err)
check(policy.Apply())
binding := protocol.RemoteInputPeer{PeerID: "test-viewer", EndpointID: strings.Repeat("a", 32), Secret: strings.Repeat("b", 64)}
check(input.SetPeers([]protocol.RemoteInputPeer{binding}))
events := make(chan Event, 64)
session := &Session{shareID: "input-loss", engine: engine, source: source, remoteInput: input, ctx: ctx, edges: map[string]*mediaedge.Edge{}, events: func(_ context.Context, event Event) { events <- event }}
defer session.CloseEdge("input-edge")
defer session.ClosePublication("input-generation", "input-edge")
packet, err := net.ListenPacket("udp4", "127.0.0.1:0")
check(err)
socket := &inputLossSocket{PacketConn: packet}
defer socket.Close()
mux := forwarding.NewUDPMux(ice.UDPMuxParams{UDPConn: socket})
defer mux.Close()
settings := webrtc.SettingEngine{}
settings.SetICEUDPMux(mux)
settings.SetIncludeLoopbackCandidate(true)
settings.SetICETimeouts(time.Second, 15*time.Second, 100*time.Millisecond)
viewer, err := webrtc.NewAPI(webrtc.WithSettingEngine(settings)).NewPeerConnection(webrtc.Configuration{})
check(err)
defer viewer.Close()
viewer.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate != nil {
value := candidate.ToJSON()
if kind == "publication" {
publication, e := session.Publication("input-generation", "input-edge")
if e == nil {
_ = publication.AddRemoteCandidate(&value)
}
} else {
_ = session.AddCandidate("input-edge", &value)
}
}
})
channel := make(chan *webrtc.DataChannel, 1)
viewer.OnDataChannel(func(dc *webrtc.DataChannel) { dc.OnOpen(func() { channel <- dc }) })
var offer webrtc.SessionDescription
var setAnswer func(webrtc.SessionDescription) error
var localState func() webrtc.PeerConnectionState
if kind == "publication" {
offer, _, err = session.PreparePublication("input-generation", "input-edge", nil)
check(err)
publication, e := session.Publication("input-generation", "input-edge")
check(e)
setAnswer = publication.SetAnswer
} else {
offer, err = session.PrepareEdge("input-edge", nil, false)
check(err)
localState = session.edge("input-edge").State
setAnswer = func(answer webrtc.SessionDescription) error { return session.SetAnswer("input-edge", answer, "") }
}
check(viewer.SetRemoteDescription(offer))
answer, err := viewer.CreateAnswer(nil)
check(err)
check(viewer.SetLocalDescription(answer))
check(setAnswer(answer))
// Native trickle candidates can precede the remote description, so drain them only now.
connected := false
for !connected {
select {
case event := <-events:
if event.Candidate != nil {
check(viewer.AddICECandidate(*event.Candidate))
}
connected = event.State == "connected"
case <-ctx.Done():
t.Fatal("edge never connected")
}
}
var dc *webrtc.DataChannel
select {
case dc = <-channel:
case <-ctx.Done():
t.Fatal("input channel did not open")
}
key, _ := hex.DecodeString(binding.Secret)
block, _ := aes.NewCipher(key)
aead, _ := cipher.NewGCM(block)
epoch := strings.Repeat("c", 32)
type inputReply struct {
Grant string `json:"grant"`
CoPlay *bool `json:"coPlay"`
}
replies := make(chan inputReply, 64)
dc.OnMessage(func(message webrtc.DataChannelMessage) {
var packet inputroute.Packet
if json.Unmarshal(message.Data, &packet) != nil {
return
}
data, e := base64.StdEncoding.DecodeString(packet.Data)
if e != nil || len(data) < 36 {
return
}
aad := append(append([]byte{1}, data[:8]...), []byte(packet.Epoch)...)
plain, e := aead.Open(nil, data[8:20], data[20:], aad)
if e != nil {
return
}
var reply inputReply
if json.Unmarshal(plain, &reply) == nil {
replies <- reply
}
})
var sequence uint64
send := func(plain []byte, epoch string) {
t.Helper()
sequence++
header := make([]byte, 20)
binary.BigEndian.PutUint64(header, sequence)
_, err := rand.Read(header[8:20])
check(err)
aad := append(append([]byte{0}, header[:8]...), []byte(epoch)...)
encrypted := aead.Seal(header, header[8:20], plain, aad)
data, _ := json.Marshal(inputroute.Packet{ID: binding.EndpointID, Epoch: epoch, Data: base64.StdEncoding.EncodeToString(encrypted)})
check(dc.SendText(string(data)))
}
send([]byte(`{"type":"request","intent":"dddddddddddddddddddddddddddddddd"}`), epoch)
wait(func() bool { return len(input.State().Requests) == 1 }, "pending approval")
oldRequest := input.State().Requests[0].ID
session.mu.Lock()
locked := true
defer func() {
if locked {
session.mu.Unlock()
}
}()
socket.drop.Store(true)
// Pion default disconnect timeout is five seconds. Keep the fixture outage
// beyond it while the Host lock delays asynchronous media projections.
select {
case <-time.After(9 * time.Second):
case <-ctx.Done():
t.Fatal("outage deadline")
}
if requests := input.State().Requests; len(requests) != 0 {
t.Errorf("transport loss kept its previous approval while Host projection was queued: %+v", requests)
}
socket.drop.Store(false)
wait(func() bool { return viewer.ConnectionState() == webrtc.PeerConnectionStateConnected }, "remote recovered while native callback delayed")
if localState != nil {
wait(func() bool { return localState() == webrtc.PeerConnectionStateConnected }, "native recovered while callback delayed")
}
freshEpoch := strings.Repeat("e", 32)
send([]byte(`{"type":"request","intent":"ffffffffffffffffffffffffffffffff"}`), freshEpoch)
wait(func() bool {
requests := input.State().Requests
return len(requests) == 1 && requests[0].ID != oldRequest
}, "new epoch request before native loss cleanup")
freshRequest := input.State().Requests[0]
check(input.Decide(freshRequest.ID, true))
session.mu.Unlock()
locked = false
for {
select {
case event := <-events:
if event.State == "disconnected" {
goto observed
}
case <-ctx.Done():
t.Fatal("missing loss callback")
}
}
observed:
if requests := input.State().Requests; len(requests) != 1 {
t.Fatalf("delayed old loss revoked new post-recovery grant: %+v", requests)
}
// The new grant must remain usable over the same physical SCTP channel.
// A capability reply has an explicit coPlay field, unlike earlier replies.
send([]byte(`{"type":"capabilities"}`), freshEpoch)
for {
select {
case reply := <-replies:
if reply.CoPlay != nil && reply.Grant != "" {
goto acknowledged
}
case <-ctx.Done():
t.Fatal("new grant did not survive capability round trip")
}
}
acknowledged:
if kind == "publication" {
session.ClosePublication("input-generation", "input-edge")
} else {
session.CloseEdge("input-edge")
}
wait(func() bool { return len(input.State().Requests) == 0 }, "closed transport permission release")
}