Compare commits

..
57 Commits
Author SHA1 Message Date
VallierDevandClaude Opus 5.5 6f1dd77ab5 Release v0.1.31
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 14:40:20 +08:00
VallierDevandClaude Opus 5.5 e5bb214f99 test: run CONTROL lifecycle tests on the virtual network
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 14:27:41 +08:00
25edc41b9f fix(portal): recover capture sooner after GNOME display changes (#35)
* fix(portal): recover capture sooner after GNOME display changes

Use Mutter notifications to enable a 200 ms grace for correlated, continuously paused capture without a valid format. Cancel stale hints on healthy renegotiation and retain the five-second fallback.

Test event ordering, repeated and stale notifications, natural recovery, and subscription teardown. Document measured recovery gains and limits.

Fixes #27

* docs: keep the #27 experiment record in the PR instead of the repository

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: VallierDev <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-17 14:26:54 +08:00
VallierDevandClaude Opus 5.5 1fa58904d7 Release v0.1.30
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 14:22:20 +08:00
VallierDevandClaude Opus 5.5 da49e71b9d fix(x11): keep the shared display open until the last capture is dropped
The X11 engine has one process-wide display connection, but every
ScreenCapture closed it on drop. During guest assistance the device
init's screen-size probe created and dropped a capture after the main
one, closing the display under it: the stream stopped with
"X11 display wake failed" and the viewer got no video. A capture
replaced after a resize had the same hazard. Count live X11 captures
and close only on the last drop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 14:19:49 +08:00
VallierDevandClaude Opus 5.5 80ac4448d6 Release v0.1.29
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 11:46:30 +08:00
cc01b3e2a5 fix(portal): send black video while locked so iPhone starts auto unlock (#34)
* fix(portal): bootstrap video for iPhone locked auto unlock

* docs: keep the #29 experiment record in the PR instead of the repository

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: VallierDev <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-17 11:44:15 +08:00
bb95774ecb fix(portal): establish desktop CONTROL while GNOME is locked and auto unlock cold connections (#30)
* fix(portal): establish locked desktop control before capture

* test(portal): preserve v4 pixel inventory after deferred capture settings

* fix(control): refresh lock state after viewer Echo handshake

* fix(portal): announce desktop target before locked capture wait

* ci: run the desktop_power native test under ASan/UBSan

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* input: skip the Portal release sweep while no session is bound

The first locked connection creates its peer before any Portal exists; the sweep
then logged two failed injections and a 'reset incomplete' warning for nothing.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* docs: describe locked cold connections, auto unlock limits and iPhone status

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* fix(portal): treat a cached screen with a closed Portal session as not ready

begin() now marks the state as waiting when the cached session is dead, and
permission/video/clipboard consumers honor it instead of reporting Allow and
starting the stale video path before maintain() has rebuilt capture.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* fix(portal): gate input on a ready desktop and share recovery backoff

Input dispatch kept only the cached screen and dropped State.waiting, so a closed
Portal passed the gate once the screen unlocked. The supervisor's recover() and the
encoder's stream-loss recovery now share failure backoff and a terminal-denial flag,
so a denied consent is not re-prompted by the other path.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* fix(control): keep unknown lock state unlocked, stop waits on Closing, never drop capture settings

- QuerySystemState with no logind lock state (Xvfb/linger) reports unlocked as before.
- The Portal wait loop also exits while the channel is Closing.
- Capture-setting requests back-pressure instead of try_send dropping the latest setting.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Co-authored-by: VallierDev <[email protected]>
2026-10-17 10:48:43 +08:00
VallierDevandClaude Opus 5.5 369b493e47 Release v0.1.28
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 02:38:28 +08:00
VallierDevandClaude Opus 5.5 ce28d7e230 Tests: run local WebRTC peers on a virtual network
The codec-negotiation and ICE/SCTP acceptance tests used real local sockets
and occasionally hit the 30 s connect timeout when the whole suite ran in
parallel. They now use webrtc-rs vnet (shared test_network helper), so they
are deterministic and the acceptance test no longer needs #[ignore].
Test-only change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 02:37:53 +08:00
VallierDevandClaude Opus 5.5 b0beba1a4e desktop-video-probe: fixed input via UU_PROBE_INPUT (#9)
UU_PROBE_INPUT=<video or image sequence> bypasses desktop capture and encodes
the file with the production FFmpeg encoder arguments, so encoder
measurements no longer depend on desktop content; UU_PROBE_LOOP=1 repeats the
input up to UU_PROBE_FRAMES. Missing/undecodable input exits non-zero. The
session path is unchanged. Verified with Y4M, MP4 and PNG sequences (repeat
runs byte-identical); NVENC/VAAPI not hardware-verified.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 01:50:24 +08:00
VallierDevandClaude Opus 5.5 77a556c674 Release v0.1.27
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 01:26:24 +08:00
VallierDevandClaude Opus 5.5 e89c91e83b Portal: a failed staleness check no longer ends the host loop
Follow-up to #26: retire_if_closed errors (poisoned lock, panicked task) are
logged and treated as not stale instead of returning from serve().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 01:08:59 +08:00
wanglz111 f35ec3b2c7 Portal: recover capture after live scaling changes (#26)
* fix(portal): recover capture after live scaling changes

* fix(portal): keep input available during session recovery

* fix(portal): serialize reconnect recovery and throttle lock probes
2026-10-17 01:08:32 +08:00
VallierDevandClaude Opus 5.5 aa4dca75d2 Release v0.1.26
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 00:50:13 +08:00
VallierDevandClaude Opus 5.5 d1791ef861 File transfer: omit the inline listing copy when the reply would not fit
Large folders (5600 entries) were sent both inline and as zlib dir_data in one
message, beyond the 256 KB SCTP limit, so the reply was dropped and the viewer
spun until its timeout. dir_data (what current viewers read) is always sent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 00:46:58 +08:00
VallierDevandClaude Opus 5.5 90e802b94a Report the display refresh rate; NVENC encodes BGRA directly (#16)
display_info/capability/codec negotiation reported a fixed 60 fps; they now
use the monitor mode (hyprctl on Hyprland, xrandr on X11, UU_DISPLAY_FPS
override, 60 otherwise; external tools bounded to 1 s). NVENC 4:2:0 without
scaling takes bgra/bgr0 input and converts on the GPU (BT.601 limited range,
same tagging as swscale), saving the CPU conversion; UU_NVENC_RGB=0 restores it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 00:34:46 +08:00
VallierDevandClaude Opus 5.5 6226e723b5 SCTP: send messages up to 256 KB like libwebrtc peers advertise
webrtc-rs fixed the association send limit at 64 KB regardless of the remote
a=max-message-size, so a large directory listing (5600 entries, ~73 KB
compressed) failed with 'outbound packet larger than maximum message size'
and the viewer waited until its timeout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 00:34:46 +08:00
VallierDevandClaude Opus 5.5 296f0fdbcc Release v0.1.25
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 21:45:44 +08:00
wanglz111 b20fd6ccef Portal: map scaled GNOME input through EIS regions (#24)
* fix(portal): map scaled GNOME input through EIS regions

Keep unsupported portals on legacy input and leave capture/uinput independent of EIS. Cache logical geometry without the native handle lock and reject geometry changes during recovery.

Add native capability, handshake, geometry and lifecycle regression coverage with sanitizer checks; preserve existing non-portal paths. Refs #23.

* fix(portal): keep version 4 capture settings in pixel coordinates

Keep native, selectable and current screen rectangles in pixels with DPR 1 so legacy setting requests cannot implicitly downscale a scaled desktop. Leave Portal and EIS coordinate mapping local and unchanged.

Add inventory/request/ack/sync roundtrip regression coverage, including repeated settings changes and restoring native output. Refs #23.
2026-10-16 21:45:17 +08:00
VallierDevandClaude Opus 5.5 51b489b1e2 Log whether an offer's client_id equals the be-controlled client_id (boolean only)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 18:19:22 +08:00
VallierDevandClaude Opus 5.5 fb09b60b8a Release v0.1.24
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 18:09:38 +08:00
GuanchengLiandClaude Code 37504c7fe8 Adapt FFmpeg output timing args to detected capabilities (#22)
* Adapt FFmpeg output timing args to detected capabilities

Probe the selected FFmpeg (PATH or UU_FFMPEG_ROOT/config ffmpeg_root)
once per process with `-h full` and cache the result per executable
environment (program + LD_LIBRARY_PATH + PATH). FFmpeg 5+ gets
`-fps_mode vfr -enc_time_base 1/<fps>`; older builds such as Ubuntu
22.04's FFmpeg 4.4 fall back to `-vsync 0`. Probe failures, timeouts
and unparseable help output all take the legacy path, and no output
side `-r` is ever emitted. HEVC availability probing stays independent.

Verified on FFmpeg 4.4.2: 20 video tests pass and desktop-video-probe
emits 5 H.264 frames with the first frame an IDR.

Co-Authored-By: Claude Code <[email protected]>

* Keep modern timing args when the FFmpeg probe fails

A failed probe (spawn error, non-zero exit, timeout, or no parseable
help text) previously fell back to -vsync 0, but -vsync is deprecated
since FFmpeg 5.1 and a newer build may reject it, killing the encoder.
Legacy is now selected only from a completed help dump that visibly
lacks -fps_mode/-enc_time_base; everything else keeps
-fps_mode vfr -enc_time_base 1/fps. Also fix the encoder comment: the
same-slot drop belongs to vfr on a 1/fps timebase, while -vsync 0
passes catch-up frames through (a known degradation on FFmpeg 4.x).

Co-Authored-By: Claude Code <[email protected]>

---------

Co-authored-by: Claude Code <[email protected]>
2026-10-16 18:09:13 +08:00
VallierDevandClaude Opus 5.5 41ba1e9440 Log whether an offer's ice_id equals the be-controlled ice_id (boolean only)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 17:17:54 +08:00
VallierDevandClaude Opus 5.5 1e4bec4514 Log offer SDP structure (attribute names, extmap URIs, payload counts only)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 15:57:49 +08:00
VallierDevandClaude Opus 5.5 b4b872878f Log the structure of incoming SOAC offers (keys, types, lengths only)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 15:44:14 +08:00
VallierDevandClaude Opus 5.5 b5b5120ef7 Release v0.1.23
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 13:26:02 +08:00
VallierDevandClaude Opus 5.5 5028ba1663 Test: pair only UDP/IPv4 host candidates in the codec negotiation test
Pairing every host interface (VPNs, IPv6) let ICE exceed the 10 s connect
timeout when the suite ran in parallel; the suite now finishes in 5 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 13:25:53 +08:00
VallierDevandClaude Opus 5.5 6058185d1f desktop-video-probe: UU_PROBE_QUALITY selects a viewer bitrate preset
The probe always used Auto (CRF), so the ABR path of the bitrate presets
(#15) could not be measured without a viewer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 13:24:31 +08:00
VallierDevandClaude Opus 5.5 6882b76801 Wayland: free cached dma-buf on SHM fallback; recapture after a stall
Review follow-ups to #20: the copy-failure fallback cleared frame state
but no longer released cached dma-buf buffers, and after a backpressure
stall the engine's prefetched frame was as old as the stall. Release
v0.1.22.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 12:36:06 +08:00
VallierDevandClaude Opus 5.5 145bf9d25b Release v0.1.21
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 12:31:38 +08:00
wenmou e603761aa8 Video: reuse capture buffers, cache unchanged frames and propagate send backpressure (#20)
* Wayland: reuse DMA-BUF capture buffers and release protocol objects

* Video: cache unchanged frames and propagate send backpressure
2026-10-16 12:31:00 +08:00
VallierDevandClaude Opus 5.5 a28800ba44 Trust system certificates and show transport error causes (#21)
reqwest used only the bundled webpki roots, so TLS-intercepting proxies
with a custom CA (SSL_CERT_FILE) failed with a bare "error sending
request". Enable native roots (rustls-native-certs honors SSL_CERT_FILE
and SSL_CERT_DIR) and report the error source chain with URL user-info
and query strings removed. Release v0.1.20.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 12:27:19 +08:00
VallierDevandClaude Opus 5.5 c080b5f653 Do not report a viewer-closed desktop stream as an error
The final send after the viewer closes fails with "codec is not supported
by remote", which read like an encoder negotiation bug.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:47:58 +08:00
VallierDevandClaude Opus 5.5 c5ba87f3ad Log streamer_data element shapes and control id relation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:45:04 +08:00
VallierDevandClaude Opus 5.5 edaa27fe6b Log the structure of connect requests (numbers, types, lengths only)
Documents what the viewer and cloud send in be-controlled (ConnectOptions
nesting, streamer_data and header value types) without rendering string
or byte contents, large numbers or identifiers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:34:12 +08:00
VallierDevandClaude Opus 5.5 d630fc4128 Release v0.1.19
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:24:51 +08:00
VallierDevandClaude Opus 5.5 aeaf818c39 Signaling: treat a connection without pings as dead and reconnect (#19)
Only a close frame or read error ended the signaling loop, so a half-open
socket after suspend or a NAT drop left the host believing it was online.
Per Engine.IO 4, report Disconnected when no ping arrives within the
server's pingInterval + pingTimeout so the existing reconnect loop runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:24:51 +08:00
VallierDevandClaude Opus 5.5 d5e9007dd6 Release v0.1.18
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:16:49 +08:00
VallierDevandGemini 4 Argon f876ee1ff3 Wayland: reuse per-slot SHM buffers across screencopy frames
Retain memfd, mmap, wl_shm_pool, and wl_buffer per pipeline slot when
dimensions, stride, and format remain unchanged, avoiding 33 MB 4K
allocations and unmaps on every frame. Rebuild on geometry/format
changes or aborted in-flight copies, and release all SHM resources on
reset/shutdown.

Co-Authored-By: Gemini 4 Argon <[email protected]>
2026-10-16 11:01:26 +08:00
VallierDevandClaude Opus 5.5 6cdc177b9e Wayland capture: pace from buffer availability; log capture exit
Review follow-ups to f0506bc: a stalled encoder read no longer shortens the
next capture interval, and the capture process exit status is logged
because nothing awaits its task.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 11:01:26 +08:00
VallierDevandClaude Opus 5.5 567662a576 X11: pick up desktop resizes between connections (#18)
The root geometry and XShm image were read once at startup, so after a
RandR or xrdp resize every new connection asked x11grab for the old size
and stalled on "transferring picture". Query the root geometry before
each desktop connection and rebuild the capture when it changed; guard
capture with a lock and reject a stale-sized buffer instead of
overflowing it. The HEVC fallback log now includes the actual failure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 10:56:48 +08:00
VallierDevandClaude Opus 5.5 f0506bcf48 Wayland: pipe capture straight into the encoder and overlap capture with writes; fix terminal races
The native Wayland session relayed each 33 MB 4K frame through this process
and the capture loop wrote synchronously, so a slow encoder read delayed the
next screencopy request (~65 fps at a 144 fps request versus 87-92 fps for
bare capture, #16). Hand the capture stdout to FFmpeg directly, double-buffer
capture against the pipe write on a separate thread, and enlarge the pipe.

Terminal: hold the size lock across the ioctl so the delayed redraw restore
cannot reapply a stale size, and record scrollback and live output under the
sink lock so a reattach never replays a chunk that is also sent live.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 10:29:09 +08:00
VallierDevandClaude Opus 5.5 3d08d81b0d Log per-window frame counts in latest-frame stats
sent/skipped are cumulative while max_* are per-window peaks, which was
easy to misread (#16). Add window_s, window_sent and window_skipped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 09:46:10 +08:00
VallierDevandClaude Opus 5.5 5e92abe373 Release v0.1.17
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 01:30:24 +08:00
VallierDevandClaude Opus 5.5 b63bedd4dc Repaint full-screen terminal programs on reattach; ABR for libx265 bitrate presets
Reattaching replays the scrollback tail, which starts mid-redraw for
full-screen programs (tmux, vim, TUIs), and an unchanged size raises no
SIGWINCH. Shrink the PTY by one row and restore the latest requested size
after a pause so the program repaints (#17).

Apply the #15 ABR change to the libx265 path as well, clarify the
non-wayland build error, and document the Wayland systemd unit and uinput
module autoload (#16).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 01:30:09 +08:00
wenmou 9c1ae123db Wayland: keep capturing without output power management; x264 ABR for bitrate presets (#15)
* Wayland: keep capturing when output power management is unavailable

keep_display_awake() runs on the desktop video start path, so a compositor\nwithout wlr-output-power-management made uu_wl_wake() return -1 and aborted\nthe whole desktop stream. Treat the missing protocol as nothing to wake.

* x264: average bitrate for the bitrate presets so 4K keyframes fit

Capped CRF plus VBV cannot fit a large IDR frame at 4K and a low cap, so\nkeyframes came out near-grey. NVENC and VAAPI already carry a target bitrate;\ngive x264 the same and keep CRF only for automatic quality.
2026-10-16 01:30:00 +08:00
VallierDevandClaude Opus 5.5 708c770b19 Support H.265 (HEVC), selected by the host when both sides support it
One video track carries H.264 or H.265 (RFC 7798 payloader); the host
announces its encoders with CONTROL Message 18 CodecNegotiation and picks
H.265 when the viewer negotiated it (the official Mac viewer offers H265
first; CaptureSettingRequest field 13 only reports the current codec).
hevc_nvenc / libx265 / hevc_vaapi with fallback to software HEVC, then
H.264; UU_VIDEO_CODEC=h264 forces H.264. True color 4:4:4 applies to H.265.
Verified with the official Mac viewer on NVENC.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 00:13:54 +08:00
VallierDevandClaude Opus 5.5 ff84aa4fd2 Support true color (H.264 4:4:4) and log viewer decoder capabilities
The viewer sends CaptureSettingRequest field 7 = 3 when 真彩 is enabled
(ff_capture_setting versions are cumulative, so our version 4 already
implies chroma sampling). Encode yuv444p with NVENC high444p / x264
high444 and report the encoded chroma in CaptureSettingSync; VAAPI and
inproc stay 4:2:0. Log ConnectOptions decoder_cap_list (numbers only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 23:58:54 +08:00
VallierDevandClaude Opus 5.5 4df95ea271 Release v0.1.14
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 23:12:28 +08:00
cnetst be7dba4669 Fix black terminal after reconnecting with a large scrollback (#14) 2026-10-15 23:12:09 +08:00
VallierDevandClaude Opus 5.5 61dfb2edf5 Release v0.1.13
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 23:02:08 +08:00
VallierDevandClaude Opus 5.5 d4483e2bbd Tray icon (AppIndicator via dlopen, GtkStatusIcon fallback); show restarts the service
Tray menu: open main window, allow switch (synced), quit. Quit stops the
systemd user unit (found via /proc/self/cgroup) so Restart=always does not
bring it back; `uuremote-server show` starts the installed unit when the host
is not running. SIGUSR1 handler is installed before the pid file is written.
Verified on Ubuntu 24.04 GNOME (X11).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 23:02:08 +08:00
VallierDevandClaude Opus 5.5 0de74566a9 README: separate the Linux login screen limit from UU account sign-in
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:54:27 +08:00
VallierDevandClaude Opus 5.5 edee53a022 Release v0.1.12
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:50:24 +08:00
VallierDevandClaude Opus 5.5 fd3caf0d15 Mac shortcuts for macOS viewers (Cmd held back, terminal-aware)
With the viewer option 「使用 macOS 快捷键」 off, Cmd arrives as VK_LWIN
(verified: Cmd 0x5B, Option 0xA4, Ctrl 0xA2; controller_platform 4 = macOS).
For macOS controllers Cmd+C/V/A/F/X/Z become Ctrl+key, Cmd+C/V become
Ctrl+Shift+C/V in a focused terminal, Ctrl+C keeps interrupting; other
combos and Cmd alone stay Super. Verified with the official Mac viewer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:50:24 +08:00
VallierDevandClaude Opus 5.5 ce7906510f Test: read the assist PTY banner until complete (flaky single read on CI)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:26:35 +08:00
68 changed files with 7114 additions and 469 deletions

No files matched your search

+13 -1
View File
@@ -28,10 +28,22 @@ jobs:
sudo apt-get update
sudo apt-get install -y build-essential pkg-config \
libx11-dev libxext-dev libxtst-dev libxfixes-dev libgtk-3-dev \
libwayland-dev libxkbcommon-dev libglib2.0-dev libpipewire-0.3-dev
libwayland-dev libxkbcommon-dev libglib2.0-dev libpipewire-0.3-dev libei-dev libeis-dev
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo test --locked --features wayland,portal
- name: Native portal and EIS regression tests (ASan/UBSan)
env:
ASAN_OPTIONS: detect_leaks=1
UBSAN_OPTIONS: halt_on_error=1
run: |
for source in tests/portal_engine.c tests/portal_input.c tests/portal_eis.c tests/desktop_power.c; do
target="/tmp/$(basename "$source" .c)"
cc -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer \
-Wall -Wextra -Wno-unused-parameter "$source" -o "$target" \
$(pkg-config --cflags --libs gio-2.0 gio-unix-2.0 libpipewire-0.3 libei-1.0 libeis-1.0) -lpthread -lm
"$target"
done
test-inproc:
runs-on: ubuntu-24.04
+18 -4
View File
@@ -40,7 +40,7 @@ sudo apt install build-essential pkg-config \
# --features inproc-av: libavcodec-dev libavformat-dev libavdevice-dev libswscale-dev libclang-dev
# (FFmpeg 6.x; in-process capture/encoding, Ubuntu 24.04 ships it)
# --features wayland: libwayland-dev libxkbcommon-dev wl-clipboard
# --features wayland,portal: + libglib2.0-dev libpipewire-0.3-dev
# --features wayland,portal: + libglib2.0-dev libpipewire-0.3-dev libei-dev (libei >= 1.1)
```
Rust stable via rustup. Commands:
@@ -106,9 +106,11 @@ viewer does not reconnect them automatically.
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
| `UU_INPUT_BACKEND` | `auto` (uinput on Wayland/portal when `/dev/uinput` is accessible, udev `uaccess` rule in README) / `uinput` / `native`. X11 keeps XTest (held-key autorepeat handling) |
| Mac viewer Cmd | With 「使用 macOS 快捷键」 off the Mac viewer sends Cmd=VK_LWIN 0x5B, Option=0xA4, Ctrl=0xA2; connect header `controller_platform`=4 is macOS. For Mac controllers Cmd is held back: Cmd+C/V/A/F/X/Z → Ctrl+key (terminal: Cmd+C/V → Ctrl+Shift+C/V), other combos/alone → Super |
| `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) |
| `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` |
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: explicit Intel/AMD; `nvenc`: force; unset: NVENC→CPU only if `/proc/driver/nvidia/version` or `/dev/nvidia0` exists, else CPU (#10) |
| `UU_VIDEO_CODEC=h264` | force H.264 even when the viewer negotiated H.265 (default: H.265 when both sides support it) |
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
@@ -125,7 +127,8 @@ every 60 s.
| `src/signaling.rs` | Engine.IO v4 / Socket.IO client; `be-controlled` binary attachment = ConnectOptions (field 1 capture_type) |
| `src/auth.rs`, `src/config.rs`, `src/login.rs`, `src/login_ui.rs` | signed REST client, config, device init + QR login, GTK window |
| `src/webrtc.rs` | peer connection, data channels, CONTROL dispatch, initialization messages |
| `src/video.rs`, `src/capture_setting.rs` | FFmpeg H.264 pipeline; viewer quality/fps/resolution settings |
| `src/video.rs`, `src/capture_setting.rs`, `src/video_track.rs`, `src/hevc.rs` | FFmpeg H.264/H.265 pipeline, one video track for both codecs (RFC 7798 HEVC payloader); viewer quality/fps/resolution/chroma settings |
| `src/locked_video.rs` | low-rate synthetic black video on the negotiated track while Portal is pending and locked; enables iPhone cold auto unlock (#29), requires FFmpeg libx264/libx265 for the selected codec |
| `src/audio.rs` | Pulse/PipeWire monitor → Opus audio track |
| `src/input.rs`, `src/input_mapping.rs`, `src/uinput.rs` | input injection (X11 XTest / Wayland / portal / uinput), Windows VK → keysym → evdev |
| `src/cursor.rs` | pointer shape mirroring (X11) |
@@ -137,12 +140,13 @@ every 60 s.
| `src/initialization.rs`, `src/control_protocol.rs`, `src/protocol.rs` | protobuf encoders/decoders for CONTROL |
| `src/session_ice.rs`, `src/network_diagnostics.rs` | cloud ICE servers, address-free path diagnostics |
| `src/desktop_backend.rs`, `src/wayland.rs`, `src/portal*.rs` | backend selection, Hyprland, GNOME/KDE portal |
| `c_engine/portal_eis.inc` | scaled GNOME input through the existing Portal session's EIS connection; all input must use EIS once connected |
| `c_engine/x11_engine.c`, `c_engine/clipboard.c` | X11 capture/input/selection |
| `c_engine/wayland_engine.c`, `c_engine/portal_engine.c` | Wayland and portal engines |
| `src/assist.rs`, `c_engine/assist_window.c` | remote assistance (guest + account) and the unified GTK main window (assist + account/QR card) |
Diagnostic subcommands: `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected),
`desktop-probe [new.png]`, `desktop-video-probe`, `wayland-input-probe`, `clipboard-acceptance`,
`desktop-probe [new.png]`, `desktop-video-probe` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto; `UU_PROBE_INPUT` fixed video/image sequence, `UU_PROBE_LOOP=1` repeats), `wayland-input-probe`, `clipboard-acceptance`,
`portal-acceptance`, `help`. Unknown arguments exit with code 2. Builds up to v0.1.0 did **not** reject them and
started a second full host server instead, so check that a subcommand exists in the binary you run.
@@ -163,7 +167,17 @@ started a second full host server instead, so check that a subcommand exists in
- CaptureSettingRequest: field 1 is an **fps option index** (1=30, 2=60, 3=90); the real frame rate is
field 18 `fpsCount`. Field 2 quality: 1 Fast, 2 General (高清 2M), 3 HD (超清 8M), 4 Bluray (原画 20M),
5 Auto, 6 Custom. Resolution: 16/17 pixel size or 5/6, 10 resolution_type (3 = follow remote).
Reply `RpcResponse{2 CaptureSettingResponse{}}` (no errors = applied) and send Message 25 CaptureSettingSync.
Field 7 `chorma_format` (ChromaFormat 1 = 4:2:0, 3 = 4:4:4) is sent when 真彩 is toggled; ff_capture_setting versions
are cumulative (3 CHROMA_SAMPLING, 4 CUSTOM_BITRATE, 5 DPI_SCALE, 6 HDR). 4:4:4 = NVENC `high444p` / x264 `high444`
with `yuv444p`; VAAPI and inproc stay 4:2:0.
Reply `RpcResponse{2 CaptureSettingResponse{}}` (no errors = applied) and send Message 25 CaptureSettingSync
(field 3 = the chroma actually encoded).
- ConnectOptions field 4 `decoder_cap_list` = DecoderCap {1 fps, 2 codec_type (1 H264, 2 H265), 3 width, 4 height,
5 chroma_format}. The Mac viewer lists H.264 and H.265, each 4:2:0 and 4:4:4, up to 3840x2160@120.
- Codec choice is the host's (official negotiateCodec): the Mac viewer offers H265 before H264 in SDP and
CaptureSettingRequest field 13 only reports its current codec. The host sends CONTROL Message 18
CodecNegotiation {1 EncoderCap {1 capture_mode=1, 2 fps, 3 codec_type, 4 width, 5 height}} at CONTROL open and
selects H.265 when both m-line codecs are bound (`UU_VIDEO_CODEC=h264` forces H.264).
- SystemMetrics (Message 13): GeneralMetric `2 system_sound_support` is an enum, **2 = supported** (1 = not
supported); SettingMetric 1 keep_mute, 2 mute_permanent, 3 auto_lock, 5 file_transfer_save_location,
8/9/10 permission states (2 = allow).
Generated
+71 -1
View File
@@ -382,6 +382,22 @@ version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
[[package]]
name = "core-foundation"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cpufeatures"
version = "0.2.17"
@@ -969,6 +985,7 @@ dependencies = [
"hyper",
"hyper-util",
"rustls 0.23.45",
"rustls-native-certs",
"tokio",
"tokio-rustls",
"tower-service",
@@ -1417,6 +1434,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
[[package]]
name = "openssl-probe"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
[[package]]
name = "p256"
version = "0.13.2"
@@ -1797,6 +1820,7 @@ dependencies = [
"pin-project-lite",
"quinn",
"rustls 0.23.45",
"rustls-native-certs",
"rustls-pki-types",
"serde",
"serde_json",
@@ -1946,6 +1970,18 @@ dependencies = [
"zeroize",
]
[[package]]
name = "rustls-native-certs"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dab5152771c58876a2146916e53e35057e1a4dfa2b9df0f0305b07f611fdea4d"
dependencies = [
"openssl-probe",
"rustls-pki-types",
"schannel",
"security-framework",
]
[[package]]
name = "rustls-pki-types"
version = "1.15.1"
@@ -1989,6 +2025,15 @@ version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
[[package]]
name = "schannel"
version = "0.1.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -2031,6 +2076,29 @@ dependencies = [
"zeroize",
]
[[package]]
name = "security-framework"
version = "3.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d"
dependencies = [
"bitflags 2.13.2",
"core-foundation",
"core-foundation-sys",
"libc",
"security-framework-sys",
]
[[package]]
name = "security-framework-sys"
version = "2.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "semver"
version = "1.0.28"
@@ -2649,8 +2717,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uu-remote-linux"
version = "0.1.11"
version = "0.1.31"
dependencies = [
"async-trait",
"base64 0.22.1",
"bytes",
"cc",
@@ -2664,6 +2733,7 @@ dependencies = [
"log",
"nix 0.29.0",
"qrcode",
"rand 0.8.8",
"reqwest",
"serde",
"serde_json",
+4 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "uu-remote-linux"
version = "0.1.11"
version = "0.1.31"
edition = "2021"
authors = ["uu-remote-linux contributors"]
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
@@ -29,13 +29,15 @@ tokio-tungstenite = { version = "0.23", features = ["rustls-tls-webpki-roots"] }
futures-util = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls", "rustls-tls-native-roots"] }
hmac = "0.12"
sha2 = "0.10"
hex = "0.4"
base64 = "0.22"
flate2 = "1.0"
bytes = "1"
async-trait = "0.1"
rand = "0.8"
webrtc = "0.10"
libc = "0.2"
nix = { version = "0.29", features = ["term", "process", "fs", "ioctl", "signal"] }
+41 -9
View File
@@ -35,7 +35,7 @@
| Remote assistance (experimental) | Like the official client: share the **device ID + verification code**; the other person enters them under 开始协助 in UU Remote. Works before sign-in (guest identity) and after; see below |
| Remote desktop | H.264 video, NVENC hardware encoding on NVIDIA GPUs, libx264 otherwise; the viewer draws its local pointer, so there is no double cursor |
| Mouse / keyboard | Absolute and relative motion, clicks, wheel, keys; no spurious key repeat on high-latency links; optional uinput virtual devices on Wayland (layout independent, see below) |
| Terminal shortcuts | Mac viewers send both Cmd and Ctrl as Ctrl. In a focused terminal window Ctrl+V / Cmd+V become Ctrl+Shift+V (paste); Ctrl+C always interrupts, copy with Cmd+Shift+C (X11) |
| Mac shortcuts | **Recommended**: turn off 「使用 macOS 快捷键」 (use macOS shortcuts) in the Mac viewer's remote-window menu; then Cmd+C/V/A/F/X/Z work as on a Mac (Cmd+C/V become Ctrl+Shift+C/V in terminals) and Ctrl+C always interrupts. With the option on, the viewer sends Cmd as Ctrl: in a terminal Ctrl+V / Cmd+V paste, Ctrl+C interrupts, copy is Cmd+Shift+C (X11) |
| Pointer shape | Remote cursor shape is mirrored to the viewer (X11) |
| Clipboard | Two-way text sync |
| Quality / frame rate / resolution | Auto, Original 20M, Ultra 8M, HD 2M, custom; 30/60/90/144 fps; "other resolutions" are scaled by the encoder, the host monitor is left untouched |
@@ -65,16 +65,27 @@ authorized session. Independent file/terminal tool peers do not reset input.
| Xfce | X11 | ✅ Tested in a VM | default |
| Cinnamon / MATE | X11 | ✅ Tested in a VM | default |
| Hyprland | Wayland (native wlroots protocols) | 🧪 Experimental, tested in a VM (some features limited, see below) | `--features wayland` |
| GNOME / KDE Plasma | Wayland (xdg-desktop-portal) | 🧪 Experimental, tested in a VM (some features limited, see below) | `--features wayland,portal` |
| GNOME / KDE Plasma | Wayland (xdg-desktop-portal) | 🧪 Experimental; validation scope and limits below | `--features wayland,portal` |
Apart from GNOME on X11, these desktops have been tested in virtual machines so far. If something goes wrong on
The GNOME Wayland Portal/EIS path has been checked for video and mouse/keyboard input on Ubuntu 26.04, GNOME 50, with one 4K screen at 200% scale; auto unlock after a cold locked connection is verified with the official Mac and iPhone 4.42.0 viewers, while other lock recovery and reconnection still need hardware acceptance testing. Apart from that path and GNOME on X11, desktop testing has primarily used virtual machines. If something goes wrong on
real hardware, please [open an issue](../../issues) with your distribution, desktop, GPU and the relevant lines from
`journalctl --user -u uuremote-native.service`.
Wayland limits: single screen, fixed resolution. The native Hyprland backend now pipelines two shm screencopy buffers and passes the viewer's requested fps to both capture pacing and the encoder. Actual fps remains limited by the compositor, rendering and readback (the old serial path measured about 16–22 fps at 2560x1600; the pipeline is VM-measured only, not a hardware guarantee). The portal backend does not support screen wake or
dynamic resolution yet; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
Wayland limits: single screen, fixed resolution. The native Hyprland backend now pipelines two shm screencopy buffers and passes the viewer's requested fps to both capture pacing and the encoder. Actual fps remains limited by the compositor, rendering and readback (the old serial path measured about 16–22 fps at 2560x1600; the pipeline is VM-measured only, not a hardware guarantee). The portal backend does not support dynamic resolution; see below for GNOME wake and lock recovery; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
permission; the permission is remembered once granted.
Scaled GNOME desktops using native Portal input use EIS on the same authorized session when `ConnectToEIS` and a screen `mapping_id` are available. Each axis is mapped using the matched region and actual frame dimensions, with no fixed resolution or scale factor. This avoids scaled-coordinate rejection by `NotifyPointerMotionAbsolute`. Portal builds require libei >= 1.1. Missing EIS support retains the legacy input path and its existing scaling limitations; other initialization failures are reported and trigger session recovery. After a successful EIS connection, all input uses EIS. Capture alone and uinput do not initialize EIS; missing optional logical dimensions do not block capture. Keyboard input uses the existing Windows VK to evdev mapping. Recovery keeps the encoder frame dimensions fixed but accepts logical-size changes after scaling. On GNOME, a display-change notification enables earlier recovery when previously streaming Portal capture remains paused without a valid format for a 200 ms grace after both conditions are present. Healthy renegotiation cancels the early check; repeated notifications do not postpone the same interruption. The five-second fallback remains without a related notification. Static video and valid-format pauses do not trigger recovery. Tested while connected on Ubuntu 26.04, GNOME Wayland, one 3840x2160 display: 200% -> 166.67% -> 200%, with continuously updating video and full-screen pointer control after recovery. Switching briefly freezes and goes black before recovering; five seconds is the fallback threshold, not total recovery time. See [#35](../../pull/35) for timing measurements and validation limits. Lock recovery still needs hardware acceptance testing; multi-monitor support, live resolution changes, and rotation/reflection transforms are outside this fix's scope. Capture settings remain at protocol version 4: advertised resolutions use pixel coordinates and DPR 1; Portal logical dimensions are used only for local input mapping.
GNOME Portal CONTROL no longer waits for a consent dialog or the first video frame. It announces the selected desktop before reporting secure-desktop state and receiving the official viewer's “auto unlock host” request. The official Android viewer requires this desktop selection before entering its auto-unlock flow, so the notification must precede capture readiness; actual screen dimensions, codec information and capture permissions still follow their real readiness state. Enable auto unlock in the viewer and use the current Linux user's login password. The host verifies it with PAM, unlocks the existing GNOME session, requests display wake, and restores capture, input and clipboard through the shared Portal session. Initial consent or revoked permissions still require local approval. Reconnect if physical or logical dimensions change.
The host also accepts a desktop connection while GNOME is locked with no Portal session yet (a cold connection): it reports lock state and accepts auto unlock, then restores picture, input and clipboard after unlock. While Portal is pending, the host sends generated black video once per second on the negotiated track by default, allowing the official iPhone viewer to enter its auto-unlock flow without extra configuration. Verified on Ubuntu GNOME Wayland with the official Mac viewer; iPhone 4.42.0 with H.265 completed a same-executable control experiment: black video ON succeeded, OFF failed, and ON succeeded again ([#29](../../issues/29)). **Limitation**: this is synthetic black video, not the GNOME lock screen, and it does not enable native mouse/keyboard input while locked. Enable 「自动解锁被控端」 in the UU client under 控制中心 → 安全 and enter the password. Without saved Portal permission, local consent is still required after unlock before the real desktop can appear.
Existing screen-off and lock settings remain unchanged. Only capture recovery in an unlocked session temporarily holds a GNOME idle inhibitor; the first frame, lock, disconnect or initialization failure releases it. An idle viewer still allows the normal screen-off and lock timeout, and manual lock remains effective. Desktop input is blocked and real video is hidden while locked or when lock state is unknown. Independent terminal, file-transfer and port-mapping peers do not wait for video initialization and survive desktop reconnection. Automated protocol and lifecycle regression tests cover this path; auto unlock and capture recovery after a cold locked connection are verified on GNOME hardware with the official Mac and iPhone 4.42.0 viewers, while screen-off and the other scenarios still need acceptance testing. Waking a fully suspended or hibernating machine is outside this CONTROL path.
Locked black video requires FFmpeg's libx264/libx265 encoder for the negotiated H.264/H.265 codec; hardware encoding support alone does not satisfy this dependency. Encoder failures are logged explicitly. Synthetic video does not change capture permissions or advertised monitor dimensions. It pauses on unlock or unknown lock state, ends when capture is ready/rejected or the connection closes, and then hands the same video track to real capture. See [#29](../../issues/29) and [#34](../../pull/34) for the controlled experiment and validation scope.
The native Wayland backend needs `wlr-output-power-management` from the compositor to wake a turned-off screen; without it only wake is unavailable — capture and encoding are unaffected.
Native Wayland experiments: `UU_WAYLAND_PIPELINE=0` disables pipelining for comparison.
`UU_WAYLAND_DMABUF=1` explicitly tries dma-buf → GBM CPU mapping (off by default;
requires runtime libgbm; `UU_WAYLAND_DMABUF_DEVICE` overrides `/dev/dri/renderD128`).
@@ -88,13 +99,13 @@ mapping and hardware acceleration remain unverified. This is not zero-copy VAAPI
| Feature | Status |
|---|---|
| Switching between multiple monitors | In progress |
| True color (4:4:4) | Not implemented; not declared in the handshake, so the client does not enable it |
| True color (4:4:4) | Supported (v0.1.15): the default FFmpeg pipeline with NVENC / libx264 encodes H.264 High 4:4:4; VAAPI and `inproc-av` stay 4:2:0 and report that to the client |
| HDR | Not implemented |
| Super screen / virtual display | Not implemented |
| Privacy screen on the host | Not implemented |
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
| Access before login (greeter / no user logged in) | The greeter cannot be controlled; without a logged-in user use headless mode or a virtual desktop (see below) |
| Linux login screen | The system login screen (before anyone logs in to the Linux desktop) cannot be controlled yet; use headless mode or a virtual desktop (see below). This is about the Linux login: **without a UU account** the host can now be used through remote assistance |
| Relay connections on restrictive networks | Uses the ICE servers provided by the UU cloud; complex network setups are not thoroughly tested |
| Acting as a viewer (controlling other machines from Linux) | Out of scope |
@@ -109,7 +120,7 @@ sudo apt install build-essential pkg-config \
# optional: Wayland backend
sudo apt install libwayland-dev libxkbcommon-dev wl-clipboard
# optional: GNOME/KDE portal backend
sudo apt install libglib2.0-dev libpipewire-0.3-dev
sudo apt install libglib2.0-dev libpipewire-0.3-dev libei-dev
```
- A Rust toolchain is required (<https://rustup.rs>).
@@ -191,6 +202,9 @@ connect from 开始协助 (remote assistance) in UU Remote.
- **Signed in**: the host is in your device list and remote assistance keeps working (same device ID). A background
service keeps the window hidden; open it with `uuremote-server show` or the "UU Remote Linux Host" launcher.
Closing the window only hides it.
- **Tray icon**: a background host shows a tray icon with Open main window, the allow switch and Quit. GNOME needs
the AppIndicator extension (bundled on Ubuntu); Xfce / Cinnamon / KDE work as is. Quit stops `uuremote-native.service`;
the launcher starts it again.
- **Settings** (as in the official client): allow switch (off by default); verification by temporary code, custom code
(8–16 letters and digits) or either; temporary code rotation after each session / hourly / 12 h / 24 h / never;
copy buttons for the ID and the code. Stored in the 0600 config.
@@ -257,12 +271,16 @@ Common environment variables:
Host settings such as "lock after remote session" and "mute host" are stored in
`~/.config/uuremote/host_settings.json`.
With external FFmpeg CPU encoding, the bitrate presets (Original / Ultra / HD / custom) use average bitrate (ABR) to avoid gray keyframes at 4K and low bitrates; Auto keeps constant rate factor (CRF).
## Let an AI agent install or develop it
If you use Claude Code, Codex, Cursor or a similar coding agent, have it read [AGENTS.md](AGENTS.md) first.
It is written for language models: build and install steps, a code map, protocol notes and development rules,
enough to take over installation or development.
For local encoding diagnostics, `desktop-video-probe [output.h264]` accepts `UU_PROBE_FPS`, `UU_PROBE_FRAMES` (default 5), and `UU_PROBE_QUALITY` (1–5); set `UU_PROBE_INPUT=clip.y4m` (also `.mp4` or `frames/%05d.png`) to bypass desktop capture and measure fixed input with the same production FFmpeg encoding options, printing dimensions and encoding statistics, and optionally `UU_PROBE_LOOP=1` to repeat until the requested frame count (unreadable or short input exits nonzero; fixed input always uses the external FFmpeg pipeline).
## How it works
```mermaid
@@ -308,6 +326,16 @@ Released under the [GPL-3.0](LICENSE): you may use, modify and redistribute it,
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
- Most of the code was written with the help of Claude and Codex.
### H.265 / HEVC
When both sides support H.265 the host uses it (as the official host does: the host picks the codec from both
capability lists; the official Mac viewer lists H265 first in its SDP). The first desktop connection probes a
synthetic frame and announces encoder support with CodecNegotiation. `hevc_nvenc`, `libx265` and explicit
`UU_VIDEO_ENCODER=vaapi` (`hevc_vaapi`); hardware failure falls back to software HEVC, then to H.264.
`UU_VIDEO_CODEC=h264` forces H.264. 8-bit; true color 4:4:4 uses hevc_nvenc `rext` / libx265 `main444-8`.
Verified with the official Mac viewer on an RTX 3090 (hevc_nvenc, including 4:4:4); libx265, hevc_vaapi and
other viewers are not hardware-verified.
### VAAPI (opt-in / 显式启用)
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
@@ -351,10 +379,14 @@ cargo build --release --features wayland
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
For uinput, load `uinput` (at boot: `echo uinput | sudo tee /etc/modules-load.d/uinput.conf`) and apply the uaccess rule above; seat ACLs require an active login.
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
This permission can inject into the whole seat, including lock/greeter screens.
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
In Hyprland / uwsm sessions `default.target` may start before the compositor exports `WAYLAND_DISPLAY`;
tie the unit to the graphical session instead (community-verified autostart on Omarchy):
`After=` and `PartOf=graphical-session.target`, `ConditionEnvironment=WAYLAND_DISPLAY`,
`Environment=UU_DESKTOP_BACKEND=wayland` (no `DISPLAY`), and `WantedBy=graphical-session.target`.
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
+50 -8
View File
@@ -31,7 +31,7 @@
| 远程协助(实验性) | 像官方客户端一样,把**设备 ID + 验证码**发给别人,对方在 UU「开始协助」里输入即可连接;登录前(访客身份)和登录后都能用,见下文 |
| 远程桌面 | H.264 视频,NVIDIA 显卡用 NVENC 硬件编码,否则用 libx264;查看端绘制本地鼠标,不会出现双光标 |
| 鼠标 / 键盘 | 绝对/相对移动、点击、滚轮、按键;修复了网络延迟导致的按键连发;Wayland 下可用 uinput 虚拟键鼠(不依赖键盘布局,见下文) |
| 终端快捷键 | Mac 控制端的 Cmd 和 Ctrl 到被控端都是 Ctrl。焦点在终端窗口时,Ctrl+V / Cmd+V 自动转成 Ctrl+Shift+V 粘贴;Ctrl+C 始终是中断,复制请用 Cmd+Shift+C(X11) |
| Mac 快捷键 | **推荐**:在 Mac 远控窗口菜单里关闭「使用 macOS 快捷键」,之后和在 Mac 上一样:Cmd+C/V/A/F/X/Z 是复制、粘贴、全选、查找、剪切、撤销(终端里 Cmd+C/V 自动变成 Ctrl+Shift+C/V),Ctrl+C 在终端里始终是中断。保持开启时,客户端会把 Cmd 和 Ctrl 都当成 Ctrl 发送:终端里 Ctrl+V / Cmd+V 自动转成粘贴,Ctrl+C 是中断,复制用 Cmd+Shift+C(X11) |
| 鼠标指针样式 | 远端指针形状实时同步(X11) |
| 剪贴板 | 双向文本同步 |
| 画质 / 帧率 / 分辨率 | 自动、原画 20M、超清 8M、高清 2M、自定义;30/60/90/144 帧;「其他分辨率」由编码器缩放,不改动主机显示器 |
@@ -59,12 +59,24 @@ Portal 仅作用于本服务获授权的会话。文件传输、终端等独立
| Xfce | X11 | ✅ 虚拟机测试通过 | 默认 |
| Cinnamon / MATE | X11 | ✅ 虚拟机测试通过 | 默认 |
| Hyprland | Wayland(wlroots 原生协议) | 🧪 实验性,虚拟机测试(部分功能受限,见下) | `--features wayland` |
| GNOME / KDE Plasma | Wayland(xdg-desktop-portal) | 🧪 实验性,虚拟机测试(部分功能受限,见下) | `--features wayland,portal` |
| GNOME / KDE Plasma | Wayland(xdg-desktop-portal) | 🧪 实验性,验证范围与限制见下 | `--features wayland,portal` |
除 GNOME(X11)外,其余桌面目前都是在虚拟机里测试的。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
GNOME Wayland 的 Portal/EIS 路径已在 Ubuntu 26.04、GNOME 50、单屏 4K/200% 缩放下验证画面和键鼠;锁屏冷连接自动解锁已用 Mac 和 iPhone 4.42.0 客户端实机验证,其余锁屏恢复及重连仍待验收。除 GNOME 的上述路径和 X11 外,其余桌面目前主要在虚拟机里测试。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持动态分辨率;GNOME 的屏幕唤醒与锁屏恢复见下;剪贴板只在控制端的远控窗口处于前台时同步。
GNOME 缩放桌面在使用 Portal 原生键鼠时,若支持 `ConnectToEIS` 和屏幕 `mapping_id`,通过同一授权会话的 EIS 接口注入输入,按匹配的区域与实际帧尺寸分别映射横纵坐标,没有固定分辨率或缩放倍率,可避免 `NotifyPointerMotionAbsolute` 在缩放时拒绝坐标。Portal 构建需要 libei ≥ 1.1。缺少 EIS 支持时保留旧输入路径及其既有缩放限制;其他初始化错误会明确报告并触发会话恢复。EIS 连接成功后全部输入统一使用 EIS。只采集画面或使用 uinput 时不会初始化 EIS;缺少可选的逻辑尺寸也不阻断采集。键盘沿用项目现有的 Windows VK → evdev 映射。恢复时保持编码器的采集帧尺寸不变,允许缩放后的逻辑尺寸变化。GNOME 显示配置变化后,已推流的 Portal 采集若持续「暂停且无有效格式」,会在关联通知后的 200 毫秒宽限结束后提前进入现有恢复流程;正常重协商恢复会取消提前判定,连续通知不会推迟同次异常的判定。未收到相关通知时保留五秒兜底;静态画面或有效格式下的暂停不会触发恢复。Ubuntu 26.04、GNOME Wayland 单屏 3840×2160 已实测保持连接切换 200%→166.67%→200%,恢复后画面持续更新且鼠标可控制全屏;切换期间会短暂停顿、黑屏后恢复,五秒是兜底判定阈值,并非总恢复时间。提前恢复的测量结果和验证范围见 [#35](../../pull/35)。此路径仍需实机验证锁屏恢复;多屏、连接中改变分辨率及旋转/镜像变换不属于本次修复范围。 捕获设置协议仍为版本 4:对客户端上报的分辨率统一使用像素坐标、DPR 为 1;Portal 逻辑尺寸仅用于本地输入映射。
GNOME Portal 的控制通道不再等待授权窗口或第一帧画面:先通知客户端当前选择桌面,再上报锁屏状态、接收官方客户端的「自动解锁被控端」请求。官方 Android 客户端需要桌面目标通知才会进入自动解锁流程,因此该通知不能等待采集就绪;真实屏幕尺寸、编码信息及采集权限仍按实际就绪状态上报。请在客户端启用自动解锁,使用当前 Linux 用户的登录密码;主机通过 PAM 验证后解锁当前 GNOME 会话、请求点亮屏幕,并恢复同一 Portal 会话中的画面、键鼠和剪贴板。首次授权或权限被撤销时仍需本机确认。物理或逻辑分辨率变化后需要重连。
桌面锁屏且尚未建立 Portal 时(冷连接),主机也会建立控制通道:上报锁屏状态并接受自动解锁,解锁后再恢复画面、键鼠和剪贴板。Portal 等待就绪期间,主机默认通过已协商的视频轨每秒发送一帧生成的纯黑画面,让官方 iPhone 客户端进入自动解锁流程,无需额外配置。Ubuntu GNOME Wayland 上已用官方 Mac 客户端验证;iPhone 4.42.0 的 H.265 冷连接完成了同一实验二进制「开启黑帧成功 → 关闭失败 → 再开成功」对照([#29](../../issues/29))。**局限**:黑帧不是真实 GNOME 锁屏画面,也不支持在锁屏界面操作原生键鼠;需要在 UU 客户端 控制中心 → 安全 → 自动解锁被控端 中启用自动解锁并输入密码。若本机没有已保存的 Portal 授权,解锁后仍需本机确认授权才能显示真实桌面。
无需修改现有熄屏和锁屏设置。主机仅在会话已解锁的采集恢复期间临时申请 GNOME 的 idle 抑制,拿到首帧后立即释放;锁屏、断开或初始化失败也会释放。远控窗口空闲时仍按原策略熄屏和锁屏,手动锁屏也继续生效。锁屏及锁状态未知时停止桌面输入并隐藏真实画面;独立终端、文件传输和端口映射连接不依赖画面初始化,也不会因桌面重连被关闭。这一恢复路径已覆盖自动化协议与生命周期回归测试,锁屏冷连接后的自动解锁与采集恢复已用官方 Mac 和 iPhone 4.42.0 客户端在 GNOME 实机验证,熄屏等其余场景仍待验收;不支持从整机挂起或休眠状态通过现有控制通道唤醒。
锁屏黑帧使用 FFmpeg 的 libx264/libx265 编码器(按已协商的 H.264/H.265 选择),硬件编码器可用并不代表这些软件编码器已安装。编码失败会明确记录日志。黑帧不改变采集权限或上报的真实屏幕尺寸;解锁或锁状态未知时暂停,采集就绪、拒绝或连接关闭时结束,再由真实视频接续同一条视频轨。对照实验与验证范围见 [#29](../../issues/29)、[#34](../../pull/34)。
原生 Wayland 后端需要合成器提供 `wlr-output-power-management` 才能唤醒已关闭的屏幕;缺少该协议不影响采集和编码。
原生 Wayland 实验选项:`UU_WAYLAND_PIPELINE=0` 可关闭流水线做对照。`UU_WAYLAND_DMABUF=1` 显式尝试 dma-buf→GBM CPU 映射(默认关闭,需要运行时 libgbm;可用 `UU_WAYLAND_DMABUF_DEVICE` 指定 render node,默认 `/dev/dri/renderD128`)。仅支持显式线性修饰符、单平面 8-bit RGB,失败回退 SHM;virgl VM 中只验证了回退,成功导入/映射和硬件加速均未验证。这不是零拷贝 VAAPI 路径;选择 `UU_VIDEO_ENCODER=vaapi` 时仍由 FFmpeg 上传 CPU 帧。
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
@@ -74,13 +86,13 @@ Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端
| 功能 | 状态 |
|---|---|
| 多显示器切换 | 开发中 |
| 真彩(4:4:4) | 未实现,握手时不声明,客户端不会启用 |
| 真彩(4:4:4) | 支持(v0.1.15):NVENC / libx264 的默认 FFmpeg 管线输出 H.264 High 4:4:4;VAAPI 和 `inproc-av` 管线仍为 4:2:0,并如实回报给客户端 |
| HDR | 未实现 |
| 超级屏 / 虚拟显示器 | 未实现 |
| 被控端防窥模式 | 未实现 |
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
| 登录前(登录界面 / 无人登录)访问 | 不支持控制登录界面;没有人登录时可以用无头模式或虚拟桌面(见下文) |
| Linux 系统登录界面 | 还不支持控制开机后的系统登录界面(还没有人登录 Linux 桌面时);这种情况可以用无头模式或虚拟桌面(见下文)。注意:这里说的是 Linux 系统登录,**不登录 UU 账号**现在可以通过「远程协助」使用 |
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 |
@@ -95,7 +107,7 @@ sudo apt install build-essential pkg-config \
# 可选:Wayland 后端
sudo apt install libwayland-dev libxkbcommon-dev wl-clipboard
# 可选:GNOME/KDE portal 后端
sudo apt install libglib2.0-dev libpipewire-0.3-dev
sudo apt install libglib2.0-dev libpipewire-0.3-dev libei-dev
```
- 需要 Rust 工具链(<https://rustup.rs>)。
@@ -171,6 +183,7 @@ X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放
- **未登录**:启动后直接显示主窗口,用访客身份提供远程协助,同时显示扫码登录二维码。扫码登录后自动切换到账号模式。
- **已登录**:本机出现在你的设备列表里,可以直接连;远程协助也照常可用(同一设备 ID)。服务在后台运行时窗口默认隐藏,用 `uuremote-server show` 或应用菜单里的「UU 远程 Linux 主机」打开。关闭窗口只是隐藏,不会停止服务。
- **托盘图标**:后台运行时顶栏/任务栏有托盘图标,菜单里可以「打开主窗口」、切换「允许他人远程协助」、「退出」。GNOME 需要 AppIndicator 扩展(Ubuntu 自带),Xfce / Cinnamon / KDE 直接可用。从托盘退出会停止 `uuremote-native.service`,之后从应用菜单打开会重新启动服务。
- **设置**(与官方一致):「允许他人远程协助」开关(默认关闭);验证方式:仅临时验证码 / 仅自定义验证码(8–16 位,需同时包含字母和数字) / 两种都可以;临时验证码自动刷新:每次远控结束 / 每小时 / 每 12 小时 / 每 24 小时 / 不刷新;ID 和验证码旁各有「复制」按钮。设置保存在本机配置文件(0600)。
- **只在本机窗口显示**:验证码不会写进日志。没有图形界面时,单独运行 `uuremote-server assist` 会在终端显示(`allow` / `deny` / `reset` / `mode …` / `refresh …` / `off`)。
- **安全**:拿到 ID 和验证码的人可以完全控制这台电脑(桌面、文件、终端)。只发给信任的人,用完关掉开关或刷新验证码。
@@ -230,6 +243,10 @@ systemctl --user restart uuremote-native.service
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。
选择码率档(原画 / 超清 / 高清 / 自定义)时,外部 FFmpeg 的 CPU 编码以平均码率(ABR)为目标,避免 4K 低码率下关键帧发灰;「自动」档仍使用恒定质量(CRF)。
本地编码诊断 `desktop-video-probe [output.h264]` 可设置 `UU_PROBE_FPS`、`UU_PROBE_FRAMES`(默认 5)和 `UU_PROBE_QUALITY`(1–5);设置 `UU_PROBE_INPUT=clip.y4m`(也支持 `.mp4`、`frames/%05d.png`)可绕过桌面采集,以相同的生产 FFmpeg 编码参数测量固定输入并打印分辨率和编码统计,`UU_PROBE_LOOP=1` 循环到指定帧数,输入无法打开或帧数不足时以非零状态退出(固定输入始终使用外部 FFmpeg 管线)。
## 让 AI 帮你安装或开发
如果你用 Claude Code、Codex、Cursor 等 AI 编程工具,让它先阅读 [AGENTS.md](AGENTS.md)。
@@ -274,6 +291,14 @@ flowchart LR
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
### H.265 / HEVC
控制端和主机都支持 H.265 时,主机自动使用 H.265(与官方主机一致,由主机按双方能力选择;官方 Mac 客户端在 SDP
中优先列出 H.265)。首次桌面连接会用合成帧探测实际编码能力,并通过 CodecNegotiation 告知客户端。支持 `hevc_nvenc`、
`libx265`、显式 `UU_VIDEO_ENCODER=vaapi` 的 `hevc_vaapi`;硬件失败先回落软件 HEVC,再回落 H.264。
`UU_VIDEO_CODEC=h264` 强制使用 H.264。8-bit;真彩 4:4:4 使用 hevc_nvenc `rext` / libx265 `main444-8`。
已用官方 Mac 客户端在 RTX 3090(hevc_nvenc,含 4:4:4)实机验收;libx265、hevc_vaapi 及其他客户端尚未实机验证。
### VAAPI 硬件编码(显式启用)
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
@@ -310,10 +335,27 @@ cargo build --release --features wayland
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
uinput 需加载内核模块并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
uinput 需加载内核模块(开机自动加载:`echo uinput | sudo tee /etc/modules-load.d/uinput.conf`)并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
仍须确认启动日志实际选择了 uinput。
Hyprland / uwsm 等会话中,`default.target` 可能早于合成器导出 `WAYLAND_DISPLAY`。systemd 单元可改为随图形会话启动
(社区在 Omarchy 上验证过开机自启):
```ini
[Unit]
After=graphical-session.target
PartOf=graphical-session.target
ConditionEnvironment=WAYLAND_DISPLAY
[Service]
Environment=UU_DESKTOP_BACKEND=wayland
# 其余同上文 X11 示例,去掉 DISPLAY
[Install]
WantedBy=graphical-session.target
```
VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-driver`,`libva-utils` 提供
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
+6 -4
View File
@@ -51,14 +51,16 @@ fn main() {
if std::env::var_os("CARGO_FEATURE_PORTAL").is_some() {
println!("cargo:rerun-if-changed=c_engine/portal_engine.c");
println!("cargo:rerun-if-changed=c_engine/portal_engine.h");
println!("cargo:rerun-if-changed=c_engine/portal_eis.inc");
println!("cargo:rerun-if-changed=c_engine/desktop_power.c");
let flags=|kind:&str| {
let output=std::process::Command::new("pkg-config").args([kind,"gio-2.0","gio-unix-2.0","libpipewire-0.3"]).output()
.expect("Install pkg-config and GIO/PipeWire development libraries");
assert!(output.status.success(),"Portal requires GIO and PipeWire development libraries");
let output=std::process::Command::new("pkg-config").args([kind,"gio-2.0","gio-unix-2.0","libpipewire-0.3","libei-1.0 >= 1.1"]).output()
.expect("Install pkg-config and GIO/PipeWire/libei development libraries");
assert!(output.status.success(),"Portal requires GIO, PipeWire and libei >= 1.1 development libraries");
String::from_utf8(output.stdout).expect("pkg-config flags must be UTF-8")
};
let mut build=cc::Build::new();
build.file("c_engine/portal_engine.c").flag_if_supported("-Wno-unused-parameter");
build.file("c_engine/portal_engine.c").file("c_engine/desktop_power.c").flag_if_supported("-Wno-unused-parameter");
for flag in flags("--cflags").split_whitespace() { build.flag(flag); }
build.compile("uu_portal_engine");
for flag in flags("--libs").split_whitespace() {
+69 -1
View File
@@ -7,6 +7,7 @@
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
static const char *CSS =
"window { background-color: #f5f6f8; }\n"
@@ -27,7 +28,7 @@ static const char *CSS =
".status.ok { color: #16a34a; }\n"
".status.error { color: #dc2626; }\n";
enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8 };
enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8, CMD_QUIT = 9 };
enum { VERIFY_TEMPORARY = 0, VERIFY_CUSTOM = 1, VERIFY_BOTH = 2 };
typedef struct { char *id; char *code; int allowed, verify, refresh, has_custom; } State;
@@ -47,6 +48,9 @@ static GtkWidget *refresh_items[5], *footer_label;
// Account card: QR login before sign-in, user info (+ logout) after.
static GtkWidget *assist_card, *account_card, *account_stack, *qr_stack, *qr_image, *login_status, *user_label;
static int show_assist = 1, show_account;
// Tray (host service only): AppIndicator via dlopen (no build dependency), else GtkStatusIcon.
static GtkWidget *tray_menu, *tray_allow;
static int suppress_tray;
static int cur_verify, cur_refresh, cur_has_custom;
static char current_id[64], current_code[64];
static int code_visible = 1, suppress_switch, start_hidden, close_hides;
@@ -274,6 +278,68 @@ static GtkWidget *build_account_card(void) {
return card;
}
static void present_window(void) { if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); } }
static void on_tray_open(GtkMenuItem *i, gpointer d) { (void)i; (void)d; present_window(); }
static void on_tray_allow(GtkCheckMenuItem *i, gpointer d) {
(void)d;
if (!suppress_tray) post(gtk_check_menu_item_get_active(i) ? CMD_ALLOW : CMD_DENY);
}
static void on_tray_quit(GtkMenuItem *i, gpointer d) {
(void)i; (void)d;
GtkWidget *dlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
"退出后本机将下线:设备列表和远程协助都无法连接,直到再次启动。确定退出吗?");
gtk_window_set_title(GTK_WINDOW(dlg), "UU 远程 Linux 主机");
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) post(CMD_QUIT);
gtk_widget_destroy(dlg);
}
static void on_status_activate(GtkStatusIcon *i, gpointer d) { (void)i; (void)d; present_window(); }
static void on_status_menu(GtkStatusIcon *i, guint button, guint time, gpointer d) {
(void)i; (void)button; (void)time; (void)d;
gtk_menu_popup_at_pointer(GTK_MENU(tray_menu), NULL);
}
static void build_tray(void) {
tray_menu = gtk_menu_new();
GtkWidget *open = gtk_menu_item_new_with_label("打开主窗口");
g_signal_connect(open, "activate", G_CALLBACK(on_tray_open), NULL);
tray_allow = gtk_check_menu_item_new_with_label("允许他人远程协助");
g_signal_connect(tray_allow, "toggled", G_CALLBACK(on_tray_allow), NULL);
GtkWidget *quit = gtk_menu_item_new_with_label("退出");
g_signal_connect(quit, "activate", G_CALLBACK(on_tray_quit), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), open);
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), tray_allow);
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), gtk_separator_menu_item_new());
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), quit);
gtk_widget_show_all(tray_menu);
const char *icon = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), "uu-remote-linux") ? "uu-remote-linux" : "computer";
// StatusNotifierItem (GNOME AppIndicator extension, KDE): libayatana-appindicator3 / libappindicator3.
const char *libs[] = { "libayatana-appindicator3.so.1", "libappindicator3.so.1" };
for (int i = 0; i < 2; i++) {
void *h = dlopen(libs[i], RTLD_LAZY | RTLD_LOCAL);
if (!h) continue;
void *(*ind_new)(const char *, const char *, int) = (void *(*)(const char *, const char *, int))dlsym(h, "app_indicator_new");
void (*set_status)(void *, int) = (void (*)(void *, int))dlsym(h, "app_indicator_set_status");
void (*set_menu)(void *, GtkMenu *) = (void (*)(void *, GtkMenu *))dlsym(h, "app_indicator_set_menu");
void (*set_title)(void *, const char *) = (void (*)(void *, const char *))dlsym(h, "app_indicator_set_title");
if (ind_new && set_status && set_menu) {
void *ind = ind_new("uu-remote-linux", icon, 0 /* APPLICATION_STATUS */);
if (set_title) set_title(ind, "UU 远程 Linux 主机");
set_menu(ind, GTK_MENU(tray_menu));
set_status(ind, 1 /* ACTIVE */);
return; // keep the library loaded for the indicator's lifetime
}
dlclose(h);
}
// XEmbed system tray (Xfce, Cinnamon, MATE...).
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GtkStatusIcon *status = gtk_status_icon_new_from_icon_name(icon);
gtk_status_icon_set_tooltip_text(status, "UU 远程 Linux 主机");
gtk_status_icon_set_title(status, "UU 远程 Linux 主机");
g_signal_connect(status, "activate", G_CALLBACK(on_status_activate), NULL);
g_signal_connect(status, "popup-menu", G_CALLBACK(on_status_menu), NULL);
G_GNUC_END_IGNORE_DEPRECATIONS
}
static void build_window(void) {
GtkCssProvider *css = gtk_css_provider_new();
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
@@ -398,6 +464,7 @@ static void build_window(void) {
gtk_box_pack_end(GTK_BOX(root), footer_label, FALSE, FALSE, 0);
if (close_hides) gtk_label_set_text(GTK_LABEL(footer_label), "开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口后远程协助仍在后台运行");
render_code();
if (close_hides) build_tray();
gtk_widget_show_all(gtk_bin_get_child(GTK_BIN(window)));
gtk_widget_set_visible(assist_card, show_assist);
gtk_widget_set_visible(hint_label, show_assist);
@@ -465,6 +532,7 @@ static gboolean apply_state(gpointer data) {
suppress_switch = 1;
gtk_switch_set_active(GTK_SWITCH(allow_switch), s->allowed);
suppress_switch = 0;
if (tray_allow) { suppress_tray = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tray_allow), s->allowed); suppress_tray = 0; }
cur_verify = s->verify; cur_refresh = s->refresh; cur_has_custom = s->has_custom;
suppress_combo = 1; gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), s->verify); suppress_combo = 0;
if (s->refresh >= 0 && s->refresh < 5) {
+154
View File
@@ -0,0 +1,154 @@
#include <gio/gio.h>
#include <stdlib.h>
#define CALL_TIMEOUT_MS 3000
#define MUTTER_DEST "org.gnome.Mutter.DisplayConfig"
#define MUTTER_PATH "/org/gnome/Mutter/DisplayConfig"
#define SESSION_DEST "org.gnome.SessionManager"
#define SESSION_PATH "/org/gnome/SessionManager"
#define SESSION_IFACE "org.gnome.SessionManager"
enum {
UU_POWER_OK = 0,
UU_POWER_SESSION_BUS = 1,
UU_POWER_CALL_FAILED = 2,
};
struct uu_idle_inhibitor {
GDBusConnection *connection;
guint32 cookie;
};
struct setup_timeout {
GMutex lock;
GCond cond;
gboolean finished;
GCancellable *cancellable;
};
static gpointer cancel_setup_on_timeout(gpointer data) {
struct setup_timeout *timeout = data;
gint64 deadline = g_get_monotonic_time() + CALL_TIMEOUT_MS * G_TIME_SPAN_MILLISECOND;
g_mutex_lock(&timeout->lock);
while (!timeout->finished &&
g_cond_wait_until(&timeout->cond, &timeout->lock, deadline)) {}
gboolean expired = !timeout->finished;
g_mutex_unlock(&timeout->lock);
if (expired) g_cancellable_cancel(timeout->cancellable);
return NULL;
}
static void finish_setup_timeout(struct setup_timeout *timeout, GThread *thread) {
g_mutex_lock(&timeout->lock);
timeout->finished = TRUE;
g_cond_signal(&timeout->cond);
g_mutex_unlock(&timeout->lock);
g_thread_join(thread);
g_cond_clear(&timeout->cond);
g_mutex_clear(&timeout->lock);
g_object_unref(timeout->cancellable);
}
/* Use a dedicated connection so closing this inhibitor cannot affect another app's bus state. */
static GDBusConnection *private_session_bus(int *error_code) {
struct setup_timeout timeout = {0};
g_mutex_init(&timeout.lock);
g_cond_init(&timeout.cond);
timeout.cancellable = g_cancellable_new();
GThread *timer = g_thread_new("uu-dbus-connect-timeout", cancel_setup_on_timeout, &timeout);
GError *error = NULL;
gchar *address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SESSION, timeout.cancellable, &error);
if (!address) {
g_clear_error(&error);
finish_setup_timeout(&timeout, timer);
*error_code = UU_POWER_SESSION_BUS;
return NULL;
}
GDBusConnection *connection = g_dbus_connection_new_for_address_sync(
address,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
NULL, timeout.cancellable, &error);
g_free(address);
finish_setup_timeout(&timeout, timer);
if (!connection) {
g_clear_error(&error);
*error_code = UU_POWER_SESSION_BUS;
return NULL;
}
g_dbus_connection_set_exit_on_close(connection, FALSE);
return connection;
}
static void close_private_bus(GDBusConnection *connection) {
if (!connection) return;
g_dbus_connection_close(connection, NULL, NULL, NULL);
g_object_unref(connection);
}
int uu_desktop_power_wake(void) {
int error_code = UU_POWER_OK;
GDBusConnection *connection = private_session_bus(&error_code);
if (!connection) return error_code;
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(
connection, MUTTER_DEST, MUTTER_PATH, "org.freedesktop.DBus.Properties", "Set",
g_variant_new("(ssv)", "org.gnome.Mutter.DisplayConfig", "PowerSaveMode",
g_variant_new_int32(0)),
G_VARIANT_TYPE("()"), G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error);
if (!reply) error_code = UU_POWER_CALL_FAILED;
if (reply) g_variant_unref(reply);
g_clear_error(&error);
close_private_bus(connection);
return error_code;
}
struct uu_idle_inhibitor *uu_desktop_idle_inhibitor_acquire(int *error_code) {
if (!error_code) return NULL;
*error_code = UU_POWER_OK;
GDBusConnection *connection = private_session_bus(error_code);
if (!connection) return NULL;
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(
connection, SESSION_DEST, SESSION_PATH, SESSION_IFACE, "Inhibit",
g_variant_new("(susu)", "uu-remote-linux", 0u,
"Remote desktop session is active", 8u),
G_VARIANT_TYPE("(u)"), G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error);
if (!reply) {
*error_code = UU_POWER_CALL_FAILED;
g_clear_error(&error);
close_private_bus(connection);
return NULL;
}
struct uu_idle_inhibitor *inhibitor = calloc(1, sizeof(*inhibitor));
if (!inhibitor) {
*error_code = UU_POWER_CALL_FAILED;
g_variant_unref(reply);
close_private_bus(connection);
return NULL;
}
g_variant_get(reply, "(u)", &inhibitor->cookie);
g_variant_unref(reply);
inhibitor->connection = connection;
return inhibitor;
}
void uu_desktop_idle_inhibitor_release(struct uu_idle_inhibitor *inhibitor) {
if (!inhibitor) return;
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(
inhibitor->connection, SESSION_DEST, SESSION_PATH, SESSION_IFACE, "Uninhibit",
g_variant_new("(u)", inhibitor->cookie), G_VARIANT_TYPE("()"),
G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error);
if (reply) g_variant_unref(reply);
g_clear_error(&error);
/* Closing the private bus also revokes the cookie if Uninhibit failed or the host exits. */
close_private_bus(inhibitor->connection);
free(inhibitor);
}
+167
View File
@@ -0,0 +1,167 @@
/* All access is serialized by PortalSession's handle mutex, including close.
* ConnectToEIS disables every legacy Notify* input method on this session. */
enum { PORTAL_EIS_UNSUPPORTED = 13 };
static void portal_eis_log(struct ei *ei, enum ei_log_priority priority,
const char *message, struct ei_log_context *context) {
/* Library error text can include input values. Keep diagnostics numeric. */
static atomic_uint count;
if (atomic_fetch_add(&count, 1) < 12)
fprintf(stderr, "Portal EIS library diagnostic: priority=%d\n", priority);
}
static int portal_eis_dispatch(struct uu_portal *p) {
ei_dispatch(p->ei);
struct ei_event *event;
while ((event = ei_get_event(p->ei))) {
struct ei_device *device = ei_event_get_device(event);
switch (ei_event_get_type(event)) {
case EI_EVENT_SEAT_ADDED:
ei_seat_bind_capabilities(ei_event_get_seat(event),
EI_DEVICE_CAP_POINTER, EI_DEVICE_CAP_POINTER_ABSOLUTE,
EI_DEVICE_CAP_BUTTON, EI_DEVICE_CAP_SCROLL, EI_DEVICE_CAP_KEYBOARD, NULL);
break;
case EI_EVENT_DEVICE_RESUMED:
if (!g_list_find(p->ei_devices, device)) {
p->ei_devices = g_list_append(p->ei_devices, ei_device_ref(device));
ei_device_start_emulating(device, ++p->ei_sequence);
}
break;
case EI_EVENT_DEVICE_PAUSED:
case EI_EVENT_DEVICE_REMOVED:
if (g_list_find(p->ei_devices, device)) {
p->ei_devices = g_list_remove(p->ei_devices, device);
ei_device_unref(device);
}
break;
case EI_EVENT_DISCONNECT:
p->ei_failed = 1;
break;
default: break;
}
ei_event_unref(event);
}
return p->ei_failed ? 9 : 0;
}
static struct ei_device *portal_eis_device(struct uu_portal *p, enum ei_device_capability capability) {
for (GList *item = p->ei_devices; item; item = item->next)
if (ei_device_has_capability(item->data, capability)) return item->data;
return NULL;
}
static struct ei_region *portal_eis_region(struct uu_portal *p, struct ei_device *device) {
for (size_t i = 0; ; i++) {
struct ei_region *region = ei_device_get_region(device, i);
if (!region) return NULL;
const char *id = ei_region_get_mapping_id(region);
if (id && p->mapping_id && !strcmp(id, p->mapping_id) &&
ei_region_get_width(region) && ei_region_get_height(region)) return region;
}
}
static struct ei_device *portal_eis_absolute(struct uu_portal *p, struct ei_region **region) {
for (GList *item = p->ei_devices; item; item = item->next) {
struct ei_device *device = item->data;
if (ei_device_has_capability(device, EI_DEVICE_CAP_POINTER_ABSOLUTE) &&
(*region = portal_eis_region(p, device))) return device;
}
return NULL;
}
static void portal_eis_close(struct uu_portal *p) {
for (GList *item = p->ei_devices; item; item = item->next) ei_device_unref(item->data);
g_list_free(p->ei_devices); p->ei_devices = NULL;
if (p->ei) p->ei = ei_unref(p->ei);
}
static int portal_eis_open(struct uu_portal *p) {
if (!p->mapping_id) return PORTAL_EIS_UNSUPPORTED;
GUnixFDList *fds = NULL;
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_with_unix_fd_list_sync(p->bus, DEST, PATH, REMOTE,
"ConnectToEIS", g_variant_new("(o@a{sv})", p->session, empty_options()),
G_VARIANT_TYPE("(h)"), G_DBUS_CALL_FLAGS_NONE, 3000, NULL, &fds, NULL, &error);
if (!reply) {
int unsupported = g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD) ||
g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED);
g_clear_error(&error); g_clear_object(&fds);
return unsupported ? PORTAL_EIS_UNSUPPORTED : 9;
}
gint32 index; g_variant_get(reply, "(h)", &index);
int fd = fds && index >= 0 && index < g_unix_fd_list_get_length(fds) ?
g_unix_fd_list_get(fds, index, NULL) : -1;
g_variant_unref(reply); g_clear_object(&fds);
if (fd < 0) return 9;
p->ei = ei_new_sender(NULL);
if (!p->ei) { close(fd); return 8; }
ei_log_set_handler(p->ei, portal_eis_log);
ei_configure_name(p->ei, "uu-remote-linux");
if (ei_setup_backend_fd(p->ei, fd)) return 9;
gint64 deadline = g_get_monotonic_time() + 3000000;
while (!portal_eis_dispatch(p)) {
struct ei_region *region = NULL;
if (portal_eis_absolute(p, &region) &&
portal_eis_device(p, EI_DEVICE_CAP_POINTER) &&
portal_eis_device(p, EI_DEVICE_CAP_BUTTON) &&
portal_eis_device(p, EI_DEVICE_CAP_SCROLL) &&
portal_eis_device(p, EI_DEVICE_CAP_KEYBOARD)) {
fprintf(stderr, "Portal EIS input ready: region=%ux%u\n",
ei_region_get_width(region), ei_region_get_height(region));
return 0;
}
gint64 remaining = deadline - g_get_monotonic_time();
if (remaining <= 0) break;
struct pollfd pending = {.fd = ei_get_fd(p->ei), .events = POLLIN};
int result = poll(&pending, 1, (int)((remaining + 999) / 1000));
if (result < 0 && errno != EINTR) break;
}
fprintf(stderr, "Portal EIS input initialization failed\n");
return 9;
}
static int portal_eis_available(struct uu_portal *p) {
if (!uu_portal_alive(p)) return 9;
return portal_eis_dispatch(p);
}
static int portal_eis_frame(struct uu_portal *p, struct ei_device *device) {
ei_device_frame(device, ei_now(p->ei));
return portal_eis_dispatch(p);
}
static int portal_eis_move(struct uu_portal *p, double x, double y, int relative) {
if (portal_eis_available(p)) return 9;
struct ei_device *device;
if (relative) {
device = portal_eis_device(p, EI_DEVICE_CAP_POINTER);
if (!device) return 9;
ei_device_pointer_motion(device, x * p->logical_width / p->width,
y * p->logical_height / p->height);
} else {
if (x < 0 || y < 0 || x >= p->width || y >= p->height) return 11;
struct ei_region *region = NULL;
device = portal_eis_absolute(p, &region);
if (!device) return 9;
double target_x = ei_region_get_x(region) + x * ei_region_get_width(region) / p->width;
double target_y = ei_region_get_y(region) + y * ei_region_get_height(region) / p->height;
ei_device_pointer_motion_absolute(device, target_x, target_y);
}
return portal_eis_frame(p, device);
}
static int portal_eis_button(struct uu_portal *p, int button, int pressed) {
if (portal_eis_available(p)) return 9;
if (button >= 4 && button <= 7) {
if (!pressed) return 0;
struct ei_device *device = portal_eis_device(p, EI_DEVICE_CAP_SCROLL);
if (!device) return 9;
int step = button == 4 || button == 6 ? -120 : 120;
ei_device_scroll_discrete(device, button <= 5 ? 0 : step, button <= 5 ? step : 0);
return portal_eis_frame(p, device);
}
int code = button == 1 ? 0x110 : button == 2 ? 0x112 : button == 3 ? 0x111 :
button == 8 ? 0x113 : button == 9 ? 0x114 : 0;
if (!code) return 11;
struct ei_device *device = portal_eis_device(p, EI_DEVICE_CAP_BUTTON);
if (!device) return 9;
ei_device_button_button(device, code, !!pressed);
return portal_eis_frame(p, device);
}
static int portal_eis_key(struct uu_portal *p, int code, int pressed) {
if (code < 0 || code > 0x2ff) return 11;
if (portal_eis_available(p)) return 9;
struct ei_device *device = portal_eis_device(p, EI_DEVICE_CAP_KEYBOARD);
if (!device) return 9;
ei_device_keyboard_key(device, (uint32_t)code, !!pressed);
return portal_eis_frame(p, device);
}
+238 -39
View File
@@ -2,6 +2,8 @@
#include "portal_engine.h"
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include <libei.h>
#include <poll.h>
#include <pipewire/pipewire.h>
#include <spa/param/video/format-utils.h>
#include <spa/pod/builder.h>
@@ -21,6 +23,10 @@
#define SCREEN "org.freedesktop.portal.ScreenCast"
#define REQUEST "org.freedesktop.portal.Request"
#define SESSION "org.freedesktop.portal.Session"
#define PORTAL_RENEGOTIATION_TIMEOUT (5 * G_TIME_SPAN_SECOND)
#define PORTAL_DISPLAY_GRACE (200 * G_TIME_SPAN_MILLISECOND)
#define MUTTER_DISPLAY "org.gnome.Mutter.DisplayConfig"
#define MUTTER_DISPLAY_PATH "/org/gnome/Mutter/DisplayConfig"
struct uu_portal {
GDBusConnection *bus;
@@ -30,7 +36,7 @@ struct uu_portal {
int dbus_started;
char *session;
char *restore_token;
guint closed_subscription, clipboard_owner_subscription, clipboard_transfer_subscription;
guint closed_subscription, clipboard_owner_subscription, clipboard_transfer_subscription, display_subscription;
int clipboard_enabled, clipboard_changed;
char *clipboard_text, *clipboard_mime;
size_t clipboard_length;
@@ -43,14 +49,26 @@ struct uu_portal {
pthread_mutex_t lock;
pthread_cond_t changed;
atomic_int closed;
uu_portal_cancelled_fn cancelled;
void *cancel_data;
int error;
enum pw_stream_state state;
int format_valid, stream_started;
gint64 interrupted_at, display_changed_at;
uint32_t node, width, height;
int logical_width, logical_height;
int absolute_stream_pixels;
struct ei *ei;
GList *ei_devices;
char *mapping_id;
uint32_t ei_sequence;
atomic_int ei_failed;
int ei_unavailable;
enum spa_video_format format;
uint8_t *frame;
size_t frame_length;
uint64_t sequence;
uint64_t revision;
};
#include "portal_clipboard.inc"
@@ -58,6 +76,11 @@ static pthread_once_t pw_once = PTHREAD_ONCE_INIT;
static atomic_uint token_sequence = 1;
static void initialize_pipewire(void) { pw_init(NULL, NULL); }
static GVariant *empty_options(void) { return g_variant_new_array(G_VARIANT_TYPE("{sv}"), NULL, 0); }
static int open_cancelled(struct uu_portal *p) {
return p->cancelled && p->cancelled(p->cancel_data);
}
static int wait_for_first_frame(struct uu_portal *p);
#include "portal_eis.inc"
static void fail(struct uu_portal *p, int error) {
pthread_mutex_lock(&p->lock);
if (!p->error) p->error = error;
@@ -73,6 +96,16 @@ static void response_received(GDBusConnection *bus, const gchar *sender, const g
g_variant_get(parameters, "(u@a{sv})", &r->code, &r->results);
r->done = 1;
}
/* Bounded consent wait; cancellation must break it within one poll interval. */
static int wait_for_response(struct uu_portal *p, struct response *response, unsigned consent_seconds) {
gint64 deadline = g_get_monotonic_time() + consent_seconds * G_TIME_SPAN_SECOND;
while (!response->done && !atomic_load(&p->closed) && !open_cancelled(p) &&
g_get_monotonic_time() < deadline) {
while (g_main_context_iteration(p->context, FALSE)) {}
if (!response->done) g_usleep(5000);
}
return response->done ? 0 : open_cancelled(p) ? 13 : atomic_load(&p->closed) ? 9 : 5;
}
static void close_request(struct uu_portal *p, const char *path) {
if (!path) return;
GVariant *r = g_dbus_connection_call_sync(p->bus, DEST, path, REQUEST, "Close",
@@ -82,6 +115,7 @@ static void close_request(struct uu_portal *p, const char *path) {
/* Subscribe before invocation, using the documented handle_token path. */
static GVariant *request(struct uu_portal *p, const char *interface, const char *method,
GVariantBuilder *options, int mode, int *error) {
if (open_cancelled(p)) { *error = 13; return NULL; }
char token[64];
snprintf(token, sizeof(token), "uu_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
g_variant_builder_add(options, "{sv}", "handle_token", g_variant_new_string(token));
@@ -98,7 +132,7 @@ static GVariant *request(struct uu_portal *p, const char *interface, const char
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, PATH, interface, method, args,
G_VARIANT_TYPE("(o)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, NULL);
char *actual = NULL;
if (!reply) { *error = 3; goto finish; }
if (!reply) { *error = open_cancelled(p) ? 13 : 3; goto finish; }
const char *handle;
g_variant_get(reply, "(&o)", &handle);
actual = g_strdup(handle);
@@ -111,13 +145,8 @@ static GVariant *request(struct uu_portal *p, const char *interface, const char
unsigned long parsed=strtoul(configured_timeout,&end,10);
if(end && !*end && parsed>=30 && parsed<=900) consent_seconds=(unsigned)parsed;
}
gint64 deadline = g_get_monotonic_time() + consent_seconds * G_TIME_SPAN_SECOND;
while (!response.done && !atomic_load(&p->closed) && g_get_monotonic_time() < deadline) {
while (g_main_context_iteration(p->context, FALSE)) {}
if (!response.done) g_usleep(5000);
}
if (!response.done) *error = atomic_load(&p->closed) ? 9 : 5;
else if (response.code != 0) *error = 4;
*error = wait_for_response(p, &response, consent_seconds);
if (!*error && response.code != 0) *error = 4;
finish:
if (*error) close_request(p, actual ? actual : expected);
g_dbus_connection_signal_unsubscribe(p->bus, subscription);
@@ -145,13 +174,50 @@ static int has_interface(struct uu_portal *p, const char *interface) {
if (!r) return 0;
g_variant_unref(r); return 1;
}
/* Called with the frame mutex held. Count only the continuous invalid pause,
* not a healthy pause before format removal, and never arm during startup. */
static void update_interruption(struct uu_portal *p) {
if (p->stream_started && p->state == PW_STREAM_STATE_PAUSED && !p->format_valid) {
if (!p->interrupted_at) p->interrupted_at = g_get_monotonic_time();
} else {
if (p->interrupted_at) p->display_changed_at = 0;
p->interrupted_at = 0;
}
}
static void monitors_changed(GDBusConnection *bus, const gchar *sender, const gchar *path,
const gchar *interface, const gchar *signal, GVariant *parameters, gpointer data) {
struct uu_portal *p = data;
pthread_mutex_lock(&p->lock);
/* #27: measured NULL format 10-36ms after the notification, with PAUSED
* 5-9ms before it. Preserve a correlated signal across repeated changes. */
if (p->stream_started && (!p->display_changed_at || !p->interrupted_at ||
p->display_changed_at < p->interrupted_at - PORTAL_DISPLAY_GRACE))
p->display_changed_at = g_get_monotonic_time();
pthread_mutex_unlock(&p->lock);
}
static void stream_state(void *data, enum pw_stream_state old, enum pw_stream_state state, const char *message) {
(void)old; (void)message;
struct uu_portal *p = data;
pthread_mutex_lock(&p->lock);
p->state = state;
if (state == PW_STREAM_STATE_STREAMING) {
p->stream_started = 1;
p->display_changed_at = 0;
}
update_interruption(p);
pthread_mutex_unlock(&p->lock);
if (state == PW_STREAM_STATE_ERROR || state == PW_STREAM_STATE_UNCONNECTED) fail(data, 9);
}
static void stream_format(void *data, uint32_t id, const struct spa_pod *param) {
struct uu_portal *p = data;
if (id != SPA_PARAM_Format || !param) return;
if (id != SPA_PARAM_Format) return;
if (!param) {
pthread_mutex_lock(&p->lock);
p->format_valid = 0;
update_interruption(p);
pthread_mutex_unlock(&p->lock);
return;
}
struct spa_video_info_raw info = {0};
if (spa_format_video_raw_parse(param, &info) < 0 || !info.size.width || !info.size.height ||
info.size.width > 8192 || info.size.height > 8192 ||
@@ -166,13 +232,16 @@ static void stream_format(void *data, uint32_t id, const struct spa_pod *param)
if (!p->frame) { p->error = 8; pthread_mutex_unlock(&p->lock); return; }
p->width = info.size.width; p->height = info.size.height; p->format = info.format;
p->frame_length = length;
p->format_valid = 1;
p->display_changed_at = 0;
update_interruption(p);
pthread_cond_broadcast(&p->changed);
pthread_mutex_unlock(&p->lock);
}
/* Called with the frame mutex held. Invalid transient chunks are discarded;
* allocation capacity is not evidence that valid pixels have been supplied. */
static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
if (!p->error && p->frame && spa->n_datas >= 1) {
if (!p->error && p->format_valid && p->frame && spa->n_datas >= 1) {
struct spa_data *d = &spa->datas[0];
struct spa_meta_header *header = spa_buffer_find_meta_data(spa, SPA_META_Header, sizeof(*header));
if (!d->chunk || (d->chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) ||
@@ -182,6 +251,7 @@ static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
memset(p->frame, 0, p->frame_length);
for (size_t pixel = 0; pixel < p->frame_length / 4; pixel++) p->frame[pixel * 4 + 3] = 255;
p->sequence++;
p->revision++;
return;
}
struct spa_meta_region *crop = spa_buffer_find_meta_data(spa, SPA_META_VideoCrop, sizeof(*crop));
@@ -203,6 +273,7 @@ static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
}
}
p->sequence++;
p->revision++;
}
}
}
@@ -224,10 +295,16 @@ static const struct pw_stream_events stream_events = {
PW_VERSION_STREAM_EVENTS, .state_changed = stream_state, .param_changed = stream_format, .process = stream_process
};
static int connect_pipewire(struct uu_portal *p) {
if (open_cancelled(p)) return 13;
GUnixFDList *fds = NULL;
GVariant *reply = g_dbus_connection_call_with_unix_fd_list_sync(p->bus, DEST, PATH, SCREEN,
"OpenPipeWireRemote", g_variant_new("(o@a{sv})", p->session, empty_options()),
G_VARIANT_TYPE("(h)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, &fds, NULL, NULL);
if (open_cancelled(p)) {
if (reply) g_variant_unref(reply);
if (fds) g_object_unref(fds);
return 13;
}
if (!reply) return 7;
gint32 index; g_variant_get(reply, "(h)", &index);
int fd = g_unix_fd_list_get(fds, index, NULL);
@@ -259,19 +336,54 @@ static int connect_pipewire(struct uu_portal *p) {
if (pw_stream_connect(p->stream, PW_DIRECTION_INPUT, p->node,
PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params, 2) < 0) return 7;
if (pw_thread_loop_start(p->pw_loop) < 0) return 7;
struct timespec until; clock_gettime(CLOCK_REALTIME, &until); until.tv_sec += 10;
return wait_for_first_frame(p);
}
static int wait_for_first_frame(struct uu_portal *p) {
struct timespec until;
gint64 deadline = g_get_monotonic_time() + 10 * G_TIME_SPAN_SECOND;
pthread_mutex_lock(&p->lock);
while (!p->sequence && !p->error) {
if (pthread_cond_timedwait(&p->changed, &p->lock, &until)) break;
while (!p->sequence && !p->error && !open_cancelled(p)) {
gint64 remaining = deadline - g_get_monotonic_time();
if (remaining <= 0) break;
clock_gettime(CLOCK_REALTIME, &until);
gint64 wait_us = MIN(100000, remaining);
until.tv_nsec += wait_us * 1000;
if (until.tv_nsec >= 1000000000) { until.tv_sec++; until.tv_nsec -= 1000000000; }
pthread_cond_timedwait(&p->changed, &p->lock, &until);
}
int result = p->error ? p->error : p->sequence ? 0 : 10;
int result = p->error ? p->error : open_cancelled(p) ? 13 : p->sequence ? 0 : 10;
pthread_mutex_unlock(&p->lock);
return result;
}
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *), void *data, int *error, uint32_t *width, uint32_t *height) {
static int read_stream_result(struct uu_portal *p, GVariant *results) {
guint devices = 0;
GVariant *streams = g_variant_lookup_value(results, "streams", G_VARIANT_TYPE("a(ua{sv})"));
gboolean has_devices = g_variant_lookup(results, "devices", "u", &devices);
fprintf(stderr, "Portal authorization result: devices_present=%d device_bits=%u stream_count=%zu\n",
has_devices, devices, streams ? (size_t)g_variant_n_children(streams) : 0);
int error = 0;
if (!has_devices || (devices & 3) != 3 || !streams || g_variant_n_children(streams) != 1) {
error = 6;
} else {
GVariant *entry = g_variant_get_child_value(streams, 0), *properties;
g_variant_get(entry, "(u@a{sv})", &p->node, &properties);
/* Optional 'size' is compositor logical coordinates, not frame pixels. */
g_variant_lookup(properties, "size", "(ii)", &p->logical_width, &p->logical_height);
const char *mapping_id = NULL;
if (g_variant_lookup(properties, "mapping_id", "&s", &mapping_id)) p->mapping_id = g_strdup(mapping_id);
g_variant_unref(properties); g_variant_unref(entry);
}
if (streams) g_variant_unref(streams);
return error;
}
static struct uu_portal *open_persist(const char *restore, void (*save_token)(const char *, void *),
void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data,
int *error, uint32_t *width, uint32_t *height) {
*error = 0; *width = *height = 0;
struct uu_portal *p = calloc(1, sizeof(*p));
if (!p) { *error = 8; return NULL; }
p->cancelled = cancelled;
p->cancel_data = cancel_data;
/* GNOME forwards NotifyPointerMotionAbsolute into Mutter's stream-to-stage
* transform. It expects stream pixels, unlike the compositor-sized KDE path.
* Do not apply the compositor scale twice on fractional-scale displays. */
@@ -283,7 +395,10 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
p->context = g_main_context_new(); g_main_context_push_thread_default(p->context);
p->bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
if (!p->bus) { *error = 1; goto done; }
if (!has_interface(p, REMOTE) || !has_interface(p, SCREEN)) { *error = 2; goto done; }
if (open_cancelled(p)) { *error = 13; goto done; }
int interfaces_available = has_interface(p, REMOTE) && has_interface(p, SCREEN);
if (open_cancelled(p)) { *error = 13; goto done; }
if (!interfaces_available) { *error = 2; goto done; }
GVariantBuilder options; GVariant *results;
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
char token[64]; snprintf(token, sizeof(token), "uu_session_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
@@ -338,18 +453,7 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
g_variant_lookup(results,"clipboard_enabled","b",&clipboard_enabled);
p->clipboard_enabled=clipboard_enabled;
fprintf(stderr,"Portal clipboard permission: granted=%d\n",clipboard_enabled);
guint devices = 0; GVariant *streams = g_variant_lookup_value(results, "streams", G_VARIANT_TYPE("a(ua{sv})"));
gboolean has_devices=g_variant_lookup(results, "devices", "u", &devices);
fprintf(stderr,"Portal authorization result: devices_present=%d device_bits=%u stream_count=%zu\n",has_devices,devices,streams?(size_t)g_variant_n_children(streams):0);
if (!has_devices || (devices & 3) != 3 || !streams || g_variant_n_children(streams) != 1) {
*error = 6;
} else {
GVariant *entry = g_variant_get_child_value(streams, 0), *properties;
g_variant_get(entry, "(u@a{sv})", &p->node, &properties);
/* 'size' is compositor logical coordinates, not the pixel frame size. */
g_variant_lookup(properties, "size", "(ii)", &p->logical_width, &p->logical_height);
g_variant_unref(properties); g_variant_unref(entry);
}
*error = read_stream_result(p, results);
if (!*error) {
const char *next = NULL;
if (g_variant_lookup(results, "restore_token", "&s", &next) && next && strlen(next) <= 4096)
@@ -357,25 +461,45 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
fprintf(stderr, "Portal persistence: requested=%d restore_attempted=%d token_returned=%d\n",
version >= 2, restore && *restore, p->restore_token && *p->restore_token);
}
if (!*error && p->restore_token && save_token) save_token(p->restore_token, data);
if (streams) g_variant_unref(streams);
if (!*error && p->restore_token && save_token) save_token(p->restore_token, save_data);
g_variant_unref(results);
if (*error) goto done;
p->main_loop = g_main_loop_new(p->context, FALSE);
if (p->absolute_stream_pixels)
p->display_subscription = g_dbus_connection_signal_subscribe(p->bus, MUTTER_DISPLAY, MUTTER_DISPLAY,
"MonitorsChanged", MUTTER_DISPLAY_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE, monitors_changed, p, NULL);
g_main_context_pop_thread_default(p->context);
if (pthread_create(&p->dbus_thread, NULL, run_dbus, p)) { *error = 3; uu_portal_close(p); return NULL; }
if (pthread_create(&p->dbus_thread, NULL, run_dbus, p)) {
*error = 3; p->cancelled = NULL; p->cancel_data = NULL;
uu_portal_close(p); return NULL;
}
p->dbus_started = 1;
*error = connect_pipewire(p);
if (*error) { uu_portal_close(p); return NULL; }
if (*error) {
p->cancelled = NULL; p->cancel_data = NULL;
uu_portal_close(p); return NULL;
}
fprintf(stderr, "Portal pointer geometry: frame=%ux%u compositor=%dx%d absolute=%s\n",
p->width, p->height, p->logical_width, p->logical_height,
p->absolute_stream_pixels ? "stream-pixels" : "compositor-logical");
p->absolute_stream_pixels ? "stream-pixels-or-eis" : "compositor-logical");
*width = p->width; *height = p->height;
p->cancelled = NULL; p->cancel_data = NULL;
return p;
done:
g_main_context_pop_thread_default(p->context);
p->cancelled = NULL; p->cancel_data = NULL;
uu_portal_close(p); return NULL;
}
struct uu_portal *uu_portal_open_persist_cancellable(const char *restore,
void (*save_token)(const char *, void *), void *save_data,
uu_portal_cancelled_fn cancelled, void *cancel_data,
int *error, uint32_t *width, uint32_t *height) {
return open_persist(restore, save_token, save_data, cancelled, cancel_data, error, width, height);
}
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *),
void *data, int *error, uint32_t *width, uint32_t *height) {
return open_persist(restore, save_token, data, NULL, NULL, error, width, height);
}
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height) {
return uu_portal_open_persist(restore, NULL, NULL, error, width, height);
}
@@ -387,6 +511,7 @@ static gboolean quit_dbus_loop(gpointer data) { g_main_loop_quit(data); return G
void uu_portal_close(struct uu_portal *p) {
if (!p) return;
atomic_store(&p->closed, 1);
portal_eis_close(p);
if (p->bus && p->session) {
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, p->session, SESSION, "Close",
NULL, NULL, G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL);
@@ -395,6 +520,7 @@ void uu_portal_close(struct uu_portal *p) {
if (p->clipboard_owner_subscription) g_dbus_connection_signal_unsubscribe(p->bus,p->clipboard_owner_subscription);
if (p->clipboard_transfer_subscription) g_dbus_connection_signal_unsubscribe(p->bus,p->clipboard_transfer_subscription);
if (p->closed_subscription) g_dbus_connection_signal_unsubscribe(p->bus, p->closed_subscription);
if (p->display_subscription) g_dbus_connection_signal_unsubscribe(p->bus, p->display_subscription);
if (p->dbus_started) {
/* A queued source also handles close racing with thread startup; an
* immediate g_main_loop_quit before run would leave run blocking. */
@@ -402,6 +528,8 @@ void uu_portal_close(struct uu_portal *p) {
g_source_set_callback(quit, quit_dbus_loop, p->main_loop, NULL);
g_source_attach(quit, p->context); g_source_unref(quit);
g_main_context_wakeup(p->context); pthread_join(p->dbus_thread, NULL);
} else if (p->context) {
while (g_main_context_iteration(p->context, FALSE)) {}
}
if (p->pw_loop) pw_thread_loop_stop(p->pw_loop);
if (p->stream) pw_stream_destroy(p->stream);
@@ -411,6 +539,7 @@ void uu_portal_close(struct uu_portal *p) {
if (p->main_loop) g_main_loop_unref(p->main_loop);
if (p->context) g_main_context_unref(p->context);
if (p->bus) g_object_unref(p->bus);
g_free(p->mapping_id);
g_free(p->clipboard_text); g_free(p->clipboard_mime); g_free(p->restore_token); free(p->session); free(p->frame);
pthread_cond_destroy(&p->changed); pthread_mutex_destroy(&p->lock); free(p);
}
@@ -418,23 +547,80 @@ void uu_portal_reset_frame(struct uu_portal *p) {
if (!p) return;
pthread_mutex_lock(&p->lock);
p->sequence = 0;
p->revision++;
if (p->frame) memset(p->frame, 0, p->frame_length);
pthread_mutex_unlock(&p->lock);
}
int uu_portal_capture(struct uu_portal *p, uint8_t *frame, size_t length) {
return uu_portal_capture_since(p, frame, length, NULL);
}
/* A static desktop can stop producing frames indefinitely. Only time out the
* paused, format-less renegotiation observed after a live scaling change (#25).
* Called with the frame mutex held; recovery runs on the Rust capture worker. */
static int stream_stalled(struct uu_portal *p) {
if (p->state != PW_STREAM_STATE_PAUSED || p->format_valid || !p->interrupted_at) return 0;
gint64 now = g_get_monotonic_time();
return now - p->interrupted_at >= PORTAL_RENEGOTIATION_TIMEOUT ||
(p->display_changed_at && p->display_changed_at >= p->interrupted_at - PORTAL_DISPLAY_GRACE &&
now - MAX(p->interrupted_at, p->display_changed_at) >= PORTAL_DISPLAY_GRACE);
}
int uu_portal_capture_since(struct uu_portal *p, uint8_t *frame, size_t length, uint64_t *revision) {
if (!p || !frame) return 11;
pthread_mutex_lock(&p->lock);
int result = atomic_load(&p->closed) ? 9 : p->error ? p->error : !p->sequence ? 10 : length != p->frame_length ? 11 : 0;
if (!result) memcpy(frame, p->frame, length);
int result = atomic_load(&p->closed) ? 9 : p->error ? p->error : stream_stalled(p) ? 9 :
!p->sequence ? 10 : length != p->frame_length ? 11 : 0;
if (!result) {
if (revision && *revision == p->revision) result = 12;
else memcpy(frame, p->frame, length);
if (revision) *revision = p->revision;
}
pthread_mutex_unlock(&p->lock); return result;
}
int uu_portal_alive(struct uu_portal *p) {
if (!p || atomic_load(&p->closed)) return 0;
if (!p || atomic_load(&p->closed) || atomic_load(&p->ei_failed)) return 0;
pthread_mutex_lock(&p->lock);
int alive = !p->error;
pthread_mutex_unlock(&p->lock);
return alive;
}
/* Video renegotiation must not block input releases or clipboard traffic. */
int uu_portal_capture_alive(struct uu_portal *p) {
if (!uu_portal_alive(p)) return 0;
pthread_mutex_lock(&p->lock);
int alive = !stream_stalled(p);
pthread_mutex_unlock(&p->lock);
return alive;
}
void uu_portal_input_dispatch(struct uu_portal *p) {
if (p->ei) portal_eis_dispatch(p);
}
static int prepare_input(struct uu_portal *p) {
if (!uu_portal_alive(p)) return 9;
/* xdg-desktop-portal 1.21 checks Notify absolute positions against logical
* stream size, while GNOME 50 forwards them into Mutter's pixel transform.
* Initialize EIS only when native input is used, leaving capture/uinput
* independent of EIS support. ConnectToEIS disables all Notify* methods. */
if (!p->ei && !p->ei_unavailable && p->absolute_stream_pixels &&
p->logical_width > 0 && p->logical_height > 0 &&
(p->width != (uint32_t)p->logical_width || p->height != (uint32_t)p->logical_height)) {
int result = portal_eis_open(p);
if (result == PORTAL_EIS_UNSUPPORTED) {
p->ei_unavailable = 1;
fprintf(stderr, "Portal EIS unavailable; retaining legacy input\n");
return 0;
}
if (result) {
/* A timeout may have committed ConnectToEIS at the portal. Any
* failure except explicit lack of support needs a fresh session. */
atomic_store(&p->ei_failed, 1);
static atomic_uint failures;
if (atomic_fetch_add(&failures, 1) < 12)
fprintf(stderr, "Portal EIS input initialization failed: code=%d\n", result);
return result;
}
}
return 0;
}
static int notify(struct uu_portal *p, const char *method, GVariant *parameters) {
if (!uu_portal_alive(p)) { g_variant_ref_sink(parameters); g_variant_unref(parameters); return 9; }
GError *error = NULL;
@@ -451,18 +637,29 @@ static int notify(struct uu_portal *p, const char *method, GVariant *parameters)
}
g_variant_unref(reply); return 0;
}
void uu_portal_logical_size(struct uu_portal *p, uint32_t *width, uint32_t *height) {
/* Stream size is optional; unknown logical geometry keeps the existing
* pixel-sized inventory without preventing capture or uinput. */
int known = p->logical_width > 0 && p->logical_height > 0;
*width = known ? (uint32_t)p->logical_width : p->width;
*height = known ? (uint32_t)p->logical_height : p->height;
}
int uu_portal_move(struct uu_portal *p, double x, double y, int relative) {
if (!p || !isfinite(x) || !isfinite(y)) return 11;
if (p->logical_width <= 0 || p->logical_height <= 0 || !p->width || !p->height) return 11;
if (!relative && (x < 0 || y < 0 || x >= p->width || y >= p->height)) return 11;
int result = prepare_input(p); if (result) return result;
if (p->ei) return portal_eis_move(p, x, y, relative);
if (relative) return notify(p, "NotifyPointerMotion", g_variant_new("(o@a{sv}dd)", p->session, empty_options(),
x * p->logical_width / p->width, y * p->logical_height / p->height));
if (x < 0 || y < 0 || x >= p->width || y >= p->height) return 11;
return notify(p, "NotifyPointerMotionAbsolute", g_variant_new("(o@a{sv}udd)", p->session, empty_options(), p->node,
p->absolute_stream_pixels ? x : x * p->logical_width / p->width,
p->absolute_stream_pixels ? y : y * p->logical_height / p->height));
}
int uu_portal_button(struct uu_portal *p, int button, int pressed) {
if (!p) return 11;
int result = prepare_input(p); if (result) return result;
if (p->ei) return portal_eis_button(p, button, pressed);
if (button >= 4 && button <= 7) {
if (!pressed) return 0;
return notify(p, "NotifyPointerAxisDiscrete", g_variant_new("(o@a{sv}ui)", p->session, empty_options(),
@@ -472,7 +669,9 @@ int uu_portal_button(struct uu_portal *p, int button, int pressed) {
if (!code) return 11;
return notify(p, "NotifyPointerButton", g_variant_new("(o@a{sv}iu)", p->session, empty_options(), code, (guint)!!pressed));
}
int uu_portal_key(struct uu_portal *p, uint32_t symbol, int pressed) {
int uu_portal_key(struct uu_portal *p, uint32_t symbol, int keycode, int pressed) {
if (!p || !symbol || symbol > INT32_MAX) return 11;
int result = prepare_input(p); if (result) return result;
if (p->ei) return portal_eis_key(p, keycode, pressed);
return notify(p, "NotifyKeyboardKeysym", g_variant_new("(o@a{sv}iu)", p->session, empty_options(), (gint)symbol, (guint)!!pressed));
}
+8 -1
View File
@@ -3,10 +3,13 @@
#include <stddef.h>
#include <stdint.h>
struct uu_portal;
typedef int (*uu_portal_cancelled_fn)(void *data);
/* One object owns one consent/session and one PipeWire stream. */
struct uu_portal *uu_portal_open(int *error, uint32_t *width, uint32_t *height);
/* Saves the rotated token immediately after Start, before video initialization. */
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *), void *data, int *error, uint32_t *width, uint32_t *height);
/* The cancellation callback and data are used only until this call returns. */
struct uu_portal *uu_portal_open_persist_cancellable(const char *restore, void (*save_token)(const char *, void *), void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data, int *error, uint32_t *width, uint32_t *height);
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height);
const char *uu_portal_restore_token(struct uu_portal *portal);
void uu_portal_close(struct uu_portal *portal);
@@ -14,9 +17,13 @@ int uu_portal_clipboard_ready(struct uu_portal *portal);
int uu_portal_clipboard_set(struct uu_portal *portal, const uint8_t *text, size_t length);
int uu_portal_clipboard_poll(struct uu_portal *portal, uint8_t *text, size_t capacity);
int uu_portal_alive(struct uu_portal *portal);
int uu_portal_capture_alive(struct uu_portal *portal);
void uu_portal_input_dispatch(struct uu_portal *portal);
void uu_portal_logical_size(struct uu_portal *portal, uint32_t *width, uint32_t *height);
void uu_portal_reset_frame(struct uu_portal *portal);
int uu_portal_capture(struct uu_portal *portal, uint8_t *bgra, size_t length);
int uu_portal_capture_since(struct uu_portal *portal, uint8_t *bgra, size_t length, uint64_t *revision);
int uu_portal_move(struct uu_portal *portal, double x, double y, int relative);
int uu_portal_button(struct uu_portal *portal, int button, int pressed);
int uu_portal_key(struct uu_portal *portal, uint32_t keysym, int pressed);
int uu_portal_key(struct uu_portal *portal, uint32_t keysym, int keycode, int pressed);
#endif
+94 -32
View File
@@ -21,6 +21,7 @@
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static struct wl_display *display;
static struct wl_registry *registry;
static struct wl_shm *shm;
static struct wl_seat *seat;
static struct zwlr_screencopy_manager_v1 *copy_manager;
@@ -65,18 +66,22 @@ static int initialized;
/* Independent slots let the compositor fill the next frame during delivery. */
struct frame {
struct zwlr_screencopy_frame_v1 *handle;
struct wl_buffer *buffer;
void *pixels;
size_t length;
struct wl_buffer *buffer,*shm_buffer;
struct wl_shm_pool *shm_pool;
void *pixels,*shm_pixels;
size_t shm_length;
int shm_fd,shm_busy;
uint32_t shm_width,shm_height,shm_stride,shm_format;
uint32_t width,height,stride,format,flags;
int done,failed,submitted;
int done,failed,submitted,released;
struct zwp_linux_buffer_params_v1 *params;
struct gbm_bo *bo;
int bo_fd,dmabuf_used;
void *map_data;
uint32_t dma_format,dma_width,dma_height;
uint32_t bo_format,bo_width,bo_height;
};
static struct frame frames[2];
static struct frame frames[2]={{.shm_fd=-1,.bo_fd=-1},{.shm_fd=-1,.bo_fd=-1}};
static unsigned pending;
static uint32_t timestamp(void) {
@@ -177,15 +182,37 @@ static void dma_init(void) {
gbm.fd=open(device && *device ? device:"/dev/dri/renderD128",O_RDWR|O_CLOEXEC);
if(gbm.fd<0 || !(gbm.device=gbm.create_device(gbm.fd))) dma_disable("render-device");
}
static void shm_clear(struct frame *frame) {
if(frame->shm_buffer) wl_buffer_destroy(frame->shm_buffer);
if(frame->shm_pool) wl_shm_pool_destroy(frame->shm_pool);
if(frame->shm_pixels) munmap(frame->shm_pixels,frame->shm_length);
if(frame->shm_fd>=0) close(frame->shm_fd);
frame->shm_buffer=NULL; frame->shm_pool=NULL; frame->shm_pixels=NULL;
frame->shm_length=0; frame->shm_fd=-1; frame->shm_busy=0;
frame->shm_width=frame->shm_height=frame->shm_stride=frame->shm_format=0;
}
static void dma_clear(struct frame *frame) {
if(frame->buffer) wl_buffer_destroy(frame->buffer);
if(frame->dmabuf_used && frame->pixels) {
gbm.bo_unmap(frame->bo,frame->map_data); frame->pixels=NULL; frame->map_data=NULL;
}
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
frame->buffer=NULL; frame->bo=NULL; frame->bo_fd=-1; frame->dmabuf_used=frame->released=0;
frame->bo_format=frame->bo_width=frame->bo_height=0;
}
static void frame_clear(struct frame *frame) {
if(frame->handle) zwlr_screencopy_frame_v1_destroy(frame->handle);
if(frame->params) zwp_linux_buffer_params_v1_destroy(frame->params);
if(frame->buffer) wl_buffer_destroy(frame->buffer);
if(frame->dmabuf_used && frame->pixels) gbm.bo_unmap(frame->bo,frame->map_data);
else if(frame->pixels) munmap(frame->pixels,frame->length);
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
memset(frame,0,sizeof(*frame));
if(frame->shm_busy) shm_clear(frame);
frame->handle=NULL; frame->params=NULL;
frame->pixels=NULL; frame->map_data=NULL; frame->dmabuf_used=0;
frame->width=frame->height=frame->stride=frame->format=frame->flags=0;
frame->done=frame->failed=frame->submitted=0;
frame->dma_format=frame->dma_width=frame->dma_height=0;
}
static void buffer_release(void*d,struct wl_buffer*b) { ((struct frame*)d)->released=1; }
static const struct wl_buffer_listener buffer_listener={buffer_release};
static void frame_fail(struct frame *frame) { frame->failed=frame->done=frame->submitted=1; }
static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h,uint32_t s) {
struct frame *frame=d;
@@ -194,22 +221,32 @@ static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,ui
(fmt!=WL_SHM_FORMAT_XRGB8888 && fmt!=WL_SHM_FORMAT_ARGB8888 &&
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { frame_fail(frame); return; }
size_t length=(size_t)s*h;
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
frame->pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (frame->pixels==MAP_FAILED) { frame->pixels=NULL; close(fd); frame_fail(frame); return; }
frame->length=length;
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
frame->buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
wl_shm_pool_destroy(pool); close(fd);
if (frame->shm_busy || !frame->shm_buffer || !frame->shm_pool || !frame->shm_pixels || frame->shm_fd<0 ||
frame->shm_length!=length || frame->shm_width!=w || frame->shm_height!=h ||
frame->shm_stride!=s || frame->shm_format!=fmt) {
shm_clear(frame);
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
void *pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (pixels==MAP_FAILED) { close(fd); frame_fail(frame); return; }
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
if (!pool) { munmap(pixels,length); close(fd); frame_fail(frame); return; }
struct wl_buffer *buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
if (!buffer) { wl_shm_pool_destroy(pool); munmap(pixels,length); close(fd); frame_fail(frame); return; }
frame->shm_fd=fd; frame->shm_pixels=pixels; frame->shm_length=length;
frame->shm_pool=pool; frame->shm_buffer=buffer;
frame->shm_width=w; frame->shm_height=h; frame->shm_stride=s; frame->shm_format=fmt;
}
frame->pixels=frame->shm_pixels;
/* copy is valid immediately for SHM, including frame versions <3. */
if (zwlr_screencopy_frame_v1_get_version(f)<3) {
zwlr_screencopy_frame_v1_copy(f,frame->buffer); frame->submitted=1;
zwlr_screencopy_frame_v1_copy(f,frame->shm_buffer); frame->shm_busy=frame->submitted=1;
}
}
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { ((struct frame*)d)->flags=value; }
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) {
struct frame *frame=d;
frame->shm_busy=0;
if(frame->dmabuf_used) {
/* GBM READ mapping synchronizes readback, possibly via a staging buffer. */
uint32_t stride=0;
@@ -223,7 +260,7 @@ static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint
}
frame->done=1;
}
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { frame_fail(d); }
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { ((struct frame*)d)->shm_busy=0; frame_fail(d); }
static void copy_damage(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t x,uint32_t y,uint32_t w,uint32_t h) {}
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {
struct frame *frame=d;
@@ -232,30 +269,40 @@ static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,ui
}
}
static void submit_shm(struct frame *frame) {
if(frame->buffer && !frame->failed) {
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); frame->submitted=1;
if(frame->shm_buffer && !frame->shm_busy && !frame->failed) {
zwlr_screencopy_frame_v1_copy(frame->handle,frame->shm_buffer); frame->shm_busy=frame->submitted=1;
} else frame_fail(frame);
}
static void dma_created(void*d,struct zwp_linux_buffer_params_v1*params,struct wl_buffer*buffer) {
struct frame *frame=d;
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
wl_buffer_destroy(frame->buffer);
munmap(frame->pixels,frame->length); frame->pixels=NULL; frame->length=0;
frame->pixels=NULL;
frame->buffer=buffer; frame->dmabuf_used=1;
frame->width=frame->dma_width; frame->height=frame->dma_height;
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
wl_buffer_add_listener(buffer,&buffer_listener,frame);
frame->released=0;
zwlr_screencopy_frame_v1_copy(frame->handle,buffer); frame->submitted=1;
}
static void dma_failed(void*d,struct zwp_linux_buffer_params_v1*params) {
struct frame *frame=d;
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
dma_disable("import"); submit_shm(frame);
dma_disable("import"); dma_clear(frame); submit_shm(frame);
}
static const struct zwp_linux_buffer_params_v1_listener dma_params_listener={dma_created,dma_failed};
static int dma_start(struct frame *frame) {
if(!dmabuf_requested || dmabuf_disabled || !gbm.device || frame->failed || !frame->buffer ||
if(!dmabuf_requested || dmabuf_disabled || !gbm.device || frame->failed || !frame->shm_buffer ||
!(dmabuf_linear_formats&dma_format_bit(frame->dma_format))) return 0;
if(frame->buffer && frame->bo && frame->released && frame->bo_width==frame->dma_width &&
frame->bo_height==frame->dma_height && frame->bo_format==frame->dma_format) {
frame->pixels=NULL; frame->dmabuf_used=1; frame->released=0;
frame->width=frame->dma_width; frame->height=frame->dma_height;
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); frame->submitted=1; return 1;
}
dma_clear(frame);
/* Single-plane linear RGB only; unsupported imports are asynchronous failures,
never create_immed protocol errors. No privilege or device probing changes. */
frame->bo=gbm.bo_create(gbm.device,frame->dma_width,frame->dma_height,frame->dma_format,(1u<<2)|(1u<<4));
@@ -264,7 +311,8 @@ static int dma_start(struct frame *frame) {
uint32_t stride=gbm.bo_get_stride(frame->bo);
if(gbm.bo_get_plane_count(frame->bo)!=1 || gbm.bo_get_modifier(frame->bo)!=0 ||
stride<frame->dma_width*4 || stride>131072 || (uint64_t)stride*frame->dma_height>256*1024*1024 ||
(frame->bo_fd=gbm.bo_get_fd(frame->bo))<0) {dma_disable("layout-or-export"); return 0;}
(frame->bo_fd=gbm.bo_get_fd(frame->bo))<0) {dma_disable("layout-or-export"); dma_clear(frame); return 0;}
frame->bo_format=frame->dma_format; frame->bo_width=frame->dma_width; frame->bo_height=frame->dma_height;
frame->params=zwp_linux_dmabuf_v1_create_params(dmabuf);
zwp_linux_buffer_params_v1_add_listener(frame->params,&dma_params_listener,frame);
zwp_linux_buffer_params_v1_add(frame->params,frame->bo_fd,0,gbm.bo_get_offset(frame->bo,0),stride,0,0);
@@ -285,14 +333,25 @@ static void reset(void) {
if (key_state) xkb_state_unref(key_state);
if (keymap) xkb_keymap_unref(keymap);
if (xkb) xkb_context_unref(xkb);
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); shm_clear(&frames[i]); }
if(gbm.device) gbm.device_destroy(gbm.device);
if(gbm.fd>=0) close(gbm.fd);
if(gbm.library) dlclose(gbm.library);
memset(&gbm,0,sizeof(gbm)); gbm.fd=-1;
/* Disconnect drops every proxy; failure never leaves an initialized backend. */
if(keyboard) zwp_virtual_keyboard_v1_destroy(keyboard);
if(pointer) zwlr_virtual_pointer_v1_destroy(pointer);
if(power) zwlr_output_power_v1_destroy(power);
if(keyboard_manager) zwp_virtual_keyboard_manager_v1_destroy(keyboard_manager);
if(pointer_manager) zwlr_virtual_pointer_manager_v1_destroy(pointer_manager);
if(power_manager) zwlr_output_power_manager_v1_destroy(power_manager);
if(copy_manager) zwlr_screencopy_manager_v1_destroy(copy_manager);
if(dmabuf) zwp_linux_dmabuf_v1_destroy(dmabuf);
for(unsigned i=0;i<output_count;i++) if(outputs[i].handle) wl_output_destroy(outputs[i].handle);
if(seat) wl_seat_destroy(seat);
if(shm) wl_shm_destroy(shm);
if(registry) wl_registry_destroy(registry);
if (display) wl_display_disconnect(display);
display=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
display=NULL; registry=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL;
power_manager=NULL; power=NULL; power_failed=0;
dmabuf=NULL; dmabuf_requested=dmabuf_disabled=dmabuf_reported=0; dmabuf_linear_formats=0;
@@ -305,7 +364,7 @@ int uu_wl_init(int32_t *w,int32_t *h) {
int result=-1;
const char *dma=getenv("UU_WAYLAND_DMABUF"); dmabuf_requested=dma && !strcmp(dma,"1");
display=wl_display_connect(NULL); if (!display) goto end;
struct wl_registry *registry=wl_display_get_registry(display);
registry=wl_display_get_registry(display);
wl_registry_add_listener(registry,&registry_listener,NULL);
if(sync_display()!=0 || sync_display()!=0) goto end;
dma_init();
@@ -347,6 +406,8 @@ end:
}
int uu_wl_wake(void) {
pthread_mutex_lock(&lock); int result=-1;
/* Output power management is optional; missing it must not block screencopy. */
if(initialized && !power) result=0;
if(initialized && power && !power_failed) {
zwlr_output_power_v1_set_mode(power,ZWLR_OUTPUT_POWER_V1_MODE_ON);
if(sync_display()==0 && !power_failed) result=0;
@@ -364,7 +425,8 @@ retry:;
if(frame->failed && frame->dmabuf_used && !retried) {
/* Mapping/copy failure retries once on SHM; connection errors do not. */
dma_disable("copy"); retried=1;
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
/* dma-buf stays disabled, so its cached buffers are never reused. */
for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); }
pending=0; goto retry;
}
if(frame->failed || !frame->pixels || frame->width!=width || frame->height!=height) goto end;
@@ -389,7 +451,7 @@ retry:;
}
frame_clear(frame); pending=1-pending;
end:
if(result!=0) for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
if(result!=0) for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); }
pthread_mutex_unlock(&lock); return result;
}
int uu_wl_move(int32_t x,int32_t y,int relative) {
+70 -45
View File
@@ -16,6 +16,7 @@
#include <X11/extensions/Xfixes.h>
#include <stdint.h>
#include <poll.h>
#include <pthread.h>
static Display *g_display = NULL;
static Window g_root = 0;
@@ -27,6 +28,8 @@ static int g_depth = 0;
static XShmSegmentInfo g_shminfo;
static XImage *g_shm_image = NULL;
static int g_use_shm = 0;
/* Guards the capture geometry and SHM image against a resize between connections. */
static pthread_mutex_t g_capture_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned char g_injected_down[32]; // keycodes currently held by the remote viewer
static int g_injected_count = 0;
static int g_restore_repeat = 0;
@@ -52,6 +55,34 @@ static void track_injected_key(int keycode, int is_press) {
}
}
static void shm_open_image(void) {
g_use_shm = 0;
if (!XShmQueryExtension(g_display)) return;
g_shm_image = XShmCreateImage(g_display, DefaultVisual(g_display, g_screen),
g_depth, ZPixmap, NULL, &g_shminfo, g_width, g_height);
if (!g_shm_image) return;
g_shminfo.shmid = shmget(IPC_PRIVATE, g_shm_image->bytes_per_line * g_shm_image->height, IPC_CREAT | 0777);
if (g_shminfo.shmid == -1) return;
g_shminfo.shmaddr = g_shm_image->data = (char *)shmat(g_shminfo.shmid, 0, 0);
g_shminfo.readOnly = False;
if (g_shminfo.shmaddr != (char *)-1 && XShmAttach(g_display, &g_shminfo)) {
XSync(g_display, False);
g_use_shm = 1;
}
shmctl(g_shminfo.shmid, IPC_RMID, 0); // 标记删除,进程退出时自动回收;失败时也不遗留段
}
static void shm_close_image(void) {
if (g_use_shm) XShmDetach(g_display, &g_shminfo);
if (g_shm_image) {
g_shm_image->data = NULL; /* shm memory is released by shmdt, not XDestroyImage */
XDestroyImage(g_shm_image);
g_shm_image = NULL;
}
if (g_shminfo.shmaddr && g_shminfo.shmaddr != (char *)-1) shmdt(g_shminfo.shmaddr);
memset(&g_shminfo, 0, sizeof(g_shminfo));
g_use_shm = 0;
}
int x11_init(const char *display_name) {
if (g_display) return 0;
@@ -77,59 +108,60 @@ int x11_init(const char *display_name) {
g_height = DisplayHeight(g_display, g_screen);
g_depth = DefaultDepth(g_display, g_screen);
// 尝试初始化 XShm
if (XShmQueryExtension(g_display)) {
g_shm_image = XShmCreateImage(g_display, DefaultVisual(g_display, g_screen),
g_depth, ZPixmap, NULL, &g_shminfo, g_width, g_height);
if (g_shm_image) {
g_shminfo.shmid = shmget(IPC_PRIVATE, g_shm_image->bytes_per_line * g_shm_image->height, IPC_CREAT | 0777);
if (g_shminfo.shmid != -1) {
g_shminfo.shmaddr = g_shm_image->data = (char *)shmat(g_shminfo.shmid, 0, 0);
g_shminfo.readOnly = False;
if (g_shminfo.shmaddr != (char *)-1) {
if (XShmAttach(g_display, &g_shminfo)) {
XSync(g_display, False);
shmctl(g_shminfo.shmid, IPC_RMID, 0); // 标记删除,进程退出时自动回收
g_use_shm = 1;
}
}
}
}
}
shm_open_image();
return 0;
}
int x11_get_dimensions(int *out_w, int *out_h) {
if (!g_display) return -1;
pthread_mutex_lock(&g_capture_lock);
*out_w = g_width;
*out_h = g_height;
pthread_mutex_unlock(&g_capture_lock);
return 0;
}
/* The startup size goes stale when RandR/xrdp resizes the root window. Query the
* server and rebuild the SHM image on change. Returns 1 if changed, 0 if not, <0 on error. */
int x11_refresh_geometry(void) {
if (!g_display) return -1;
Window root; int x, y; unsigned int w, h, border, depth;
if (!XGetGeometry(g_display, g_root, &root, &x, &y, &w, &h, &border, &depth) || !w || !h) return -2;
pthread_mutex_lock(&g_capture_lock);
int changed = (int)w != g_width || (int)h != g_height;
if (changed) {
shm_close_image();
g_width = (int)w; g_height = (int)h;
shm_open_image();
}
pthread_mutex_unlock(&g_capture_lock);
return changed;
}
/**
* 截取一帧画面(RGBA/BGRA 格式)
* @param out_buffer 输出缓冲区,大小必须至少为 width * height * 4
*/
/* *out_w/*out_h carry the caller's buffer geometry in; a mismatch (desktop resized
* since that buffer was sized) returns -4 instead of overflowing it. */
int x11_capture_frame(unsigned char *out_buffer, int *out_w, int *out_h) {
if (!g_display) return -1;
pthread_mutex_lock(&g_capture_lock);
int result = 0;
if (*out_w != g_width || *out_h != g_height) result = -4;
else if (g_use_shm && g_shm_image) {
if (!XShmGetImage(g_display, g_root, g_shm_image, 0, 0, AllPlanes)) result = -2;
else memcpy(out_buffer, g_shm_image->data, (size_t)g_width * g_height * 4);
} else {
// Fallback: XGetImage
XImage *img = XGetImage(g_display, g_root, 0, 0, g_width, g_height, AllPlanes, ZPixmap);
if (!img) result = -3;
else { memcpy(out_buffer, img->data, (size_t)g_width * g_height * 4); XDestroyImage(img); }
}
*out_w = g_width;
*out_h = g_height;
if (g_use_shm && g_shm_image) {
if (!XShmGetImage(g_display, g_root, g_shm_image, 0, 0, AllPlanes)) {
return -2;
}
memcpy(out_buffer, g_shm_image->data, g_width * g_height * 4);
return 0;
}
// Fallback: XGetImage
XImage *img = XGetImage(g_display, g_root, 0, 0, g_width, g_height, AllPlanes, ZPixmap);
if (!img) return -3;
memcpy(out_buffer, img->data, g_width * g_height * 4);
XDestroyImage(img);
return 0;
pthread_mutex_unlock(&g_capture_lock);
return result;
}
// ============ 原生输入注入 ============
@@ -261,16 +293,9 @@ int x11_terminal_shortcut_state(void) {
void x11_close() {
if (!g_display) return;
if (g_use_shm) {
XShmDetach(g_display, &g_shminfo);
if (g_shm_image) {
XDestroyImage(g_shm_image);
g_shm_image = NULL;
}
if (g_shminfo.shmaddr && g_shminfo.shmaddr != (char *)-1) {
shmdt(g_shminfo.shmaddr);
}
}
pthread_mutex_lock(&g_capture_lock);
shm_close_image();
pthread_mutex_unlock(&g_capture_lock);
XCloseDisplay(g_display);
g_display = NULL;
}
+15
View File
@@ -0,0 +1,15 @@
Mac 快捷键和在 Mac 上一样用。/ Mac shortcuts work as on a Mac.
### 新增 / Added
- **Mac 快捷键**:在 Mac 远控窗口菜单里**关闭「使用 macOS 快捷键」**后:
- Cmd+C / V / A / F / X / Z 是复制、粘贴、全选、查找、剪切、撤销(Cmd+Shift+Z 等组合同样可用);
- 在终端里 Cmd+C / Cmd+V 自动变成终端的复制、粘贴(Ctrl+Shift+C / V),**Ctrl+C 始终是中断**;
- 其他 Cmd 组合键和单独按 Cmd 仍然是 Super(Windows 键)。
只对 Mac 控制端生效(按连接请求里的控制端平台判断),Windows 和手机控制端不受影响。✅ 已用官方 Mac 客户端在 X11 实机验收。
保持该选项开启时行为和以前一样(客户端会把 Cmd 当成 Ctrl 发送)。
With 「使用 macOS 快捷键」 off, Cmd shortcuts behave as on a Mac (terminal-aware copy/paste), only for macOS viewers.
### 下载 / Download
`uu-remote-linux-v0.1.12-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+17
View File
@@ -0,0 +1,17 @@
托盘图标。/ Tray icon.
### 新增 / Added
- **托盘图标**:后台运行时,顶栏/任务栏会显示托盘图标,菜单里有「打开主窗口」、「允许他人远程协助」(与主窗口开关同步)和「退出」。
- GNOME 通过 AppIndicator 显示(Ubuntu 自带扩展;运行时加载 libayatana-appindicator,无需额外编译依赖);Xfce / Cinnamon / KDE 等使用系统托盘。
- 「退出」会停止 `uuremote-native.service`(不会被自动重启);之后从应用菜单「UU 远程 Linux 主机」打开,会重新启动服务并显示主窗口。
✅ Ubuntu 24.04 GNOME(X11)实机验收:图标、菜单、开关同步、退出、从应用菜单重新启动。⚠️ Xfce 等系统托盘路径尚未实机验收。
Tray icon with open / allow / quit; Quit stops the user service and the launcher restarts it. Verified on Ubuntu GNOME.
### 改进 / Improved
- `uuremote-server show`:主机没在运行时,优先启动已安装的 `uuremote-native.service`,而不是另起一个不受管理的进程。
- 修复启动时的一个时序问题:先注册窗口唤起信号再写入 PID 文件,避免过早的 `show` 意外结束主机进程。
### 下载 / Download
`uu-remote-linux-v0.1.13-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+13
View File
@@ -0,0 +1,13 @@
修复手机切回终端时黑屏。/ Fixes a black terminal after returning to it on mobile.
### 修复 / Fixed
- **终端历史输出较多时,重连后黑屏**(#14,感谢 @cnetst):手机客户端切到后台再回到同一个终端时,如果之前积累的输出较多,
会整屏黑屏,连新建终端也没有输出,只能完全退出 App 重连。原因是重连时把最多 256 KB 的历史输出作为一条消息发送,
超过了 WebRTC 数据通道单条消息 64 KB 的上限,发送失败后整个终端连接卡住。现在历史输出按 16 KB 分段发送,顺序不变。
✅ 已用官方手机客户端在 3090 实机验收。
Terminal scrollback is replayed in 16 KiB chunks instead of one message over the 64 KiB SCTP limit. Verified on mobile.
### 下载 / Download
`uu-remote-linux-v0.1.14-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+20
View File
@@ -0,0 +1,20 @@
支持真彩(4:4:4)。/ True color (4:4:4) support.
### 新功能 / Added
- **真彩(4:4:4)**:在客户端画面菜单打开「真彩」后,主机改用 H.264 High 4:4:4 编码,彩色细字(红字、链接、
代码高亮)边缘不再发虚;关闭后切回 4:2:0。适用于默认的 FFmpeg 管线(NVENC 或 libx264);VAAPI 和
`--features inproc-av` 管线仍为 4:2:0,并如实告知客户端。
✅ 已用官方 Mac 客户端在 RTX 3090(NVENC)上实机验证:开关来回切换正常、画面正常解码。
When 真彩 is enabled the host encodes H.264 High 4:4:4 (default FFmpeg pipeline with NVENC or libx264); VAAPI and
inproc-av stay 4:2:0 and report it. Verified with the official Mac viewer on NVENC (toggling, decoding).
- 日志记录客户端声明的解码能力(编码类型、色度、最大分辨率、帧率,仅数字),便于排查画质问题。
The log now shows the viewer's declared decoder capabilities (numbers only).
### 说明 / Notes
- 4:4:4 码率会略高于 4:2:0;在缩放到较低分辨率时差别不明显,建议配合「跟随远端」分辨率使用。
4:4:4 costs some bitrate; the difference is hardly visible when the stream is scaled down.
### 下载 / Download
`uu-remote-linux-v0.1.15-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+22
View File
@@ -0,0 +1,22 @@
支持 H.265(HEVC)。/ H.265 (HEVC) support.
### 新功能 / Added
- **H.265**:控制端和主机都支持时自动使用 H.265,同等画质下码率更低。与官方主机一致,由主机按双方能力选择编码
(官方 Mac 客户端没有手动切换编码的菜单)。连接时主机会告知客户端自己能编哪些格式(CodecNegotiation)。
支持 `hevc_nvenc`、`libx265` 和显式 VAAPI 的 `hevc_vaapi`;硬件失败先回落软件 HEVC,再回落 H.264。
需要时用 `UU_VIDEO_CODEC=h264` 强制使用 H.264。
- 真彩(4:4:4)同样适用于 H.265(hevc_nvenc `rext` / libx265 `main444-8`)。
✅ 已用官方 Mac 客户端在 RTX 3090(hevc_nvenc,含 4:4:4)实机验收:画面正常、重连正常、无丢帧。
⚠️ libx265、hevc_vaapi、Windows/手机客户端尚未实机验证;如有问题请设置 `UU_VIDEO_CODEC=h264` 并提 issue。
The host now uses H.265 when both sides support it (the host picks the codec, like the official host), announces
its encoders with CodecNegotiation, and falls back to software HEVC, then H.264. `UU_VIDEO_CODEC=h264` forces H.264.
Verified with the official Mac viewer on NVENC including 4:4:4; other encoders and viewers are unverified.
### 修复 / Fixed
- 日志新增客户端 offer 中的编码列表与最终协商结果(仅编码名),便于排查画面问题。
The log shows offered and negotiated video codecs (names only).
### 下载 / Download
`uu-remote-linux-v0.1.16-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+24
View File
@@ -0,0 +1,24 @@
修复终端接回错位、低码率 CPU 编码灰屏、niri 无法建流。/ Terminal reattach, low-bitrate CPU encoding and niri fixes.
### 修复 / Fixed
- **终端接回后全屏程序画面错位**(#17):接回已有会话时,主机先把终端行数临时减一、稍后恢复,触发 tmux、vim、
Claude Code 等全屏程序整屏重画。
⚠️ 已通过单元测试;尚未用 Android 客户端实机验证,如仍错位请在 #17 反馈。
Reattaching a terminal session now nudges the PTY size so full-screen programs repaint. Unit-tested; not yet
verified with the Android viewer.
- **低码率档周期性灰屏**(#15,感谢 @mengdehong):外部 FFmpeg 的 CPU 编码(libx264,以及同样处理的 libx265)在
码率档(原画 / 超清 / 高清 / 自定义)改用平均码率(ABR),避免 4K 低码率下关键帧耗尽缓冲而发灰;「自动」档仍用 CRF。
libx264 修复由贡献者在 Android 客户端实测;libx265 只验证了参数可用。
CPU encoding uses ABR for bitrate presets (CRF for Auto), fixing gray keyframes at 4K and low bitrates.
- **niri 等合成器无法建流**(#15):原生 Wayland 后端在合成器没有 `wlr-output-power-management` 时跳过唤醒屏幕,
不再中断采集。Capture continues when the compositor lacks output power management.
### 文档 / Docs
- Wayland 会话的 systemd 单元写法、uinput 模块开机加载(#16,感谢 @meotao 的实机测量与建议);未编入 `wayland`
特性时的报错改为明确提示重新编译。
Wayland systemd unit, uinput autoload, and a clearer error when the binary lacks the `wayland` feature.
### 下载 / Download
`uu-remote-linux-v0.1.17-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+24
View File
@@ -0,0 +1,24 @@
修复分辨率变化后无法出画面;Wayland 采集提速;终端接回修复。/ Resolution change fix, faster Wayland capture, terminal fixes.
### 修复 / Fixed
- **服务运行中桌面分辨率变化后,新连接一直停在「正在传输画面」**(#18,感谢 @liangjiahui01):每次建立桌面连接前重新读取
X11 桌面尺寸,变化时重建截屏;截屏尺寸不符时报明确错误,不再误报「HEVC unavailable」。
已在隔离 Xvfb 上用 `xrandr` 改尺寸验证;xrdp 场景未实测。连接期间改分辨率暂不支持(需重连)。
X11 now re-reads the desktop size before each connection, so a RandR/xrdp resize no longer stalls new connections.
- **终端接回**(#17 后续):修复延迟恢复尺寸可能覆盖新尺寸的竞态,以及接回时同一段输出既在回放里又被实时重发的问题。
Fixed a size-restore race and duplicated output on terminal reattach.
### 改进 / Improved
- **原生 Wayland 采集**(#16,感谢 @meotao 的实机测量):采集进程直接写入 FFmpeg,不再经主进程中转 4K 帧;采集与写管道
双线程交替,管道缓冲扩大;SHM 截屏缓冲复用,不再每帧分配。
⚠️ 只做了编译和单元测试,没有在真机上测到提速效果;请求高帧率时的实际帧率欢迎在 #16 反馈。
Native Wayland: capture pipes straight into FFmpeg, overlaps capture with writes and reuses SHM buffers. Not yet
measured on real hardware.
- 视频统计日志新增本窗口的 `window_s`、`window_sent`、`window_skipped`(原 `sent`/`skipped` 仍为累计值)。
The latest-frame stats line adds per-window counts.
### 下载 / Download
`uu-remote-linux-v0.1.18-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
Wayland 后端需自行用 `--features wayland` 编译。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+14
View File
@@ -0,0 +1,14 @@
信令断线检测:主机休眠恢复或网络悄悄断开后自动重新上线。/ Detect dead signaling connections and reconnect.
### 修复 / Fixed
- **主机显示在线但控制端看不到、只能手动重启服务**(#19,感谢 @Keinns):之前只有收到关闭帧或读错误才判定信令断开;
系统休眠恢复、NAT 超时等造成的半开连接会让主机一直以为自己在线。现在按 Engine.IO 协议,超过服务器下发的
`pingInterval + pingTimeout`(目前约 33 秒)没收到心跳就判定断线,走已有的自动重连。日志:`Signaling heartbeat lost`。
已用模拟的「不发心跳」信令服务器单元测试验证;真实休眠恢复场景未实测。
Signaling now treats a connection without pings for pingInterval + pingTimeout as dead and reconnects.
Unit-tested against a silent mock server; real suspend/resume not yet measured.
### 下载 / Download
`uu-remote-linux-v0.1.19-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+16
View File
@@ -0,0 +1,16 @@
登录诊断与系统证书支持。/ Login diagnostics and system certificate support.
### 修复 / Fixed
- **企业代理 / 自定义 CA 环境下登录失败**(#21,感谢 @liuji10000qian-svg):除内置根证书外,现在也信任系统证书库,
并遵循 `SSL_CERT_FILE` / `SSL_CERT_DIR`。会做 TLS 拦截的代理(使用自签 CA)此前会导致设备注册请求失败。
Trust the system certificate store and honor `SSL_CERT_FILE` / `SSL_CERT_DIR` in addition to the bundled roots.
- **网络请求失败时只显示「error sending request」**:错误信息现在包含底层原因链(证书、代理、DNS、超时等),
并去掉 URL 中的用户名密码(如代理凭据)和查询参数。
Transport errors now include their cause chain, with URL credentials and query strings removed.
⚠️ 自定义 CA / 代理环境未实测,请在 #21 反馈新版本的错误信息。
### 下载 / Download
`uu-remote-linux-v0.1.20-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+17
View File
@@ -0,0 +1,17 @@
视频管线减少重复分配和过量编码。/ Fewer allocations and less wasted encoding in the video pipeline.
### 改进 / Improved(#20,感谢 @mengdehong)
- 采集到发送之间最多 4 帧在途:网络发不出去时采集端自动放慢,不再白白编码无法送出的帧。
贡献者实验(发送受阻场景):编码 CPU 耗时降低约 88%。
- 原生 Wayland 可选的 dma-buf 路径、Portal / KMS 路径复用采集缓冲;Portal 同一帧不重复复制。
- X11 进程内编码(`inproc-av`,需自行编译)缓存静态画面的采集与转色结果:静态画面每帧 CPU 降低约 40%。
- 原生 Wayland 关闭时完整释放协议对象。
⚠️ 以上性能数据来自贡献者在隔离 headless Sway / Xvfb 中的合成画面实验,未在维护者的真机上复测。
预编译的 X11 包默认走外部 FFmpeg 管线,受影响的主要是 Wayland / Portal / KMS / `inproc-av` 自编译用户。
Performance figures come from the contributor's headless Sway/Xvfb experiments; not re-measured on maintainer hardware.
### 下载 / Download
`uu-remote-linux-v0.1.21-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+15
View File
@@ -0,0 +1,15 @@
v0.1.21 视频管线改动的两处修正。/ Two follow-up fixes to the v0.1.21 video pipeline changes.
### 修复 / Fixed
- 原生 Wayland 的 dma-buf 映射失败、回退到 SHM 时,已缓存的 dma-buf 缓冲没有释放(最多两个槽位,到会话结束才回收)。
Release cached dma-buf buffers when capture falls back to SHM.
- 原生 Wayland 在发送受阻(背压)解除后的第一帧可能是受阻前预取的旧画面;现在等待超过两个帧间隔时会重新采集。
After a backpressure stall the first native Wayland frame is captured fresh instead of reusing the stale prefetch.
两处均由代码审查发现,只做了编译和单元测试,未在真机复现。
Both found in review; compile- and unit-tested only.
### 下载 / Download
`uu-remote-linux-v0.1.22-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+14
View File
@@ -0,0 +1,14 @@
测量工具:`desktop-video-probe` 可选码率档。/ Probe bitrate presets.
### 新增 / Added
- `desktop-video-probe` 支持 `UU_PROBE_QUALITY=1..5`,对应控制端画质档(1 极速 1M、2 流畅 2M、3 高清 8M、4 蓝光 20M、5 自动)。
码率档走平均码率(ABR)路径,便于不用控制端复测 #15 的灰屏修复(#9 的建议)。
`desktop-video-probe` accepts `UU_PROBE_QUALITY=1..5` (viewer bitrate presets; 5 = Auto/CRF).
### 其他 / Other
- 修复一个在多网卡机器上偶发超时的编码协商单元测试。
### 下载 / Download
`uu-remote-linux-v0.1.23-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+17
View File
@@ -0,0 +1,17 @@
支持 Ubuntu 22.04 自带的 FFmpeg 4.4。/ Works with Ubuntu 22.04's FFmpeg 4.4.
### 改进 / Improved
- **自动适配 FFmpeg 版本**(#22,感谢 @Guancheng-Li):启动编码前检测所用 FFmpeg 支持的参数。FFmpeg 5 及以上继续使用
`-fps_mode vfr -enc_time_base 1/<fps>`;FFmpeg 4.x(如 Ubuntu 22.04)改用 `-vsync 0`。检测失败时保持新版参数。
FFmpeg 4.x 下采集卡顿后的补帧会全部编码(新版会丢弃),属于已知降级。贡献者已在 FFmpeg 4.4.2 上验证。
The host probes the FFmpeg it uses and falls back to `-vsync 0` only on FFmpeg 4.x; probe failures keep the modern arguments.
### 其他 / Other
- 新增几条只记结构的诊断日志(收到的 SOAC offer 结构、SDP 段落属性名、ice_id 是否一致),用于排查与官方控制端的互通,
不记录 SDP 内容或任何 ID。
Structure-only diagnostics for incoming offers (no SDP contents or ids).
### 下载 / Download
`uu-remote-linux-v0.1.24-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+18
View File
@@ -0,0 +1,18 @@
GNOME Wayland 缩放下的鼠标修复。/ Pointer fix for scaled GNOME Wayland desktops.
### 修复 / Fixed
- **GNOME Wayland 开启桌面缩放后,远程鼠标只能控制左上部分**(#23、#24,感谢 @wanglz111):Portal 后端在同一授权会话里
改用 EIS 输入,按 `mapping_id` 匹配屏幕区域,并按实际帧尺寸映射坐标;缺少映射信息或 Portal 不支持 EIS 时保留原路径。
日志不记录输入内容。X11、原生 wlroots 和 uinput 路径不变。
⚠️ 贡献者在此前的迭代上实测过 Ubuntu 26.04 / GNOME 50、单屏 4K、200% 缩放;最终提交、其他缩放比例、锁屏恢复、
断线重连和多屏尚未实机验证。
Portal input uses EIS regions mapped to the captured frame; the final commit and other scales are not yet hardware-verified.
### 构建 / Build
- `--features wayland,portal` 现在需要 `libei >= 1.1` 开发包(Debian/Ubuntu:`libei-dev`)。预编译的 X11 包不受影响。
Portal builds now require libei >= 1.1.
### 下载 / Download
`uu-remote-linux-v0.1.25-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+21
View File
@@ -0,0 +1,21 @@
大目录文件传输修复、上报实际刷新率、NVENC 省掉 CPU 颜色转换。/ Large-folder file transfer fix, real refresh rate, NVENC without CPU color conversion.
### 修复 / Fixed
- **文件传输打开文件很多的目录时一直转圈**:目录回复同时带了未压缩和压缩两份列表,几千个文件时超过数据通道单条消息上限,
回复被丢弃。现在压缩列表始终发送,未压缩副本只在放得下时附带;同时把 SCTP 发送上限从 64 KB 提到 256 KB
(与官方控制端在 SDP 中声明的一致)。已在 5600 个文件的目录上实测。
Large directory listings no longer exceed the data-channel message limit (verified with 5600 entries).
### 改进 / Improved
- **上报显示器实际刷新率**(#16):`display_info`、能力通知和编解码协商原先固定报 60,现在取当前显示模式
(Hyprland 用 `hyprctl`,X11 用 `xrandr`;取不到时仍为 60,`UU_DISPLAY_FPS` 可手动指定)。
The host reports the monitor's refresh rate instead of a fixed 60 (`UU_DISPLAY_FPS` overrides).
- **NVENC 直接接收 BGRA/BGR0**(#16):4:2:0 且不缩放时,颜色转换交给 GPU,省掉 FFmpeg 子进程里的 CPU 转换;
色彩标记与原来一致(BT.601 有限范围)。`UU_NVENC_RGB=0` 可恢复原路径。
⚠️ 已在 X11 + RTX 3090 上实测画面与颜色;Hyprland 4K 高帧率下的 CPU/帧率收益尚未实测。
NVENC converts RGB on the GPU; verified on X11, the Hyprland 4K gain is not yet measured.
### 下载 / Download
`uu-remote-linux-v0.1.26-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+16
View File
@@ -0,0 +1,16 @@
GNOME Wayland 连接中切换缩放后的停帧修复。/ Recover from live scaling changes on GNOME Wayland.
### 修复 / Fixed
- **GNOME Wayland 连接中切换显示缩放后画面持续冻结**(#25、#26,感谢 @wanglz111):已推流的 Portal 流若「暂停且没有有效格式」
持续 5 秒,复用现有会话恢复;恢复时允许逻辑尺寸变化,仍校验采集帧尺寸。启动阶段和静态画面不会触发这个超时,
等待授权与 D-Bus 的过程不再占用输入锁。
⚠️ 贡献者实测:Ubuntu 26.04 + GNOME Wayland、单屏 3840×2160,连接中 200%→166.67%→200% 恢复后画面和全屏鼠标正常;
**切换时会短暂停顿、黑屏后恢复,并非无缝;5 秒是判定阈值,不是总恢复时间**。物理分辨率变化、多屏、锁屏恢复未实机验证。
Portal recovers after a live scale change (brief freeze/black, not seamless); physical resolution changes remain unsupported.
- Portal 失效检查出错时只记日志,不再让主机主循环退出。/ A failed Portal staleness check no longer ends the host loop.
### 下载 / Download
`uu-remote-linux-v0.1.27-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
GNOME/KDE Wayland 的 Portal 后端需自行用 `--features wayland,portal` 构建(需要 libei-dev ≥ 1.1)。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+16
View File
@@ -0,0 +1,16 @@
编码探针支持固定输入,测试更稳定。/ Fixed-input encoder probe and deterministic WebRTC tests.
### 新增 / Added
- **`desktop-video-probe` 支持固定输入**(#9,感谢 @hiclaw 的建议):设置 `UU_PROBE_INPUT=<视频或图片序列>`(如 `clip.y4m`、
`clip.mp4`、`frames/%05d.png`)时不采集桌面,而是用与生产会话相同的 FFmpeg 编码参数(编码器、码率档、GOP、色度、缩放)
编码该输入,消除桌面内容对测量的影响;`UU_PROBE_LOOP=1` 循环输入直到 `UU_PROBE_FRAMES`。输入打不开或帧数不足时非零退出。
正常会话路径不变。已用 Y4M、MP4、PNG 序列验证,重复运行输出逐字节一致。⚠️ NVENC/VAAPI 下的固定输入未实机验证。
`desktop-video-probe` can encode a fixed input (`UU_PROBE_INPUT`) with the production encoder arguments.
### 内部 / Internal
- 本地 WebRTC 单测改用虚拟网络,修复全量并行测试时偶发的 30 秒超时。/ Local WebRTC tests run on a virtual network.
### 下载 / Download
`uu-remote-linux-v0.1.28-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+22
View File
@@ -0,0 +1,22 @@
GNOME Wayland 锁屏时也能连接并自动解锁。/ Connect and auto unlock while GNOME Wayland is locked.
### 修复 / Fixed
- **GNOME Wayland(Portal)锁屏冷连接**(#28,感谢 @wanglz111,#30):服务在已解锁时启动、随后锁屏,再从客户端连接时,
以前因为 Portal 无法创建而整个连接被丢弃,无法远程自动解锁。现在先建立控制通道、上报锁屏状态并处理「自动解锁被控端」,
Portal 就绪后再恢复画面、输入和剪贴板;锁屏期间不发画面、不注入桌面输入,并唤醒显示器(不会解锁)。
官方 **Mac 客户端**已实机验证(锁屏 → 连接 → 自动解锁 → 画面和输入恢复)。
- **iPhone 锁屏冷连接自动解锁**(#29,感谢 @wanglz111,#34):iPhone 4.42.0 要先收到视频帧才会发起自动解锁。Portal 等待就绪且
确认锁屏时,主机每秒在已协商的视频轨上发一帧生成的黑画面(需 FFmpeg 带 libx264/libx265),解锁后由真实画面接续。
贡献者用同一二进制做了 开→关→开 对照,iPhone 4.42.0 + H.265 实机通过;⚠️ H.264 手机实机、其他客户端版本、慢速中继未验收。
黑帧不是真实锁屏画面,锁屏界面不能操作键鼠;未开启「自动解锁被控端」时无法远程解锁(控制中心 → 安全 → 自动解锁被控端)。
Locked cold connections on GNOME Wayland now establish CONTROL first and support auto unlock (Mac verified); black video
while locked lets iPhone 4.42.0 start auto unlock (H.265 verified by the contributor).
- 断开事件只关闭对应 `ice_id` 的连接,不再误关同时打开的文件传输、终端、端口映射连接。
Releases close only the matching peer; tool peers stay open.
- X11 主机回归:控制通道处理改动后,已在 X11 + NVENC 主机上实测连接、Echo、锁屏状态上报和画面;画质切换由自动化测试覆盖。
X11 host re-checked live (connection, Echo, lock state, video).
### 下载 / Download
`uu-remote-linux-v0.1.29-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+13
View File
@@ -0,0 +1,13 @@
修复未登录(访客)远程协助时 X11 没有画面。/ Fix missing video during guest remote assistance on X11.
### 修复 / Fixed
- **X11 访客远程协助无画面**:未登录账号、以访客身份开启远程协助时,设备初始化过程会临时打开再关闭一次屏幕采集,
把正在使用的 X11 连接一起关掉,日志出现 `Live desktop stream stopped: X11 display wake failed`,控制端连上后一直黑屏。
现在 X11 连接按引用计数管理,只有最后一个采集对象释放时才关闭;X11 分辨率变化后重建采集也不再有同样的隐患。
已在虚拟机(Xvfb 虚拟桌面)上实测:访客协助加入、画面、鼠标移动/点击、键盘均正常。⚠️ 未用官方客户端在真机上复测。
Guest assistance on X11 no longer loses the display connection; video, mouse and keyboard verified in a VM.
### 下载 / Download
`uu-remote-linux-v0.1.30-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+18
View File
@@ -0,0 +1,18 @@
GNOME 修改缩放后画面恢复更快。/ Faster capture recovery after GNOME display changes.
### 修复 / Fixed
- **GNOME Wayland 改缩放后黑屏/停顿时间缩短**(#27,感谢 @wanglz111,#35):以前切换缩放(如 200% ↔ 166.67%)后,Portal 采集要等
5 秒兜底才恢复。现在监听 Mutter 的显示配置变化通知,采集持续「暂停且无有效格式」超过 200 毫秒宽限即提前进入原有恢复流程;
正常重协商会取消提前判定,连续切换只恢复一次,5 秒兜底保留。贡献者在 Ubuntu 26.04、GNOME 50、单屏 3840×2160 上实测:
通知到会话恢复从约 6.2 秒降到约 1.4 秒。⚠️ 200 毫秒是在该机器上测得的初始值;官方客户端侧的画面恢复耗时、多屏、
物理分辨率变化与旋转未验收。X11 与其他桌面不受影响。
On GNOME, Mutter `MonitorsChanged` lets a stalled Portal capture recover after a 200 ms grace instead of the 5 s fallback
(host-side recovery about 6.2 s → 1.4 s on the contributor's 4K display).
### 内部 / Internal
- CONTROL 生命周期单测改用虚拟网络,不再因本机网卡环境偶发超时。/ CONTROL lifecycle tests run on the virtual network.
### 下载 / Download
`uu-remote-linux-v0.1.31-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+12 -6
View File
@@ -1,4 +1,4 @@
//! Opt-in guest assistance. Protocol sources: docs/validation/remote-assist-evidence.md.
//! Opt-in guest assistance. Protocol sources: AGENTS.md "Remote assistance".
//! Secret values never implement Display/Debug or enter diagnostics.
use crate::auth::{Credentials, RoomData, UUAuthClient};
use serde_json::{json, Value};
@@ -68,7 +68,7 @@ fn valid_custom(code: &str) -> bool {
&& code.bytes().any(|b| b.is_ascii_alphabetic()) && code.bytes().any(|b| b.is_ascii_digit())
}
pub enum Command { Allow, Deny, Reset, Stop, Verify(Verify), Refresh(Refresh), Custom(String), Logout }
pub enum Command { Allow, Deny, Reset, Stop, Verify(Verify), Refresh(Refresh), Custom(String), Logout, Quit }
#[cfg(uu_gtk)]
extern "C" {
@@ -156,6 +156,7 @@ impl LocalUi {
6 => if let Some(r) = Refresh::from_index(index) { return Command::Refresh(r) },
7 => return Command::Custom(text),
8 => return Command::Logout,
9 => return Command::Quit,
_ => tokio::time::sleep(std::time::Duration::from_millis(150)).await,
}
},
@@ -477,10 +478,15 @@ mod tests {
let ui = LocalUi::Tty(tokio::io::unix::AsyncFd::new(slave).unwrap());
let mut master = std::fs::File::from(pair.master);
ui.show("fixture-123", "FIXTURE8", false, Verify::Temporary, Refresh::Session, false).unwrap();
let mut bytes = [0u8; 1024];
let n = master.read(&mut bytes).unwrap();
let text = std::str::from_utf8(&bytes[..n]).unwrap();
assert!(text.contains("fixture-123") && text.contains("FIXTURE8") && text.contains("false"));
// A PTY may deliver the banner in several chunks: read until complete (bounded).
let mut text = String::new();
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(3);
let complete = |t: &str| t.contains("fixture-123") && t.contains("FIXTURE8") && t.contains("false") && t.contains("off=");
while !complete(&text) && std::time::Instant::now() < deadline {
let mut bytes = [0u8; 1024];
match master.read(&mut bytes) { Ok(n) if n > 0 => text.push_str(&String::from_utf8_lossy(&bytes[..n])), _ => break }
}
assert!(complete(&text));
for (input, expected) in [(b"unknown\nallow\n".as_slice(), 0), (b"reset\n".as_slice(), 1), (b"off\n".as_slice(), 2)] {
master.write_all(input).unwrap();
let command = tokio::time::timeout(std::time::Duration::from_secs(2), ui.command()).await.unwrap();
+38 -2
View File
@@ -91,6 +91,36 @@ pub struct UUAuthClient {
guest_id: Option<String>,
}
/// Transport failures name only the outer request; the cause (certificate, proxy, DNS,
/// timeout) lives in the source chain. URL user-info (proxy credentials) and query
/// strings are removed before the chain is shown.
pub fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
let mut parts = vec![error.to_string()];
let mut source = error.source();
while let Some(cause) = source {
let text = cause.to_string();
if parts.last() != Some(&text) { parts.push(text); }
source = cause.source();
if parts.len() >= 8 { break; }
}
redact_urls(&parts.join(": "))
}
fn redact_urls(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(i) = rest.find("://") {
let (head, tail) = rest.split_at(i + 3);
out.push_str(head);
let end = tail.find(|c: char| c.is_whitespace() || c == ')' || c == '"' || c == '\'').unwrap_or(tail.len());
let url = &tail[..end];
let url = url.rsplit_once('@').map_or(url, |(_, host)| host);
out.push_str(url.split(['?', '#']).next().unwrap_or(""));
rest = &tail[end..];
}
out.push_str(rest);
out
}
impl UUAuthClient {
pub fn new(creds: Credentials) -> Self {
let http = reqwest::Client::builder()
@@ -200,7 +230,7 @@ impl UUAuthClient {
let body_str = serde_json::to_string(body).map_err(|e| e.to_string())?;
let resp = self.http.post(format!("{}{}", BASE_URL, path))
.headers(self.build_headers("POST", path, &body_str)).body(body_str)
.send().await.map_err(|e| format!("HTTP post {path} failed: {e}"))?;
.send().await.map_err(|e| format!("HTTP post {path} failed: {}", transport_error(&e)))?;
let status = resp.status().as_u16();
let value = resp.json().await.unwrap_or(serde_json::Value::Null);
Ok((status, value))
@@ -219,7 +249,7 @@ impl UUAuthClient {
}
async fn assist_send(&self, request: reqwest::Request) -> Result<(u16, serde_json::Value), String> {
let response = self.http.execute(request)
.await.map_err(|_| "assist request transport failed")?;
.await.map_err(|e| format!("assist request transport failed: {}", transport_error(&e)))?;
let status = response.status().as_u16();
let bytes = response.bytes().await.map_err(|_| "assist response read failed")?;
if bytes.len() > 65536 { return Err("assist response too large".into()); }
@@ -310,4 +340,10 @@ mod guest_tests {
let mut without_guest = params; without_guest.remove("x-param-guest-id");
assert_ne!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", &without_guest, ""));
}
#[test]
fn transport_error_redacts_proxy_credentials_and_queries() {
let text = super::redact_urls("proxy http://user:[email protected]:3128 failed for (https://api.example/p?token=abc)");
assert_eq!(text, "proxy http://proxy.example:3128 failed for (https://api.example/p)");
}
}
+18 -6
View File
@@ -27,17 +27,20 @@ pub fn h264_device_capability(
fps: i32,
codec_impl: i32,
) -> Value {
device_capability(client_id, ice_id, display_id, fps, codec_impl, false)
}
pub fn device_capability(client_id: &str, ice_id: &str, display_id: i32, fps: i32, codec_impl: i32, hevc: bool) -> Value {
let mut codecs = vec![json!({"video_codec": VIDEO_CODEC_H264, "chroma_sampling": 1,
"codec_impl": codec_impl, "bit_depth": 8})];
// Official VideoCodecCapability::SerializeJson uses the same fields for CodecType H265=2.
if hevc { codecs.push(json!({"video_codec": 2, "chroma_sampling": 1, "codec_impl": codec_impl, "bit_depth": 8})); }
json!({
"client_id": client_id,
"type": "device_capability",
"data": {
"display_info": [{"id": display_id, "fps": fps, "type": 0, "hdr": 0}],
"video_codec_capability": [{
"video_codec": VIDEO_CODEC_H264,
"chroma_sampling": 1,
"codec_impl": codec_impl,
"bit_depth": 8
}],
"video_codec_capability": codecs,
"ice_id": ice_id
}
})
@@ -47,6 +50,15 @@ pub fn h264_device_capability(
mod tests {
use super::*;
#[test]
fn hevc_capability_uses_official_codec_enum_and_420_only() {
let payload = device_capability("client-example", "ice-example", 0, 60, 5, true);
assert_eq!(payload["data"]["video_codec_capability"], json!([
{"video_codec":1,"chroma_sampling":1,"codec_impl":5,"bit_depth":8},
{"video_codec":2,"chroma_sampling":1,"codec_impl":5,"bit_depth":8}]));
assert_eq!(device_capability("c", "i", 0, 60, 5, false)["data"]["video_codec_capability"].as_array().unwrap().len(), 1);
}
#[test]
fn ice_finished_keeps_string_identifiers_and_exact_fields() {
let payload = ice_finished("ice-example", "control-example");
+36 -10
View File
@@ -6,6 +6,7 @@ use std::sync::Mutex;
extern "C" {
fn x11_init(display_name: *const libc::c_char) -> libc::c_int;
fn x11_get_dimensions(out_w: *mut libc::c_int, out_h: *mut libc::c_int) -> libc::c_int;
fn x11_refresh_geometry() -> libc::c_int;
fn x11_capture_frame(
out_buffer: *mut libc::c_uchar,
out_w: *mut libc::c_int,
@@ -37,10 +38,22 @@ unsafe fn x11_capture_frame(
}
#[cfg(not(target_os = "linux"))]
unsafe fn x11_close() {}
#[cfg(not(target_os = "linux"))]
unsafe fn x11_refresh_geometry() -> libc::c_int { 0 }
#[cfg(not(target_os = "linux"))]
unsafe fn x11_display_name() -> *const libc::c_char { b":0\0".as_ptr().cast() }
/// The X11 root can be resized while the host runs (RandR, xrdp reconnects); the
/// geometry read at startup then makes x11grab reject every new connection.
pub fn x11_resized() -> bool {
match unsafe { x11_refresh_geometry() } {
1 => true,
0 => false,
code => { log::warn!("X11 geometry query failed with code {code}"); false }
}
}
pub struct ScreenCapture {
pub backend: crate::desktop_backend::DesktopBackend,
pub init_error: Option<String>,
@@ -53,24 +66,27 @@ pub struct ScreenCapture {
}
impl ScreenCapture {
pub fn open_portal(cancelled: &std::sync::atomic::AtomicBool) -> Result<Self, crate::portal::PortalError> {
let session = std::sync::Arc::new(crate::portal::PortalSession::open_cancellable(cancelled)?);
Ok(Self { backend: crate::desktop_backend::DesktopBackend::Portal, init_error: None,
display: "wayland:portal".into(), width: session.width, height: session.height,
portal: Some(session), kms: None, buffer: Mutex::new(Vec::new()) })
}
pub fn new() -> Result<Self, String> {
let backend=crate::desktop_backend::detect()?;
if backend == crate::desktop_backend::DesktopBackend::Kms {
let kms=crate::kms::Capture::open()?;
let (width,height)=(kms.width,kms.height);
return Ok(Self { backend,init_error:None,portal:None,display:"kms:linear".into(),width,height,
buffer:Mutex::new(vec![0;width as usize*height as usize*4]),kms:Some(kms) });
buffer:Mutex::new(Vec::new()),kms:Some(kms) });
}
if backend == crate::desktop_backend::DesktopBackend::Portal {
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
let (width,height)=(session.width,session.height);
return Ok(Self { backend,init_error:None,portal:Some(session),kms:None,display:"wayland:portal".into(),width,height,
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
return Self::open_portal(&std::sync::atomic::AtomicBool::new(false)).map_err(|error| error.to_string());
}
if backend == crate::desktop_backend::DesktopBackend::Wayland {
let (width,height)=crate::wayland::init()?;
return Ok(Self { backend, init_error:None, portal:None, kms:None, display:"wayland:native".into(), width,height,
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
buffer:Mutex::new(Vec::new()) });
}
let ret = unsafe { x11_init(std::ptr::null()) };
if ret != 0 {
@@ -85,14 +101,14 @@ impl ScreenCapture {
return Err(format!("Invalid screen dimensions: {}x{}", w, h));
}
let buf_size = (w * h * 4) as usize;
let buffer = Mutex::new(vec![0u8; buf_size]);
let buffer = Mutex::new(Vec::new());
log::info!("ScreenCapture initialized: {}x{}", w, h);
let display_ptr = unsafe { x11_display_name() };
if display_ptr.is_null() { return Err("Capture display name unavailable".into()); }
let display = unsafe { std::ffi::CStr::from_ptr(display_ptr) }.to_string_lossy().into_owned();
log::info!("Selected capture display: {}", display);
X11_USERS.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
Ok(Self {
backend,
init_error:None,
@@ -115,6 +131,7 @@ impl ScreenCapture {
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
if let Some(error)=&self.init_error { return Err(error.clone()); }
let mut buf = self.buffer.lock().unwrap();
buf.resize(self.width as usize * self.height as usize * 4, 0);
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
@@ -126,6 +143,7 @@ impl ScreenCapture {
let ret = unsafe { x11_capture_frame(buf.as_mut_ptr(), &mut w, &mut h) };
if ret != 0 {
if ret == -4 { return Err(format!("desktop resized to {w}x{h}; reconnect to capture the new size")); }
return Err(format!("x11_capture_frame failed with code {}", ret));
}
@@ -133,16 +151,24 @@ impl ScreenCapture {
}
}
/// The X11 engine keeps one process-wide display connection shared by every ScreenCapture.
/// Only the last one may close it: a short-lived capture (device init's screen size during guest
/// assistance, or the replaced capture after a resize) used to close it under the live stream.
static X11_USERS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
impl Drop for ScreenCapture {
fn drop(&mut self) {
if self.init_error.is_none() && self.backend==crate::desktop_backend::DesktopBackend::X11 { unsafe { x11_close() }; }
if self.init_error.is_none() && self.backend==crate::desktop_backend::DesktopBackend::X11
&& X11_USERS.fetch_sub(1, std::sync::atomic::Ordering::AcqRel) == 1 { unsafe { x11_close() }; }
}
}
/// Keep the display visible only while a peer is streaming; never unlock it.
pub fn keep_display_awake() -> Result<(), String> {
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Kms { return Ok(()); }
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal { return Ok(()); }
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal {
return crate::desktop_power::wake();
}
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
return crate::wayland::wake();
}
+243 -8
View File
@@ -19,24 +19,40 @@ pub const QUALITY_AUTO: u64 = 5;
pub const QUALITY_CUSTOM: u64 = 6;
/// FeatureFlag.ff_capture_setting we declare: fps, preset quality and custom bitrate.
/// (Chroma 4:4:4, DPI scaling and HDR are higher versions and are not implemented.)
/// Versions are cumulative (official FF_CaptureSetting enum: 3 CHROMA_SAMPLING, 4 CUSTOM_BITRATE,
/// 5 DPI_SCALE, 6 HDR), so 4 also tells the viewer we accept a chroma format (真彩).
pub const FEATURE_VERSION: u64 = 4;
/// Official GvPb_ChromaFormat values (CaptureSettingRequest field 7, CaptureSettingSync field 3).
pub const CHROMA_420: u64 = 1;
pub const CHROMA_444: u64 = 3;
const MIN_FPS: u32 = 5;
const MAX_FPS: u32 = 144;
/// Official GvPb_CodecType, CaptureSettingRequest.codec_type (field 13).
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum VideoCodec { #[default] H264, H265 }
impl VideoCodec {
pub fn wire(self) -> u64 { match self { Self::H264 => 1, Self::H265 => 2 } }
pub fn mime(self) -> &'static str { match self { Self::H264 => "video/H264", Self::H265 => "video/H265" } }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VideoSettings {
pub fps: u32,
pub codec: VideoCodec,
pub quality: u64,
/// Viewer max_custom_bitrate (only used for QUALITY_CUSTOM): Mbit/s, or kbit/s above 1000.
pub custom_mbps: u32,
/// Scaled stream size (其他分辨率); None streams the native desktop size.
pub output: Option<(u32, u32)>,
/// CHROMA_420 or CHROMA_444 (真彩); 4:4:4 only when the encoder pipeline supports it.
pub chroma: u64,
}
impl Default for VideoSettings {
fn default() -> Self { Self { fps: 60, quality: QUALITY_AUTO, custom_mbps: 0, output: None } }
fn default() -> Self { Self { codec: VideoCodec::H264, fps: 60, quality: QUALITY_AUTO, custom_mbps: 0, output: None, chroma: CHROMA_420 } }
}
/// Selectable smaller stream sizes with the desktop's aspect ratio (even dimensions).
@@ -95,6 +111,21 @@ fn fps_from_option(value: u64) -> Option<u64> {
/// Applies a capture-setting RPC to `settings` and returns the RpcResponse payload (Message 22),
/// or None when the RPC is not a capture/fps/quality setting.
pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32)) -> Option<Vec<u8>> {
handle_rpc_with_codecs(rpc, settings, native, true, false)
}
/// Initial codec once the m-line is negotiated: H.265 when both sides support it (the Mac viewer
/// lists H265 first in its offer), unless UU_VIDEO_CODEC=h264.
pub fn preferred_codec(h264: bool, h265: bool) -> VideoCodec {
let forced_h264 = std::env::var("UU_VIDEO_CODEC").is_ok_and(|v| v.eq_ignore_ascii_case("h264"));
if h265 && !(forced_h264 && h264) { VideoCodec::H265 } else { VideoCodec::H264 }
}
pub fn handle_rpc_with_codecs(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool) -> Option<Vec<u8>> {
apply_rpc(rpc, settings, native, h264, h265, crate::video::full_chroma_available())
}
fn apply_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool, full_chroma: bool) -> Option<Vec<u8>> {
let header = message(rpc, 1).unwrap_or(&[]);
let mut next = *settings.borrow();
if let Some(request) = message(rpc, 2) {
@@ -102,11 +133,22 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
// Numbers only: fps, quality, bitrate, resolution and flags carry no user content.
log::info!("Capture setting request: {:?}", fields.iter().filter_map(|f| match f.value {
WireValue::Varint(v) => Some((f.number, v)), _ => None }).collect::<Vec<_>>());
// codec_type 13 (1 H264, 2 H265): like the official host (negotiateCodec), the host picks the
// codec from both capability lists; the Mac viewer sends its current codec (1 while receiving
// H.264), so only an explicit H.265 request switches, never one absent from the m-line.
if number(&fields, 13) == Some(2) && h265 { next.codec = VideoCodec::H265; }
// Field 1 is the viewer's fps option index (1 = 30, 2 = 60, ...); 18 fpsCount is the frame rate.
if let Some(fps) = number(&fields, 18).filter(|v| *v > 0).or_else(|| number(&fields, 1).and_then(fps_from_option)) { next.fps = fps as u32; }
if let Some(quality) = number(&fields, 2).filter(|v| *v > 0) { next.quality = quality; }
if let Some(bitrate) = number(&fields, 8).filter(|v| *v > 0) { next.custom_mbps = bitrate as u32; }
// ChooseResolutionType 3 = follow remote (native); otherwise pixel size, else logical size.
// Field 7 chorma_format (sic): 3 = 4:4:4 when 真彩 is on, 1 = 4:2:0. The Sync reports what is encoded.
match number(&fields, 7) {
Some(CHROMA_444) if full_chroma => next.chroma = CHROMA_444,
Some(CHROMA_444) => { log::info!("Chroma 4:4:4 requested; this encoder pipeline only supports 4:2:0"); next.chroma = CHROMA_420; }
Some(CHROMA_420) => next.chroma = CHROMA_420,
_ => {}
}
// ChooseResolutionType 3 = follow remote (native); v4 uses pixels in both size pairs.
if number(&fields, 10) == Some(3) { next.output = None; }
else if let Some((w, h)) = number(&fields, 16).zip(number(&fields, 17)).or_else(|| number(&fields, 5).zip(number(&fields, 6))) {
next.output = clamp_output(native, w, h);
@@ -129,7 +171,7 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
}
next.fps = next.fps.clamp(MIN_FPS, MAX_FPS);
settings.send_if_modified(|current| if *current != next {
log::info!("Desktop video settings: fps={} quality={} max_kbps={:?} output={:?}", next.fps, next.quality, next.max_kbps(), next.output);
log::info!("Desktop video settings: fps={} quality={} max_kbps={:?} output={:?} chroma={}", next.fps, next.quality, next.max_kbps(), next.output, next.chroma);
*current = next; true
} else { false });
let mut out = Vec::new();
@@ -138,13 +180,15 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
Some(out)
}
/// Message 7 ScreenSources for the single desktop: native size, scaled choices, current size.
/// Message 7 ScreenSources in pixels for capture-setting v4: native, choices, current size.
pub fn screen_sources(seq: u64, timestamp: u64, native: (u32, u32), output: Option<(u32, u32)>, name: &str) -> Result<Vec<u8>, String> {
use crate::initialization::{Screen, WinRect};
let (w, h) = (native.0 as i32, native.1 as i32);
let scaled: Vec<(i32, i32)> = scaled_sizes(native).into_iter().map(|(w, h)| (w as i32, h as i32)).collect();
// DPI_SCALE starts at version 5; the observed v4 request uses fields 5/6 as pixels.
// Keep every advertised rectangle in that space; Portal/EIS maps input independently.
let resolution = WinRect { left: 0, top: 0, width: w, height: h, pixel_width: w, pixel_height: h };
let screens = [Screen { id: 0, fps: crate::video::FPS as i32, resolution, primary: true, dpr: 1.0,
let screens = [Screen { id: 0, fps: crate::video::display_fps() as i32, resolution, primary: true, dpr: 1.0,
display_name: name, video_track_index: 0, screen_type: 0, builtin_screen_type: 0,
scaled: &scaled, current: output.map(|(w, h)| (w as i32, h as i32)) }];
crate::initialization::encode_screen_sources(seq, timestamp, &screens, 0)
@@ -156,7 +200,7 @@ pub fn sync_payload(settings: &VideoSettings, native: (u32, u32)) -> Vec<u8> {
let mut out = Vec::new();
put_varint(1, u64::from(settings.fps), &mut out);
put_varint(2, settings.quality, &mut out);
put_varint(3, 1, &mut out); // ChromaFormat_420
put_varint(3, settings.chroma, &mut out);
if settings.custom_mbps > 0 { put_varint(4, u64::from(settings.custom_mbps), &mut out); }
put_varint(5, u64::from(size.0), &mut out);
put_varint(6, u64::from(size.1), &mut out);
@@ -165,6 +209,23 @@ pub fn sync_payload(settings: &VideoSettings, native: (u32, u32)) -> Vec<u8> {
out
}
/// Message 18 CodecNegotiation {1 encoder_cap_list: EncoderCap {1 capture_mode (1 DESKTOP), 2 fps,
/// 3 codec_type, 4 width, 5 height}} (official Windows host descriptor). The viewer's own list is
/// ConnectOptions field 4 decoder_cap_list; it only selects a codec the host announces here.
pub fn codec_negotiation(size: (u32, u32), fps: u32, hevc: bool) -> Vec<u8> {
let mut out = Vec::new();
for codec in [Some(VideoCodec::H264), hevc.then_some(VideoCodec::H265)].into_iter().flatten() {
let mut cap = Vec::new();
put_varint(1, 1, &mut cap);
put_varint(2, u64::from(fps), &mut cap);
put_varint(3, codec.wire(), &mut cap);
put_varint(4, u64::from(size.0), &mut cap);
put_varint(5, u64::from(size.1), &mut cap);
put_bytes(1, &cap, &mut out);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
@@ -187,7 +248,7 @@ mod tests {
put_varint(18, 30, &mut body);
let request = rpc(77, 2, &body);
let response = handle_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080)).unwrap();
assert_eq!(*rx.borrow(), VideoSettings { fps: 30, quality: QUALITY_GENERAL, custom_mbps: 0, output: None });
assert_eq!(*rx.borrow(), VideoSettings { codec: VideoCodec::H264, fps: 30, quality: QUALITY_GENERAL, custom_mbps: 0, output: None, chroma: CHROMA_420 });
assert_eq!(rx.borrow().max_kbps(), Some(2_000));
let fields = decode_wire(&response).unwrap();
assert_eq!(message(&fields, 1), Some(&[8u8, 77][..]));
@@ -208,6 +269,29 @@ mod tests {
assert!(handle_rpc(&decode_wire(&rpc(3, 9, &[])).unwrap(), &tx, (1920, 1080)).is_none());
}
#[test]
fn true_color_request_selects_444_only_when_the_encoder_supports_it() {
let (tx, rx) = watch::channel(VideoSettings::default());
// Observed Mac viewer request with 真彩 on: field 7 = 3.
let request = rpc(5, 2, &[8, 2, 16, 4, 56, 3, 144, 1, 60]);
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, false).unwrap();
assert_eq!(rx.borrow().chroma, CHROMA_420);
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
assert_eq!(rx.borrow().chroma, CHROMA_444);
assert_eq!(number(&decode_wire(&sync_payload(&rx.borrow(), (1920, 1080))).unwrap(), 3), Some(CHROMA_444));
apply_rpc(&decode_wire(&rpc(6, 2, &[56, 1])).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
assert_eq!(rx.borrow().chroma, CHROMA_420);
}
#[test]
fn codec_negotiation_lists_h264_then_optional_h265() {
assert_eq!(codec_negotiation((1920, 1080), 60, false), [10, 12, 8, 1, 16, 60, 24, 1, 32, 128, 15, 40, 184, 8]);
let both = codec_negotiation((1920, 1080), 60, true);
let caps: Vec<_> = decode_wire(&both).unwrap().iter().filter_map(|f| match f.value { WireValue::Bytes(b) => Some(b), _ => None })
.map(|b| number(&decode_wire(b).unwrap(), 3)).collect();
assert_eq!(caps, [Some(1), Some(2)]);
}
#[test]
fn resolution_requests_scale_below_native_only() {
let (tx, rx) = watch::channel(VideoSettings::default());
@@ -218,6 +302,133 @@ mod tests {
assert_eq!(scaled_sizes((1920, 1080)), vec![(1600, 900), (1280, 720), (960, 540), (640, 360)]);
}
#[test]
fn version_four_native_inventory_roundtrips_without_downscaling() {
assert_eq!(FEATURE_VERSION, 4);
let native = (3840, 2160);
let inventory = screen_sources(1, 2, native, None, "test").unwrap();
let envelope = decode_wire(&inventory).unwrap();
let sources = decode_wire(message(&envelope, 7).unwrap()).unwrap();
let display = decode_wire(message(&sources, 1).unwrap()).unwrap();
let rect = decode_wire(message(&display, 4).unwrap()).unwrap();
let mut body = Vec::new();
put_varint(5, number(&rect, 3).unwrap(), &mut body);
put_varint(6, number(&rect, 4).unwrap(), &mut body);
put_varint(2, QUALITY_BLURAY, &mut body);
let (tx, rx) = watch::channel(VideoSettings::default());
handle_rpc(&decode_wire(&rpc(77, 2, &body)).unwrap(), &tx, native).unwrap();
assert_eq!(rx.borrow().output, None, "native selection must preserve the full frame");
let sync = sync_payload(&rx.borrow(), native);
let fields = decode_wire(&sync).unwrap();
assert_eq!(number(&fields, 5), Some(3840));
assert_eq!(number(&fields, 6), Some(2160));
}
fn advertised_display(native: (u32, u32), output: Option<(u32, u32)>) -> Vec<u8> {
let inventory = screen_sources(1, 2, native, output, "test").unwrap();
let envelope = decode_wire(&inventory).unwrap();
let sources = decode_wire(message(&envelope, 7).unwrap()).unwrap();
message(&sources, 1).unwrap().to_vec()
}
#[test]
fn version_four_resolution_choices_and_settings_roundtrip_in_pixels() {
assert_eq!(FEATURE_VERSION, 4);
for native in [(1920, 1080), (2560, 1440), (3840, 2160), (5120, 2880),
(7680, 4320), (3440, 1440), (2160, 3840), (2257, 1505)] {
let display = advertised_display(native, None);
let fields = decode_wire(&display).unwrap();
assert!(fields.iter().any(|f| f.number == 8 && f.value == WireValue::Fixed64(1.0f64.to_le_bytes())));
assert_eq!(message(&fields, 6), message(&fields, 4));
let choices: Vec<_> = fields.iter().filter_map(|f| match f.value {
WireValue::Bytes(rect) if f.number == 3 => Some(rect), _ => None,
}).collect();
assert_eq!(choices.len(), 1 + scaled_sizes(native).len());
assert_eq!(choices[0], message(&fields, 6).unwrap());
for rect in choices {
let rect = decode_wire(rect).unwrap();
let (w, h) = (number(&rect, 3).unwrap(), number(&rect, 4).unwrap());
assert_eq!(number(&rect, 5), Some(w));
assert_eq!(number(&rect, 6), Some(h));
let selected = (w as u32, h as u32);
let expected = (selected != native).then_some(selected);
// Exercise both the observed legacy capture request and the resolution-only RPC.
for case in [2, 6] {
let (tx, rx) = watch::channel(VideoSettings::default());
let mut body = Vec::new();
let (wf, hf) = if case == 2 { (5, 6) } else { (1, 2) };
put_varint(wf, w, &mut body); put_varint(hf, h, &mut body);
if case == 2 {
put_varint(2, QUALITY_GENERAL, &mut body);
put_varint(18, 30, &mut body);
}
let response = handle_rpc(&decode_wire(&rpc(77, case, &body)).unwrap(), &tx, native).unwrap();
let response = decode_wire(&response).unwrap();
assert_eq!(message(&response, 1), Some(&[8u8, 77][..]));
assert_eq!(message(&response, 2), Some(&[][..]));
assert_eq!(rx.borrow().output, expected);
if case == 2 {
assert_eq!(rx.borrow().quality, QUALITY_GENERAL);
assert_eq!(rx.borrow().fps, 30);
}
let sync = sync_payload(&rx.borrow(), native);
let sync = decode_wire(&sync).unwrap();
for (wf, hf) in [(5, 6), (9, 10)] {
assert_eq!(number(&sync, wf), Some(w));
assert_eq!(number(&sync, hf), Some(h));
}
let updated = advertised_display(native, rx.borrow().output);
let updated = decode_wire(&updated).unwrap();
assert_eq!(message(&updated, 6), message(&fields, 6));
assert_eq!(message(&updated, 3), message(&fields, 3));
let current = decode_wire(message(&updated, 4).unwrap()).unwrap();
for (wf, hf) in [(3, 4), (5, 6)] {
assert_eq!(number(&current, wf), Some(w));
assert_eq!(number(&current, hf), Some(h));
}
// Echo synchronized dimensions with a quality/fps change, then restore native.
let mut body = Vec::new();
put_varint(5, number(&sync, 5).unwrap(), &mut body);
put_varint(6, number(&sync, 6).unwrap(), &mut body);
put_varint(2, QUALITY_BLURAY, &mut body);
put_varint(18, 60, &mut body);
handle_rpc(&decode_wire(&rpc(78, 2, &body)).unwrap(), &tx, native).unwrap();
assert_eq!(rx.borrow().output, expected);
assert_eq!(rx.borrow().quality, QUALITY_BLURAY);
assert_eq!(rx.borrow().fps, 60);
let mut native_request = Vec::new();
put_varint(5, u64::from(native.0), &mut native_request);
put_varint(6, u64::from(native.1), &mut native_request);
handle_rpc(&decode_wire(&rpc(79, 2, &native_request)).unwrap(), &tx, native).unwrap();
assert_eq!(rx.borrow().output, None);
handle_rpc(&decode_wire(&rpc(80, 2, &body)).unwrap(), &tx, native).unwrap();
assert_eq!(rx.borrow().output, expected);
handle_rpc(&decode_wire(&rpc(81, 2, &[80, 3])).unwrap(), &tx, native).unwrap();
assert_eq!(rx.borrow().output, None);
}
}
}
}
#[test]
fn explicit_pixel_size_takes_precedence_and_follow_remote_restores_native() {
let native = (3840, 2160);
let (tx, rx) = watch::channel(VideoSettings { output: Some((1280, 720)), ..Default::default() });
let mut body = Vec::new();
for (field, value) in [(5, 1920), (6, 1080), (16, 2560), (17, 1440)] {
put_varint(field, value, &mut body);
}
handle_rpc(&decode_wire(&rpc(1, 2, &body)).unwrap(), &tx, native).unwrap();
assert_eq!(rx.borrow().output, Some((2560, 1440)));
put_varint(10, 3, &mut body);
handle_rpc(&decode_wire(&rpc(2, 2, &body)).unwrap(), &tx, native).unwrap();
assert_eq!(rx.borrow().output, None);
let sync = sync_payload(&rx.borrow(), native);
let fields = decode_wire(&sync).unwrap();
assert_eq!(number(&fields, 5), Some(3840));
assert_eq!(number(&fields, 6), Some(2160));
}
#[test]
fn auto_quality_keeps_constant_quality_encoding() {
assert_eq!(VideoSettings::default().max_kbps(), None);
@@ -235,3 +446,27 @@ mod tests {
assert_eq!(number(&fields, 10), Some(1080));
}
}
#[cfg(test)]
mod codec_tests {
use super::*;
#[test]
fn codec_is_opt_in_and_requires_negotiated_support() {
let (tx, rx) = watch::channel(VideoSettings::default());
let rpc = |value| decode_wire(if value == 2 { &[18, 2, 104, 2] } else { &[18, 2, 104, 1] }).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H264);
handle_rpc_with_codecs(&rpc(2), &tx, (1920,1080), true, false).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H264);
handle_rpc_with_codecs(&rpc(2), &tx, (1920,1080), true, true).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H265);
handle_rpc_with_codecs(&rpc(1), &tx, (1920,1080), false, true).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H265); // unnegotiated H264 is never sent
// An unrelated FPS RPC must preserve the explicit codec selection.
handle_rpc_with_codecs(&decode_wire(&[26,2,8,1]).unwrap(), &tx, (1920,1080), true, true).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H265);
// The viewer reports its current codec (1) in every request; that must not undo H.265.
handle_rpc_with_codecs(&rpc(1), &tx, (1920,1080), true, true).unwrap();
assert_eq!(rx.borrow().codec, VideoCodec::H265);
assert_eq!(preferred_codec(true, false), VideoCodec::H264);
}
}
+217
View File
@@ -0,0 +1,217 @@
//! Real local SCTP peers; no cloud, desktop consent, audio or power operations.
use super::*;
use crate::protocol::{decode_wire, WireValue};
use std::{collections::HashMap, time::Duration};
use tokio::sync::mpsc;
type Messages = mpsc::UnboundedReceiver<(String, Vec<u8>)>;
struct Viewer {
pc: Arc<RTCPeerConnection>,
channels: HashMap<String, Arc<RTCDataChannel>>,
messages: Messages,
router: Arc<Mutex<webrtc::util::vnet::router::Router>>,
}
async fn connect(input: Arc<InputInjector>, desktop: bool,
portal: Option<Arc<crate::desktop_session::PortalDesktop>>, labels: &[&str]) -> (WebRtcSession, Viewer) {
let (host_setting, viewer_setting, router) = crate::test_network::virtual_network().await;
let screen = Arc::new(ScreenCapture::unavailable("synthetic locked desktop".into()));
let host = WebRtcSession::new(screen, input, crate::signaling::SignalingClient::disconnected(),
desktop, crate::session_ice::SessionIce::virtual_net(host_setting), portal).await.unwrap();
let pc = Arc::new(APIBuilder::new().with_setting_engine(viewer_setting).build().new_peer_connection(RTCConfiguration::default()).await.unwrap());
let (tx, messages) = mpsc::unbounded_channel();
let mut channels = HashMap::new();
for label in labels {
let channel = pc.create_data_channel(label, None).await.unwrap();
let (tx, name) = (tx.clone(), label.to_string());
channel.on_message(Box::new(move |message| {
let _ = tx.send((name.clone(), message.data.to_vec()));
Box::pin(async {})
}));
channels.insert(label.to_string(), channel);
}
let mut gathered = pc.gathering_complete_promise().await;
pc.set_local_description(pc.create_offer(None).await.unwrap()).await.unwrap();
gathered.recv().await;
host.pc.set_remote_description(pc.local_description().await.unwrap()).await.unwrap();
let mut gathered = host.pc.gathering_complete_promise().await;
host.pc.set_local_description(host.pc.create_answer(None).await.unwrap()).await.unwrap();
gathered.recv().await;
pc.set_remote_description(host.pc.local_description().await.unwrap()).await.unwrap();
tokio::time::timeout(Duration::from_secs(8), async {
while channels.values().any(|channel| channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open) {
tokio::time::sleep(Duration::from_millis(20)).await;
}
}).await.expect("local SCTP channels did not open");
(host, Viewer { pc, channels, messages, router })
}
fn body(message: &[u8], number: u32) -> Option<&[u8]> {
decode_wire(message).ok()?.into_iter().find_map(|field| match (field.number, field.value) {
(n, WireValue::Bytes(bytes)) if n == number => Some(bytes), _ => None,
})
}
fn bytes(number: u32, value: &[u8]) -> Vec<u8> {
fn varint(mut value: usize, out: &mut Vec<u8>) {
while value >= 128 { out.push((value as u8 & 127) | 128); value >>= 7; }
out.push(value as u8);
}
let mut out = Vec::new();
varint(((number << 3) | 2) as usize, &mut out);
varint(value.len(), &mut out);
out.extend_from_slice(value);
out
}
async fn receive(messages: &mut Messages, label: &str, field: u32) -> Vec<u8> {
tokio::time::timeout(Duration::from_secs(5), async {
loop {
let (channel, message) = messages.recv().await.expect("peer stopped responding");
if channel == label && body(&message, field).is_some() { return message; }
}
}).await.expect("expected protocol response timed out")
}
async fn send(viewer: &Viewer, label: &str, message: Vec<u8>) {
viewer.channels[label].send(&message.into()).await.unwrap();
}
fn mapping(kind: u8, payload: &[u8]) -> Vec<u8> {
let mut frame = vec![8, 1, 16, 2, 24, 3, 32, kind];
frame.extend(bytes(5, payload));
crate::initialization::envelope(1, 0, 27, &frame)
}
#[tokio::test]
async fn closing_unnegotiated_peers_releases_callback_state() {
for desktop in [false, true] {
let input = Arc::new(InputInjector::without_desktop());
let capture = Arc::new(ScreenCapture::unavailable("synthetic desktop".into()));
let peer = WebRtcSession::new(capture, input,
crate::signaling::SignalingClient::disconnected(), desktop,
crate::session_ice::SessionIce::local(), None).await.unwrap();
let weak = Arc::downgrade(&peer.pc);
peer.pc.close().await.unwrap();
drop(peer);
tokio::time::timeout(Duration::from_secs(4), async {
while weak.upgrade().is_some() {
tokio::time::sleep(Duration::from_millis(20)).await;
}
}).await.expect("closed peer retained by callbacks");
}
}
#[tokio::test]
async fn locked_control_unlock_requests_and_tools_work_without_a_video_frame() {
tokio::time::timeout(Duration::from_secs(35), async {
let input = Arc::new(InputInjector::without_desktop());
let (lock, locked) = tokio::sync::watch::channel(Some(true));
let portal = Arc::new(crate::desktop_session::PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic locked desktop".into())), locked));
let (desktop, mut viewer) = connect(input.clone(), true, Some(portal.clone()), &["CONTROL_DATA_CHANNEL"]).await;
// Official Android 4.43.0's unlock presenter requires CaptureChange
// (desktop/window) before it will use the secure state to send action15.
let selected = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 8).await;
let selected = decode_wire(body(&selected, 8).unwrap()).unwrap();
let capture_type = selected.iter().find_map(|f| match (f.number, &f.value) {
(1, WireValue::Varint(value)) => Some(*value), _ => None,
}).unwrap_or(0);
assert_eq!(capture_type, 0);
let state = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 15).await;
let secure = body(body(&state, 15).unwrap(), 1).unwrap();
assert!(decode_wire(secure).unwrap().iter().any(|f| f.number == 1 && f.value == WireValue::Varint(1)));
assert!(portal.snapshot().screen.init_error.is_some());
// A capture request waiting for unlock must not occupy CONTROL dispatch.
let mut capture = bytes(1, &[8, 31]);
capture.extend(bytes(2, &[16, 2, 144, 1, 60]));
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(10, 0, 21, &capture)).await;
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(91, 0, 3, &[8, 0])).await;
let echo = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 3).await;
let echo: serde_json::Value = serde_json::from_slice(body(body(&echo, 3).unwrap(), 2).unwrap()).unwrap();
assert_eq!(echo["seq"], 91);
send(&viewer, "CONTROL_DATA_CHANNEL",
crate::initialization::envelope(92, 0, 16, &[8, 1])).await;
let state = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 15).await;
assert!(body(body(&state, 15).unwrap(), 1).is_some());
// Missing password exercises the real AUTO_UNLOCK_SYSTEM failure route
// without invoking PAM or unlocking the local user's desktop.
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(1, 0, 3, &[8, 15])).await;
receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 12).await;
let (terminal, mut terminal_viewer) = connect(input.clone(), false, None,
&["CONTROL_DATA_CHANNEL", "BINARY_DATA_CHANNEL"]).await;
send(&terminal_viewer, "BINARY_DATA_CHANNEL",
crate::terminal::encode(crate::terminal::LIST_REQ, 71, &[])).await;
let frame = tokio::time::timeout(Duration::from_secs(5), async {
loop {
let (channel, message) = terminal_viewer.messages.recv().await.unwrap();
if channel == "BINARY_DATA_CHANNEL" { break crate::terminal::decode(&message).unwrap(); }
}
}).await.unwrap();
assert_eq!((frame.kind, frame.session), (crate::terminal::LIST_RESP, 71));
let (files, mut file_viewer) = connect(input.clone(), false, None,
&["CONTROL_DATA_CHANNEL", "FILE_DATA_CHANNEL"]).await;
let directory = std::env::temp_dir().join(format!("uu-control-empty-{}-{}", std::process::id(), rand::random::<u64>()));
std::fs::create_dir(&directory).unwrap();
let mut read = bytes(1, &[8, 7]);
read.extend(bytes(2, directory.to_str().unwrap().as_bytes()));
let mut request = bytes(1, &[8, 99]);
request.extend(bytes(8, &bytes(10, &read)));
let query = crate::initialization::envelope(5, 0, 21, &request);
send(&file_viewer, "CONTROL_DATA_CHANNEL", query.clone()).await;
let response = receive(&mut file_viewer.messages, "FILE_DATA_CHANNEL", 22).await;
let listing = body(body(body(&response, 22).unwrap(), 4).unwrap(), 7).unwrap();
assert!(decode_wire(listing).unwrap().iter().any(|f| f.number == 4 && f.value == WireValue::Varint(1)));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let echo = tokio::spawn(async move {
let (socket, _) = listener.accept().await.unwrap();
let (mut read, mut write) = socket.into_split();
tokio::io::copy(&mut read, &mut write).await.unwrap();
});
let (ports, mut port_viewer) = connect(input, false, None,
&["CONTROL_DATA_CHANNEL", "FILE_DATA_CHANNEL"]).await;
let target = serde_json::json!({"version":1,"target_host":"127.0.0.1","target_port":port}).to_string();
send(&port_viewer, "FILE_DATA_CHANNEL", mapping(0, target.as_bytes())).await;
let ack = receive(&mut port_viewer.messages, "FILE_DATA_CHANNEL", 27).await;
let ack: serde_json::Value = serde_json::from_slice(body(body(&ack, 27).unwrap(), 5).unwrap()).unwrap();
assert_eq!(ack["ok"], true);
let mut sessions = HashMap::from([
("desktop".into(), desktop), ("terminal".into(), terminal), ("files".into(), files),
("ports".into(), ports),
]);
assert!(crate::superseded_peer_ids(&sessions, "another-tool", false).is_empty());
assert_eq!(crate::superseded_peer_ids(&sessions, "next-desktop", true), ["desktop"]);
for id in crate::superseded_peer_ids(&sessions, "next-desktop", true) {
sessions.remove(&id).unwrap().pc.close().await.unwrap();
}
assert_eq!(sessions["terminal"].pc.connection_state(), RTCPeerConnectionState::Connected);
assert_eq!(sessions["files"].pc.connection_state(), RTCPeerConnectionState::Connected);
// A desktop close must not cancel the independent file-transfer responder.
send(&file_viewer, "CONTROL_DATA_CHANNEL", query).await;
receive(&mut file_viewer.messages, "FILE_DATA_CHANNEL", 22).await;
send(&port_viewer, "FILE_DATA_CHANNEL", mapping(2, b"local echo")).await;
let echoed = receive(&mut port_viewer.messages, "FILE_DATA_CHANNEL", 27).await;
assert_eq!(body(body(&echoed, 27).unwrap(), 5), Some(b"local echo".as_slice()));
send(&terminal_viewer, "BINARY_DATA_CHANNEL",
crate::terminal::encode(crate::terminal::LIST_REQ, 72, &[])).await;
let frame = tokio::time::timeout(Duration::from_secs(5), async {
loop {
let (channel, message) = terminal_viewer.messages.recv().await.unwrap();
if channel == "BINARY_DATA_CHANNEL" { break crate::terminal::decode(&message).unwrap(); }
}
}).await.unwrap();
assert_eq!((frame.kind, frame.session), (crate::terminal::LIST_RESP, 72));
std::fs::remove_dir(directory).unwrap();
for (_, peer) in sessions { peer.pc.close().await.unwrap(); }
viewer.pc.close().await.unwrap();
terminal_viewer.pc.close().await.unwrap();
file_viewer.pc.close().await.unwrap();
port_viewer.pc.close().await.unwrap();
for router in [&viewer.router, &terminal_viewer.router, &file_viewer.router, &port_viewer.router] {
router.lock().await.stop().await.unwrap();
}
echo.abort();
drop(lock);
}).await.expect("locked bootstrap/tool regression exceeded deadline");
}
+101
View File
@@ -0,0 +1,101 @@
//! Small GNOME-only display wake and idle-inhibition bridge.
#[cfg(all(target_os = "linux", feature = "portal"))]
mod native {
use std::{ffi::c_void, ptr::NonNull};
extern "C" {
fn uu_desktop_power_wake() -> i32;
fn uu_desktop_idle_inhibitor_acquire(error: *mut i32) -> *mut c_void;
fn uu_desktop_idle_inhibitor_release(inhibitor: *mut c_void);
}
pub struct Handle(NonNull<c_void>);
// GDBusConnection is thread-safe; the C handle is only used by its single Drop call.
unsafe impl Send for Handle {}
impl Handle {
pub fn acquire() -> Result<Self, String> {
let mut error = 0;
let pointer = unsafe { uu_desktop_idle_inhibitor_acquire(&mut error) };
NonNull::new(pointer).map(Self).ok_or_else(|| super::error("idle inhibition", error))
}
pub fn wake() -> Result<(), String> {
super::check("display wake", unsafe { uu_desktop_power_wake() })
}
}
impl Drop for Handle {
fn drop(&mut self) {
unsafe { uu_desktop_idle_inhibitor_release(self.0.as_ptr()) }
}
}
}
/// Set GNOME's display power-save mode to on. This does not unlock the session or change
/// lock-screen policy. Session-bus setup and each method call are bounded to three seconds.
pub fn wake() -> Result<(), String> {
if !gnome_portal_active() {
return Ok(());
}
#[cfg(all(target_os = "linux", feature = "portal"))]
{
native::Handle::wake()
}
#[cfg(not(all(target_os = "linux", feature = "portal")))]
{
Ok(())
}
}
/// Holds a GNOME SessionManager idle-only inhibitor until dropped.
///
/// Acquire and drop perform blocking D-Bus work. Session-bus setup and each method call are
/// bounded to three seconds. Callers on an async runtime should move both operations to a
/// blocking worker.
pub struct IdleInhibitor {
#[cfg(all(target_os = "linux", feature = "portal"))]
_handle: Option<native::Handle>,
}
impl IdleInhibitor {
pub fn acquire() -> Result<Self, String> {
if !gnome_portal_active() {
return Ok(Self {
#[cfg(all(target_os = "linux", feature = "portal"))]
_handle: None,
});
}
#[cfg(all(target_os = "linux", feature = "portal"))]
{
native::Handle::acquire().map(|handle| Self { _handle: Some(handle) })
}
#[cfg(not(all(target_os = "linux", feature = "portal")))]
{
Ok(Self {})
}
}
}
fn gnome_portal_active() -> bool {
let gnome = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default().split(':')
.any(|desktop| desktop.eq_ignore_ascii_case("gnome"));
gnome && crate::desktop_backend::detect().is_ok_and(|backend| backend == crate::desktop_backend::DesktopBackend::Portal)
}
#[cfg(all(target_os = "linux", feature = "portal"))]
fn check(operation: &str, code: i32) -> Result<(), String> {
if code == 0 { Ok(()) } else { Err(error(operation, code)) }
}
#[cfg(all(target_os = "linux", feature = "portal"))]
fn error(operation: &str, code: i32) -> String {
let reason = match code {
1 => "session D-Bus unavailable",
2 => "GNOME D-Bus operation failed",
_ => "GNOME D-Bus operation failed",
};
format!("{operation}: {reason} (code {code})")
}
+567
View File
@@ -0,0 +1,567 @@
//! Portal availability is independent of the authenticated CONTROL transport.
use std::future::Future;
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
use std::time::Instant;
use tokio::sync::{watch, Mutex};
use crate::{capture::ScreenCapture, input::DesktopInputSession};
use crate::portal::PortalError;
use webrtc::{data_channel::{RTCDataChannel, data_channel_state::RTCDataChannelState},
peer_connection::{RTCPeerConnection, peer_connection_state::RTCPeerConnectionState}};
#[derive(Clone)]
pub struct State {
pub screen: Arc<ScreenCapture>,
pub waiting: bool,
}
impl State {
pub fn permission(&self, desktop: bool) -> crate::system_metrics::Permission {
use crate::system_metrics::Permission;
if !desktop { Permission::Allow }
else if self.waiting { Permission::Unknown }
else if self.screen.init_error.is_none() { Permission::Allow }
else { Permission::Reject }
}
/// Capture exists and is not waiting for a replacement of a closed Portal session.
pub fn ready(&self) -> bool { self.screen.init_error.is_none() && !self.waiting }
}
pub struct PortalDesktop {
state: watch::Sender<State>,
opening: Arc<Mutex<()>>,
locked: watch::Receiver<Option<bool>>,
}
struct CancelOnDrop(Arc<AtomicBool>);
impl Drop for CancelOnDrop {
fn drop(&mut self) { self.0.store(true, Ordering::Release); }
}
enum Inhibitor {
Real(crate::desktop_power::IdleInhibitor),
#[cfg(test)]
Fake(DropSignal),
}
#[cfg(test)]
struct DropSignal(Arc<std::sync::atomic::AtomicUsize>);
#[cfg(test)]
impl Drop for DropSignal {
fn drop(&mut self) { self.0.fetch_add(1, Ordering::Relaxed); }
}
struct AttemptPrep {
wake: Result<(), String>,
inhibitor: IdleGuard,
}
#[derive(Default)]
struct IdleGuard(Option<Inhibitor>);
impl Drop for IdleGuard {
fn drop(&mut self) {
match self.0.take() {
Some(Inhibitor::Real(guard)) => { tokio::task::spawn_blocking(move || drop(guard)); }
#[cfg(test)]
Some(Inhibitor::Fake(signal)) => drop(signal),
None => {}
}
}
}
enum AttemptOutcome {
Ready(Arc<ScreenCapture>),
Retry(String),
Terminal(String),
Cancelled,
}
fn classify(result: Result<Arc<ScreenCapture>, PortalError>) -> AttemptOutcome {
match result {
Ok(screen) => AttemptOutcome::Ready(screen),
Err(error) if error.code == 13 => AttemptOutcome::Cancelled,
// 4/5/14 are fatal session/geometry states; 6/8 are unsupported
// consent/format shapes which must not loop prompts forever.
Err(error) if matches!(error.code, 4 | 5 | 6 | 8 | 14) => AttemptOutcome::Terminal(error.to_string()),
Err(error) => AttemptOutcome::Retry(error.to_string()),
}
}
fn should_prepare_power(healthy: bool, force_wake: bool) -> bool {
!healthy || force_wake
}
fn prepare_attempt<W, I>(
cancelled: &AtomicBool,
owner: &DesktopInputSession,
locked: &watch::Receiver<Option<bool>>,
wake: W,
inhibit: I,
) -> Option<AttemptPrep>
where
W: FnOnce() -> Result<(), String>,
I: FnOnce() -> IdleGuard,
{
let current = || !cancelled.load(Ordering::Acquire)
&& owner.is_current()
&& *locked.borrow() == Some(false);
if !current() { return None; }
let wake = wake();
if !current() { return None; }
let inhibitor = inhibit();
if !current() { return None; }
Some(AttemptPrep { wake, inhibitor })
}
impl PortalDesktop {
pub fn new(screen: Arc<ScreenCapture>) -> Self {
Self { state: watch::channel(State { waiting: screen.init_error.is_some(), screen }).0,
opening: Arc::new(Mutex::new(())), locked: crate::session_lock::watch_locked() }
}
pub fn begin(&self) {
// GNOME closes the Portal session on lock; a cached screen with a dead session
// is not ready (no Allow, no stale video path) until maintain() rebuilds it.
self.state.send_modify(|state| state.waiting = state.screen.init_error.is_some()
|| state.screen.portal.as_ref().is_some_and(|portal| !portal.is_alive()));
}
pub fn snapshot(&self) -> State { self.state.borrow().clone() }
pub fn subscribe(&self) -> watch::Receiver<State> { self.state.subscribe() }
pub fn lock_state(&self) -> watch::Receiver<Option<bool>> { self.locked.clone() }
#[cfg(test)]
pub fn with_lock_state(screen: Arc<ScreenCapture>, locked: watch::Receiver<Option<bool>>) -> Self {
Self { state: watch::channel(State { waiting: screen.init_error.is_some(), screen }).0,
opening: Arc::new(Mutex::new(())), locked }
}
#[cfg(test)]
pub fn publish_test_screen(&self, screen: Arc<ScreenCapture>) {
self.state.send_replace(State { screen, waiting: false });
}
fn publish(&self, owner: &DesktopInputSession, screen: Arc<ScreenCapture>) {
owner.with_current(|| {
if screen.init_error.is_some() { owner.release_input(); }
owner.bind_portal(screen.portal.clone());
self.state.send_replace(State { screen, waiting: false });
});
}
fn pause(&self, owner: &DesktopInputSession) {
let screen = self.snapshot().screen;
owner.with_current(|| {
owner.release_input();
if let Some(portal) = &screen.portal { portal.pause(); }
});
}
pub async fn run(self: Arc<Self>, pc: Arc<RTCPeerConnection>, channel: Arc<RTCDataChannel>,
owner: Arc<DesktopInputSession>) {
self.run_until(owner, closed(&pc, &channel)).await;
}
pub async fn run_until<F>(self: Arc<Self>, owner: Arc<DesktopInputSession>, wait_closed: F)
where F: Future<Output = ()> {
tokio::select! {
result = self.maintain(owner.clone()) => {
if let Err(error) = result {
log::warn!("Portal desktop unavailable: {error}");
self.publish(&owner, Arc::new(ScreenCapture::unavailable(error)));
}
},
_ = wait_closed => {},
}
}
async fn maintain(&self, owner: Arc<DesktopInputSession>) -> Result<(), String> {
self.maintain_with(owner,
crate::desktop_power::wake,
|| match crate::desktop_power::IdleInhibitor::acquire() {
Ok(guard) => IdleGuard(Some(Inhibitor::Real(guard))),
Err(error) => { log::warn!("Desktop idle inhibition unavailable: {error}"); IdleGuard::default() }
},
|screen, cancel| {
if let Some(portal) = &screen.portal {
portal.recover(&cancel)?;
Ok(screen)
} else {
ScreenCapture::open_portal(&cancel).map(Arc::new)
}
}).await
}
async fn maintain_with<W, I, O>(&self, owner: Arc<DesktopInputSession>, wake: W, inhibit: I, open: O) -> Result<(), String>
where
W: Fn() -> Result<(), String> + Send + Sync + 'static,
I: Fn() -> IdleGuard + Send + Sync + 'static,
O: Fn(Arc<ScreenCapture>, Arc<AtomicBool>) -> Result<Arc<ScreenCapture>, PortalError> + Send + Sync + 'static,
{
let wake = Arc::new(wake);
let inhibit = Arc::new(inhibit);
let open = Arc::new(open);
let mut locked = self.locked.clone();
let mut previous = None;
let mut retry_at = Instant::now();
loop {
if !owner.is_current() { return Ok(()); }
let state = *locked.borrow_and_update();
if state != Some(false) {
if previous == Some(false) { self.pause(&owner); }
} else {
// A fresh unlocked transition gets an immediate wake/recovery attempt.
let force_wake = previous != Some(false);
if force_wake { retry_at = Instant::now(); }
let mut attempt_locked = locked.clone();
let outcome = tokio::select! {
biased;
_ = wait_for_lock(&mut locked) => AttemptOutcome::Cancelled,
outcome = self.portal_attempt(&owner, &mut attempt_locked, retry_at, force_wake, wake.clone(), inhibit.clone(), open.clone()) => outcome,
};
match outcome {
AttemptOutcome::Ready(screen) => self.publish(&owner, screen),
AttemptOutcome::Terminal(error) => return Err(error),
AttemptOutcome::Retry(error) => {
log::warn!("Portal desktop recovery deferred: {error}");
retry_at = Instant::now() + std::time::Duration::from_secs(5);
}
AttemptOutcome::Cancelled => {}
}
if *locked.borrow_and_update() != Some(false) { self.pause(&owner); }
}
previous = *locked.borrow_and_update();
tokio::select! {
_ = locked.changed() => {},
_ = tokio::time::sleep(std::time::Duration::from_millis(200)) => {},
}
}
}
async fn portal_attempt<W, I, O>(&self, owner: &Arc<DesktopInputSession>,
locked: &mut watch::Receiver<Option<bool>>, retry_at: Instant, force_wake: bool,
wake: Arc<W>, inhibit: Arc<I>, open: Arc<O>) -> AttemptOutcome
where
W: Fn() -> Result<(), String> + Send + Sync + 'static,
I: Fn() -> IdleGuard + Send + Sync + 'static,
O: Fn(Arc<ScreenCapture>, Arc<AtomicBool>) -> Result<Arc<ScreenCapture>, PortalError> + Send + Sync + 'static,
{
if !owner.is_current() || *locked.borrow() != Some(false) || Instant::now() < retry_at {
return AttemptOutcome::Cancelled;
}
let gate = self.opening.clone().lock_owned().await;
// The state can change while this attempt waits for an earlier worker
// to finish rotating the Portal permission token.
if !owner.is_current() || *locked.borrow() != Some(false) {
return AttemptOutcome::Cancelled;
}
let screen = self.snapshot().screen;
let healthy = screen.portal.as_ref().is_some_and(|portal| portal.is_alive());
if !should_prepare_power(healthy, force_wake) {
return AttemptOutcome::Cancelled;
}
let cancel = Arc::new(AtomicBool::new(false));
let _cancel = CancelOnDrop(cancel.clone());
let prep_cancel = cancel.clone();
let prep_owner = owner.clone();
let prep_locked = locked.clone();
let prep = match tokio::task::spawn_blocking(move || prepare_attempt(
&prep_cancel,
&prep_owner,
&prep_locked,
|| (*wake)(),
|| (*inhibit)(),
)).await {
Ok(Some(prep)) => prep,
Ok(None) => return AttemptOutcome::Cancelled,
Err(_) => return AttemptOutcome::Terminal("Desktop power worker failed".into()),
};
if let Err(error) = prep.wake { log::warn!("Portal screen wake failed: {error}"); }
// Held by the cancellable async helper, not the blocking worker, so a
// lock transition releases it while worker cleanup may still be running.
let _inhibitor = prep.inhibitor;
if !owner.is_current() || *locked.borrow() != Some(false) {
return AttemptOutcome::Cancelled;
}
// A new controller can arrive while the cached Portal handle is alive
// but the display has gone to sleep without a lock transition.
if healthy { return AttemptOutcome::Ready(screen); }
let worker_cancel = cancel.clone();
let result = tokio::task::spawn_blocking(move || {
// The worker owns the gate until its real cleanup completes; a
// cancelled helper only signals it and cannot start an overlap.
let _gate = gate;
if worker_cancel.load(Ordering::Acquire) {
return Err(PortalError { code: 13 });
}
open(screen, worker_cancel)
}).await;
if !owner.is_current() || *locked.borrow() != Some(false) {
return AttemptOutcome::Cancelled;
}
match result {
Ok(result) => classify(result),
Err(_) => AttemptOutcome::Terminal("Portal initialization worker failed".into()),
}
}
}
async fn wait_for_lock(locked: &mut watch::Receiver<Option<bool>>) {
while locked.changed().await.is_ok() {
if *locked.borrow_and_update() != Some(false) { return; }
}
}
pub async fn closed(pc: &RTCPeerConnection, channel: &RTCDataChannel) {
while !matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
&& channel.ready_state() == RTCDataChannelState::Open {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
}
pub async fn wait_ready(state: &mut watch::Receiver<State>, channel: &RTCDataChannel) -> Option<Arc<ScreenCapture>> {
loop {
let current = state.borrow_and_update().clone();
if current.ready() { return Some(current.screen); }
if !current.waiting || channel.ready_state() != RTCDataChannelState::Open { return None; }
tokio::select! {
changed = state.changed() => if changed.is_err() { return None; },
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::AtomicUsize;
fn host() -> PortalDesktop {
let (_, locked) = watch::channel(Some(true));
PortalDesktop::with_lock_state(Arc::new(ScreenCapture::unavailable("pending".into())), locked)
}
fn ready_screen() -> Arc<ScreenCapture> {
let mut screen = ScreenCapture::unavailable("synthetic".into());
screen.backend = crate::desktop_backend::DesktopBackend::Portal;
screen.init_error = None;
screen.width = 1920;
screen.height = 1080;
Arc::new(screen)
}
#[test]
fn cached_screen_waiting_for_a_closed_portal_is_not_ready_or_allowed() {
use crate::system_metrics::Permission;
let live = State { screen: ready_screen(), waiting: false };
assert!(live.ready() && live.permission(true) == Permission::Allow);
let stale = State { screen: ready_screen(), waiting: true };
assert!(!stale.ready());
assert!(stale.permission(true) == Permission::Unknown);
assert!(stale.permission(false) == Permission::Allow);
}
#[test]
fn recovered_screen_is_retained_across_transient_disconnect() {
let host = host();
let mut updates = host.subscribe();
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
owner.connected();owner.disconnected();
let screen = ready_screen();
host.publish(&owner, screen.clone());
assert!(updates.has_changed().unwrap());
assert!(!updates.borrow_and_update().waiting);
assert!(Arc::ptr_eq(&host.snapshot().screen, &screen));
assert!(!host.snapshot().waiting);
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Allow);
owner.with_input(|| panic!("publication must not reactivate input"));
owner.connected();
assert!(Arc::ptr_eq(&host.snapshot().screen, &screen));
}
#[test]
fn terminal_failure_publishes_rejection_while_disconnected() {
let host = host();
let mut updates = host.subscribe();
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
owner.connected();owner.disconnected();
host.publish(&owner, Arc::new(ScreenCapture::unavailable("consent rejected".into())));
assert!(updates.has_changed().unwrap());
assert!(!updates.borrow_and_update().waiting);
assert!(!host.snapshot().waiting);
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Reject);
owner.connected();
assert!(!host.snapshot().waiting);
assert_eq!(host.snapshot().screen.init_error.as_deref(), Some("consent rejected"));
}
#[test]
fn replaced_peer_cannot_publish_success_or_failure() {
let host = host();
let input = Arc::new(crate::input::InputInjector::without_desktop());
let old = input.desktop_session();
let current = input.desktop_session();
let screen = ready_screen();
host.publish(&current, screen.clone());
host.publish(&old, Arc::new(ScreenCapture::unavailable("late failure".into())));
host.publish(&old, ready_screen());
assert!(Arc::ptr_eq(&host.snapshot().screen, &screen));
assert!(!host.snapshot().waiting);
}
#[test]
fn unavailable_desktop_does_not_reject_independent_tools() {
let host = PortalDesktop::new(Arc::new(ScreenCapture::unavailable("not authorized".into())));
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Unknown);
assert_eq!(host.snapshot().permission(false), crate::system_metrics::Permission::Allow);
host.state.send_modify(|state| state.waiting = false);
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Reject);
assert_eq!(host.snapshot().permission(false), crate::system_metrics::Permission::Allow);
host.begin();
assert!(host.snapshot().waiting);
}
#[test]
fn unsupported_portal_shapes_are_terminal_while_transient_codes_retry() {
for code in [4, 5, 6, 8, 14] {
assert!(matches!(classify(Err(PortalError { code })), AttemptOutcome::Terminal(_)), "code {code}");
}
for code in [1, 3, 7, 9, 10] {
assert!(matches!(classify(Err(PortalError { code })), AttemptOutcome::Retry(_)), "code {code}");
}
assert!(matches!(classify(Err(PortalError { code: 13 })), AttemptOutcome::Cancelled));
}
#[test]
fn fresh_unlock_wakes_even_when_cached_portal_is_healthy() {
assert!(should_prepare_power(true, true));
assert!(should_prepare_power(false, false));
assert!(!should_prepare_power(true, false));
}
#[test]
fn queued_power_prep_checks_cancel_lock_and_current_owner_before_wake() {
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
let (lock, locked) = watch::channel(Some(false));
let wakes = AtomicUsize::new(0);
let inhibitors = AtomicUsize::new(0);
let cancelled = AtomicBool::new(true);
assert!(prepare_attempt(&cancelled, &owner, &locked,
|| { wakes.fetch_add(1, Ordering::Relaxed); Ok(()) },
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
assert_eq!(wakes.load(Ordering::Relaxed), 0);
cancelled.store(false, Ordering::Release);
lock.send(Some(true)).unwrap();
assert!(prepare_attempt(&cancelled, &owner, &locked,
|| { wakes.fetch_add(1, Ordering::Relaxed); Ok(()) },
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
assert_eq!(wakes.load(Ordering::Relaxed), 0);
lock.send(Some(false)).unwrap();
let locked_during_wake = lock.clone();
assert!(prepare_attempt(&cancelled, &owner, &locked,
|| {
wakes.fetch_add(1, Ordering::Relaxed);
locked_during_wake.send(Some(true)).unwrap();
Ok(())
},
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
assert_eq!(wakes.load(Ordering::Relaxed), 1);
assert_eq!(inhibitors.load(Ordering::Relaxed), 0);
lock.send(Some(false)).unwrap();
let replacement = input.desktop_session();
assert!(prepare_attempt(&cancelled, &owner, &locked,
|| { wakes.fetch_add(1, Ordering::Relaxed); Ok(()) },
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
assert_eq!(wakes.load(Ordering::Relaxed), 1);
assert!(replacement.is_current());
assert_eq!(inhibitors.load(Ordering::Relaxed), 0);
}
#[test]
fn cancelling_a_peer_signals_its_blocking_permission_worker() {
let flag = Arc::new(AtomicBool::new(false));
let guard = CancelOnDrop(flag.clone());
assert!(!flag.load(Ordering::Acquire));
drop(guard);
assert!(flag.load(Ordering::Acquire));
}
#[tokio::test]
async fn lock_transition_cancels_blocked_attempt_and_releases_inhibitor() {
let (lock, locked) = watch::channel(Some(false));
let host = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("pending".into())), locked));
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
let releases = Arc::new(AtomicUsize::new(0));
let wakes = Arc::new(AtomicUsize::new(0));
let (started, mut started_rx) = tokio::sync::mpsc::unbounded_channel();
let task = tokio::spawn({
let host = host.clone();
let owner = owner.clone();
let releases = releases.clone();
let wakes = wakes.clone();
async move {
host.maintain_with(owner,
move || {
wakes.fetch_add(1, Ordering::Relaxed);
Ok(())
},
move || IdleGuard(Some(Inhibitor::Fake(DropSignal(releases.clone())))),
move |_screen, cancel| {
let _ = started.send(());
while !cancel.load(Ordering::Acquire) {
std::thread::sleep(std::time::Duration::from_millis(1));
}
Err(PortalError { code: 13 })
}).await
}
});
started_rx.recv().await.unwrap();
lock.send(Some(true)).unwrap();
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
while releases.load(Ordering::Relaxed) == 0 {
assert!(tokio::time::Instant::now() < deadline, "cancelled attempt did not release inhibitor");
tokio::task::yield_now().await;
}
assert_eq!(wakes.load(Ordering::Relaxed), 1);
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
while host.opening.try_lock().is_err() {
assert!(tokio::time::Instant::now() < deadline, "cancelled worker did not finish");
tokio::task::yield_now().await;
}
task.abort();
let _ = task.await;
}
#[tokio::test]
async fn inhibitor_release_does_not_wait_for_blocked_worker_cleanup() {
let (lock, locked) = watch::channel(Some(false));
let host = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("pending".into())), locked));
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
let releases = Arc::new(AtomicUsize::new(0));
let worker_release = Arc::new(AtomicBool::new(false));
let (started, mut started_rx) = tokio::sync::mpsc::unbounded_channel();
let task = tokio::spawn({
let host = host.clone();
let owner = owner.clone();
let releases = releases.clone();
let worker_release = worker_release.clone();
async move {
host.maintain_with(owner,
|| Ok(()),
move || IdleGuard(Some(Inhibitor::Fake(DropSignal(releases.clone())))),
move |screen, _cancel| {
let _ = started.send(());
while !worker_release.load(Ordering::Acquire) {
std::thread::sleep(std::time::Duration::from_millis(1));
}
Ok(screen)
}).await
}
});
started_rx.recv().await.unwrap();
lock.send(Some(true)).unwrap();
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
while releases.load(Ordering::Relaxed) == 0 {
assert!(tokio::time::Instant::now() < deadline, "cancelled attempt did not release inhibitor");
tokio::task::yield_now().await;
}
assert!(host.opening.try_lock().is_err(), "worker cleanup must still own the gate");
worker_release.store(true, Ordering::Release);
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
loop {
if host.opening.try_lock().is_ok() { break; }
assert!(tokio::time::Instant::now() < deadline, "worker cleanup did not release the gate");
tokio::task::yield_now().await;
}
task.abort();
let _ = task.await;
}
}
+6 -2
View File
@@ -392,8 +392,12 @@ impl FileSession {
let mut listing = Enc::default();
for e in &entries { listing = listing.bytes(1, &e.0); }
let compressed = zlib(&listing.0);
log::info!("File transfer read_dir: request_id={} root={} entries={} dir_data_bytes={}", rid, path.is_empty(), entries.len(), compressed.len());
for e in entries { body = body.msg(3, e); }
// Both copies in one message: a 5600-entry folder is ~73 KB compressed but several hundred KB
// inline, beyond the 256 KB SCTP message limit, and the reply was dropped. Keep the inline copy
// only when the message still fits; current viewers read dir_data.
let inline = listing.0.len() + compressed.len() < 200_000;
log::info!("File transfer read_dir: request_id={} root={} entries={} dir_data_bytes={} inline={}", rid, path.is_empty(), entries.len(), compressed.len(), inline);
if inline { for e in entries { body = body.msg(3, e); } }
body = body.uint(4, FT_SUCCEEDED).bytes(5, &compressed);
}
Err(code) => { log::info!("File transfer read_dir failed: request_id={} error={}", rid, code); body = body.uint(4, code); }
+72
View File
@@ -0,0 +1,72 @@
//! RFC 7798 §4.4.1 / §4.4.3, single-layer non-interleaved HEVC (no DONL).
//! Parameter sets travel in-band before each IDR; no aggregation is required.
use bytes::Bytes;
pub fn payload(mtu: usize, annex_b: &[u8]) -> Result<Vec<Bytes>, String> {
if mtu < 4 { return Err("HEVC payload MTU too small".into()); }
let mut starts = Vec::new();
let mut i = 0;
while i + 3 <= annex_b.len() {
let prefix = if annex_b[i..].starts_with(&[0, 0, 0, 1]) { 4 }
else if annex_b[i..].starts_with(&[0, 0, 1]) { 3 } else { i += 1; continue; };
starts.push((i, i + prefix)); i += prefix;
}
if starts.is_empty() { return Err("HEVC access unit has no Annex-B start code".into()); }
let mut out = Vec::new();
for (index, &(_, start)) in starts.iter().enumerate() {
let mut end = starts.get(index + 1).map_or(annex_b.len(), |s| s.0);
while end > start && annex_b[end - 1] == 0 { end -= 1; } // trailing_zero_8bits
let nal = &annex_b[start..end];
if nal.len() < 2 || nal[0] & 0x80 != 0 || nal[1] & 7 == 0 {
return Err("Invalid HEVC NAL header".into());
}
let kind = (nal[0] >> 1) & 63;
if kind >= 48 { return Err("HEVC Annex-B contains RTP packet type".into()); }
if nal.len() <= mtu { out.push(Bytes::copy_from_slice(nal)); continue; }
let chunks = nal[2..].chunks(mtu - 3);
let last = chunks.len() - 1;
for (n, chunk) in chunks.enumerate() {
let mut fu = Vec::with_capacity(3 + chunk.len());
// Keep F, LayerId and TID, replacing only the NAL type with FU=49.
fu.extend_from_slice(&[(nal[0] & 0x81) | (49 << 1), nal[1],
kind | if n == 0 { 0x80 } else { 0 } | if n == last { 0x40 } else { 0 }]);
fu.extend_from_slice(chunk); out.push(fu.into());
}
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parameter_sets_and_small_nals_remain_in_order_without_start_codes() {
assert_eq!(payload(1200, &[0,0,0,1,64,1,9,0,0,1,66,1,8,0,0,1,38,1,7]).unwrap(),
vec![Bytes::from_static(&[64,1,9]), Bytes::from_static(&[66,1,8]), Bytes::from_static(&[38,1,7])]);
}
#[test]
fn fu_round_trip_preserves_header_payload_and_mtu_boundaries() {
for mtu in [4, 5, 7, 1190] {
let header = [0x27, 0x2b]; // IDR type19, nonzero layer and temporal id
let data: Vec<u8> = (0..2501).map(|i| (i % 251 + 1) as u8).collect();
let mut unit = vec![0,0,0,1]; unit.extend(header); unit.extend(&data);
let packets = payload(mtu, &unit).unwrap();
assert!(packets.len() > 1);
let mut rebuilt = vec![(packets[0][0] & 0x81) | ((packets[0][2] & 63) << 1), packets[0][1]];
for (i, p) in packets.iter().enumerate() {
assert!(p.len() <= mtu); assert_eq!((p[0] >> 1) & 63, 49);
assert_eq!(p[2] & 0x80 != 0, i == 0);
assert_eq!(p[2] & 0x40 != 0, i + 1 == packets.len());
rebuilt.extend_from_slice(&p[3..]);
}
assert_eq!(&rebuilt[..2], &header); assert_eq!(&rebuilt[2..], &data);
}
}
#[test]
fn rejects_invalid_headers_and_too_small_mtu() {
for bytes in [&[0,0,1,64][..], &[0,0,1,64,0][..], &[0,0,1,128,1][..], &[64,1][..]] {
assert!(payload(1200, bytes).is_err());
}
assert!(payload(3, &[0,0,1,64,1]).is_err());
}
}
+3 -2
View File
@@ -62,7 +62,7 @@ pub struct Screen<'a> {
pub fps: i32,
pub resolution: WinRect,
pub primary: bool,
/// Measured pixel/logical scale; never zero or guessed from a display name.
/// Pixel/logical scale of the advertised coordinate space (1.0 for capture-setting v4).
pub dpr: f64,
pub display_name: &'a str,
pub video_track_index: i32,
@@ -128,7 +128,8 @@ pub fn encode_screen_sources(seq: u64, timestamp: u64, screens: &[Screen<'_>], c
}
/// Message.which field 8 CaptureChange. Enum: desktop=0, window=1,
/// MuMu=2, hook=3, none=99. Caller confirms the active capture before emitting.
/// MuMu=2, hook=3, none=99. Declares the selected target, not frame readiness:
/// Android's unlock presenter requires desktop/window here even while locked.
pub fn encode_capture_change(seq: u64, timestamp: u64, capture_type: i32, type_value: i32, description: &str) -> Result<Vec<u8>, String> {
if !matches!(capture_type, 0 | 1 | 2 | 3 | 99) {
return Err("Unknown CaptureChange type".into());
+77 -26
View File
@@ -1,4 +1,4 @@
//! In-process X11 capture and H.264 encoding through libavdevice/libavcodec.
//! In-process X11 capture and H.264/HEVC encoding through libavdevice/libavcodec.
//! It replaces the `ffmpeg` child process with the same codec options, so the viewer
//! still receives the same Annex-B access units (AUD, SPS/PPS, IDR, P).
use std::ffi::CString;
@@ -11,6 +11,8 @@ use ff::ChannelLayout;
use ff::util::picture;
use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational};
use tokio::sync::mpsc;
use tokio::sync::{Semaphore, OwnedSemaphorePermit};
use std::collections::VecDeque;
use crate::damage_capture::DamageCapture;
@@ -25,6 +27,7 @@ pub struct Config {
pub max_bps: i64,
pub buffer_bits: i64,
pub encoder: crate::video::Encoder,
pub codec: crate::capture_setting::VideoCodec,
/// Capture on XDamage changes (XShm) instead of x11grab; `UU_CAPTURE=grab` turns it off.
pub damage: bool,
}
@@ -57,13 +60,38 @@ fn ensure_init() -> Result<(), String> {
result
}
/// Sends one access unit per encoder packet. Returns `Ok(false)` when the receiver is gone.
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<Vec<u8>>) -> Result<bool, String> {
pub struct EncodedUnit { pub data: Vec<u8>, pub permit: OwnedSemaphorePermit, pub completed_at: Instant }
fn acquire(budget: &Arc<Semaphore>, units: &mpsc::Sender<EncodedUnit>) -> Option<OwnedSemaphorePermit> {
tokio::runtime::Handle::current().block_on(async {
tokio::select! {
permit = budget.clone().acquire_owned() => permit.ok(),
_ = units.closed() => None,
}
})
}
#[cfg(test)]
mod budget_tests {
use super::*;
#[tokio::test]
async fn closing_encoder_output_wakes_a_blocked_capture() {
let budget=Arc::new(Semaphore::new(0));
let (tx,rx)=mpsc::channel(1);
let worker=tokio::task::spawn_blocking(move || acquire(&budget,&tx));
tokio::task::yield_now().await;
drop(rx);
assert!(tokio::time::timeout(Duration::from_secs(1),worker).await.unwrap().unwrap().is_none());
}
}
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<EncodedUnit>, permits: &mut VecDeque<OwnedSemaphorePermit>) -> Result<bool, String> {
loop {
match encoder.receive_packet(packet) {
Ok(()) => {
let data = packet.data().unwrap_or_default().to_vec();
if units.blocking_send(data).is_err() { return Ok(false); }
let permit = permits.pop_front().ok_or("Encoder returned an unbudgeted access unit")?;
if units.blocking_send(EncodedUnit {data, permit, completed_at: Instant::now()}).is_err() { return Ok(false); }
}
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => return Ok(true),
Err(error) => return Err(text(error)),
@@ -71,11 +99,11 @@ fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender
}
}
/// Opens the H.264 encoder for `config`. Returns the encoder, its input pixel format and output size.
/// Opens the selected video encoder for `config`. Returns the encoder, its input pixel format and output size.
fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
let (width, height) = config.output.unwrap_or(config.input);
let codec = encoder::find_by_name(config.encoder.name())
.ok_or("H.264 encoder is not available in this FFmpeg build")?;
let codec = encoder::find_by_name(config.encoder.name_for(config.codec))
.ok_or_else(|| format!("{} is not available in this FFmpeg build", config.encoder.name_for(config.codec)))?;
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
context.set_width(width);
@@ -99,15 +127,22 @@ fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), St
let cq = config.cq.to_string();
let gop = config.gop.to_string();
let mut options = Dictionary::new();
let hevc = config.codec == crate::capture_setting::VideoCodec::H265;
if config.encoder == crate::video::Encoder::Vaapi {
for (key,value) in [("profile", "constrained_baseline"), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
for (key,value) in [("profile", if hevc { "main" } else { "constrained_baseline" }), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
options.set("qmin", &cq);
} else if config.encoder == crate::video::Encoder::Nvenc {
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", "baseline"), ("rc", "vbr"),
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", if hevc { "main" } else { "baseline" }), ("rc", "vbr"),
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
options.set(key, value);
}
options.set("cq", &cq);
} else if hevc {
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "main"), ("threads", "4"), ("forced-idr", "1")] {
options.set(key, value);
}
options.set("crf", &cq);
options.set("x265-params", &crate::video::x265_params(&gop));
} else {
let x264 = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "baseline"), ("threads", "4")] {
@@ -160,11 +195,11 @@ fn send_video_frame(encoder: &mut encoder::Video, frame: &frame::Video, kind: cr
}
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
pub fn run(config: Config, units: mpsc::Sender<EncodedUnit>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>) -> Result<(), String> {
ensure_init()?;
if config.damage {
match DamageCapture::open(&config.display) {
Ok(capture) => return run_damage(config, capture, units, stop, idr),
Ok(capture) => return run_damage(config, capture, units, stop, idr, budget),
Err(error) => log::warn!("Damage-driven capture unavailable, using x11grab: {error}"),
}
}
@@ -187,14 +222,18 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None;
let mut decoded = frame::Video::empty();
let mut converted = frame::Video::new(pixel, width, height);
let mut converted = frame::Video::empty();
let mut packet = Packet::empty();
let started = Instant::now();
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
let mut last_at: Option<Duration> = None;
let mut last_slot: i64 = -1;
for (stream, input_packet) in input.packets() {
let mut permits = VecDeque::with_capacity(4);
let mut packets = input.packets();
loop {
if stop.load(Ordering::Relaxed) { return Ok(()); }
let Some(permit) = acquire(&budget, &units) else { return Ok(()); };
let Some((stream, input_packet)) = packets.next() else { return Ok(()); };
if stream.index() != index { continue; }
decoder.send_packet(&input_packet).map_err(text)?;
match decoder.receive_frame(&mut decoded) {
@@ -228,20 +267,19 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
frame.set_pts(Some(slot));
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
send_video_frame(&mut encoder, frame, config.encoder)?;
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
permits.push_back(permit);
if !drain(&mut encoder, &mut packet, &units, &mut permits)? { return Ok(()); }
}
Ok(())
}
/// Damage-driven loop. Damage only marks the picture dirty; a dirty picture is captured and encoded on
/// the next fixed frame tick, so the stream keeps a steady cadence while the screen moves. The viewer's
/// jitter buffer grows with irregular arrivals, and irregular sends were what raised its latency.
/// An idle screen sends a keepalive about once a second so the viewer still sees traffic.
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<EncodedUnit>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>) -> Result<(), String> {
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
let (cap_width, cap_height) = capture.size();
let mut captured = frame::Video::new(Pixel::BGRZ, cap_width, cap_height);
let mut converted = frame::Video::new(pixel, width, height);
let mut converted = frame::Video::empty();
let mut scaler = None;
let mut packet = Packet::empty();
let started = Instant::now();
@@ -253,6 +291,9 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
let mut next_tick = Instant::now();
let mut last_send = Instant::now();
let mut last_slot: i64 = -1;
let mut permits = VecDeque::with_capacity(4);
let locked = crate::session_lock::watch_locked();
let mut last_locked = *locked.borrow();
loop {
if stop.load(Ordering::Relaxed) { return Ok(()); }
let deadline = if dirty { next_tick } else { last_send + keepalive };
@@ -263,13 +304,20 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
let now = Instant::now();
if dirty && now < next_tick { continue; }
if !dirty && now < last_send + keepalive { continue; }
let Some(permit) = acquire(&budget, &units) else { return Ok(()); };
if stop.load(Ordering::Relaxed) { return Ok(()); }
dirty |= capture.wait(Duration::ZERO)?;
let state = *locked.borrow();
if state != last_locked { dirty = true; last_locked = state; }
let source = capture.capture()?;
let bytes_per_line = cap_width as usize * 4;
let stride = captured.stride(0);
let row = bytes_per_line.min(stride);
for (y, src) in source.chunks_exact(bytes_per_line).enumerate() {
captured.data_mut(0)[y * stride..y * stride + row].copy_from_slice(&src[..row]);
if dirty {
let source = capture.capture()?;
let bytes_per_line = cap_width as usize * 4;
let stride = captured.stride(0);
let row = bytes_per_line.min(stride);
for (y, src) in source.chunks_exact(bytes_per_line).enumerate() {
captured.data_mut(0)[y * stride..y * stride + row].copy_from_slice(&src[..row]);
}
}
let slot = ((started.elapsed().as_micros() * u128::from(config.fps) + 500_000) / 1_000_000) as i64;
@@ -282,13 +330,16 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
if scaler.is_none() {
scaler = Some(scaling::Context::get(Pixel::BGRZ, cap_width, cap_height, pixel, width, height, scaling::Flags::BILINEAR).map_err(text)?);
}
if let Some(context) = scaler.as_mut() { context.run(&captured, &mut converted).map_err(text)?; }
if dirty {
if let Some(context) = scaler.as_mut() { context.run(&captured, &mut converted).map_err(text)?; }
}
&mut converted
};
frame.set_pts(Some(slot));
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
send_video_frame(&mut encoder, frame, config.encoder)?;
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
permits.push_back(permit);
if !drain(&mut encoder, &mut packet, &units, &mut permits)? { return Ok(()); }
let now = Instant::now();
if dirty {
// Keep the tick phase; after a long idle, restart the phase from now instead of bursting to catch up.
+128 -2
View File
@@ -43,8 +43,12 @@ struct HeldInput { buttons: std::collections::BTreeSet<i32>, keys: std::collecti
/// Held modifier VKs (bit 0 Ctrl, 1 Shift, 2 Alt, 3 Win) and keys that got a synthetic Shift.
#[derive(Default)]
struct Shortcuts { modifiers: u8, shifted: std::collections::BTreeSet<i32> }
/// Mac viewer with 「使用 macOS 快捷键」 off sends Cmd as VK_LWIN (verified: Cmd 0x5B, Option 0xA4,
/// Ctrl 0xA2). Cmd is held back until we know whether it starts a Mac shortcut.
#[derive(Default)]
struct CmdState { held: bool, pending: bool, passthrough: bool, combos: std::collections::BTreeMap<i32, bool> }
pub struct InputInjector { serial:std::sync::Mutex<()>, owner:std::sync::Mutex<DesktopOwner>, shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
pub struct InputInjector { mac_controller: std::sync::atomic::AtomicBool, cmd: std::sync::Mutex<CmdState>, serial:std::sync::Mutex<()>, owner:std::sync::Mutex<DesktopOwner>, shortcuts: std::sync::Mutex<Shortcuts>, uinput: Option<crate::uinput::Devices>, screen: std::sync::Mutex<(u32,u32)>, portal_failures: std::sync::atomic::AtomicUsize, portal_outcomes: [std::sync::atomic::AtomicUsize; 3], portal:std::sync::Mutex<Option<std::sync::Arc<crate::portal::PortalSession>>>, backend: crate::desktop_backend::DesktopBackend, pending: std::sync::Mutex<PendingPointer>, held: std::sync::Mutex<HeldInput> }
impl InputInjector {
pub fn new() -> Self {
@@ -55,7 +59,7 @@ impl InputInjector {
Err(e) => { log::warn!("uinput unavailable ({}); using the desktop input path. See README for the udev rule.", e.kind()); None }
}
} else { None };
Self { serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
Self { mac_controller: std::sync::atomic::AtomicBool::new(false), cmd: std::sync::Mutex::new(CmdState::default()), serial:std::sync::Mutex::new(()), owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts: std::sync::Mutex::new(Shortcuts::default()), uinput, screen: std::sync::Mutex::new((0,0)), portal_failures: std::sync::atomic::AtomicUsize::new(0), portal_outcomes: std::array::from_fn(|_| std::sync::atomic::AtomicUsize::new(0)), portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11), pending: std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
}
pub fn bind_portal(&self,session:Option<std::sync::Arc<crate::portal::PortalSession>>) {
@@ -167,8 +171,62 @@ impl InputInjector {
shift
}
/// The current desktop controller is the macOS viewer (connect header controller_platform).
pub fn set_mac_controller(&self, mac: bool) {
self.mac_controller.store(mac, std::sync::atomic::Ordering::Relaxed);
*self.cmd.lock().unwrap() = CmdState::default();
}
fn terminal_focused(&self) -> bool {
self.backend == crate::desktop_backend::DesktopBackend::X11 && std::env::var_os("DISPLAY").is_some()
&& !std::env::var("UU_TERMINAL_SHORTCUTS").is_ok_and(|v| v == "0")
&& unsafe { x11_terminal_shortcut_state() } & 1 != 0
}
/// Mac habits for a Mac viewer with 「使用 macOS 快捷键」 off: Cmd+C/V/A/F/X/Z (the keys that option
/// lists) become Ctrl+key, and in a focused terminal Cmd+C/V become Ctrl+Shift+C/V (copy/paste)
/// while Ctrl+C still interrupts. Other Cmd combos, and Cmd alone, stay Super. Returns true when handled.
fn mac_cmd(&self, key: i32, is_press: bool) -> bool {
const SUPER_L: u64 = 0xffeb; const CONTROL_L: u64 = 0xffe3; const SHIFT_L: u64 = 0xffe1;
let mut st = self.cmd.lock().unwrap();
if matches!(key, 0x5b | 0x5c) {
if is_press { st.held = true; st.pending = true; st.passthrough = false; }
else {
if st.passthrough { self.inject_key(SUPER_L, false); }
else if st.pending { self.inject_key(SUPER_L, true); self.inject_key(SUPER_L, false); }
st.held = false; st.pending = false; st.passthrough = false;
}
return true;
}
if !is_press {
let Some(shifted) = st.combos.remove(&key) else { return false };
self.key_symbol_event(key, false);
if shifted { self.inject_key(SHIFT_L, false); }
self.inject_key(CONTROL_L, false);
return true;
}
if !st.held { return false; }
// Shift/Option/Ctrl pressed with Cmd (e.g. Cmd+Shift+Z) must not decide the combo yet.
if matches!(key, 0x10..=0x12 | 0xa0..=0xa5) { return false; }
if st.combos.contains_key(&key) { self.key_symbol_event(key, true); return true; } // autorepeat
if matches!(key, 0x41 | 0x43 | 0x46 | 0x56 | 0x58 | 0x5a) && !st.passthrough {
st.pending = false;
let shifted = matches!(key, 0x43 | 0x56) && self.terminal_focused();
self.inject_key(CONTROL_L, true);
if shifted { self.inject_key(SHIFT_L, true); }
self.key_symbol_event(key, true);
st.combos.insert(key, shifted);
static NOTED: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
if NOTED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 { log::info!("Mac shortcut: Cmd combo sent as Ctrl{}", if shifted { "+Shift (terminal)" } else { "" }); }
return true;
}
if st.pending { self.inject_key(SUPER_L, true); st.pending = false; st.passthrough = true; }
false
}
pub fn windows_key_event(&self, key: i32, is_press: bool) {
let _serial=self.serial.lock().unwrap();
if self.mac_controller.load(std::sync::atomic::Ordering::Relaxed) && self.mac_cmd(key, is_press) { return; }
if self.terminal_shift(key, is_press) {
const SHIFT_L: u64 = 0xffe1;
if is_press {
@@ -207,6 +265,8 @@ impl InputInjector {
unsafe {x11_release_all();}
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland && self.portal.lock().unwrap().is_none() {
crate::wayland::release_all();
} else if self.backend==crate::desktop_backend::DesktopBackend::Portal && self.portal.lock().unwrap().is_none() {
// Nothing is bound yet (e.g. the first locked connection): no input can be held.
} else {
// Portal/Wayland cannot query global state. Sweep every key/button
// that this host can emit through the currently bound session.
@@ -221,6 +281,7 @@ impl InputInjector {
}
*held=HeldInput::default();
*self.shortcuts.lock().unwrap()=Shortcuts::default();
*self.cmd.lock().unwrap()=CmdState::default();
}
// Per-injector budgets cover missing bindings, locked sessions, stale handles
// and D-Bus errors. Never include keys, coordinates, text or portal tokens.
@@ -386,6 +447,15 @@ impl InputInjector {
}
}
impl DesktopInputSession {
pub fn is_current(&self) -> bool {
let owner = self.input.owner.lock().unwrap();
owner.owns(self.id)
}
/// Call only inside with_current or with_input, so a superseded peer cannot change bindings.
pub fn bind_portal(&self, portal: Option<std::sync::Arc<crate::portal::PortalSession>>) {
self.input.bind_portal(portal);
}
pub fn release_input(&self) { self.input.release_held(); }
pub fn connected(&self) {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && !owner.active {self.input.release_held();owner.active=true;}
@@ -394,6 +464,11 @@ impl DesktopInputSession {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {owner.active=false;self.input.release_held();}
}
/// Lifecycle updates survive a transient disconnect, but serialize with replacement and shutdown.
pub fn with_current(&self, action:impl FnOnce()) {
let owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) {action();}
}
pub fn with_input(&self, action:impl FnOnce()) {
let owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {action();}
@@ -401,11 +476,62 @@ impl DesktopInputSession {
}
impl Drop for DesktopInputSession {fn drop(&mut self) {self.disconnected();}}
#[cfg(test)]
impl InputInjector {
pub fn without_desktop() -> Self {
Self { mac_controller:std::sync::atomic::AtomicBool::new(false),
cmd:std::sync::Mutex::new(CmdState::default()), serial:std::sync::Mutex::new(()),
owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts:std::sync::Mutex::new(Shortcuts::default()),
uinput:None, screen:std::sync::Mutex::new((0,0)), portal_failures:std::sync::atomic::AtomicUsize::new(0),
portal_outcomes:std::array::from_fn(|_|std::sync::atomic::AtomicUsize::new(0)),
portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::DesktopBackend::Portal,
pending:std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
}
}
#[cfg(test)]
mod lifecycle_tests {
use super::*;
use std::sync::{Arc,atomic::{AtomicUsize,Ordering}};
#[test]
fn current_owner_can_publish_while_disconnected_but_cannot_inject() {
let input=Arc::new(InputInjector::without_desktop());
let session=input.desktop_session();
session.connected();session.disconnected();
let publications=AtomicUsize::new(0);
session.with_current(||{publications.fetch_add(1,Ordering::Relaxed);});
session.with_input(||panic!("disconnected owner must not inject"));
assert_eq!(publications.load(Ordering::Relaxed),1);
session.connected();
session.with_input(||{publications.fetch_add(1,Ordering::Relaxed);});
let next=input.desktop_session();
session.with_current(||panic!("replaced owner must not publish"));
next.with_current(||{publications.fetch_add(1,Ordering::Relaxed);});
input.shutdown();
next.with_current(||panic!("shutdown must reject lifecycle updates"));
assert_eq!(publications.load(Ordering::Relaxed),3);
}
#[test]
fn current_owner_gate_holds_ownership_through_publication() {
let input=Arc::new(InputInjector::without_desktop());
let session=input.desktop_session();
let (started,ready)=std::sync::mpsc::channel();
let (finish,resume)=std::sync::mpsc::channel();
let worker=session.clone();
let publication=std::thread::spawn(move ||worker.with_current(|| {
started.send(()).unwrap();resume.recv().unwrap();
}));
ready.recv_timeout(std::time::Duration::from_secs(2)).unwrap();
assert!(input.owner.try_lock().is_err());
let replacing=input.clone();
let replacement=std::thread::spawn(move ||replacing.desktop_session());
finish.send(()).unwrap();
publication.join().unwrap();
let next=replacement.join().unwrap();
assert!(next.is_current());
session.with_current(||panic!("late publication after replacement"));
}
#[test]
fn cleanup_discards_motion_shortcuts_and_untracked_input_ledger() {
let input=Arc::new(InputInjector::new());
input.held.lock().unwrap().keys.insert(0xffe1);
+101
View File
@@ -0,0 +1,101 @@
//! Black video keeps locked Portal initialization reachable by the iPhone viewer.
use std::time::Duration;
use bytes::Bytes;
use webrtc::{media::Sample, peer_connection::{RTCPeerConnection, peer_connection_state::RTCPeerConnectionState}};
use crate::{capture_setting::VideoCodec, desktop_session::PortalDesktop, video_track::VideoTrack};
async fn black_frame(codec: VideoCodec) -> Result<Bytes, String> {
let mut command = crate::video::ffmpeg_command();
command.args(["-hide_banner", "-loglevel", "error", "-nostdin",
"-f", "lavfi", "-i", "color=c=black:s=1280x720:r=1", "-frames:v", "1",
"-an", "-pix_fmt", "yuv420p", "-threads", "1", "-preset", "ultrafast", "-tune", "zerolatency"]);
match codec {
VideoCodec::H264 => { command.args(["-c:v", "libx264", "-profile:v", "baseline",
"-x264-params", "keyint=1:repeat-headers=1:aud=1", "-f", "h264"]); },
VideoCodec::H265 => { command.args(["-c:v", "libx265",
"-x265-params", "keyint=1:repeat-headers=1:aud=1:pools=1:frame-threads=1:log-level=error", "-f", "hevc"]); },
}
command.arg("pipe:1").kill_on_drop(true);
let output = tokio::time::timeout(Duration::from_secs(5), command.output()).await
.map_err(|_| "Locked desktop video encoder timed out".to_string())?
.map_err(|error| format!("Locked desktop video encoder: {error}"))?;
if !output.status.success() || output.stdout.is_empty() {
return Err(format!("Locked desktop video encoder failed: status={}", output.status));
}
Ok(output.stdout.into())
}
pub async fn run(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop) -> Result<(), String> {
run_with_encoder(pc, track, desktop, black_frame).await
}
async fn run_with_encoder<F, Fut>(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop, mut encode: F) -> Result<(), String>
where F: FnMut(VideoCodec) -> Fut, Fut: std::future::Future<Output = Result<Bytes, String>> {
// #29: iPhone 4.42.0 cold ON/OFF/ON tests only sent action15 after black video.
// Keep CONTROL permissions truthful and use the existing video track until real capture is ready.
let mut updates = desktop.subscribe();
let mut locked = desktop.lock_state();
let mut tick = tokio::time::interval(Duration::from_secs(1));
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut frame = None;
let mut sent = 0u64;
let mut attempts = 0u64;
loop {
let state = updates.borrow_and_update().clone();
let _ = locked.borrow_and_update();
if state.ready() || !state.waiting || crate::video::transport_closed(pc) {
if attempts > 0 { log::info!("Locked desktop video ended: sent={sent} attempts={attempts}"); }
return Ok(());
}
tokio::select! {
biased;
changed = updates.changed() => if changed.is_err() { return Ok(()); },
changed = locked.changed() => if changed.is_err() { return Ok(()); },
_ = tick.tick() => {
if !updates.borrow().waiting || *locked.borrow() != Some(true)
|| pc.connection_state() != RTCPeerConnectionState::Connected { continue; }
if frame.is_none() {
let codec = crate::capture_setting::preferred_codec(
track.supports(VideoCodec::H264), track.supports(VideoCodec::H265));
let encoded = tokio::select! {
biased;
_ = updates.changed() => continue,
_ = locked.changed() => continue,
result = encode(codec) => result?,
};
frame = Some((codec, encoded));
}
if crate::video::transport_closed(pc) { return Ok(()); }
// Encoding can finish in the same poll that publishes unlock/readiness.
if !updates.borrow().waiting || *locked.borrow() != Some(true)
|| pc.connection_state() != RTCPeerConnectionState::Connected { continue; }
let (codec, data) = frame.as_ref().unwrap();
let sample = Sample { data: data.clone(), duration: Duration::from_secs(1), ..Default::default() };
let extensions = [crate::rtp_metadata::capture_index(0), crate::rtp_metadata::new_frame()];
let revision = track.revision();
// No monitor inventory or permission claims: 1280x720 is only this synthetic bitstream.
tokio::select! {
biased;
_ = updates.changed() => continue,
_ = locked.changed() => continue,
result = tokio::time::timeout(Duration::from_secs(2),
track.write_sample_with_extensions(*codec, &sample, &extensions)) => {
if crate::video::transport_closed(pc) { return Ok(()); }
result.map_err(|_| "Locked desktop video send timed out".to_string())?
.map_err(|error| format!("Locked desktop video send: {error}"))?;
},
}
attempts += 1;
if track.revision() > revision { sent += 1; }
tick.reset();
if attempts == 1 || attempts % 10 == 0 {
log::info!("Locked desktop video: sent={sent} codec={codec:?} synthetic_size=1280x720 bytes={} attempts={attempts}", data.len());
}
},
}
}
}
#[cfg(test)]
#[path = "locked_video_tests.rs"]
mod tests;
+350
View File
@@ -0,0 +1,350 @@
use super::{black_frame, run, run_with_encoder};
use crate::{
capture::ScreenCapture,
capture_setting::VideoCodec,
desktop_backend::DesktopBackend,
desktop_session::PortalDesktop,
video_track::VideoTrack,
};
use std::{process::Stdio, sync::Arc, time::Duration};
use tokio::{io::AsyncWriteExt, sync::{mpsc, watch}};
use webrtc::{
api::{media_engine::MediaEngine, APIBuilder},
media::Sample,
peer_connection::{
configuration::RTCConfiguration,
peer_connection_state::RTCPeerConnectionState,
RTCPeerConnection,
},
rtp::packet::Packet,
rtp_transceiver::{
rtp_codec::{RTCRtpCodecCapability, RTCRtpCodecParameters, RTPCodecType},
},
util::vnet::router::Router,
};
async fn decode_gray(data: &[u8], demuxer: &str) -> (Vec<u8>, String) {
let mut command = crate::video::ffmpeg_command();
command.args([
"-hide_banner", "-loglevel", "info", "-nostdin", "-f", demuxer, "-i", "pipe:0",
"-frames:v", "1", "-vf", "scale=in_range=tv:out_range=pc,format=gray,showinfo",
"-pix_fmt", "gray", "-f", "rawvideo", "pipe:1",
]);
command.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).kill_on_drop(true);
let mut child = command.spawn().expect("start FFmpeg decoder");
let mut stdin = child.stdin.take().expect("FFmpeg decoder stdin");
let output = tokio::time::timeout(Duration::from_secs(10), async move {
stdin.write_all(data).await.expect("write encoded frame to FFmpeg");
drop(stdin);
child.wait_with_output().await.expect("wait for FFmpeg decoder")
}).await.expect("FFmpeg decoder timed out");
assert!(output.status.success(), "FFmpeg decoder failed: status={}", output.status);
(output.stdout, String::from_utf8_lossy(&output.stderr).into_owned())
}
async fn black_frame_decodes_as_1280x720_black(codec: VideoCodec, demuxer: &str) {
let encoded = black_frame(codec).await.expect("encode black frame");
assert!(!encoded.is_empty());
let (gray, diagnostics) = decode_gray(&encoded, demuxer).await;
assert!(diagnostics.contains("s:1280x720"), "decoded dimensions were not 1280x720");
assert_eq!(gray.len(), 1280 * 720, "expected one 1280x720 gray frame");
assert!(gray.iter().all(|&pixel| pixel <= 1), "decoded frame was not black");
}
#[tokio::test]
#[ignore = "requires FFmpeg libx264/libx265"]
async fn h264_black_frame_decodes_to_1280x720_black() {
black_frame_decodes_as_1280x720_black(VideoCodec::H264, "h264").await;
}
#[tokio::test]
#[ignore = "requires FFmpeg libx264/libx265"]
async fn h265_black_frame_decodes_to_1280x720_black() {
black_frame_decodes_as_1280x720_black(VideoCodec::H265, "hevc").await;
}
fn ready_screen() -> Arc<ScreenCapture> {
let mut screen = ScreenCapture::unavailable("synthetic ready desktop".into());
screen.backend = DesktopBackend::Portal;
screen.init_error = None;
screen.display = "wayland:test".into();
screen.width = 1280;
screen.height = 720;
Arc::new(screen)
}
async fn receive_frame(packets: &mut mpsc::UnboundedReceiver<Packet>, within: Duration) -> Vec<Packet> {
tokio::time::timeout(within, async {
let mut frame = Vec::new();
loop {
let packet = packets.recv().await.expect("RTP packet receiver closed");
let marker = packet.header.marker;
frame.push(packet);
if marker {
return frame;
}
}
}).await.expect("timed out waiting for an RTP frame")
}
async fn assert_no_rtp(packets: &mut mpsc::UnboundedReceiver<Packet>, within: Duration) {
assert!(tokio::time::timeout(within, packets.recv()).await.is_err(),
"locked video task sent RTP while transmission was disabled");
}
fn assert_single_rtp_frame(frame: &[Packet]) {
let first = frame.first().expect("RTP frame has packets");
let last = frame.last().unwrap();
assert!(last.header.marker, "RTP frame ends with a marker packet");
for pair in frame.windows(2) {
assert_eq!(pair[0].header.ssrc, pair[1].header.ssrc);
assert_eq!(pair[0].header.timestamp, pair[1].header.timestamp);
assert_eq!(pair[1].header.sequence_number, pair[0].header.sequence_number.wrapping_add(1));
}
assert!(frame.iter().all(|packet| packet.header.ssrc == first.header.ssrc));
}
struct VirtualPeers {
host: Arc<RTCPeerConnection>,
remote: Arc<RTCPeerConnection>,
_control: Arc<webrtc::data_channel::RTCDataChannel>,
router: Arc<tokio::sync::Mutex<Router>>,
track: Arc<VideoTrack>,
packets: mpsc::UnboundedReceiver<Packet>,
}
impl VirtualPeers {
async fn new(codec: VideoCodec) -> Self {
let (remote_setting, host_setting, router) = crate::test_network::virtual_network().await;
let mut remote_media = MediaEngine::default();
match codec {
VideoCodec::H264 => remote_media.register_codec(RTCRtpCodecParameters {
capability: RTCRtpCodecCapability {
mime_type: VideoCodec::H264.mime().into(),
clock_rate: 90_000,
sdp_fmtp_line: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f".into(),
..Default::default()
},
payload_type: 102,
..Default::default()
}, RTPCodecType::Video).unwrap(),
VideoCodec::H265 => crate::video_track::register_hevc(&mut remote_media).unwrap(),
}
let remote = Arc::new(APIBuilder::new().with_setting_engine(remote_setting)
.with_media_engine(remote_media).build()
.new_peer_connection(RTCConfiguration::default()).await.unwrap());
remote.add_transceiver_from_kind(RTPCodecType::Video, None).await.unwrap();
let control = remote.create_data_channel("CONTROL_DATA_CHANNEL", None).await.unwrap();
let mut host_media = MediaEngine::default();
host_media.register_default_codecs().unwrap();
crate::video_track::register_hevc(&mut host_media).unwrap();
let host = Arc::new(APIBuilder::new().with_setting_engine(host_setting)
.with_media_engine(host_media).build()
.new_peer_connection(RTCConfiguration::default()).await.unwrap());
let track = Arc::new(VideoTrack::new());
host.add_track(track.clone()).await.unwrap();
let (packet_tx, packets) = mpsc::unbounded_channel();
remote.on_track(Box::new(move |remote_track, _, _| {
let packet_tx = packet_tx.clone();
Box::pin(async move {
tokio::spawn(async move {
while let Ok((packet, _)) = remote_track.read_rtp().await {
if packet_tx.send(packet).is_err() { break; }
}
});
})
}));
Self { host, remote, _control: control, router, track, packets }
}
async fn connect(&self, codec: VideoCodec) {
let offer = self.remote.create_offer(None).await.unwrap();
let mut gathered = self.remote.gathering_complete_promise().await;
self.remote.set_local_description(offer).await.unwrap();
gathered.recv().await;
self.host.set_remote_description(self.remote.local_description().await.unwrap()).await.unwrap();
let answer = self.host.create_answer(None).await.unwrap();
let mut gathered = self.host.gathering_complete_promise().await;
self.host.set_local_description(answer).await.unwrap();
gathered.recv().await;
self.remote.set_remote_description(self.host.local_description().await.unwrap()).await.unwrap();
tokio::time::timeout(Duration::from_secs(15), async {
while self.host.connection_state() != RTCPeerConnectionState::Connected
|| !self.track.supports(codec)
{
tokio::time::sleep(Duration::from_millis(10)).await;
}
}).await.expect("virtual WebRTC peers did not connect with the requested codec");
assert!(!self.track.supports(match codec {
VideoCodec::H264 => VideoCodec::H265,
VideoCodec::H265 => VideoCodec::H264,
}));
}
async fn close(self) {
self.host.close().await.unwrap();
self.remote.close().await.unwrap();
self.router.lock().await.stop().await.unwrap();
}
}
async fn exercise_locked_video(codec: VideoCodec) {
tokio::time::timeout(Duration::from_secs(35), async {
let mut peers = VirtualPeers::new(codec).await;
let (lock_tx, lock_rx) = watch::channel(Some(true));
let desktop = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
lock_rx,
));
assert!(desktop.snapshot().waiting);
assert!(!desktop.snapshot().ready());
let run_host = peers.host.clone();
let run_track = peers.track.clone();
let run_desktop = desktop.clone();
let task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
assert_ne!(peers.host.connection_state(), RTCPeerConnectionState::Connected);
assert_no_rtp(&mut peers.packets, Duration::from_millis(150)).await;
peers.connect(codec).await;
let locked_frame = receive_frame(&mut peers.packets, Duration::from_secs(5)).await;
assert_single_rtp_frame(&locked_frame);
let last_locked_packet = locked_frame.last().unwrap().clone();
lock_tx.send_replace(None);
assert_no_rtp(&mut peers.packets, Duration::from_millis(1200)).await;
lock_tx.send_replace(Some(false));
assert_no_rtp(&mut peers.packets, Duration::from_millis(1200)).await;
desktop.publish_test_screen(ready_screen());
tokio::time::timeout(Duration::from_secs(2), task).await
.expect("locked video task did not stop when the desktop became ready")
.expect("locked video task panicked")
.expect("locked video task returned an error");
let live_data = black_frame(codec).await.expect("encode live test sample");
peers.track.write_sample_with_extensions(codec, &Sample {
data: live_data,
duration: Duration::from_millis(20),
..Default::default()
}, &[]).await.unwrap();
let live_frame = receive_frame(&mut peers.packets, Duration::from_secs(5)).await;
assert_single_rtp_frame(&live_frame);
let first_live_packet = &live_frame[0];
assert_eq!(first_live_packet.header.ssrc, last_locked_packet.header.ssrc);
assert_eq!(first_live_packet.header.sequence_number,
last_locked_packet.header.sequence_number.wrapping_add(1));
assert_eq!(first_live_packet.header.timestamp,
last_locked_packet.header.timestamp.wrapping_add(90_000));
peers.close().await;
}).await.expect("locked video WebRTC test exceeded its deadline");
}
#[tokio::test]
#[ignore = "requires FFmpeg libx264/libx265"]
async fn locked_video_gates_on_pending_locked_connected_and_preserves_video_timeline() {
exercise_locked_video(VideoCodec::H264).await;
exercise_locked_video(VideoCodec::H265).await;
}
#[tokio::test]
async fn locked_video_rechecks_state_when_encoding_finishes() {
#[derive(Clone, Copy)]
enum Transition { Unlocked, Unknown, Ready }
tokio::time::timeout(Duration::from_secs(20), async {
let mut peers = VirtualPeers::new(VideoCodec::H264).await;
peers.connect(VideoCodec::H264).await;
for transition in [Transition::Unlocked, Transition::Unknown, Transition::Ready] {
let (lock_tx, lock_rx) = watch::channel(Some(true));
let desktop = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
lock_rx,
));
let run_host = peers.host.clone();
let run_track = peers.track.clone();
let run_desktop = desktop.clone();
let (encoded_tx, mut encoded_rx) = mpsc::unbounded_channel();
let revision = peers.track.revision();
let task = tokio::spawn(async move {
run_with_encoder(&run_host, &run_track, &run_desktop, |_| {
let desktop = run_desktop.clone();
let lock_tx = lock_tx.clone();
let encoded_tx = encoded_tx.clone();
async move {
// Change state during the encoder's final poll, before it returns Ready.
match transition {
Transition::Unlocked => { lock_tx.send_replace(Some(false)); },
Transition::Unknown => { lock_tx.send_replace(None); },
Transition::Ready => desktop.publish_test_screen(ready_screen()),
}
encoded_tx.send(()).unwrap();
Ok(bytes::Bytes::from_static(&[0, 0, 1, 65, 1, 2]))
}
}).await
});
tokio::time::timeout(Duration::from_secs(2), encoded_rx.recv()).await
.expect("test encoder was not polled").expect("test encoder channel closed");
assert_no_rtp(&mut peers.packets, Duration::from_millis(250)).await;
assert_eq!(peers.track.revision(), revision, "stale state allowed a synthetic frame");
desktop.publish_test_screen(ready_screen());
tokio::time::timeout(Duration::from_secs(2), task).await
.expect("locked video task did not stop after readiness")
.expect("locked video task panicked")
.expect("locked video task returned an error");
}
peers.close().await;
}).await.expect("locked video concurrent-transition test exceeded its deadline");
}
#[tokio::test]
async fn locked_video_stops_on_capture_rejection_and_terminal_peer_close() {
tokio::time::timeout(Duration::from_secs(25), async {
let mut peers = VirtualPeers::new(VideoCodec::H264).await;
peers.connect(VideoCodec::H264).await;
let (_lock_tx, lock_rx) = watch::channel(Some(false));
let rejected_desktop = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
lock_rx.clone(),
));
let run_host = peers.host.clone();
let run_track = peers.track.clone();
let run_desktop = rejected_desktop.clone();
let rejection_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
rejected_desktop.publish_test_screen(
Arc::new(ScreenCapture::unavailable("synthetic capture rejection".into())),
);
assert_eq!(rejected_desktop.snapshot().permission(true),
crate::system_metrics::Permission::Reject);
tokio::time::timeout(Duration::from_secs(2), rejection_task).await
.expect("locked video task did not stop after capture rejection")
.expect("locked video task panicked")
.expect("locked video task returned an error");
let pending_desktop = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
lock_rx,
));
let run_host = peers.host.clone();
let run_track = peers.track.clone();
let run_desktop = pending_desktop;
let close_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
peers.host.close().await.unwrap();
tokio::time::timeout(Duration::from_secs(2), close_task).await
.expect("locked video task did not stop after terminal peer close")
.expect("locked video task panicked")
.expect("locked video task returned an error");
peers.packets.close();
assert!(peers.packets.try_recv().is_err(), "locked video task sent RTP while unlocked");
peers.remote.close().await.unwrap();
peers.router.lock().await.stop().await.unwrap();
}).await.expect("locked video lifecycle test exceeded its deadline");
}
+119 -29
View File
@@ -2,6 +2,8 @@
//! Zero-Wine, Zero-Python, Single-Binary, 5 Native Tools Integrated
mod desktop_backend;
mod desktop_session;
mod desktop_power;
mod portal;
mod network_diagnostics;
mod session_ice;
@@ -24,6 +26,11 @@ mod signaling;
mod terminal;
mod webrtc;
mod video;
mod video_track;
mod locked_video;
#[cfg(test)]
mod test_network;
mod hevc;
#[cfg(feature = "inproc-av")]
mod damage_capture;
#[cfg(feature = "inproc-av")]
@@ -56,6 +63,52 @@ use webrtc::WebRtcSession;
/// Set before a normal return when the host must come back in the other mode (signed in / out).
static RESTART: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
fn superseded_peer_ids(sessions: &HashMap<String, WebRtcSession>, ice: &str, desktop: bool) -> Vec<String> {
sessions.iter().filter(|(id, peer)| id.as_str() == ice || (desktop && peer.desktop))
.map(|(id, _)| id.clone()).collect()
}
fn released_peer_ids<T>(sessions: &HashMap<String, T>, ice: Option<&serde_json::Value>) -> Vec<String> {
let ice = ice.map(|id| id.as_str().map(str::to_owned).unwrap_or_else(|| id.to_string()));
sessions.keys().filter(|id| ice.as_deref().map_or(true, |ice| id.as_str() == ice)).cloned().collect()
}
#[cfg(test)]
mod peer_release_tests {
use super::*;
#[test]
fn explicit_releases_leave_independent_and_replacement_peers_alive() {
let mut peers = HashMap::from([
("old-desktop".to_owned(), ()), ("files".to_owned(), ()),
("terminal".to_owned(), ()), ("port-mapping".to_owned(), ()),
]);
let targets = released_peer_ids(&peers, Some(&serde_json::json!("old-desktop")));
assert_eq!(targets, ["old-desktop"]);
for id in targets { peers.remove(&id); }
peers.insert("new-desktop".to_owned(), ());
assert!(released_peer_ids(&peers, Some(&serde_json::json!("old-desktop"))).is_empty());
assert!(released_peer_ids(&peers, Some(&serde_json::json!("unknown"))).is_empty());
for id in ["files", "terminal", "port-mapping", "new-desktop"] {
assert_eq!(released_peer_ids(&peers, Some(&serde_json::json!(id))), [id]);
}
assert_eq!(peers.len(), 4);
}
#[test]
fn release_without_an_ice_id_retains_room_wide_cleanup() {
let peers = HashMap::from([("desktop".to_owned(), ()), ("files".to_owned(), ())]);
let mut targets = released_peer_ids(&peers, None);
targets.sort();
assert_eq!(targets, ["desktop", "files"]);
}
#[test]
fn explicit_non_string_ice_ids_never_become_room_wide_releases() {
let peers = HashMap::from([("123".to_owned(), ()), ("files".to_owned(), ())]);
for id in [serde_json::json!(999), serde_json::Value::Null, serde_json::json!(false), serde_json::json!({})] {
assert!(released_peer_ids(&peers, Some(&id)).is_empty());
}
assert_eq!(released_peer_ids(&peers, Some(&serde_json::json!(123))), ["123"]);
}
}
/// Re-executes this host; the new instance opens its window so the user sees the result.
fn restart_host() -> ! {
use std::os::unix::process::CommandExt;
@@ -63,14 +116,19 @@ fn restart_host() -> ! {
eprintln!("restart failed: {error}");
std::process::exit(1);
}
/// systemd user unit running this process (from /proc/self/cgroup), e.g. "uuremote-native.service".
fn systemd_unit() -> Option<String> {
std::env::var_os("INVOCATION_ID")?;
let cgroup = std::fs::read_to_string("/proc/self/cgroup").ok()?;
cgroup.lines().find_map(|l| l.rsplit('/').next().filter(|u| u.ends_with(".service")).map(str::to_owned))
}
fn display_present() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() }
/// SIGUSR1 (`uuremote-server show`) opens the host window.
fn present_on_usr1(ui: std::sync::Arc<assist::LocalUi>) {
// Install the handler before publishing the pid: SIGUSR1's default action terminates the process.
let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) else { return };
write_host_pid();
tokio::spawn(async move {
let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) else { return };
while usr1.recv().await.is_some() { ui.present(); }
});
tokio::spawn(async move { while usr1.recv().await.is_some() { ui.present(); } });
}
fn host_pid_path() -> std::path::PathBuf { config::path().with_file_name("host.pid") }
@@ -94,6 +152,24 @@ fn show_running_host() -> Result<(), Box<dyn std::error::Error>> {
if let Some(pid) = running {
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
}
// Stopped from the tray but installed as a user service: start the service (not a second,
// unmanaged host), wait for it to publish its pid, then ask it to show the window.
let unit = std::env::var("UU_SERVICE_UNIT").unwrap_or_else(|_| "uuremote-native.service".into());
let installed = std::process::Command::new("systemctl").args(["--user", "cat", &unit])
.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).status().is_ok_and(|s| s.success());
if installed && std::process::Command::new("systemctl").args(["--user", "start", &unit]).status().is_ok_and(|s| s.success()) {
let before = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok();
for _ in 0..60 {
std::thread::sleep(std::time::Duration::from_millis(500));
let fresh = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok().is_some_and(|t| Some(t) != before);
let pid = std::fs::read_to_string(host_pid_path()).ok().and_then(|s| s.trim().parse::<i32>().ok());
if let (true, Some(pid)) = (fresh, pid) {
std::thread::sleep(std::time::Duration::from_millis(500)); // signal handler installed right after the pid
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
}
}
return Err("the UU remote host service started but did not open its window".into());
}
use std::os::unix::process::CommandExt;
Err(Box::new(std::process::Command::new("/proc/self/exe").env("UU_SHOW_WINDOW", "1").exec()))
}
@@ -207,6 +283,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut frame = vec![0; screen.width as usize * screen.height as usize * 4];
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
loop {
if *locked.borrow() == Some(false) {
session.recover(&std::sync::atomic::AtomicBool::new(false)).map_err(|error| error.to_string())?;
}
session.capture(&mut frame)?;
if *locked.borrow() == Some(false) && session.is_alive() { break; }
if std::time::Instant::now() >= deadline { return Err("Test VM portal did not recover".into()); }
@@ -293,6 +372,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
return Ok(());
}
if std::env::args().nth(1).as_deref() == Some("desktop-video-probe") {
if let Some(input)=std::env::var_os("UU_PROBE_INPUT") {
return video::probe_input(input,std::env::args().nth(2)).await.map_err(|e|e.into());
}
let screen=ScreenCapture::new()?;
capture::keep_display_awake()?;
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),std::env::args().nth(2)).await.map_err(|e|e.into());
@@ -326,9 +408,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let capture = Arc::new((if desktop_backend::headless() {
Err("headless mode (UU_DESKTOP_BACKEND=none)".to_owned())
} else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
&& std::env::var("UU_PORTAL_PREAUTHORIZE").ok().as_deref()!=Some("1") {
Err("Portal screen access requires a desktop connection and local consent".to_owned())
} else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal {
Err("Portal capture waits for a desktop CONTROL connection".to_owned())
} else { ScreenCapture::new() }).unwrap_or_else(|error| {
log::warn!("Desktop unavailable; terminal and file tools remain available: {error}");
ScreenCapture::unavailable(error)
@@ -394,6 +475,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutdown: &mut tokio::sync::watch::Receiver<bool>,
assist: &mut Option<assist::Assist>, commands: &mut mpsc::Receiver<assist::Command>, webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>>) -> Result<(), Box<dyn std::error::Error>> {
let portal_desktop = (capture.backend == desktop_backend::DesktopBackend::Portal && !desktop_backend::headless())
.then(|| Arc::new(desktop_session::PortalDesktop::new(capture.clone())));
let mut auth = if let Some(session) = assist.as_ref() { session.client.clone() } else {
let creds = match config::file().and_then(Credentials::load) {
Ok(creds) => creds,
@@ -426,6 +509,8 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
// One peer per viewer connection (desktop, file transfer, terminal...), keyed by ice_id.
let mut next_capture_type: Option<(u64,crate::session_ice::SessionIce)> = None;
let mut capture_types: HashMap<String, (u64,crate::session_ice::SessionIce)> = HashMap::new();
let mut mac_by_ice: HashMap<String, bool> = HashMap::new();
let mut next_mac = false;
loop {
if *shutdown.borrow() {
@@ -522,6 +607,14 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
SignalingEvent::AssistCommand(assist::Command::Verify(v)) => { if let Some(s) = assist.as_mut() { s.set_verify(v)?; } }
SignalingEvent::AssistCommand(assist::Command::Refresh(r)) => { if let Some(s) = assist.as_mut() { s.set_refresh(r)?; } }
SignalingEvent::AssistCommand(assist::Command::Custom(c)) => { if let Some(s) = assist.as_mut() { s.set_custom(c)?; } }
SignalingEvent::AssistCommand(assist::Command::Quit) => {
// Tray 退出: under systemd (Restart=always) stop the unit itself, else just exit.
log::info!("Quit requested from the tray");
if let Some(unit) = systemd_unit() {
let _ = std::process::Command::new("systemctl").args(["--user", "stop", "--no-block", &unit]).status();
}
return Ok(());
}
SignalingEvent::AssistCommand(assist::Command::Logout) => {
if let Some(s) = assist.as_mut().filter(|s| s.is_account()) {
s.logout().await;
@@ -579,7 +672,9 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
.and_then(|s| s.as_str())
);
}
SignalingEvent::ConnectRequest { capture_type, ice_id, transport } => {
SignalingEvent::ConnectRequest { capture_type, ice_id, transport, controller_mac } => {
if mac_by_ice.len()>=32 { mac_by_ice.clear(); }
match ice_id.clone() { Some(id) => { mac_by_ice.insert(id, controller_mac); } None => next_mac = controller_mac }
// Pending cloud credentials live only within this room; cap
// requests that never receive an offer rather than retain them.
if capture_types.len()>=32 { capture_types.clear(); }
@@ -610,24 +705,21 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
let (capture_type,transport) = capture_types.remove(&ice).or_else(|| next_capture_type.take()).unwrap_or((1,crate::session_ice::SessionIce::default()));
// CaptureType: 0 unknown, 1 desktop, 2 window; 5 file transfer, 8 terminal, 9 port mapping.
let desktop = capture_type <= 2;
let controller_mac = mac_by_ice.remove(&ice).unwrap_or(next_mac);
if desktop { input.set_mac_controller(controller_mac); }
log::info!("Offer for connection: capture_type={} desktop={} active_peers={}", capture_type, desktop, sessions.len());
if let Some(old) = sessions.remove(&ice) { let _ = old.pc.close().await; }
if desktop {
// One desktop stream at a time: a new desktop offer replaces stale ones.
let stale: Vec<String> = sessions.iter().filter(|(_, s)| s.desktop).map(|(k, _)| k.clone()).collect();
for k in stale { if let Some(old) = sessions.remove(&k) { let _ = old.pc.close().await; } }
// Replace only this ICE id or a previous desktop, never tool peers.
for id in superseded_peer_ids(&sessions, &ice, desktop) {
if let Some(old) = sessions.remove(&id) { let _ = old.pc.close().await; }
}
let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
&& crate::session_lock::locked_now() == Some(false);
if desktop && desktop_backend::headless() {
log::info!("Desktop connection ignored: headless mode (terminal, file transfer and port mapping remain available)");
continue;
}
let session_capture=if desktop && (capture.init_error.is_some() || stale_portal) {
if stale_portal {
input.release_held();input.bind_portal(None);
capture=Arc::new(ScreenCapture::unavailable("Portal authorization expired".into()));
}
let resized=desktop && capture.init_error.is_none() && capture.backend==desktop_backend::DesktopBackend::X11
&& tokio::task::spawn_blocking(crate::capture::x11_resized).await.unwrap_or(false);
if resized { log::info!("Desktop geometry changed since startup; reinitializing capture"); }
let session_capture=if desktop && portal_desktop.is_none() && (capture.init_error.is_some() || resized) {
match tokio::task::spawn_blocking(ScreenCapture::new).await {
Ok(Ok(screen))=>{ capture=Arc::new(screen);capture.clone() },
Ok(Err(error))=>{ log::warn!("Desktop connection unavailable: {error}"); continue; },
@@ -636,8 +728,10 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
} else { capture.clone() };
// Independent file/terminal peers must not detach the
// current desktop's authorized Portal input session.
if desktop { input.bind_portal(session_capture.portal.clone()); }
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport).await {
if desktop && portal_desktop.is_none() { input.bind_portal(session_capture.portal.clone()); }
if desktop { if let Some(portal) = &portal_desktop { portal.begin(); } }
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport,
if desktop { portal_desktop.clone() } else { None }).await {
Ok(sess) => {
// Register before awaiting SDP: shutdown can always close
// a peer whose answer may already be queued to the viewer.
@@ -675,15 +769,11 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
}
}
SignalingEvent::Released(val) | SignalingEvent::Left(val) => {
let ice = ["ice_id", "iceId"].iter().find_map(|k| val.get(*k).or_else(|| val.get("data").and_then(|d| d.get(*k))))
.and_then(|v| v.as_str()).map(str::to_owned);
let ice = ["ice_id", "iceId"].iter().find_map(|k| val.get(*k).or_else(|| val.get("data").and_then(|d| d.get(*k))));
let keys: Vec<String> = val.as_object().map(|o| o.keys().take(16).cloned().collect()).unwrap_or_default();
let mut sessions = webrtc_sessions.lock().await;
let targets: Vec<String> = match &ice {
Some(id) if sessions.contains_key(id) => vec![id.clone()],
_ => sessions.keys().cloned().collect(),
};
log::info!("Remote controller disconnected: keys={:?} ice_matched={} closing={}", keys, ice.as_ref().is_some_and(|i| sessions.contains_key(i)), targets.len());
let targets = released_peer_ids(&sessions, ice);
log::info!("Remote controller disconnected: keys={:?} ice_matched={} closing={}", keys, ice.is_some() && !targets.is_empty(), targets.len());
let mut desktop_ended = false;
for k in targets {
if let Some(old) = sessions.remove(&k) {
+8 -8
View File
@@ -154,22 +154,21 @@ mod tests {
assert_eq!(rates.sample(now + std::time::Duration::from_secs(3), 0, 0), None);
}
/// Exercises actual ICE nomination and SCTP delivery between local peers, without
/// Exercises actual ICE nomination and SCTP delivery between virtual peers, without
/// login, public STUN, external endpoints or production configuration.
#[tokio::test]
#[ignore = "opens local UDP sockets; run explicitly for network acceptance"]
async fn local_selected_pair_and_payload() {
use std::sync::Arc;
use webrtc::api::{APIBuilder, setting_engine::SettingEngine};
use webrtc::api::APIBuilder;
use webrtc::peer_connection::configuration::RTCConfiguration;
let mut settings = SettingEngine::default();
// This crate excludes loopback addresses during ICE gathering. Both
// peers still run in this process and use only gathered local addresses.
settings.set_ice_multicast_dns_mode(webrtc::ice::mdns::MulticastDnsMode::Disabled);
let (left_setting, right_setting, router) = crate::test_network::virtual_network().await;
let mut media = webrtc::api::media_engine::MediaEngine::default();
media.register_default_codecs().unwrap();
let api = APIBuilder::new().with_media_engine(media).with_setting_engine(settings).build();
let api = APIBuilder::new().with_media_engine(media).with_setting_engine(left_setting).build();
let left = Arc::new(api.new_peer_connection(RTCConfiguration::default()).await.unwrap());
let mut media = webrtc::api::media_engine::MediaEngine::default();
media.register_default_codecs().unwrap();
let api = APIBuilder::new().with_media_engine(media).with_setting_engine(right_setting).build();
let right = Arc::new(api.new_peer_connection(RTCConfiguration::default()).await.unwrap());
for (label, peer) in [("left", &left), ("right", &right)] {
peer.on_peer_connection_state_change(Box::new(move |state| {
@@ -222,6 +221,7 @@ mod tests {
let outcome = tokio::time::timeout(std::time::Duration::from_secs(15), acceptance).await;
left.close().await.unwrap();
right.close().await.unwrap();
router.lock().await.stop().await.unwrap();
outcome.expect("isolated local ICE/SCTP did not connect within 15 seconds");
}
}
+518 -43
View File
@@ -1,37 +1,121 @@
//! GNOME/KDE portal session. Capture and input must share this Arc; do not
//! recreate it in an encoder subprocess (that would request consent again).
use std::sync::Arc;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PortalError { pub code: i32 }
impl std::fmt::Display for PortalError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}", error(self.code))
}
}
impl std::error::Error for PortalError {}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct FrameVersion(usize, u64);
#[cfg(unix)]
#[path = "portal_permissions.rs"]
mod permissions;
#[cfg(all(target_os = "linux", feature = "portal"))]
mod native {
use std::{ffi::c_void, ptr::NonNull, sync::Mutex};
use std::{ffi::c_void, ptr::NonNull, sync::{Mutex, MutexGuard, TryLockError}};
extern "C" {
fn uu_portal_open_persist(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
fn uu_portal_open_persist_cancellable(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), save_data: *mut c_void, cancelled: extern "C" fn(*mut c_void) -> i32, cancel_data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
fn uu_portal_clipboard_supported(portal: *mut c_void) -> i32;
fn uu_portal_clipboard_ready(portal: *mut c_void) -> i32;
fn uu_portal_clipboard_set(portal: *mut c_void, text: *const u8, length: usize) -> i32;
fn uu_portal_clipboard_poll(portal: *mut c_void, text: *mut u8, capacity: usize) -> i32;
fn uu_portal_close(portal: *mut c_void);
fn uu_portal_alive(portal: *mut c_void) -> i32;
fn uu_portal_capture_alive(portal: *mut c_void) -> i32;
fn uu_portal_input_dispatch(portal: *mut c_void);
fn uu_portal_reset_frame(portal: *mut c_void);
fn uu_portal_capture(portal: *mut c_void, bgra: *mut u8, length: usize) -> i32;
fn uu_portal_capture_since(portal: *mut c_void, bgra: *mut u8, length: usize, revision: *mut u64) -> i32;
fn uu_portal_move(portal: *mut c_void, x: f64, y: f64, relative: i32) -> i32;
fn uu_portal_button(portal: *mut c_void, button: i32, pressed: i32) -> i32;
fn uu_portal_key(portal: *mut c_void, keysym: u32, pressed: i32) -> i32;
fn uu_portal_key(portal: *mut c_void, keysym: u32, keycode: i32, pressed: i32) -> i32;
}
struct Handle(NonNull<c_void>);
// The engine owns callback threads; public calls are serialized by Mutex.
unsafe impl Send for Handle {}
impl Drop for Handle { fn drop(&mut self) { unsafe { uu_portal_close(self.0.as_ptr()) } } }
pub struct Session { handle: Mutex<Handle>, locked: tokio::sync::watch::Receiver<Option<bool>>, was_locked: std::sync::atomic::AtomicBool, retry_at: Mutex<Option<std::time::Instant>>, pub width: u32, pub height: u32 }
fn blank_changed(frame: &mut [u8], version: &mut Option<super::FrameVersion>) -> bool {
if *version == Some(super::FrameVersion(0, 0)) { return false; }
super::blank(frame); *version = Some(super::FrameVersion(0, 0)); true
}
// Portal preparation and native teardown can wait for consent or D-Bus.
// Only the swap may hold the mutex shared with input and clipboard.
fn replace_handle<T>(current: &Mutex<T>, locked: MutexGuard<'_, T>, prepare: impl FnOnce() -> Result<Option<T>, String>) -> Result<bool, String> {
drop(locked);
let Some(replacement) = prepare()? else { return Ok(false); };
let old = {
let mut handle = current.lock().map_err(|_| "Portal session lock poisoned")?;
std::mem::replace(&mut *handle, replacement)
};
drop(old);
Ok(true)
}
#[derive(Default)]
struct Recovery { retry_at: Option<std::time::Instant>, failed_until: Option<std::time::Instant>, denied: bool }
impl Recovery {
// Shared by the encoder's stream-loss recovery and the desktop supervisor: a failed open
// backs both off, and a terminal consent/format failure stops the encoder from prompting again.
fn failed(&mut self, now: std::time::Instant, code: i32) {
let until = now + std::time::Duration::from_secs(5);
self.failed_until = Some(until);
self.retry_at = Some(until);
if matches!(code, 4 | 5 | 6 | 8 | 14) { self.denied = true; }
}
fn succeeded(&mut self) { *self = Self::default(); }
fn ready(&mut self, now: std::time::Instant, unlocked: impl FnOnce() -> bool) -> bool {
if self.denied { return false; }
let deadline = self.retry_at.get_or_insert(now + std::time::Duration::from_secs(1));
if now < *deadline { return false; }
*deadline = now + std::time::Duration::from_secs(1);
unlocked()
}
}
fn clear_pause_for_unlocked_replacement(was_locked: &std::sync::atomic::AtomicBool, state: Option<bool>) {
if state == Some(false) {
was_locked.store(false, std::sync::atomic::Ordering::Release);
}
}
pub struct Session { handle: Mutex<Handle>, locked: tokio::sync::watch::Receiver<Option<bool>>, was_locked: std::sync::atomic::AtomicBool, recovery: Mutex<Recovery>, pub width: u32, pub height: u32 }
impl Session {
fn take_lock_observation(&self) -> Result<bool, String> {
// A recovery in progress must see the lock even if another capture
// observes unlock and would otherwise reset only the old handle.
let _recovery = match self.recovery.try_lock() {
Ok(recovery) => recovery,
Err(TryLockError::WouldBlock) => return Ok(false),
Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()),
};
Ok(self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel))
}
fn check_geometry(&self, replacement: &Self) -> Result<(), super::PortalError> {
// Encoder buffers use frame pixels. Logical geometry and EIS regions
// belong to the replacement handle and may change with scaling (#25).
if (self.width, self.height) != (replacement.width, replacement.height) {
Err(super::PortalError { code: 14 })
} else {
Ok(())
}
}
pub fn is_alive(&self) -> bool {
self.handle.lock().is_ok_and(|handle| unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 })
}
pub fn open() -> Result<Self, String> {
fn open_inner(cancelled: Option<&std::sync::atomic::AtomicBool>) -> Result<Self, super::PortalError> {
extern "C" fn is_cancelled(data: *mut c_void) -> i32 {
let cancelled = unsafe { &*(data as *const std::sync::atomic::AtomicBool) };
i32::from(cancelled.load(std::sync::atomic::Ordering::Acquire))
}
if cancelled.is_some_and(|value| value.load(std::sync::atomic::Ordering::Acquire)) {
return Err(super::PortalError { code: 13 });
}
let (mut error, mut width, mut height) = (0, 0, 0);
let directory = super::permissions::directory();
let key = super::permissions::desktop_key();
@@ -48,57 +132,160 @@ mod native {
});
if result.is_err() { log::warn!("Portal permission cache callback failed"); }
}
let pointer = unsafe { uu_portal_open_persist(token.as_ref().map_or(std::ptr::null(), |v| v.as_ptr()), save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height) };
let handle = NonNull::new(pointer).ok_or_else(|| super::error(error))?;
let restore = token.as_ref().map_or(std::ptr::null(), |value| value.as_ptr());
let pointer = unsafe {
if let Some(cancelled) = cancelled {
uu_portal_open_persist_cancellable(
restore, save_token, &mut cache as *mut _ as *mut c_void,
is_cancelled, cancelled as *const _ as *mut c_void,
&mut error, &mut width, &mut height,
)
} else {
uu_portal_open_persist(restore, save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height)
}
};
let handle = NonNull::new(pointer).ok_or(super::PortalError { code: error })?;
// A freshly opened session holds no lock-screen frame: only an observed lock (or a
// closed stream) must discard the cached frame. Starting "locked" cleared the first
// valid frame, and a static desktop sends no new one, so the viewer stayed black (#2).
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), retry_at: Mutex::new(None), width, height })
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), recovery: Mutex::new(Recovery::default()), width, height })
}
pub fn open() -> Result<Self, String> {
Self::open_inner(None).map_err(|error| error.to_string())
}
pub fn open_cancellable(cancelled: &std::sync::atomic::AtomicBool) -> Result<Self, super::PortalError> {
Self::open_inner(Some(cancelled))
}
pub fn recover(&self, cancelled: &std::sync::atomic::AtomicBool) -> Result<bool, super::PortalError> {
use std::sync::atomic::Ordering;
if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); }
// Serialize with the encoder's stream-loss recovery: two openers would ask for consent twice.
let mut recovery = loop {
match self.recovery.try_lock() {
Ok(recovery) => break recovery,
Err(TryLockError::WouldBlock) => {
if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); }
std::thread::sleep(std::time::Duration::from_millis(20));
}
Err(TryLockError::Poisoned(_)) => return Err(super::PortalError { code: 3 }),
}
};
if recovery.failed_until.is_some_and(|until| std::time::Instant::now() < until) {
return Err(super::PortalError { code: 3 });
}
let alive = {
let handle = self.handle.lock().map_err(|_| super::PortalError { code: 3 })?;
unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 && uu_portal_capture_alive(handle.0.as_ptr()) != 0 }
};
if alive { return Ok(false); }
let result = super::open_then_install(cancelled, || Self::open_cancellable(cancelled), |replacement| {
self.check_geometry(&replacement)?;
let replacement_handle = replacement.handle.into_inner().map_err(|_| super::PortalError { code: 3 })?;
let mut handle = self.handle.lock().map_err(|_| super::PortalError { code: 3 })?;
if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); }
*handle = replacement_handle;
clear_pause_for_unlocked_replacement(&self.was_locked, *self.locked.borrow());
log::info!("Portal capture session restored after closure");
Ok(())
});
match result {
Ok(()) => { recovery.succeeded(); Ok(true) }
Err(error) => {
if error.code != 13 { recovery.failed(std::time::Instant::now(), error.code); }
Err(error)
}
}
}
pub fn pause(&self) {
if let Ok(handle) = self.handle.lock() {
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
} else {
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
}
}
pub fn capture(&self, frame: &mut [u8]) -> Result<(), String> {
let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
self.capture_changed(frame, &mut None).map(|_| ())
}
pub fn capture_changed(&self, frame: &mut [u8], version: &mut Option<super::FrameVersion>) -> Result<bool, String> {
if frame.len() != self.width as usize * self.height as usize * 4 { return Err(super::error(11)); }
let handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
// Drain EIS even on a static desktop so pause/disconnect and sync
// events are processed without waiting for remote input.
unsafe { uu_portal_input_dispatch(handle.0.as_ptr()) };
let state = *self.locked.borrow();
if state != Some(false) {
// Unknown state (watcher not reported yet): show nothing but keep the cached frame.
if state == Some(true) {
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
}
super::blank(frame); return Ok(());
}
if self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel) {
// First unlocked capture after a real lock: drop frames captured while locked.
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
super::blank(frame); return Ok(());
}
if unsafe { uu_portal_alive(handle.0.as_ptr()) } == 0 {
// GNOME can close the stream before the periodic lock watcher updates.
if crate::session_lock::locked_now() != Some(false) {
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
super::blank(frame); return Ok(());
}
// logind's LockedHint can lag compositor stream closure. Give it a
// full watcher interval before attempting another permission session.
let mut retry = self.retry_at.lock().map_err(|_| "Portal recovery lock poisoned")?;
let deadline = retry.get_or_insert_with(|| std::time::Instant::now() + std::time::Duration::from_secs(1));
if std::time::Instant::now() < *deadline { super::blank(frame); return Ok(()); }
let replacement = match Self::open() {
Ok(value) => value,
Err(error) => {
log::warn!("Portal capture recovery deferred: {error}");
*retry = Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
super::blank(frame); return Ok(());
return Ok(blank_changed(frame, version));
}
if self.was_locked.load(std::sync::atomic::Ordering::Acquire) {
// First unlocked capture after a real lock: drop frames captured while locked.
if self.take_lock_observation()? { unsafe { uu_portal_reset_frame(handle.0.as_ptr()) }; }
return Ok(blank_changed(frame, version));
}
if unsafe { uu_portal_capture_alive(handle.0.as_ptr()) } == 0 {
// Serialize recovery without making another capture worker hold
// the input mutex while waiting for this worker's Portal calls.
let mut recovery = match self.recovery.try_lock() {
Ok(retry) => retry,
Err(TryLockError::WouldBlock) => return Ok(blank_changed(frame, version)),
Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()),
};
if replace_handle(&self.handle, handle, || {
{
let handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
if unsafe { uu_portal_capture_alive(handle.0.as_ptr()) } != 0 {
recovery.retry_at = None;
return Ok(None);
}
}
// logind's LockedHint can lag compositor stream closure.
if !recovery.ready(std::time::Instant::now(), || {
if crate::session_lock::locked_now() == Some(false) { return true; }
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
false
}) { return Ok(None); }
let replacement = match Self::open_inner(None) {
Ok(value) => value,
Err(error) => {
log::warn!("Portal capture recovery deferred: {error}");
recovery.failed(std::time::Instant::now(), error.code);
return Ok(None);
}
};
recovery.succeeded();
self.check_geometry(&replacement).map_err(|error| error.to_string())?;
// Consent can outlive the unlocked state checked above.
if *self.locked.borrow() != Some(false) || self.was_locked.load(std::sync::atomic::Ordering::Acquire) {
return Ok(None);
}
};
*retry = None;
if replacement.width != self.width || replacement.height != self.height {
return Err("Portal dimensions changed; reconnect required".into());
replacement.handle.into_inner().map(Some).map_err(|_| "Portal session lock poisoned".into())
})? {
*version = None;
log::info!("Portal capture session restored after stream loss");
}
// Recheck lock state on the next capture before showing the new session.
return Ok(blank_changed(frame, version));
} else {
match self.recovery.try_lock() {
Ok(mut recovery) => recovery.retry_at = None,
Err(TryLockError::WouldBlock) => (),
Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()),
}
*handle = replacement.handle.into_inner().map_err(|_| "Portal session lock poisoned")?;
log::info!("Portal capture session restored after closure");
}
let identity = handle.0.as_ptr() as usize;
let mut revision = version.filter(|v| v.0 == identity).map_or(0, |v| v.1);
let code = unsafe { uu_portal_capture_since(handle.0.as_ptr(), frame.as_mut_ptr(), frame.len(), &mut revision) };
match code {
0 | 12 => { *version = Some(super::FrameVersion(identity, revision)); Ok(code == 0) }
9 | 10 => Ok(blank_changed(frame, version)),
_ => Err(super::error(code)),
}
let code = unsafe { uu_portal_capture(handle.0.as_ptr(), frame.as_mut_ptr(), frame.len()) };
if code == 10 || code == 9 { super::blank(frame); Ok(()) } else { super::check(code) }
}
pub fn clipboard_supported(&self) -> bool {
@@ -134,20 +321,270 @@ mod native {
if pressed && *self.locked.borrow() != Some(false) { return Err("Portal input blocked while locked".into()); }
let symbol = u32::try_from(symbol).map_err(|_| "Invalid Portal keysym")?;
let handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
super::check(unsafe { uu_portal_key(handle.0.as_ptr(), symbol, i32::from(pressed)) })
let keycode = crate::uinput::keysym_to_evdev(u64::from(symbol)).map_or(-1, i32::from);
super::check(unsafe { uu_portal_key(handle.0.as_ptr(), symbol, keycode, i32::from(pressed)) })
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{mem::ManuallyDrop, sync::Arc};
struct Fixture(ManuallyDrop<Session>);
impl Fixture {
fn new(physical: (u32, u32)) -> Self {
let (_, locked) = tokio::sync::watch::channel(None);
Self(ManuallyDrop::new(Session {
handle: Mutex::new(Handle(NonNull::dangling())),
locked,
was_locked: std::sync::atomic::AtomicBool::new(false),
recovery: Mutex::new(Recovery::default()),
width: physical.0,
height: physical.1,
}))
}
fn session(&self) -> &Session { &self.0 }
}
impl Drop for Fixture {
fn drop(&mut self) {
let Session { handle, locked, was_locked, recovery, .. } =
unsafe { ManuallyDrop::take(&mut self.0) };
let handle = handle.into_inner().unwrap_or_else(std::sync::PoisonError::into_inner);
std::mem::forget(handle);
drop((locked, was_locked, recovery));
}
}
#[test]
fn recovery_requires_only_matching_frame_dimensions() {
let original = Fixture::new((3840, 2160));
let same = Fixture::new((3840, 2160));
let width_changed = Fixture::new((2560, 2160));
let height_changed = Fixture::new((3840, 1440));
assert!(original.session().check_geometry(same.session()).is_ok());
assert!(original.session().check_geometry(width_changed.session()).is_err());
assert!(original.session().check_geometry(height_changed.session()).is_err());
}
struct TestHandle {
generation: u8,
key_held: bool,
button_held: bool,
retirement: Option<(std::sync::mpsc::Sender<()>, std::sync::mpsc::Receiver<()>)>,
}
impl Drop for TestHandle {
fn drop(&mut self) {
if let Some((entered, resume)) = self.retirement.take() {
let _ = entered.send(());
let _ = resume.recv_timeout(std::time::Duration::from_secs(10));
}
}
}
fn release_input(current: Arc<Mutex<TestHandle>>) -> (std::thread::JoinHandle<()>, std::sync::mpsc::Receiver<(u8, bool, bool)>) {
let (released, received) = std::sync::mpsc::channel();
let worker = std::thread::spawn(move || {
let mut handle = current.lock().unwrap();
handle.key_held = false;
handle.button_held = false;
released.send((handle.generation, handle.key_held, handle.button_held)).unwrap();
});
(worker, received)
}
#[test]
fn recovery_does_not_block_input_releases_during_open_or_close() {
use std::{sync::mpsc::channel, time::Duration};
let timeout = Duration::from_secs(3);
let (opening, opened) = channel();
let (resume_open, wait_open) = channel();
let (closing, closed) = channel();
let (resume_close, wait_close) = channel();
let current = Arc::new(Mutex::new(TestHandle {
generation: 1, key_held: true, button_held: true, retirement: Some((closing, wait_close)),
}));
let retry = Arc::new(Mutex::new(None::<std::time::Instant>));
let recovery = {
let current = current.clone();
let retry = retry.clone();
std::thread::spawn(move || {
let handle = current.lock().unwrap();
let _owner = retry.try_lock().unwrap();
replace_handle(&current, handle, || {
opening.send(()).unwrap();
wait_open.recv_timeout(Duration::from_secs(10)).unwrap();
Ok(Some(TestHandle {
generation: 2, key_held: true, button_held: true, retirement: None,
}))
})
})
};
let opening_result = opened.recv_timeout(timeout);
let (first_worker, first_release) = release_input(current.clone());
let first_result = first_release.recv_timeout(timeout);
// A concurrent capture must not wait on recovery while retaining input's mutex.
let (contended, contention) = channel();
let contender = {
let current = current.clone();
let retry = retry.clone();
std::thread::spawn(move || {
let _handle = current.lock().unwrap();
contended.send(matches!(retry.try_lock(), Err(TryLockError::WouldBlock))).unwrap();
})
};
let contention_result = contention.recv_timeout(timeout);
let _ = resume_open.send(());
let closing_result = closed.recv_timeout(timeout);
let (second_worker, second_release) = release_input(current.clone());
let second_result = second_release.recv_timeout(timeout);
// Always release blocked work before asserting, including on regression.
let _ = resume_close.send(());
let recovered = recovery.join().unwrap();
first_worker.join().unwrap();
second_worker.join().unwrap();
contender.join().unwrap();
assert!(opening_result.is_ok() && closing_result.is_ok());
assert_eq!(first_result.unwrap(), (1, false, false));
assert_eq!(second_result.unwrap(), (2, false, false));
assert!(contention_result.unwrap());
assert!(recovered.unwrap());
}
#[test]
fn failed_or_deferred_recovery_keeps_the_current_handle_unlocked() {
let current = Mutex::new(1);
for failure in [false, true] {
let handle = current.lock().unwrap();
let result = replace_handle(&current, handle, || {
assert_eq!(*current.try_lock().unwrap(), 1);
if failure { Err("open failed".into()) } else { Ok(None) }
});
if failure { assert_eq!(result.unwrap_err(), "open failed"); }
else { assert!(!result.unwrap()); }
assert_eq!(*current.try_lock().unwrap(), 1);
}
}
#[test]
fn terminal_failure_stops_encoder_recovery_until_the_supervisor_succeeds() {
let now = std::time::Instant::now();
let later = now + std::time::Duration::from_secs(60);
let mut recovery = Recovery::default();
recovery.failed(now, 3);
assert!(!recovery.denied && recovery.failed_until.is_some());
recovery.failed(now, 4);
assert!(recovery.denied);
assert!(!recovery.ready(later, || panic!("denied consent must not be probed again")));
recovery.succeeded();
assert!(!recovery.denied && recovery.failed_until.is_none() && recovery.retry_at.is_none());
}
#[test]
fn recovery_backoff_skips_lock_probes_until_the_deadline() {
use std::{cell::Cell, time::{Duration, Instant}};
let now = Instant::now();
let probes = Cell::new(0);
let unlocked = || { probes.set(probes.get() + 1); true };
let mut recovery = Recovery::default();
// Initial one-second grace period, polled at up to 144 fps.
for millis in 0..1000 {
assert!(!recovery.ready(now + Duration::from_millis(millis), unlocked));
}
assert_eq!(probes.get(), 0);
assert!(recovery.ready(now + Duration::from_secs(1), unlocked));
assert_eq!(probes.get(), 1);
// A failed open schedules the existing five-second retry delay.
recovery.retry_at = Some(now + Duration::from_secs(6));
for millis in 1000..6000 {
assert!(!recovery.ready(now + Duration::from_millis(millis), unlocked));
}
assert_eq!(probes.get(), 1);
// A locked/unknown probe denies opening and is itself throttled.
assert!(!recovery.ready(now + Duration::from_secs(6), || { probes.set(2); false }));
assert!(!recovery.ready(now + Duration::from_millis(6999), || panic!("early lock probe")));
assert!(recovery.ready(now + Duration::from_secs(7), unlocked));
assert_eq!(probes.get(), 3);
}
#[test]
fn lock_observation_survives_unlock_while_recovery_is_opening() {
use std::{sync::{atomic::Ordering, mpsc::channel}, time::Duration};
let mut fixture = Fixture::new((2, 2));
let (locked, watch) = tokio::sync::watch::channel(Some(false));
fixture.0.locked = watch;
let session = fixture.session();
std::thread::scope(|threads| {
let (opening, opened) = channel();
let (resume, wait) = channel();
let recovery = threads.spawn(move || {
let _owner = session.recovery.lock().unwrap();
opening.send(()).unwrap();
wait.recv_timeout(Duration::from_secs(10)).unwrap();
});
let ready = opened.recv_timeout(Duration::from_secs(3));
locked.send_replace(Some(true));
session.was_locked.store(true, Ordering::Release);
locked.send_replace(Some(false));
let (reset, result) = channel();
let capture = threads.spawn(move || {
let _handle = session.handle.lock().unwrap();
assert_eq!(*session.locked.borrow(), Some(false));
reset.send(session.take_lock_observation()).unwrap();
});
let consumed = result.recv_timeout(Duration::from_secs(3));
let still_pending = session.was_locked.load(Ordering::Acquire);
let _ = resume.send(());
recovery.join().unwrap();
capture.join().unwrap();
assert!(ready.is_ok());
assert!(!consumed.unwrap().unwrap());
assert!(still_pending);
});
assert!(session.take_lock_observation().unwrap());
assert!(!session.was_locked.load(std::sync::atomic::Ordering::Acquire));
}
#[test]
fn cancelled_recovery_does_not_touch_the_handle() {
let session = Fixture::new((1920, 1080));
let cancelled = std::sync::atomic::AtomicBool::new(true);
assert_eq!(session.session().recover(&cancelled).unwrap_err().code, 13);
}
#[test]
fn unlocked_replacement_keeps_its_first_static_frame() {
let was_locked = std::sync::atomic::AtomicBool::new(true);
clear_pause_for_unlocked_replacement(&was_locked, Some(false));
assert!(!was_locked.load(std::sync::atomic::Ordering::Acquire));
}
}
}
fn blank(frame: &mut [u8]) { for pixel in frame.chunks_exact_mut(4) { pixel.copy_from_slice(&[0, 0, 0, 255]); } }
fn open_then_install<T>(
cancelled: &std::sync::atomic::AtomicBool,
open: impl FnOnce() -> Result<T, PortalError>,
install: impl FnOnce(T) -> Result<(), PortalError>,
) -> Result<(), PortalError> {
use std::sync::atomic::Ordering;
if cancelled.load(Ordering::Acquire) { return Err(PortalError { code: 13 }); }
let replacement = open()?;
if cancelled.load(Ordering::Acquire) { return Err(PortalError { code: 13 }); }
install(replacement)
}
fn error(code: i32) -> String {
let reason = match code {
1 => "session bus unavailable", 2 => "RemoteDesktop/ScreenCast portal unavailable",
3 => "request failed", 4 => "permission cancelled or denied", 5 => "permission request timed out",
6 => "single monitor and keyboard/pointer permission required", 7 => "PipeWire connection failed",
8 => "unsupported frame format or size", 9 => "session closed or stream lost",
10 => "frame unavailable", 11 => "invalid input or frame buffer", _ => "operation failed",
10 => "frame unavailable", 11 => "invalid input or frame buffer",
13 => "operation cancelled", 14 => "dimensions changed; reconnect required",
_ => "operation failed",
};
format!("Portal {reason} (code {code})")
}
@@ -165,7 +602,17 @@ impl PortalSession {
pub fn clipboard_poll(&self) -> Option<String> { None }
pub fn is_alive(&self) -> bool { false }
pub fn open() -> Result<Self, String> { Err("Portal backend requires Linux and --features portal".into()) }
pub fn open_cancellable(cancelled: &std::sync::atomic::AtomicBool) -> Result<Self, PortalError> {
if cancelled.load(std::sync::atomic::Ordering::Acquire) { Err(PortalError { code: 13 }) }
else { Err(PortalError { code: 2 }) }
}
pub fn recover(&self, cancelled: &std::sync::atomic::AtomicBool) -> Result<bool, PortalError> {
if cancelled.load(std::sync::atomic::Ordering::Acquire) { Err(PortalError { code: 13 }) }
else { Err(PortalError { code: 2 }) }
}
pub fn pause(&self) {}
pub fn capture(&self, _: &mut [u8]) -> Result<(), String> { Err(error(2)) }
pub fn capture_changed(&self, _: &mut [u8], _: &mut Option<FrameVersion>) -> Result<bool, String> { Err(error(2)) }
pub fn move_pointer(&self, _: f64, _: f64, _: bool) -> Result<(), String> { Err(error(2)) }
pub fn button(&self, _: i32, _: bool) -> Result<(), String> { Err(error(2)) }
pub fn key(&self, _: u64, _: bool) -> Result<(), String> { Err(error(2)) }
@@ -195,4 +642,32 @@ mod tests {
assert!(super::check(4).unwrap_err().contains("cancelled or denied"));
assert!(super::check(9).unwrap_err().contains("closed or stream lost"));
}
#[test]
fn portal_error_display_is_fixed_and_sanitized() {
assert_eq!(super::PortalError { code: 13 }.to_string(), "Portal operation cancelled (code 13)");
assert_eq!(super::PortalError { code: 14 }.to_string(), "Portal dimensions changed; reconnect required (code 14)");
assert_eq!(super::PortalError { code: 123 }.to_string(), "Portal operation failed (code 123)");
}
#[test]
fn opening_with_pre_cancelled_flag_returns_cancellation_code() {
let cancelled = std::sync::atomic::AtomicBool::new(true);
let code = match super::PortalSession::open_cancellable(&cancelled) {
Err(error) => error.code,
Ok(_) => panic!("pre-cancelled portal open unexpectedly succeeded"),
};
assert_eq!(code, 13);
}
#[test]
fn replacement_opens_before_installing_under_the_handle_lock() {
let handle = std::sync::Mutex::new(7);
let cancelled = std::sync::atomic::AtomicBool::new(false);
super::open_then_install(&cancelled, || {
assert_eq!(*handle.try_lock().unwrap(), 7);
Ok(11)
}, |replacement| {
*handle.lock().unwrap() = replacement;
Ok(())
}).unwrap();
assert_eq!(*handle.lock().unwrap(), 11);
}
}
+76
View File
@@ -75,6 +75,66 @@ pub fn decode_wire(input: &[u8]) -> Result<Vec<WireField<'_>>, WireError> {
Ok(fields)
}
/// Structure-only rendering for diagnostics: field numbers, wire types, lengths and small
/// varints (enums, sizes, fps). String and byte contents are never rendered.
pub fn wire_shape(data: &[u8], depth: u8) -> String {
let Ok(fields) = decode_wire(data) else { return format!("bytes({})", data.len()) };
let parts: Vec<String> = fields.iter().map(|f| match f.value {
WireValue::Varint(u64::MAX) => format!("{}=-1", f.number),
WireValue::Varint(v) if v <= 0xFFFF => format!("{}={v}", f.number),
WireValue::Varint(_) => format!("{}=big", f.number),
WireValue::Fixed32(_) => format!("{}:f32", f.number),
WireValue::Fixed64(_) => format!("{}:f64", f.number),
WireValue::Bytes(b) => format!("{}:{}", f.number, bytes_shape(b, depth)),
}).collect();
format!("{{{}}}", parts.join(","))
}
fn bytes_shape(b: &[u8], depth: u8) -> String {
let printable = !b.is_empty() && std::str::from_utf8(b).is_ok_and(|t| t.chars().all(|c| !c.is_control()));
if printable { return format!("str({})", b.len()); }
if depth > 0 && !b.is_empty() && decode_wire(b).is_ok_and(|f| !f.is_empty() && f.iter().all(|x| (1..=128).contains(&x.number))) {
return wire_shape(b, depth - 1);
}
format!("bytes({})", b.len())
}
/// JSON counterpart: keys and value types; strings as lengths (or nested JSON/base64-protobuf
/// shape), numbers only when small. Values that could identify a user are never rendered.
pub fn json_shape(value: &serde_json::Value, depth: u8) -> String {
use base64::Engine;
use serde_json::Value;
match value {
Value::Null => "null".into(),
Value::Bool(b) => b.to_string(),
Value::Number(n) => match n.as_i64() { Some(v) if v.abs() <= 0xFFFF => v.to_string(), _ => "num".into() },
Value::String(t) => {
if depth > 0 {
if let Ok(inner @ (Value::Object(_) | Value::Array(_))) = serde_json::from_str::<Value>(t) {
return format!("json{}", json_shape(&inner, depth - 1));
}
if t.len() >= 8 && t.len() % 4 == 0 {
if let Ok(raw) = base64::engine::general_purpose::STANDARD.decode(t) {
if decode_wire(&raw).is_ok_and(|f| !f.is_empty()) { return format!("b64{}", wire_shape(&raw, depth - 1)); }
}
}
}
format!("str({})", t.len())
}
Value::Array(items) => match items.first() {
Some(first) if depth > 0 => format!("[{}x{}]", items.len(), json_shape(first, depth - 1)),
_ => format!("[{}]", items.len()),
},
Value::Object(map) if depth > 0 => {
let parts: Vec<String> = map.iter().take(48).map(|(k, v)| {
let key: String = k.chars().filter(|c| c.is_ascii_alphanumeric() || *c == '_').take(40).collect();
format!("{key}:{}", json_shape(v, depth - 1))
}).collect();
format!("{{{}}}", parts.join(","))
}
Value::Object(map) => format!("{{{} keys}}", map.len()),
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -105,4 +165,20 @@ mod tests {
assert_eq!(decode_wire(&[11]), Err(WireError::UnsupportedWireType(3)));
assert_eq!(decode_wire(&[255, 255, 255, 255, 31]), Err(WireError::InvalidField));
}
#[test]
fn shapes_never_render_contents() {
// {1:1, 2:-1, 4:{1:60,2:2}, 9:"device-secret", 12:big}
let mut data = vec![0x08, 0x01, 0x10];
data.extend([0xff; 9]); data.push(0x01);
data.extend([0x22, 0x04, 0x08, 0x3c, 0x10, 0x02]);
data.extend([0x4a, 0x0d]); data.extend(b"device-secret");
data.extend([0x60, 0x80, 0x80, 0x80, 0x80, 0x10]);
let shape = super::wire_shape(&data, 3);
assert_eq!(shape, "{1=1,2=-1,4:{1=60,2=2},9:str(13),12=big}");
let header = serde_json::json!({"streamer_data": "{\"token\":\"secret-value\",\"n\":3}", "city": "Somewhere", "id": 123456789});
let shape = super::json_shape(&header, 4);
assert!(!shape.contains("secret") && !shape.contains("Somewhere") && !shape.contains("123456789"), "{shape}");
assert!(shape.contains("streamer_data:json{n:3,token:str(12)}"), "{shape}");
}
}
+13
View File
@@ -7,9 +7,22 @@ use webrtc::peer_connection::{configuration::RTCConfiguration,policy::ice_transp
pub struct SessionIce {
servers: Vec<RTCIceServer>,
relay_only: bool,
#[cfg(test)]
pub(crate) setting_engine: Option<webrtc::api::setting_engine::SettingEngine>,
}
impl SessionIce {
#[cfg(test)]
pub fn local() -> Self {
// A supplied empty URL list disables the public STUN defaults in local peer tests.
Self { servers: vec![RTCIceServer::default()], ..Default::default() }
}
#[cfg(test)]
pub fn virtual_net(setting_engine: webrtc::api::setting_engine::SettingEngine) -> Self {
Self { setting_engine: Some(setting_engine), ..Self::local() }
}
pub fn from_header(header:&Value)->Self {
let data=header.get("data").unwrap_or(header);
let field=|name:&str|header.get(name).or_else(||data.get(name));
+13 -8
View File
@@ -119,7 +119,9 @@ fn unlock_with(password: &str) -> UnlockResult {
if Command::new("loginctl").args(["unlock-session", &session]).status().map(|s| !s.success()).unwrap_or(true) {
return UnlockResult::NoPermission;
}
let _ = crate::capture::keep_display_awake(); // a blanked monitor would keep the stream black
if let Err(error) = crate::capture::keep_display_awake() {
log::warn!("Display wake after auto unlock failed: {error}");
}
// Post-unlock confirmation like the official host: the lock screen must actually go away.
for _ in 0..100 { // GNOME takes ~3 s to drop the shield after logind's Unlock
if session_locked(&session) == Some(false) { return UnlockResult::Success; }
@@ -134,23 +136,26 @@ type Sequence = std::sync::Arc<std::sync::atomic::AtomicU64>;
fn now() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs() }
/// Message 15 SystemStateChange { 1: SecureDesktopState } on CONTROL.
pub async fn send_state(channel: &Channel, sequence: &Sequence, locked: bool) {
pub async fn send_state(channel: &Channel, sequence: &Sequence, locked: bool) -> bool {
let seq = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let message = crate::initialization::encode_secure_desktop(seq, now(), locked, false, supported());
if let Err(e) = channel.send(&bytes::Bytes::from(message)).await { log::warn!("Secure desktop state send failed: {e}"); }
match channel.send(&bytes::Bytes::from(message)).await {
Ok(_) => {
log::info!("Secure desktop state sent: locked={} auto_unlock={}", locked, supported());
true
}
Err(e) => { log::warn!("Secure desktop state send failed: {e}"); false }
}
}
/// Reports the lock state to one desktop viewer: now, then on every change, until it closes.
pub async fn report_states(channel: Channel, sequence: Sequence) {
pub async fn report_states(channel: Channel, sequence: Sequence, mut state: watch::Receiver<Option<bool>>) {
use webrtc::data_channel::data_channel_state::RTCDataChannelState;
let mut state = watch_locked();
let mut sent = None;
loop {
let locked = *state.borrow_and_update();
if let Some(locked) = locked.filter(|l| sent != Some(*l)) {
send_state(&channel, &sequence, locked).await;
log::info!("Secure desktop state sent: locked={} auto_unlock={}", locked, supported());
sent = Some(locked);
if send_state(&channel, &sequence, locked).await { sent = Some(locked); }
}
tokio::select! {
changed = state.changed() => if changed.is_err() { return; },
+84 -2
View File
@@ -41,7 +41,7 @@ pub enum SignalingEvent {
BeControlled(Value),
/// be-controlled with its binary app_data (ConnectOptions): capture type and the
/// ice_id it announces, when present.
ConnectRequest { capture_type: u64, ice_id: Option<String>, transport:crate::session_ice::SessionIce },
ConnectRequest { capture_type: u64, ice_id: Option<String>, transport:crate::session_ice::SessionIce, controller_mac: bool },
Soac(SoacMessage),
Released(Value),
Left(Value),
@@ -54,6 +54,13 @@ pub struct SignalingClient {
_reconnect_key: Arc<std::sync::Mutex<Option<String>>>,
}
#[cfg(test)]
impl SignalingClient {
pub fn disconnected() -> Arc<Self> {
Arc::new(Self { tx_outgoing: mpsc::channel(1).0, _reconnect_key: Arc::new(std::sync::Mutex::new(None)) })
}
}
fn refreshed_key(ack:&Value)->Option<String> {
let values=ack.as_array()?;
if values.first()?.as_str()? != "success" { return None; }
@@ -95,6 +102,30 @@ mod refresh_tests {
// Retain it until after the server sees closure, just like PC callbacks.
drop(retained);
}
#[tokio::test]
async fn silent_half_open_socket_is_reported_disconnected() {
use futures_util::{SinkExt,StreamExt};
use tokio_tungstenite::tungstenite::Message;
let listener=tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address=listener.local_addr().unwrap();
let (hold_tx,hold_rx)=tokio::sync::oneshot::channel::<()>();
tokio::spawn(async move {
let (tcp,_)=listener.accept().await.unwrap();
let mut socket=tokio_tungstenite::accept_async(tcp).await.unwrap();
// Shortest accepted window (clamped to 5 s); the server then never pings.
socket.send(Message::Text(r#"0{"pingInterval":1000,"pingTimeout":1000}"#.into())).await.unwrap();
let _=socket.next().await;
socket.send(Message::Text("40{}".into())).await.unwrap();
let _=hold_rx.await; // keep the TCP connection open but silent
});
let (sender,mut events)=tokio::sync::mpsc::channel(8);
let _client=super::SignalingClient::connect(&format!("ws://{address}"),"synthetic-only",sender).await.unwrap();
let disconnected=tokio::time::timeout(std::time::Duration::from_secs(8),async {
loop { if matches!(events.recv().await,Some(super::SignalingEvent::Disconnected)|None) { return; } }
}).await;
assert!(disconnected.is_ok(),"a socket without pings must be reported dead");
drop(hold_tx);
}
}
impl SignalingClient {
@@ -156,9 +187,22 @@ impl SignalingClient {
let mut accepted_refresh=0;
// Socket.IO binary event awaiting attachments: (event name, header JSON).
let mut pending_binary: Option<(String, Value)> = None;
// Relation check only: the be-controlled ice_id versus the next offer's data.ice_id.
let mut last_connect_ice: Option<String> = None;
let mut last_connect_client: Option<String> = None;
// Engine.IO 4: a client treats the connection as dead when no ping arrives within
// pingInterval + pingTimeout. A half-open TCP socket (suspend, NAT drop) otherwise
// blocks the read forever and the host silently stays offline.
let mut ping_window=std::time::Duration::from_secs(25+20);
let mut ping_deadline=tokio::time::Instant::now()+ping_window;
loop {
let msg_res=tokio::select! {
_=event_sender.closed()=>break,
_=tokio::time::sleep_until(ping_deadline) => {
log::warn!("Signaling heartbeat lost: generation={} age_s={} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs());
let _ = event_sender.send(SignalingEvent::Disconnected).await;
break;
},
// Official ControlledSession RefreshSignalTokenLoop: first
// 30s, success every ~300s, failure retry after 30s. Native
// libstreamer 0x885774 / 0x885e58; emit has NO payload args.
@@ -185,12 +229,17 @@ impl SignalingClient {
if txt.starts_with('0') {
if let Ok(open)=serde_json::from_str::<Value>(&txt[1..]) {
log::info!("Signaling heartbeat config: generation={} ping_interval_ms={:?} ping_timeout_ms={:?}",generation,open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64));
if let (Some(interval),Some(timeout))=(open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64)) {
ping_window=std::time::Duration::from_millis(interval.saturating_add(timeout).clamp(5_000,300_000));
ping_deadline=tokio::time::Instant::now()+ping_window;
}
}
// Open packet -> 发送 Socket.IO connect
let _ = tx_out_clone.send("40".into()).await;
} else if txt == "2" {
// Ping -> 回复 Pong
let gap=last_ping.elapsed().as_secs();last_ping=std::time::Instant::now();pings+=1;
ping_deadline=tokio::time::Instant::now()+ping_window;
if tx_out_clone.send("3".into()).await.is_ok() { pong_queued+=1; }
if pings==1 || pings%12==0 {
log::info!("Signaling heartbeat progress: generation={} age_s={} pings={} pong_queued={} gap_s={}",generation,started.elapsed().as_secs(),pings,pong_queued,gap);
@@ -255,6 +304,15 @@ impl SignalingClient {
.await;
}
"soac" => {
// Offers only: structure (keys, types, lengths), never SDP text or ids.
let kind = data.get("data").unwrap_or(data).get("type").and_then(Value::as_str);
if kind == Some("offer") {
log::info!("SOAC offer shape: {}", crate::protocol::json_shape(data, 3));
let offer_ice = data.get("data").unwrap_or(data).get("ice_id").and_then(Value::as_str);
log::info!("SOAC offer ice_id equals be-controlled ice_id: {:?}", last_connect_ice.as_deref().zip(offer_ice).map(|(a, b)| a == b));
let offer_client = data.get("client_id").and_then(Value::as_str);
log::info!("SOAC offer client_id equals be-controlled client_id: {:?}", last_connect_client.as_deref().zip(offer_client).map(|(a, b)| a == b));
}
let (target_val, client_id_opt) = if let Some(inner) = data.get("data") {
(inner.clone(), data.get("client_id").and_then(|v| v.as_str()).map(|s| s.to_string()))
} else {
@@ -317,7 +375,31 @@ impl SignalingClient {
let ice_id = ["ice_id", "iceId"].iter().find_map(|k| header.get(*k).or_else(|| header.get("data").and_then(|d| d.get(*k))))
.and_then(|v| v.as_str()).map(str::to_owned);
log::info!("be-controlled connect request: capture_type={} header_keys={:?} ice_id_present={}", capture_type, keys, ice_id.is_some());
let _ = event_sender.send(SignalingEvent::ConnectRequest { capture_type, ice_id, transport:crate::session_ice::SessionIce::from_header(&header) }).await;
last_connect_ice = ice_id.clone();
last_connect_client = header.get("client_id").and_then(Value::as_str).map(str::to_owned);
// Structure only (numbers, types, lengths): documents what the viewer and cloud send.
let header_shape: String = crate::protocol::json_shape(&header, 7).chars().take(8000).collect();
log::info!("Connect request shape: app_data={} header={}", crate::protocol::wire_shape(data, 3), header_shape);
// Relation only, never the ids themselves.
let control_id = header.get("streamer_data").and_then(Value::as_str)
.and_then(|t| serde_json::from_str::<Value>(t).ok())
.and_then(|v| v.get("control_id").and_then(Value::as_str).map(str::to_owned));
let app_control_id = header.get("app_control_id").and_then(Value::as_str);
log::info!("Connect request ids: streamer_control_id_equals_app_control_id={:?}",
control_id.as_deref().zip(app_control_id).map(|(a, b)| a == b));
// ConnectOptions 4 decoder_cap_list: DecoderCap {1 fps, 2 codec_type (1 H264, 2 H265),
// 3 width, 4 height, 5 chroma_format (1 4:2:0, 3 4:4:4)}. Numbers only.
let caps: Vec<Vec<(u32, u64)>> = fields.iter().flatten().filter(|f| f.number == 4).filter_map(|f| match f.value {
crate::protocol::WireValue::Bytes(b) => crate::protocol::decode_wire(b).ok().map(|cap| cap.iter().filter_map(|x| match x.value {
crate::protocol::WireValue::Varint(v) => Some((x.number, v)), _ => None }).collect()),
_ => None }).collect();
if !caps.is_empty() { log::info!("Viewer decoder caps (fps,codec,w,h,chroma fields): {:?}", caps); }
// Viewer platform (website enum: 1 windows, 2 android, 3 ios, 4 macos, 5 tv).
let platform = header.get("controller_platform").or_else(|| header.get("data").and_then(|d| d.get("controller_platform")));
let controller_mac = platform.is_some_and(|v| v.as_i64() == Some(4)
|| v.as_str().is_some_and(|s| s == "4" || s.to_ascii_lowercase().contains("mac") || s.to_ascii_lowercase().contains("darwin")));
log::info!("Controller platform: {} mac={}", platform.map(|v| v.to_string()).unwrap_or_default().chars().take(24).collect::<String>(), controller_mac);
let _ = event_sender.send(SignalingEvent::ConnectRequest { capture_type, ice_id, transport:crate::session_ice::SessionIce::from_header(&header), controller_mac }).await;
}
}
}
+102 -12
View File
@@ -34,6 +34,7 @@ const MAGIC: &[u8; 4] = b"TERM";
const HEADER: usize = 16;
const MAX_PAYLOAD: usize = 0x10_0000;
const SCROLLBACK: usize = 256 * 1024;
const OUTPUT_CHUNK: usize = 16 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame { pub kind: u8, pub flags: [u8; 2], pub session: u32, pub payload: Vec<u8> }
@@ -70,9 +71,24 @@ struct Session {
pid: i32,
sink: Mutex<Option<UnboundedSender<Vec<u8>>>>,
scrollback: Mutex<VecDeque<u8>>,
size: Mutex<(u16, u16)>,
}
impl Session {
/// Lock order sink → scrollback matches OPEN, so a reattach snapshot sees each
/// chunk either in the replay or live, never both.
fn record_output(&self, data: &[u8]) {
let mut sink = self.sink.lock().unwrap();
{
let mut sb = self.scrollback.lock().unwrap();
sb.extend(data);
let excess = sb.len().saturating_sub(SCROLLBACK);
sb.drain(..excess);
}
if let Some(tx) = sink.as_ref() {
if tx.send(encode(STDOUT, self.id, data)).is_err() { *sink = None; }
}
}
fn emit(&self, kind: u8, payload: &[u8]) -> bool {
let mut sink = self.sink.lock().unwrap();
if let Some(tx) = sink.as_ref() {
@@ -81,10 +97,31 @@ impl Session {
}
false
}
// The size lock is held across the ioctl so a delayed restore cannot reapply a stale size.
fn resize(&self, cols: u16, rows: u16) {
let mut size = self.size.lock().unwrap();
*size = (cols, rows);
self.set_winsize(cols, rows);
}
fn set_winsize(&self, cols: u16, rows: u16) {
let win = libc::winsize { ws_row: rows, ws_col: cols, ws_xpixel: 0, ws_ypixel: 0 };
unsafe { libc::ioctl(self.master.as_raw_fd(), libc::TIOCSWINSZ as _, &win); }
}
/// Full-screen programs repaint only on SIGWINCH, which an unchanged size never raises:
/// shrink by one row, then restore the latest requested size after a pause.
fn force_redraw(self: &Arc<Self>) {
{
let size = self.size.lock().unwrap();
if size.1 <= 1 { return; }
self.set_winsize(size.0, size.1 - 1);
}
let s = self.clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(150));
let size = s.size.lock().unwrap();
s.set_winsize(size.0, size.1);
});
}
fn write(&self, data: &[u8]) {
let mut off = 0;
while off < data.len() {
@@ -136,23 +173,15 @@ fn spawn(requested_id: u32, cols: u16, rows: u16, shell: Option<&str>) -> Result
}
};
let session = Arc::new(Session { id, shell: shell.clone(), created_ms: now_ms(), pid: child.id() as i32,
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()) });
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()), size: Mutex::new((cols, rows)) });
let mut reader = std::fs::File::from(session.master.try_clone().map_err(|e| format!("pty dup failed: {e}"))?);
let worker = session.clone();
std::thread::Builder::new().name(format!("uu-term-{id}")).spawn(move || {
let mut buf = vec![0u8; 16 * 1024];
let mut buf = vec![0u8; OUTPUT_CHUNK];
loop {
match reader.read(&mut buf) {
Ok(0) | Err(_) => break,
Ok(n) => {
{
let mut sb = worker.scrollback.lock().unwrap();
sb.extend(&buf[..n]);
let excess = sb.len().saturating_sub(SCROLLBACK);
sb.drain(..excess);
}
worker.emit(STDOUT, &buf[..n]);
}
Ok(n) => worker.record_output(&buf[..n]),
}
}
let code = child.wait().ok().and_then(|s| s.code()).unwrap_or(-1);
@@ -211,6 +240,7 @@ impl TerminalConn {
OPEN => {
let v = json(&frame.payload);
let (cols, rows) = dims(&v);
let reattach = existing.is_some();
let session = match existing {
Some(s) => { s.resize(cols, rows); Ok(s) }
None => spawn(frame.session, cols, rows, v.get("shell").and_then(|s| s.as_str())),
@@ -222,7 +252,11 @@ impl TerminalConn {
self.send(OPEN_RESP, s.id, &reply(0, "ok"));
*sink = Some(self.out.clone());
let replay: Vec<u8> = s.scrollback.lock().unwrap().iter().copied().collect();
if !replay.is_empty() { self.send(STDOUT, s.id, &replay); }
// A full scrollback exceeds SCTP's 64 KiB outbound message limit.
for chunk in replay.chunks(OUTPUT_CHUNK) { self.send(STDOUT, s.id, chunk); }
drop(sink);
// The replay tail starts mid-redraw for full-screen programs.
if reattach { s.force_redraw(); }
}
Err(e) => { log::warn!("Terminal open failed: {}", e); self.send(OPEN_RESP, frame.session, &reply(1, &e)); }
}
@@ -289,6 +323,62 @@ mod tests {
}
}
#[test]
fn force_redraw_shrinks_then_restores_latest_size() {
let pty = nix::pty::openpty(None, None).unwrap();
let session = Arc::new(Session { id: u32::MAX - 2, shell: "/bin/sh".into(), created_ms: now_ms(),
master: pty.master, pid: 0, sink: Mutex::new(None),
scrollback: Mutex::new(VecDeque::new()), size: Mutex::new((80, 24)) });
let rows = |s: &Session| {
let mut win = libc::winsize { ws_row: 0, ws_col: 0, ws_xpixel: 0, ws_ypixel: 0 };
unsafe { libc::ioctl(s.master.as_raw_fd(), libc::TIOCGWINSZ as _, &mut win); }
(win.ws_col, win.ws_row)
};
session.resize(100, 30);
session.force_redraw();
assert_eq!(rows(&session), (100, 29));
session.resize(120, 40);
std::thread::sleep(std::time::Duration::from_millis(400));
assert_eq!(rows(&session), (120, 40), "restore must use the latest requested size");
}
#[test]
fn reconnect_replays_large_history_in_bounded_frames() {
let pty = nix::pty::openpty(None, None).unwrap();
let id = u32::MAX - 1;
let history: Vec<u8> = (0..SCROLLBACK).map(|n| (n % 251) as u8).collect();
let session = Arc::new(Session { id, shell: "/bin/sh".into(), created_ms: now_ms(),
master: pty.master, pid: 0, sink: Mutex::new(None),
scrollback: Mutex::new(history.iter().copied().collect()), size: Mutex::new((80, 24)) });
registry().lock().unwrap().insert(id, session.clone());
struct Cleanup(u32);
impl Drop for Cleanup { fn drop(&mut self) { registry().lock().unwrap().remove(&self.0); } }
let _cleanup = Cleanup(id);
for _ in 0..2 {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let conn = TerminalConn::new(tx);
conn.handle(&encode(OPEN, id, br#"{"cols":80,"rows":24}"#));
let opened = decode(&rx.try_recv().unwrap()).unwrap();
assert_eq!(opened.kind, OPEN_RESP);
assert_eq!(opened.session, id);
let mut replay = Vec::new();
while replay.len() < history.len() {
let packet = rx.try_recv().unwrap();
// SCTP's default outbound limit includes the TERM header.
assert!(packet.len() <= 65536, "terminal replay exceeds the SCTP message limit");
let frame = decode(&packet).unwrap();
assert_eq!(frame.kind, STDOUT);
assert_eq!(frame.session, id);
replay.extend(frame.payload);
}
assert!(replay == history, "terminal replay must preserve every byte in order");
assert!(session.emit(STDOUT, b"live output"));
assert_eq!(decode(&rx.try_recv().unwrap()).unwrap().payload, b"live output");
conn.handle(&encode(LIST_REQ, 7, &[]));
assert_eq!(decode(&rx.try_recv().unwrap()).unwrap().kind, LIST_RESP);
conn.handle(&encode(DETACH, id, &[]));
}
}
#[test]
fn open_spawns_shell_and_streams_output() {
for requested_id in [0, u32::MAX] {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
+26
View File
@@ -0,0 +1,26 @@
use std::sync::Arc;
use tokio::sync::Mutex;
use webrtc::{api::setting_engine::SettingEngine, ice::{mdns::MulticastDnsMode, network_type::NetworkType},
util::vnet::{net::{Net, NetConfig}, router::{Router, RouterConfig}}};
pub(crate) async fn virtual_network() -> (SettingEngine, SettingEngine, Arc<Mutex<Router>>) {
// Each pair owns its router and address space; no host interfaces or sockets are used.
let router = Arc::new(Mutex::new(Router::new(RouterConfig {
cidr: "192.0.2.0/24".into(), ..Default::default()
}).unwrap()));
let mut settings = Vec::new();
for ip in ["192.0.2.1", "192.0.2.2"] {
let net = Arc::new(Net::new(Some(NetConfig { static_ips: vec![ip.into()], ..Default::default() })));
let nic = net.get_nic().unwrap();
router.lock().await.add_net(nic.clone()).await.unwrap();
nic.lock().await.set_router(router.clone()).await.unwrap();
let mut setting = SettingEngine::default();
setting.set_vnet(Some(net));
setting.set_network_types(vec![NetworkType::Udp4]);
setting.set_ice_multicast_dns_mode(MulticastDnsMode::Disabled);
settings.push(setting);
}
router.lock().await.start().await.unwrap();
let right = settings.pop().unwrap();
(settings.pop().unwrap(), right, router)
}
+737 -104
View File
File diff suppressed because it is too large. Load diff
+174
View File
@@ -0,0 +1,174 @@
//! One negotiated video m-line/SSRC, with codec-specific payload types.
//! A single RTP sequence/timestamp survives format changes (never two independent SSRC timelines).
use std::{any::Any, sync::atomic::{AtomicBool, Ordering}};
use async_trait::async_trait;
use tokio::sync::Mutex;
use webrtc::{error::{Error, Result}, media::Sample, rtp::{packet::Packet, header::Header, packetizer::Payloader}};
use webrtc::rtp_transceiver::rtp_codec::{RTCRtpCodecCapability, RTCRtpCodecParameters, RTPCodecType};
use webrtc::track::track_local::{TrackLocal, TrackLocalContext, track_local_static_rtp::TrackLocalStaticRTP};
use crate::capture_setting::VideoCodec;
/// Official SDK GetSupportedFormats constructs H265 with an empty parameter map.
pub fn register_hevc(media: &mut webrtc::api::media_engine::MediaEngine) -> Result<()> {
media.register_codec(RTCRtpCodecParameters { capability: RTCRtpCodecCapability {
mime_type: VideoCodec::H265.mime().into(), clock_rate: 90_000,
rtcp_feedback: vec![webrtc::rtp_transceiver::RTCPFeedback { typ: "nack".into(), parameter: String::new() },
webrtc::rtp_transceiver::RTCPFeedback { typ: "nack".into(), parameter: "pli".into() }],
..Default::default() }, payload_type: 126, ..Default::default() }, RTPCodecType::Video)
}
#[derive(Debug)]
struct Timeline { sequence: u16, timestamp: u32, h264: webrtc::rtp::codecs::h264::H264Payloader }
#[derive(Debug)]
pub struct VideoTrack {
h264: TrackLocalStaticRTP, h265: TrackLocalStaticRTP,
has_h264: AtomicBool, has_h265: AtomicBool, timeline: Mutex<Timeline>,
progress: tokio::sync::watch::Sender<(u64, VideoCodec)>,
}
impl VideoTrack {
pub fn new() -> Self {
let track = |codec: VideoCodec| TrackLocalStaticRTP::new(
RTCRtpCodecCapability { mime_type: codec.mime().into(), clock_rate: 90_000, ..Default::default() },
"video_0".into(), "video_0".into());
let (progress, _) = tokio::sync::watch::channel((0, VideoCodec::H264));
Self { progress, h264: track(VideoCodec::H264), h265: track(VideoCodec::H265), has_h264: AtomicBool::new(false),
has_h265: AtomicBool::new(false), timeline: Mutex::new(Timeline { sequence: rand::random(), timestamp: rand::random(), h264: Default::default() }) }
}
pub fn supports(&self, codec: VideoCodec) -> bool {
match codec { VideoCodec::H264 => &self.has_h264, VideoCodec::H265 => &self.has_h265 }.load(Ordering::Acquire)
}
pub fn revision(&self) -> u64 { self.progress.borrow().0 }
/// Format-change RPC success follows a transmitted frame, including a negotiated fallback.
pub async fn wait_for_applied(&self, since: u64, settings: &tokio::sync::watch::Sender<crate::capture_setting::VideoSettings>) -> bool {
let mut progress = self.progress.subscribe();
tokio::time::timeout(std::time::Duration::from_secs(10), async {
loop {
let (revision, codec) = *progress.borrow_and_update();
if revision > since && codec == settings.borrow().codec { return true; }
if progress.changed().await.is_err() { return false; }
}
}).await.unwrap_or(false)
}
pub async fn write_sample_with_extensions(&self, codec: VideoCodec, sample: &Sample, extensions: &[webrtc::rtp::extension::HeaderExtension]) -> Result<()> {
if !self.supports(codec) { return Err(Error::ErrUnsupportedCodec); }
let track = match codec { VideoCodec::H264 => &self.h264, VideoCodec::H265 => &self.h265 };
if track.all_binding_paused().await { return Ok(()); }
let mut state = self.timeline.lock().await;
// Leave room for the RTP header and negotiated UU extensions below the 1200 byte MTU.
let payloads = match codec {
VideoCodec::H264 => state.h264.payload(1160, &sample.data).map_err(Error::Rtp)?,
VideoCodec::H265 => crate::hevc::payload(1160, &sample.data).map_err(Error::new)?,
};
let count = payloads.len();
let mut complete = count > 0;
let timestamp = state.timestamp;
state.timestamp = state.timestamp.wrapping_add((sample.duration.as_secs_f64() * 90_000.0) as u32);
for (index, payload) in payloads.into_iter().enumerate() {
let packet = Packet { header: Header { version: 2, sequence_number: state.sequence, timestamp,
marker: index + 1 == count, ..Default::default() }, payload };
state.sequence = state.sequence.wrapping_add(1);
complete &= track.write_rtp_with_extensions(&packet, extensions).await? > 0;
}
if complete { self.progress.send_modify(|value| { value.0 += 1; value.1 = codec; }); }
Ok(())
}
}
#[async_trait]
impl TrackLocal for VideoTrack {
async fn bind(&self, context: &TrackLocalContext) -> Result<RTCRtpCodecParameters> {
let h264 = self.h264.bind(context).await.ok();
let h265 = self.h265.bind(context).await.ok();
self.has_h264.store(h264.is_some(), Ordering::Release);
self.has_h265.store(h265.is_some(), Ordering::Release);
log::info!("Video track negotiated: h264={} h265={}", h264.is_some(), h265.is_some());
h264.or(h265).ok_or(Error::ErrUnsupportedCodec)
}
async fn unbind(&self, context: &TrackLocalContext) -> Result<()> {
if self.has_h264.swap(false, Ordering::AcqRel) { self.h264.unbind(context).await?; }
if self.has_h265.swap(false, Ordering::AcqRel) { self.h265.unbind(context).await?; }
Ok(())
}
fn id(&self) -> &str { "video_0" }
fn stream_id(&self) -> &str { "video_0" }
fn kind(&self) -> RTPCodecType { RTPCodecType::Video }
fn as_any(&self) -> &dyn Any { self }
}
#[cfg(test)]
mod tests {
use super::*;
use webrtc::{api::{APIBuilder, media_engine::MediaEngine}, peer_connection::configuration::RTCConfiguration};
async fn negotiate(h264: bool, hevc: bool) {
let mut remote_media = MediaEngine::default();
if h264 { remote_media.register_codec(RTCRtpCodecParameters { capability: RTCRtpCodecCapability {
mime_type: VideoCodec::H264.mime().into(), clock_rate: 90_000,
sdp_fmtp_line: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f".into(), ..Default::default()
}, payload_type: 102, ..Default::default() }, RTPCodecType::Video).unwrap(); }
if hevc { register_hevc(&mut remote_media).unwrap(); }
let (remote_setting, host_setting, router) = crate::test_network::virtual_network().await;
let remote = APIBuilder::new().with_setting_engine(remote_setting).with_media_engine(remote_media).build().new_peer_connection(RTCConfiguration::default()).await.unwrap();
remote.add_transceiver_from_kind(RTPCodecType::Video, None).await.unwrap();
let mut media = MediaEngine::default(); media.register_default_codecs().unwrap(); register_hevc(&mut media).unwrap();
let host = APIBuilder::new().with_setting_engine(host_setting).with_media_engine(media).build().new_peer_connection(RTCConfiguration::default()).await.unwrap();
let (packets_tx, mut packets_rx) = tokio::sync::mpsc::unbounded_channel();
remote.on_track(Box::new(move |remote_track, _, _| {
let packets_tx = packets_tx.clone();
Box::pin(async move {
tokio::spawn(async move {
while let Ok((packet, _)) = remote_track.read_rtp().await {
if packets_tx.send(packet).is_err() { break; }
}
});
})
}));
let track = std::sync::Arc::new(VideoTrack::new());
host.add_track(track.clone()).await.unwrap();
let offer = remote.create_offer(None).await.unwrap();
let mut gathered = remote.gathering_complete_promise().await;
remote.set_local_description(offer).await.unwrap();
gathered.recv().await;
host.set_remote_description(remote.local_description().await.unwrap()).await.unwrap();
let answer = host.create_answer(None).await.unwrap();
assert_eq!(answer.sdp.contains("H265/90000"), hevc);
assert_eq!(answer.sdp.contains("H264/90000"), h264);
let mut gathered = host.gathering_complete_promise().await;
host.set_local_description(answer).await.unwrap();
gathered.recv().await;
remote.set_remote_description(host.local_description().await.unwrap()).await.unwrap();
tokio::time::timeout(std::time::Duration::from_secs(10), async {
while !track.supports(VideoCodec::H264) && !track.supports(VideoCodec::H265) { tokio::time::sleep(std::time::Duration::from_millis(10)).await; }
}).await.unwrap();
assert_eq!(track.supports(VideoCodec::H264), h264);
assert_eq!(track.supports(VideoCodec::H265), hevc);
tokio::time::timeout(std::time::Duration::from_secs(30), async {
while host.connection_state() != webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Connected {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
}).await.unwrap();
let codecs = if !h264 { vec![VideoCodec::H265] } else if hevc { vec![VideoCodec::H264, VideoCodec::H265, VideoCodec::H264] } else { vec![VideoCodec::H264] };
let mut previous: Option<Packet> = None;
for codec in codecs {
let data = match codec { VideoCodec::H264 => bytes::Bytes::from_static(&[0,0,1,65,1,2]),
VideoCodec::H265 => bytes::Bytes::from_static(&[0,0,1,38,1,2]) };
let (settings, _) = tokio::sync::watch::channel(crate::capture_setting::VideoSettings { codec, ..Default::default() });
let applied = track.wait_for_applied(track.revision(), &settings);
tokio::pin!(applied);
assert!(tokio::time::timeout(std::time::Duration::from_millis(1), &mut applied).await.is_err());
track.write_sample_with_extensions(codec, &Sample { data, duration: std::time::Duration::from_millis(20), ..Default::default() }, &[]).await.unwrap();
assert!(applied.await);
let packet = tokio::time::timeout(std::time::Duration::from_secs(3), packets_rx.recv()).await.unwrap().unwrap();
assert_eq!(packet.header.payload_type, if codec == VideoCodec::H264 { 102 } else { 126 });
assert!(packet.header.marker);
if let Some(previous) = previous {
assert_eq!(packet.header.ssrc, previous.header.ssrc);
assert_eq!(packet.header.sequence_number, previous.header.sequence_number.wrapping_add(1));
assert_eq!(packet.header.timestamp, previous.header.timestamp.wrapping_add(1800));
}
previous = Some(packet);
}
host.close().await.unwrap(); remote.close().await.unwrap();
router.lock().await.stop().await.unwrap();
}
#[tokio::test]
async fn negotiated_codecs_only_and_legacy_h264_offer() { negotiate(true, false).await; negotiate(true, true).await; negotiate(false, true).await; }
}
+36 -6
View File
@@ -26,7 +26,7 @@ pub fn init() -> Result<(u32,u32),String> {
return Ok((w as u32,h as u32));
}
#[cfg(not(all(target_os="linux", feature="wayland")))]
Err("Wayland backend requires Linux and a build with --features wayland".into())
Err("this build was compiled without the wayland feature; rebuild with --features wayland".into())
}
pub fn capture(buffer:&mut [u8]) -> Result<(),String> {
#[cfg(all(target_os="linux", feature="wayland"))]
@@ -60,19 +60,49 @@ pub fn key(symbol:u64,pressed:bool)->i32 {
}
/// A separate process owns capture; encoder cancellation closes its pipe.
/// Capture and the pipe write run on separate threads with two frame buffers, so a
/// slow encoder read no longer delays the next screencopy request.
pub fn capture_stdout() -> Result<(),String> {
use std::io::Write;
use std::io::{Read,Write};
let (w,h)=init()?;
let mut frame=vec![0u8;(w as usize)*(h as usize)*4];
let mut stdout=std::io::stdout().lock();
let size=(w as usize)*(h as usize)*4;
let fps=capture_fps(std::env::var("UU_WAYLAND_FPS").ok().as_deref())?;
let interval=std::time::Duration::from_nanos(1_000_000_000/u64::from(fps));
// A larger pipe absorbs more of each frame per write; failure keeps the default.
#[cfg(target_os="linux")]
let _=unsafe { libc::fcntl(1, libc::F_SETPIPE_SZ, 1<<20) };
let (full_tx,full_rx)=std::sync::mpsc::sync_channel::<Vec<u8>>(1);
let (free_tx,free_rx)=std::sync::mpsc::sync_channel::<Vec<u8>>(2);
for _ in 0..2 { free_tx.send(vec![0u8;size]).map_err(|_|"Capture buffers")?; }
let writer=std::thread::Builder::new().name("uu-wl-write".into()).spawn(move || -> Result<(),String> {
let mut stdout=std::io::stdout().lock();
for frame in full_rx {
stdout.write_all(&frame).map_err(|e|format!("Capture pipe: {e}"))?;
stdout.flush().map_err(|e|format!("Capture pipe: {e}"))?;
if free_tx.send(frame).is_err() { break; }
}
Ok(())
}).map_err(|e|format!("Capture writer: {e}"))?;
let demand=std::env::var("UU_WAYLAND_DEMAND").is_ok_and(|v|v=="1");
let mut stdin=std::io::stdin().lock();
let mut delivered=std::time::Instant::now();
loop {
if demand { stdin.read_exact(&mut [0]).map_err(|_|"Capture demand pipe closed")?; }
// Time from buffer availability: a slow encoder read must not shorten the next interval.
let Ok(mut frame)=free_rx.recv() else { break };
let start=std::time::Instant::now();
// The engine prefetches the next frame on delivery; after a backpressure stall that
// prefetch is as old as the stall, so drop it and capture the current screen.
if demand && start.duration_since(delivered)>interval*2 { capture(&mut frame)?; }
capture(&mut frame)?;
stdout.write_all(&frame).map_err(|e|format!("Capture pipe: {e}"))?;
if let Some(left)=interval.checked_sub(start.elapsed()) { std::thread::sleep(left); }
delivered=std::time::Instant::now();
if full_tx.send(frame).is_err() { break; }
if !demand {
if let Some(left)=interval.checked_sub(start.elapsed()) { std::thread::sleep(left); }
}
}
drop(full_tx);
writer.join().map_err(|_|"Capture writer panicked")?
}
pub(crate) fn capture_fps(value:Option<&str>)->Result<u32,String> {
+362 -43
View File
@@ -25,6 +25,15 @@ use webrtc::dtls_transport::dtls_role::DTLSRole;
use webrtc::track::track_local::track_local_static_sample::TrackLocalStaticSample;
use webrtc::track::track_local::TrackLocalWriter;
fn is_capture_setting_message(data: &[u8]) -> bool {
use crate::protocol::WireValue;
let Ok(fields) = crate::protocol::decode_wire(data) else { return false; };
fields.iter().find_map(|field| match (field.number, &field.value) {
(21, WireValue::Bytes(rpc)) => crate::protocol::decode_wire(rpc).ok(),
_ => None,
}).is_some_and(|rpc| rpc.iter().any(|field| matches!(field.number, 2 | 3 | 4 | 6)))
}
#[derive(Debug, Default, Clone)]
pub struct SessionContext {
pub client_id: Option<String>,
@@ -48,11 +57,15 @@ impl WebRtcSession {
signaling_client: Arc<crate::signaling::SignalingClient>,
desktop: bool,
transport:crate::session_ice::SessionIce,
portal_desktop: Option<Arc<crate::desktop_session::PortalDesktop>>,
) -> Result<Self, String> {
let mut m = MediaEngine::default();
m.register_default_codecs()
.map_err(|e| format!("Register standard codecs failed: {}", e))?;
let hevc = desktop && crate::video::hevc_available().await;
if hevc { crate::video_track::register_hevc(&mut m).map_err(|e| format!("Register HEVC failed: {e}"))?; }
m.register_header_extension(
webrtc::rtp_transceiver::rtp_codec::RTCRtpHeaderExtensionCapability { uri: crate::rtp_metadata::CAPTURE_INDEX_URI.into() },
webrtc::rtp_transceiver::rtp_codec::RTPCodecType::Video, None,
@@ -65,6 +78,8 @@ impl WebRtcSession {
webrtc::interceptor::registry::Registry::new(), &mut m,
).map_err(|e| format!("Register RTP/RTCP interceptors failed: {}", e))?;
let mut se = webrtc::api::setting_engine::SettingEngine::default();
#[cfg(test)]
if let Some(setting) = &transport.setting_engine { se = setting.clone(); }
se.set_answering_dtls_role(DTLSRole::Server)
.map_err(|e| format!("Configure DTLS answer role failed: {}", e))?;
se.set_ice_multicast_dns_mode(webrtc::ice::mdns::MulticastDnsMode::Disabled);
@@ -162,7 +177,7 @@ impl WebRtcSession {
tokio::spawn(async move {
let ctx = ctx.lock().await.clone();
if let (Some(client), Some(ice)) = (&ctx.client_id, ctx.ice_id.as_ref().and_then(|v| v.as_str())) {
let payload = crate::capability::h264_device_capability(client, ice, 0, crate::video::FPS as i32, 5);
let payload = crate::capability::device_capability(client, ice, 0, crate::video::display_fps() as i32, 5, hevc);
if let Err(e) = sig.emit_event(crate::capability::FORWARD_SETTING_EVENT, payload).await {
log::error!("Device capability notification failed: {}", e);
}
@@ -179,14 +194,7 @@ impl WebRtcSession {
}));
// 注册视频轨
let video_track = Arc::new(TrackLocalStaticSample::new(
RTCRtpCodecCapability {
mime_type: "video/H264".to_owned(),
..Default::default()
},
"video_0".to_owned(),
"video_0".to_owned(),
));
let video_track = Arc::new(crate::video_track::VideoTrack::new());
// Set by PLI/FIR; the desktop encoder answers with an IDR on its next frame.
let idr_request = Arc::new(std::sync::atomic::AtomicBool::new(false));
@@ -226,7 +234,7 @@ impl WebRtcSession {
});
// Desktop sound on the viewer's audio m-line (it always offers Opus).
if desktop && crate::audio::enabled() {
if desktop && crate::audio::enabled() && !cfg!(test) {
let audio_track = Arc::new(TrackLocalStaticSample::new(
RTCRtpCodecCapability { mime_type: "audio/opus".to_owned(), clock_rate: 48000, channels: 2, ..Default::default() },
"audio_0".to_owned(), "audio_0".to_owned(),
@@ -251,7 +259,7 @@ impl WebRtcSession {
// Per-connection mute / lock-after-session settings (安全 menu).
let peer_actions = Arc::new(crate::system_actions::PeerActions::default());
let desktop_sound = desktop && crate::audio::enabled();
if desktop {
if desktop && !cfg!(test) {
let (end_pc, actions) = (pc.clone(), peer_actions.clone());
tokio::spawn(async move {
{
@@ -273,11 +281,39 @@ impl WebRtcSession {
});
}
let portal=capture.portal.clone();
let stream_desktop = portal_desktop.clone();
// File/terminal/port-mapping connections carry no desktop video.
if desktop {
let control_video_track = video_track.clone();
let stream_settings = video_settings.clone();
if desktop && !cfg!(test) {
let state_pc = streaming_pc.clone();
tokio::spawn(async move {
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, idr_request).await {
log::error!("Live desktop stream stopped: {}", e);
let screen = if let Some(desktop) = stream_desktop {
if let Err(error) = crate::locked_video::run(&streaming_pc, &video_track, &desktop).await {
log::warn!("Locked desktop video unavailable: {error}");
}
let mut updates = desktop.subscribe();
loop {
let state = updates.borrow_and_update().clone();
if state.ready() { break Some(state.screen); }
if !state.waiting { return; }
if crate::video::transport_closed(&streaming_pc) { return; }
tokio::select! {
changed = updates.changed() => if changed.is_err() { return; },
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
}
}
} else { None };
let (capture_size, capture_display, portal) = screen.as_ref().map_or(
(capture_size, capture_display, portal),
|screen| ((screen.width, screen.height), screen.display.clone(), screen.portal.clone()));
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, stream_settings, idr_request).await {
// A send racing the viewer's close fails with a misleading track error.
if matches!(state_pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed) {
log::info!("Live desktop stream ended: peer connection closed ({})", e);
} else {
log::error!("Live desktop stream stopped: {}", e);
}
}
});
} else {
@@ -285,12 +321,25 @@ impl WebRtcSession {
}
let input_clone = input.clone();
let control_capture = capture.clone();
let desktop_pc = Arc::downgrade(&pc);
let control_sequence = Arc::new(std::sync::atomic::AtomicU64::new(1));
// Capability belongs to the granted session, including while GNOME is
// locked. Echo can precede TEXT readiness and CONTROL auto-unlock;
// advertising false here would disable clipboard for the entire peer.
let portal_clipboard_supported = desktop && capture.portal.as_ref().is_some_and(|session| session.clipboard_supported());
let portal_clipboard_supported = desktop && portal_desktop.as_ref().map_or_else(
|| capture.portal.as_ref().is_some_and(|session| session.clipboard_supported()),
|manager| {
let state = manager.snapshot();
state.ready()
&& state.screen.portal.as_ref().is_some_and(|session| session.clipboard_supported())
});
let text_clipboard_ready = Arc::new(std::sync::atomic::AtomicBool::new(portal_clipboard_supported));
let last_echo_request = Arc::new(std::sync::atomic::AtomicU64::new(0));
let echo_seen = Arc::new(std::sync::atomic::AtomicBool::new(false));
// Same watcher the desktop session uses: the manager's when Portal owns the
// session, otherwise the global lock watcher. Unknown state fails closed below.
let channel_lock_state = portal_desktop.as_ref().map_or_else(
crate::session_lock::watch_locked, |manager| manager.lock_state());
// One file-transfer session per peer. The viewer may send FileTransferFtp RPCs on CONTROL,
// but it receives file traffic only through streamerDidReceiveFileData, so every reply and
// host-initiated request goes out on FILE_DATA_CHANNEL.
@@ -333,11 +382,17 @@ impl WebRtcSession {
let input_inst = input_clone.clone();
let channel_input_session=input_session.clone();
let screen = control_capture.clone();
let portal_desktop = portal_desktop.clone();
let desktop_pc = desktop_pc.clone();
let out_sequence = control_sequence.clone();
let shared_clipboard_ready = text_clipboard_ready.clone();
let last_echo_request = last_echo_request.clone();
let echo_seen = echo_seen.clone();
let channel_lock_state = channel_lock_state.clone();
let peer_files = peer_files.clone();
let peer_port_mapping = port_mapping.clone();
let peer_video_settings = video_settings.clone();
let peer_video_track = control_video_track.clone();
let peer_actions = peer_actions.clone();
if label == "FILE_DATA_CHANNEL" { *file_channel.lock().unwrap() = Some(d.clone()); }
@@ -345,7 +400,6 @@ impl WebRtcSession {
let lbl = label.clone();
let inp = input_inst.clone();
let d_reply = d.clone();
let input_size = (screen.width, screen.height);
let reply_sequence = out_sequence.clone();
let channel_video_settings = peer_video_settings.clone();
let channel_actions = peer_actions.clone();
@@ -373,6 +427,88 @@ impl WebRtcSession {
}
});
}
let (capture_tx, mut capture_rx) = tokio::sync::mpsc::channel::<Vec<u8>>(16);
if label == "CONTROL_DATA_CHANNEL" {
let settings = channel_video_settings.clone();
let track = peer_video_track.clone();
let response_tx = outbound.clone();
let sequence = reply_sequence.clone();
let manager = portal_desktop.clone();
let channel = Arc::downgrade(&d);
let initial_screen = screen.clone();
tokio::spawn(async move {
let mut health = tokio::time::interval(std::time::Duration::from_millis(100));
loop {
let request = tokio::select! {
request = capture_rx.recv() => match request { Some(request) => request, None => break },
_ = health.tick() => {
if channel.upgrade().is_none_or(|channel| matches!(
channel.ready_state(),
webrtc::data_channel::data_channel_state::RTCDataChannelState::Closing
| webrtc::data_channel::data_channel_state::RTCDataChannelState::Closed
)) { break; }
continue;
}
};
let active_screen = if desktop {
if let Some(manager) = &manager {
loop {
let state = manager.snapshot();
if !state.waiting && state.screen.init_error.is_some() {
log::warn!("Capture setting request dropped: Portal desktop unavailable");
break None;
}
let live = state.screen.init_error.is_none()
&& (cfg!(test) || state.screen.portal.as_ref().is_some_and(|portal| portal.is_alive()))
&& *manager.lock_state().borrow() == Some(false);
if live { break Some(state.screen); }
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
if channel.upgrade().is_none_or(|channel| channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open) { return; }
}
} else {
Some(initial_screen.clone())
}
} else {
Some(initial_screen.clone())
};
let Some(active_screen) = active_screen else { continue; };
let input_size = if active_screen.init_error.is_none() {
(active_screen.width, active_screen.height)
} else { (1920, 1080) };
let Ok(fields) = crate::protocol::decode_wire(&request) else { continue; };
use crate::protocol::WireValue;
let Some(rpc) = fields.iter().find_map(|f| match (f.number, &f.value) {
(21, WireValue::Bytes(rpc)) => Some(*rpc), _ => None,
}) else { continue; };
let Ok(rpc_fields) = crate::protocol::decode_wire(rpc) else { continue; };
let before = *settings.borrow();
let output_before = before.output;
let revision = track.revision();
let Some(response) = crate::capture_setting::handle_rpc_with_codecs(
&rpc_fields, &settings, input_size,
track.supports(crate::capture_setting::VideoCodec::H264),
track.supports(crate::capture_setting::VideoCodec::H265)) else { continue; };
let codec_changed = settings.borrow().codec != before.codec;
if desktop && codec_changed && !track.wait_for_applied(revision, &settings).await {
log::warn!("Codec switch did not produce a transmitted frame; success reply withheld");
continue;
}
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default().as_secs();
let next = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let _ = response_tx.send(crate::initialization::envelope(next(), timestamp, 22, &response));
let current = *settings.borrow();
if current.output != output_before {
match crate::capture_setting::screen_sources(next(), timestamp, input_size, current.output, "Linux desktop") {
Ok(screens) => { let _ = response_tx.send(screens); }
Err(error) => log::warn!("Screen sources update failed: {}", error),
}
}
let sync = crate::capture_setting::sync_payload(&current, input_size);
let _ = response_tx.send(crate::initialization::envelope(next(), timestamp, 25, &sync));
}
});
}
// The viewer sends FileTransferFtp RPCs (Message 21/22) on CONTROL; accept them on TEXT/FILE too.
let file_session = if matches!(label.as_str(), "CONTROL_DATA_CHANNEL" | "TEXT_DATA_CHANNEL" | "FILE_DATA_CHANNEL") {
Some(peer_files.clone())
@@ -386,21 +522,59 @@ impl WebRtcSession {
let initial_sequence = out_sequence.clone();
let clipboard_sink = clipboard_input.clone();
let clipboard_ready = clipboard_enabled.clone();
let last_echo_request = last_echo_request.clone();
let echo_seen = echo_seen.clone();
let rich_outgoing=rich_state.clone();
let open_actions = channel_actions.clone();
let open_video = channel_video_settings.clone();
let open_track = peer_video_track.clone();
let open_desktop = portal_desktop.clone();
let open_owner = channel_input_session.clone();
let open_screen = screen.clone();
d.on_open(Box::new(move || {
let init_actions = open_actions.clone();
let init_video = open_video.clone();
let init_track = open_track.clone();
let channel = channel.clone();
let sequence = initial_sequence.clone();
let screen = screen.clone();
let screen = open_screen.clone();
let clipboard_sink = clipboard_sink.clone();
let clipboard_ready = clipboard_ready.clone();
let rich_outgoing=rich_outgoing.clone();
let portal_desktop = open_desktop.clone();
let owner = open_owner.clone();
let pc = desktop_pc.upgrade();
Box::pin(async move {
// Permission dialogs and first-frame waits must not occupy the
// channel-open callback or delay CONTROL message dispatch.
tokio::spawn(async move {
if channel.label() == "TEXT_DATA_CHANNEL" {
if !desktop { return; }
let screen = if let Some(desktop) = &portal_desktop {
let mut updates = desktop.subscribe();
let Some(_) = crate::desktop_session::wait_ready(&mut updates, &channel).await else { return; };
let mut locked = desktop.lock_state();
loop {
let state = desktop.snapshot();
if !state.waiting && state.screen.init_error.is_some() { return; }
if let Some(session) = state.screen.portal.as_ref() {
if session.is_alive() && *locked.borrow() == Some(false) {
break state.screen;
}
}
tokio::select! {
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
changed = locked.changed() => if changed.is_err() { return; },
_ = async {
loop {
if matches!(channel.ready_state(), webrtc::data_channel::data_channel_state::RTCDataChannelState::Closing
| webrtc::data_channel::data_channel_state::RTCDataChannelState::Closed) { return; }
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
} => return,
}
}
} else { screen };
let bridge = if let Some(session)=screen.portal.clone() { crate::clipboard::start_portal(session) } else { crate::clipboard::start(screen.display.clone()) };
*clipboard_sink.lock().unwrap() = Some(bridge.incoming);
let clipboard_channel = channel.clone();
@@ -445,6 +619,92 @@ impl WebRtcSession {
});
return;
}
let early_desktop_selection = desktop && portal_desktop.is_some();
if early_desktop_selection {
// Android 4.43.0's unlock presenter requires CaptureChange
// CT_DESKTOP/CT_WINDOW as well as SecureDesktopState before
// sending UnlockRemoteSystem, even with no video frames.
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let message = crate::initialization::encode_capture_change(
sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
timestamp, 0, 0, "Linux desktop").expect("desktop capture type");
if let Err(error) = channel.send(&bytes::Bytes::from(message)).await {
log::error!("CONTROL desktop selection failed: {error}");
return;
}
log::info!("CONTROL initialization field 8 sent (desktop selection before capture)");
}
if desktop {
// Report secure-desktop state before waiting for capture,
// so AUTO_UNLOCK_SYSTEM can arrive on this same CONTROL.
let state = portal_desktop.as_ref().map_or_else(
crate::session_lock::watch_locked, |desktop| desktop.lock_state());
tokio::spawn(crate::session_lock::report_states(channel.clone(), sequence.clone(), state));
}
if let Some(manager) = &portal_desktop {
let mut updates = manager.subscribe();
let channel = channel.clone();
let sequence = sequence.clone();
let echo_request = last_echo_request.clone();
let clipboard_ready = clipboard_ready.clone();
tokio::spawn(async move {
let mut health = tokio::time::interval(std::time::Duration::from_millis(100));
loop {
tokio::select! {
changed = updates.changed() => if changed.is_err() { break; },
_ = health.tick() => {
if channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open { break; }
continue;
}
}
let state = updates.borrow_and_update().clone();
let supported = state.ready()
&& state.screen.portal.as_ref().is_some_and(|portal| portal.clipboard_supported());
clipboard_ready.store(supported, std::sync::atomic::Ordering::Release);
// echo_seen is published after the request seq, so reading it
// first guarantees a non-stale sequence for the replay.
if supported && echo_seen.load(std::sync::atomic::Ordering::Acquire) {
let request_seq = echo_request.load(std::sync::atomic::Ordering::Acquire);
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let response = crate::control_protocol::encode_echo_response_with_clipboard(
sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
timestamp, request_seq, true);
let _ = channel.send(&bytes::Bytes::from(response)).await;
}
if !state.waiting && state.screen.init_error.is_some() { break; }
}
});
}
let screen = if let Some(portal) = &portal_desktop {
if !cfg!(test) {
if let (Some(owner), Some(pc)) = (owner, pc) {
tokio::spawn(portal.clone().run(pc, channel.clone(), owner));
}
}
let snapshot = portal.snapshot();
if !snapshot.ready() {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let message = crate::initialization::envelope(
sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 13,
&init_actions.metrics_payload(desktop_sound, snapshot.permission(true)));
if channel.send(&bytes::Bytes::from(message)).await.is_err() { return; }
let mut updates = portal.subscribe();
match crate::desktop_session::wait_ready(&mut updates, &channel).await {
Some(screen) => screen,
None => {
if channel.ready_state() == webrtc::data_channel::data_channel_state::RTCDataChannelState::Open {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let message = crate::initialization::envelope(
sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 13,
&init_actions.metrics_payload(desktop_sound, crate::system_metrics::Permission::Reject));
let _ = channel.send(&bytes::Bytes::from(message)).await;
}
return;
}
}
} else { snapshot.screen }
} else { screen };
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let next_seq = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -462,9 +722,19 @@ impl WebRtcSession {
(13, Ok(crate::initialization::envelope(next_seq(), timestamp, 13, &init_actions.metrics_payload(desktop_sound, desktop_permission)))),
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
&crate::system_metrics::remote_download_path(&home)))),
(18, Ok(crate::initialization::envelope(next_seq(), timestamp, 18,
&crate::capture_setting::codec_negotiation(screen_size, crate::video::display_fps(), init_track.supports(crate::capture_setting::VideoCodec::H265))))),
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
];
if desktop && screen.init_error.is_none() {
let codec = crate::capture_setting::preferred_codec(init_track.supports(crate::capture_setting::VideoCodec::H264),
init_track.supports(crate::capture_setting::VideoCodec::H265));
init_video.send_if_modified(|current| if current.codec != codec {
log::info!("Desktop codec selected: {:?}", codec); current.codec = codec; true } else { false });
}
for (field, message) in messages {
if early_desktop_selection && field == 8 { continue; }
if (!desktop || screen.init_error.is_some()) && field == 18 { continue; }
if desktop && screen.init_error.is_some() && matches!(field,7|8) { continue; }
let message = match message {
Ok(message) => message,
@@ -478,7 +748,6 @@ impl WebRtcSession {
}
// Mirror the Linux pointer shape; the video itself carries no pointer.
if !desktop { return; }
tokio::spawn(crate::session_lock::report_states(channel.clone(), sequence.clone()));
let mut shapes = crate::cursor::monitor(&screen.display);
let mut health = tokio::time::interval(std::time::Duration::from_millis(200));
let mut sent = 0usize;
@@ -502,12 +771,19 @@ impl WebRtcSession {
}
}
}
});
})
}));
}
d.on_message(Box::new(move |msg: DataChannelMessage| {
// Keep the whole state: a cached screen waiting for a closed Portal is not ready for input.
let (screen, desktop_ready) = portal_desktop.as_ref().map_or_else(|| (screen.clone(), true), |desktop| {
let state = desktop.snapshot();
let ready = state.ready();
(state.screen, ready)
});
let channel_name = lbl.clone();
let input_worker = inp.clone();
let message_input_session=channel_input_session.clone();
@@ -521,33 +797,29 @@ impl WebRtcSession {
let rich_incoming=rich_state.clone();
let file_worker = file_session.clone();
let mapping = peer_port_mapping.clone();
let video_settings = channel_video_settings.clone();
let actions = channel_actions.clone();
let portal_for_message = portal_desktop.clone();
let lock_for_message = channel_lock_state.clone();
let echo_for_message = last_echo_request.clone();
let echo_seen_for_message = echo_seen.clone();
let capture_requests = capture_tx.clone();
Box::pin(async move {
let input_size = (screen.width, screen.height);
if let Some(term) = &terminal_worker {
if crate::terminal::is_frame(&msg.data) { term.handle(&msg.data); return; }
}
if channel_name == "CONTROL_DATA_CHANNEL" && is_capture_setting_message(&msg.data) {
// Back-pressure like the old inline handler instead of dropping the latest setting.
if capture_requests.send(msg.data.to_vec()).await.is_err() {
log::warn!("Capture setting queue closed; request dropped");
}
return;
}
if let Ok(fields) = crate::protocol::decode_wire(&msg.data) {
use crate::protocol::WireValue;
let rpc = fields.iter().find_map(|f| match (f.number, &f.value) { (21, WireValue::Bytes(b)) => Some(*b), _ => None });
let rpc_fields = rpc.and_then(|r| crate::protocol::decode_wire(r).ok());
let output_before = video_settings.borrow().output;
if let Some(response) = rpc_fields.as_deref().and_then(|r| crate::capture_setting::handle_rpc(r, &video_settings, input_size)) {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let next = || out_sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let _ = rpc_out.send(crate::initialization::envelope(next(), ts, 22, &response));
let current = *video_settings.borrow();
if current.output != output_before {
match crate::capture_setting::screen_sources(next(), ts, input_size, current.output, "Linux desktop") {
Ok(screens) => { let _ = rpc_out.send(screens); },
Err(e) => log::warn!("Screen sources update failed: {}", e),
}
}
let sync = crate::capture_setting::sync_payload(&current, input_size);
let _ = rpc_out.send(crate::initialization::envelope(next(), ts, 25, &sync));
return;
}
if let Some(response) = rpc_fields.as_deref().and_then(|r| crate::system_actions::plugin_response(r).or_else(|| crate::system_actions::session_rpc_response(r))) {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let _ = rpc_out.send(crate::initialization::envelope(out_sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 22, &response));
@@ -696,10 +968,17 @@ impl WebRtcSession {
// QuerySystemState { 1: SS_ID_SECURE_DESKTOP_STATE = 1 } asks for the lock state now.
let query = fields.iter().find_map(|f| match (&f.value, f.number) { (WireValue::Bytes(v), 16) => Some(*v), _ => None });
if let Some(query) = query {
let id = crate::protocol::decode_wire(query).ok().and_then(|q| q.iter().find_map(|f| match (&f.value, f.number) { (WireValue::Varint(v), 1) => Some(*v), _ => None }));
let query_fields = crate::protocol::decode_wire(query).ok();
let id = query_fields.as_ref().and_then(|q| q.iter().find_map(|f| match (&f.value, f.number) { (WireValue::Varint(v), 1) => Some(*v), _ => None }));
let shape = query_fields.as_ref().map(|fields| fields.iter().map(|field| match &field.value {
WireValue::Bytes(bytes) => (field.number, "bytes", bytes.len()),
_ => (field.number, "number", 0),
}).collect::<Vec<_>>());
log::info!("CONTROL system-state query: fields={:?} secure_desktop={}", shape, id == Some(1));
if id == Some(1) {
let locked = *crate::session_lock::watch_locked().borrow();
crate::session_lock::send_state(&reply_chan, &out_sequence, locked.unwrap_or(false)).await;
let locked = *lock_for_message.borrow();
// Unknown (no logind session, e.g. Xvfb) stays unlocked, as before.
crate::session_lock::send_state(&reply_chan, &out_sequence, locked == Some(true)).await;
}
}
if let Some(simple) = simple {
@@ -721,6 +1000,7 @@ impl WebRtcSession {
log::info!("SimpleAction {} ({}) received: value={:?} args={}", action,
crate::control_protocol::simple_action_name(action).unwrap_or("?"), value, sa::args_shape(&args));
let (actions, input, chan, seq_out) = (actions.clone(), input_worker.clone(), reply_chan.clone(), out_sequence.clone());
let settings_portal = portal_for_message.clone();
tokio::spawn(async move {
let worker = actions.clone();
let report = tokio::task::spawn_blocking(move || worker.handle(action, value, &args, || {
@@ -730,7 +1010,10 @@ impl WebRtcSession {
})).await.unwrap_or(false);
if report {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let message = crate::initialization::envelope(seq_out.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 13, &actions.metrics_payload(desktop_sound, crate::system_metrics::Permission::Allow));
let permission = settings_portal.as_ref().map_or(
crate::system_metrics::Permission::Allow,
|manager| manager.snapshot().permission(desktop));
let message = crate::initialization::envelope(seq_out.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 13, &actions.metrics_payload(desktop_sound, permission));
if let Err(e) = chan.send(&bytes::Bytes::from(message)).await { log::warn!("Settings report failed: {}", e); }
}
});
@@ -739,6 +1022,8 @@ impl WebRtcSession {
log::info!("SimpleAction {} ({}) not handled: args={}", action, crate::control_protocol::simple_action_name(action).unwrap_or("?"), sa::args_shape(args));
}
if action == 0 {
echo_for_message.store(seq, std::sync::atomic::Ordering::Release);
echo_seen_for_message.store(true, std::sync::atomic::Ordering::Release);
let response = crate::control_protocol::encode_echo_response_with_clipboard(
out_sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs(), seq,
@@ -746,10 +1031,7 @@ impl WebRtcSession {
if let Err(e) = reply_chan.send(&bytes::Bytes::from(response)).await {
log::error!("CONTROL echo response failed: {}", e);
} else {
static RESPONSES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
if RESPONSES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) == 0 {
log::info!("CONTROL protobuf echo request answered");
}
log::info!("CONTROL protobuf echo request answered");
}
}
}
@@ -762,6 +1044,8 @@ impl WebRtcSession {
if channel_name.contains("CONTROL") {
match crate::control_protocol::decode_control(&msg.data) {
Ok(crate::control_protocol::ControlMessage::Input(event)) => {
if screen.backend == crate::desktop_backend::DesktopBackend::Portal
&& (screen.init_error.is_some() || !desktop_ready || *lock_for_message.borrow() != Some(false)) { return; }
use crate::control_protocol::ControlInput;
// Separate receipt budgets keep motion floods from hiding clicks.
static RECEIPTS: [std::sync::atomic::AtomicUsize; 3] = [const { std::sync::atomic::AtomicUsize::new(0) }; 3];
@@ -865,6 +1149,33 @@ impl WebRtcSession {
};
log::info!("Offer x-uuremote-quic: {}", quic_summary);
log::info!("Offer includes UU capture-index extension: {}", sdp_str.lines().any(|l| l.starts_with("a=extmap:") && l.contains(crate::rtp_metadata::CAPTURE_INDEX_URI)));
let mut codecs: Vec<&str> = sdp_str.lines().filter_map(|l| l.strip_prefix("a=rtpmap:")).filter_map(|l| l.split_whitespace().nth(1))
.filter_map(|c| c.split('/').next()).collect();
codecs.dedup();
log::info!("Offer codecs: {:?}", codecs);
// SDP structure only: attribute names per section and public extmap URIs; never ufrag,
// password, fingerprint, candidate or fmtp values.
let mut sections: Vec<String> = Vec::new();
let mut current = String::from("session");
let mut names: std::collections::BTreeSet<String> = Default::default();
let mut flush = |current: &str, names: &mut std::collections::BTreeSet<String>, sections: &mut Vec<String>| {
sections.push(format!("{current}[{}]", names.iter().cloned().collect::<Vec<_>>().join(",")));
names.clear();
};
for line in sdp_str.lines() {
if let Some(m) = line.strip_prefix("m=") {
flush(&current, &mut names, &mut sections);
let mut parts = m.split_whitespace();
let kind = parts.next().unwrap_or("?");
current = format!("{kind}/{}pt", parts.skip(2).count());
} else if let Some(a) = line.strip_prefix("a=") {
let name = a.split([':', ' ']).next().unwrap_or("");
if name == "extmap" { if let Some(uri) = a.split_whitespace().nth(1) { names.insert(format!("extmap={}", uri.rsplit('/').next().unwrap_or(uri))); } }
else if !name.is_empty() && name.len() <= 32 { names.insert(name.to_owned()); }
}
}
flush(&current, &mut names, &mut sections);
log::info!("Offer SDP structure: {}", sections.join(" | "));
let offer = RTCSessionDescription::offer(sdp_str)
.map_err(|e| format!("Invalid offer: {}", e))?;
@@ -996,6 +1307,14 @@ mod tests {
}
}
#[cfg(test)]
#[path = "desktop_control_tests.rs"]
mod desktop_control_tests;
#[cfg(test)]
#[path = "webrtc_lifecycle_tests.rs"]
mod webrtc_lifecycle_tests;
/// Virtual/overlay interfaces never carry the UU video path (ZeroTier, containers).
fn ice_interface_allowed(name: &str) -> bool {
!["zt", "docker", "br-", "veth", "virbr", "lo"].iter().any(|prefix| name.starts_with(prefix))
+229
View File
@@ -0,0 +1,229 @@
//! CONTROL lifecycle regressions: an early Echo must be answered while the Portal
//! session is still pending, capture settings must be deferred (never dropped, never
//! blocking Echo/unlock) until the desktop is ready, and tool peers must apply
//! settings against the nominal screen without waiting for a desktop at all.
//! Local SCTP peers only; no cloud, desktop consent, audio or power operations.
use super::*;
use crate::protocol::{decode_wire, WireValue};
use std::{collections::HashMap, time::Duration};
use tokio::sync::mpsc;
type Messages = mpsc::UnboundedReceiver<(String, Vec<u8>)>;
struct Viewer {
pc: Arc<RTCPeerConnection>,
channels: HashMap<String, Arc<RTCDataChannel>>,
messages: Messages,
router: Arc<Mutex<webrtc::util::vnet::router::Router>>,
}
async fn connect(desktop: bool, portal: Option<Arc<crate::desktop_session::PortalDesktop>>)
-> (WebRtcSession, Viewer) {
let input = Arc::new(InputInjector::without_desktop());
let (host_setting, viewer_setting, router) = crate::test_network::virtual_network().await;
let screen = Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into()));
let host = WebRtcSession::new(screen, input, crate::signaling::SignalingClient::disconnected(),
desktop, crate::session_ice::SessionIce::virtual_net(host_setting), portal).await.unwrap();
let pc = Arc::new(APIBuilder::new().with_setting_engine(viewer_setting).build().new_peer_connection(RTCConfiguration::default()).await.unwrap());
let (tx, messages) = mpsc::unbounded_channel();
let mut channels = HashMap::new();
for label in ["CONTROL_DATA_CHANNEL"] {
let channel = pc.create_data_channel(label, None).await.unwrap();
let (tx, name) = (tx.clone(), label.to_string());
channel.on_message(Box::new(move |message| {
let _ = tx.send((name.clone(), message.data.to_vec()));
Box::pin(async {})
}));
channels.insert(label.to_string(), channel);
}
let mut gathered = pc.gathering_complete_promise().await;
pc.set_local_description(pc.create_offer(None).await.unwrap()).await.unwrap();
gathered.recv().await;
host.pc.set_remote_description(pc.local_description().await.unwrap()).await.unwrap();
let mut gathered = host.pc.gathering_complete_promise().await;
host.pc.set_local_description(host.pc.create_answer(None).await.unwrap()).await.unwrap();
gathered.recv().await;
pc.set_remote_description(host.pc.local_description().await.unwrap()).await.unwrap();
tokio::time::timeout(Duration::from_secs(8), async {
while channels.values().any(|channel| channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open) {
tokio::time::sleep(Duration::from_millis(20)).await;
}
}).await.expect("local SCTP channels did not open");
(host, Viewer { pc, channels, messages, router })
}
fn body(message: &[u8], number: u32) -> Option<&[u8]> {
decode_wire(message).ok()?.into_iter().find_map(|field| match (field.number, field.value) {
(n, WireValue::Bytes(bytes)) if n == number => Some(bytes), _ => None,
})
}
fn varint_of(message: &[u8], number: u32) -> Option<u64> {
decode_wire(message).ok()?.into_iter().find_map(|field| match (field.number, field.value) {
(n, WireValue::Varint(value)) if n == number => Some(value), _ => None,
})
}
fn put_varint(mut value: usize, out: &mut Vec<u8>) {
while value >= 128 { out.push((value as u8 & 127) | 128); value >>= 7; }
out.push(value as u8);
}
fn field_bytes(number: u32, value: &[u8]) -> Vec<u8> {
let mut out = Vec::new();
put_varint(((number << 3) | 2) as usize, &mut out);
put_varint(value.len(), &mut out);
out.extend_from_slice(value);
out
}
fn field_varint(number: u32, value: u64, out: &mut Vec<u8>) {
put_varint((number << 3) as usize, out);
put_varint(value as usize, out);
}
/// Message 21 RpcRequest {1 header{1 request_id}, <case>}, the verified viewer shape.
fn capture_rpc(request_id: u64, case: u32, setting: &[u8]) -> Vec<u8> {
let mut header = Vec::new();
field_varint(1, request_id, &mut header);
let mut rpc = field_bytes(1, &header);
rpc.extend(field_bytes(case, setting));
crate::initialization::envelope(11, 0, 21, &rpc)
}
/// Message 3 SimpleAction action 0: the viewer's FeatureFlag Echo request.
fn echo_request(seq: u64) -> Vec<u8> {
crate::initialization::envelope(seq, 0, 3, &[8, 0])
}
async fn receive(messages: &mut Messages, field: u32, within: Duration) -> Option<Vec<u8>> {
tokio::time::timeout(within, async {
loop {
let (channel, message) = messages.recv().await?;
if channel == "CONTROL_DATA_CHANNEL" && body(&message, field).is_some() { return Some(message); }
}
}).await.unwrap_or(None)
}
/// The Echo response advertises ff_clipboard (FeatureFlag field 8) only when true.
fn echo_advertises_clipboard(message: &[u8]) -> bool {
body(message, 3).and_then(|action| decode_wire(action).ok()).is_some_and(|action| {
action.iter().any(|field| match (field.number, &field.value) {
(4, WireValue::Bytes(flags)) => decode_wire(flags).ok().is_some_and(|flags| {
flags.iter().any(|flag| flag.number == 8)
}),
_ => false,
})
})
}
fn ready_screen() -> Arc<ScreenCapture> {
let mut screen = ScreenCapture::unavailable("synthetic ready desktop".into());
screen.backend = crate::desktop_backend::DesktopBackend::Portal;
screen.init_error = None;
screen.display = "wayland:test".into();
screen.width = 1920;
screen.height = 1080;
Arc::new(screen)
}
/// A Portal screen whose session reports clipboard support, for the refresh path.
/// `PortalSession` is a real native handle type, so this uses the manager's own
/// test publish hook with a session-less screen and asserts the negative path:
/// a ready screen without a Portal session must not advertise ff_clipboard.
#[tokio::test]
async fn ready_without_portal_session_does_not_advertise_clipboard() {
tokio::time::timeout(Duration::from_secs(20), async {
let (_, locked) = tokio::sync::watch::channel(Some(false));
let portal = Arc::new(crate::desktop_session::PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())), locked));
let (host, mut viewer) = connect(true, Some(portal.clone())).await;
// Establish the early Echo while the desktop is still pending: no clipboard claim.
viewer.channels["CONTROL_DATA_CHANNEL"].send(&echo_request(3).into()).await.unwrap();
let echo = receive(&mut viewer.messages, 3, Duration::from_secs(3)).await.expect("early Echo");
assert!(!echo_advertises_clipboard(&echo));
// Readiness with no Portal session keeps the capability false and must not
// manufacture an unsolicited Echo response from a timer tick.
portal.publish_test_screen(ready_screen());
assert!(receive(&mut viewer.messages, 3, Duration::from_millis(800)).await.is_none(),
"Echo was re-sent without an established clipboard capability");
viewer.pc.close().await.unwrap();
host.pc.close().await.unwrap();
viewer.router.lock().await.stop().await.unwrap();
}).await.expect("clipboard capability regression exceeded deadline");
}
#[tokio::test]
async fn pending_portal_answers_echo_immediately_and_defers_capture_settings() {
tokio::time::timeout(Duration::from_secs(30), async {
let (_, locked) = tokio::sync::watch::channel(Some(false));
let portal = Arc::new(crate::desktop_session::PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())), locked));
let (host, mut viewer) = connect(true, Some(portal.clone())).await;
// Echo must be answered while the placeholder screen is still pending, and it
// must not claim a clipboard bridge that is not established yet.
viewer.channels["CONTROL_DATA_CHANNEL"].send(&echo_request(7).into()).await.unwrap();
let echo = receive(&mut viewer.messages, 3, Duration::from_secs(3)).await.expect("early Echo was not answered while Portal was pending");
assert!(!echo_advertises_clipboard(&echo));
let args = body(&echo, 3).and_then(|action| decode_wire(action).ok())
.and_then(|action| action.into_iter().find_map(|field| match (field.number, field.value) {
(2, WireValue::Bytes(bytes)) => Some(bytes), _ => None,
}));
assert_eq!(args, Some(br#"{"seq":7}"#.as_slice()));
// A capture setting arriving during the same pending window is queued, not dropped.
let mut setting = Vec::new();
field_varint(2, 2, &mut setting); // quality = General (高清 2M)
field_varint(18, 30, &mut setting); // fpsCount = 30
field_varint(5, 1920, &mut setting);
field_varint(6, 1080, &mut setting);
viewer.channels["CONTROL_DATA_CHANNEL"].send(&capture_rpc(77, 2, &setting).into()).await.unwrap();
assert!(receive(&mut viewer.messages, 22, Duration::from_millis(700)).await.is_none(),
"capture setting answered before the desktop was ready");
// Readiness publishes the real geometry; the queued request is applied and answered.
let mut screen = ready_screen();
let native = Arc::get_mut(&mut screen).unwrap();
native.width = 3840;
native.height = 2160;
portal.publish_test_screen(screen);
let response = receive(&mut viewer.messages, 22, Duration::from_secs(5)).await.expect("deferred capture setting was never answered");
let header = body(&response, 22).and_then(|r| body(r, 1)).expect("RpcResponse header");
assert_eq!(varint_of(header, 1), Some(77), "deferred reply lost the request id");
let inventory = receive(&mut viewer.messages, 7, Duration::from_secs(5)).await.expect("updated ScreenSources");
let display = body(&inventory, 7).and_then(|sources| body(sources, 1)).expect("display");
// v4 advertises pixels even when the recovered desktop's native size differs.
for (field, size) in [(4, (1920, 1080)), (6, (3840, 2160))] {
let rect = body(display, field).expect("display rectangle");
assert_eq!((varint_of(rect, 3), varint_of(rect, 4)), (Some(size.0), Some(size.1)));
assert_eq!((varint_of(rect, 5), varint_of(rect, 6)), (Some(size.0), Some(size.1)));
}
assert!(decode_wire(display).unwrap().iter().any(|field|
field.number == 8 && field.value == WireValue::Fixed64(1.0f64.to_le_bytes())));
let sync = receive(&mut viewer.messages, 25, Duration::from_secs(5)).await.expect("CaptureSettingSync");
let sync = body(&sync, 25).expect("CaptureSettingSync payload");
assert_eq!(varint_of(sync, 1), Some(30));
assert_eq!((varint_of(sync, 5), varint_of(sync, 6)), (Some(1920), Some(1080)));
// CONTROL still serves Echo after readiness, and no timer-driven flood appears.
viewer.channels["CONTROL_DATA_CHANNEL"].send(&echo_request(9).into()).await.unwrap();
assert!(receive(&mut viewer.messages, 3, Duration::from_secs(3)).await.is_some());
assert!(receive(&mut viewer.messages, 3, Duration::from_millis(700)).await.is_none(),
"Echo was re-sent without a capability change");
viewer.pc.close().await.unwrap();
host.pc.close().await.unwrap();
viewer.router.lock().await.stop().await.unwrap();
}).await.expect("pending-Portal lifecycle regression exceeded deadline");
}
#[tokio::test]
async fn tool_peer_applies_capture_settings_without_a_desktop() {
tokio::time::timeout(Duration::from_secs(20), async {
let (host, mut viewer) = connect(false, None).await;
let mut setting = Vec::new();
field_varint(1, 1280, &mut setting);
field_varint(2, 720, &mut setting);
viewer.channels["CONTROL_DATA_CHANNEL"].send(&capture_rpc(5, 6, &setting).into()).await.unwrap();
let response = receive(&mut viewer.messages, 22, Duration::from_secs(3)).await.expect("tool capture setting was not answered");
let header = body(&response, 22).and_then(|r| body(r, 1)).expect("RpcResponse header");
assert_eq!(varint_of(header, 1), Some(5));
let sync = receive(&mut viewer.messages, 25, Duration::from_secs(3)).await.expect("CaptureSettingSync");
let sync = body(&sync, 25).expect("CaptureSettingSync payload");
assert_eq!((varint_of(sync, 5), varint_of(sync, 6)), (Some(1280), Some(720)));
viewer.pc.close().await.unwrap();
host.pc.close().await.unwrap();
viewer.router.lock().await.stop().await.unwrap();
}).await.expect("tool capture-setting regression exceeded deadline");
}
+244
View File
@@ -0,0 +1,244 @@
#define _GNU_SOURCE
#include <assert.h>
#include <gio/gio.h>
#include <string.h>
#include "../c_engine/desktop_power.c"
static const char *introspection_xml =
"<node>"
" <interface name='org.freedesktop.DBus.Properties'>"
" <method name='Set'>"
" <arg type='s' direction='in'/><arg type='s' direction='in'/>"
" <arg type='v' direction='in'/>"
" </method>"
" </interface>"
" <interface name='org.gnome.SessionManager'>"
" <method name='Inhibit'>"
" <arg type='s' direction='in'/><arg type='u' direction='in'/>"
" <arg type='s' direction='in'/><arg type='u' direction='in'/>"
" <arg type='u' direction='out'/>"
" </method>"
" <method name='Uninhibit'>"
" <arg type='u' direction='in'/>"
" </method>"
" </interface>"
"</node>";
static GMutex state_lock;
static GCond ready_cond;
static gboolean service_ready;
static gboolean fail_uninhibit;
static unsigned wake_calls, inhibit_calls, uninhibit_calls;
static guint32 last_flags, last_cookie;
static char *wake_sender, *inhibit_sender, *uninhibit_sender;
static GDBusConnection *service_connection;
static GMainLoop *service_loop;
static void replace_string(char **target, const char *value) {
g_free(*target);
*target = g_strdup(value);
}
static void method_call(GDBusConnection *connection, const gchar *sender, const gchar *path,
const gchar *interface, const gchar *method, GVariant *parameters,
GDBusMethodInvocation *invocation, gpointer data) {
(void)connection; (void)path; (void)data;
g_mutex_lock(&state_lock);
if (!strcmp(interface, "org.freedesktop.DBus.Properties") && !strcmp(method, "Set")) {
const char *target_interface, *property;
GVariant *boxed, *value;
g_variant_get(parameters, "(&s&s@v)", &target_interface, &property, &boxed);
value = g_variant_get_variant(boxed);
assert(!strcmp(target_interface, "org.gnome.Mutter.DisplayConfig"));
assert(!strcmp(property, "PowerSaveMode"));
assert(g_variant_is_of_type(value, G_VARIANT_TYPE_INT32));
assert(g_variant_get_int32(value) == 0);
wake_calls++;
replace_string(&wake_sender, sender);
g_variant_unref(value);
g_variant_unref(boxed);
g_mutex_unlock(&state_lock);
g_dbus_method_invocation_return_value(invocation, g_variant_new("()"));
return;
}
if (!strcmp(interface, "org.gnome.SessionManager") && !strcmp(method, "Inhibit")) {
const char *app, *reason;
guint32 xid, flags;
g_variant_get(parameters, "(&su&su)", &app, &xid, &reason, &flags);
assert(!strcmp(app, "uu-remote-linux"));
assert(xid == 0);
assert(!strcmp(reason, "Remote desktop session is active"));
inhibit_calls++;
last_flags = flags;
replace_string(&inhibit_sender, sender);
g_mutex_unlock(&state_lock);
g_dbus_method_invocation_return_value(invocation, g_variant_new("(u)", 73u));
return;
}
if (!strcmp(interface, "org.gnome.SessionManager") && !strcmp(method, "Uninhibit")) {
guint32 cookie;
g_variant_get(parameters, "(u)", &cookie);
uninhibit_calls++;
last_cookie = cookie;
replace_string(&uninhibit_sender, sender);
gboolean fail = fail_uninhibit;
g_mutex_unlock(&state_lock);
if (fail) g_dbus_method_invocation_return_dbus_error(invocation,
"org.freedesktop.DBus.Error.Failed", "synthetic failure");
else g_dbus_method_invocation_return_value(invocation, g_variant_new("()"));
return;
}
g_mutex_unlock(&state_lock);
g_dbus_method_invocation_return_dbus_error(invocation,
"org.freedesktop.DBus.Error.UnknownMethod", "unexpected method");
}
static const GDBusInterfaceVTable vtable = { .method_call = method_call };
static GDBusConnection *connect_bus(const char *address) {
GError *error = NULL;
GDBusConnection *connection = g_dbus_connection_new_for_address_sync(address,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
NULL, NULL, &error);
assert(connection && !error);
g_dbus_connection_set_exit_on_close(connection, FALSE);
return connection;
}
static void request_name(GDBusConnection *connection, const char *name) {
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(connection, "org.freedesktop.DBus",
"/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName",
g_variant_new("(su)", name, 4u), G_VARIANT_TYPE("(u)"),
G_DBUS_CALL_FLAGS_NONE, 3000, NULL, &error);
assert(reply && !error);
guint32 result;
g_variant_get(reply, "(u)", &result);
assert(result == 1);
g_variant_unref(reply);
}
static gpointer run_service(gpointer data) {
const char *address = data;
GMainContext *context = g_main_context_new();
g_main_context_push_thread_default(context);
service_connection = connect_bus(address);
GError *error = NULL;
GDBusNodeInfo *info = g_dbus_node_info_new_for_xml(introspection_xml, &error);
assert(info && !error);
assert(g_dbus_connection_register_object(service_connection, MUTTER_PATH,
info->interfaces[0], &vtable, NULL, NULL, &error));
assert(!error);
assert(g_dbus_connection_register_object(service_connection, SESSION_PATH,
info->interfaces[1], &vtable, NULL, NULL, &error));
assert(!error);
request_name(service_connection, MUTTER_DEST);
request_name(service_connection, SESSION_DEST);
service_loop = g_main_loop_new(context, FALSE);
g_mutex_lock(&state_lock);
service_ready = TRUE;
g_cond_broadcast(&ready_cond);
g_mutex_unlock(&state_lock);
g_main_loop_run(service_loop);
g_dbus_node_info_unref(info);
g_main_loop_unref(service_loop);
g_dbus_connection_close_sync(service_connection, NULL, NULL);
g_object_unref(service_connection);
g_main_context_pop_thread_default(context);
g_main_context_unref(context);
return NULL;
}
static gboolean has_owner(const char *name) {
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(service_connection,
"org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
"GetNameOwner", g_variant_new("(s)", name), G_VARIANT_TYPE("(s)"),
G_DBUS_CALL_FLAGS_NONE, 500, NULL, &error);
if (reply) g_variant_unref(reply);
g_clear_error(&error);
return reply != NULL;
}
static gboolean wait_until_gone(const char *name) {
gint64 deadline = g_get_monotonic_time() + 3 * G_TIME_SPAN_SECOND;
do {
if (!has_owner(name)) return TRUE;
g_usleep(10000);
} while (g_get_monotonic_time() < deadline);
return FALSE;
}
static char *copy_sender(unsigned which) {
g_mutex_lock(&state_lock);
char *sender = which == 0 ? wake_sender : which == 1 ? inhibit_sender : uninhibit_sender;
char *copy = g_strdup(sender);
g_mutex_unlock(&state_lock);
return copy;
}
int main(void) {
g_mutex_init(&state_lock);
g_cond_init(&ready_cond);
GTestDBus *bus = g_test_dbus_new(G_TEST_DBUS_NONE);
g_test_dbus_up(bus);
GThread *thread = g_thread_new("fake-session-bus", run_service,
(gpointer)g_test_dbus_get_bus_address(bus));
g_mutex_lock(&state_lock);
while (!service_ready) g_cond_wait(&ready_cond, &state_lock);
g_mutex_unlock(&state_lock);
assert(uu_desktop_power_wake() == 0);
char *wake_owner = copy_sender(0);
assert(wake_owner && wait_until_gone(wake_owner));
g_free(wake_owner);
int error = 0;
struct uu_idle_inhibitor *first = uu_desktop_idle_inhibitor_acquire(&error);
assert(first && error == 0);
char *first_owner = copy_sender(1);
assert(first_owner && has_owner(first_owner));
struct uu_idle_inhibitor *second = uu_desktop_idle_inhibitor_acquire(&error);
assert(second && error == 0);
char *second_owner = copy_sender(1);
assert(second_owner && strcmp(first_owner, second_owner));
assert(has_owner(first_owner) && has_owner(second_owner));
uu_desktop_idle_inhibitor_release(first);
g_mutex_lock(&state_lock);
assert(inhibit_calls == 2 && last_flags == 8);
assert(uninhibit_calls == 1 && last_cookie == 73);
g_mutex_unlock(&state_lock);
assert(wait_until_gone(first_owner) && has_owner(second_owner));
g_mutex_lock(&state_lock);
fail_uninhibit = TRUE;
g_mutex_unlock(&state_lock);
uu_desktop_idle_inhibitor_release(second);
g_mutex_lock(&state_lock);
assert(uninhibit_calls == 2);
assert(uninhibit_sender && strcmp(uninhibit_sender, second_owner) == 0);
g_mutex_unlock(&state_lock);
assert(wait_until_gone(second_owner));
g_free(first_owner);
g_free(second_owner);
g_mutex_lock(&state_lock);
g_main_loop_quit(service_loop);
g_mutex_unlock(&state_lock);
g_thread_join(thread);
g_test_dbus_down(bus);
g_object_unref(bus);
g_free(wake_sender);
g_free(inhibit_sender);
g_free(uninhibit_sender);
g_cond_clear(&ready_cond);
g_mutex_clear(&state_lock);
return 0;
}
+486
View File
@@ -0,0 +1,486 @@
#define _GNU_SOURCE
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include <assert.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <unistd.h>
struct harness;
static struct harness *active_harness;
static void *pump_server_during_open(void *data);
static GVariant *test_call_with_unix_fd_list_sync(GDBusConnection *connection,
const gchar *bus_name, const gchar *object_path, const gchar *interface_name,
const gchar *method_name, GVariant *parameters, const GVariantType *reply_type,
GDBusCallFlags flags, gint timeout_msec, GUnixFDList *fd_list,
GUnixFDList **out_fd_list, GCancellable *cancellable, GError **error);
#define g_dbus_connection_call_with_unix_fd_list_sync test_call_with_unix_fd_list_sync
#include "../c_engine/portal_engine.c"
#undef g_dbus_connection_call_with_unix_fd_list_sync
#include <libeis.h>
#define CAP_MASK (EI_DEVICE_CAP_POINTER | EI_DEVICE_CAP_POINTER_ABSOLUTE | \
EI_DEVICE_CAP_BUTTON | EI_DEVICE_CAP_SCROLL | EI_DEVICE_CAP_KEYBOARD)
#define EVENT_CAP_MASK (EIS_DEVICE_CAP_POINTER | EIS_DEVICE_CAP_POINTER_ABSOLUTE | \
EIS_DEVICE_CAP_BUTTON | EIS_DEVICE_CAP_SCROLL | EIS_DEVICE_CAP_KEYBOARD)
static const unsigned capabilities[] = {
EIS_DEVICE_CAP_POINTER,
EIS_DEVICE_CAP_POINTER_ABSOLUTE,
EIS_DEVICE_CAP_BUTTON,
EIS_DEVICE_CAP_SCROLL,
EIS_DEVICE_CAP_KEYBOARD,
};
struct received {
enum eis_event_type type;
double x, y;
uint32_t code;
int pressed, dx, dy;
};
struct geometry {
uint32_t width, height, logical_width, logical_height, x, y;
};
struct harness {
const struct geometry *geometry;
struct uu_portal *portal;
struct eis *server;
struct eis_client *client;
struct eis_seat *seat;
struct eis_device *device;
struct received events[64];
size_t n_events;
unsigned frames, starts, stops, diagnostics;
unsigned connect_calls;
atomic_int stop_server_pump;
pthread_t server_pump_thread;
};
static GVariant *test_call_with_unix_fd_list_sync(GDBusConnection *connection,
const gchar *bus_name, const gchar *object_path, const gchar *interface_name,
const gchar *method_name, GVariant *parameters, const GVariantType *reply_type,
GDBusCallFlags flags, gint timeout_msec, GUnixFDList *fd_list,
GUnixFDList **out_fd_list, GCancellable *cancellable, GError **error) {
(void)cancellable;
(void)error;
assert(active_harness);
assert(connection == active_harness->portal->bus);
assert(!strcmp(bus_name, "org.freedesktop.portal.Desktop"));
assert(!strcmp(object_path, "/org/freedesktop/portal/desktop"));
assert(!strcmp(interface_name, "org.freedesktop.portal.RemoteDesktop"));
assert(!strcmp(method_name, "ConnectToEIS"));
assert(g_variant_is_of_type(parameters, G_VARIANT_TYPE("(oa{sv})")));
assert(g_variant_type_equal(reply_type, G_VARIANT_TYPE("(h)")));
assert(flags == G_DBUS_CALL_FLAGS_NONE && timeout_msec == 3000);
assert(!fd_list && out_fd_list);
const char *session;
GVariant *options;
g_variant_get(parameters, "(&o@a{sv})", &session, &options);
assert(!strcmp(session, "/synthetic/session"));
assert(g_variant_n_children(options) == 0);
g_variant_unref(options);
int client_fd = eis_backend_fd_add_client(active_harness->server);
assert(client_fd >= 0);
GUnixFDList *fds = g_unix_fd_list_new();
int index = g_unix_fd_list_append(fds, client_fd, NULL);
close(client_fd);
assert(index == 0);
*out_fd_list = fds;
active_harness->connect_calls++;
g_variant_ref_sink(parameters); g_variant_unref(parameters);
/* Hand off the server only after creating the socket; libeis contexts
* must never be dispatched and mutated by different threads at once. */
assert(pthread_create(&active_harness->server_pump_thread, NULL,
pump_server_during_open, active_harness) == 0);
return g_variant_ref_sink(g_variant_new("(h)", index));
}
static void test_diagnostic(struct ei *ei, enum ei_log_priority priority,
const char *message, struct ei_log_context *context) {
(void)ei;
(void)priority;
(void)message;
(void)context;
if (active_harness) active_harness->diagnostics++;
}
static void add_device(struct harness *h) {
if (h->device) return;
h->device = eis_seat_new_device(h->seat);
assert(h->device);
eis_device_configure_type(h->device, EIS_DEVICE_TYPE_VIRTUAL);
eis_device_configure_name(h->device, "synthetic portal EIS device");
for (size_t i = 0; i < sizeof(capabilities) / sizeof(capabilities[0]); i++)
eis_device_configure_capability(h->device, capabilities[i]);
/* The first region belongs to another stream. Matching by order would
* produce valid EIS events on the wrong screen. */
struct eis_region *region = eis_device_new_region(h->device);
assert(region);
eis_region_set_offset(region, 0, 0);
eis_region_set_size(region, 100, 100);
eis_region_set_mapping_id(region, "another-screen");
eis_region_add(region);
eis_region_unref(region);
region = eis_device_new_region(h->device);
assert(region);
eis_region_set_offset(region, h->geometry->x, h->geometry->y);
eis_region_set_size(region, h->geometry->logical_width, h->geometry->logical_height);
eis_region_set_mapping_id(region, "synthetic-screen");
eis_region_add(region);
eis_region_unref(region);
eis_device_add(h->device);
eis_device_resume(h->device);
}
static void handle_server_events(struct harness *h) {
struct eis_event *event;
while ((event = eis_get_event(h->server))) {
enum eis_event_type type = eis_event_get_type(event);
if (type == EIS_EVENT_CLIENT_CONNECT) {
h->client = eis_client_ref(eis_event_get_client(event));
eis_client_connect(h->client);
h->seat = eis_client_new_seat(h->client, "synthetic-seat");
assert(h->seat);
for (size_t i = 0; i < sizeof(capabilities) / sizeof(capabilities[0]); i++)
eis_seat_configure_capability(h->seat, capabilities[i]);
eis_seat_add(h->seat);
} else if (type == EIS_EVENT_SEAT_BIND) {
unsigned bound = 0;
for (size_t i = 0; i < sizeof(capabilities) / sizeof(capabilities[0]); i++) {
if (eis_event_seat_has_capability(event, capabilities[i])) bound |= capabilities[i];
}
if (!h->device) {
assert(bound == EVENT_CAP_MASK);
add_device(h);
}
} else if (type == EIS_EVENT_FRAME) {
h->frames++;
} else if (type == EIS_EVENT_DEVICE_START_EMULATING) {
h->starts++;
} else if (type == EIS_EVENT_DEVICE_STOP_EMULATING) {
h->stops++;
} else if (type == EIS_EVENT_POINTER_MOTION || type == EIS_EVENT_POINTER_MOTION_ABSOLUTE ||
type == EIS_EVENT_BUTTON_BUTTON || type == EIS_EVENT_SCROLL_DISCRETE ||
type == EIS_EVENT_KEYBOARD_KEY) {
assert(h->n_events < sizeof(h->events) / sizeof(h->events[0]));
struct received *got = &h->events[h->n_events++];
got->type = type;
switch (type) {
case EIS_EVENT_POINTER_MOTION:
got->x = eis_event_pointer_get_dx(event);
got->y = eis_event_pointer_get_dy(event);
break;
case EIS_EVENT_POINTER_MOTION_ABSOLUTE:
got->x = eis_event_pointer_get_absolute_x(event);
got->y = eis_event_pointer_get_absolute_y(event);
break;
case EIS_EVENT_BUTTON_BUTTON:
got->code = eis_event_button_get_button(event);
got->pressed = eis_event_button_get_is_press(event);
break;
case EIS_EVENT_SCROLL_DISCRETE:
got->dx = eis_event_scroll_get_discrete_dx(event);
got->dy = eis_event_scroll_get_discrete_dy(event);
break;
case EIS_EVENT_KEYBOARD_KEY:
got->code = eis_event_keyboard_get_key(event);
got->pressed = eis_event_keyboard_get_key_is_press(event);
break;
default:
break;
}
}
eis_event_unref(event);
}
}
static void pump_server(struct harness *h) {
eis_dispatch(h->server);
handle_server_events(h);
}
static void pump(struct harness *h) {
pump_server(h);
uu_portal_input_dispatch(h->portal);
pump_server(h);
}
static void *pump_server_during_open(void *data) {
struct harness *h = data;
while (!atomic_load(&h->stop_server_pump)) {
pump_server(h);
g_usleep(1000);
}
return NULL;
}
static int ready(struct harness *h) {
return h->portal->ei_devices && h->device && h->starts > 0;
}
static int paused(struct harness *h) {
return !h->portal->ei_devices;
}
static int disconnected(struct harness *h) {
return atomic_load(&h->portal->ei_failed);
}
static void wait_for(struct harness *h, int (*condition)(struct harness *)) {
gint64 deadline = g_get_monotonic_time() + 3000000;
while (!condition(h) && g_get_monotonic_time() < deadline) {
pump(h);
g_usleep(1000);
}
assert(condition(h));
}
static void clear_events(struct harness *h) {
h->n_events = 0;
h->frames = 0;
}
static void wait_for_input(struct harness *h) {
gint64 deadline = g_get_monotonic_time() + 1000000;
while (!h->n_events && g_get_monotonic_time() < deadline) {
pump(h);
g_usleep(1000);
}
assert(h->n_events);
assert(h->frames > 0);
}
static void pause_resume(struct harness *h) {
/* libeis 1.5 pauses only a resumed device; pause while emulating is a
* no-op. End the client transaction before exercising pause/resume. */
assert(h->portal->ei_devices);
ei_device_stop_emulating(h->portal->ei_devices->data);
gint64 deadline = g_get_monotonic_time() + 3000000;
while (!h->stops && g_get_monotonic_time() < deadline) {
pump(h);
g_usleep(1000);
}
assert(h->stops == 1);
eis_device_pause(h->device);
deadline = g_get_monotonic_time() + 3000000;
while (h->portal->ei_devices && g_get_monotonic_time() < deadline) {
pump(h);
g_usleep(1000);
}
assert(!h->portal->ei_devices);
eis_device_resume(h->device);
deadline = g_get_monotonic_time() + 3000000;
while ((!h->portal->ei_devices || h->starts < 2) && g_get_monotonic_time() < deadline) {
pump(h);
g_usleep(1000);
}
assert(h->portal->ei_devices && h->starts == 2 && !h->diagnostics);
}
static void run_geometry(const struct geometry *geometry) {
struct harness h = {.geometry = geometry};
h.portal = calloc(1, sizeof(*h.portal));
assert(h.portal);
pthread_mutex_init(&h.portal->lock, NULL);
pthread_cond_init(&h.portal->changed, NULL);
h.portal->width = geometry->width;
h.portal->height = geometry->height;
h.portal->logical_width = geometry->logical_width;
h.portal->logical_height = geometry->logical_height;
h.portal->mapping_id = g_strdup("synthetic-screen");
h.portal->session = strdup("/synthetic/session");
h.portal->absolute_stream_pixels = 1;
h.server = eis_new(NULL);
assert(h.server && eis_setup_backend_fd(h.server) == 0);
active_harness = &h;
int scaled = geometry->width != geometry->logical_width || geometry->height != geometry->logical_height;
int open_result = scaled ? prepare_input(h.portal) : portal_eis_open(h.portal);
atomic_store(&h.stop_server_pump, 1);
assert(pthread_join(h.server_pump_thread, NULL) == 0);
assert(open_result == 0 && h.connect_calls == 1);
ei_log_set_priority(h.portal->ei, EI_LOG_PRIORITY_ERROR);
ei_log_set_handler(h.portal->ei, test_diagnostic);
wait_for(&h, ready);
assert(h.portal->ei_devices);
struct ei_device *device = h.portal->ei_devices->data;
assert(((ei_device_has_capability(device, EI_DEVICE_CAP_POINTER) ? EI_DEVICE_CAP_POINTER : 0) |
(ei_device_has_capability(device, EI_DEVICE_CAP_POINTER_ABSOLUTE) ? EI_DEVICE_CAP_POINTER_ABSOLUTE : 0) |
(ei_device_has_capability(device, EI_DEVICE_CAP_BUTTON) ? EI_DEVICE_CAP_BUTTON : 0) |
(ei_device_has_capability(device, EI_DEVICE_CAP_SCROLL) ? EI_DEVICE_CAP_SCROLL : 0) |
(ei_device_has_capability(device, EI_DEVICE_CAP_KEYBOARD) ? EI_DEVICE_CAP_KEYBOARD : 0)) == CAP_MASK);
struct ei_region *region = ei_device_get_region(device, 1);
assert(region && !strcmp(ei_region_get_mapping_id(region), "synthetic-screen"));
assert(ei_region_get_x(region) == geometry->x && ei_region_get_y(region) == geometry->y);
assert(ei_region_get_width(region) == geometry->logical_width && ei_region_get_height(region) == geometry->logical_height);
clear_events(&h);
assert(uu_portal_move(h.portal, 0, 0, 0) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
assert(h.events[0].x == geometry->x && h.events[0].y == geometry->y);
clear_events(&h);
assert(uu_portal_move(h.portal, geometry->width / 2.0, geometry->height / 2.0, 0) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
assert(h.events[0].x == geometry->x + geometry->logical_width / 2.0);
assert(h.events[0].y == geometry->y + geometry->logical_height / 2.0);
clear_events(&h);
assert(uu_portal_move(h.portal, geometry->width - 1, geometry->height - 1, 0) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
double right = geometry->x + geometry->logical_width -
(double)geometry->logical_width / geometry->width;
double bottom = geometry->y + geometry->logical_height -
(double)geometry->logical_height / geometry->height;
assert(fabs(h.events[0].x - right) < 0.001 && fabs(h.events[0].y - bottom) < 0.001);
clear_events(&h);
assert(uu_portal_move(h.portal, geometry->width - 1, 0, 0) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
assert(fabs(h.events[0].x - right) < 0.001 && h.events[0].y == geometry->y);
clear_events(&h);
assert(uu_portal_move(h.portal, 0, geometry->height - 1, 0) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
assert(h.events[0].x == geometry->x && fabs(h.events[0].y - bottom) < 0.001);
assert(uu_portal_move(h.portal, geometry->width, geometry->height, 0) == 11);
assert(uu_portal_move(h.portal, -1, -1, 0) == 11);
clear_events(&h);
assert(uu_portal_move(h.portal, geometry->width / 8.0, geometry->height / 8.0, 1) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION);
assert(h.events[0].x == geometry->logical_width / 8.0);
assert(h.events[0].y == geometry->logical_height / 8.0);
clear_events(&h);
assert(uu_portal_key(h.portal, 'a', 30, 1) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_KEYBOARD_KEY);
assert(h.events[0].code == 30 && h.events[0].pressed);
clear_events(&h);
assert(uu_portal_button(h.portal, 1, 1) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_BUTTON_BUTTON);
assert(h.events[0].code == 0x110 && h.events[0].pressed);
clear_events(&h);
assert(uu_portal_button(h.portal, 4, 1) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_SCROLL_DISCRETE);
assert(h.events[0].dx == 0 && h.events[0].dy == -120);
uint8_t storage[256];
struct spa_pod_builder builder = SPA_POD_BUILDER_INIT(storage, sizeof(storage));
const struct spa_pod *format = spa_format_video_raw_build(&builder, SPA_PARAM_Format,
&SPA_VIDEO_INFO_RAW_INIT(.format=SPA_VIDEO_FORMAT_BGRA,
.size=SPA_RECTANGLE(geometry->width, geometry->height), .framerate=SPA_FRACTION(60,1)));
assert(format);
stream_format(h.portal, SPA_PARAM_Format, format);
stream_state(h.portal, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_STREAMING, NULL);
stream_state(h.portal, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
stream_format(h.portal, SPA_PARAM_Format, NULL);
/* Expire only video recovery; keep EIS and the harness wall clock live. */
pthread_mutex_lock(&h.portal->lock);
h.portal->interrupted_at = g_get_monotonic_time() - PORTAL_RENEGOTIATION_TIMEOUT - 1;
pthread_mutex_unlock(&h.portal->lock);
assert(!uu_portal_capture_alive(h.portal));
uint8_t pixel = 99;
assert(uu_portal_capture_since(h.portal, &pixel, sizeof(pixel), NULL) == 9 && pixel == 99);
clear_events(&h);
assert(uu_portal_key(h.portal, 'a', 30, 0) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_KEYBOARD_KEY);
assert(h.events[0].code == 30 && !h.events[0].pressed);
clear_events(&h);
assert(uu_portal_button(h.portal, 1, 0) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_BUTTON_BUTTON);
assert(h.events[0].code == 0x110 && !h.events[0].pressed);
stream_format(h.portal, SPA_PARAM_Format, format);
stream_state(h.portal, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_STREAMING, NULL);
assert(uu_portal_capture_alive(h.portal) && !h.portal->interrupted_at);
/* Natural video recovery keeps this EIS device, with neither input held. */
assert(h.portal->ei_devices->data == device && h.connect_calls == 1 && h.starts == 1);
for (int pressed = 1; pressed >= 0; pressed--) {
clear_events(&h);
assert(uu_portal_key(h.portal, 'a', 30, pressed) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_KEYBOARD_KEY);
assert(h.events[0].code == 30 && h.events[0].pressed == pressed);
clear_events(&h);
assert(uu_portal_button(h.portal, 1, pressed) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_BUTTON_BUTTON);
assert(h.events[0].code == 0x110 && h.events[0].pressed == pressed);
}
pause_resume(&h);
clear_events(&h);
assert(uu_portal_move(h.portal, geometry->width - 1, geometry->height - 1, 0) == 0);
wait_for_input(&h);
assert(h.events[0].type == EIS_EVENT_POINTER_MOTION_ABSOLUTE);
assert(fabs(h.events[0].x - right) < 0.001 && fabs(h.events[0].y - bottom) < 0.001);
eis_device_remove(h.device);
wait_for(&h, paused);
clear_events(&h);
assert(uu_portal_move(h.portal, 100, 100, 0) == 9);
assert(uu_portal_key(h.portal, 'a', 30, 1) == 9);
assert(uu_portal_button(h.portal, 1, 1) == 9);
pump(&h);
assert(h.n_events == 0);
eis_client_disconnect(h.client);
wait_for(&h, disconnected);
assert(!uu_portal_alive(h.portal));
clear_events(&h);
assert(uu_portal_move(h.portal, 100, 100, 0) == 9);
assert(uu_portal_key(h.portal, 'a', 30, 1) == 9);
assert(uu_portal_button(h.portal, 1, 1) == 9);
pump(&h);
assert(h.n_events == 0);
assert(!h.diagnostics);
uu_portal_close(h.portal);
active_harness = NULL;
if (h.device) eis_device_unref(h.device);
if (h.seat) eis_seat_unref(h.seat);
if (h.client) eis_client_unref(h.client);
eis_unref(h.server);
}
int main(void) {
const struct geometry cases[] = {
{1920, 1080, 1920, 1080, 0, 0}, /* 100% */
{2560, 1440, 2048, 1152, 320, 90}, /* 125% */
{2560, 1440, 1707, 960, 0, 0}, /* 150%, rounded logical size */
{3840, 2160, 2560, 1440, 100, 200}, /* 150% */
{3840, 2160, 2194, 1234, 0, 0}, /* 175%, rounded logical size */
{3840, 2160, 1920, 1080, 320, 90}, /* 200% */
{3840, 2160, 2304, 1296, 320, 90}, /* replacement at 166.67% */
{3840, 2160, 1920, 1080, 320, 90}, /* replacement back at 200% */
{5120, 2880, 2560, 1440, 0, 0}, /* 5K */
{7680, 4320, 3840, 2160, 7680, 0}, /* 8K, nonzero origin */
{3440, 1440, 2293, 960, 0, 1080}, /* ultrawide */
{2160, 3840, 1080, 1920, 1920, 0}, /* portrait */
{2257, 1505, 1806, 1204, 0, 0}, /* odd frame dimensions */
};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) run_geometry(&cases[i]);
printf("Portal EIS integration checks passed: %zu geometries\n", sizeof(cases) / sizeof(cases[0]));
return 0;
}
+418 -2
View File
@@ -1,7 +1,362 @@
#define _GNU_SOURCE
/* Protocol-independent C bounds/lifecycle checks; never requests consent. */
#include <gio/gio.h>
#include <assert.h>
#include "portal_engine.c"
static gint64 test_monotonic_time = 1000000;
static gint64 portal_test_monotonic_time(void) { return test_monotonic_time; }
#define g_get_monotonic_time portal_test_monotonic_time
#include "../c_engine/portal_engine.c"
#undef g_get_monotonic_time
static struct uu_portal *test_portal(void) {
struct uu_portal *p = calloc(1, sizeof(*p));
assert(p);
pthread_mutex_init(&p->lock, NULL);
pthread_cond_init(&p->changed, NULL);
return p;
}
static void test_format(struct uu_portal *p, uint32_t width, uint32_t height) {
uint8_t storage[256];
struct spa_pod_builder builder = SPA_POD_BUILDER_INIT(storage, sizeof(storage));
const struct spa_pod *format = spa_format_video_raw_build(&builder, SPA_PARAM_Format,
&SPA_VIDEO_INFO_RAW_INIT(.format=SPA_VIDEO_FORMAT_BGRA, .size=SPA_RECTANGLE(width,height),
.framerate=SPA_FRACTION(60,1)));
assert(format);
stream_format(p, SPA_PARAM_Format, format);
}
static void test_frame(struct uu_portal *p) {
uint8_t pixels[16] = {1,2,3,255, 4,5,6,255, 7,8,9,255, 10,11,12,255};
struct spa_chunk chunk = {.offset=0, .size=sizeof(pixels), .stride=8};
struct spa_data data = {.type=SPA_DATA_MemPtr, .data=pixels, .maxsize=sizeof(pixels), .chunk=&chunk};
struct spa_buffer buffer = {.n_datas=1, .datas=&data};
copy_video_buffer(p, &buffer);
}
static void assert_capture_ok(struct uu_portal *p, uint8_t *pixels) {
uint64_t revision = p->revision + 1;
assert(uu_portal_capture_since(p, pixels, p->frame_length, &revision) == 0);
}
static void portal_renegotiation_timeout(void) {
uint8_t pixels[16];
struct uu_portal *p = test_portal();
test_monotonic_time = 1000000;
test_format(p, 2, 2);
assert(p->format_valid);
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
test_frame(p);
assert(p->sequence == 1 && uu_portal_capture_alive(p));
assert_capture_ok(p, pixels);
gint64 paused_at = test_monotonic_time;
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
assert(!p->interrupted_at);
stream_format(p, SPA_PARAM_Format, NULL);
assert(!p->format_valid && p->interrupted_at == paused_at && uu_portal_capture_alive(p));
uint64_t sequence = p->sequence;
test_frame(p);
assert(p->sequence == sequence); /* Buffers without a negotiated format are ignored. */
test_monotonic_time = paused_at + 3 * G_TIME_SPAN_SECOND;
stream_state(p, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_PAUSED, NULL);
stream_format(p, SPA_PARAM_Format, NULL);
test_monotonic_time = paused_at + 4 * G_TIME_SPAN_SECOND;
stream_format(p, SPA_PARAM_Format, NULL);
assert(p->interrupted_at == paused_at); /* Repeated NULL formats do not restart the deadline. */
assert(uu_portal_capture_alive(p));
assert_capture_ok(p, pixels);
test_monotonic_time = paused_at + 5 * G_TIME_SPAN_SECOND - 1;
assert(uu_portal_capture_alive(p));
assert_capture_ok(p, pixels);
test_monotonic_time = paused_at + 5 * G_TIME_SPAN_SECOND;
assert(!uu_portal_capture_alive(p) && uu_portal_alive(p));
uint64_t revision = 0;
memset(pixels, 99, sizeof(pixels));
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 9);
assert(!revision && pixels[0] == 99 && pixels[15] == 99);
/* Late renegotiation can recover naturally before the worker replaces it. */
test_format(p, 2, 2);
stream_state(p, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_STREAMING, NULL);
test_frame(p);
assert(uu_portal_capture_alive(p) && p->revision == 2);
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 0);
assert(revision == 2 && pixels[0] == 1);
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 12);
uu_portal_close(p);
}
static void portal_renegotiation_recovery(void) {
uint8_t pixels[16];
struct uu_portal *p = test_portal();
test_monotonic_time = 2000000;
test_format(p, 2, 2);
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
test_frame(p);
gint64 paused_at = test_monotonic_time;
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
stream_format(p, SPA_PARAM_Format, NULL);
test_monotonic_time = paused_at + 2 * G_TIME_SPAN_SECOND;
test_format(p, 2, 2);
assert(p->format_valid && !p->interrupted_at);
stream_state(p, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_STREAMING, NULL);
assert(p->interrupted_at == 0 && uu_portal_capture_alive(p));
test_monotonic_time = paused_at + 6 * G_TIME_SPAN_SECOND;
assert(uu_portal_capture_alive(p));
assert_capture_ok(p, pixels);
uu_portal_close(p);
}
static void portal_renegotiation_event_order_and_healthy_states(void) {
uint8_t pixels[16];
struct uu_portal *p = test_portal();
test_monotonic_time = 3000000;
test_format(p, 2, 2);
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
test_frame(p);
stream_format(p, SPA_PARAM_Format, NULL); /* Format can disappear before the state callback. */
assert(!p->format_valid && !p->interrupted_at && uu_portal_capture_alive(p));
test_monotonic_time += 20 * G_TIME_SPAN_SECOND;
assert(uu_portal_capture_alive(p));
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
gint64 paused_at = test_monotonic_time;
assert(p->interrupted_at == paused_at);
test_monotonic_time = paused_at + 5 * G_TIME_SPAN_SECOND;
assert(!uu_portal_capture_alive(p) && uu_portal_alive(p));
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), NULL) == 9);
uu_portal_close(p);
p = test_portal();
test_monotonic_time = 4000000;
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_PAUSED, NULL);
assert(!p->interrupted_at);
test_monotonic_time += 60 * G_TIME_SPAN_SECOND;
assert(uu_portal_capture_alive(p)); /* A stream that never reached STREAMING has no recovery deadline. */
assert(uu_portal_capture(p, pixels, sizeof(pixels)) == 10);
uu_portal_close(p);
p = test_portal();
test_monotonic_time = 5000000;
test_format(p, 2, 2);
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
test_frame(p);
uint64_t revision = 0;
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 0);
test_monotonic_time += 60 * G_TIME_SPAN_SECOND;
assert(uu_portal_capture_alive(p)); /* A static screen can stream without producing a new frame. */
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 12);
uu_portal_close(p);
p = test_portal();
test_monotonic_time = 6000000;
test_format(p, 2, 2);
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
test_frame(p);
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
test_monotonic_time += 6 * G_TIME_SPAN_SECOND;
assert(p->format_valid && uu_portal_capture_alive(p)); /* PAUSED alone is not a format failure. */
assert_capture_ok(p, pixels);
assert(!p->interrupted_at);
/* Healthy paused time must not consume a later negotiation's allowance. */
stream_format(p, SPA_PARAM_Format, NULL);
paused_at = test_monotonic_time;
assert(p->interrupted_at == paused_at && uu_portal_capture_alive(p));
test_monotonic_time += 4 * G_TIME_SPAN_SECOND;
assert(uu_portal_capture_alive(p));
test_format(p, 2, 2);
assert(!p->interrupted_at && uu_portal_capture_alive(p));
/* A second format removal while still paused gets a new full deadline. */
test_monotonic_time += 20 * G_TIME_SPAN_SECOND;
stream_format(p, SPA_PARAM_Format, NULL);
paused_at = test_monotonic_time;
assert(p->interrupted_at == paused_at && uu_portal_capture_alive(p));
test_monotonic_time += 5 * G_TIME_SPAN_SECOND - 1;
assert(uu_portal_capture_alive(p));
test_monotonic_time++;
assert(!uu_portal_capture_alive(p) && uu_portal_alive(p));
uu_portal_close(p);
}
static void portal_changed_physical_format_still_fails(void) {
struct uu_portal *p = test_portal();
uint8_t pixels[24];
test_monotonic_time = 7000000;
test_format(p, 2, 2);
assert(p->format_valid && !p->error);
test_format(p, 3, 2);
assert(p->error == 8 && !uu_portal_capture_alive(p));
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), NULL) == 8);
uu_portal_close(p);
}
static void display_change(struct uu_portal *p) {
monitors_changed(NULL, NULL, NULL, NULL, NULL, NULL, p);
}
static struct uu_portal *streaming_portal(void) {
struct uu_portal *p = test_portal();
test_format(p, 2, 2);
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
test_frame(p);
return p;
}
static void portal_display_change_recovery(void) {
/* Notification, PAUSED and NULL can arrive in any order. */
const unsigned orders[][3] = {{0,1,2}, {0,2,1}, {1,0,2}, {1,2,0}, {2,0,1}, {2,1,0}};
for (size_t order = 0; order < G_N_ELEMENTS(orders); order++) {
test_monotonic_time = 10000000;
struct uu_portal *p = streaming_portal();
for (unsigned n = 0; n < 3; n++) {
test_monotonic_time += 20 * G_TIME_SPAN_MILLISECOND;
switch (orders[order][n]) {
case 0: display_change(p); break;
case 1: stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL); break;
case 2: stream_format(p, SPA_PARAM_Format, NULL); break;
}
}
gint64 deadline = MAX(p->display_changed_at, p->interrupted_at) + PORTAL_DISPLAY_GRACE;
test_monotonic_time = deadline - 1;
assert(uu_portal_capture_alive(p));
test_monotonic_time++;
assert(!uu_portal_capture_alive(p) && uu_portal_alive(p));
p->clipboard_enabled = 1;
assert(uu_portal_clipboard_ready(p));
uint8_t pixels[16];
assert(uu_portal_capture(p, pixels, sizeof(pixels)) == 9);
uu_portal_close(p);
}
/* A delayed notification still accelerates an existing invalid pause. */
test_monotonic_time = 20000000;
struct uu_portal *p = streaming_portal();
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
stream_format(p, SPA_PARAM_Format, NULL);
test_monotonic_time += G_TIME_SPAN_SECOND;
display_change(p);
gint64 notified_at = p->display_changed_at;
test_monotonic_time += 100 * G_TIME_SPAN_MILLISECOND;
display_change(p);
assert(p->display_changed_at == notified_at); /* Repeated signals cannot postpone recovery. */
test_monotonic_time = notified_at + PORTAL_DISPLAY_GRACE;
assert(!uu_portal_capture_alive(p) && uu_portal_alive(p));
uu_portal_close(p);
}
static void portal_display_change_healthy_and_stale(void) {
test_monotonic_time = 30000000;
struct uu_portal *p = test_portal();
display_change(p);
assert(!p->display_changed_at); /* Ignore notifications during startup. */
uu_portal_close(p);
p = streaming_portal();
display_change(p);
test_monotonic_time += 60 * G_TIME_SPAN_SECOND;
assert(uu_portal_capture_alive(p)); /* No new frames on a static screen is healthy. */
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
assert(uu_portal_capture_alive(p)); /* A valid format must not trigger recovery. */
stream_format(p, SPA_PARAM_Format, NULL);
gint64 invalid_at = test_monotonic_time;
test_monotonic_time += PORTAL_DISPLAY_GRACE;
assert(uu_portal_capture_alive(p)); /* The notification from a minute ago is unrelated. */
test_monotonic_time = invalid_at + PORTAL_RENEGOTIATION_TIMEOUT;
assert(!uu_portal_capture_alive(p));
uu_portal_close(p);
test_monotonic_time = 100000000;
p = streaming_portal();
display_change(p);
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
stream_format(p, SPA_PARAM_Format, NULL);
test_monotonic_time += PORTAL_DISPLAY_GRACE - 1;
test_format(p, 2, 2); /* Natural renegotiation wins before the early check. */
assert(!p->display_changed_at && !p->interrupted_at);
test_monotonic_time += G_TIME_SPAN_SECOND;
assert(uu_portal_capture_alive(p));
stream_format(p, SPA_PARAM_Format, NULL);
test_monotonic_time += PORTAL_DISPLAY_GRACE;
assert(uu_portal_capture_alive(p)); /* Recovered signal cannot accelerate another failure. */
display_change(p); /* A later display change correlates with the retained invalid state. */
test_monotonic_time += PORTAL_DISPLAY_GRACE;
assert(!uu_portal_capture_alive(p) && uu_portal_alive(p));
/* Even after the fast deadline, recovery can still win before replacement. */
test_format(p, 2, 2);
assert(uu_portal_capture_alive(p) && !p->display_changed_at);
display_change(p);
test_format(p, 2, 2); /* Successful format without an invalid pause discards its hint. */
assert(!p->display_changed_at);
display_change(p);
stream_state(p, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_STREAMING, NULL);
assert(!p->display_changed_at);
uu_portal_close(p);
}
static void portal_display_subscription_lifecycle(void) {
GTestDBus *test_bus = g_test_dbus_new(G_TEST_DBUS_NONE);
g_test_dbus_up(test_bus);
GDBusConnection *bus = g_dbus_connection_new_for_address_sync(g_test_dbus_get_bus_address(test_bus),
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
NULL, NULL, NULL);
assert(bus);
const char *sender = g_dbus_connection_get_unique_name(bus);
for (unsigned threaded = 0; threaded < 2; threaded++) {
struct uu_portal *p = streaming_portal();
p->bus = g_object_ref(bus);
p->context = g_main_context_new();
p->main_loop = g_main_loop_new(p->context, FALSE);
g_main_context_push_thread_default(p->context);
p->display_subscription = g_dbus_connection_signal_subscribe(bus, sender, MUTTER_DISPLAY,
"MonitorsChanged", MUTTER_DISPLAY_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE, monitors_changed, p, NULL);
g_main_context_pop_thread_default(p->context);
/* A synchronous roundtrip places the match rule before the signal. */
GVariant *reply = g_dbus_connection_call_sync(bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
"org.freedesktop.DBus", "GetId", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL);
assert(reply); g_variant_unref(reply);
assert(g_dbus_connection_emit_signal(bus, NULL, MUTTER_DISPLAY_PATH, MUTTER_DISPLAY,
"MonitorsChanged", g_variant_new("()"), NULL));
assert(g_dbus_connection_flush_sync(bus, NULL, NULL));
/* The test clock is fixed; use a bounded iteration count for delivery. */
for (unsigned attempt = 0; attempt < 1000 && !p->display_changed_at; attempt++) {
while (g_main_context_iteration(p->context, FALSE)) {}
if (!p->display_changed_at) g_usleep(1000);
}
assert(p->display_changed_at);
if (threaded) {
assert(!pthread_create(&p->dbus_thread, NULL, run_dbus, p));
p->dbus_started = 1;
}
assert(g_dbus_connection_emit_signal(bus, NULL, MUTTER_DISPLAY_PATH, MUTTER_DISPLAY,
"MonitorsChanged", g_variant_new("()"), NULL));
assert(g_dbus_connection_flush_sync(bus, NULL, NULL));
uu_portal_close(p); /* Queued signals and immediate loop shutdown must be safe. */
}
g_dbus_connection_close_sync(bus, NULL, NULL);
g_object_unref(bus);
g_test_dbus_down(test_bus);
g_object_unref(test_bus);
}
static int cancel_immediately(void *data) { return *(const int *)data; }
struct cancel_later { int *cancelled; int polls; };
static int cancel_after_polls(void *data) {
struct cancel_later *state = data;
if (state->polls++ >= 2) *state->cancelled = 1;
return *state->cancelled;
}
static void cancellable_waits(void) {
int cancelled = 1, error = 0;
struct uu_portal p = {.cancelled=cancel_immediately, .cancel_data=&cancelled};
pthread_mutex_init(&p.lock, NULL);
pthread_cond_init(&p.changed, NULL);
assert(wait_for_first_frame(&p) == 13);
pthread_cond_destroy(&p.changed);
pthread_mutex_destroy(&p.lock);
GVariantBuilder options;
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
assert(!request(&p, REMOTE, "Start", &options, 2, &error));
assert(error == 13);
g_variant_builder_clear(&options);
/* A cancel arriving during the consent wait must stop it without the full timeout. */
int waiting_cancelled = 0;
struct cancel_later state = {.cancelled=&waiting_cancelled};
struct response response = {0};
struct uu_portal waiting = {.cancelled=cancel_after_polls, .cancel_data=&state};
assert(wait_for_response(&waiting, &response, 120) == 13);
assert(waiting_cancelled);
}
static void clipboard_bounds(void) {
int fds[2]; uint8_t bytes[8]={0};
assert(pipe2(fds,O_NONBLOCK)==0);
@@ -14,8 +369,69 @@ static void clipboard_bounds(void) {
assert(clipboard_io(fds[1],bytes,1,1)==-1); close(fds[1]);
assert(clipboard_io(-1,bytes,0,1)==0);
}
static void capture_with_optional_geometry(int has_size) {
struct uu_portal *p = calloc(1, sizeof(*p));
assert(p);
pthread_mutex_init(&p->lock, NULL);
pthread_cond_init(&p->changed, NULL);
p->width = p->height = 2;
p->absolute_stream_pixels = 1;
GVariantBuilder properties, streams, result;
g_variant_builder_init(&properties, G_VARIANT_TYPE_VARDICT);
if (has_size) g_variant_builder_add(&properties, "{sv}", "size", g_variant_new("(ii)", 1, 1));
g_variant_builder_init(&streams, G_VARIANT_TYPE("a(ua{sv})"));
g_variant_builder_add(&streams, "(u@a{sv})", 42u, g_variant_builder_end(&properties));
g_variant_builder_init(&result, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&result, "{sv}", "devices", g_variant_new_uint32(3));
g_variant_builder_add(&result, "{sv}", "streams", g_variant_builder_end(&streams));
GVariant *reply = g_variant_ref_sink(g_variant_builder_end(&result));
assert(read_stream_result(p, reply) == 0 && p->node == 42);
g_variant_unref(reply);
p->frame_length = 16;
p->frame = calloc(1, p->frame_length);
assert(p->frame);
p->sequence = 1;
uint8_t pixels[16];
/* Optional compositor metadata must not gate capture/uinput. */
uu_portal_input_dispatch(p);
assert(!p->ei && uu_portal_alive(p));
assert(uu_portal_capture(p, pixels, sizeof(pixels)) == 0);
uint32_t width, height;
uu_portal_logical_size(p, &width, &height);
assert(width == (has_size ? 1 : 2) && height == width);
if (!has_size) assert(uu_portal_move(p, 1, 1, 0) == 11);
assert(!p->ei && uu_portal_alive(p));
uu_portal_close(p);
}
static void authorization_still_required(void) {
const unsigned cases[][2] = {{0,1}, {1,1}, {2,1}, {3,0}, {3,2}};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
GVariantBuilder streams, result;
g_variant_builder_init(&streams, G_VARIANT_TYPE("a(ua{sv})"));
for (unsigned n = 0; n < cases[i][1]; n++)
g_variant_builder_add(&streams, "(u@a{sv})", n + 42, empty_options());
g_variant_builder_init(&result, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&result, "{sv}", "devices", g_variant_new_uint32(cases[i][0]));
g_variant_builder_add(&result, "{sv}", "streams", g_variant_builder_end(&streams));
GVariant *reply = g_variant_ref_sink(g_variant_builder_end(&result));
struct uu_portal p = {0};
assert(read_stream_result(&p, reply) == 6);
g_variant_unref(reply);
}
}
int main(void) {
portal_renegotiation_timeout();
portal_renegotiation_recovery();
portal_renegotiation_event_order_and_healthy_states();
portal_changed_physical_format_still_fails();
portal_display_change_recovery();
portal_display_change_healthy_and_stale();
portal_display_subscription_lifecycle();
cancellable_waits();
clipboard_bounds();
capture_with_optional_geometry(0);
capture_with_optional_geometry(1);
authorization_still_required();
struct uu_portal *p = calloc(1, sizeof(*p));
assert(p);
pthread_mutex_init(&p->lock, NULL);
@@ -88,7 +504,7 @@ int main(void) {
assert(uu_portal_clipboard_poll(p,NULL,CLIP_LIMIT)==-1);
p->error=8;
assert(!uu_portal_alive(p));
assert(uu_portal_key(p, 'a', 1)==9); /* no input into an unusable stream */
assert(uu_portal_key(p, 'a', 30, 1)==9); /* no input into an unusable stream */
p->error=0;
p->clipboard_enabled=1;
assert(uu_portal_clipboard_supported(p));
+141
View File
@@ -0,0 +1,141 @@
#define _GNU_SOURCE
/* Stub only the D-Bus I/O boundary; no real session bus or consent requests. */
#include <assert.h>
#include <gio/gio.h>
#include <sys/socket.h>
static GVariant *test_call(GDBusConnection *, const gchar *, const gchar *, const gchar *,
const gchar *, GVariant *, const GVariantType *, GDBusCallFlags, gint, GCancellable *, GError **);
static GVariant *test_connect(GDBusConnection *, const gchar *, const gchar *, const gchar *,
const gchar *, GVariant *, const GVariantType *, GDBusCallFlags, gint,
GUnixFDList *, GUnixFDList **, GCancellable *, GError **);
#define g_dbus_connection_call_sync test_call
#define g_dbus_connection_call_with_unix_fd_list_sync test_connect
#include "../c_engine/portal_engine.c"
#undef g_dbus_connection_call_sync
#undef g_dbus_connection_call_with_unix_fd_list_sync
enum response_mode { UNKNOWN_METHOD, NOT_SUPPORTED, TEMPORARY_FAILURE, NO_FDS, BAD_INDEX, DISCONNECTED };
static enum response_mode mode;
static unsigned connects, legacy_calls;
static double last_x, last_y;
static GVariant *test_call(GDBusConnection *bus, const gchar *dest, const gchar *path,
const gchar *interface, const gchar *method, GVariant *parameters,
const GVariantType *type, GDBusCallFlags flags, gint timeout,
GCancellable *cancel, GError **error) {
assert(!strcmp(interface, REMOTE));
assert(!strcmp(method, "NotifyKeyboardKeysym") || !strcmp(method, "NotifyPointerButton") ||
!strcmp(method, "NotifyPointerAxisDiscrete") || !strcmp(method, "NotifyPointerMotion") ||
!strcmp(method, "NotifyPointerMotionAbsolute"));
if (!strcmp(method, "NotifyPointerMotion") || !strcmp(method, "NotifyPointerMotionAbsolute")) {
unsigned offset = !strcmp(method, "NotifyPointerMotion") ? 2 : 3;
GVariant *x = g_variant_get_child_value(parameters, offset);
GVariant *y = g_variant_get_child_value(parameters, offset + 1);
last_x = g_variant_get_double(x); last_y = g_variant_get_double(y);
g_variant_unref(x); g_variant_unref(y);
}
legacy_calls++;
g_variant_ref_sink(parameters); g_variant_unref(parameters);
return g_variant_ref_sink(g_variant_new("()"));
}
static GVariant *test_connect(GDBusConnection *bus, const gchar *dest, const gchar *path,
const gchar *interface, const gchar *method, GVariant *parameters,
const GVariantType *type, GDBusCallFlags flags, gint timeout,
GUnixFDList *in_fds, GUnixFDList **out_fds, GCancellable *cancel, GError **error) {
assert(!strcmp(method, "ConnectToEIS") && !strcmp(interface, REMOTE));
assert(g_variant_is_of_type(parameters, G_VARIANT_TYPE("(oa{sv})")));
assert(g_variant_type_equal(type, G_VARIANT_TYPE("(h)")));
connects++;
g_variant_ref_sink(parameters); g_variant_unref(parameters);
if (mode <= TEMPORARY_FAILURE) {
int code = mode == UNKNOWN_METHOD ? G_DBUS_ERROR_UNKNOWN_METHOD :
mode == NOT_SUPPORTED ? G_DBUS_ERROR_NOT_SUPPORTED : G_DBUS_ERROR_TIMED_OUT;
g_set_error_literal(error, G_DBUS_ERROR, code, "synthetic EIS response");
return NULL;
}
if (mode != NO_FDS) *out_fds = g_unix_fd_list_new();
if (mode == DISCONNECTED) {
int sockets[2];
assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sockets) == 0);
close(sockets[1]);
assert(g_unix_fd_list_append(*out_fds, sockets[0], NULL) == 0);
close(sockets[0]);
}
return g_variant_ref_sink(g_variant_new("(h)", 0));
}
static struct uu_portal *session(int scaled, int gnome, int mapping) {
struct uu_portal *p = calloc(1, sizeof(*p));
assert(p);
pthread_mutex_init(&p->lock, NULL); pthread_cond_init(&p->changed, NULL);
p->width = 3840; p->height = 2160;
p->logical_width = scaled ? 1920 : 3840; p->logical_height = scaled ? 1080 : 2160;
p->absolute_stream_pixels = gnome;
p->session = g_strdup("/synthetic/session");
if (mapping) p->mapping_id = g_strdup("synthetic-screen");
connects = legacy_calls = 0;
return p;
}
static void legacy_input(struct uu_portal *p) {
assert(uu_portal_key(p, 'a', 30, 1) == 0);
assert(uu_portal_key(p, 'a', 30, 0) == 0);
assert(uu_portal_button(p, 1, 1) == 0);
assert(uu_portal_button(p, 1, 0) == 0);
assert(uu_portal_button(p, 4, 1) == 0);
assert(uu_portal_move(p, 8, 4, 1) == 0);
assert(last_x == (double)8 * p->logical_width / p->width);
assert(last_y == (double)4 * p->logical_height / p->height);
assert(uu_portal_move(p, 1920, 1080, 0) == 0);
assert(last_x == (p->absolute_stream_pixels ? 1920 : 960));
assert(last_y == (p->absolute_stream_pixels ? 1080 : 540));
assert(legacy_calls == 7 && uu_portal_alive(p) && !p->ei);
}
int main(void) {
struct uu_portal *p = session(1, 1, 0);
legacy_input(p);
assert(!connects && p->ei_unavailable);
uu_portal_close(p);
for (int response = UNKNOWN_METHOD; response <= NOT_SUPPORTED; response++) {
mode = response;
p = session(1, 1, 1);
legacy_input(p);
assert(connects == 1 && p->ei_unavailable);
uu_portal_close(p);
}
p = session(0, 1, 1); /* Unscaled GNOME keeps Notify*. */
legacy_input(p);
assert(!connects);
uu_portal_close(p);
p = session(1, 0, 1); /* KDE keeps its compositor-logical mapping. */
legacy_input(p);
assert(!connects);
uu_portal_close(p);
mode = TEMPORARY_FAILURE;
p = session(1, 1, 1);
assert(uu_portal_key(p, 'a', 30, 1) == 9);
assert(connects == 1 && !legacy_calls && !uu_portal_alive(p) && !p->ei_unavailable);
assert(uu_portal_key(p, 'a', 30, 0) == 9 && connects == 1);
uu_portal_close(p);
mode = UNKNOWN_METHOD; /* A replacement session can select its input backend afresh. */
p = session(1, 1, 1);
legacy_input(p);
assert(connects == 1);
uu_portal_close(p);
for (int response = NO_FDS; response <= DISCONNECTED; response++) {
mode = response;
p = session(1, 1, 1);
assert(uu_portal_move(p, 10, 10, 0) == 9);
assert(!uu_portal_alive(p));
assert(uu_portal_key(p, 'a', 30, 1) == 9);
assert(uu_portal_button(p, 1, 1) == 9);
assert(connects == 1 && !legacy_calls); /* Never mix Notify* after success. */
uu_portal_close(p);
}
puts("Portal input capability/failure checks passed");
return 0;
}
+4 -1
View File
@@ -156,7 +156,10 @@ impl RTCSctpTransport {
association = sctp::association::Association::client(sctp::association::Config {
net_conn: Arc::clone(net_conn) as Arc<dyn Conn + Send + Sync>,
max_receive_buffer_size: 0,
max_message_size: 0,
// uu-remote-linux patch: the association's send limit ignores the remote
// a=max-message-size and defaults to 64 KB; libwebrtc peers (official viewers,
// our answers/offers) advertise 262144. A 5600-entry directory reply is ~73 KB.
max_message_size: 262_144,
name: String::new(),
}) => {
break Arc::new(association?);