Files
piik/internal/app/remoteinput/manager.go
T
AAA 24e9b639a8 feat: add attended remote control and refine shared viewing (#482)
Piik can now let one invited Viewer control the Host's shared desktop or window, using the existing media route and explicit source-scoped consent. This phase also finishes Viewer playback settings, the combined interaction window, the shared control/theme/icon polish, and concrete forwarding-feedback recovery fixes.

Remote input stays off by default, uses one controller and releases held input on revocation, retirement or loss. Existing 2.x sharing remains compatible; new capabilities are negotiated with supporting App/Site/relay peers. No virtual input driver, new media listener or route planner is added.

Validation: full Web check (97 files / 1,677 tests), Windows Go/native checks; seven real Browser/App lifecycle scenarios covering P2P, SFU and fallback-to-direct continuity; existing cross-version and physical Windows routed-input evidence; native macOS/Linux packaging, Go race/security and container checks through the publishing pipeline. Physical macOS/Linux permissions, mobile devices, mixed DPI and commercial games retain their documented limits. README/wiki keep deployment under the optional self-hosted Server chapters.

Refs #472, #477, #479, #480. Issue follow-up happens after publication; unmatched field reports remain open.

## Release notes

### 中文

这是一次功能更新:一起看屏幕时,可以把键鼠交给一位朋友操作,聊天和表情也整合进了同一个互动窗口。本次还整理了分享与播放设置,并修复部分连接恢复和中转画质停滞的问题。

#### 新功能与体验

- **可选键鼠远控**:房主可以让一位朋友操作当前分享的桌面或窗口,随时撤回授权;换源或停播后关闭远控。提供手机触控、缩放和小键盘。当前实机验证覆盖 Windows App。
- **观众播放设置**:新增完整显示、等比铺满、拉伸铺满,以及自动/仅 P2P/仅 SFU 线路选择;站点和房主的线路限制仍然生效。
- **互动窗口**:聊天、参与者和表情放在同一窗口;可以从聊天头像选择接收者,新增点赞、倒彩等表情。支持的浏览器可以独立置顶,继续查看聊天和互动。全屏保留弹幕开关。
- **分享与播放设置更清楚**:暂停与停止相邻,画面、声音、连接和远控各有对应入口。观众播控在移动鼠标时显现,远控时收进画面边缘的小抽屉;点击画面不再意外暂停。没有声音输入时播控给出明确反馈,恢复输入后保留观众的音量设置。

#### 修复

- **连接与播放修复**:修正部分中转路径的媒体时钟、往返时间与重复反馈统计,改善这些错误导致的画质恢复停滞;重新进入房间后,SFU 连接仍可继续尝试直连。同步底层握手恢复修复。
- **界面修复**:修复浮窗返回后遮挡全屏、无法操作,窄屏控件错位、菜单超出边界等问题。虚拟键盘长按不会选中文字,拖出、取消或失焦后释放按键。页面和选源面板统一中性底色;App 图标与 favicon 保留小电视轮廓,精修对比度与底板层次。
- **兼容性修复**:新表情按站点支持情况显示,避免旧站点拒收后断开连接。补齐旧浏览器的浮层支持;修正 macOS `.app` 对包外启动文件的依赖。macOS 的 Finder、权限和远控仍需实机验证。

#### 更新说明

沿用 2.x 兼容边界。使用远控需要更新房主 App 和所连接的站点;要获得完整的中转修复,请同时更新 App 与自部署 Server。

### English

This feature update lets a Host hand keyboard and mouse control to one friend while sharing a screen. Chat and reactions now share one interaction window. It also clarifies sharing and playback settings and fixes specific connection-recovery and server-forwarding quality stalls.

#### Features and experience

- **Optional keyboard and mouse control:** the Host can let one friend control the shared desktop or window and revoke access at any time. Changing sources or stopping sharing disables control. Touch controls, zoom and an on-screen keyboard are included. Physical validation currently covers the Windows App.
- **Viewer playback settings:** choose fit, crop or stretch, and Auto, P2P-only or SFU-only routing. Site and Host restrictions still apply.
- **One interaction window:** chat, participants and reactions stay together. Choose a recipient from their chat avatar and use new reactions, including thumbs up and down. Supported browsers can detach the window and keep chat and reactions visible on top. Fullscreen retains the chat-overlay toggle.
- **Clearer sharing and playback controls:** pause and stop stay together, with separate picture, sound, connection and remote-control settings. Pointer movement reveals the playback bar during viewing; remote control uses a small drawer at the picture edge. Clicking the picture no longer pauses it accidentally. Sound controls reflect missing input and retain the Viewer's volume when it returns.

#### Fixes

- **Connection and playback fixes:** correct media clocks, round-trip-time updates and duplicate feedback accounting on affected forwarding paths, improving recovery from the quality stalls caused by those errors. Rejoining a room restores direct-connection attempts while SFU playback continues. Incorporate upstream handshake-recovery fixes.
- **Interface fixes:** returning a detached tool during fullscreen no longer leaves an inaccessible panel over the picture. Fix compact-layout shifts and overflowing menus. Holding a virtual key no longer selects its label; release, cancellation and focus loss clear held keys. Pages and the source picker use shared neutral surfaces. Refine App icons and favicons while retaining the familiar television outline.
- **Compatibility fixes:** show new reactions only when the site supports them, preventing disconnects on older sites. Support popovers on older browsers and remove the macOS `.app` launcher's dependency on files outside the bundle. Finder, permissions and remote input still need physical macOS validation.

#### Updating

The existing 2.x compatibility boundary is retained. Remote control needs an updated Host App and site; update both App and self-hosted Server for the complete forwarding fix.
2026-10-19 01:58:33 +08:00

610 lines
15 KiB
Go

// Package remoteinput owns the source-scoped desktop input grant.
// Only the owning Host control session can choose its authorization policy. Media routing remains
// outside this package; closing its exact source or connection releases input.
package remoteinput
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"io"
"runtime"
"sort"
"sync"
"time"
"github.com/TNTcraftHIM/Piik/internal/app/nativecapture"
"github.com/TNTcraftHIM/Piik/internal/media/inputroute"
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
"github.com/pion/webrtc/v4"
)
const ChannelLabel = inputroute.Label
type bounds struct{ left, top, width, height int32 }
type inputEvent struct {
Type string `json:"type"`
Grant string `json:"grant,omitempty"`
Intent string `json:"intent,omitempty"`
Password string `json:"password,omitempty"`
X float64 `json:"x,omitempty"`
Y float64 `json:"y,omitempty"`
Button int `json:"button,omitempty"`
Down bool `json:"down,omitempty"`
Code string `json:"code,omitempty"`
Text string `json:"text,omitempty"`
Delta int `json:"delta,omitempty"`
DX int32 `json:"dx,omitempty"`
DY int32 `json:"dy,omitempty"`
}
type inputSink interface {
Authorize() error
Target(nativecapture.CaptureTarget, *nativecapture.Stream) (bounds, bool, error)
Apply(bounds, inputEvent) error
Reset() error
}
type View struct {
ID string `json:"id"`
PeerID string `json:"peerId"`
State string `json:"state"`
}
// One manager covers both native sessions: they operate the same physical desktop.
type Manager struct {
mu sync.Mutex
sink inputSink
connections map[endpointKey]*connection
active *connection
closed bool
stop context.CancelFunc
}
type Session struct {
manager *Manager
target nativecapture.CaptureTarget
capture *nativecapture.Stream
paused bool
closed bool
changed chan struct{}
route *inputroute.Relay
mode string
policyChange *PolicyChange
password [32]byte
passwordRetry time.Time
}
type connection struct {
owner *Session
edgeID string
epoch string
id string
requested bool
requestID string
intent string
grant string
suspended bool
peerID string
secret string
cipher *inputCipher
}
type endpointKey struct {
owner *Session
id string
}
// New prepares the platform backend. Input remains disabled until the sharing
// session enables it; no service, driver or startup entry is installed.
func New(ctx context.Context, captureInput bool) (*Manager, error) {
sink, err := newPlatformSink(captureInput)
if err != nil || sink == nil {
return nil, err
}
ctx, cancel := context.WithCancel(ctx)
m := &Manager{sink: sink, connections: make(map[endpointKey]*connection), stop: cancel}
if err := watchStopShortcut(ctx, func() { m.Revoke() }); err != nil {
cancel()
if closer, ok := sink.(io.Closer); ok {
_ = closer.Close()
}
return nil, err
}
// Foreground changes must release held input even when the remote player
// is holding a key without sending more events. Only an active grant polls.
go func() {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
m.mu.Lock()
m.refreshActive()
m.mu.Unlock()
}
}
}()
return m, nil
}
func (m *Manager) NewSession() *Session {
if m == nil {
return nil
}
s := &Session{manager: m, changed: make(chan struct{}, 1), mode: "off"}
s.route = inputroute.New(s.receivePacket)
return s
}
// Changes coalesces notifications; State reads the current permission and grant owner. Input
// packets do not generate UI updates, and no callback runs under the input lock.
func (s *Session) Changes() <-chan struct{} {
if s == nil {
return nil
}
return s.changed
}
func (s *Session) notify() {
select {
case s.changed <- struct{}{}:
default:
}
}
// The channel belongs to the sharing session; input permission belongs to its
// current source. Replacing a picture revokes the grant without losing the
// channel needed to request control of the next source.
func (s *Session) SetTarget(target nativecapture.CaptureTarget, capture *nativecapture.Stream) {
s.setTarget(target, capture, nil)
}
// A Portal permission change replaces its capture as part of that same intent.
// Always bind the installed picture, including after cancellation; only a still
// current change may survive this binding and subsequently enable input.
func (change *PolicyChange) SetTarget(target nativecapture.CaptureTarget, capture *nativecapture.Stream) {
change.session.setTarget(target, capture, change)
}
func (s *Session) setTarget(target nativecapture.CaptureTarget, capture *nativecapture.Stream, change *PolicyChange) {
if s == nil {
return
}
m := s.manager
m.mu.Lock()
defer m.mu.Unlock()
if m.closed || s.closed {
return
}
if target.Kind != "display" && target.Kind != "window" && !(s.UsesCaptureInput() && target.Kind == "picker") {
target = nativecapture.CaptureTarget{}
}
s.target = target
s.capture = capture
if change == nil || s.policyChange != change {
s.cancelPolicy()
}
s.applyPolicy("off", [32]byte{})
}
// The Linux Portal owns input and screen selection in the same OS session.
func (s *Session) UsesCaptureInput() bool { return s != nil && runtime.GOOS == "linux" }
func (s *Session) Attach(edgeID string, channel *webrtc.DataChannel) {
s.route.AttachDown(edgeID, channel)
}
// Bindings come only from the owning Host's authenticated room connection.
// Receiving a packet from a relay never creates or names an input identity.
func (s *Session) SetPeers(peers []protocol.RemoteInputPeer) error {
if s == nil {
return errors.New("remote input is unavailable")
}
m := s.manager
m.mu.Lock()
defer m.mu.Unlock()
if m.closed || s.closed {
return errors.New("remote input is closed")
}
retained := make(map[string]bool, len(peers))
for _, peer := range peers {
if !protocol.ValidInputKey(peer.EndpointID, peer.Secret) || !protocol.ValidOpaqueID(peer.PeerID) || retained[peer.EndpointID] {
return errors.New("invalid input endpoint")
}
retained[peer.EndpointID] = true
}
for _, peer := range peers {
previous := m.connections[endpointKey{s, peer.EndpointID}]
if previous != nil && previous.secret == peer.Secret && previous.peerID == peer.PeerID {
continue
}
if previous == m.active && previous != nil {
m.release()
}
cipher, _ := newInputCipher(peer.Secret)
c := &connection{owner: s, id: peer.EndpointID, peerID: peer.PeerID, secret: peer.Secret, cipher: cipher}
m.connections[endpointKey{s, c.id}] = c
// Signaling and the media return path can arrive in either order.
// Ask this endpoint to sync only after its authenticated key is installed.
ready := true
s.route.Reply(inputroute.Packet{ID: c.id, Ready: &ready})
}
for id, c := range m.connections {
if c.owner == s && !retained[id.id] {
if m.active == c {
m.release()
}
delete(m.connections, id)
}
}
s.notify()
return nil
}
func (s *Session) receivePacket(edgeID string, packet inputroute.Packet) {
m := s.manager
m.mu.Lock()
c := m.connections[endpointKey{s, packet.ID}]
if c == nil || m.closed || s.closed {
m.mu.Unlock()
return
}
if packet.Closed {
if c.edgeID == edgeID && c.epoch == packet.Epoch {
m.revoke(c)
c.edgeID = ""
}
m.mu.Unlock()
return
}
plain, err := c.cipher.open(packet.Data, packet.Epoch)
if err != nil {
m.mu.Unlock()
return
}
if c.edgeID != "" && (c.edgeID != edgeID || c.epoch != packet.Epoch) {
m.revoke(c)
}
c.edgeID, c.epoch = edgeID, packet.Epoch
m.mu.Unlock()
m.receive(c, webrtc.DataChannelMessage{IsString: true, Data: plain}, packet.Epoch)
}
func (m *Manager) send(c *connection, state string) {
m.reply(c, state, "")
}
func (m *Manager) reply(c *connection, state, reason string) {
if c.epoch == "" {
return
}
if state == "ready" && (!c.owner.available() || c.owner.mode == "off") {
state = "unavailable"
}
data, _ := json.Marshal(struct {
Platform string `json:"platform"`
State string `json:"state"`
Grant string `json:"grant,omitempty"`
Intent string `json:"intent,omitempty"`
Mode string `json:"mode"`
Reason string `json:"reason,omitempty"`
}{runtime.GOOS, state, c.grant, c.intent, c.owner.mode, reason})
c.owner.route.Reply(inputroute.Packet{ID: c.id, Epoch: c.epoch, Data: c.cipher.seal(data, c.epoch)})
}
func (s *Session) available() bool {
return !s.closed && !s.paused && s.target.SourceID != ""
}
func (m *Manager) state(c *connection) string {
if m.active == c {
if c.suspended {
return "suspended"
}
return "active"
}
if c.requested {
return "pending"
}
return "ready"
}
// Caller holds mu. Source identity, geometry and focus are checked at the
// input owner, never inferred from a Browser's last displayed video frame.
func (m *Manager) refreshActive() (bounds, bool) {
c := m.active
if c == nil {
return bounds{}, false
}
b, focused, err := m.sink.Target(c.owner.target, c.owner.capture)
if err != nil {
m.release()
m.send(c, "failed")
return bounds{}, false
}
suspended := !focused
if c.suspended != suspended {
c.suspended = suspended
if !focused {
_ = m.sink.Reset()
}
m.send(c, m.state(c))
c.owner.notify()
}
return b, focused
}
func (m *Manager) release() {
if c := m.active; c != nil {
m.active = nil
c.grant = ""
c.suspended = false
_ = m.sink.Reset()
m.send(c, "ready")
c.owner.notify()
}
}
func (m *Manager) receive(c *connection, message webrtc.DataChannelMessage, epoch string) {
if !message.IsString || len(message.Data) > maxInputBytes {
return
}
var event inputEvent
decoder := json.NewDecoder(bytes.NewReader(message.Data))
decoder.DisallowUnknownFields()
if decoder.Decode(&event) != nil || decoder.Decode(&struct{}{}) != io.EOF {
return
}
m.mu.Lock()
defer m.mu.Unlock()
if m.closed || c.owner.closed || m.connections[endpointKey{c.owner, c.id}] != c || c.epoch != epoch {
return
}
switch event.Type {
case "sync":
m.send(c, m.state(c))
case "request":
if !c.owner.available() || c.owner.mode == "off" || c.requested || c == m.active {
return
}
if intent, err := hex.DecodeString(event.Intent); err != nil || len(intent) != 16 {
return
}
id := make([]byte, 16)
_, _ = rand.Read(id)
c.requestID, c.intent = hex.EncodeToString(id), event.Intent
if c.owner.mode != "ask" {
if reason := c.owner.authorize(event.Password); reason != "" {
m.reply(c, "ready", reason)
return
}
// Automatic permission never displaces another controller, including
// a controller of the App's other native session.
if m.active != nil {
m.reply(c, "ready", "busy")
return
}
m.grant(c)
return
}
c.requested = true
m.send(c, "pending")
c.owner.notify()
case "release":
if event.Intent != c.intent || (!c.requested && m.active != c) {
return
}
c.requested = false
if m.active == c {
m.release()
} else {
m.send(c, "ready")
}
c.owner.notify()
default:
if m.active != c || c.grant == "" || event.Grant != c.grant || !c.owner.available() {
return
}
if event.Type == "reset" {
_ = m.sink.Reset()
return
}
b, focused := m.refreshActive()
if !focused {
return
}
var err error
if event.Type == "text" {
err = applyText(m.sink, b, event.Text)
} else {
err = m.sink.Apply(b, event)
}
if err != nil {
m.release()
m.send(c, "failed")
}
}
}
func (m *Manager) revoke(c *connection) {
c.requested = false
if m.active == c {
m.release()
} else {
m.send(c, "ready")
}
c.owner.notify()
}
func (s *Session) Pause(paused bool) {
if s == nil {
return
}
m := s.manager
m.mu.Lock()
defer m.mu.Unlock()
s.paused = paused
if paused {
s.cancelPolicy()
}
if paused && m.active != nil && m.active.owner == s {
m.release()
}
for _, c := range m.connections {
if c.owner == s {
if paused {
c.requested = false
}
m.send(c, m.state(c))
}
}
s.notify()
}
// Transport loss retires permission, not the media-owned channel. A recovered
// ICE connection can carry a new request, but never restore the previous grant.
func (s *Session) RevokeEdge(edgeID string) {
if s == nil {
return
}
m := s.manager
m.mu.Lock()
defer m.mu.Unlock()
for _, c := range m.connections {
if c.owner == s && c.edgeID == edgeID {
m.revoke(c)
}
}
}
func (s *Session) Close() {
if s == nil {
return
}
m := s.manager
m.mu.Lock()
s.closed = true
s.cancelPolicy()
if m.active != nil && m.active.owner == s {
m.release()
}
for id, c := range m.connections {
if c.owner == s {
delete(m.connections, id)
}
}
s.notify()
m.mu.Unlock()
s.route.Close()
}
func (m *Manager) Revoke() {
if m == nil {
return
}
m.mu.Lock()
defer m.mu.Unlock()
if m.active != nil {
m.active.owner.setPolicy("ask", [32]byte{})
}
}
func (m *Manager) Close() {
if m == nil {
return
}
m.mu.Lock()
if m.closed {
m.mu.Unlock()
return
}
m.closed = true
m.release()
if closer, ok := m.sink.(io.Closer); ok {
_ = closer.Close()
}
m.mu.Unlock()
if m.stop != nil {
m.stop()
}
}
type State struct {
Platform string `json:"platform"`
Mode string `json:"mode"`
Requests []View `json:"requests"`
}
func (s *Session) State() State {
state := State{Platform: runtime.GOOS, Mode: "off", Requests: []View{}}
if s == nil {
return state
}
m := s.manager
m.mu.Lock()
defer m.mu.Unlock()
state.Mode = s.mode
for _, c := range m.connections {
if c.owner == s && (c.requested || m.active == c) {
state.Requests = append(state.Requests, View{c.requestID, c.peerID, m.state(c)})
}
}
sort.Slice(state.Requests, func(i, j int) bool { return state.Requests[i].ID < state.Requests[j].ID })
return state
}
func (s *Session) Decide(id string, allow bool) error {
if s == nil {
return errors.New("remote input is unavailable")
}
m := s.manager
m.mu.Lock()
defer m.mu.Unlock()
var c *connection
for _, candidate := range m.connections {
if candidate.owner == s && candidate.requestID == id {
c = candidate
break
}
}
if m.closed || c == nil || !s.available() || c.edgeID == "" || (!c.requested && m.active != c) {
return errors.New("request is no longer available")
}
defer s.notify()
if !allow {
if s.mode != "ask" {
s.setPolicy("ask", [32]byte{})
return nil
}
c.requested = false
if m.active == c {
m.release()
} else {
m.send(c, "ready")
}
return nil
}
m.grant(c)
return nil
}
func (m *Manager) grant(c *connection) {
m.release()
token := make([]byte, 32)
_, _ = rand.Read(token)
c.grant, c.requested = hex.EncodeToString(token), false
m.active = c
m.refreshActive()
if m.active == c {
m.send(c, m.state(c))
}
c.owner.notify()
}