Compare commits

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

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

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

---------

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Co-authored-by: VallierDev <[email protected]>
2026-10-17 10:48:43 +08:00
29 changed files with 2778 additions and 211 deletions

No files matched your search

+1 -1
View File
@@ -37,7 +37,7 @@ jobs:
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
for source in tests/portal_engine.c tests/portal_input.c tests/portal_eis.c tests/desktop_power.c; do
target="/tmp/$(basename "$source" .c)"
cc -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer \
-Wall -Wextra -Wno-unused-parameter "$source" -o "$target" \
+1
View File
@@ -128,6 +128,7 @@ every 60 s.
| `src/auth.rs`, `src/config.rs`, `src/login.rs`, `src/login_ui.rs` | signed REST client, config, device init + QR login, GTK window |
| `src/webrtc.rs` | peer connection, data channels, CONTROL dispatch, initialization messages |
| `src/video.rs`, `src/capture_setting.rs`, `src/video_track.rs`, `src/hevc.rs` | FFmpeg H.264/H.265 pipeline, one video track for both codecs (RFC 7798 HEVC payloader); viewer quality/fps/resolution/chroma settings |
| `src/locked_video.rs` | low-rate synthetic black video on the negotiated track while Portal is pending and locked; enables iPhone cold auto unlock (#29), requires FFmpeg libx264/libx265 for the selected codec |
| `src/audio.rs` | Pulse/PipeWire monitor → Opus audio track |
| `src/input.rs`, `src/input_mapping.rs`, `src/uinput.rs` | input injection (X11 XTest / Wayland / portal / uinput), Windows VK → keysym → evdev |
| `src/cursor.rs` | pointer shape mirroring (X11) |
Generated
+1 -1
View File
@@ -2717,7 +2717,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uu-remote-linux"
version = "0.1.28"
version = "0.1.29"
dependencies = [
"async-trait",
"base64 0.22.1",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "uu-remote-linux"
version = "0.1.28"
version = "0.1.29"
edition = "2021"
authors = ["uu-remote-linux contributors"]
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
+10 -3
View File
@@ -67,16 +67,23 @@ authorized session. Independent file/terminal tool peers do not reset input.
| 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; validation scope and limits below | `--features wayland,portal` |
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
The GNOME Wayland Portal/EIS path has been checked for video and mouse/keyboard input on Ubuntu 26.04, GNOME 50, with one 4K screen at 200% scale; auto unlock after a cold locked connection is verified with the official Mac and iPhone 4.42.0 viewers, while other lock recovery and reconnection still need hardware acceptance testing. Apart from that path and GNOME on X11, desktop testing has primarily used virtual machines. If something goes wrong on
real hardware, please [open an issue](../../issues) with your distribution, desktop, GPU and the relevant lines from
`journalctl --user -u uuremote-native.service`.
Wayland limits: single screen, fixed resolution. The native Hyprland backend now pipelines two shm screencopy buffers and passes the viewer's requested fps to both capture pacing and the encoder. Actual fps remains limited by the compositor, rendering and readback (the old serial path measured about 16–22 fps at 2560x1600; the pipeline is VM-measured only, not a hardware guarantee). The portal backend does not support screen wake or
dynamic resolution yet; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
Wayland limits: single screen, fixed resolution. The native Hyprland backend now pipelines two shm screencopy buffers and passes the viewer's requested fps to both capture pacing and the encoder. Actual fps remains limited by the compositor, rendering and readback (the old serial path measured about 16–22 fps at 2560x1600; the pipeline is VM-measured only, not a hardware guarantee). The portal backend does not support dynamic resolution; see below for GNOME wake and lock recovery; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
permission; the permission is remembered once granted.
Scaled GNOME desktops using native Portal input use EIS on the same authorized session when `ConnectToEIS` and a screen `mapping_id` are available. Each axis is mapped using the matched region and actual frame dimensions, with no fixed resolution or scale factor. This avoids scaled-coordinate rejection by `NotifyPointerMotionAbsolute`. Portal builds require libei >= 1.1. Missing EIS support retains the legacy input path and its existing scaling limitations; other initialization failures are reported and trigger session recovery. After a successful EIS connection, all input uses EIS. Capture alone and uinput do not initialize EIS; missing optional logical dimensions do not block capture. Keyboard input uses the existing Windows VK to evdev mapping. Recovery keeps the encoder frame dimensions fixed but accepts logical-size changes after scaling. 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.
GNOME Portal CONTROL no longer waits for a consent dialog or the first video frame. It announces the selected desktop before reporting secure-desktop state and receiving the official viewer's “auto unlock host” request. The official Android viewer requires this desktop selection before entering its auto-unlock flow, so the notification must precede capture readiness; actual screen dimensions, codec information and capture permissions still follow their real readiness state. Enable auto unlock in the viewer and use the current Linux user's login password. The host verifies it with PAM, unlocks the existing GNOME session, requests display wake, and restores capture, input and clipboard through the shared Portal session. Initial consent or revoked permissions still require local approval. Reconnect if physical or logical dimensions change.
The host also accepts a desktop connection while GNOME is locked with no Portal session yet (a cold connection): it reports lock state and accepts auto unlock, then restores picture, input and clipboard after unlock. While Portal is pending, the host sends generated black video once per second on the negotiated track by default, allowing the official iPhone viewer to enter its auto-unlock flow without extra configuration. Verified on Ubuntu GNOME Wayland with the official Mac viewer; iPhone 4.42.0 with H.265 completed a same-executable control experiment: black video ON succeeded, OFF failed, and ON succeeded again ([#29](../../issues/29)). **Limitation**: this is synthetic black video, not the GNOME lock screen, and it does not enable native mouse/keyboard input while locked. Enable 「自动解锁被控端」 in the UU client under 控制中心 → 安全 and enter the password. Without saved Portal permission, local consent is still required after unlock before the real desktop can appear.
Existing screen-off and lock settings remain unchanged. Only capture recovery in an unlocked session temporarily holds a GNOME idle inhibitor; the first frame, lock, disconnect or initialization failure releases it. An idle viewer still allows the normal screen-off and lock timeout, and manual lock remains effective. Desktop input is blocked and real video is hidden while locked or when lock state is unknown. Independent terminal, file-transfer and port-mapping peers do not wait for video initialization and survive desktop reconnection. Automated protocol and lifecycle regression tests cover this path; auto unlock and capture recovery after a cold locked connection are verified on GNOME hardware with the official Mac and iPhone 4.42.0 viewers, while screen-off and the other scenarios still need acceptance testing. Waking a fully suspended or hibernating machine is outside this CONTROL path.
Locked black video requires FFmpeg's libx264/libx265 encoder for the negotiated H.264/H.265 codec; hardware encoding support alone does not satisfy this dependency. Encoder failures are logged explicitly. Synthetic video does not change capture permissions or advertised monitor dimensions. It pauses on unlock or unknown lock state, ends when capture is ready/rejected or the connection closes, and then hands the same video track to real capture. See [#29](../../issues/29) and [#34](../../pull/34) for the controlled experiment and validation scope.
The native Wayland backend needs `wlr-output-power-management` from the compositor to wake a turned-off screen; without it only wake is unavailable — capture and encoding are unaffected.
Native Wayland experiments: `UU_WAYLAND_PIPELINE=0` disables pipelining for comparison.
+10 -2
View File
@@ -61,13 +61,21 @@ Portal 仅作用于本服务获授权的会话。文件传输、终端等独立
| Hyprland | Wayland(wlroots 原生协议) | 🧪 实验性,虚拟机测试(部分功能受限,见下) | `--features wayland` |
| GNOME / KDE Plasma | Wayland(xdg-desktop-portal) | 🧪 实验性,验证范围与限制见下 | `--features wayland,portal` |
GNOME Wayland 的 Portal/EIS 路径已在 Ubuntu 26.04、GNOME 50、单屏 4K/200% 缩放下验证画面和键鼠;锁屏恢复及重连仍待实机验收。除 GNOME 的上述路径和 X11 外,其余桌面目前主要在虚拟机里测试。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
GNOME Wayland 的 Portal/EIS 路径已在 Ubuntu 26.04、GNOME 50、单屏 4K/200% 缩放下验证画面和键鼠;锁屏冷连接自动解锁已用 Mac 和 iPhone 4.42.0 客户端实机验证,其余锁屏恢复及重连仍待验收。除 GNOME 的上述路径和 X11 外,其余桌面目前主要在虚拟机里测试。如果你在真机上遇到问题,欢迎[提交 issue](../../issues),
请附上发行版、桌面环境、显卡型号和 `journalctl --user -u uuremote-native.service` 里的相关日志。
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持动态分辨率;GNOME 的屏幕唤醒与锁屏恢复见下;剪贴板只在控制端的远控窗口处于前台时同步。
GNOME 缩放桌面在使用 Portal 原生键鼠时,若支持 `ConnectToEIS` 和屏幕 `mapping_id`,通过同一授权会话的 EIS 接口注入输入,按匹配的区域与实际帧尺寸分别映射横纵坐标,没有固定分辨率或缩放倍率,可避免 `NotifyPointerMotionAbsolute` 在缩放时拒绝坐标。Portal 构建需要 libei ≥ 1.1。缺少 EIS 支持时保留旧输入路径及其既有缩放限制;其他初始化错误会明确报告并触发会话恢复。EIS 连接成功后全部输入统一使用 EIS。只采集画面或使用 uinput 时不会初始化 EIS;缺少可选的逻辑尺寸也不阻断采集。键盘沿用项目现有的 Windows VK → evdev 映射。恢复时保持编码器的采集帧尺寸不变,允许缩放后的逻辑尺寸变化。已开始推流的 Portal 采集若暂停且无有效协商格式持续五秒,将进入会话恢复;静态桌面不会触发该超时。Ubuntu 26.04、GNOME Wayland 单屏 3840×2160 已实测保持连接切换 200%→166.67%→200%,恢复后画面持续更新且鼠标可控制全屏;切换期间会短暂停顿、黑屏后恢复,五秒是故障判定阈值,并非总恢复时间。此路径仍需实机验证锁屏恢复;多屏、连接中改变分辨率及旋转/镜像变换不属于本次修复范围。 捕获设置协议仍为版本 4:对客户端上报的分辨率统一使用像素坐标、DPR 为 1;Portal 逻辑尺寸仅用于本地输入映射。
GNOME Portal 的控制通道不再等待授权窗口或第一帧画面:先通知客户端当前选择桌面,再上报锁屏状态、接收官方客户端的「自动解锁被控端」请求。官方 Android 客户端需要桌面目标通知才会进入自动解锁流程,因此该通知不能等待采集就绪;真实屏幕尺寸、编码信息及采集权限仍按实际就绪状态上报。请在客户端启用自动解锁,使用当前 Linux 用户的登录密码;主机通过 PAM 验证后解锁当前 GNOME 会话、请求点亮屏幕,并恢复同一 Portal 会话中的画面、键鼠和剪贴板。首次授权或权限被撤销时仍需本机确认。物理或逻辑分辨率变化后需要重连。
桌面锁屏且尚未建立 Portal 时(冷连接),主机也会建立控制通道:上报锁屏状态并接受自动解锁,解锁后再恢复画面、键鼠和剪贴板。Portal 等待就绪期间,主机默认通过已协商的视频轨每秒发送一帧生成的纯黑画面,让官方 iPhone 客户端进入自动解锁流程,无需额外配置。Ubuntu GNOME Wayland 上已用官方 Mac 客户端验证;iPhone 4.42.0 的 H.265 冷连接完成了同一实验二进制「开启黑帧成功 → 关闭失败 → 再开成功」对照([#29](../../issues/29))。**局限**:黑帧不是真实 GNOME 锁屏画面,也不支持在锁屏界面操作原生键鼠;需要在 UU 客户端 控制中心 → 安全 → 自动解锁被控端 中启用自动解锁并输入密码。若本机没有已保存的 Portal 授权,解锁后仍需本机确认授权才能显示真实桌面。
无需修改现有熄屏和锁屏设置。主机仅在会话已解锁的采集恢复期间临时申请 GNOME 的 idle 抑制,拿到首帧后立即释放;锁屏、断开或初始化失败也会释放。远控窗口空闲时仍按原策略熄屏和锁屏,手动锁屏也继续生效。锁屏及锁状态未知时停止桌面输入并隐藏真实画面;独立终端、文件传输和端口映射连接不依赖画面初始化,也不会因桌面重连被关闭。这一恢复路径已覆盖自动化协议与生命周期回归测试,锁屏冷连接后的自动解锁与采集恢复已用官方 Mac 和 iPhone 4.42.0 客户端在 GNOME 实机验证,熄屏等其余场景仍待验收;不支持从整机挂起或休眠状态通过现有控制通道唤醒。
锁屏黑帧使用 FFmpeg 的 libx264/libx265 编码器(按已协商的 H.264/H.265 选择),硬件编码器可用并不代表这些软件编码器已安装。编码失败会明确记录日志。黑帧不改变采集权限或上报的真实屏幕尺寸;解锁或锁状态未知时暂停,采集就绪、拒绝或连接关闭时结束,再由真实视频接续同一条视频轨。对照实验与验证范围见 [#29](../../issues/29)、[#34](../../pull/34)。
原生 Wayland 后端需要合成器提供 `wlr-output-power-management` 才能唤醒已关闭的屏幕;缺少该协议不影响采集和编码。
原生 Wayland 实验选项:`UU_WAYLAND_PIPELINE=0` 可关闭流水线做对照。`UU_WAYLAND_DMABUF=1` 显式尝试 dma-buf→GBM CPU 映射(默认关闭,需要运行时 libgbm;可用 `UU_WAYLAND_DMABUF_DEVICE` 指定 render node,默认 `/dev/dri/renderD128`)。仅支持显式线性修饰符、单平面 8-bit RGB,失败回退 SHM;virgl VM 中只验证了回退,成功导入/映射和硬件加速均未验证。这不是零拷贝 VAAPI 路径;选择 `UU_VIDEO_ENCODER=vaapi` 时仍由 FFmpeg 上传 CPU 帧。
+2 -1
View File
@@ -52,6 +52,7 @@ fn main() {
println!("cargo:rerun-if-changed=c_engine/portal_engine.c");
println!("cargo:rerun-if-changed=c_engine/portal_engine.h");
println!("cargo:rerun-if-changed=c_engine/portal_eis.inc");
println!("cargo:rerun-if-changed=c_engine/desktop_power.c");
let flags=|kind:&str| {
let output=std::process::Command::new("pkg-config").args([kind,"gio-2.0","gio-unix-2.0","libpipewire-0.3","libei-1.0 >= 1.1"]).output()
.expect("Install pkg-config and GIO/PipeWire/libei development libraries");
@@ -59,7 +60,7 @@ fn main() {
String::from_utf8(output.stdout).expect("pkg-config flags must be UTF-8")
};
let mut build=cc::Build::new();
build.file("c_engine/portal_engine.c").flag_if_supported("-Wno-unused-parameter");
build.file("c_engine/portal_engine.c").file("c_engine/desktop_power.c").flag_if_supported("-Wno-unused-parameter");
for flag in flags("--cflags").split_whitespace() { build.flag(flag); }
build.compile("uu_portal_engine");
for flag in flags("--libs").split_whitespace() {
+154
View File
@@ -0,0 +1,154 @@
#include <gio/gio.h>
#include <stdlib.h>
#define CALL_TIMEOUT_MS 3000
#define MUTTER_DEST "org.gnome.Mutter.DisplayConfig"
#define MUTTER_PATH "/org/gnome/Mutter/DisplayConfig"
#define SESSION_DEST "org.gnome.SessionManager"
#define SESSION_PATH "/org/gnome/SessionManager"
#define SESSION_IFACE "org.gnome.SessionManager"
enum {
UU_POWER_OK = 0,
UU_POWER_SESSION_BUS = 1,
UU_POWER_CALL_FAILED = 2,
};
struct uu_idle_inhibitor {
GDBusConnection *connection;
guint32 cookie;
};
struct setup_timeout {
GMutex lock;
GCond cond;
gboolean finished;
GCancellable *cancellable;
};
static gpointer cancel_setup_on_timeout(gpointer data) {
struct setup_timeout *timeout = data;
gint64 deadline = g_get_monotonic_time() + CALL_TIMEOUT_MS * G_TIME_SPAN_MILLISECOND;
g_mutex_lock(&timeout->lock);
while (!timeout->finished &&
g_cond_wait_until(&timeout->cond, &timeout->lock, deadline)) {}
gboolean expired = !timeout->finished;
g_mutex_unlock(&timeout->lock);
if (expired) g_cancellable_cancel(timeout->cancellable);
return NULL;
}
static void finish_setup_timeout(struct setup_timeout *timeout, GThread *thread) {
g_mutex_lock(&timeout->lock);
timeout->finished = TRUE;
g_cond_signal(&timeout->cond);
g_mutex_unlock(&timeout->lock);
g_thread_join(thread);
g_cond_clear(&timeout->cond);
g_mutex_clear(&timeout->lock);
g_object_unref(timeout->cancellable);
}
/* Use a dedicated connection so closing this inhibitor cannot affect another app's bus state. */
static GDBusConnection *private_session_bus(int *error_code) {
struct setup_timeout timeout = {0};
g_mutex_init(&timeout.lock);
g_cond_init(&timeout.cond);
timeout.cancellable = g_cancellable_new();
GThread *timer = g_thread_new("uu-dbus-connect-timeout", cancel_setup_on_timeout, &timeout);
GError *error = NULL;
gchar *address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SESSION, timeout.cancellable, &error);
if (!address) {
g_clear_error(&error);
finish_setup_timeout(&timeout, timer);
*error_code = UU_POWER_SESSION_BUS;
return NULL;
}
GDBusConnection *connection = g_dbus_connection_new_for_address_sync(
address,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
NULL, timeout.cancellable, &error);
g_free(address);
finish_setup_timeout(&timeout, timer);
if (!connection) {
g_clear_error(&error);
*error_code = UU_POWER_SESSION_BUS;
return NULL;
}
g_dbus_connection_set_exit_on_close(connection, FALSE);
return connection;
}
static void close_private_bus(GDBusConnection *connection) {
if (!connection) return;
g_dbus_connection_close(connection, NULL, NULL, NULL);
g_object_unref(connection);
}
int uu_desktop_power_wake(void) {
int error_code = UU_POWER_OK;
GDBusConnection *connection = private_session_bus(&error_code);
if (!connection) return error_code;
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(
connection, MUTTER_DEST, MUTTER_PATH, "org.freedesktop.DBus.Properties", "Set",
g_variant_new("(ssv)", "org.gnome.Mutter.DisplayConfig", "PowerSaveMode",
g_variant_new_int32(0)),
G_VARIANT_TYPE("()"), G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error);
if (!reply) error_code = UU_POWER_CALL_FAILED;
if (reply) g_variant_unref(reply);
g_clear_error(&error);
close_private_bus(connection);
return error_code;
}
struct uu_idle_inhibitor *uu_desktop_idle_inhibitor_acquire(int *error_code) {
if (!error_code) return NULL;
*error_code = UU_POWER_OK;
GDBusConnection *connection = private_session_bus(error_code);
if (!connection) return NULL;
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(
connection, SESSION_DEST, SESSION_PATH, SESSION_IFACE, "Inhibit",
g_variant_new("(susu)", "uu-remote-linux", 0u,
"Remote desktop session is active", 8u),
G_VARIANT_TYPE("(u)"), G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error);
if (!reply) {
*error_code = UU_POWER_CALL_FAILED;
g_clear_error(&error);
close_private_bus(connection);
return NULL;
}
struct uu_idle_inhibitor *inhibitor = calloc(1, sizeof(*inhibitor));
if (!inhibitor) {
*error_code = UU_POWER_CALL_FAILED;
g_variant_unref(reply);
close_private_bus(connection);
return NULL;
}
g_variant_get(reply, "(u)", &inhibitor->cookie);
g_variant_unref(reply);
inhibitor->connection = connection;
return inhibitor;
}
void uu_desktop_idle_inhibitor_release(struct uu_idle_inhibitor *inhibitor) {
if (!inhibitor) return;
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(
inhibitor->connection, SESSION_DEST, SESSION_PATH, SESSION_IFACE, "Uninhibit",
g_variant_new("(u)", inhibitor->cookie), G_VARIANT_TYPE("()"),
G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT_MS, NULL, &error);
if (reply) g_variant_unref(reply);
g_clear_error(&error);
/* Closing the private bus also revokes the cookie if Uninhibit failed or the host exits. */
close_private_bus(inhibitor->connection);
free(inhibitor);
}
+70 -17
View File
@@ -46,6 +46,8 @@ struct uu_portal {
pthread_mutex_t lock;
pthread_cond_t changed;
atomic_int closed;
uu_portal_cancelled_fn cancelled;
void *cancel_data;
int error;
enum pw_stream_state state;
int format_valid, stream_started;
@@ -71,6 +73,10 @@ static pthread_once_t pw_once = PTHREAD_ONCE_INIT;
static atomic_uint token_sequence = 1;
static void initialize_pipewire(void) { pw_init(NULL, NULL); }
static GVariant *empty_options(void) { return g_variant_new_array(G_VARIANT_TYPE("{sv}"), NULL, 0); }
static int open_cancelled(struct uu_portal *p) {
return p->cancelled && p->cancelled(p->cancel_data);
}
static int wait_for_first_frame(struct uu_portal *p);
#include "portal_eis.inc"
static void fail(struct uu_portal *p, int error) {
pthread_mutex_lock(&p->lock);
@@ -87,6 +93,16 @@ static void response_received(GDBusConnection *bus, const gchar *sender, const g
g_variant_get(parameters, "(u@a{sv})", &r->code, &r->results);
r->done = 1;
}
/* Bounded consent wait; cancellation must break it within one poll interval. */
static int wait_for_response(struct uu_portal *p, struct response *response, unsigned consent_seconds) {
gint64 deadline = g_get_monotonic_time() + consent_seconds * G_TIME_SPAN_SECOND;
while (!response->done && !atomic_load(&p->closed) && !open_cancelled(p) &&
g_get_monotonic_time() < deadline) {
while (g_main_context_iteration(p->context, FALSE)) {}
if (!response->done) g_usleep(5000);
}
return response->done ? 0 : open_cancelled(p) ? 13 : atomic_load(&p->closed) ? 9 : 5;
}
static void close_request(struct uu_portal *p, const char *path) {
if (!path) return;
GVariant *r = g_dbus_connection_call_sync(p->bus, DEST, path, REQUEST, "Close",
@@ -96,6 +112,7 @@ static void close_request(struct uu_portal *p, const char *path) {
/* Subscribe before invocation, using the documented handle_token path. */
static GVariant *request(struct uu_portal *p, const char *interface, const char *method,
GVariantBuilder *options, int mode, int *error) {
if (open_cancelled(p)) { *error = 13; return NULL; }
char token[64];
snprintf(token, sizeof(token), "uu_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
g_variant_builder_add(options, "{sv}", "handle_token", g_variant_new_string(token));
@@ -112,7 +129,7 @@ static GVariant *request(struct uu_portal *p, const char *interface, const char
GVariant *reply = g_dbus_connection_call_sync(p->bus, DEST, PATH, interface, method, args,
G_VARIANT_TYPE("(o)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, NULL);
char *actual = NULL;
if (!reply) { *error = 3; goto finish; }
if (!reply) { *error = open_cancelled(p) ? 13 : 3; goto finish; }
const char *handle;
g_variant_get(reply, "(&o)", &handle);
actual = g_strdup(handle);
@@ -125,13 +142,8 @@ static GVariant *request(struct uu_portal *p, const char *interface, const char
unsigned long parsed=strtoul(configured_timeout,&end,10);
if(end && !*end && parsed>=30 && parsed<=900) consent_seconds=(unsigned)parsed;
}
gint64 deadline = g_get_monotonic_time() + consent_seconds * G_TIME_SPAN_SECOND;
while (!response.done && !atomic_load(&p->closed) && g_get_monotonic_time() < deadline) {
while (g_main_context_iteration(p->context, FALSE)) {}
if (!response.done) g_usleep(5000);
}
if (!response.done) *error = atomic_load(&p->closed) ? 9 : 5;
else if (response.code != 0) *error = 4;
*error = wait_for_response(p, &response, consent_seconds);
if (!*error && response.code != 0) *error = 4;
finish:
if (*error) close_request(p, actual ? actual : expected);
g_dbus_connection_signal_unsubscribe(p->bus, subscription);
@@ -262,10 +274,16 @@ static const struct pw_stream_events stream_events = {
PW_VERSION_STREAM_EVENTS, .state_changed = stream_state, .param_changed = stream_format, .process = stream_process
};
static int connect_pipewire(struct uu_portal *p) {
if (open_cancelled(p)) return 13;
GUnixFDList *fds = NULL;
GVariant *reply = g_dbus_connection_call_with_unix_fd_list_sync(p->bus, DEST, PATH, SCREEN,
"OpenPipeWireRemote", g_variant_new("(o@a{sv})", p->session, empty_options()),
G_VARIANT_TYPE("(h)"), G_DBUS_CALL_FLAGS_NONE, 10000, NULL, &fds, NULL, NULL);
if (open_cancelled(p)) {
if (reply) g_variant_unref(reply);
if (fds) g_object_unref(fds);
return 13;
}
if (!reply) return 7;
gint32 index; g_variant_get(reply, "(h)", &index);
int fd = g_unix_fd_list_get(fds, index, NULL);
@@ -297,12 +315,22 @@ static int connect_pipewire(struct uu_portal *p) {
if (pw_stream_connect(p->stream, PW_DIRECTION_INPUT, p->node,
PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS, params, 2) < 0) return 7;
if (pw_thread_loop_start(p->pw_loop) < 0) return 7;
struct timespec until; clock_gettime(CLOCK_REALTIME, &until); until.tv_sec += 10;
return wait_for_first_frame(p);
}
static int wait_for_first_frame(struct uu_portal *p) {
struct timespec until;
gint64 deadline = g_get_monotonic_time() + 10 * G_TIME_SPAN_SECOND;
pthread_mutex_lock(&p->lock);
while (!p->sequence && !p->error) {
if (pthread_cond_timedwait(&p->changed, &p->lock, &until)) break;
while (!p->sequence && !p->error && !open_cancelled(p)) {
gint64 remaining = deadline - g_get_monotonic_time();
if (remaining <= 0) break;
clock_gettime(CLOCK_REALTIME, &until);
gint64 wait_us = MIN(100000, remaining);
until.tv_nsec += wait_us * 1000;
if (until.tv_nsec >= 1000000000) { until.tv_sec++; until.tv_nsec -= 1000000000; }
pthread_cond_timedwait(&p->changed, &p->lock, &until);
}
int result = p->error ? p->error : p->sequence ? 0 : 10;
int result = p->error ? p->error : open_cancelled(p) ? 13 : p->sequence ? 0 : 10;
pthread_mutex_unlock(&p->lock);
return result;
}
@@ -327,10 +355,14 @@ static int read_stream_result(struct uu_portal *p, GVariant *results) {
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) {
static struct uu_portal *open_persist(const char *restore, void (*save_token)(const char *, void *),
void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data,
int *error, uint32_t *width, uint32_t *height) {
*error = 0; *width = *height = 0;
struct uu_portal *p = calloc(1, sizeof(*p));
if (!p) { *error = 8; return NULL; }
p->cancelled = cancelled;
p->cancel_data = cancel_data;
/* GNOME forwards NotifyPointerMotionAbsolute into Mutter's stream-to-stage
* transform. It expects stream pixels, unlike the compositor-sized KDE path.
* Do not apply the compositor scale twice on fractional-scale displays. */
@@ -342,7 +374,10 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
p->context = g_main_context_new(); g_main_context_push_thread_default(p->context);
p->bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
if (!p->bus) { *error = 1; goto done; }
if (!has_interface(p, REMOTE) || !has_interface(p, SCREEN)) { *error = 2; goto done; }
if (open_cancelled(p)) { *error = 13; goto done; }
int interfaces_available = has_interface(p, REMOTE) && has_interface(p, SCREEN);
if (open_cancelled(p)) { *error = 13; goto done; }
if (!interfaces_available) { *error = 2; goto done; }
GVariantBuilder options; GVariant *results;
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
char token[64]; snprintf(token, sizeof(token), "uu_session_%u_%u", (unsigned)getpid(), atomic_fetch_add(&token_sequence, 1));
@@ -405,24 +440,42 @@ struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)
fprintf(stderr, "Portal persistence: requested=%d restore_attempted=%d token_returned=%d\n",
version >= 2, restore && *restore, p->restore_token && *p->restore_token);
}
if (!*error && p->restore_token && save_token) save_token(p->restore_token, data);
if (!*error && p->restore_token && save_token) save_token(p->restore_token, save_data);
g_variant_unref(results);
if (*error) goto done;
p->main_loop = g_main_loop_new(p->context, FALSE);
g_main_context_pop_thread_default(p->context);
if (pthread_create(&p->dbus_thread, NULL, run_dbus, p)) { *error = 3; uu_portal_close(p); return NULL; }
if (pthread_create(&p->dbus_thread, NULL, run_dbus, p)) {
*error = 3; p->cancelled = NULL; p->cancel_data = NULL;
uu_portal_close(p); return NULL;
}
p->dbus_started = 1;
*error = connect_pipewire(p);
if (*error) { uu_portal_close(p); return NULL; }
if (*error) {
p->cancelled = NULL; p->cancel_data = NULL;
uu_portal_close(p); return NULL;
}
fprintf(stderr, "Portal pointer geometry: frame=%ux%u compositor=%dx%d absolute=%s\n",
p->width, p->height, p->logical_width, p->logical_height,
p->absolute_stream_pixels ? "stream-pixels-or-eis" : "compositor-logical");
*width = p->width; *height = p->height;
p->cancelled = NULL; p->cancel_data = NULL;
return p;
done:
g_main_context_pop_thread_default(p->context);
p->cancelled = NULL; p->cancel_data = NULL;
uu_portal_close(p); return NULL;
}
struct uu_portal *uu_portal_open_persist_cancellable(const char *restore,
void (*save_token)(const char *, void *), void *save_data,
uu_portal_cancelled_fn cancelled, void *cancel_data,
int *error, uint32_t *width, uint32_t *height) {
return open_persist(restore, save_token, save_data, cancelled, cancel_data, error, width, height);
}
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *),
void *data, int *error, uint32_t *width, uint32_t *height) {
return open_persist(restore, save_token, data, NULL, NULL, error, width, height);
}
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height) {
return uu_portal_open_persist(restore, NULL, NULL, error, width, height);
}
+3
View File
@@ -3,10 +3,13 @@
#include <stddef.h>
#include <stdint.h>
struct uu_portal;
typedef int (*uu_portal_cancelled_fn)(void *data);
/* One object owns one consent/session and one PipeWire stream. */
struct uu_portal *uu_portal_open(int *error, uint32_t *width, uint32_t *height);
/* Saves the rotated token immediately after Start, before video initialization. */
struct uu_portal *uu_portal_open_persist(const char *restore, void (*save_token)(const char *, void *), void *data, int *error, uint32_t *width, uint32_t *height);
/* The cancellation callback and data are used only until this call returns. */
struct uu_portal *uu_portal_open_persist_cancellable(const char *restore, void (*save_token)(const char *, void *), void *save_data, uu_portal_cancelled_fn cancelled, void *cancel_data, int *error, uint32_t *width, uint32_t *height);
struct uu_portal *uu_portal_open_restore(const char *restore, int *error, uint32_t *width, uint32_t *height);
const char *uu_portal_restore_token(struct uu_portal *portal);
void uu_portal_close(struct uu_portal *portal);
+22
View File
@@ -0,0 +1,22 @@
GNOME Wayland 锁屏时也能连接并自动解锁。/ Connect and auto unlock while GNOME Wayland is locked.
### 修复 / Fixed
- **GNOME Wayland(Portal)锁屏冷连接**(#28,感谢 @wanglz111,#30):服务在已解锁时启动、随后锁屏,再从客户端连接时,
以前因为 Portal 无法创建而整个连接被丢弃,无法远程自动解锁。现在先建立控制通道、上报锁屏状态并处理「自动解锁被控端」,
Portal 就绪后再恢复画面、输入和剪贴板;锁屏期间不发画面、不注入桌面输入,并唤醒显示器(不会解锁)。
官方 **Mac 客户端**已实机验证(锁屏 → 连接 → 自动解锁 → 画面和输入恢复)。
- **iPhone 锁屏冷连接自动解锁**(#29,感谢 @wanglz111,#34):iPhone 4.42.0 要先收到视频帧才会发起自动解锁。Portal 等待就绪且
确认锁屏时,主机每秒在已协商的视频轨上发一帧生成的黑画面(需 FFmpeg 带 libx264/libx265),解锁后由真实画面接续。
贡献者用同一二进制做了 开→关→开 对照,iPhone 4.42.0 + H.265 实机通过;⚠️ H.264 手机实机、其他客户端版本、慢速中继未验收。
黑帧不是真实锁屏画面,锁屏界面不能操作键鼠;未开启「自动解锁被控端」时无法远程解锁(控制中心 → 安全 → 自动解锁被控端)。
Locked cold connections on GNOME Wayland now establish CONTROL first and support auto unlock (Mac verified); black video
while locked lets iPhone 4.42.0 start auto unlock (H.265 verified by the contributor).
- 断开事件只关闭对应 `ice_id` 的连接,不再误关同时打开的文件传输、终端、端口映射连接。
Releases close only the matching peer; tool peers stay open.
- X11 主机回归:控制通道处理改动后,已在 X11 + NVENC 主机上实测连接、Echo、锁屏状态上报和画面;画质切换由自动化测试覆盖。
X11 host re-checked live (connection, Echo, lock state, video).
### 下载 / Download
`uu-remote-linux-v0.1.29-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+10 -5
View File
@@ -66,6 +66,12 @@ pub struct ScreenCapture {
}
impl ScreenCapture {
pub fn open_portal(cancelled: &std::sync::atomic::AtomicBool) -> Result<Self, crate::portal::PortalError> {
let session = std::sync::Arc::new(crate::portal::PortalSession::open_cancellable(cancelled)?);
Ok(Self { backend: crate::desktop_backend::DesktopBackend::Portal, init_error: None,
display: "wayland:portal".into(), width: session.width, height: session.height,
portal: Some(session), kms: None, buffer: Mutex::new(Vec::new()) })
}
pub fn new() -> Result<Self, String> {
let backend=crate::desktop_backend::detect()?;
if backend == crate::desktop_backend::DesktopBackend::Kms {
@@ -75,10 +81,7 @@ impl ScreenCapture {
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::new()) });
return Self::open_portal(&std::sync::atomic::AtomicBool::new(false)).map_err(|error| error.to_string());
}
if backend == crate::desktop_backend::DesktopBackend::Wayland {
let (width,height)=crate::wayland::init()?;
@@ -156,7 +159,9 @@ impl Drop for ScreenCapture {
/// Keep the display visible only while a peer is streaming; never unlock it.
pub fn keep_display_awake() -> Result<(), String> {
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Kms { return Ok(()); }
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal { return Ok(()); }
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Portal {
return crate::desktop_power::wake();
}
if crate::desktop_backend::detect()? == crate::desktop_backend::DesktopBackend::Wayland {
return crate::wayland::wake();
}
+212
View File
@@ -0,0 +1,212 @@
//! Real local SCTP peers; no cloud, desktop consent, audio or power operations.
use super::*;
use crate::protocol::{decode_wire, WireValue};
use std::{collections::HashMap, time::Duration};
use tokio::sync::mpsc;
type Messages = mpsc::UnboundedReceiver<(String, Vec<u8>)>;
struct Viewer {
pc: Arc<RTCPeerConnection>,
channels: HashMap<String, Arc<RTCDataChannel>>,
messages: Messages,
}
async fn connect(input: Arc<InputInjector>, desktop: bool,
portal: Option<Arc<crate::desktop_session::PortalDesktop>>, labels: &[&str]) -> (WebRtcSession, Viewer) {
let screen = Arc::new(ScreenCapture::unavailable("synthetic locked desktop".into()));
let host = WebRtcSession::new(screen, input, crate::signaling::SignalingClient::disconnected(),
desktop, crate::session_ice::SessionIce::local(), portal).await.unwrap();
let pc = Arc::new(APIBuilder::new().build().new_peer_connection(RTCConfiguration::default()).await.unwrap());
let (tx, messages) = mpsc::unbounded_channel();
let mut channels = HashMap::new();
for label in labels {
let channel = pc.create_data_channel(label, None).await.unwrap();
let (tx, name) = (tx.clone(), label.to_string());
channel.on_message(Box::new(move |message| {
let _ = tx.send((name.clone(), message.data.to_vec()));
Box::pin(async {})
}));
channels.insert(label.to_string(), channel);
}
let mut gathered = pc.gathering_complete_promise().await;
pc.set_local_description(pc.create_offer(None).await.unwrap()).await.unwrap();
gathered.recv().await;
host.pc.set_remote_description(pc.local_description().await.unwrap()).await.unwrap();
let mut gathered = host.pc.gathering_complete_promise().await;
host.pc.set_local_description(host.pc.create_answer(None).await.unwrap()).await.unwrap();
gathered.recv().await;
pc.set_remote_description(host.pc.local_description().await.unwrap()).await.unwrap();
tokio::time::timeout(Duration::from_secs(8), async {
while channels.values().any(|channel| channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open) {
tokio::time::sleep(Duration::from_millis(20)).await;
}
}).await.expect("local SCTP channels did not open");
(host, Viewer { pc, channels, messages })
}
fn body(message: &[u8], number: u32) -> Option<&[u8]> {
decode_wire(message).ok()?.into_iter().find_map(|field| match (field.number, field.value) {
(n, WireValue::Bytes(bytes)) if n == number => Some(bytes), _ => None,
})
}
fn bytes(number: u32, value: &[u8]) -> Vec<u8> {
fn varint(mut value: usize, out: &mut Vec<u8>) {
while value >= 128 { out.push((value as u8 & 127) | 128); value >>= 7; }
out.push(value as u8);
}
let mut out = Vec::new();
varint(((number << 3) | 2) as usize, &mut out);
varint(value.len(), &mut out);
out.extend_from_slice(value);
out
}
async fn receive(messages: &mut Messages, label: &str, field: u32) -> Vec<u8> {
tokio::time::timeout(Duration::from_secs(5), async {
loop {
let (channel, message) = messages.recv().await.expect("peer stopped responding");
if channel == label && body(&message, field).is_some() { return message; }
}
}).await.expect("expected protocol response timed out")
}
async fn send(viewer: &Viewer, label: &str, message: Vec<u8>) {
viewer.channels[label].send(&message.into()).await.unwrap();
}
fn mapping(kind: u8, payload: &[u8]) -> Vec<u8> {
let mut frame = vec![8, 1, 16, 2, 24, 3, 32, kind];
frame.extend(bytes(5, payload));
crate::initialization::envelope(1, 0, 27, &frame)
}
#[tokio::test]
async fn closing_unnegotiated_peers_releases_callback_state() {
for desktop in [false, true] {
let input = Arc::new(InputInjector::without_desktop());
let capture = Arc::new(ScreenCapture::unavailable("synthetic desktop".into()));
let peer = WebRtcSession::new(capture, input,
crate::signaling::SignalingClient::disconnected(), desktop,
crate::session_ice::SessionIce::local(), None).await.unwrap();
let weak = Arc::downgrade(&peer.pc);
peer.pc.close().await.unwrap();
drop(peer);
tokio::time::timeout(Duration::from_secs(4), async {
while weak.upgrade().is_some() {
tokio::time::sleep(Duration::from_millis(20)).await;
}
}).await.expect("closed peer retained by callbacks");
}
}
#[tokio::test]
async fn locked_control_unlock_requests_and_tools_work_without_a_video_frame() {
tokio::time::timeout(Duration::from_secs(35), async {
let input = Arc::new(InputInjector::without_desktop());
let (lock, locked) = tokio::sync::watch::channel(Some(true));
let portal = Arc::new(crate::desktop_session::PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic locked desktop".into())), locked));
let (desktop, mut viewer) = connect(input.clone(), true, Some(portal.clone()), &["CONTROL_DATA_CHANNEL"]).await;
// Official Android 4.43.0's unlock presenter requires CaptureChange
// (desktop/window) before it will use the secure state to send action15.
let selected = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 8).await;
let selected = decode_wire(body(&selected, 8).unwrap()).unwrap();
let capture_type = selected.iter().find_map(|f| match (f.number, &f.value) {
(1, WireValue::Varint(value)) => Some(*value), _ => None,
}).unwrap_or(0);
assert_eq!(capture_type, 0);
let state = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 15).await;
let secure = body(body(&state, 15).unwrap(), 1).unwrap();
assert!(decode_wire(secure).unwrap().iter().any(|f| f.number == 1 && f.value == WireValue::Varint(1)));
assert!(portal.snapshot().screen.init_error.is_some());
// A capture request waiting for unlock must not occupy CONTROL dispatch.
let mut capture = bytes(1, &[8, 31]);
capture.extend(bytes(2, &[16, 2, 144, 1, 60]));
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(10, 0, 21, &capture)).await;
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(91, 0, 3, &[8, 0])).await;
let echo = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 3).await;
let echo: serde_json::Value = serde_json::from_slice(body(body(&echo, 3).unwrap(), 2).unwrap()).unwrap();
assert_eq!(echo["seq"], 91);
send(&viewer, "CONTROL_DATA_CHANNEL",
crate::initialization::envelope(92, 0, 16, &[8, 1])).await;
let state = receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 15).await;
assert!(body(body(&state, 15).unwrap(), 1).is_some());
// Missing password exercises the real AUTO_UNLOCK_SYSTEM failure route
// without invoking PAM or unlocking the local user's desktop.
send(&viewer, "CONTROL_DATA_CHANNEL", crate::initialization::envelope(1, 0, 3, &[8, 15])).await;
receive(&mut viewer.messages, "CONTROL_DATA_CHANNEL", 12).await;
let (terminal, mut terminal_viewer) = connect(input.clone(), false, None,
&["CONTROL_DATA_CHANNEL", "BINARY_DATA_CHANNEL"]).await;
send(&terminal_viewer, "BINARY_DATA_CHANNEL",
crate::terminal::encode(crate::terminal::LIST_REQ, 71, &[])).await;
let frame = tokio::time::timeout(Duration::from_secs(5), async {
loop {
let (channel, message) = terminal_viewer.messages.recv().await.unwrap();
if channel == "BINARY_DATA_CHANNEL" { break crate::terminal::decode(&message).unwrap(); }
}
}).await.unwrap();
assert_eq!((frame.kind, frame.session), (crate::terminal::LIST_RESP, 71));
let (files, mut file_viewer) = connect(input.clone(), false, None,
&["CONTROL_DATA_CHANNEL", "FILE_DATA_CHANNEL"]).await;
let directory = std::env::temp_dir().join(format!("uu-control-empty-{}-{}", std::process::id(), rand::random::<u64>()));
std::fs::create_dir(&directory).unwrap();
let mut read = bytes(1, &[8, 7]);
read.extend(bytes(2, directory.to_str().unwrap().as_bytes()));
let mut request = bytes(1, &[8, 99]);
request.extend(bytes(8, &bytes(10, &read)));
let query = crate::initialization::envelope(5, 0, 21, &request);
send(&file_viewer, "CONTROL_DATA_CHANNEL", query.clone()).await;
let response = receive(&mut file_viewer.messages, "FILE_DATA_CHANNEL", 22).await;
let listing = body(body(body(&response, 22).unwrap(), 4).unwrap(), 7).unwrap();
assert!(decode_wire(listing).unwrap().iter().any(|f| f.number == 4 && f.value == WireValue::Varint(1)));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let echo = tokio::spawn(async move {
let (socket, _) = listener.accept().await.unwrap();
let (mut read, mut write) = socket.into_split();
tokio::io::copy(&mut read, &mut write).await.unwrap();
});
let (ports, mut port_viewer) = connect(input, false, None,
&["CONTROL_DATA_CHANNEL", "FILE_DATA_CHANNEL"]).await;
let target = serde_json::json!({"version":1,"target_host":"127.0.0.1","target_port":port}).to_string();
send(&port_viewer, "FILE_DATA_CHANNEL", mapping(0, target.as_bytes())).await;
let ack = receive(&mut port_viewer.messages, "FILE_DATA_CHANNEL", 27).await;
let ack: serde_json::Value = serde_json::from_slice(body(body(&ack, 27).unwrap(), 5).unwrap()).unwrap();
assert_eq!(ack["ok"], true);
let mut sessions = HashMap::from([
("desktop".into(), desktop), ("terminal".into(), terminal), ("files".into(), files),
("ports".into(), ports),
]);
assert!(crate::superseded_peer_ids(&sessions, "another-tool", false).is_empty());
assert_eq!(crate::superseded_peer_ids(&sessions, "next-desktop", true), ["desktop"]);
for id in crate::superseded_peer_ids(&sessions, "next-desktop", true) {
sessions.remove(&id).unwrap().pc.close().await.unwrap();
}
assert_eq!(sessions["terminal"].pc.connection_state(), RTCPeerConnectionState::Connected);
assert_eq!(sessions["files"].pc.connection_state(), RTCPeerConnectionState::Connected);
// A desktop close must not cancel the independent file-transfer responder.
send(&file_viewer, "CONTROL_DATA_CHANNEL", query).await;
receive(&mut file_viewer.messages, "FILE_DATA_CHANNEL", 22).await;
send(&port_viewer, "FILE_DATA_CHANNEL", mapping(2, b"local echo")).await;
let echoed = receive(&mut port_viewer.messages, "FILE_DATA_CHANNEL", 27).await;
assert_eq!(body(body(&echoed, 27).unwrap(), 5), Some(b"local echo".as_slice()));
send(&terminal_viewer, "BINARY_DATA_CHANNEL",
crate::terminal::encode(crate::terminal::LIST_REQ, 72, &[])).await;
let frame = tokio::time::timeout(Duration::from_secs(5), async {
loop {
let (channel, message) = terminal_viewer.messages.recv().await.unwrap();
if channel == "BINARY_DATA_CHANNEL" { break crate::terminal::decode(&message).unwrap(); }
}
}).await.unwrap();
assert_eq!((frame.kind, frame.session), (crate::terminal::LIST_RESP, 72));
std::fs::remove_dir(directory).unwrap();
for (_, peer) in sessions { peer.pc.close().await.unwrap(); }
viewer.pc.close().await.unwrap();
terminal_viewer.pc.close().await.unwrap();
file_viewer.pc.close().await.unwrap();
port_viewer.pc.close().await.unwrap();
echo.abort();
drop(lock);
}).await.expect("locked bootstrap/tool regression exceeded deadline");
}
+101
View File
@@ -0,0 +1,101 @@
//! Small GNOME-only display wake and idle-inhibition bridge.
#[cfg(all(target_os = "linux", feature = "portal"))]
mod native {
use std::{ffi::c_void, ptr::NonNull};
extern "C" {
fn uu_desktop_power_wake() -> i32;
fn uu_desktop_idle_inhibitor_acquire(error: *mut i32) -> *mut c_void;
fn uu_desktop_idle_inhibitor_release(inhibitor: *mut c_void);
}
pub struct Handle(NonNull<c_void>);
// GDBusConnection is thread-safe; the C handle is only used by its single Drop call.
unsafe impl Send for Handle {}
impl Handle {
pub fn acquire() -> Result<Self, String> {
let mut error = 0;
let pointer = unsafe { uu_desktop_idle_inhibitor_acquire(&mut error) };
NonNull::new(pointer).map(Self).ok_or_else(|| super::error("idle inhibition", error))
}
pub fn wake() -> Result<(), String> {
super::check("display wake", unsafe { uu_desktop_power_wake() })
}
}
impl Drop for Handle {
fn drop(&mut self) {
unsafe { uu_desktop_idle_inhibitor_release(self.0.as_ptr()) }
}
}
}
/// Set GNOME's display power-save mode to on. This does not unlock the session or change
/// lock-screen policy. Session-bus setup and each method call are bounded to three seconds.
pub fn wake() -> Result<(), String> {
if !gnome_portal_active() {
return Ok(());
}
#[cfg(all(target_os = "linux", feature = "portal"))]
{
native::Handle::wake()
}
#[cfg(not(all(target_os = "linux", feature = "portal")))]
{
Ok(())
}
}
/// Holds a GNOME SessionManager idle-only inhibitor until dropped.
///
/// Acquire and drop perform blocking D-Bus work. Session-bus setup and each method call are
/// bounded to three seconds. Callers on an async runtime should move both operations to a
/// blocking worker.
pub struct IdleInhibitor {
#[cfg(all(target_os = "linux", feature = "portal"))]
_handle: Option<native::Handle>,
}
impl IdleInhibitor {
pub fn acquire() -> Result<Self, String> {
if !gnome_portal_active() {
return Ok(Self {
#[cfg(all(target_os = "linux", feature = "portal"))]
_handle: None,
});
}
#[cfg(all(target_os = "linux", feature = "portal"))]
{
native::Handle::acquire().map(|handle| Self { _handle: Some(handle) })
}
#[cfg(not(all(target_os = "linux", feature = "portal")))]
{
Ok(Self {})
}
}
}
fn gnome_portal_active() -> bool {
let gnome = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default().split(':')
.any(|desktop| desktop.eq_ignore_ascii_case("gnome"));
gnome && crate::desktop_backend::detect().is_ok_and(|backend| backend == crate::desktop_backend::DesktopBackend::Portal)
}
#[cfg(all(target_os = "linux", feature = "portal"))]
fn check(operation: &str, code: i32) -> Result<(), String> {
if code == 0 { Ok(()) } else { Err(error(operation, code)) }
}
#[cfg(all(target_os = "linux", feature = "portal"))]
fn error(operation: &str, code: i32) -> String {
let reason = match code {
1 => "session D-Bus unavailable",
2 => "GNOME D-Bus operation failed",
_ => "GNOME D-Bus operation failed",
};
format!("{operation}: {reason} (code {code})")
}
+567
View File
@@ -0,0 +1,567 @@
//! Portal availability is independent of the authenticated CONTROL transport.
use std::future::Future;
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
use std::time::Instant;
use tokio::sync::{watch, Mutex};
use crate::{capture::ScreenCapture, input::DesktopInputSession};
use crate::portal::PortalError;
use webrtc::{data_channel::{RTCDataChannel, data_channel_state::RTCDataChannelState},
peer_connection::{RTCPeerConnection, peer_connection_state::RTCPeerConnectionState}};
#[derive(Clone)]
pub struct State {
pub screen: Arc<ScreenCapture>,
pub waiting: bool,
}
impl State {
pub fn permission(&self, desktop: bool) -> crate::system_metrics::Permission {
use crate::system_metrics::Permission;
if !desktop { Permission::Allow }
else if self.waiting { Permission::Unknown }
else if self.screen.init_error.is_none() { Permission::Allow }
else { Permission::Reject }
}
/// Capture exists and is not waiting for a replacement of a closed Portal session.
pub fn ready(&self) -> bool { self.screen.init_error.is_none() && !self.waiting }
}
pub struct PortalDesktop {
state: watch::Sender<State>,
opening: Arc<Mutex<()>>,
locked: watch::Receiver<Option<bool>>,
}
struct CancelOnDrop(Arc<AtomicBool>);
impl Drop for CancelOnDrop {
fn drop(&mut self) { self.0.store(true, Ordering::Release); }
}
enum Inhibitor {
Real(crate::desktop_power::IdleInhibitor),
#[cfg(test)]
Fake(DropSignal),
}
#[cfg(test)]
struct DropSignal(Arc<std::sync::atomic::AtomicUsize>);
#[cfg(test)]
impl Drop for DropSignal {
fn drop(&mut self) { self.0.fetch_add(1, Ordering::Relaxed); }
}
struct AttemptPrep {
wake: Result<(), String>,
inhibitor: IdleGuard,
}
#[derive(Default)]
struct IdleGuard(Option<Inhibitor>);
impl Drop for IdleGuard {
fn drop(&mut self) {
match self.0.take() {
Some(Inhibitor::Real(guard)) => { tokio::task::spawn_blocking(move || drop(guard)); }
#[cfg(test)]
Some(Inhibitor::Fake(signal)) => drop(signal),
None => {}
}
}
}
enum AttemptOutcome {
Ready(Arc<ScreenCapture>),
Retry(String),
Terminal(String),
Cancelled,
}
fn classify(result: Result<Arc<ScreenCapture>, PortalError>) -> AttemptOutcome {
match result {
Ok(screen) => AttemptOutcome::Ready(screen),
Err(error) if error.code == 13 => AttemptOutcome::Cancelled,
// 4/5/14 are fatal session/geometry states; 6/8 are unsupported
// consent/format shapes which must not loop prompts forever.
Err(error) if matches!(error.code, 4 | 5 | 6 | 8 | 14) => AttemptOutcome::Terminal(error.to_string()),
Err(error) => AttemptOutcome::Retry(error.to_string()),
}
}
fn should_prepare_power(healthy: bool, force_wake: bool) -> bool {
!healthy || force_wake
}
fn prepare_attempt<W, I>(
cancelled: &AtomicBool,
owner: &DesktopInputSession,
locked: &watch::Receiver<Option<bool>>,
wake: W,
inhibit: I,
) -> Option<AttemptPrep>
where
W: FnOnce() -> Result<(), String>,
I: FnOnce() -> IdleGuard,
{
let current = || !cancelled.load(Ordering::Acquire)
&& owner.is_current()
&& *locked.borrow() == Some(false);
if !current() { return None; }
let wake = wake();
if !current() { return None; }
let inhibitor = inhibit();
if !current() { return None; }
Some(AttemptPrep { wake, inhibitor })
}
impl PortalDesktop {
pub fn new(screen: Arc<ScreenCapture>) -> Self {
Self { state: watch::channel(State { waiting: screen.init_error.is_some(), screen }).0,
opening: Arc::new(Mutex::new(())), locked: crate::session_lock::watch_locked() }
}
pub fn begin(&self) {
// GNOME closes the Portal session on lock; a cached screen with a dead session
// is not ready (no Allow, no stale video path) until maintain() rebuilds it.
self.state.send_modify(|state| state.waiting = state.screen.init_error.is_some()
|| state.screen.portal.as_ref().is_some_and(|portal| !portal.is_alive()));
}
pub fn snapshot(&self) -> State { self.state.borrow().clone() }
pub fn subscribe(&self) -> watch::Receiver<State> { self.state.subscribe() }
pub fn lock_state(&self) -> watch::Receiver<Option<bool>> { self.locked.clone() }
#[cfg(test)]
pub fn with_lock_state(screen: Arc<ScreenCapture>, locked: watch::Receiver<Option<bool>>) -> Self {
Self { state: watch::channel(State { waiting: screen.init_error.is_some(), screen }).0,
opening: Arc::new(Mutex::new(())), locked }
}
#[cfg(test)]
pub fn publish_test_screen(&self, screen: Arc<ScreenCapture>) {
self.state.send_replace(State { screen, waiting: false });
}
fn publish(&self, owner: &DesktopInputSession, screen: Arc<ScreenCapture>) {
owner.with_current(|| {
if screen.init_error.is_some() { owner.release_input(); }
owner.bind_portal(screen.portal.clone());
self.state.send_replace(State { screen, waiting: false });
});
}
fn pause(&self, owner: &DesktopInputSession) {
let screen = self.snapshot().screen;
owner.with_current(|| {
owner.release_input();
if let Some(portal) = &screen.portal { portal.pause(); }
});
}
pub async fn run(self: Arc<Self>, pc: Arc<RTCPeerConnection>, channel: Arc<RTCDataChannel>,
owner: Arc<DesktopInputSession>) {
self.run_until(owner, closed(&pc, &channel)).await;
}
pub async fn run_until<F>(self: Arc<Self>, owner: Arc<DesktopInputSession>, wait_closed: F)
where F: Future<Output = ()> {
tokio::select! {
result = self.maintain(owner.clone()) => {
if let Err(error) = result {
log::warn!("Portal desktop unavailable: {error}");
self.publish(&owner, Arc::new(ScreenCapture::unavailable(error)));
}
},
_ = wait_closed => {},
}
}
async fn maintain(&self, owner: Arc<DesktopInputSession>) -> Result<(), String> {
self.maintain_with(owner,
crate::desktop_power::wake,
|| match crate::desktop_power::IdleInhibitor::acquire() {
Ok(guard) => IdleGuard(Some(Inhibitor::Real(guard))),
Err(error) => { log::warn!("Desktop idle inhibition unavailable: {error}"); IdleGuard::default() }
},
|screen, cancel| {
if let Some(portal) = &screen.portal {
portal.recover(&cancel)?;
Ok(screen)
} else {
ScreenCapture::open_portal(&cancel).map(Arc::new)
}
}).await
}
async fn maintain_with<W, I, O>(&self, owner: Arc<DesktopInputSession>, wake: W, inhibit: I, open: O) -> Result<(), String>
where
W: Fn() -> Result<(), String> + Send + Sync + 'static,
I: Fn() -> IdleGuard + Send + Sync + 'static,
O: Fn(Arc<ScreenCapture>, Arc<AtomicBool>) -> Result<Arc<ScreenCapture>, PortalError> + Send + Sync + 'static,
{
let wake = Arc::new(wake);
let inhibit = Arc::new(inhibit);
let open = Arc::new(open);
let mut locked = self.locked.clone();
let mut previous = None;
let mut retry_at = Instant::now();
loop {
if !owner.is_current() { return Ok(()); }
let state = *locked.borrow_and_update();
if state != Some(false) {
if previous == Some(false) { self.pause(&owner); }
} else {
// A fresh unlocked transition gets an immediate wake/recovery attempt.
let force_wake = previous != Some(false);
if force_wake { retry_at = Instant::now(); }
let mut attempt_locked = locked.clone();
let outcome = tokio::select! {
biased;
_ = wait_for_lock(&mut locked) => AttemptOutcome::Cancelled,
outcome = self.portal_attempt(&owner, &mut attempt_locked, retry_at, force_wake, wake.clone(), inhibit.clone(), open.clone()) => outcome,
};
match outcome {
AttemptOutcome::Ready(screen) => self.publish(&owner, screen),
AttemptOutcome::Terminal(error) => return Err(error),
AttemptOutcome::Retry(error) => {
log::warn!("Portal desktop recovery deferred: {error}");
retry_at = Instant::now() + std::time::Duration::from_secs(5);
}
AttemptOutcome::Cancelled => {}
}
if *locked.borrow_and_update() != Some(false) { self.pause(&owner); }
}
previous = *locked.borrow_and_update();
tokio::select! {
_ = locked.changed() => {},
_ = tokio::time::sleep(std::time::Duration::from_millis(200)) => {},
}
}
}
async fn portal_attempt<W, I, O>(&self, owner: &Arc<DesktopInputSession>,
locked: &mut watch::Receiver<Option<bool>>, retry_at: Instant, force_wake: bool,
wake: Arc<W>, inhibit: Arc<I>, open: Arc<O>) -> AttemptOutcome
where
W: Fn() -> Result<(), String> + Send + Sync + 'static,
I: Fn() -> IdleGuard + Send + Sync + 'static,
O: Fn(Arc<ScreenCapture>, Arc<AtomicBool>) -> Result<Arc<ScreenCapture>, PortalError> + Send + Sync + 'static,
{
if !owner.is_current() || *locked.borrow() != Some(false) || Instant::now() < retry_at {
return AttemptOutcome::Cancelled;
}
let gate = self.opening.clone().lock_owned().await;
// The state can change while this attempt waits for an earlier worker
// to finish rotating the Portal permission token.
if !owner.is_current() || *locked.borrow() != Some(false) {
return AttemptOutcome::Cancelled;
}
let screen = self.snapshot().screen;
let healthy = screen.portal.as_ref().is_some_and(|portal| portal.is_alive());
if !should_prepare_power(healthy, force_wake) {
return AttemptOutcome::Cancelled;
}
let cancel = Arc::new(AtomicBool::new(false));
let _cancel = CancelOnDrop(cancel.clone());
let prep_cancel = cancel.clone();
let prep_owner = owner.clone();
let prep_locked = locked.clone();
let prep = match tokio::task::spawn_blocking(move || prepare_attempt(
&prep_cancel,
&prep_owner,
&prep_locked,
|| (*wake)(),
|| (*inhibit)(),
)).await {
Ok(Some(prep)) => prep,
Ok(None) => return AttemptOutcome::Cancelled,
Err(_) => return AttemptOutcome::Terminal("Desktop power worker failed".into()),
};
if let Err(error) = prep.wake { log::warn!("Portal screen wake failed: {error}"); }
// Held by the cancellable async helper, not the blocking worker, so a
// lock transition releases it while worker cleanup may still be running.
let _inhibitor = prep.inhibitor;
if !owner.is_current() || *locked.borrow() != Some(false) {
return AttemptOutcome::Cancelled;
}
// A new controller can arrive while the cached Portal handle is alive
// but the display has gone to sleep without a lock transition.
if healthy { return AttemptOutcome::Ready(screen); }
let worker_cancel = cancel.clone();
let result = tokio::task::spawn_blocking(move || {
// The worker owns the gate until its real cleanup completes; a
// cancelled helper only signals it and cannot start an overlap.
let _gate = gate;
if worker_cancel.load(Ordering::Acquire) {
return Err(PortalError { code: 13 });
}
open(screen, worker_cancel)
}).await;
if !owner.is_current() || *locked.borrow() != Some(false) {
return AttemptOutcome::Cancelled;
}
match result {
Ok(result) => classify(result),
Err(_) => AttemptOutcome::Terminal("Portal initialization worker failed".into()),
}
}
}
async fn wait_for_lock(locked: &mut watch::Receiver<Option<bool>>) {
while locked.changed().await.is_ok() {
if *locked.borrow_and_update() != Some(false) { return; }
}
}
pub async fn closed(pc: &RTCPeerConnection, channel: &RTCDataChannel) {
while !matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
&& channel.ready_state() == RTCDataChannelState::Open {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
}
pub async fn wait_ready(state: &mut watch::Receiver<State>, channel: &RTCDataChannel) -> Option<Arc<ScreenCapture>> {
loop {
let current = state.borrow_and_update().clone();
if current.ready() { return Some(current.screen); }
if !current.waiting || channel.ready_state() != RTCDataChannelState::Open { return None; }
tokio::select! {
changed = state.changed() => if changed.is_err() { return None; },
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::AtomicUsize;
fn host() -> PortalDesktop {
let (_, locked) = watch::channel(Some(true));
PortalDesktop::with_lock_state(Arc::new(ScreenCapture::unavailable("pending".into())), locked)
}
fn ready_screen() -> Arc<ScreenCapture> {
let mut screen = ScreenCapture::unavailable("synthetic".into());
screen.backend = crate::desktop_backend::DesktopBackend::Portal;
screen.init_error = None;
screen.width = 1920;
screen.height = 1080;
Arc::new(screen)
}
#[test]
fn cached_screen_waiting_for_a_closed_portal_is_not_ready_or_allowed() {
use crate::system_metrics::Permission;
let live = State { screen: ready_screen(), waiting: false };
assert!(live.ready() && live.permission(true) == Permission::Allow);
let stale = State { screen: ready_screen(), waiting: true };
assert!(!stale.ready());
assert!(stale.permission(true) == Permission::Unknown);
assert!(stale.permission(false) == Permission::Allow);
}
#[test]
fn recovered_screen_is_retained_across_transient_disconnect() {
let host = host();
let mut updates = host.subscribe();
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
owner.connected();owner.disconnected();
let screen = ready_screen();
host.publish(&owner, screen.clone());
assert!(updates.has_changed().unwrap());
assert!(!updates.borrow_and_update().waiting);
assert!(Arc::ptr_eq(&host.snapshot().screen, &screen));
assert!(!host.snapshot().waiting);
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Allow);
owner.with_input(|| panic!("publication must not reactivate input"));
owner.connected();
assert!(Arc::ptr_eq(&host.snapshot().screen, &screen));
}
#[test]
fn terminal_failure_publishes_rejection_while_disconnected() {
let host = host();
let mut updates = host.subscribe();
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
owner.connected();owner.disconnected();
host.publish(&owner, Arc::new(ScreenCapture::unavailable("consent rejected".into())));
assert!(updates.has_changed().unwrap());
assert!(!updates.borrow_and_update().waiting);
assert!(!host.snapshot().waiting);
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Reject);
owner.connected();
assert!(!host.snapshot().waiting);
assert_eq!(host.snapshot().screen.init_error.as_deref(), Some("consent rejected"));
}
#[test]
fn replaced_peer_cannot_publish_success_or_failure() {
let host = host();
let input = Arc::new(crate::input::InputInjector::without_desktop());
let old = input.desktop_session();
let current = input.desktop_session();
let screen = ready_screen();
host.publish(&current, screen.clone());
host.publish(&old, Arc::new(ScreenCapture::unavailable("late failure".into())));
host.publish(&old, ready_screen());
assert!(Arc::ptr_eq(&host.snapshot().screen, &screen));
assert!(!host.snapshot().waiting);
}
#[test]
fn unavailable_desktop_does_not_reject_independent_tools() {
let host = PortalDesktop::new(Arc::new(ScreenCapture::unavailable("not authorized".into())));
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Unknown);
assert_eq!(host.snapshot().permission(false), crate::system_metrics::Permission::Allow);
host.state.send_modify(|state| state.waiting = false);
assert_eq!(host.snapshot().permission(true), crate::system_metrics::Permission::Reject);
assert_eq!(host.snapshot().permission(false), crate::system_metrics::Permission::Allow);
host.begin();
assert!(host.snapshot().waiting);
}
#[test]
fn unsupported_portal_shapes_are_terminal_while_transient_codes_retry() {
for code in [4, 5, 6, 8, 14] {
assert!(matches!(classify(Err(PortalError { code })), AttemptOutcome::Terminal(_)), "code {code}");
}
for code in [1, 3, 7, 9, 10] {
assert!(matches!(classify(Err(PortalError { code })), AttemptOutcome::Retry(_)), "code {code}");
}
assert!(matches!(classify(Err(PortalError { code: 13 })), AttemptOutcome::Cancelled));
}
#[test]
fn fresh_unlock_wakes_even_when_cached_portal_is_healthy() {
assert!(should_prepare_power(true, true));
assert!(should_prepare_power(false, false));
assert!(!should_prepare_power(true, false));
}
#[test]
fn queued_power_prep_checks_cancel_lock_and_current_owner_before_wake() {
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
let (lock, locked) = watch::channel(Some(false));
let wakes = AtomicUsize::new(0);
let inhibitors = AtomicUsize::new(0);
let cancelled = AtomicBool::new(true);
assert!(prepare_attempt(&cancelled, &owner, &locked,
|| { wakes.fetch_add(1, Ordering::Relaxed); Ok(()) },
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
assert_eq!(wakes.load(Ordering::Relaxed), 0);
cancelled.store(false, Ordering::Release);
lock.send(Some(true)).unwrap();
assert!(prepare_attempt(&cancelled, &owner, &locked,
|| { wakes.fetch_add(1, Ordering::Relaxed); Ok(()) },
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
assert_eq!(wakes.load(Ordering::Relaxed), 0);
lock.send(Some(false)).unwrap();
let locked_during_wake = lock.clone();
assert!(prepare_attempt(&cancelled, &owner, &locked,
|| {
wakes.fetch_add(1, Ordering::Relaxed);
locked_during_wake.send(Some(true)).unwrap();
Ok(())
},
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
assert_eq!(wakes.load(Ordering::Relaxed), 1);
assert_eq!(inhibitors.load(Ordering::Relaxed), 0);
lock.send(Some(false)).unwrap();
let replacement = input.desktop_session();
assert!(prepare_attempt(&cancelled, &owner, &locked,
|| { wakes.fetch_add(1, Ordering::Relaxed); Ok(()) },
|| { inhibitors.fetch_add(1, Ordering::Relaxed); IdleGuard::default() }).is_none());
assert_eq!(wakes.load(Ordering::Relaxed), 1);
assert!(replacement.is_current());
assert_eq!(inhibitors.load(Ordering::Relaxed), 0);
}
#[test]
fn cancelling_a_peer_signals_its_blocking_permission_worker() {
let flag = Arc::new(AtomicBool::new(false));
let guard = CancelOnDrop(flag.clone());
assert!(!flag.load(Ordering::Acquire));
drop(guard);
assert!(flag.load(Ordering::Acquire));
}
#[tokio::test]
async fn lock_transition_cancels_blocked_attempt_and_releases_inhibitor() {
let (lock, locked) = watch::channel(Some(false));
let host = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("pending".into())), locked));
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
let releases = Arc::new(AtomicUsize::new(0));
let wakes = Arc::new(AtomicUsize::new(0));
let (started, mut started_rx) = tokio::sync::mpsc::unbounded_channel();
let task = tokio::spawn({
let host = host.clone();
let owner = owner.clone();
let releases = releases.clone();
let wakes = wakes.clone();
async move {
host.maintain_with(owner,
move || {
wakes.fetch_add(1, Ordering::Relaxed);
Ok(())
},
move || IdleGuard(Some(Inhibitor::Fake(DropSignal(releases.clone())))),
move |_screen, cancel| {
let _ = started.send(());
while !cancel.load(Ordering::Acquire) {
std::thread::sleep(std::time::Duration::from_millis(1));
}
Err(PortalError { code: 13 })
}).await
}
});
started_rx.recv().await.unwrap();
lock.send(Some(true)).unwrap();
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
while releases.load(Ordering::Relaxed) == 0 {
assert!(tokio::time::Instant::now() < deadline, "cancelled attempt did not release inhibitor");
tokio::task::yield_now().await;
}
assert_eq!(wakes.load(Ordering::Relaxed), 1);
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
while host.opening.try_lock().is_err() {
assert!(tokio::time::Instant::now() < deadline, "cancelled worker did not finish");
tokio::task::yield_now().await;
}
task.abort();
let _ = task.await;
}
#[tokio::test]
async fn inhibitor_release_does_not_wait_for_blocked_worker_cleanup() {
let (lock, locked) = watch::channel(Some(false));
let host = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("pending".into())), locked));
let input = Arc::new(crate::input::InputInjector::without_desktop());
let owner = input.desktop_session();
let releases = Arc::new(AtomicUsize::new(0));
let worker_release = Arc::new(AtomicBool::new(false));
let (started, mut started_rx) = tokio::sync::mpsc::unbounded_channel();
let task = tokio::spawn({
let host = host.clone();
let owner = owner.clone();
let releases = releases.clone();
let worker_release = worker_release.clone();
async move {
host.maintain_with(owner,
|| Ok(()),
move || IdleGuard(Some(Inhibitor::Fake(DropSignal(releases.clone())))),
move |screen, _cancel| {
let _ = started.send(());
while !worker_release.load(Ordering::Acquire) {
std::thread::sleep(std::time::Duration::from_millis(1));
}
Ok(screen)
}).await
}
});
started_rx.recv().await.unwrap();
lock.send(Some(true)).unwrap();
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
while releases.load(Ordering::Relaxed) == 0 {
assert!(tokio::time::Instant::now() < deadline, "cancelled attempt did not release inhibitor");
tokio::task::yield_now().await;
}
assert!(host.opening.try_lock().is_err(), "worker cleanup must still own the gate");
worker_release.store(true, Ordering::Release);
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
loop {
if host.opening.try_lock().is_ok() { break; }
assert!(tokio::time::Instant::now() < deadline, "worker cleanup did not release the gate");
tokio::task::yield_now().await;
}
task.abort();
let _ = task.await;
}
}
+2 -1
View File
@@ -128,7 +128,8 @@ pub fn encode_screen_sources(seq: u64, timestamp: u64, screens: &[Screen<'_>], c
}
/// Message.which field 8 CaptureChange. Enum: desktop=0, window=1,
/// MuMu=2, hook=3, none=99. Caller confirms the active capture before emitting.
/// MuMu=2, hook=3, none=99. Declares the selected target, not frame readiness:
/// Android's unlock presenter requires desktop/window here even while locked.
pub fn encode_capture_change(seq: u64, timestamp: u64, capture_type: i32, type_value: i32, description: &str) -> Result<Vec<u8>, String> {
if !matches!(capture_type, 0 | 1 | 2 | 3 | 99) {
return Err("Unknown CaptureChange type".into());
+67
View File
@@ -265,6 +265,8 @@ impl InputInjector {
unsafe {x11_release_all();}
} else if self.backend==crate::desktop_backend::DesktopBackend::Wayland && self.portal.lock().unwrap().is_none() {
crate::wayland::release_all();
} else if self.backend==crate::desktop_backend::DesktopBackend::Portal && self.portal.lock().unwrap().is_none() {
// Nothing is bound yet (e.g. the first locked connection): no input can be held.
} else {
// Portal/Wayland cannot query global state. Sweep every key/button
// that this host can emit through the currently bound session.
@@ -445,6 +447,15 @@ impl InputInjector {
}
}
impl DesktopInputSession {
pub fn is_current(&self) -> bool {
let owner = self.input.owner.lock().unwrap();
owner.owns(self.id)
}
/// Call only inside with_current or with_input, so a superseded peer cannot change bindings.
pub fn bind_portal(&self, portal: Option<std::sync::Arc<crate::portal::PortalSession>>) {
self.input.bind_portal(portal);
}
pub fn release_input(&self) { self.input.release_held(); }
pub fn connected(&self) {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && !owner.active {self.input.release_held();owner.active=true;}
@@ -453,6 +464,11 @@ impl DesktopInputSession {
let mut owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {owner.active=false;self.input.release_held();}
}
/// Lifecycle updates survive a transient disconnect, but serialize with replacement and shutdown.
pub fn with_current(&self, action:impl FnOnce()) {
let owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) {action();}
}
pub fn with_input(&self, action:impl FnOnce()) {
let owner=self.input.owner.lock().unwrap();
if owner.owns(self.id) && owner.active {action();}
@@ -460,11 +476,62 @@ impl DesktopInputSession {
}
impl Drop for DesktopInputSession {fn drop(&mut self) {self.disconnected();}}
#[cfg(test)]
impl InputInjector {
pub fn without_desktop() -> Self {
Self { mac_controller:std::sync::atomic::AtomicBool::new(false),
cmd:std::sync::Mutex::new(CmdState::default()), serial:std::sync::Mutex::new(()),
owner:std::sync::Mutex::new(DesktopOwner::default()), shortcuts:std::sync::Mutex::new(Shortcuts::default()),
uinput:None, screen:std::sync::Mutex::new((0,0)), portal_failures:std::sync::atomic::AtomicUsize::new(0),
portal_outcomes:std::array::from_fn(|_|std::sync::atomic::AtomicUsize::new(0)),
portal:std::sync::Mutex::new(None), backend:crate::desktop_backend::DesktopBackend::Portal,
pending:std::sync::Mutex::new(PendingPointer::default()), held:std::sync::Mutex::new(HeldInput::default()) }
}
}
#[cfg(test)]
mod lifecycle_tests {
use super::*;
use std::sync::{Arc,atomic::{AtomicUsize,Ordering}};
#[test]
fn current_owner_can_publish_while_disconnected_but_cannot_inject() {
let input=Arc::new(InputInjector::without_desktop());
let session=input.desktop_session();
session.connected();session.disconnected();
let publications=AtomicUsize::new(0);
session.with_current(||{publications.fetch_add(1,Ordering::Relaxed);});
session.with_input(||panic!("disconnected owner must not inject"));
assert_eq!(publications.load(Ordering::Relaxed),1);
session.connected();
session.with_input(||{publications.fetch_add(1,Ordering::Relaxed);});
let next=input.desktop_session();
session.with_current(||panic!("replaced owner must not publish"));
next.with_current(||{publications.fetch_add(1,Ordering::Relaxed);});
input.shutdown();
next.with_current(||panic!("shutdown must reject lifecycle updates"));
assert_eq!(publications.load(Ordering::Relaxed),3);
}
#[test]
fn current_owner_gate_holds_ownership_through_publication() {
let input=Arc::new(InputInjector::without_desktop());
let session=input.desktop_session();
let (started,ready)=std::sync::mpsc::channel();
let (finish,resume)=std::sync::mpsc::channel();
let worker=session.clone();
let publication=std::thread::spawn(move ||worker.with_current(|| {
started.send(()).unwrap();resume.recv().unwrap();
}));
ready.recv_timeout(std::time::Duration::from_secs(2)).unwrap();
assert!(input.owner.try_lock().is_err());
let replacing=input.clone();
let replacement=std::thread::spawn(move ||replacing.desktop_session());
finish.send(()).unwrap();
publication.join().unwrap();
let next=replacement.join().unwrap();
assert!(next.is_current());
session.with_current(||panic!("late publication after replacement"));
}
#[test]
fn cleanup_discards_motion_shortcuts_and_untracked_input_ledger() {
let input=Arc::new(InputInjector::new());
input.held.lock().unwrap().keys.insert(0xffe1);
+101
View File
@@ -0,0 +1,101 @@
//! Black video keeps locked Portal initialization reachable by the iPhone viewer.
use std::time::Duration;
use bytes::Bytes;
use webrtc::{media::Sample, peer_connection::{RTCPeerConnection, peer_connection_state::RTCPeerConnectionState}};
use crate::{capture_setting::VideoCodec, desktop_session::PortalDesktop, video_track::VideoTrack};
async fn black_frame(codec: VideoCodec) -> Result<Bytes, String> {
let mut command = crate::video::ffmpeg_command();
command.args(["-hide_banner", "-loglevel", "error", "-nostdin",
"-f", "lavfi", "-i", "color=c=black:s=1280x720:r=1", "-frames:v", "1",
"-an", "-pix_fmt", "yuv420p", "-threads", "1", "-preset", "ultrafast", "-tune", "zerolatency"]);
match codec {
VideoCodec::H264 => { command.args(["-c:v", "libx264", "-profile:v", "baseline",
"-x264-params", "keyint=1:repeat-headers=1:aud=1", "-f", "h264"]); },
VideoCodec::H265 => { command.args(["-c:v", "libx265",
"-x265-params", "keyint=1:repeat-headers=1:aud=1:pools=1:frame-threads=1:log-level=error", "-f", "hevc"]); },
}
command.arg("pipe:1").kill_on_drop(true);
let output = tokio::time::timeout(Duration::from_secs(5), command.output()).await
.map_err(|_| "Locked desktop video encoder timed out".to_string())?
.map_err(|error| format!("Locked desktop video encoder: {error}"))?;
if !output.status.success() || output.stdout.is_empty() {
return Err(format!("Locked desktop video encoder failed: status={}", output.status));
}
Ok(output.stdout.into())
}
pub async fn run(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop) -> Result<(), String> {
run_with_encoder(pc, track, desktop, black_frame).await
}
async fn run_with_encoder<F, Fut>(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop, mut encode: F) -> Result<(), String>
where F: FnMut(VideoCodec) -> Fut, Fut: std::future::Future<Output = Result<Bytes, String>> {
// #29: iPhone 4.42.0 cold ON/OFF/ON tests only sent action15 after black video.
// Keep CONTROL permissions truthful and use the existing video track until real capture is ready.
let mut updates = desktop.subscribe();
let mut locked = desktop.lock_state();
let mut tick = tokio::time::interval(Duration::from_secs(1));
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut frame = None;
let mut sent = 0u64;
let mut attempts = 0u64;
loop {
let state = updates.borrow_and_update().clone();
let _ = locked.borrow_and_update();
if state.ready() || !state.waiting || crate::video::transport_closed(pc) {
if attempts > 0 { log::info!("Locked desktop video ended: sent={sent} attempts={attempts}"); }
return Ok(());
}
tokio::select! {
biased;
changed = updates.changed() => if changed.is_err() { return Ok(()); },
changed = locked.changed() => if changed.is_err() { return Ok(()); },
_ = tick.tick() => {
if !updates.borrow().waiting || *locked.borrow() != Some(true)
|| pc.connection_state() != RTCPeerConnectionState::Connected { continue; }
if frame.is_none() {
let codec = crate::capture_setting::preferred_codec(
track.supports(VideoCodec::H264), track.supports(VideoCodec::H265));
let encoded = tokio::select! {
biased;
_ = updates.changed() => continue,
_ = locked.changed() => continue,
result = encode(codec) => result?,
};
frame = Some((codec, encoded));
}
if crate::video::transport_closed(pc) { return Ok(()); }
// Encoding can finish in the same poll that publishes unlock/readiness.
if !updates.borrow().waiting || *locked.borrow() != Some(true)
|| pc.connection_state() != RTCPeerConnectionState::Connected { continue; }
let (codec, data) = frame.as_ref().unwrap();
let sample = Sample { data: data.clone(), duration: Duration::from_secs(1), ..Default::default() };
let extensions = [crate::rtp_metadata::capture_index(0), crate::rtp_metadata::new_frame()];
let revision = track.revision();
// No monitor inventory or permission claims: 1280x720 is only this synthetic bitstream.
tokio::select! {
biased;
_ = updates.changed() => continue,
_ = locked.changed() => continue,
result = tokio::time::timeout(Duration::from_secs(2),
track.write_sample_with_extensions(*codec, &sample, &extensions)) => {
if crate::video::transport_closed(pc) { return Ok(()); }
result.map_err(|_| "Locked desktop video send timed out".to_string())?
.map_err(|error| format!("Locked desktop video send: {error}"))?;
},
}
attempts += 1;
if track.revision() > revision { sent += 1; }
tick.reset();
if attempts == 1 || attempts % 10 == 0 {
log::info!("Locked desktop video: sent={sent} codec={codec:?} synthetic_size=1280x720 bytes={} attempts={attempts}", data.len());
}
},
}
}
}
#[cfg(test)]
#[path = "locked_video_tests.rs"]
mod tests;
+350
View File
@@ -0,0 +1,350 @@
use super::{black_frame, run, run_with_encoder};
use crate::{
capture::ScreenCapture,
capture_setting::VideoCodec,
desktop_backend::DesktopBackend,
desktop_session::PortalDesktop,
video_track::VideoTrack,
};
use std::{process::Stdio, sync::Arc, time::Duration};
use tokio::{io::AsyncWriteExt, sync::{mpsc, watch}};
use webrtc::{
api::{media_engine::MediaEngine, APIBuilder},
media::Sample,
peer_connection::{
configuration::RTCConfiguration,
peer_connection_state::RTCPeerConnectionState,
RTCPeerConnection,
},
rtp::packet::Packet,
rtp_transceiver::{
rtp_codec::{RTCRtpCodecCapability, RTCRtpCodecParameters, RTPCodecType},
},
util::vnet::router::Router,
};
async fn decode_gray(data: &[u8], demuxer: &str) -> (Vec<u8>, String) {
let mut command = crate::video::ffmpeg_command();
command.args([
"-hide_banner", "-loglevel", "info", "-nostdin", "-f", demuxer, "-i", "pipe:0",
"-frames:v", "1", "-vf", "scale=in_range=tv:out_range=pc,format=gray,showinfo",
"-pix_fmt", "gray", "-f", "rawvideo", "pipe:1",
]);
command.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).kill_on_drop(true);
let mut child = command.spawn().expect("start FFmpeg decoder");
let mut stdin = child.stdin.take().expect("FFmpeg decoder stdin");
let output = tokio::time::timeout(Duration::from_secs(10), async move {
stdin.write_all(data).await.expect("write encoded frame to FFmpeg");
drop(stdin);
child.wait_with_output().await.expect("wait for FFmpeg decoder")
}).await.expect("FFmpeg decoder timed out");
assert!(output.status.success(), "FFmpeg decoder failed: status={}", output.status);
(output.stdout, String::from_utf8_lossy(&output.stderr).into_owned())
}
async fn black_frame_decodes_as_1280x720_black(codec: VideoCodec, demuxer: &str) {
let encoded = black_frame(codec).await.expect("encode black frame");
assert!(!encoded.is_empty());
let (gray, diagnostics) = decode_gray(&encoded, demuxer).await;
assert!(diagnostics.contains("s:1280x720"), "decoded dimensions were not 1280x720");
assert_eq!(gray.len(), 1280 * 720, "expected one 1280x720 gray frame");
assert!(gray.iter().all(|&pixel| pixel <= 1), "decoded frame was not black");
}
#[tokio::test]
#[ignore = "requires FFmpeg libx264/libx265"]
async fn h264_black_frame_decodes_to_1280x720_black() {
black_frame_decodes_as_1280x720_black(VideoCodec::H264, "h264").await;
}
#[tokio::test]
#[ignore = "requires FFmpeg libx264/libx265"]
async fn h265_black_frame_decodes_to_1280x720_black() {
black_frame_decodes_as_1280x720_black(VideoCodec::H265, "hevc").await;
}
fn ready_screen() -> Arc<ScreenCapture> {
let mut screen = ScreenCapture::unavailable("synthetic ready desktop".into());
screen.backend = DesktopBackend::Portal;
screen.init_error = None;
screen.display = "wayland:test".into();
screen.width = 1280;
screen.height = 720;
Arc::new(screen)
}
async fn receive_frame(packets: &mut mpsc::UnboundedReceiver<Packet>, within: Duration) -> Vec<Packet> {
tokio::time::timeout(within, async {
let mut frame = Vec::new();
loop {
let packet = packets.recv().await.expect("RTP packet receiver closed");
let marker = packet.header.marker;
frame.push(packet);
if marker {
return frame;
}
}
}).await.expect("timed out waiting for an RTP frame")
}
async fn assert_no_rtp(packets: &mut mpsc::UnboundedReceiver<Packet>, within: Duration) {
assert!(tokio::time::timeout(within, packets.recv()).await.is_err(),
"locked video task sent RTP while transmission was disabled");
}
fn assert_single_rtp_frame(frame: &[Packet]) {
let first = frame.first().expect("RTP frame has packets");
let last = frame.last().unwrap();
assert!(last.header.marker, "RTP frame ends with a marker packet");
for pair in frame.windows(2) {
assert_eq!(pair[0].header.ssrc, pair[1].header.ssrc);
assert_eq!(pair[0].header.timestamp, pair[1].header.timestamp);
assert_eq!(pair[1].header.sequence_number, pair[0].header.sequence_number.wrapping_add(1));
}
assert!(frame.iter().all(|packet| packet.header.ssrc == first.header.ssrc));
}
struct VirtualPeers {
host: Arc<RTCPeerConnection>,
remote: Arc<RTCPeerConnection>,
_control: Arc<webrtc::data_channel::RTCDataChannel>,
router: Arc<tokio::sync::Mutex<Router>>,
track: Arc<VideoTrack>,
packets: mpsc::UnboundedReceiver<Packet>,
}
impl VirtualPeers {
async fn new(codec: VideoCodec) -> Self {
let (remote_setting, host_setting, router) = crate::test_network::virtual_network().await;
let mut remote_media = MediaEngine::default();
match codec {
VideoCodec::H264 => remote_media.register_codec(RTCRtpCodecParameters {
capability: RTCRtpCodecCapability {
mime_type: VideoCodec::H264.mime().into(),
clock_rate: 90_000,
sdp_fmtp_line: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f".into(),
..Default::default()
},
payload_type: 102,
..Default::default()
}, RTPCodecType::Video).unwrap(),
VideoCodec::H265 => crate::video_track::register_hevc(&mut remote_media).unwrap(),
}
let remote = Arc::new(APIBuilder::new().with_setting_engine(remote_setting)
.with_media_engine(remote_media).build()
.new_peer_connection(RTCConfiguration::default()).await.unwrap());
remote.add_transceiver_from_kind(RTPCodecType::Video, None).await.unwrap();
let control = remote.create_data_channel("CONTROL_DATA_CHANNEL", None).await.unwrap();
let mut host_media = MediaEngine::default();
host_media.register_default_codecs().unwrap();
crate::video_track::register_hevc(&mut host_media).unwrap();
let host = Arc::new(APIBuilder::new().with_setting_engine(host_setting)
.with_media_engine(host_media).build()
.new_peer_connection(RTCConfiguration::default()).await.unwrap());
let track = Arc::new(VideoTrack::new());
host.add_track(track.clone()).await.unwrap();
let (packet_tx, packets) = mpsc::unbounded_channel();
remote.on_track(Box::new(move |remote_track, _, _| {
let packet_tx = packet_tx.clone();
Box::pin(async move {
tokio::spawn(async move {
while let Ok((packet, _)) = remote_track.read_rtp().await {
if packet_tx.send(packet).is_err() { break; }
}
});
})
}));
Self { host, remote, _control: control, router, track, packets }
}
async fn connect(&self, codec: VideoCodec) {
let offer = self.remote.create_offer(None).await.unwrap();
let mut gathered = self.remote.gathering_complete_promise().await;
self.remote.set_local_description(offer).await.unwrap();
gathered.recv().await;
self.host.set_remote_description(self.remote.local_description().await.unwrap()).await.unwrap();
let answer = self.host.create_answer(None).await.unwrap();
let mut gathered = self.host.gathering_complete_promise().await;
self.host.set_local_description(answer).await.unwrap();
gathered.recv().await;
self.remote.set_remote_description(self.host.local_description().await.unwrap()).await.unwrap();
tokio::time::timeout(Duration::from_secs(15), async {
while self.host.connection_state() != RTCPeerConnectionState::Connected
|| !self.track.supports(codec)
{
tokio::time::sleep(Duration::from_millis(10)).await;
}
}).await.expect("virtual WebRTC peers did not connect with the requested codec");
assert!(!self.track.supports(match codec {
VideoCodec::H264 => VideoCodec::H265,
VideoCodec::H265 => VideoCodec::H264,
}));
}
async fn close(self) {
self.host.close().await.unwrap();
self.remote.close().await.unwrap();
self.router.lock().await.stop().await.unwrap();
}
}
async fn exercise_locked_video(codec: VideoCodec) {
tokio::time::timeout(Duration::from_secs(35), async {
let mut peers = VirtualPeers::new(codec).await;
let (lock_tx, lock_rx) = watch::channel(Some(true));
let desktop = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
lock_rx,
));
assert!(desktop.snapshot().waiting);
assert!(!desktop.snapshot().ready());
let run_host = peers.host.clone();
let run_track = peers.track.clone();
let run_desktop = desktop.clone();
let task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
assert_ne!(peers.host.connection_state(), RTCPeerConnectionState::Connected);
assert_no_rtp(&mut peers.packets, Duration::from_millis(150)).await;
peers.connect(codec).await;
let locked_frame = receive_frame(&mut peers.packets, Duration::from_secs(5)).await;
assert_single_rtp_frame(&locked_frame);
let last_locked_packet = locked_frame.last().unwrap().clone();
lock_tx.send_replace(None);
assert_no_rtp(&mut peers.packets, Duration::from_millis(1200)).await;
lock_tx.send_replace(Some(false));
assert_no_rtp(&mut peers.packets, Duration::from_millis(1200)).await;
desktop.publish_test_screen(ready_screen());
tokio::time::timeout(Duration::from_secs(2), task).await
.expect("locked video task did not stop when the desktop became ready")
.expect("locked video task panicked")
.expect("locked video task returned an error");
let live_data = black_frame(codec).await.expect("encode live test sample");
peers.track.write_sample_with_extensions(codec, &Sample {
data: live_data,
duration: Duration::from_millis(20),
..Default::default()
}, &[]).await.unwrap();
let live_frame = receive_frame(&mut peers.packets, Duration::from_secs(5)).await;
assert_single_rtp_frame(&live_frame);
let first_live_packet = &live_frame[0];
assert_eq!(first_live_packet.header.ssrc, last_locked_packet.header.ssrc);
assert_eq!(first_live_packet.header.sequence_number,
last_locked_packet.header.sequence_number.wrapping_add(1));
assert_eq!(first_live_packet.header.timestamp,
last_locked_packet.header.timestamp.wrapping_add(90_000));
peers.close().await;
}).await.expect("locked video WebRTC test exceeded its deadline");
}
#[tokio::test]
#[ignore = "requires FFmpeg libx264/libx265"]
async fn locked_video_gates_on_pending_locked_connected_and_preserves_video_timeline() {
exercise_locked_video(VideoCodec::H264).await;
exercise_locked_video(VideoCodec::H265).await;
}
#[tokio::test]
async fn locked_video_rechecks_state_when_encoding_finishes() {
#[derive(Clone, Copy)]
enum Transition { Unlocked, Unknown, Ready }
tokio::time::timeout(Duration::from_secs(20), async {
let mut peers = VirtualPeers::new(VideoCodec::H264).await;
peers.connect(VideoCodec::H264).await;
for transition in [Transition::Unlocked, Transition::Unknown, Transition::Ready] {
let (lock_tx, lock_rx) = watch::channel(Some(true));
let desktop = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
lock_rx,
));
let run_host = peers.host.clone();
let run_track = peers.track.clone();
let run_desktop = desktop.clone();
let (encoded_tx, mut encoded_rx) = mpsc::unbounded_channel();
let revision = peers.track.revision();
let task = tokio::spawn(async move {
run_with_encoder(&run_host, &run_track, &run_desktop, |_| {
let desktop = run_desktop.clone();
let lock_tx = lock_tx.clone();
let encoded_tx = encoded_tx.clone();
async move {
// Change state during the encoder's final poll, before it returns Ready.
match transition {
Transition::Unlocked => { lock_tx.send_replace(Some(false)); },
Transition::Unknown => { lock_tx.send_replace(None); },
Transition::Ready => desktop.publish_test_screen(ready_screen()),
}
encoded_tx.send(()).unwrap();
Ok(bytes::Bytes::from_static(&[0, 0, 1, 65, 1, 2]))
}
}).await
});
tokio::time::timeout(Duration::from_secs(2), encoded_rx.recv()).await
.expect("test encoder was not polled").expect("test encoder channel closed");
assert_no_rtp(&mut peers.packets, Duration::from_millis(250)).await;
assert_eq!(peers.track.revision(), revision, "stale state allowed a synthetic frame");
desktop.publish_test_screen(ready_screen());
tokio::time::timeout(Duration::from_secs(2), task).await
.expect("locked video task did not stop after readiness")
.expect("locked video task panicked")
.expect("locked video task returned an error");
}
peers.close().await;
}).await.expect("locked video concurrent-transition test exceeded its deadline");
}
#[tokio::test]
async fn locked_video_stops_on_capture_rejection_and_terminal_peer_close() {
tokio::time::timeout(Duration::from_secs(25), async {
let mut peers = VirtualPeers::new(VideoCodec::H264).await;
peers.connect(VideoCodec::H264).await;
let (_lock_tx, lock_rx) = watch::channel(Some(false));
let rejected_desktop = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
lock_rx.clone(),
));
let run_host = peers.host.clone();
let run_track = peers.track.clone();
let run_desktop = rejected_desktop.clone();
let rejection_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
rejected_desktop.publish_test_screen(
Arc::new(ScreenCapture::unavailable("synthetic capture rejection".into())),
);
assert_eq!(rejected_desktop.snapshot().permission(true),
crate::system_metrics::Permission::Reject);
tokio::time::timeout(Duration::from_secs(2), rejection_task).await
.expect("locked video task did not stop after capture rejection")
.expect("locked video task panicked")
.expect("locked video task returned an error");
let pending_desktop = Arc::new(PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())),
lock_rx,
));
let run_host = peers.host.clone();
let run_track = peers.track.clone();
let run_desktop = pending_desktop;
let close_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
peers.host.close().await.unwrap();
tokio::time::timeout(Duration::from_secs(2), close_task).await
.expect("locked video task did not stop after terminal peer close")
.expect("locked video task panicked")
.expect("locked video task returned an error");
peers.packets.close();
assert!(peers.packets.try_recv().is_err(), "locked video task sent RTP while unlocked");
peers.remote.close().await.unwrap();
peers.router.lock().await.stop().await.unwrap();
}).await.expect("locked video lifecycle test exceeded its deadline");
}
+67 -32
View File
@@ -2,6 +2,8 @@
//! Zero-Wine, Zero-Python, Single-Binary, 5 Native Tools Integrated
mod desktop_backend;
mod desktop_session;
mod desktop_power;
mod portal;
mod network_diagnostics;
mod session_ice;
@@ -25,6 +27,7 @@ mod terminal;
mod webrtc;
mod video;
mod video_track;
mod locked_video;
#[cfg(test)]
mod test_network;
mod hevc;
@@ -60,6 +63,52 @@ use webrtc::WebRtcSession;
/// Set before a normal return when the host must come back in the other mode (signed in / out).
static RESTART: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
fn superseded_peer_ids(sessions: &HashMap<String, WebRtcSession>, ice: &str, desktop: bool) -> Vec<String> {
sessions.iter().filter(|(id, peer)| id.as_str() == ice || (desktop && peer.desktop))
.map(|(id, _)| id.clone()).collect()
}
fn released_peer_ids<T>(sessions: &HashMap<String, T>, ice: Option<&serde_json::Value>) -> Vec<String> {
let ice = ice.map(|id| id.as_str().map(str::to_owned).unwrap_or_else(|| id.to_string()));
sessions.keys().filter(|id| ice.as_deref().map_or(true, |ice| id.as_str() == ice)).cloned().collect()
}
#[cfg(test)]
mod peer_release_tests {
use super::*;
#[test]
fn explicit_releases_leave_independent_and_replacement_peers_alive() {
let mut peers = HashMap::from([
("old-desktop".to_owned(), ()), ("files".to_owned(), ()),
("terminal".to_owned(), ()), ("port-mapping".to_owned(), ()),
]);
let targets = released_peer_ids(&peers, Some(&serde_json::json!("old-desktop")));
assert_eq!(targets, ["old-desktop"]);
for id in targets { peers.remove(&id); }
peers.insert("new-desktop".to_owned(), ());
assert!(released_peer_ids(&peers, Some(&serde_json::json!("old-desktop"))).is_empty());
assert!(released_peer_ids(&peers, Some(&serde_json::json!("unknown"))).is_empty());
for id in ["files", "terminal", "port-mapping", "new-desktop"] {
assert_eq!(released_peer_ids(&peers, Some(&serde_json::json!(id))), [id]);
}
assert_eq!(peers.len(), 4);
}
#[test]
fn release_without_an_ice_id_retains_room_wide_cleanup() {
let peers = HashMap::from([("desktop".to_owned(), ()), ("files".to_owned(), ())]);
let mut targets = released_peer_ids(&peers, None);
targets.sort();
assert_eq!(targets, ["desktop", "files"]);
}
#[test]
fn explicit_non_string_ice_ids_never_become_room_wide_releases() {
let peers = HashMap::from([("123".to_owned(), ()), ("files".to_owned(), ())]);
for id in [serde_json::json!(999), serde_json::Value::Null, serde_json::json!(false), serde_json::json!({})] {
assert!(released_peer_ids(&peers, Some(&id)).is_empty());
}
assert_eq!(released_peer_ids(&peers, Some(&serde_json::json!(123))), ["123"]);
}
}
/// Re-executes this host; the new instance opens its window so the user sees the result.
fn restart_host() -> ! {
use std::os::unix::process::CommandExt;
@@ -234,6 +283,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut frame = vec![0; screen.width as usize * screen.height as usize * 4];
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
loop {
if *locked.borrow() == Some(false) {
session.recover(&std::sync::atomic::AtomicBool::new(false)).map_err(|error| error.to_string())?;
}
session.capture(&mut frame)?;
if *locked.borrow() == Some(false) && session.is_alive() { break; }
if std::time::Instant::now() >= deadline { return Err("Test VM portal did not recover".into()); }
@@ -356,9 +408,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let capture = Arc::new((if desktop_backend::headless() {
Err("headless mode (UU_DESKTOP_BACKEND=none)".to_owned())
} else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal
&& std::env::var("UU_PORTAL_PREAUTHORIZE").ok().as_deref()!=Some("1") {
Err("Portal screen access requires a desktop connection and local consent".to_owned())
} else if desktop_backend::detect()?==desktop_backend::DesktopBackend::Portal {
Err("Portal capture waits for a desktop CONTROL connection".to_owned())
} else { ScreenCapture::new() }).unwrap_or_else(|error| {
log::warn!("Desktop unavailable; terminal and file tools remain available: {error}");
ScreenCapture::unavailable(error)
@@ -424,6 +475,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutdown: &mut tokio::sync::watch::Receiver<bool>,
assist: &mut Option<assist::Assist>, commands: &mut mpsc::Receiver<assist::Command>, webrtc_sessions: Arc<Mutex<HashMap<String, WebRtcSession>>>) -> Result<(), Box<dyn std::error::Error>> {
let portal_desktop = (capture.backend == desktop_backend::DesktopBackend::Portal && !desktop_backend::headless())
.then(|| Arc::new(desktop_session::PortalDesktop::new(capture.clone())));
let mut auth = if let Some(session) = assist.as_ref() { session.client.clone() } else {
let creds = match config::file().and_then(Credentials::load) {
Ok(creds) => creds,
@@ -655,22 +708,10 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
let controller_mac = mac_by_ice.remove(&ice).unwrap_or(next_mac);
if desktop { input.set_mac_controller(controller_mac); }
log::info!("Offer for connection: capture_type={} desktop={} active_peers={}", capture_type, desktop, sessions.len());
if let Some(old) = sessions.remove(&ice) { let _ = old.pc.close().await; }
if desktop {
// One desktop stream at a time: a new desktop offer replaces stale ones.
let stale: Vec<String> = sessions.iter().filter(|(_, s)| s.desktop).map(|(k, _)| k.clone()).collect();
for k in stale { if let Some(old) = sessions.remove(&k) { let _ = old.pc.close().await; } }
// Replace only this ICE id or a previous desktop, never tool peers.
for id in superseded_peer_ids(&sessions, &ice, desktop) {
if let Some(old) = sessions.remove(&id) { let _ = old.pc.close().await; }
}
let stale_portal=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;
@@ -678,11 +719,7 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
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()));
}
let session_capture=if desktop && portal_desktop.is_none() && (capture.init_error.is_some() || resized) {
match tokio::task::spawn_blocking(ScreenCapture::new).await {
Ok(Ok(screen))=>{ capture=Arc::new(screen);capture.clone() },
Ok(Err(error))=>{ log::warn!("Desktop connection unavailable: {error}"); continue; },
@@ -691,8 +728,10 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
} else { capture.clone() };
// Independent file/terminal peers must not detach the
// current desktop's authorized Portal input session.
if desktop { input.bind_portal(session_capture.portal.clone()); }
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport).await {
if desktop && portal_desktop.is_none() { input.bind_portal(session_capture.portal.clone()); }
if desktop { if let Some(portal) = &portal_desktop { portal.begin(); } }
match WebRtcSession::new(session_capture, input.clone(), signaling_client.clone(), desktop, transport,
if desktop { portal_desktop.clone() } else { None }).await {
Ok(sess) => {
// Register before awaiting SDP: shutdown can always close
// a peer whose answer may already be queued to the viewer.
@@ -730,15 +769,11 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
}
}
SignalingEvent::Released(val) | SignalingEvent::Left(val) => {
let ice = ["ice_id", "iceId"].iter().find_map(|k| val.get(*k).or_else(|| val.get("data").and_then(|d| d.get(*k))))
.and_then(|v| v.as_str()).map(str::to_owned);
let ice = ["ice_id", "iceId"].iter().find_map(|k| val.get(*k).or_else(|| val.get("data").and_then(|d| d.get(*k))));
let keys: Vec<String> = val.as_object().map(|o| o.keys().take(16).cloned().collect()).unwrap_or_default();
let mut sessions = webrtc_sessions.lock().await;
let targets: Vec<String> = match &ice {
Some(id) if sessions.contains_key(id) => vec![id.clone()],
_ => sessions.keys().cloned().collect(),
};
log::info!("Remote controller disconnected: keys={:?} ice_matched={} closing={}", keys, ice.as_ref().is_some_and(|i| sessions.contains_key(i)), targets.len());
let targets = released_peer_ids(&sessions, ice);
log::info!("Remote controller disconnected: keys={:?} ice_matched={} closing={}", keys, ice.is_some() && !targets.is_empty(), targets.len());
let mut desktop_ended = false;
for k in targets {
if let Some(old) = sessions.remove(&k) {
+188 -93
View File
@@ -1,6 +1,18 @@
//! GNOME/KDE portal session. Capture and input must share this Arc; do not
//! recreate it in an encoder subprocess (that would request consent again).
use std::sync::Arc;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PortalError { pub code: i32 }
impl std::fmt::Display for PortalError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}", error(self.code))
}
}
impl std::error::Error for PortalError {}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct FrameVersion(usize, u64);
#[cfg(unix)]
@@ -12,6 +24,7 @@ mod native {
use std::{ffi::c_void, ptr::NonNull, sync::{Mutex, MutexGuard, TryLockError}};
extern "C" {
fn uu_portal_open_persist(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
fn uu_portal_open_persist_cancellable(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), save_data: *mut c_void, cancelled: extern "C" fn(*mut c_void) -> i32, cancel_data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
fn uu_portal_clipboard_supported(portal: *mut c_void) -> i32;
fn uu_portal_clipboard_ready(portal: *mut c_void) -> i32;
fn uu_portal_clipboard_set(portal: *mut c_void, text: *const u8, length: usize) -> i32;
@@ -47,27 +60,29 @@ mod native {
Ok(true)
}
#[derive(Default)]
struct Recovery { retry_at: Option<std::time::Instant>, retired: bool }
struct Recovery { retry_at: Option<std::time::Instant>, failed_until: Option<std::time::Instant>, denied: bool }
impl Recovery {
// Shared by the encoder's stream-loss recovery and the desktop supervisor: a failed open
// backs both off, and a terminal consent/format failure stops the encoder from prompting again.
fn failed(&mut self, now: std::time::Instant, code: i32) {
let until = now + std::time::Duration::from_secs(5);
self.failed_until = Some(until);
self.retry_at = Some(until);
if matches!(code, 4 | 5 | 6 | 8 | 14) { self.denied = true; }
}
fn succeeded(&mut self) { *self = Self::default(); }
fn ready(&mut self, now: std::time::Instant, unlocked: impl FnOnce() -> bool) -> bool {
if self.retired { return false; }
if self.denied { return false; }
let deadline = self.retry_at.get_or_insert(now + std::time::Duration::from_secs(1));
if now < *deadline { return false; }
*deadline = now + std::time::Duration::from_secs(1);
unlocked()
}
}
fn 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)
fn clear_pause_for_unlocked_replacement(was_locked: &std::sync::atomic::AtomicBool, state: Option<bool>) {
if state == Some(false) {
was_locked.store(false, std::sync::atomic::Ordering::Release);
}
}
pub struct Session { handle: Mutex<Handle>, locked: tokio::sync::watch::Receiver<Option<bool>>, was_locked: std::sync::atomic::AtomicBool, recovery: Mutex<Recovery>, pub width: u32, pub height: u32 }
impl Session {
@@ -81,11 +96,11 @@ mod native {
};
Ok(self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel))
}
fn check_geometry(&self, replacement: &Self) -> Result<(), String> {
fn check_geometry(&self, replacement: &Self) -> Result<(), super::PortalError> {
// Encoder buffers use frame pixels. Logical geometry and EIS regions
// belong to the replacement handle and may change with scaling (#25).
if (self.width, self.height) != (replacement.width, replacement.height) {
Err("Portal dimensions changed; reconnect required".into())
Err(super::PortalError { code: 14 })
} else {
Ok(())
}
@@ -93,11 +108,14 @@ mod native {
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> {
fn open_inner(cancelled: Option<&std::sync::atomic::AtomicBool>) -> Result<Self, super::PortalError> {
extern "C" fn is_cancelled(data: *mut c_void) -> i32 {
let cancelled = unsafe { &*(data as *const std::sync::atomic::AtomicBool) };
i32::from(cancelled.load(std::sync::atomic::Ordering::Acquire))
}
if cancelled.is_some_and(|value| value.load(std::sync::atomic::Ordering::Acquire)) {
return Err(super::PortalError { code: 13 });
}
let (mut error, mut width, mut height) = (0, 0, 0);
let directory = super::permissions::directory();
let key = super::permissions::desktop_key();
@@ -114,13 +132,79 @@ mod native {
});
if result.is_err() { log::warn!("Portal permission cache callback failed"); }
}
let pointer = unsafe { uu_portal_open_persist(token.as_ref().map_or(std::ptr::null(), |v| v.as_ptr()), save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height) };
let handle = NonNull::new(pointer).ok_or_else(|| super::error(error))?;
let restore = token.as_ref().map_or(std::ptr::null(), |value| value.as_ptr());
let pointer = unsafe {
if let Some(cancelled) = cancelled {
uu_portal_open_persist_cancellable(
restore, save_token, &mut cache as *mut _ as *mut c_void,
is_cancelled, cancelled as *const _ as *mut c_void,
&mut error, &mut width, &mut height,
)
} else {
uu_portal_open_persist(restore, save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height)
}
};
let handle = NonNull::new(pointer).ok_or(super::PortalError { code: error })?;
// A freshly opened session holds no lock-screen frame: only an observed lock (or a
// closed stream) must discard the cached frame. Starting "locked" cleared the first
// valid frame, and a static desktop sends no new one, so the viewer stayed black (#2).
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), recovery: Mutex::new(Recovery::default()), width, height })
}
pub fn open() -> Result<Self, String> {
Self::open_inner(None).map_err(|error| error.to_string())
}
pub fn open_cancellable(cancelled: &std::sync::atomic::AtomicBool) -> Result<Self, super::PortalError> {
Self::open_inner(Some(cancelled))
}
pub fn recover(&self, cancelled: &std::sync::atomic::AtomicBool) -> Result<bool, super::PortalError> {
use std::sync::atomic::Ordering;
if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); }
// Serialize with the encoder's stream-loss recovery: two openers would ask for consent twice.
let mut recovery = loop {
match self.recovery.try_lock() {
Ok(recovery) => break recovery,
Err(TryLockError::WouldBlock) => {
if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); }
std::thread::sleep(std::time::Duration::from_millis(20));
}
Err(TryLockError::Poisoned(_)) => return Err(super::PortalError { code: 3 }),
}
};
if recovery.failed_until.is_some_and(|until| std::time::Instant::now() < until) {
return Err(super::PortalError { code: 3 });
}
let alive = {
let handle = self.handle.lock().map_err(|_| super::PortalError { code: 3 })?;
unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 && uu_portal_capture_alive(handle.0.as_ptr()) != 0 }
};
if alive { return Ok(false); }
let result = super::open_then_install(cancelled, || Self::open_cancellable(cancelled), |replacement| {
self.check_geometry(&replacement)?;
let replacement_handle = replacement.handle.into_inner().map_err(|_| super::PortalError { code: 3 })?;
let mut handle = self.handle.lock().map_err(|_| super::PortalError { code: 3 })?;
if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); }
*handle = replacement_handle;
clear_pause_for_unlocked_replacement(&self.was_locked, *self.locked.borrow());
log::info!("Portal capture session restored after closure");
Ok(())
});
match result {
Ok(()) => { recovery.succeeded(); Ok(true) }
Err(error) => {
if error.code != 13 { recovery.failed(std::time::Instant::now(), error.code); }
Err(error)
}
}
}
pub fn pause(&self) {
if let Ok(handle) = self.handle.lock() {
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
} else {
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
}
}
pub fn capture(&self, frame: &mut [u8]) -> Result<(), String> {
self.capture_changed(frame, &mut None).map(|_| ())
}
@@ -134,8 +218,8 @@ mod native {
if state != Some(false) {
// Unknown state (watcher not reported yet): show nothing but keep the cached frame.
if state == Some(true) {
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
}
return Ok(blank_changed(frame, version));
}
@@ -166,16 +250,16 @@ mod native {
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
false
}) { return Ok(None); }
let replacement = match Self::open() {
let replacement = match Self::open_inner(None) {
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));
recovery.failed(std::time::Instant::now(), error.code);
return Ok(None);
}
};
recovery.retry_at = None;
self.check_geometry(&replacement)?;
recovery.succeeded();
self.check_geometry(&replacement).map_err(|error| error.to_string())?;
// Consent can outlive the unlocked state checked above.
if *self.locked.borrow() != Some(false) || self.was_locked.load(std::sync::atomic::Ordering::Acquire) {
return Ok(None);
@@ -384,70 +468,17 @@ mod native {
}
#[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(&current, 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(&current, &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(&current, &recovery, |alive| *alive, || false).unwrap());
assert!(!recovery.lock().unwrap().retired);
assert!(retire_if_closed(&current, &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")));
fn terminal_failure_stops_encoder_recovery_until_the_supervisor_succeeds() {
let now = std::time::Instant::now();
let later = now + std::time::Duration::from_secs(60);
let mut recovery = Recovery::default();
recovery.failed(now, 3);
assert!(!recovery.denied && recovery.failed_until.is_some());
recovery.failed(now, 4);
assert!(recovery.denied);
assert!(!recovery.ready(later, || panic!("denied consent must not be probed again")));
recovery.succeeded();
assert!(!recovery.denied && recovery.failed_until.is_none() && recovery.retry_at.is_none());
}
#[test]
@@ -514,18 +545,46 @@ mod native {
assert!(session.take_lock_observation().unwrap());
assert!(!session.was_locked.load(std::sync::atomic::Ordering::Acquire));
}
#[test]
fn cancelled_recovery_does_not_touch_the_handle() {
let session = Fixture::new((1920, 1080));
let cancelled = std::sync::atomic::AtomicBool::new(true);
assert_eq!(session.session().recover(&cancelled).unwrap_err().code, 13);
}
#[test]
fn unlocked_replacement_keeps_its_first_static_frame() {
let was_locked = std::sync::atomic::AtomicBool::new(true);
clear_pause_for_unlocked_replacement(&was_locked, Some(false));
assert!(!was_locked.load(std::sync::atomic::Ordering::Acquire));
}
}
}
fn blank(frame: &mut [u8]) { for pixel in frame.chunks_exact_mut(4) { pixel.copy_from_slice(&[0, 0, 0, 255]); } }
fn open_then_install<T>(
cancelled: &std::sync::atomic::AtomicBool,
open: impl FnOnce() -> Result<T, PortalError>,
install: impl FnOnce(T) -> Result<(), PortalError>,
) -> Result<(), PortalError> {
use std::sync::atomic::Ordering;
if cancelled.load(Ordering::Acquire) { return Err(PortalError { code: 13 }); }
let replacement = open()?;
if cancelled.load(Ordering::Acquire) { return Err(PortalError { code: 13 }); }
install(replacement)
}
fn error(code: i32) -> String {
let reason = match code {
1 => "session bus unavailable", 2 => "RemoteDesktop/ScreenCast portal unavailable",
3 => "request failed", 4 => "permission cancelled or denied", 5 => "permission request timed out",
6 => "single monitor and keyboard/pointer permission required", 7 => "PipeWire connection failed",
8 => "unsupported frame format or size", 9 => "session closed or stream lost",
10 => "frame unavailable", 11 => "invalid input or frame buffer", _ => "operation failed",
10 => "frame unavailable", 11 => "invalid input or frame buffer",
13 => "operation cancelled", 14 => "dimensions changed; reconnect required",
_ => "operation failed",
};
format!("Portal {reason} (code {code})")
}
@@ -542,8 +601,16 @@ 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 open_cancellable(cancelled: &std::sync::atomic::AtomicBool) -> Result<Self, PortalError> {
if cancelled.load(std::sync::atomic::Ordering::Acquire) { Err(PortalError { code: 13 }) }
else { Err(PortalError { code: 2 }) }
}
pub fn recover(&self, cancelled: &std::sync::atomic::AtomicBool) -> Result<bool, PortalError> {
if cancelled.load(std::sync::atomic::Ordering::Acquire) { Err(PortalError { code: 13 }) }
else { Err(PortalError { code: 2 }) }
}
pub fn pause(&self) {}
pub fn capture(&self, _: &mut [u8]) -> Result<(), String> { Err(error(2)) }
pub fn capture_changed(&self, _: &mut [u8], _: &mut Option<FrameVersion>) -> Result<bool, String> { Err(error(2)) }
pub fn move_pointer(&self, _: f64, _: f64, _: bool) -> Result<(), String> { Err(error(2)) }
@@ -575,4 +642,32 @@ mod tests {
assert!(super::check(4).unwrap_err().contains("cancelled or denied"));
assert!(super::check(9).unwrap_err().contains("closed or stream lost"));
}
#[test]
fn portal_error_display_is_fixed_and_sanitized() {
assert_eq!(super::PortalError { code: 13 }.to_string(), "Portal operation cancelled (code 13)");
assert_eq!(super::PortalError { code: 14 }.to_string(), "Portal dimensions changed; reconnect required (code 14)");
assert_eq!(super::PortalError { code: 123 }.to_string(), "Portal operation failed (code 123)");
}
#[test]
fn opening_with_pre_cancelled_flag_returns_cancellation_code() {
let cancelled = std::sync::atomic::AtomicBool::new(true);
let code = match super::PortalSession::open_cancellable(&cancelled) {
Err(error) => error.code,
Ok(_) => panic!("pre-cancelled portal open unexpectedly succeeded"),
};
assert_eq!(code, 13);
}
#[test]
fn replacement_opens_before_installing_under_the_handle_lock() {
let handle = std::sync::Mutex::new(7);
let cancelled = std::sync::atomic::AtomicBool::new(false);
super::open_then_install(&cancelled, || {
assert_eq!(*handle.try_lock().unwrap(), 7);
Ok(11)
}, |replacement| {
*handle.lock().unwrap() = replacement;
Ok(())
}).unwrap();
assert_eq!(*handle.lock().unwrap(), 11);
}
}
+6
View File
@@ -10,6 +10,12 @@ pub struct SessionIce {
}
impl SessionIce {
#[cfg(test)]
pub fn local() -> Self {
// A supplied empty URL list disables the public STUN defaults in local peer tests.
Self { servers: vec![RTCIceServer::default()], relay_only: false }
}
pub fn from_header(header:&Value)->Self {
let data=header.get("data").unwrap_or(header);
let field=|name:&str|header.get(name).or_else(||data.get(name));
+13 -8
View File
@@ -119,7 +119,9 @@ fn unlock_with(password: &str) -> UnlockResult {
if Command::new("loginctl").args(["unlock-session", &session]).status().map(|s| !s.success()).unwrap_or(true) {
return UnlockResult::NoPermission;
}
let _ = crate::capture::keep_display_awake(); // a blanked monitor would keep the stream black
if let Err(error) = crate::capture::keep_display_awake() {
log::warn!("Display wake after auto unlock failed: {error}");
}
// Post-unlock confirmation like the official host: the lock screen must actually go away.
for _ in 0..100 { // GNOME takes ~3 s to drop the shield after logind's Unlock
if session_locked(&session) == Some(false) { return UnlockResult::Success; }
@@ -134,23 +136,26 @@ type Sequence = std::sync::Arc<std::sync::atomic::AtomicU64>;
fn now() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs() }
/// Message 15 SystemStateChange { 1: SecureDesktopState } on CONTROL.
pub async fn send_state(channel: &Channel, sequence: &Sequence, locked: bool) {
pub async fn send_state(channel: &Channel, sequence: &Sequence, locked: bool) -> bool {
let seq = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let message = crate::initialization::encode_secure_desktop(seq, now(), locked, false, supported());
if let Err(e) = channel.send(&bytes::Bytes::from(message)).await { log::warn!("Secure desktop state send failed: {e}"); }
match channel.send(&bytes::Bytes::from(message)).await {
Ok(_) => {
log::info!("Secure desktop state sent: locked={} auto_unlock={}", locked, supported());
true
}
Err(e) => { log::warn!("Secure desktop state send failed: {e}"); false }
}
}
/// Reports the lock state to one desktop viewer: now, then on every change, until it closes.
pub async fn report_states(channel: Channel, sequence: Sequence) {
pub async fn report_states(channel: Channel, sequence: Sequence, mut state: watch::Receiver<Option<bool>>) {
use webrtc::data_channel::data_channel_state::RTCDataChannelState;
let mut state = watch_locked();
let mut sent = None;
loop {
let locked = *state.borrow_and_update();
if let Some(locked) = locked.filter(|l| sent != Some(*l)) {
send_state(&channel, &sequence, locked).await;
log::info!("Secure desktop state sent: locked={} auto_unlock={}", locked, supported());
sent = Some(locked);
if send_state(&channel, &sequence, locked).await { sent = Some(locked); }
}
tokio::select! {
changed = state.changed() => if changed.is_err() { return; },
+7
View File
@@ -54,6 +54,13 @@ pub struct SignalingClient {
_reconnect_key: Arc<std::sync::Mutex<Option<String>>>,
}
#[cfg(test)]
impl SignalingClient {
pub fn disconnected() -> Arc<Self> {
Arc::new(Self { tx_outgoing: mpsc::channel(1).0, _reconnect_key: Arc::new(std::sync::Mutex::new(None)) })
}
}
fn refreshed_key(ack:&Value)->Option<String> {
let values=ack.as_array()?;
if values.first()?.as_str()? != "success" { return None; }
+7 -4
View File
@@ -274,7 +274,7 @@ fn ffmpeg_spec() -> FfmpegSpec {
}
}
fn ffmpeg_command() -> Command { ffmpeg_spec().command() }
pub(crate) fn ffmpeg_command() -> Command { ffmpeg_spec().command() }
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum FfmpegTimingMode { Modern, Legacy }
@@ -673,7 +673,7 @@ fn wayland_capture_command(executable:&std::path::Path,fps:u32)->Command {
command
}
fn transport_closed(pc: &RTCPeerConnection) -> bool {
pub(crate) fn transport_closed(pc: &RTCPeerConnection) -> bool {
matches!(pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed)
|| pc.sctp().state() == webrtc::sctp_transport::sctp_transport_state::RTCSctpTransportState::Closed
}
@@ -805,7 +805,10 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
&& pc.sctp().state() == webrtc::sctp_transport::sctp_transport_state::RTCSctpTransportState::Connected { break; }
tokio::time::sleep(Duration::from_millis(100)).await;
}
crate::capture::keep_display_awake()?;
// Portal power/idle policy belongs to its CONTROL-scoped supervisor,
// including when video has no frames or the user locks manually.
if portal.is_none() { crate::capture::keep_display_awake()?; }
let portal_stream = portal.is_some();
let (latest, mut receiver) = mpsc::channel(4);
let budget=Arc::new(Semaphore::new(4));
let sequence = Arc::new(std::sync::atomic::AtomicU64::new(0));
@@ -892,7 +895,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
max_send_ms=max_send_ms.max(send_started.elapsed().as_millis());
frames += 1;
if stats.elapsed() >= Duration::from_secs(30) {
crate::capture::keep_display_awake()?;
if !portal_stream { crate::capture::keep_display_awake()?; }
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);
+306 -42
View File
@@ -25,6 +25,15 @@ use webrtc::dtls_transport::dtls_role::DTLSRole;
use webrtc::track::track_local::track_local_static_sample::TrackLocalStaticSample;
use webrtc::track::track_local::TrackLocalWriter;
fn is_capture_setting_message(data: &[u8]) -> bool {
use crate::protocol::WireValue;
let Ok(fields) = crate::protocol::decode_wire(data) else { return false; };
fields.iter().find_map(|field| match (field.number, &field.value) {
(21, WireValue::Bytes(rpc)) => crate::protocol::decode_wire(rpc).ok(),
_ => None,
}).is_some_and(|rpc| rpc.iter().any(|field| matches!(field.number, 2 | 3 | 4 | 6)))
}
#[derive(Debug, Default, Clone)]
pub struct SessionContext {
pub client_id: Option<String>,
@@ -48,6 +57,7 @@ impl WebRtcSession {
signaling_client: Arc<crate::signaling::SignalingClient>,
desktop: bool,
transport:crate::session_ice::SessionIce,
portal_desktop: Option<Arc<crate::desktop_session::PortalDesktop>>,
) -> Result<Self, String> {
let mut m = MediaEngine::default();
m.register_default_codecs()
@@ -222,7 +232,7 @@ impl WebRtcSession {
});
// Desktop sound on the viewer's audio m-line (it always offers Opus).
if desktop && crate::audio::enabled() {
if desktop && crate::audio::enabled() && !cfg!(test) {
let audio_track = Arc::new(TrackLocalStaticSample::new(
RTCRtpCodecCapability { mime_type: "audio/opus".to_owned(), clock_rate: 48000, channels: 2, ..Default::default() },
"audio_0".to_owned(), "audio_0".to_owned(),
@@ -247,7 +257,7 @@ impl WebRtcSession {
// Per-connection mute / lock-after-session settings (安全 menu).
let peer_actions = Arc::new(crate::system_actions::PeerActions::default());
let desktop_sound = desktop && crate::audio::enabled();
if desktop {
if desktop && !cfg!(test) {
let (end_pc, actions) = (pc.clone(), peer_actions.clone());
tokio::spawn(async move {
{
@@ -269,12 +279,32 @@ impl WebRtcSession {
});
}
let portal=capture.portal.clone();
let stream_desktop = portal_desktop.clone();
// File/terminal/port-mapping connections carry no desktop video.
let control_video_track = video_track.clone();
let stream_settings = video_settings.clone();
if desktop {
if desktop && !cfg!(test) {
let state_pc = streaming_pc.clone();
tokio::spawn(async move {
let screen = if let Some(desktop) = stream_desktop {
if let Err(error) = crate::locked_video::run(&streaming_pc, &video_track, &desktop).await {
log::warn!("Locked desktop video unavailable: {error}");
}
let mut updates = desktop.subscribe();
loop {
let state = updates.borrow_and_update().clone();
if state.ready() { break Some(state.screen); }
if !state.waiting { return; }
if crate::video::transport_closed(&streaming_pc) { return; }
tokio::select! {
changed = updates.changed() => if changed.is_err() { return; },
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
}
}
} else { None };
let (capture_size, capture_display, portal) = screen.as_ref().map_or(
(capture_size, capture_display, portal),
|screen| ((screen.width, screen.height), screen.display.clone(), screen.portal.clone()));
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, stream_settings, idr_request).await {
// A send racing the viewer's close fails with a misleading track error.
if matches!(state_pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed) {
@@ -289,12 +319,25 @@ impl WebRtcSession {
}
let input_clone = input.clone();
let control_capture = capture.clone();
let desktop_pc = Arc::downgrade(&pc);
let control_sequence = Arc::new(std::sync::atomic::AtomicU64::new(1));
// Capability belongs to the granted session, including while GNOME is
// locked. Echo can precede TEXT readiness and CONTROL auto-unlock;
// advertising false here would disable clipboard for the entire peer.
let portal_clipboard_supported = desktop && capture.portal.as_ref().is_some_and(|session| session.clipboard_supported());
let portal_clipboard_supported = desktop && portal_desktop.as_ref().map_or_else(
|| capture.portal.as_ref().is_some_and(|session| session.clipboard_supported()),
|manager| {
let state = manager.snapshot();
state.ready()
&& state.screen.portal.as_ref().is_some_and(|session| session.clipboard_supported())
});
let text_clipboard_ready = Arc::new(std::sync::atomic::AtomicBool::new(portal_clipboard_supported));
let last_echo_request = Arc::new(std::sync::atomic::AtomicU64::new(0));
let echo_seen = Arc::new(std::sync::atomic::AtomicBool::new(false));
// Same watcher the desktop session uses: the manager's when Portal owns the
// session, otherwise the global lock watcher. Unknown state fails closed below.
let channel_lock_state = portal_desktop.as_ref().map_or_else(
crate::session_lock::watch_locked, |manager| manager.lock_state());
// One file-transfer session per peer. The viewer may send FileTransferFtp RPCs on CONTROL,
// but it receives file traffic only through streamerDidReceiveFileData, so every reply and
// host-initiated request goes out on FILE_DATA_CHANNEL.
@@ -337,8 +380,13 @@ impl WebRtcSession {
let input_inst = input_clone.clone();
let channel_input_session=input_session.clone();
let screen = control_capture.clone();
let portal_desktop = portal_desktop.clone();
let desktop_pc = desktop_pc.clone();
let out_sequence = control_sequence.clone();
let shared_clipboard_ready = text_clipboard_ready.clone();
let last_echo_request = last_echo_request.clone();
let echo_seen = echo_seen.clone();
let channel_lock_state = channel_lock_state.clone();
let peer_files = peer_files.clone();
let peer_port_mapping = port_mapping.clone();
let peer_video_settings = video_settings.clone();
@@ -350,7 +398,6 @@ impl WebRtcSession {
let lbl = label.clone();
let inp = input_inst.clone();
let d_reply = d.clone();
let input_size = (screen.width, screen.height);
let reply_sequence = out_sequence.clone();
let channel_video_settings = peer_video_settings.clone();
let channel_actions = peer_actions.clone();
@@ -378,6 +425,88 @@ impl WebRtcSession {
}
});
}
let (capture_tx, mut capture_rx) = tokio::sync::mpsc::channel::<Vec<u8>>(16);
if label == "CONTROL_DATA_CHANNEL" {
let settings = channel_video_settings.clone();
let track = peer_video_track.clone();
let response_tx = outbound.clone();
let sequence = reply_sequence.clone();
let manager = portal_desktop.clone();
let channel = Arc::downgrade(&d);
let initial_screen = screen.clone();
tokio::spawn(async move {
let mut health = tokio::time::interval(std::time::Duration::from_millis(100));
loop {
let request = tokio::select! {
request = capture_rx.recv() => match request { Some(request) => request, None => break },
_ = health.tick() => {
if channel.upgrade().is_none_or(|channel| matches!(
channel.ready_state(),
webrtc::data_channel::data_channel_state::RTCDataChannelState::Closing
| webrtc::data_channel::data_channel_state::RTCDataChannelState::Closed
)) { break; }
continue;
}
};
let active_screen = if desktop {
if let Some(manager) = &manager {
loop {
let state = manager.snapshot();
if !state.waiting && state.screen.init_error.is_some() {
log::warn!("Capture setting request dropped: Portal desktop unavailable");
break None;
}
let live = state.screen.init_error.is_none()
&& (cfg!(test) || state.screen.portal.as_ref().is_some_and(|portal| portal.is_alive()))
&& *manager.lock_state().borrow() == Some(false);
if live { break Some(state.screen); }
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
if channel.upgrade().is_none_or(|channel| channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open) { return; }
}
} else {
Some(initial_screen.clone())
}
} else {
Some(initial_screen.clone())
};
let Some(active_screen) = active_screen else { continue; };
let input_size = if active_screen.init_error.is_none() {
(active_screen.width, active_screen.height)
} else { (1920, 1080) };
let Ok(fields) = crate::protocol::decode_wire(&request) else { continue; };
use crate::protocol::WireValue;
let Some(rpc) = fields.iter().find_map(|f| match (f.number, &f.value) {
(21, WireValue::Bytes(rpc)) => Some(*rpc), _ => None,
}) else { continue; };
let Ok(rpc_fields) = crate::protocol::decode_wire(rpc) else { continue; };
let before = *settings.borrow();
let output_before = before.output;
let revision = track.revision();
let Some(response) = crate::capture_setting::handle_rpc_with_codecs(
&rpc_fields, &settings, input_size,
track.supports(crate::capture_setting::VideoCodec::H264),
track.supports(crate::capture_setting::VideoCodec::H265)) else { continue; };
let codec_changed = settings.borrow().codec != before.codec;
if desktop && codec_changed && !track.wait_for_applied(revision, &settings).await {
log::warn!("Codec switch did not produce a transmitted frame; success reply withheld");
continue;
}
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default().as_secs();
let next = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let _ = response_tx.send(crate::initialization::envelope(next(), timestamp, 22, &response));
let current = *settings.borrow();
if current.output != output_before {
match crate::capture_setting::screen_sources(next(), timestamp, input_size, current.output, "Linux desktop") {
Ok(screens) => { let _ = response_tx.send(screens); }
Err(error) => log::warn!("Screen sources update failed: {}", error),
}
}
let sync = crate::capture_setting::sync_payload(&current, input_size);
let _ = response_tx.send(crate::initialization::envelope(next(), timestamp, 25, &sync));
}
});
}
// The viewer sends FileTransferFtp RPCs (Message 21/22) on CONTROL; accept them on TEXT/FILE too.
let file_session = if matches!(label.as_str(), "CONTROL_DATA_CHANNEL" | "TEXT_DATA_CHANNEL" | "FILE_DATA_CHANNEL") {
Some(peer_files.clone())
@@ -391,23 +520,59 @@ impl WebRtcSession {
let initial_sequence = out_sequence.clone();
let clipboard_sink = clipboard_input.clone();
let clipboard_ready = clipboard_enabled.clone();
let last_echo_request = last_echo_request.clone();
let echo_seen = echo_seen.clone();
let rich_outgoing=rich_state.clone();
let open_actions = channel_actions.clone();
let open_video = channel_video_settings.clone();
let open_track = peer_video_track.clone();
let open_desktop = portal_desktop.clone();
let open_owner = channel_input_session.clone();
let open_screen = screen.clone();
d.on_open(Box::new(move || {
let init_actions = open_actions.clone();
let init_video = open_video.clone();
let init_track = open_track.clone();
let channel = channel.clone();
let sequence = initial_sequence.clone();
let screen = screen.clone();
let screen = open_screen.clone();
let clipboard_sink = clipboard_sink.clone();
let clipboard_ready = clipboard_ready.clone();
let rich_outgoing=rich_outgoing.clone();
let portal_desktop = open_desktop.clone();
let owner = open_owner.clone();
let pc = desktop_pc.upgrade();
Box::pin(async move {
// Permission dialogs and first-frame waits must not occupy the
// channel-open callback or delay CONTROL message dispatch.
tokio::spawn(async move {
if channel.label() == "TEXT_DATA_CHANNEL" {
if !desktop { return; }
let screen = if let Some(desktop) = &portal_desktop {
let mut updates = desktop.subscribe();
let Some(_) = crate::desktop_session::wait_ready(&mut updates, &channel).await else { return; };
let mut locked = desktop.lock_state();
loop {
let state = desktop.snapshot();
if !state.waiting && state.screen.init_error.is_some() { return; }
if let Some(session) = state.screen.portal.as_ref() {
if session.is_alive() && *locked.borrow() == Some(false) {
break state.screen;
}
}
tokio::select! {
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {},
changed = locked.changed() => if changed.is_err() { return; },
_ = async {
loop {
if matches!(channel.ready_state(), webrtc::data_channel::data_channel_state::RTCDataChannelState::Closing
| webrtc::data_channel::data_channel_state::RTCDataChannelState::Closed) { return; }
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
} => return,
}
}
} else { screen };
let bridge = if let Some(session)=screen.portal.clone() { crate::clipboard::start_portal(session) } else { crate::clipboard::start(screen.display.clone()) };
*clipboard_sink.lock().unwrap() = Some(bridge.incoming);
let clipboard_channel = channel.clone();
@@ -452,6 +617,92 @@ impl WebRtcSession {
});
return;
}
let early_desktop_selection = desktop && portal_desktop.is_some();
if early_desktop_selection {
// Android 4.43.0's unlock presenter requires CaptureChange
// CT_DESKTOP/CT_WINDOW as well as SecureDesktopState before
// sending UnlockRemoteSystem, even with no video frames.
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let message = crate::initialization::encode_capture_change(
sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
timestamp, 0, 0, "Linux desktop").expect("desktop capture type");
if let Err(error) = channel.send(&bytes::Bytes::from(message)).await {
log::error!("CONTROL desktop selection failed: {error}");
return;
}
log::info!("CONTROL initialization field 8 sent (desktop selection before capture)");
}
if desktop {
// Report secure-desktop state before waiting for capture,
// so AUTO_UNLOCK_SYSTEM can arrive on this same CONTROL.
let state = portal_desktop.as_ref().map_or_else(
crate::session_lock::watch_locked, |desktop| desktop.lock_state());
tokio::spawn(crate::session_lock::report_states(channel.clone(), sequence.clone(), state));
}
if let Some(manager) = &portal_desktop {
let mut updates = manager.subscribe();
let channel = channel.clone();
let sequence = sequence.clone();
let echo_request = last_echo_request.clone();
let clipboard_ready = clipboard_ready.clone();
tokio::spawn(async move {
let mut health = tokio::time::interval(std::time::Duration::from_millis(100));
loop {
tokio::select! {
changed = updates.changed() => if changed.is_err() { break; },
_ = health.tick() => {
if channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open { break; }
continue;
}
}
let state = updates.borrow_and_update().clone();
let supported = state.ready()
&& state.screen.portal.as_ref().is_some_and(|portal| portal.clipboard_supported());
clipboard_ready.store(supported, std::sync::atomic::Ordering::Release);
// echo_seen is published after the request seq, so reading it
// first guarantees a non-stale sequence for the replay.
if supported && echo_seen.load(std::sync::atomic::Ordering::Acquire) {
let request_seq = echo_request.load(std::sync::atomic::Ordering::Acquire);
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let response = crate::control_protocol::encode_echo_response_with_clipboard(
sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
timestamp, request_seq, true);
let _ = channel.send(&bytes::Bytes::from(response)).await;
}
if !state.waiting && state.screen.init_error.is_some() { break; }
}
});
}
let screen = if let Some(portal) = &portal_desktop {
if !cfg!(test) {
if let (Some(owner), Some(pc)) = (owner, pc) {
tokio::spawn(portal.clone().run(pc, channel.clone(), owner));
}
}
let snapshot = portal.snapshot();
if !snapshot.ready() {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let message = crate::initialization::envelope(
sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 13,
&init_actions.metrics_payload(desktop_sound, snapshot.permission(true)));
if channel.send(&bytes::Bytes::from(message)).await.is_err() { return; }
let mut updates = portal.subscribe();
match crate::desktop_session::wait_ready(&mut updates, &channel).await {
Some(screen) => screen,
None => {
if channel.ready_state() == webrtc::data_channel::data_channel_state::RTCDataChannelState::Open {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let message = crate::initialization::envelope(
sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 13,
&init_actions.metrics_payload(desktop_sound, crate::system_metrics::Permission::Reject));
let _ = channel.send(&bytes::Bytes::from(message)).await;
}
return;
}
}
} else { snapshot.screen }
} else { screen };
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let next_seq = || sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -480,6 +731,7 @@ impl WebRtcSession {
log::info!("Desktop codec selected: {:?}", codec); current.codec = codec; true } else { false });
}
for (field, message) in messages {
if early_desktop_selection && field == 8 { continue; }
if (!desktop || screen.init_error.is_some()) && field == 18 { continue; }
if desktop && screen.init_error.is_some() && matches!(field,7|8) { continue; }
let message = match message {
@@ -494,7 +746,6 @@ impl WebRtcSession {
}
// Mirror the Linux pointer shape; the video itself carries no pointer.
if !desktop { return; }
tokio::spawn(crate::session_lock::report_states(channel.clone(), sequence.clone()));
let mut shapes = crate::cursor::monitor(&screen.display);
let mut health = tokio::time::interval(std::time::Duration::from_millis(200));
let mut sent = 0usize;
@@ -518,12 +769,19 @@ impl WebRtcSession {
}
}
}
});
})
}));
}
d.on_message(Box::new(move |msg: DataChannelMessage| {
// Keep the whole state: a cached screen waiting for a closed Portal is not ready for input.
let (screen, desktop_ready) = portal_desktop.as_ref().map_or_else(|| (screen.clone(), true), |desktop| {
let state = desktop.snapshot();
let ready = state.ready();
(state.screen, ready)
});
let channel_name = lbl.clone();
let input_worker = inp.clone();
let message_input_session=channel_input_session.clone();
@@ -537,43 +795,29 @@ impl WebRtcSession {
let rich_incoming=rich_state.clone();
let file_worker = file_session.clone();
let mapping = peer_port_mapping.clone();
let video_settings = channel_video_settings.clone();
let negotiated_track = peer_video_track.clone();
let actions = channel_actions.clone();
let portal_for_message = portal_desktop.clone();
let lock_for_message = channel_lock_state.clone();
let echo_for_message = last_echo_request.clone();
let echo_seen_for_message = echo_seen.clone();
let capture_requests = capture_tx.clone();
Box::pin(async move {
let input_size = (screen.width, screen.height);
if let Some(term) = &terminal_worker {
if crate::terminal::is_frame(&msg.data) { term.handle(&msg.data); return; }
}
if channel_name == "CONTROL_DATA_CHANNEL" && is_capture_setting_message(&msg.data) {
// Back-pressure like the old inline handler instead of dropping the latest setting.
if capture_requests.send(msg.data.to_vec()).await.is_err() {
log::warn!("Capture setting queue closed; request dropped");
}
return;
}
if let Ok(fields) = crate::protocol::decode_wire(&msg.data) {
use crate::protocol::WireValue;
let rpc = fields.iter().find_map(|f| match (f.number, &f.value) { (21, WireValue::Bytes(b)) => Some(*b), _ => None });
let rpc_fields = rpc.and_then(|r| crate::protocol::decode_wire(r).ok());
let before = *video_settings.borrow();
let output_before = before.output;
let revision = negotiated_track.revision();
if let Some(response) = rpc_fields.as_deref().and_then(|r| crate::capture_setting::handle_rpc_with_codecs(r, &video_settings, input_size, negotiated_track.supports(crate::capture_setting::VideoCodec::H264), negotiated_track.supports(crate::capture_setting::VideoCodec::H265))) {
let codec_changed = video_settings.borrow().codec != before.codec;
if codec_changed && !negotiated_track.wait_for_applied(revision, &video_settings).await {
// Official host replies after startVideoCaptureAndSendRpcResponse.
// Do not fabricate a codec error number or acknowledge an encoder that never started.
log::warn!("Codec switch did not produce a transmitted frame; success reply withheld");
return;
}
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let next = || out_sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let _ = rpc_out.send(crate::initialization::envelope(next(), ts, 22, &response));
let current = *video_settings.borrow();
if current.output != output_before {
match crate::capture_setting::screen_sources(next(), ts, input_size, current.output, "Linux desktop") {
Ok(screens) => { let _ = rpc_out.send(screens); },
Err(e) => log::warn!("Screen sources update failed: {}", e),
}
}
let sync = crate::capture_setting::sync_payload(&current, input_size);
let _ = rpc_out.send(crate::initialization::envelope(next(), ts, 25, &sync));
return;
}
if let Some(response) = rpc_fields.as_deref().and_then(|r| crate::system_actions::plugin_response(r).or_else(|| crate::system_actions::session_rpc_response(r))) {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let _ = rpc_out.send(crate::initialization::envelope(out_sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 22, &response));
@@ -722,10 +966,17 @@ impl WebRtcSession {
// QuerySystemState { 1: SS_ID_SECURE_DESKTOP_STATE = 1 } asks for the lock state now.
let query = fields.iter().find_map(|f| match (&f.value, f.number) { (WireValue::Bytes(v), 16) => Some(*v), _ => None });
if let Some(query) = query {
let id = crate::protocol::decode_wire(query).ok().and_then(|q| q.iter().find_map(|f| match (&f.value, f.number) { (WireValue::Varint(v), 1) => Some(*v), _ => None }));
let query_fields = crate::protocol::decode_wire(query).ok();
let id = query_fields.as_ref().and_then(|q| q.iter().find_map(|f| match (&f.value, f.number) { (WireValue::Varint(v), 1) => Some(*v), _ => None }));
let shape = query_fields.as_ref().map(|fields| fields.iter().map(|field| match &field.value {
WireValue::Bytes(bytes) => (field.number, "bytes", bytes.len()),
_ => (field.number, "number", 0),
}).collect::<Vec<_>>());
log::info!("CONTROL system-state query: fields={:?} secure_desktop={}", shape, id == Some(1));
if id == Some(1) {
let locked = *crate::session_lock::watch_locked().borrow();
crate::session_lock::send_state(&reply_chan, &out_sequence, locked.unwrap_or(false)).await;
let locked = *lock_for_message.borrow();
// Unknown (no logind session, e.g. Xvfb) stays unlocked, as before.
crate::session_lock::send_state(&reply_chan, &out_sequence, locked == Some(true)).await;
}
}
if let Some(simple) = simple {
@@ -747,6 +998,7 @@ impl WebRtcSession {
log::info!("SimpleAction {} ({}) received: value={:?} args={}", action,
crate::control_protocol::simple_action_name(action).unwrap_or("?"), value, sa::args_shape(&args));
let (actions, input, chan, seq_out) = (actions.clone(), input_worker.clone(), reply_chan.clone(), out_sequence.clone());
let settings_portal = portal_for_message.clone();
tokio::spawn(async move {
let worker = actions.clone();
let report = tokio::task::spawn_blocking(move || worker.handle(action, value, &args, || {
@@ -756,7 +1008,10 @@ impl WebRtcSession {
})).await.unwrap_or(false);
if report {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
let message = crate::initialization::envelope(seq_out.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 13, &actions.metrics_payload(desktop_sound, crate::system_metrics::Permission::Allow));
let permission = settings_portal.as_ref().map_or(
crate::system_metrics::Permission::Allow,
|manager| manager.snapshot().permission(desktop));
let message = crate::initialization::envelope(seq_out.fetch_add(1, std::sync::atomic::Ordering::Relaxed), ts, 13, &actions.metrics_payload(desktop_sound, permission));
if let Err(e) = chan.send(&bytes::Bytes::from(message)).await { log::warn!("Settings report failed: {}", e); }
}
});
@@ -765,6 +1020,8 @@ impl WebRtcSession {
log::info!("SimpleAction {} ({}) not handled: args={}", action, crate::control_protocol::simple_action_name(action).unwrap_or("?"), sa::args_shape(args));
}
if action == 0 {
echo_for_message.store(seq, std::sync::atomic::Ordering::Release);
echo_seen_for_message.store(true, std::sync::atomic::Ordering::Release);
let response = crate::control_protocol::encode_echo_response_with_clipboard(
out_sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs(), seq,
@@ -772,10 +1029,7 @@ impl WebRtcSession {
if let Err(e) = reply_chan.send(&bytes::Bytes::from(response)).await {
log::error!("CONTROL echo response failed: {}", e);
} else {
static RESPONSES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
if RESPONSES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) == 0 {
log::info!("CONTROL protobuf echo request answered");
}
log::info!("CONTROL protobuf echo request answered");
}
}
}
@@ -788,6 +1042,8 @@ impl WebRtcSession {
if channel_name.contains("CONTROL") {
match crate::control_protocol::decode_control(&msg.data) {
Ok(crate::control_protocol::ControlMessage::Input(event)) => {
if screen.backend == crate::desktop_backend::DesktopBackend::Portal
&& (screen.init_error.is_some() || !desktop_ready || *lock_for_message.borrow() != Some(false)) { return; }
use crate::control_protocol::ControlInput;
// Separate receipt budgets keep motion floods from hiding clicks.
static RECEIPTS: [std::sync::atomic::AtomicUsize; 3] = [const { std::sync::atomic::AtomicUsize::new(0) }; 3];
@@ -1049,6 +1305,14 @@ mod tests {
}
}
#[cfg(test)]
#[path = "desktop_control_tests.rs"]
mod desktop_control_tests;
#[cfg(test)]
#[path = "webrtc_lifecycle_tests.rs"]
mod webrtc_lifecycle_tests;
/// Virtual/overlay interfaces never carry the UU video path (ZeroTier, containers).
fn ice_interface_allowed(name: &str) -> bool {
!["zt", "docker", "br-", "veth", "virbr", "lo"].iter().any(|prefix| name.starts_with(prefix))
+224
View File
@@ -0,0 +1,224 @@
//! CONTROL lifecycle regressions: an early Echo must be answered while the Portal
//! session is still pending, capture settings must be deferred (never dropped, never
//! blocking Echo/unlock) until the desktop is ready, and tool peers must apply
//! settings against the nominal screen without waiting for a desktop at all.
//! Local SCTP peers only; no cloud, desktop consent, audio or power operations.
use super::*;
use crate::protocol::{decode_wire, WireValue};
use std::{collections::HashMap, time::Duration};
use tokio::sync::mpsc;
type Messages = mpsc::UnboundedReceiver<(String, Vec<u8>)>;
struct Viewer {
pc: Arc<RTCPeerConnection>,
channels: HashMap<String, Arc<RTCDataChannel>>,
messages: Messages,
}
async fn connect(desktop: bool, portal: Option<Arc<crate::desktop_session::PortalDesktop>>)
-> (WebRtcSession, Viewer) {
let input = Arc::new(InputInjector::without_desktop());
let screen = Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into()));
let host = WebRtcSession::new(screen, input, crate::signaling::SignalingClient::disconnected(),
desktop, crate::session_ice::SessionIce::local(), portal).await.unwrap();
let pc = Arc::new(APIBuilder::new().build().new_peer_connection(RTCConfiguration::default()).await.unwrap());
let (tx, messages) = mpsc::unbounded_channel();
let mut channels = HashMap::new();
for label in ["CONTROL_DATA_CHANNEL"] {
let channel = pc.create_data_channel(label, None).await.unwrap();
let (tx, name) = (tx.clone(), label.to_string());
channel.on_message(Box::new(move |message| {
let _ = tx.send((name.clone(), message.data.to_vec()));
Box::pin(async {})
}));
channels.insert(label.to_string(), channel);
}
let mut gathered = pc.gathering_complete_promise().await;
pc.set_local_description(pc.create_offer(None).await.unwrap()).await.unwrap();
gathered.recv().await;
host.pc.set_remote_description(pc.local_description().await.unwrap()).await.unwrap();
let mut gathered = host.pc.gathering_complete_promise().await;
host.pc.set_local_description(host.pc.create_answer(None).await.unwrap()).await.unwrap();
gathered.recv().await;
pc.set_remote_description(host.pc.local_description().await.unwrap()).await.unwrap();
tokio::time::timeout(Duration::from_secs(8), async {
while channels.values().any(|channel| channel.ready_state() != webrtc::data_channel::data_channel_state::RTCDataChannelState::Open) {
tokio::time::sleep(Duration::from_millis(20)).await;
}
}).await.expect("local SCTP channels did not open");
(host, Viewer { pc, channels, messages })
}
fn body(message: &[u8], number: u32) -> Option<&[u8]> {
decode_wire(message).ok()?.into_iter().find_map(|field| match (field.number, field.value) {
(n, WireValue::Bytes(bytes)) if n == number => Some(bytes), _ => None,
})
}
fn varint_of(message: &[u8], number: u32) -> Option<u64> {
decode_wire(message).ok()?.into_iter().find_map(|field| match (field.number, field.value) {
(n, WireValue::Varint(value)) if n == number => Some(value), _ => None,
})
}
fn put_varint(mut value: usize, out: &mut Vec<u8>) {
while value >= 128 { out.push((value as u8 & 127) | 128); value >>= 7; }
out.push(value as u8);
}
fn field_bytes(number: u32, value: &[u8]) -> Vec<u8> {
let mut out = Vec::new();
put_varint(((number << 3) | 2) as usize, &mut out);
put_varint(value.len(), &mut out);
out.extend_from_slice(value);
out
}
fn field_varint(number: u32, value: u64, out: &mut Vec<u8>) {
put_varint((number << 3) as usize, out);
put_varint(value as usize, out);
}
/// Message 21 RpcRequest {1 header{1 request_id}, <case>}, the verified viewer shape.
fn capture_rpc(request_id: u64, case: u32, setting: &[u8]) -> Vec<u8> {
let mut header = Vec::new();
field_varint(1, request_id, &mut header);
let mut rpc = field_bytes(1, &header);
rpc.extend(field_bytes(case, setting));
crate::initialization::envelope(11, 0, 21, &rpc)
}
/// Message 3 SimpleAction action 0: the viewer's FeatureFlag Echo request.
fn echo_request(seq: u64) -> Vec<u8> {
crate::initialization::envelope(seq, 0, 3, &[8, 0])
}
async fn receive(messages: &mut Messages, field: u32, within: Duration) -> Option<Vec<u8>> {
tokio::time::timeout(within, async {
loop {
let (channel, message) = messages.recv().await?;
if channel == "CONTROL_DATA_CHANNEL" && body(&message, field).is_some() { return Some(message); }
}
}).await.unwrap_or(None)
}
/// The Echo response advertises ff_clipboard (FeatureFlag field 8) only when true.
fn echo_advertises_clipboard(message: &[u8]) -> bool {
body(message, 3).and_then(|action| decode_wire(action).ok()).is_some_and(|action| {
action.iter().any(|field| match (field.number, &field.value) {
(4, WireValue::Bytes(flags)) => decode_wire(flags).ok().is_some_and(|flags| {
flags.iter().any(|flag| flag.number == 8)
}),
_ => false,
})
})
}
fn ready_screen() -> Arc<ScreenCapture> {
let mut screen = ScreenCapture::unavailable("synthetic ready desktop".into());
screen.backend = crate::desktop_backend::DesktopBackend::Portal;
screen.init_error = None;
screen.display = "wayland:test".into();
screen.width = 1920;
screen.height = 1080;
Arc::new(screen)
}
/// A Portal screen whose session reports clipboard support, for the refresh path.
/// `PortalSession` is a real native handle type, so this uses the manager's own
/// test publish hook with a session-less screen and asserts the negative path:
/// a ready screen without a Portal session must not advertise ff_clipboard.
#[tokio::test]
async fn ready_without_portal_session_does_not_advertise_clipboard() {
tokio::time::timeout(Duration::from_secs(20), async {
let (_, locked) = tokio::sync::watch::channel(Some(false));
let portal = Arc::new(crate::desktop_session::PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())), locked));
let (host, mut viewer) = connect(true, Some(portal.clone())).await;
// Establish the early Echo while the desktop is still pending: no clipboard claim.
viewer.channels["CONTROL_DATA_CHANNEL"].send(&echo_request(3).into()).await.unwrap();
let echo = receive(&mut viewer.messages, 3, Duration::from_secs(3)).await.expect("early Echo");
assert!(!echo_advertises_clipboard(&echo));
// Readiness with no Portal session keeps the capability false and must not
// manufacture an unsolicited Echo response from a timer tick.
portal.publish_test_screen(ready_screen());
assert!(receive(&mut viewer.messages, 3, Duration::from_millis(800)).await.is_none(),
"Echo was re-sent without an established clipboard capability");
viewer.pc.close().await.unwrap();
host.pc.close().await.unwrap();
}).await.expect("clipboard capability regression exceeded deadline");
}
#[tokio::test]
async fn pending_portal_answers_echo_immediately_and_defers_capture_settings() {
tokio::time::timeout(Duration::from_secs(30), async {
let (_, locked) = tokio::sync::watch::channel(Some(false));
let portal = Arc::new(crate::desktop_session::PortalDesktop::with_lock_state(
Arc::new(ScreenCapture::unavailable("synthetic pending desktop".into())), locked));
let (host, mut viewer) = connect(true, Some(portal.clone())).await;
// Echo must be answered while the placeholder screen is still pending, and it
// must not claim a clipboard bridge that is not established yet.
viewer.channels["CONTROL_DATA_CHANNEL"].send(&echo_request(7).into()).await.unwrap();
let echo = receive(&mut viewer.messages, 3, Duration::from_secs(3)).await.expect("early Echo was not answered while Portal was pending");
assert!(!echo_advertises_clipboard(&echo));
let args = body(&echo, 3).and_then(|action| decode_wire(action).ok())
.and_then(|action| action.into_iter().find_map(|field| match (field.number, field.value) {
(2, WireValue::Bytes(bytes)) => Some(bytes), _ => None,
}));
assert_eq!(args, Some(br#"{"seq":7}"#.as_slice()));
// A capture setting arriving during the same pending window is queued, not dropped.
let mut setting = Vec::new();
field_varint(2, 2, &mut setting); // quality = General (高清 2M)
field_varint(18, 30, &mut setting); // fpsCount = 30
field_varint(5, 1920, &mut setting);
field_varint(6, 1080, &mut setting);
viewer.channels["CONTROL_DATA_CHANNEL"].send(&capture_rpc(77, 2, &setting).into()).await.unwrap();
assert!(receive(&mut viewer.messages, 22, Duration::from_millis(700)).await.is_none(),
"capture setting answered before the desktop was ready");
// Readiness publishes the real geometry; the queued request is applied and answered.
let mut screen = ready_screen();
let native = Arc::get_mut(&mut screen).unwrap();
native.width = 3840;
native.height = 2160;
portal.publish_test_screen(screen);
let response = receive(&mut viewer.messages, 22, Duration::from_secs(5)).await.expect("deferred capture setting was never answered");
let header = body(&response, 22).and_then(|r| body(r, 1)).expect("RpcResponse header");
assert_eq!(varint_of(header, 1), Some(77), "deferred reply lost the request id");
let inventory = receive(&mut viewer.messages, 7, Duration::from_secs(5)).await.expect("updated ScreenSources");
let display = body(&inventory, 7).and_then(|sources| body(sources, 1)).expect("display");
// v4 advertises pixels even when the recovered desktop's native size differs.
for (field, size) in [(4, (1920, 1080)), (6, (3840, 2160))] {
let rect = body(display, field).expect("display rectangle");
assert_eq!((varint_of(rect, 3), varint_of(rect, 4)), (Some(size.0), Some(size.1)));
assert_eq!((varint_of(rect, 5), varint_of(rect, 6)), (Some(size.0), Some(size.1)));
}
assert!(decode_wire(display).unwrap().iter().any(|field|
field.number == 8 && field.value == WireValue::Fixed64(1.0f64.to_le_bytes())));
let sync = receive(&mut viewer.messages, 25, Duration::from_secs(5)).await.expect("CaptureSettingSync");
let sync = body(&sync, 25).expect("CaptureSettingSync payload");
assert_eq!(varint_of(sync, 1), Some(30));
assert_eq!((varint_of(sync, 5), varint_of(sync, 6)), (Some(1920), Some(1080)));
// CONTROL still serves Echo after readiness, and no timer-driven flood appears.
viewer.channels["CONTROL_DATA_CHANNEL"].send(&echo_request(9).into()).await.unwrap();
assert!(receive(&mut viewer.messages, 3, Duration::from_secs(3)).await.is_some());
assert!(receive(&mut viewer.messages, 3, Duration::from_millis(700)).await.is_none(),
"Echo was re-sent without a capability change");
viewer.pc.close().await.unwrap();
host.pc.close().await.unwrap();
}).await.expect("pending-Portal lifecycle regression exceeded deadline");
}
#[tokio::test]
async fn tool_peer_applies_capture_settings_without_a_desktop() {
tokio::time::timeout(Duration::from_secs(20), async {
let (host, mut viewer) = connect(false, None).await;
let mut setting = Vec::new();
field_varint(1, 1280, &mut setting);
field_varint(2, 720, &mut setting);
viewer.channels["CONTROL_DATA_CHANNEL"].send(&capture_rpc(5, 6, &setting).into()).await.unwrap();
let response = receive(&mut viewer.messages, 22, Duration::from_secs(3)).await.expect("tool capture setting was not answered");
let header = body(&response, 22).and_then(|r| body(r, 1)).expect("RpcResponse header");
assert_eq!(varint_of(header, 1), Some(5));
let sync = receive(&mut viewer.messages, 25, Duration::from_secs(3)).await.expect("CaptureSettingSync");
let sync = body(&sync, 25).expect("CaptureSettingSync payload");
assert_eq!((varint_of(sync, 5), varint_of(sync, 6)), (Some(1280), Some(720)));
viewer.pc.close().await.unwrap();
host.pc.close().await.unwrap();
}).await.expect("tool capture-setting regression exceeded deadline");
}
+244
View File
@@ -0,0 +1,244 @@
#define _GNU_SOURCE
#include <assert.h>
#include <gio/gio.h>
#include <string.h>
#include "../c_engine/desktop_power.c"
static const char *introspection_xml =
"<node>"
" <interface name='org.freedesktop.DBus.Properties'>"
" <method name='Set'>"
" <arg type='s' direction='in'/><arg type='s' direction='in'/>"
" <arg type='v' direction='in'/>"
" </method>"
" </interface>"
" <interface name='org.gnome.SessionManager'>"
" <method name='Inhibit'>"
" <arg type='s' direction='in'/><arg type='u' direction='in'/>"
" <arg type='s' direction='in'/><arg type='u' direction='in'/>"
" <arg type='u' direction='out'/>"
" </method>"
" <method name='Uninhibit'>"
" <arg type='u' direction='in'/>"
" </method>"
" </interface>"
"</node>";
static GMutex state_lock;
static GCond ready_cond;
static gboolean service_ready;
static gboolean fail_uninhibit;
static unsigned wake_calls, inhibit_calls, uninhibit_calls;
static guint32 last_flags, last_cookie;
static char *wake_sender, *inhibit_sender, *uninhibit_sender;
static GDBusConnection *service_connection;
static GMainLoop *service_loop;
static void replace_string(char **target, const char *value) {
g_free(*target);
*target = g_strdup(value);
}
static void method_call(GDBusConnection *connection, const gchar *sender, const gchar *path,
const gchar *interface, const gchar *method, GVariant *parameters,
GDBusMethodInvocation *invocation, gpointer data) {
(void)connection; (void)path; (void)data;
g_mutex_lock(&state_lock);
if (!strcmp(interface, "org.freedesktop.DBus.Properties") && !strcmp(method, "Set")) {
const char *target_interface, *property;
GVariant *boxed, *value;
g_variant_get(parameters, "(&s&s@v)", &target_interface, &property, &boxed);
value = g_variant_get_variant(boxed);
assert(!strcmp(target_interface, "org.gnome.Mutter.DisplayConfig"));
assert(!strcmp(property, "PowerSaveMode"));
assert(g_variant_is_of_type(value, G_VARIANT_TYPE_INT32));
assert(g_variant_get_int32(value) == 0);
wake_calls++;
replace_string(&wake_sender, sender);
g_variant_unref(value);
g_variant_unref(boxed);
g_mutex_unlock(&state_lock);
g_dbus_method_invocation_return_value(invocation, g_variant_new("()"));
return;
}
if (!strcmp(interface, "org.gnome.SessionManager") && !strcmp(method, "Inhibit")) {
const char *app, *reason;
guint32 xid, flags;
g_variant_get(parameters, "(&su&su)", &app, &xid, &reason, &flags);
assert(!strcmp(app, "uu-remote-linux"));
assert(xid == 0);
assert(!strcmp(reason, "Remote desktop session is active"));
inhibit_calls++;
last_flags = flags;
replace_string(&inhibit_sender, sender);
g_mutex_unlock(&state_lock);
g_dbus_method_invocation_return_value(invocation, g_variant_new("(u)", 73u));
return;
}
if (!strcmp(interface, "org.gnome.SessionManager") && !strcmp(method, "Uninhibit")) {
guint32 cookie;
g_variant_get(parameters, "(u)", &cookie);
uninhibit_calls++;
last_cookie = cookie;
replace_string(&uninhibit_sender, sender);
gboolean fail = fail_uninhibit;
g_mutex_unlock(&state_lock);
if (fail) g_dbus_method_invocation_return_dbus_error(invocation,
"org.freedesktop.DBus.Error.Failed", "synthetic failure");
else g_dbus_method_invocation_return_value(invocation, g_variant_new("()"));
return;
}
g_mutex_unlock(&state_lock);
g_dbus_method_invocation_return_dbus_error(invocation,
"org.freedesktop.DBus.Error.UnknownMethod", "unexpected method");
}
static const GDBusInterfaceVTable vtable = { .method_call = method_call };
static GDBusConnection *connect_bus(const char *address) {
GError *error = NULL;
GDBusConnection *connection = g_dbus_connection_new_for_address_sync(address,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
NULL, NULL, &error);
assert(connection && !error);
g_dbus_connection_set_exit_on_close(connection, FALSE);
return connection;
}
static void request_name(GDBusConnection *connection, const char *name) {
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(connection, "org.freedesktop.DBus",
"/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName",
g_variant_new("(su)", name, 4u), G_VARIANT_TYPE("(u)"),
G_DBUS_CALL_FLAGS_NONE, 3000, NULL, &error);
assert(reply && !error);
guint32 result;
g_variant_get(reply, "(u)", &result);
assert(result == 1);
g_variant_unref(reply);
}
static gpointer run_service(gpointer data) {
const char *address = data;
GMainContext *context = g_main_context_new();
g_main_context_push_thread_default(context);
service_connection = connect_bus(address);
GError *error = NULL;
GDBusNodeInfo *info = g_dbus_node_info_new_for_xml(introspection_xml, &error);
assert(info && !error);
assert(g_dbus_connection_register_object(service_connection, MUTTER_PATH,
info->interfaces[0], &vtable, NULL, NULL, &error));
assert(!error);
assert(g_dbus_connection_register_object(service_connection, SESSION_PATH,
info->interfaces[1], &vtable, NULL, NULL, &error));
assert(!error);
request_name(service_connection, MUTTER_DEST);
request_name(service_connection, SESSION_DEST);
service_loop = g_main_loop_new(context, FALSE);
g_mutex_lock(&state_lock);
service_ready = TRUE;
g_cond_broadcast(&ready_cond);
g_mutex_unlock(&state_lock);
g_main_loop_run(service_loop);
g_dbus_node_info_unref(info);
g_main_loop_unref(service_loop);
g_dbus_connection_close_sync(service_connection, NULL, NULL);
g_object_unref(service_connection);
g_main_context_pop_thread_default(context);
g_main_context_unref(context);
return NULL;
}
static gboolean has_owner(const char *name) {
GError *error = NULL;
GVariant *reply = g_dbus_connection_call_sync(service_connection,
"org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
"GetNameOwner", g_variant_new("(s)", name), G_VARIANT_TYPE("(s)"),
G_DBUS_CALL_FLAGS_NONE, 500, NULL, &error);
if (reply) g_variant_unref(reply);
g_clear_error(&error);
return reply != NULL;
}
static gboolean wait_until_gone(const char *name) {
gint64 deadline = g_get_monotonic_time() + 3 * G_TIME_SPAN_SECOND;
do {
if (!has_owner(name)) return TRUE;
g_usleep(10000);
} while (g_get_monotonic_time() < deadline);
return FALSE;
}
static char *copy_sender(unsigned which) {
g_mutex_lock(&state_lock);
char *sender = which == 0 ? wake_sender : which == 1 ? inhibit_sender : uninhibit_sender;
char *copy = g_strdup(sender);
g_mutex_unlock(&state_lock);
return copy;
}
int main(void) {
g_mutex_init(&state_lock);
g_cond_init(&ready_cond);
GTestDBus *bus = g_test_dbus_new(G_TEST_DBUS_NONE);
g_test_dbus_up(bus);
GThread *thread = g_thread_new("fake-session-bus", run_service,
(gpointer)g_test_dbus_get_bus_address(bus));
g_mutex_lock(&state_lock);
while (!service_ready) g_cond_wait(&ready_cond, &state_lock);
g_mutex_unlock(&state_lock);
assert(uu_desktop_power_wake() == 0);
char *wake_owner = copy_sender(0);
assert(wake_owner && wait_until_gone(wake_owner));
g_free(wake_owner);
int error = 0;
struct uu_idle_inhibitor *first = uu_desktop_idle_inhibitor_acquire(&error);
assert(first && error == 0);
char *first_owner = copy_sender(1);
assert(first_owner && has_owner(first_owner));
struct uu_idle_inhibitor *second = uu_desktop_idle_inhibitor_acquire(&error);
assert(second && error == 0);
char *second_owner = copy_sender(1);
assert(second_owner && strcmp(first_owner, second_owner));
assert(has_owner(first_owner) && has_owner(second_owner));
uu_desktop_idle_inhibitor_release(first);
g_mutex_lock(&state_lock);
assert(inhibit_calls == 2 && last_flags == 8);
assert(uninhibit_calls == 1 && last_cookie == 73);
g_mutex_unlock(&state_lock);
assert(wait_until_gone(first_owner) && has_owner(second_owner));
g_mutex_lock(&state_lock);
fail_uninhibit = TRUE;
g_mutex_unlock(&state_lock);
uu_desktop_idle_inhibitor_release(second);
g_mutex_lock(&state_lock);
assert(uninhibit_calls == 2);
assert(uninhibit_sender && strcmp(uninhibit_sender, second_owner) == 0);
g_mutex_unlock(&state_lock);
assert(wait_until_gone(second_owner));
g_free(first_owner);
g_free(second_owner);
g_mutex_lock(&state_lock);
g_main_loop_quit(service_loop);
g_mutex_unlock(&state_lock);
g_thread_join(thread);
g_test_dbus_down(bus);
g_object_unref(bus);
g_free(wake_sender);
g_free(inhibit_sender);
g_free(uninhibit_sender);
g_cond_clear(&ready_cond);
g_mutex_clear(&state_lock);
return 0;
}
+31
View File
@@ -185,6 +185,36 @@ static void portal_changed_physical_format_still_fails(void) {
uu_portal_close(p);
}
static int cancel_immediately(void *data) { return *(const int *)data; }
struct cancel_later { int *cancelled; int polls; };
static int cancel_after_polls(void *data) {
struct cancel_later *state = data;
if (state->polls++ >= 2) *state->cancelled = 1;
return *state->cancelled;
}
static void cancellable_waits(void) {
int cancelled = 1, error = 0;
struct uu_portal p = {.cancelled=cancel_immediately, .cancel_data=&cancelled};
pthread_mutex_init(&p.lock, NULL);
pthread_cond_init(&p.changed, NULL);
assert(wait_for_first_frame(&p) == 13);
pthread_cond_destroy(&p.changed);
pthread_mutex_destroy(&p.lock);
GVariantBuilder options;
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
assert(!request(&p, REMOTE, "Start", &options, 2, &error));
assert(error == 13);
g_variant_builder_clear(&options);
/* A cancel arriving during the consent wait must stop it without the full timeout. */
int waiting_cancelled = 0;
struct cancel_later state = {.cancelled=&waiting_cancelled};
struct response response = {0};
struct uu_portal waiting = {.cancelled=cancel_after_polls, .cancel_data=&state};
assert(wait_for_response(&waiting, &response, 120) == 13);
assert(waiting_cancelled);
}
static void clipboard_bounds(void) {
int fds[2]; uint8_t bytes[8]={0};
assert(pipe2(fds,O_NONBLOCK)==0);
@@ -252,6 +282,7 @@ int main(void) {
portal_renegotiation_recovery();
portal_renegotiation_event_order_and_healthy_states();
portal_changed_physical_format_still_fails();
cancellable_waits();
clipboard_bounds();
capture_with_optional_geometry(0);
capture_with_optional_geometry(1);