mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Compare commits
No files matched your search
@@ -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; 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
|
||||
|
||||
@@ -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:
|
||||
@@ -139,12 +139,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), `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.
|
||||
|
||||
|
||||
Generated
+69
-1
@@ -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,7 +2717,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.16"
|
||||
version = "0.1.27"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.16"
|
||||
version = "0.1.27"
|
||||
edition = "2021"
|
||||
authors = ["uu-remote-linux contributors"]
|
||||
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
||||
@@ -29,7 +29,7 @@ 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"
|
||||
|
||||
+14
-4
@@ -65,9 +65,9 @@ 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; 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`.
|
||||
|
||||
@@ -75,6 +75,10 @@ Wayland limits: single screen, fixed resolution. The native Hyprland backend now
|
||||
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"
|
||||
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. A previously streaming Portal capture that stays paused without a negotiated format for five seconds enters session recovery; a static desktop does not trigger this timeout. 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 detection threshold, not the total recovery time. 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.
|
||||
|
||||
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`).
|
||||
@@ -109,7 +113,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>).
|
||||
@@ -260,6 +264,8 @@ 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.
|
||||
@@ -364,10 +370,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`.
|
||||
|
||||
@@ -59,13 +59,17 @@ 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% 缩放下验证画面和键鼠;锁屏恢复及重连仍待实机验收。除 GNOME 的上述路径和 X11 外,其余桌面目前主要在虚拟机里测试。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
|
||||
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
|
||||
|
||||
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
|
||||
|
||||
GNOME 缩放桌面在使用 Portal 原生键鼠时,若支持 `ConnectToEIS` 和屏幕 `mapping_id`,通过同一授权会话的 EIS 接口注入输入,按匹配的区域与实际帧尺寸分别映射横纵坐标,没有固定分辨率或缩放倍率,可避免 `NotifyPointerMotionAbsolute` 在缩放时拒绝坐标。Portal 构建需要 libei ≥ 1.1。缺少 EIS 支持时保留旧输入路径及其既有缩放限制;其他初始化错误会明确报告并触发会话恢复。EIS 连接成功后全部输入统一使用 EIS。只采集画面或使用 uinput 时不会初始化 EIS;缺少可选的逻辑尺寸也不阻断采集。键盘沿用项目现有的 Windows VK → evdev 映射。恢复时保持编码器的采集帧尺寸不变,允许缩放后的逻辑尺寸变化。已开始推流的 Portal 采集若暂停且无有效协商格式持续五秒,将进入会话恢复;静态桌面不会触发该超时。Ubuntu 26.04、GNOME Wayland 单屏 3840×2160 已实测保持连接切换 200%→166.67%→200%,恢复后画面持续更新且鼠标可控制全屏;切换期间会短暂停顿、黑屏后恢复,五秒是故障判定阈值,并非总恢复时间。此路径仍需实机验证锁屏恢复;多屏、连接中改变分辨率及旋转/镜像变换不属于本次修复范围。 捕获设置协议仍为版本 4:对客户端上报的分辨率统一使用像素坐标、DPR 为 1;Portal 逻辑尺寸仅用于本地输入映射。
|
||||
|
||||
原生 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 帧。
|
||||
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
|
||||
|
||||
@@ -95,7 +99,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>)。
|
||||
@@ -231,6 +235,8 @@ systemctl --user restart uuremote-native.service
|
||||
|
||||
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。
|
||||
|
||||
选择码率档(原画 / 超清 / 高清 / 自定义)时,外部 FFmpeg 的 CPU 编码以平均码率(ABR)为目标,避免 4K 低码率下关键帧发灰;「自动」档仍使用恒定质量(CRF)。
|
||||
|
||||
## 让 AI 帮你安装或开发
|
||||
|
||||
如果你用 Claude Code、Codex、Cursor 等 AI 编程工具,让它先阅读 [AGENTS.md](AGENTS.md)。
|
||||
@@ -319,10 +325,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`。
|
||||
|
||||
@@ -51,10 +51,11 @@ 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");
|
||||
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();
|
||||
|
||||
@@ -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, ®ion) &&
|
||||
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, ®ion);
|
||||
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);
|
||||
}
|
||||
+137
-21
@@ -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,7 @@
|
||||
#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)
|
||||
|
||||
struct uu_portal {
|
||||
GDBusConnection *bus;
|
||||
@@ -44,13 +47,23 @@ struct uu_portal {
|
||||
pthread_cond_t changed;
|
||||
atomic_int closed;
|
||||
int error;
|
||||
enum pw_stream_state state;
|
||||
int format_valid, stream_started;
|
||||
gint64 interrupted_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 +71,7 @@ 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); }
|
||||
#include "portal_eis.inc"
|
||||
static void fail(struct uu_portal *p, int error) {
|
||||
pthread_mutex_lock(&p->lock);
|
||||
if (!p->error) p->error = error;
|
||||
@@ -145,13 +159,33 @@ 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 p->interrupted_at = 0;
|
||||
}
|
||||
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;
|
||||
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 +200,15 @@ 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;
|
||||
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 +218,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 +240,7 @@ static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
|
||||
}
|
||||
}
|
||||
p->sequence++;
|
||||
p->revision++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -268,6 +306,27 @@ static int connect_pipewire(struct uu_portal *p) {
|
||||
pthread_mutex_unlock(&p->lock);
|
||||
return result;
|
||||
}
|
||||
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;
|
||||
}
|
||||
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) {
|
||||
*error = 0; *width = *height = 0;
|
||||
struct uu_portal *p = calloc(1, sizeof(*p));
|
||||
@@ -338,18 +397,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)
|
||||
@@ -358,7 +406,6 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
|
||||
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);
|
||||
g_variant_unref(results);
|
||||
if (*error) goto done;
|
||||
p->main_loop = g_main_loop_new(p->context, FALSE);
|
||||
@@ -369,7 +416,7 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
|
||||
if (*error) { 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;
|
||||
return p;
|
||||
done:
|
||||
@@ -387,6 +434,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);
|
||||
@@ -411,6 +459,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 +467,77 @@ 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) {
|
||||
return p->state == PW_STREAM_STATE_PAUSED && !p->format_valid && p->interrupted_at &&
|
||||
g_get_monotonic_time() - p->interrupted_at >= PORTAL_RENEGOTIATION_TIMEOUT;
|
||||
}
|
||||
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 +554,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 +586,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));
|
||||
}
|
||||
@@ -14,9 +14,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
@@ -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,®istry_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
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
@@ -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.**
|
||||
+38
-2
@@ -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)");
|
||||
}
|
||||
}
|
||||
+19
-5
@@ -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>,
|
||||
@@ -59,18 +72,18 @@ impl ScreenCapture {
|
||||
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]) });
|
||||
buffer:Mutex::new(Vec::new()) });
|
||||
}
|
||||
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,8 +98,7 @@ 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() };
|
||||
@@ -115,6 +127,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 +139,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));
|
||||
}
|
||||
|
||||
|
||||
+132
-3
@@ -148,7 +148,7 @@ fn apply_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native:
|
||||
Some(CHROMA_420) => next.chroma = CHROMA_420,
|
||||
_ => {}
|
||||
}
|
||||
// ChooseResolutionType 3 = follow remote (native); otherwise pixel size, else logical size.
|
||||
// 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);
|
||||
@@ -180,13 +180,15 @@ fn apply_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native:
|
||||
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)
|
||||
@@ -300,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(¤t, wf), Some(w));
|
||||
assert_eq!(number(¤t, 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);
|
||||
|
||||
+6
-2
@@ -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); }
|
||||
|
||||
@@ -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,
|
||||
|
||||
+63
-20
@@ -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;
|
||||
|
||||
@@ -58,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)),
|
||||
@@ -168,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}"),
|
||||
}
|
||||
}
|
||||
@@ -195,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) {
|
||||
@@ -236,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();
|
||||
@@ -261,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 };
|
||||
@@ -271,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;
|
||||
@@ -290,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.
|
||||
|
||||
+14
-3
@@ -656,13 +656,24 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
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; } }
|
||||
}
|
||||
let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
|
||||
&& crate::session_lock::locked_now() == Some(false);
|
||||
let stale_portal=if desktop {
|
||||
if let Some(session)=capture.portal.clone() {
|
||||
// A failed check must not end the host loop; treat it as not stale.
|
||||
match tokio::task::spawn_blocking(move || session.retire_if_closed()).await {
|
||||
Ok(Ok(stale)) => stale,
|
||||
Ok(Err(error)) => { log::warn!("Portal staleness check failed: {error}"); false }
|
||||
Err(_) => { log::warn!("Portal staleness check panicked"); false }
|
||||
}
|
||||
} else { false }
|
||||
} else { 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) {
|
||||
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 && (capture.init_error.is_some() || stale_portal || resized) {
|
||||
if stale_portal {
|
||||
input.release_held();input.bind_portal(None);
|
||||
capture=Arc::new(ScreenCapture::unavailable("Portal authorization expired".into()));
|
||||
|
||||
+416
-36
@@ -1,13 +1,15 @@
|
||||
//! 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, 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_clipboard_supported(portal: *mut c_void) -> i32;
|
||||
@@ -16,21 +18,85 @@ mod native {
|
||||
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>, retired: bool }
|
||||
impl Recovery {
|
||||
fn ready(&mut self, now: std::time::Instant, unlocked: impl FnOnce() -> bool) -> bool {
|
||||
if self.retired { 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 retire_if_closed<T>(current: &Mutex<T>, recovery: &Mutex<Recovery>, alive: impl FnOnce(&T) -> bool, unlocked: impl FnOnce() -> bool) -> Result<bool, String> {
|
||||
// Wait outside the input mutex. Inspect the published replacement if recovery
|
||||
// won; otherwise prevent the old encoder from opening after reconnect takes over.
|
||||
let mut recovery = recovery.lock().map_err(|_| "Portal recovery lock poisoned")?;
|
||||
let alive = {
|
||||
let handle = current.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||
alive(&handle)
|
||||
};
|
||||
if alive || !unlocked() { return Ok(false); }
|
||||
recovery.retired = true;
|
||||
Ok(true)
|
||||
}
|
||||
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<(), String> {
|
||||
// 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("Portal dimensions changed; reconnect required".into())
|
||||
} 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 retire_if_closed(&self) -> Result<bool, String> {
|
||||
retire_if_closed(&self.handle, &self.recovery, |handle| unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 },
|
||||
|| crate::session_lock::locked_now() == Some(false))
|
||||
}
|
||||
pub fn open() -> Result<Self, String> {
|
||||
let (mut error, mut width, mut height) = (0, 0, 0);
|
||||
let directory = super::permissions::directory();
|
||||
@@ -53,10 +119,17 @@ mod native {
|
||||
// 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 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.
|
||||
@@ -64,41 +137,71 @@ mod native {
|
||||
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
||||
}
|
||||
super::blank(frame); return Ok(());
|
||||
return Ok(blank_changed(frame, version));
|
||||
}
|
||||
if self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel) {
|
||||
if self.was_locked.load(std::sync::atomic::Ordering::Acquire) {
|
||||
// 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 self.take_lock_observation()? { unsafe { uu_portal_reset_frame(handle.0.as_ptr()) }; }
|
||||
return Ok(blank_changed(frame, version));
|
||||
}
|
||||
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(());
|
||||
}
|
||||
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()),
|
||||
};
|
||||
*retry = None;
|
||||
if replacement.width != self.width || replacement.height != self.height {
|
||||
return Err("Portal dimensions changed; reconnect required".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() {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
log::warn!("Portal capture recovery deferred: {error}");
|
||||
recovery.retry_at = Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
|
||||
return Ok(None);
|
||||
}
|
||||
};
|
||||
recovery.retry_at = None;
|
||||
self.check_geometry(&replacement)?;
|
||||
// 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);
|
||||
}
|
||||
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 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) }
|
||||
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)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clipboard_supported(&self) -> bool {
|
||||
@@ -134,7 +237,282 @@ 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(¤t, 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(¤t, 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 reconnect_waits_for_recovery_and_rechecks_the_published_handle() {
|
||||
use std::{sync::mpsc::channel, time::Duration};
|
||||
for succeeds in [false, true] {
|
||||
let current = Arc::new(Mutex::new(false));
|
||||
let recovery = Arc::new(Mutex::new(Recovery::default()));
|
||||
let (opening, opened) = channel();
|
||||
let (resume, wait) = channel();
|
||||
let (checked, observed) = channel();
|
||||
let (starting, started) = channel();
|
||||
let worker = {
|
||||
let current = current.clone();
|
||||
let recovery = recovery.clone();
|
||||
std::thread::spawn(move || {
|
||||
let handle = current.lock().unwrap();
|
||||
let _owner = recovery.lock().unwrap();
|
||||
replace_handle(¤t, handle, || {
|
||||
opening.send(()).unwrap();
|
||||
wait.recv_timeout(Duration::from_secs(10)).unwrap();
|
||||
if succeeds { Ok(Some(true)) } else { Ok(None) }
|
||||
}).unwrap()
|
||||
})
|
||||
};
|
||||
let ready = opened.recv_timeout(Duration::from_secs(3));
|
||||
let reconnect = {
|
||||
let current = current.clone();
|
||||
let recovery = recovery.clone();
|
||||
std::thread::spawn(move || {
|
||||
starting.send(()).unwrap();
|
||||
retire_if_closed(¤t, &recovery, |alive| {
|
||||
checked.send(*alive).unwrap();
|
||||
*alive
|
||||
}, || {
|
||||
assert!(current.try_lock().is_ok());
|
||||
true
|
||||
}).unwrap()
|
||||
})
|
||||
};
|
||||
let started = started.recv_timeout(Duration::from_secs(3));
|
||||
// A reconnect waiting on recovery must leave input's mutex available.
|
||||
let input_available = current.try_lock().is_ok();
|
||||
let premature_check = observed.recv_timeout(Duration::from_millis(100));
|
||||
let _ = resume.send(());
|
||||
let recovered = worker.join().unwrap();
|
||||
let stale = reconnect.join().unwrap();
|
||||
assert!(ready.is_ok() && started.is_ok() && input_available);
|
||||
assert!(matches!(premature_check, Err(std::sync::mpsc::RecvTimeoutError::Timeout)));
|
||||
assert_eq!(observed.recv_timeout(Duration::from_secs(3)).unwrap(), succeeds);
|
||||
assert_eq!(recovered, succeeds);
|
||||
assert_eq!(stale, !succeeds);
|
||||
assert_eq!(recovery.lock().unwrap().retired, !succeeds);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reconnect_retirement_prevents_a_late_encoder_recovery() {
|
||||
let current = Mutex::new(false);
|
||||
let recovery = Mutex::new(Recovery::default());
|
||||
// Locked or unknown sessions must not be retired by a reconnect.
|
||||
assert!(!retire_if_closed(¤t, &recovery, |alive| *alive, || false).unwrap());
|
||||
assert!(!recovery.lock().unwrap().retired);
|
||||
assert!(retire_if_closed(¤t, &recovery, |alive| *alive, || true).unwrap());
|
||||
let mut recovery = recovery.lock().unwrap();
|
||||
recovery.retry_at = Some(std::time::Instant::now());
|
||||
assert!(!recovery.ready(std::time::Instant::now(), || panic!("retired encoder probed logind")));
|
||||
}
|
||||
|
||||
#[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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,8 +542,10 @@ impl PortalSession {
|
||||
pub fn clipboard_set(&self, _: &str) -> Result<(), String> { Err(error(2)) }
|
||||
pub fn clipboard_poll(&self) -> Option<String> { None }
|
||||
pub fn is_alive(&self) -> bool { false }
|
||||
pub fn retire_if_closed(&self) -> Result<bool, String> { Ok(false) }
|
||||
pub fn open() -> Result<Self, String> { Err("Portal backend requires Linux and --features portal".into()) }
|
||||
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)) }
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,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 +180,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 +222,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 +297,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,6 +368,18 @@ 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());
|
||||
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 {
|
||||
|
||||
+61
-11
@@ -71,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() {
|
||||
@@ -82,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() {
|
||||
@@ -137,7 +173,7 @@ 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 || {
|
||||
@@ -145,15 +181,7 @@ fn spawn(requested_id: u32, cols: u16, rows: u16, shell: Option<&str>) -> Result
|
||||
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);
|
||||
@@ -212,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())),
|
||||
@@ -225,6 +254,9 @@ impl TerminalConn {
|
||||
let replay: Vec<u8> = s.scrollback.lock().unwrap().iter().copied().collect();
|
||||
// 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)); }
|
||||
}
|
||||
@@ -291,13 +323,31 @@ 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()) });
|
||||
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); } }
|
||||
|
||||
+405
-84
@@ -1,9 +1,12 @@
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::process::Command;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::{mpsc, Semaphore, OwnedSemaphorePermit};
|
||||
use webrtc::media::Sample;
|
||||
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
|
||||
use webrtc::peer_connection::RTCPeerConnection;
|
||||
@@ -11,6 +14,60 @@ use crate::capture_setting::{VideoCodec, VideoSettings};
|
||||
|
||||
/// Default frame rate; the viewer's 帧率 setting overrides it per connection.
|
||||
pub const FPS: u64 = 60;
|
||||
|
||||
/// Refresh rate reported to the viewer (display_info / capability / codec negotiation), like the
|
||||
/// official hosts report the monitor's mode (official Mac capability: 120). `UU_DISPLAY_FPS` overrides;
|
||||
/// Hyprland from `hyprctl -j monitors`, X11 from the current `xrandr` mode, otherwise 60.
|
||||
pub fn display_fps() -> u32 {
|
||||
static CACHE: std::sync::Mutex<Option<(std::time::Instant, u32)>> = std::sync::Mutex::new(None);
|
||||
let mut cache = CACHE.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some((at, fps)) = *cache { if at.elapsed() < Duration::from_secs(30) { return fps; } }
|
||||
let fps = std::env::var("UU_DISPLAY_FPS").ok().and_then(|v| v.trim().parse::<f64>().ok())
|
||||
.or_else(detect_refresh).map_or(FPS as u32, |hz| hz.round().clamp(24.0, 360.0) as u32);
|
||||
*cache = Some((std::time::Instant::now(), fps));
|
||||
fps
|
||||
}
|
||||
fn detect_refresh() -> Option<f64> {
|
||||
if cfg!(test) { return None; }
|
||||
// Bounded: a display tool must never stall connection setup (e.g. an X server started on demand).
|
||||
let run = |program: &str, args: &[&str]| -> Option<String> {
|
||||
use std::io::Read;
|
||||
let mut child = std::process::Command::new(program).args(args).stdin(std::process::Stdio::null())
|
||||
.stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null()).spawn().ok()?;
|
||||
let deadline = std::time::Instant::now() + Duration::from_secs(1);
|
||||
let status = loop {
|
||||
if let Some(status) = child.try_wait().ok()? { break status; }
|
||||
if std::time::Instant::now() >= deadline { let _ = child.kill(); let _ = child.wait(); return None; }
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
};
|
||||
let mut text = String::new();
|
||||
child.stdout.take()?.read_to_string(&mut text).ok()?;
|
||||
status.success().then_some(text)
|
||||
};
|
||||
if std::env::var_os("HYPRLAND_INSTANCE_SIGNATURE").is_some() {
|
||||
if let Some(hz) = run("hyprctl", &["-j", "monitors"]).as_deref().and_then(hyprland_refresh) { return Some(hz); }
|
||||
}
|
||||
if std::env::var_os("DISPLAY").is_some() { return run("xrandr", &["--current"]).as_deref().and_then(xrandr_refresh); }
|
||||
None
|
||||
}
|
||||
/// The focused monitor (else the first); the portal/Hyprland capture serves one output.
|
||||
fn hyprland_refresh(json: &str) -> Option<f64> {
|
||||
let monitors: Vec<serde_json::Value> = serde_json::from_str(json).ok()?;
|
||||
let monitor = monitors.iter().find(|m| m["focused"] == true).or(monitors.first())?;
|
||||
monitor["refreshRate"].as_f64().filter(|hz| *hz > 0.0)
|
||||
}
|
||||
/// The current mode (`60.00*+`) of the output marked `primary`, else of the first connected output.
|
||||
fn xrandr_refresh(text: &str) -> Option<f64> {
|
||||
let current = |lines: &mut dyn Iterator<Item = &str>| lines.take_while(|l| l.starts_with(' '))
|
||||
.flat_map(str::split_whitespace).find(|t| t.contains('*'))
|
||||
.and_then(|t| t.trim_end_matches(['*', '+']).parse::<f64>().ok());
|
||||
let mut lines = text.lines();
|
||||
while let Some(line) = lines.next() {
|
||||
if line.contains(" connected primary") { return current(&mut lines); }
|
||||
}
|
||||
let mut lines = text.lines().skip_while(|l| !l.contains(" connected")).skip(1);
|
||||
current(&mut lines)
|
||||
}
|
||||
/// Keyframe interval. All-intra 60fps measured 30 Mbit/s on the real desktop (saturating the
|
||||
/// viewer's Wi-Fi: 3-6 fps received). On a static desktop with NVENC CQ18 a 0.5s GOP measured
|
||||
/// 7.7 Mbit/s and a 2s GOP 1.9 Mbit/s. A broken prediction chain freezes until the next IDR or
|
||||
@@ -66,6 +123,7 @@ struct CompleteFrame {
|
||||
sequence: u64,
|
||||
completed_at: Instant,
|
||||
data: Vec<u8>,
|
||||
_permit: Option<OwnedSemaphorePermit>,
|
||||
}
|
||||
|
||||
const RTP_CLOCK: u64 = 90_000;
|
||||
@@ -136,13 +194,27 @@ pub(crate) fn x265_params(gop: &str) -> String {
|
||||
fn cpu_hevc_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
|
||||
let (maxrate, buffer) = rate_args(settings, Encoder::Cpu);
|
||||
let (profile, pix_fmt) = chroma_args(settings, Encoder::Cpu);
|
||||
["-c:v", "libx265", "-preset", "superfast", "-tune", "zerolatency", "-profile:v", profile,
|
||||
"-pix_fmt", pix_fmt, "-g", gop, "-bf", "0", "-threads", "4", "-crf", &settings.cq().to_string(),
|
||||
let mut options = ["-c:v", "libx265", "-preset", "superfast", "-tune", "zerolatency", "-profile:v", profile,
|
||||
"-pix_fmt", pix_fmt, "-g", gop, "-bf", "0", "-threads", "4",
|
||||
"-maxrate", &maxrate, "-bufsize", &buffer, "-x265-params", &x265_params(gop)]
|
||||
.into_iter().map(str::to_owned).collect()
|
||||
.into_iter().map(str::to_owned).collect::<Vec<_>>();
|
||||
// Same as libx264: capped CRF starves repeated keyframes, so bitrate presets use ABR.
|
||||
match settings.max_kbps() {
|
||||
Some(k) => options.extend(["-b:v".into(), format!("{}k", k * 3 / 4)]),
|
||||
None => options.extend(["-crf".into(), settings.cq().to_string()]),
|
||||
}
|
||||
options
|
||||
}
|
||||
|
||||
fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str) -> Vec<String> {
|
||||
/// NVENC converts packed RGB on the GPU (BT.601 limited range, tagged bt470bg like the swscale default;
|
||||
/// compared on the 3090), so 4:2:0 without scaling skips the CPU BGRA→YUV pass (#16: ~2 cores at 4K).
|
||||
/// `input` is the source pixel format; `UU_NVENC_RGB=0` restores CPU conversion.
|
||||
fn nvenc_rgb_input(settings: &VideoSettings, encoder: Encoder, input: &'static str) -> Option<&'static str> {
|
||||
(encoder == Encoder::Nvenc && settings.chroma != crate::capture_setting::CHROMA_444 && settings.output.is_none()
|
||||
&& std::env::var("UU_NVENC_RGB").as_deref() != Ok("0")).then_some(input)
|
||||
}
|
||||
|
||||
fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str, rgb: Option<&'static str>) -> Vec<String> {
|
||||
let (maxrate, bufsize) = rate_args(settings, encoder);
|
||||
let cq = settings.cq().to_string();
|
||||
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
|
||||
@@ -150,27 +222,122 @@ fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str) -> Vec
|
||||
let (profile, pix_fmt) = chroma_args(settings, encoder);
|
||||
if encoder == Encoder::Nvenc {
|
||||
return ["-c:v", encoder.name_for(settings.codec), "-preset", "p4", "-tune", "ull",
|
||||
"-profile:v", profile, "-pix_fmt", pix_fmt, "-g", gop, "-bf", "0",
|
||||
"-profile:v", profile, "-pix_fmt", rgb.unwrap_or(pix_fmt), "-g", gop, "-bf", "0",
|
||||
"-rc", "vbr", "-cq", &cq, "-b:v", &target, "-maxrate", &maxrate, "-bufsize", &bufsize,
|
||||
"-rc-lookahead", "0", "-zerolatency", "1", "-delay", "0", "-aud", "1"]
|
||||
.into_iter().map(str::to_owned).collect();
|
||||
}
|
||||
if settings.codec == VideoCodec::H265 { return cpu_hevc_options(settings, gop); }
|
||||
let params = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
|
||||
["-c:v", "libx264", "-preset", "superfast", "-tune", "zerolatency",
|
||||
let mut options = vec!["-c:v", "libx264", "-preset", "superfast", "-tune", "zerolatency",
|
||||
"-profile:v", profile, "-pix_fmt", pix_fmt, "-g", gop, "-threads", "4",
|
||||
"-crf", &cq, "-maxrate", &maxrate, "-bufsize", &bufsize, "-x264-params", ¶ms]
|
||||
.into_iter().map(str::to_owned).collect()
|
||||
"-maxrate", &maxrate, "-bufsize", &bufsize, "-x264-params", ¶ms];
|
||||
// Capped CRF starves repeated 4K keyframes; use ABR for bitrate presets.
|
||||
if settings.max_kbps().is_some() { options.extend(["-b:v", &target]); }
|
||||
else { options.extend(["-crf", &cq]); }
|
||||
options.into_iter().map(str::to_owned).collect()
|
||||
}
|
||||
|
||||
fn ffmpeg_command() -> Command {
|
||||
match crate::config::ffmpeg_root() {
|
||||
Some(root) => { let mut command = Command::new(root.join("bin/ffmpeg"));
|
||||
command.env("LD_LIBRARY_PATH", root.join("lib/x86_64-linux-gnu")); command }
|
||||
None => Command::new("ffmpeg"),
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
struct FfmpegSpec {
|
||||
program: PathBuf,
|
||||
ld_library_path: Option<OsString>,
|
||||
path: Option<OsString>,
|
||||
}
|
||||
|
||||
impl FfmpegSpec {
|
||||
fn command(&self) -> Command {
|
||||
let mut command = Command::new(&self.program);
|
||||
if let Some(path) = &self.ld_library_path { command.env("LD_LIBRARY_PATH", path); }
|
||||
if let Some(path) = &self.path { command.env("PATH", path); }
|
||||
command
|
||||
}
|
||||
}
|
||||
|
||||
fn ffmpeg_spec() -> FfmpegSpec {
|
||||
match crate::config::ffmpeg_root() {
|
||||
Some(root) => FfmpegSpec {
|
||||
program: root.join("bin/ffmpeg"),
|
||||
ld_library_path: Some(root.join("lib/x86_64-linux-gnu").into_os_string()),
|
||||
path: None,
|
||||
},
|
||||
None => FfmpegSpec {
|
||||
program: PathBuf::from("ffmpeg"),
|
||||
ld_library_path: None,
|
||||
path: std::env::var_os("PATH"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn ffmpeg_command() -> Command { ffmpeg_spec().command() }
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum FfmpegTimingMode { Modern, Legacy }
|
||||
|
||||
fn help_has_option(help: &str, option: &str) -> bool {
|
||||
help.lines().filter_map(|line| line.split_whitespace().next()).any(|token| {
|
||||
token == option || token.starts_with(&format!("{option}:") ) || token.starts_with(&format!("{option}["))
|
||||
})
|
||||
}
|
||||
|
||||
fn timing_mode_from_help(help: &str) -> FfmpegTimingMode {
|
||||
if help_has_option(help, "-fps_mode") && help_has_option(help, "-enc_time_base") {
|
||||
FfmpegTimingMode::Modern
|
||||
} else {
|
||||
FfmpegTimingMode::Legacy
|
||||
}
|
||||
}
|
||||
|
||||
fn timing_args(mode: FfmpegTimingMode, fps: u32) -> Vec<String> {
|
||||
match mode {
|
||||
FfmpegTimingMode::Modern => vec!["-fps_mode".into(), "vfr".into(), "-enc_time_base".into(), format!("1/{fps}")],
|
||||
FfmpegTimingMode::Legacy => vec!["-vsync".into(), "0".into()],
|
||||
}
|
||||
}
|
||||
|
||||
/// Legacy is chosen only from a completed help dump that visibly lacks the
|
||||
/// modern options. A failed probe (spawn error, non-zero exit, timeout, or no
|
||||
/// parseable help text) must keep the modern args: `-vsync` is deprecated
|
||||
/// since FFmpeg 5.1 and may not be accepted by newer builds, so falling back
|
||||
/// to it on a failed probe would kill the encoder there — worse than guessing.
|
||||
fn timing_mode_from_probe(success: bool, stdout: &[u8], stderr: &[u8]) -> FfmpegTimingMode {
|
||||
let mut help = Vec::with_capacity(stdout.len() + stderr.len());
|
||||
help.extend_from_slice(stdout);
|
||||
help.extend_from_slice(stderr);
|
||||
if !success || help.is_empty() { return FfmpegTimingMode::Modern; }
|
||||
timing_mode_from_help(&String::from_utf8_lossy(&help))
|
||||
}
|
||||
|
||||
async fn probe_timing_mode(spec: &FfmpegSpec) -> FfmpegTimingMode {
|
||||
let mut command = spec.command();
|
||||
command.args(["-hide_banner", "-nostdin", "-h", "full"])
|
||||
.kill_on_drop(true)
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped());
|
||||
match tokio::time::timeout(Duration::from_secs(3), command.output()).await {
|
||||
Ok(Ok(output)) => timing_mode_from_probe(output.status.success(), &output.stdout, &output.stderr),
|
||||
_ => timing_mode_from_probe(false, &[], &[]),
|
||||
}
|
||||
}
|
||||
|
||||
fn timing_cache() -> &'static Mutex<HashMap<FfmpegSpec, Arc<tokio::sync::OnceCell<FfmpegTimingMode>>>> {
|
||||
static CACHE: OnceLock<Mutex<HashMap<FfmpegSpec, Arc<tokio::sync::OnceCell<FfmpegTimingMode>>>>> = OnceLock::new();
|
||||
CACHE.get_or_init(|| Mutex::new(HashMap::new()))
|
||||
}
|
||||
|
||||
fn timing_cell(
|
||||
cache: &Mutex<HashMap<FfmpegSpec, Arc<tokio::sync::OnceCell<FfmpegTimingMode>>>>,
|
||||
spec: &FfmpegSpec,
|
||||
) -> Arc<tokio::sync::OnceCell<FfmpegTimingMode>> {
|
||||
let mut cache = cache.lock().expect("FFmpeg timing cache poisoned");
|
||||
cache.entry(spec.clone()).or_insert_with(|| Arc::new(tokio::sync::OnceCell::const_new())).clone()
|
||||
}
|
||||
|
||||
async fn ffmpeg_timing_mode(spec: &FfmpegSpec) -> FfmpegTimingMode {
|
||||
let cell = timing_cell(timing_cache(), spec);
|
||||
*cell.get_or_init(|| probe_timing_mode(spec)).await
|
||||
}
|
||||
|
||||
/// A compiled-in encoder name does not prove that its driver can initialize.
|
||||
/// Probe one synthetic 4:2:0 frame once per process before advertising HEVC.
|
||||
pub async fn hevc_available() -> bool {
|
||||
@@ -186,7 +353,7 @@ pub async fn hevc_available() -> bool {
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
command.args(["-f", "lavfi", "-i", "color=black:size=256x256:rate=1", "-frames:v", "1", "-an"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vf", &vaapi_filter(None)]); }
|
||||
command.args(encoder_options(&settings, encoder, "30"));
|
||||
command.args(encoder_options(&settings, encoder, "30", None));
|
||||
command.args(["-f", "hevc", "pipe:1"]).kill_on_drop(true).stderr(std::process::Stdio::null());
|
||||
if let Ok(Ok(output)) = tokio::time::timeout(Duration::from_secs(5), command.output()).await {
|
||||
if output.status.success() && is_independent_frame_for(&output.stdout, VideoCodec::H265) {
|
||||
@@ -260,16 +427,16 @@ impl Drop for StopOnDrop { fn drop(&mut self) { self.0.store(true, std::sync::at
|
||||
async fn read_encoder(
|
||||
size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||
latest: mpsc::Sender<Arc<CompleteFrame>>,
|
||||
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
|
||||
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
|
||||
) -> Result<(), String> {
|
||||
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct).
|
||||
let encoder = Encoder::requested();
|
||||
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone()).await {
|
||||
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr,budget).await; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone(),budget.clone()).await {
|
||||
Ok(())=>Ok(()),
|
||||
Err(error) if !latest.is_closed()=>{
|
||||
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name_for(settings.codec),error);
|
||||
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await
|
||||
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr,budget).await
|
||||
},
|
||||
Err(error)=>Err(error),
|
||||
}
|
||||
@@ -279,21 +446,21 @@ async fn read_encoder(
|
||||
#[cfg(feature = "inproc-av")]
|
||||
async fn read_inprocess(
|
||||
config: crate::inproc_encoder::Config, latest: mpsc::Sender<Arc<CompleteFrame>>,
|
||||
sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
|
||||
sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
|
||||
) -> Result<(), String> {
|
||||
let codec = config.codec;
|
||||
let (units_tx, mut units) = mpsc::channel::<Vec<u8>>(4);
|
||||
let (units_tx, mut units) = mpsc::channel::<crate::inproc_encoder::EncodedUnit>(4);
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
// If this future is dropped (reader task aborted), the blocking encoder stops at its next packet.
|
||||
let _stop_on_drop = StopOnDrop(stop.clone());
|
||||
let job = tokio::task::spawn_blocking({
|
||||
let stop = stop.clone();
|
||||
move || crate::inproc_encoder::run(config, units_tx, stop, idr)
|
||||
move || crate::inproc_encoder::run(config, units_tx, stop, idr, budget)
|
||||
});
|
||||
let mut gate = DependencyGate::default();
|
||||
while let Some(data) = units.recv().await {
|
||||
while let Some(unit) = units.recv().await {
|
||||
let sequence = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
|
||||
gate.submit(&latest, Arc::new(CompleteFrame { codec, sequence, completed_at: Instant::now(), data }));
|
||||
gate.submit(&latest, Arc::new(CompleteFrame { codec, sequence, completed_at: unit.completed_at, data: unit.data, _permit: Some(unit.permit) }));
|
||||
if latest.is_closed() { break; }
|
||||
}
|
||||
// Dropping the receiver also unblocks the encoder if it is waiting to send.
|
||||
@@ -324,10 +491,11 @@ fn rate_args(settings: &VideoSettings, encoder: Encoder) -> (String, String) {
|
||||
|
||||
async fn read_encoder_with(
|
||||
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,sequence:Arc<std::sync::atomic::AtomicU64>,
|
||||
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
|
||||
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
|
||||
)->Result<(),String>{
|
||||
let kms=display.starts_with("kms:");
|
||||
let wayland=kms || display.starts_with("wayland:");
|
||||
let (permits_tx,mut permits_rx)=mpsc::channel::<OwnedSemaphorePermit>(4);
|
||||
let kms_capture=if kms {
|
||||
let capture=tokio::task::spawn_blocking(crate::kms::Capture::open).await.map_err(|_|"KMS worker failed")??;
|
||||
if (capture.width,capture.height)!=size { return Err("KMS mode changed; restart host to refresh desktop geometry".into()); }
|
||||
@@ -345,7 +513,7 @@ async fn read_encoder_with(
|
||||
};
|
||||
log::info!("Desktop encoder: display={} source={}x{} output={:?} fps={} cq={} maxrate={}k encoder={} pipeline=in-process",
|
||||
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name_for(settings.codec));
|
||||
return read_inprocess(config, latest, sequence, idr).await;
|
||||
return read_inprocess(config, latest, sequence, idr, budget).await;
|
||||
}
|
||||
let size = format!("{}x{}", size.0, size.1);
|
||||
let fps = settings.fps.to_string();
|
||||
@@ -353,9 +521,12 @@ async fn read_encoder_with(
|
||||
let gop = gop_frames(settings.fps, false).to_string();
|
||||
let (maxrate, _) = rate_args(&settings, encoder);
|
||||
let cq = settings.cq().to_string();
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} pix_fmt={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name_for(settings.codec),if encoder == Encoder::Vaapi { "nv12" } else { chroma_args(&settings, encoder).1 });
|
||||
// x11grab delivers bgr0; the Wayland/portal/KMS pipe is bgra.
|
||||
let rgb = nvenc_rgb_input(&settings, encoder, if wayland { "bgra" } else { "bgr0" });
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} pix_fmt={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name_for(settings.codec),if encoder == Encoder::Vaapi { "nv12" } else { rgb.unwrap_or(chroma_args(&settings, encoder).1) });
|
||||
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
|
||||
let mut command = ffmpeg_command();
|
||||
let ffmpeg = ffmpeg_spec();
|
||||
let mut command = ffmpeg.command();
|
||||
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
if wayland {
|
||||
@@ -372,13 +543,35 @@ async fn read_encoder_with(
|
||||
} else if let Some((width, height)) = settings.output {
|
||||
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
|
||||
}
|
||||
command.args(encoder_options(&settings, encoder, &gop));
|
||||
command.args(encoder_options(&settings, encoder, &gop, rgb));
|
||||
// x11grab paces against an absolute schedule; after a capture stall it grabs
|
||||
// back-to-back to "catch up" (measured 69 frames in 200ms). vfr on a 1/FPS
|
||||
// timebase drops those same-slot frames instead of encoding a burst.
|
||||
let time_base = format!("1/{fps}");
|
||||
if wayland { command.stdin(std::process::Stdio::piped()); }
|
||||
let mut child=command.args(["-fps_mode","vfr","-enc_time_base",&time_base,"-f",if settings.codec == VideoCodec::H265 { "hevc" } else { "h264" },"pipe:1"])
|
||||
// timebase drops those same-slot frames instead of encoding a burst. The
|
||||
// legacy -vsync 0 path (FFmpeg 4.x) passes catch-up frames straight through
|
||||
// and encodes the whole burst; a known degradation on old FFmpeg.
|
||||
// Native Wayland: the capture process writes straight into the encoder's stdin;
|
||||
// relaying 33 MB 4K frames through this process cost a full extra copy per frame.
|
||||
let mut native_capture=None;
|
||||
if wayland && kms_capture.is_none() && portal.is_none() {
|
||||
// Linux keeps the running executable inode accessible after a scoped
|
||||
// development/release replacement; current_exe may name a deleted path.
|
||||
#[cfg(target_os="linux")]
|
||||
let executable=std::path::PathBuf::from("/proc/self/exe");
|
||||
#[cfg(not(target_os="linux"))]
|
||||
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
|
||||
let mut capture=wayland_capture_command(&executable,settings.fps)
|
||||
.env("UU_WAYLAND_DEMAND","1")
|
||||
.kill_on_drop(true).stdin(std::process::Stdio::piped()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
|
||||
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
|
||||
let source:std::process::Stdio=capture.stdout.take().ok_or("Capture stdout missing")?
|
||||
.try_into().map_err(|e|format!("Capture pipe: {e}"))?;
|
||||
command.stdin(source);
|
||||
native_capture=Some(capture);
|
||||
} else if wayland { command.stdin(std::process::Stdio::piped()); }
|
||||
let timing = timing_args(ffmpeg_timing_mode(&ffmpeg).await, settings.fps);
|
||||
let mut output_args = timing;
|
||||
output_args.extend(["-f".into(), if settings.codec == VideoCodec::H265 { "hevc".into() } else { "h264".into() }, "pipe:1".into()]);
|
||||
let mut child=command.args(output_args)
|
||||
.kill_on_drop(true)
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::inherit())
|
||||
@@ -390,11 +583,13 @@ async fn read_encoder_with(
|
||||
use tokio::io::AsyncWriteExt;
|
||||
let mut tick=tokio::time::interval(Duration::from_nanos(1_000_000_000/u64::from(settings.fps)));
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
|
||||
loop {
|
||||
tick.tick().await;
|
||||
let permit=budget.clone().acquire_owned().await.map_err(|_|"Encoder stopped")?;
|
||||
permits_tx.send(permit).await.map_err(|_|"Encoder stopped")?;
|
||||
let capture=capture.clone();
|
||||
let bytes=tokio::task::spawn_blocking(move || {
|
||||
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
|
||||
bytes=tokio::task::spawn_blocking(move || {
|
||||
capture.capture(&mut bytes)?; Ok::<_,String>(bytes)
|
||||
}).await.map_err(|_|"KMS worker failed")??;
|
||||
sink.write_all(&bytes).await.map_err(|_|"KMS encoder pipe failed")?;
|
||||
@@ -407,33 +602,42 @@ async fn read_encoder_with(
|
||||
let interval=Duration::from_nanos(1_000_000_000/u64::from(settings.fps));
|
||||
let mut tick=tokio::time::interval(interval);
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
let mut frame=vec![0;(session.width as usize)*(session.height as usize)*4];
|
||||
let mut version=None;
|
||||
loop {
|
||||
tick.tick().await;
|
||||
let permit=budget.clone().acquire_owned().await.map_err(|_|"Encoder stopped")?;
|
||||
permits_tx.send(permit).await.map_err(|_|"Encoder stopped")?;
|
||||
let session=session.clone();
|
||||
let frame=tokio::task::spawn_blocking(move || {
|
||||
let mut frame=vec![0;(session.width as usize)*(session.height as usize)*4];
|
||||
session.capture(&mut frame)?;
|
||||
Ok::<_,String>(frame)
|
||||
(frame,version)=tokio::task::spawn_blocking(move || {
|
||||
session.capture_changed(&mut frame,&mut version)?;
|
||||
Ok::<_,String>((frame,version))
|
||||
}).await.map_err(|e|format!("Portal capture worker: {e}"))??;
|
||||
sink.write_all(&frame).await.map_err(|e|format!("Portal encoder pipe: {e}"))?;
|
||||
}
|
||||
})))
|
||||
} else if wayland {
|
||||
// Linux keeps the running executable inode accessible after a scoped
|
||||
// development/release replacement; current_exe may name a deleted path.
|
||||
#[cfg(target_os="linux")]
|
||||
let executable=std::path::PathBuf::from("/proc/self/exe");
|
||||
#[cfg(not(target_os="linux"))]
|
||||
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
|
||||
let mut capture=wayland_capture_command(&executable,settings.fps)
|
||||
.kill_on_drop(true).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
|
||||
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
|
||||
let mut source=capture.stdout.take().ok_or("Capture stdout missing")?;
|
||||
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||
} else if let Some(mut capture)=native_capture {
|
||||
let mut demand=capture.stdin.take().ok_or("Capture stdin missing")?;
|
||||
Some(EncoderTask(tokio::spawn(async move {
|
||||
let result=tokio::io::copy(&mut source,&mut sink).await;
|
||||
drop(capture);
|
||||
result.map(|_|()).map_err(|e|format!("Wayland frame pipe: {e}"))
|
||||
use tokio::io::AsyncWriteExt;
|
||||
tokio::select! {
|
||||
result=async {
|
||||
let mut tick=tokio::time::interval(Duration::from_nanos(1_000_000_000/u64::from(settings.fps)));
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
loop {
|
||||
let permit=budget.clone().acquire_owned().await.map_err(|_|"Encoder stopped")?;
|
||||
tick.tick().await;
|
||||
permits_tx.send(permit).await.map_err(|_|"Encoder stopped")?;
|
||||
demand.write_all(&[1]).await.map_err(|_|"Capture demand pipe closed")?;
|
||||
}
|
||||
} => result,
|
||||
status=capture.wait() => {
|
||||
let status=status.map_err(|e|format!("Wayland capture: {e}"))?;
|
||||
// Nothing awaits this task; log so the cause survives the generic encoder EOF.
|
||||
log::warn!("Wayland capture exited: {status}");
|
||||
Err(format!("Wayland capture exited: {status}"))
|
||||
},
|
||||
}
|
||||
})))
|
||||
} else { None };
|
||||
let mut stdout = child.stdout.take().ok_or("Encoder stdout missing")?;
|
||||
@@ -452,8 +656,9 @@ async fn read_encoder_with(
|
||||
if next > MAX_ACCESS_UNIT { return Err("Encoder access unit exceeds limit".into()); }
|
||||
let frame: Vec<u8> = pending.drain(..next).collect();
|
||||
let sequence = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
|
||||
let permit=if wayland {Some(permits_rx.recv().await.ok_or("Capture producer stopped")?)} else {None};
|
||||
gate.submit(&latest, Arc::new(CompleteFrame {
|
||||
codec: settings.codec, sequence, completed_at: Instant::now(), data: frame,
|
||||
codec: settings.codec, sequence, completed_at: Instant::now(), data: frame, _permit: permit,
|
||||
}));
|
||||
if latest.is_closed() { return Ok(()); }
|
||||
}
|
||||
@@ -482,8 +687,14 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
|
||||
let (tx,mut rx)=mpsc::channel(4);
|
||||
let sequence=Arc::new(std::sync::atomic::AtomicU64::new(0));
|
||||
let fps=crate::wayland::capture_fps(std::env::var("UU_PROBE_FPS").ok().as_deref())?;
|
||||
let settings=VideoSettings {fps,..VideoSettings::default()};
|
||||
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)))));
|
||||
// UU_PROBE_QUALITY selects a viewer preset (1 fast 1M, 2 general 2M, 3 HD 8M, 4 bluray 20M,
|
||||
// 5 auto); bitrate presets exercise the capped/ABR encoder path instead of CRF.
|
||||
let quality=match std::env::var("UU_PROBE_QUALITY").ok().as_deref() {
|
||||
None=>crate::capture_setting::QUALITY_AUTO,
|
||||
Some(v)=>v.parse::<u64>().ok().filter(|q|(1..=5).contains(q)).ok_or("UU_PROBE_QUALITY must be 1-5")?,
|
||||
};
|
||||
let settings=VideoSettings {fps,quality,..VideoSettings::default()};
|
||||
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)),Arc::new(Semaphore::new(4)))));
|
||||
let mut count=0;
|
||||
let mut bytes=0;
|
||||
// UU_PROBE_FRAMES lengthens the sample for baseline measurements; default is 5 frames.
|
||||
@@ -511,6 +722,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
}
|
||||
crate::capture::keep_display_awake()?;
|
||||
let (latest, mut receiver) = mpsc::channel(4);
|
||||
let budget=Arc::new(Semaphore::new(4));
|
||||
let sequence = Arc::new(std::sync::atomic::AtomicU64::new(0));
|
||||
let mut current = *settings.borrow_and_update();
|
||||
if !track.supports(current.codec) && track.supports(VideoCodec::H265) {
|
||||
@@ -518,7 +730,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
applied.send_replace(current);
|
||||
settings.borrow_and_update();
|
||||
}
|
||||
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone())));
|
||||
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone())));
|
||||
let mut previous = None;
|
||||
let mut timeline = RtpTimeline { fps: u64::from(current.fps), ..Default::default() };
|
||||
let mut settings_open = true;
|
||||
@@ -529,39 +741,42 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
let mut health = tokio::time::interval(Duration::from_millis(200));
|
||||
health.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
let mut stats = Instant::now();
|
||||
loop {
|
||||
// sent/skipped are cumulative; window_* and max_* cover the interval since the last stats line.
|
||||
let (mut window_frames, mut window_skipped) = (0u64, 0u64);
|
||||
let mut send_interrupted=false;
|
||||
'stream: loop {
|
||||
let next = *settings.borrow_and_update();
|
||||
if next != current || send_interrupted {
|
||||
let discard = next.codec != current.codec || send_interrupted;
|
||||
current = next;
|
||||
timeline.fps = u64::from(next.fps);
|
||||
if discard {
|
||||
reader.0.abort();
|
||||
let _ = (&mut reader.0).await;
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone()));
|
||||
} else {
|
||||
reader.restart(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone())).await;
|
||||
}
|
||||
send_interrupted=false;
|
||||
}
|
||||
tokio::select! {
|
||||
result = &mut reader.0 => {
|
||||
let result = result.map_err(|e| format!("Encoder reader task: {e}"))?;
|
||||
if result.is_err() && current.codec == VideoCodec::H265 && track.supports(VideoCodec::H264) {
|
||||
log::warn!("HEVC unavailable; falling back to negotiated H264");
|
||||
current.codec = VideoCodec::H264;
|
||||
if let (Err(error), VideoCodec::H265, true) = (&result, current.codec, track.supports(VideoCodec::H264)) {
|
||||
log::warn!("HEVC stream failed ({error}); falling back to negotiated H264");
|
||||
applied.send_modify(|value| value.codec = VideoCodec::H264);
|
||||
current = *settings.borrow_and_update();
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone()));
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone()));
|
||||
continue;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
_ = health.tick() => { if transport_closed(&pc) { return Ok(()); } },
|
||||
changed = settings.changed(), if settings_open => {
|
||||
if changed.is_err() { settings_open = false; continue; }
|
||||
// Restart the encoder with the viewer's new 帧率/画质; it begins with an IDR, and frames
|
||||
// still queued from the old encoder remain a valid chain before it.
|
||||
let next = *settings.borrow_and_update();
|
||||
let codec_changed = next.codec != current.codec;
|
||||
current = next;
|
||||
timeline.fps = u64::from(next.fps);
|
||||
if codec_changed {
|
||||
reader.0.abort();
|
||||
let _ = (&mut reader.0).await;
|
||||
// Drain only after the old producer is gone, before the new IDR can enter.
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone()));
|
||||
} else {
|
||||
reader.restart(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone())).await;
|
||||
}
|
||||
if changed.is_err() { settings_open = false; }
|
||||
continue;
|
||||
},
|
||||
frame = receiver.recv() => {
|
||||
// A closed queue means the reader ended; its JoinHandle carries the reason.
|
||||
@@ -582,6 +797,10 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
tokio::select! {
|
||||
result = &mut send => { result.map_err(|e| format!("Video send: {e}"))?; break; },
|
||||
_ = health.tick() => { if transport_closed(&pc) { return Ok(()); } },
|
||||
changed = settings.changed(), if settings_open => {
|
||||
if changed.is_err() { settings_open=false; }
|
||||
else if *settings.borrow()!=current { send_interrupted=true; continue 'stream; }
|
||||
},
|
||||
}
|
||||
}
|
||||
previous = Some(frame.sequence);
|
||||
@@ -589,7 +808,9 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
frames += 1;
|
||||
if stats.elapsed() >= Duration::from_secs(30) {
|
||||
crate::capture::keep_display_awake()?;
|
||||
log::info!("Desktop latest-frame stats: sent={} skipped={} latest_seq={} max_slot_age_ms={} max_send_ms={}", frames, skipped, frame.sequence, max_age_ms,max_send_ms);
|
||||
log::info!("Desktop latest-frame stats: sent={} skipped={} latest_seq={} window_s={} window_sent={} window_skipped={} max_slot_age_ms={} max_send_ms={}",
|
||||
frames, skipped, frame.sequence, stats.elapsed().as_secs(), frames - window_frames, skipped - window_skipped, max_age_ms, max_send_ms);
|
||||
(window_frames, window_skipped) = (frames, skipped);
|
||||
stats = Instant::now();
|
||||
max_age_ms = 0;
|
||||
max_send_ms = 0;
|
||||
@@ -601,6 +822,16 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn display_refresh_parsers() {
|
||||
let xrandr = "Screen 0: minimum 8 x 8, current 5760 x 2160\nHDMI-1 connected 1920x1080+3840+0 (normal) 0mm x 0mm\n 1920x1080 60.00*+ 50.00\nDP-1 connected primary 3840x2160+0+0 (normal) 600mm x 340mm\n 3840x2160 60.00 + 143.98*\n 2560x1440 120.00\n";
|
||||
assert_eq!(xrandr_refresh(xrandr), Some(143.98));
|
||||
assert_eq!(xrandr_refresh("Screen 0:\nVGA-1 disconnected\nHDMI-1 connected 1920x1080+0+0\n 1920x1080 74.97*+ 60.00\n"), Some(74.97));
|
||||
assert_eq!(xrandr_refresh("Screen 0:\nHDMI-1 connected\n 1920x1080 60.00 +\n"), None);
|
||||
assert_eq!(hyprland_refresh(r#"[{"name":"HDMI-A-1","focused":false,"refreshRate":60.0},{"name":"DP-1","focused":true,"refreshRate":143.856}]"#), Some(143.856));
|
||||
assert_eq!(hyprland_refresh("[]"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_wayland_child_uses_requested_capture_rate() {
|
||||
for fps in [5,30,60,90,144] {
|
||||
@@ -610,6 +841,96 @@ mod tests {
|
||||
}
|
||||
}
|
||||
use super::*;
|
||||
#[tokio::test]
|
||||
async fn frame_budget_spans_queued_sending_and_restarted_encoders() {
|
||||
let budget=Arc::new(Semaphore::new(2));
|
||||
let (tx,mut rx)=mpsc::channel(4);
|
||||
let mut gate=DependencyGate::default();
|
||||
for sequence in 1..=2 {
|
||||
let permit=budget.clone().acquire_owned().await.unwrap();
|
||||
gate.submit(&tx,Arc::new(CompleteFrame {codec:VideoCodec::H264,sequence,completed_at:Instant::now(),
|
||||
data:vec![0,0,1,5,42],_permit:Some(permit)}));
|
||||
}
|
||||
let sending=rx.recv().await.unwrap();
|
||||
assert_eq!(budget.available_permits(),0);
|
||||
let restarted=tokio::spawn(budget.clone().acquire_owned());
|
||||
tokio::task::yield_now().await;
|
||||
assert!(!restarted.is_finished());
|
||||
drop(sending);
|
||||
let permit=tokio::time::timeout(Duration::from_secs(1),restarted).await.unwrap().unwrap().unwrap();
|
||||
assert_eq!(budget.available_permits(),0);
|
||||
drop(rx);
|
||||
assert_eq!(budget.available_permits(),1);
|
||||
drop(permit);
|
||||
assert_eq!(budget.available_permits(),2);
|
||||
}
|
||||
#[test]
|
||||
fn timing_help_requires_both_modern_options() {
|
||||
let modern = " -fps_mode mode video sync method\n -enc_time_base ratio set encoder timebase";
|
||||
assert_eq!(timing_mode_from_help(modern), FfmpegTimingMode::Modern);
|
||||
assert_eq!(timing_mode_from_help(" -vsync parameter\n"), FfmpegTimingMode::Legacy);
|
||||
assert_eq!(timing_mode_from_help(" -fps_mode mode\n"), FfmpegTimingMode::Legacy);
|
||||
assert_eq!(timing_mode_from_help(" -enc_time_base ratio\n"), FfmpegTimingMode::Legacy);
|
||||
assert_eq!(timing_mode_from_help(" --not-fps_mode-test\n"), FfmpegTimingMode::Legacy);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timing_args_match_ffmpeg_generation() {
|
||||
assert_eq!(timing_args(FfmpegTimingMode::Modern, 60),
|
||||
vec!["-fps_mode", "vfr", "-enc_time_base", "1/60"]);
|
||||
assert_eq!(timing_args(FfmpegTimingMode::Legacy, 30), vec!["-vsync", "0"]);
|
||||
assert!(!timing_args(FfmpegTimingMode::Modern, 60).contains(&"-r".to_owned()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timing_probe_failure_keeps_modern_args() {
|
||||
let modern = b" -fps_mode mode\n -enc_time_base ratio\n";
|
||||
// Legacy is only confirmed from a completed help dump that lacks the options.
|
||||
assert_eq!(timing_mode_from_probe(true, b"", b" -fps_mode mode\n"), FfmpegTimingMode::Legacy);
|
||||
// Any probe failure keeps the modern args (-vsync is deprecated on new FFmpeg).
|
||||
assert_eq!(timing_mode_from_probe(false, modern, &[]), FfmpegTimingMode::Modern);
|
||||
assert_eq!(timing_mode_from_probe(false, &[], &[]), FfmpegTimingMode::Modern);
|
||||
assert_eq!(timing_mode_from_probe(true, &[], &[]), FfmpegTimingMode::Modern);
|
||||
assert_eq!(timing_mode_from_probe(true, modern, &[]), FfmpegTimingMode::Modern);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn timing_cache_initializer_runs_once_for_concurrent_requests() {
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
let cell = Arc::new(tokio::sync::OnceCell::new());
|
||||
let calls = Arc::new(AtomicUsize::new(0));
|
||||
let first = cell.clone();
|
||||
let first_calls = calls.clone();
|
||||
let second = cell.clone();
|
||||
let second_calls = calls.clone();
|
||||
let (first_mode, second_mode) = tokio::join!(
|
||||
async move { *first.get_or_init(|| async {
|
||||
first_calls.fetch_add(1, Ordering::SeqCst);
|
||||
tokio::task::yield_now().await;
|
||||
FfmpegTimingMode::Legacy
|
||||
}).await },
|
||||
async move { *second.get_or_init(|| async {
|
||||
second_calls.fetch_add(1, Ordering::SeqCst);
|
||||
tokio::task::yield_now().await;
|
||||
FfmpegTimingMode::Modern
|
||||
}).await },
|
||||
);
|
||||
assert_eq!(first_mode, second_mode);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ffmpeg_specs_separate_executable_environments() {
|
||||
let path = FfmpegSpec { program: PathBuf::from("ffmpeg"), ld_library_path: None,
|
||||
path: Some(OsString::from("/one")) };
|
||||
let other_path = FfmpegSpec { program: PathBuf::from("ffmpeg"), ld_library_path: None,
|
||||
path: Some(OsString::from("/two")) };
|
||||
let bundled = FfmpegSpec { program: PathBuf::from("/opt/ffmpeg/bin/ffmpeg"),
|
||||
ld_library_path: Some(OsString::from("/opt/ffmpeg/lib/x86_64-linux-gnu")), path: None };
|
||||
assert_ne!(path, other_path);
|
||||
assert_ne!(path, bundled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x264_buffer_fits_keyframes() {
|
||||
assert_eq!(buffer_kbits(2000, Encoder::Nvenc), 250);
|
||||
@@ -647,7 +968,7 @@ mod tests {
|
||||
let (tx, mut rx) = mpsc::channel(4);
|
||||
let old_tx = tx.clone();
|
||||
let (started_tx, started_rx) = tokio::sync::oneshot::channel();
|
||||
let frame = |sequence, idr| Arc::new(CompleteFrame {
|
||||
let frame = |sequence, idr| Arc::new(CompleteFrame { _permit: None,
|
||||
sequence, codec: VideoCodec::H264, completed_at: Instant::now(),
|
||||
data: vec![0, 0, 1, 9, 240, 0, 0, 1, if idr { 5 } else { 1 }, 42],
|
||||
});
|
||||
@@ -742,7 +1063,7 @@ mod tests {
|
||||
async fn bounded_queue_never_sends_broken_prediction_chain_after_overload(){
|
||||
let(tx,mut rx)=mpsc::channel(1);
|
||||
let mut gate=DependencyGate::default();
|
||||
let frame=|sequence,idr|Arc::new(CompleteFrame{sequence,codec:VideoCodec::H264,completed_at:Instant::now(),data:vec![0,0,1,9,240,0,0,1,if idr{5}else{1},42]});
|
||||
let frame=|sequence,idr|Arc::new(CompleteFrame{_permit:None,sequence,codec:VideoCodec::H264,completed_at:Instant::now(),data:vec![0,0,1,9,240,0,0,1,if idr{5}else{1},42]});
|
||||
gate.submit(&tx,frame(1,false));assert!(rx.try_recv().is_err()); // no IDR yet
|
||||
gate.submit(&tx,frame(2,true));
|
||||
gate.submit(&tx,frame(3,false)); // queue full: P3 lost
|
||||
@@ -775,7 +1096,7 @@ mod tests {
|
||||
async fn hevc_dependency_gate_recovers_only_at_irap_after_overflow() {
|
||||
let (tx, mut rx) = mpsc::channel(1);
|
||||
let mut gate = DependencyGate::default();
|
||||
let frame = |sequence, irap| Arc::new(CompleteFrame { codec: VideoCodec::H265,
|
||||
let frame = |sequence, irap| Arc::new(CompleteFrame { _permit: None, codec: VideoCodec::H265,
|
||||
sequence, completed_at: Instant::now(), data: vec![0,0,1,if irap { 38 } else { 2 },1,42] });
|
||||
gate.submit(&tx, frame(1, false));
|
||||
assert!(rx.try_recv().is_err());
|
||||
|
||||
+7
-3
@@ -61,14 +61,15 @@ impl VideoTrack {
|
||||
};
|
||||
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: state.timestamp,
|
||||
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; }); }
|
||||
state.timestamp = state.timestamp.wrapping_add((sample.duration.as_secs_f64() * 90_000.0) as u32);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -106,6 +107,9 @@ mod tests {
|
||||
if hevc { register_hevc(&mut remote_media).unwrap(); }
|
||||
let mut setting = webrtc::api::setting_engine::SettingEngine::default();
|
||||
setting.set_ice_multicast_dns_mode(webrtc::ice::mdns::MulticastDnsMode::Disabled);
|
||||
// UDP over IPv4 only: pairing every host interface (VPNs, IPv6) made ICE exceed the
|
||||
// connect timeout when the whole suite runs in parallel.
|
||||
setting.set_network_types(vec![webrtc::ice::network_type::NetworkType::Udp4]);
|
||||
let remote = APIBuilder::new().with_setting_engine(setting.clone()).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();
|
||||
@@ -140,7 +144,7 @@ mod tests {
|
||||
}).await.unwrap();
|
||||
assert_eq!(track.supports(VideoCodec::H264), h264);
|
||||
assert_eq!(track.supports(VideoCodec::H265), hevc);
|
||||
tokio::time::timeout(std::time::Duration::from_secs(10), async {
|
||||
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;
|
||||
}
|
||||
|
||||
+36
-6
@@ -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> {
|
||||
|
||||
+32
-3
@@ -165,7 +165,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::device_capability(client, ice, 0, crate::video::FPS as i32, 5, hevc);
|
||||
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);
|
||||
}
|
||||
@@ -273,9 +273,15 @@ impl WebRtcSession {
|
||||
let control_video_track = video_track.clone();
|
||||
let stream_settings = video_settings.clone();
|
||||
if desktop {
|
||||
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, stream_settings, idr_request).await {
|
||||
log::error!("Live desktop stream stopped: {}", e);
|
||||
// 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 {
|
||||
@@ -464,7 +470,7 @@ impl WebRtcSession {
|
||||
(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::FPS as u32, init_track.supports(crate::capture_setting::VideoCodec::H265))))),
|
||||
&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() {
|
||||
@@ -889,6 +895,29 @@ impl WebRtcSession {
|
||||
.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(¤t, &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(¤t, &mut names, &mut sections);
|
||||
log::info!("Offer SDP structure: {}", sections.join(" | "));
|
||||
let offer = RTCSessionDescription::offer(sdp_str)
|
||||
.map_err(|e| format!("Invalid offer: {}", e))?;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
+242
-2
@@ -1,7 +1,190 @@
|
||||
#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 clipboard_bounds(void) {
|
||||
int fds[2]; uint8_t bytes[8]={0};
|
||||
assert(pipe2(fds,O_NONBLOCK)==0);
|
||||
@@ -14,8 +197,65 @@ 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();
|
||||
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 +328,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));
|
||||
|
||||
@@ -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
@@ -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?);
|
||||
|
||||
Reference in new issue
Block a user