Compare commits

..
11 Commits
Author SHA1 Message Date
VallierDevandClaude Opus 5.5 46589b3fac Release v0.1.7
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:24:22 +08:00
VallierDevandClaude Opus 5.5 708d9de2de Docs: record Intel VAAPI hardware acceptance; AMD and inproc VAAPI remain unverified
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:22:34 +08:00
VallierDevandClaude Opus 5.5 ebe6a7e19a Clarify mute logs with the virtual sink; document the PipeWire fallback
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:22:20 +08:00
VallierDevandClaude Opus 5.5 09f7169f62 Virtual audio without pactl: PipeWire-native null sink
Default Ubuntu desktops ship PipeWire/WirePlumber without pulseaudio-utils,
so the pactl-only route silently fell back to plain muting. When pactl is
missing, create the null sink with pw-cli (object.linger), switch the default
with wpctl and destroy only our own null-audio-sink node on restore;
WirePlumber moves default-following streams itself. Recovery records carry
the backend (old records default to pactl).

Verified on Ubuntu 24.04 (PipeWire 1.0.5, WirePlumber 0.4.17): a playing
stream moves to the sink and back, the monitor is readable, SIGKILL leftovers
are recovered on the next start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:16:11 +08:00
VallierDevandClaude Opus 5.5 9125e51350 Review fixes: tolerate a vanished default sink, recover stale virtual displays
- virtual_audio: when the previous default output no longer exists, still
  unload the null sink instead of keeping all output on it.
- uu-virtual-desktop: a socket/lock left by a dead Xvfb no longer blocks the
  service forever (Restart=always loop); a live owner is still refused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:03:42 +08:00
VallierDev 0da79a45eb Record VM acceptance results and add guarded synthetic audio fixture 2026-10-15 10:56:54 +08:00
VallierDev d461ef9bed Add opt-in linear KMS capture prototype with explicit security limitations 2026-10-15 10:49:12 +08:00
VallierDev f123f0ba05 Document Arch and Omarchy setup and exercise native desktop input and clipboard 2026-10-15 10:48:45 +08:00
VallierDev 90c33e173c Supervise authenticated virtual desktops on the linger user bus 2026-10-15 10:48:07 +08:00
VallierDev ac0f2bf2c6 Route muted sessions through a recoverable Pulse null sink 2026-10-15 10:47:31 +08:00
VallierDev 8a8a4c165f Add explicit VAAPI encoding with CPU fallback in both pipelines 2026-10-15 10:28:24 +08:00
24 changed files with 901 additions and 84 deletions

No files matched your search

+67 -2
View File
@@ -104,11 +104,11 @@ viewer does not reconnect them automatically.
|---|---|
| `~/.config/uuremote/config.json` | user_id, device_id, client_id, system_id, machine_guid, token, csrf; optional `sign_key`, `ffmpeg_root`. `$UU_CONFIG` overrides the path |
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent |
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
| `UU_INPUT_BACKEND` | `auto` (uinput on Wayland/portal when `/dev/uinput` is accessible, udev `uaccess` rule in README) / `uinput` / `native`. X11 keeps XTest (held-key autorepeat handling) |
| `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) |
| `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` |
| `UU_VIDEO_ENCODER=cpu` | force libx264 instead of NVENC |
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: explicit Intel/AMD; unset: NVENC→CPU |
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
@@ -224,3 +224,68 @@ started a second full host server instead, so check that a subcommand exists in
- New protocol behavior: cite the evidence (observed field numbers, official log strings) in a comment.
- Update README.md / README.en.md when user-visible features or limitations change, and add
`docs/release-notes/<tag>.md` before tagging a release.
### VAAPI (opt-in / 显式启用)
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
VAAPI path are not hardware-verified.
### Virtual audio / 虚拟声卡
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
moves streams from the original default output and captures `uu-remote-sink.monitor`.
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
The last desktop session restores the previous default output and unloads the module.
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
A private recovery record beside the config and a process lock recover crash leftovers on startup.
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
### Virtual desktop user bus
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
### Arch Linux / Omarchy
```sh
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
cargo test --locked --features wayland
cargo build --release --features wayland
```
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
This permission can inject into the whole seat, including lock/greeter screens.
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
### Experimental KMS / 实验后端
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
Read the [KMS limitations, permission risk and revocation steps](docs/validation/kms.md) before use.
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
is unavailable without a desktop session. No default release behavior changes.
For headless desktop offers, [official error evidence remains incomplete](docs/validation/headless-error-evidence.md);
the existing offer behavior is retained.
Generated
+1 -1
View File
@@ -2649,7 +2649,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uu-remote-linux"
version = "0.1.6"
version = "0.1.7"
dependencies = [
"base64 0.22.1",
"bytes",
+3 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "uu-remote-linux"
version = "0.1.6"
version = "0.1.7"
edition = "2021"
authors = ["uu-remote-linux contributors"]
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
@@ -16,6 +16,8 @@ path = "src/main.rs"
[features]
default = []
wayland = []
# Experimental direct linear framebuffer capture; never enabled in release builds.
kms = []
portal = []
# In-process capture and encoding through FFmpeg 6 libraries (libavcodec/libavdevice dev packages).
# Off by default so release binaries do not link the build machine's libav.
+68 -3
View File
@@ -176,7 +176,7 @@ Log in once with `uuremote-server login --no-gui` (QR in the terminal).
**Virtual desktop**: for a full remote desktop, run a desktop session (Xfce recommended) on an Xvfb screen:
```sh
sudo apt install xvfb dbus-x11 xfce4 # or another session, chosen with UU_VIRTUAL_SESSION
sudo apt install xvfb xauth x11-utils dbus-x11 dbus-user-session xfce4 # or another session, chosen with UU_VIRTUAL_SESSION
install -Dm755 contrib/virtual-desktop/uu-virtual-desktop ~/.local/bin/uu-virtual-desktop
install -Dm644 contrib/virtual-desktop/uu-virtual-desktop.service ~/.config/systemd/user/uu-virtual-desktop.service
install -Dm644 contrib/virtual-desktop/virtual-display.conf ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
@@ -200,10 +200,10 @@ Common environment variables:
| Variable | Purpose |
|---|---|
| `UU_CONFIG` | Config file path (default `~/.config/uuremote/config.json`) |
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `none` (headless) |
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless) |
| `UU_INPUT_BACKEND` | `auto` (default: uinput on Wayland when accessible) / `uinput` / `native` |
| `UU_TERMINAL_SHORTCUTS=0` | Disable the Ctrl+V paste rewrite in terminal windows |
| `UU_VIDEO_ENCODER=cpu` | Force CPU encoding |
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: opt-in Intel/AMD; unset: existing NVENC→CPU |
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
| `UU_LOGIN_WINDOW=0` | Do not open the login window automatically when credentials are missing |
@@ -262,3 +262,68 @@ Released under the [GPL-3.0](LICENSE): you may use, modify and redistribute it,
- The NetEase UU Remote team, for a remote tool that is a pleasure to use.
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
- Most of the code was written with the help of Claude and Codex.
### VAAPI (opt-in / 显式启用)
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
`/dev/dri/renderD128` (render-node permissions required). No automatic VAAPI selection.
Both FFmpeg process and `--features inproc-av` upload NV12 frames to a VAAPI frame pool.
VBR uses the viewer bitrate preset and CQ-derived minimum QP, no B-frames, async depth 1,
AUD and the existing GOP/VFR timing. Unsupported device/driver/initialization falls back to libx264
with a warning. Accepted on Intel UHD 770 (iHD) with the default FFmpeg process pipeline and the official Mac
viewer (presets, fps, scaling, reconnect; no fallback, ~40% less CPU than libx264). AMD and the `inproc-av`
VAAPI path are not hardware-verified.
### Virtual audio / 虚拟声卡
The viewer's 静音运行 switch creates the null sink `uu-remote-sink`,
moves streams from the original default output and captures `uu-remote-sink.monitor`.
New output goes to this sink; streams explicitly pinned to other outputs are untouched.
The last desktop session restores the previous default output and unloads the module.
`mute_permanent` retains the physical mute policy, but still releases the virtual sink.
A private recovery record beside the config and a process lock recover crash leftovers on startup.
`pactl` is used when installed (PulseAudio / PipeWire-pulse); plain PipeWire systems without it (default
Ubuntu desktop) use `pw-cli`/`wpctl` for the same sink and WirePlumber moves playing streams. Accepted on
Ubuntu 24.04 with the official Mac viewer. When routing is unavailable, the existing mute/monitor path remains.
Changing the switch restarts only the scoped audio encoder; normal non-muted capture is unchanged.
`virtual-audio-probe` temporarily routes output for five seconds and restores it (local diagnostic).
### Virtual desktop user bus
The supervisor imports DISPLAY and XAUTHORITY into the linger user bus, waits for authenticated
X11 readiness, and keeps its cleanup trap alive. The sample drop-in uses `%t/uu-virtual-99.Xauthority`.
Use a dedicated headless account. See the [setup and official Mac acceptance checklist](contrib/virtual-desktop/ACCEPTANCE.md).
### Arch Linux / Omarchy
```sh
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
cargo test --locked --features wayland
cargo build --release --features wayland
```
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
This permission can inject into the whole seat, including lock/greeter screens.
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
Rolling Arch FFmpeg may be newer than supported `inproc-av` (FFmpeg 6.x); use the default process
pipeline or build against a separate FFmpeg 6 prefix. Do not downgrade the system FFmpeg.
User-bus headless setup and the official Mac checklist: [virtual desktop acceptance](contrib/virtual-desktop/ACCEPTANCE.md).
### Experimental KMS / 实验后端
`--features kms` plus explicit `UU_DESKTOP_BACKEND=kms` enables a limited linear-framebuffer
prototype; `UU_KMS_DEVICE=/dev/dri/card0`. Requires libdrm headers and deliberately granted
`cap_sys_admin+ep`, plus card-node access. It is never automatically selected or granted privileges.
Read the [KMS limitations, permission risk and revocation steps](docs/validation/kms.md) before use.
The VM GDM greeter capture was solid black (failed visual acceptance); lock/greeter support and Intel/AMD hardware remain unverified. Use uinput for input; clipboard
is unavailable without a desktop session. No default release behavior changes.
For headless desktop offers, [official error evidence remains incomplete](docs/validation/headless-error-evidence.md);
the existing offer behavior is retained.
+58 -3
View File
@@ -167,7 +167,7 @@ X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放
**虚拟桌面**:需要完整的远程桌面时,用 Xvfb 创建一块虚拟屏幕,再在上面跑一个桌面环境(推荐 Xfce):
```sh
sudo apt install xvfb dbus-x11 xfce4 # 或其他桌面环境,用 UU_VIRTUAL_SESSION 指定
sudo apt install xvfb xauth x11-utils dbus-x11 dbus-user-session xfce4 # 或其他桌面环境,用 UU_VIRTUAL_SESSION 指定
install -Dm755 contrib/virtual-desktop/uu-virtual-desktop ~/.local/bin/uu-virtual-desktop
install -Dm644 contrib/virtual-desktop/uu-virtual-desktop.service ~/.config/systemd/user/uu-virtual-desktop.service
install -Dm644 contrib/virtual-desktop/virtual-display.conf ~/.config/systemd/user/uuremote-native.service.d/virtual-display.conf
@@ -190,10 +190,10 @@ systemctl --user restart uuremote-native.service
| 变量 | 作用 |
|---|---|
| `UU_CONFIG` | 配置文件路径(默认 `~/.config/uuremote/config.json`) |
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `none`(无头模式) |
| `UU_DESKTOP_BACKEND` | `auto` / `x11` / `wayland` / `portal` / `kms`(实验性特性) / `none`(无头模式) |
| `UU_INPUT_BACKEND` | `auto`(默认,Wayland 下可用时用 uinput)/ `uinput` / `native` |
| `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 |
| `UU_VIDEO_ENCODER=cpu` | 强制使用 CPU 编码 |
| `UU_VIDEO_ENCODER` | `cpu` 强制 CPU;`vaapi` 显式启用 Intel/AMD 硬编;未设置时保留 NVENC→CPU |
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
@@ -244,3 +244,58 @@ flowchart LR
- 网易 UU 远程团队:做了一个好用的远程工具。
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
### VAAPI 硬件编码(显式启用)
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
`/dev/dri/renderD128`,运行用户需要 render 节点权限。不会自动选择 VAAPI;设备或驱动初始化
失败会明确警告并回退 libx264。外部 FFmpeg 和 `--features inproc-av` 均先转 NV12 再上传硬件帧。
VBR 使用控制端码率预设及 CQ 对应的最小 QP,无 B 帧、async depth=1,并保留现有 GOP/VFR 规则。
Intel 核显(UHD 770,iHD 驱动)已实机验收:默认的外部 FFmpeg 管线用官方 Mac 控制端测试了各档画质、帧率、缩放和重连,全程硬件编码、无回退,CPU 占用比 libx264 低约 40%。**AMD 显卡和 `--features inproc-av` 管线的 VAAPI 尚未实机验证。**
### 静音运行与虚拟声卡
控制端开启「静音运行」后,程序创建虚拟输出 `uu-remote-sink`,保存原默认输出,
迁移其播放流并采集 `uu-remote-sink.monitor`。新播放也走虚拟输出;其他输出上的流不迁移。
最后一个桌面连接结束后恢复原默认输出并卸载模块;`mute_permanent` 仍保留原物理静音策略。
恢复记录保存在配置旁,权限 0600;进程锁避免重复实例抢占,异常退出后下次启动会恢复遗留路由。
有 `pactl` 时用它(PulseAudio / PipeWire-pulse);没有 `pactl` 的纯 PipeWire 系统(如默认的 Ubuntu 桌面)
自动改用 `pw-cli`/`wpctl` 创建同名虚拟输出,正在播放的声音由 WirePlumber 自动迁移。已在 Ubuntu 24.04 实机上用官方 Mac 控制端验收。
路由失败时保留原静音/monitor 方案。开关变化仅重启当前音频编码器,未开启时采集方式保持不变。
本地诊断命令 `virtual-audio-probe` 会临时切换输出五秒并恢复。
### 虚拟桌面的用户总线
默认 `UU_VIRTUAL_BUS=user`:监督脚本将 DISPLAY/XAUTHORITY 导入 linger 用户总线,等待经过认证的
X11 就绪后通知 systemd;退出时清理 Xvfb。示例主机 drop-in 使用 `%t/uu-virtual-99.Xauthority`。
请使用没有真实图形会话的专用账号;参见[设置与官方 Mac 验收清单](contrib/virtual-desktop/ACCEPTANCE.md)。
### Arch Linux / Omarchy
```sh
sudo pacman -S --needed base-devel rust pkgconf clang ffmpeg libx11 libxext libxtst libxfixes gtk3 \
wayland libxkbcommon wl-clipboard libpipewire glib2 libpulse
cargo test --locked --features wayland
cargo build --release --features wayland
```
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
uinput 需加载内核模块并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
仍须确认启动日志实际选择了 uinput。
VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-driver`,`libva-utils` 提供
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
滚动 Arch 的 FFmpeg 可能超出 `inproc-av` 支持的 6.x;请使用默认进程方案或独立的 FFmpeg 6 前缀,
不要降级系统 FFmpeg。当前 Arch/Hyprland VM 验证结果见[开发验证记录](docs/validation/v017-results.md)。
### 实验性 KMS 截屏
编译 `--features kms` 后显式设置 `UU_DESKTOP_BACKEND=kms`;`UU_KMS_DEVICE` 默认
`/dev/dri/card0`。需要 libdrm、card 节点访问权及主动授予的 `cap_sys_admin+ep`。
程序不会自动启用或授权;此 capability 权限很大,先阅读[限制、安全风险及撤销步骤](docs/validation/kms.md)。
当前仅为线性帧缓冲原型:VM 的 GDM 截图仍是黑屏,**没有通过登录界面视觉验收**,不承诺锁屏可用。
键鼠需 uinput;没有桌面会话时不提供剪贴板。
无头桌面错误码的[官方证据仍不足](docs/validation/headless-error-evidence.md),因此保持原有 offer 行为。
+10
View File
@@ -9,6 +9,16 @@ fn main() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
if target_os == "linux" {
if std::env::var_os("CARGO_FEATURE_KMS").is_some() {
println!("cargo:rerun-if-changed=c_engine/kms_engine.c");
let output=std::process::Command::new("pkg-config").args(["--cflags","libdrm"]).output().expect("KMS requires pkg-config and libdrm development headers");
assert!(output.status.success(), "KMS requires libdrm development headers");
let mut build=cc::Build::new();
build.file("c_engine/kms_engine.c");
for flag in String::from_utf8_lossy(&output.stdout).split_whitespace() { build.flag(flag); }
build.compile("uu_kms_engine");
println!("cargo:rustc-link-lib=drm");
}
cc::Build::new()
.file("c_engine/x11_engine.c")
.file("c_engine/clipboard.c")
+100
View File
@@ -0,0 +1,100 @@
/* Opt-in KMS prototype: read active primary-plane linear XRGB/ARGB buffers.
* No modesetting, DRM master acquisition, authentication or content logging.
*/
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_fourcc.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
struct uu_kms { int fd; uint32_t crtc, plane, width, height; };
static uint32_t primary_plane(int fd, uint32_t crtc_id) {
drmModePlaneRes *planes=drmModeGetPlaneResources(fd);
if (!planes) return 0;
uint32_t selected=0;
for (uint32_t i=0;i<planes->count_planes && !selected;i++) {
drmModePlane *plane=drmModeGetPlane(fd,planes->planes[i]);
if (plane && plane->crtc_id==crtc_id && plane->fb_id) {
drmModeObjectProperties *props=drmModeObjectGetProperties(fd,plane->plane_id,DRM_MODE_OBJECT_PLANE);
if (props) {
for (uint32_t j=0;j<props->count_props;j++) {
drmModePropertyRes *prop=drmModeGetProperty(fd,props->props[j]);
if (prop && strcmp(prop->name,"type")==0 && props->prop_values[j]==DRM_PLANE_TYPE_PRIMARY) selected=plane->plane_id;
if (prop) drmModeFreeProperty(prop);
}
drmModeFreeObjectProperties(props);
}
}
if (plane) drmModeFreePlane(plane);
}
drmModeFreePlaneResources(planes);
return selected;
}
static void close_handles(int fd, drmModeFB2 *fb) {
for (int i=0;i<4;i++) {
if (!fb->handles[i]) continue;
int duplicate=0;
for (int j=0;j<i;j++) if (fb->handles[j]==fb->handles[i]) duplicate=1;
if (!duplicate) { struct drm_gem_close c={.handle=fb->handles[i]}; drmIoctl(fd,DRM_IOCTL_GEM_CLOSE,&c); }
}
}
void uu_kms_close(struct uu_kms *s) { if (s) { close(s->fd); free(s); } }
struct uu_kms *uu_kms_open(const char *device, uint32_t *w, uint32_t *h) {
int fd=open(device,O_RDWR|O_CLOEXEC);
if (fd<0) return NULL;
if (drmSetClientCap(fd,DRM_CLIENT_CAP_UNIVERSAL_PLANES,1)<0) { close(fd); return NULL; }
drmModeRes *res=drmModeGetResources(fd);
if (!res) { close(fd); return NULL; }
struct uu_kms *s=NULL;
for (int i=0;i<res->count_crtcs;i++) {
drmModeCrtc *crtc=drmModeGetCrtc(fd,res->crtcs[i]);
uint32_t plane=crtc ? primary_plane(fd,crtc->crtc_id) : 0;
if (crtc && plane && crtc->mode_valid && crtc->width && crtc->height && crtc->width<=16384 && crtc->height<=16384) {
s=calloc(1,sizeof(*s));
if (s) { s->fd=fd; s->crtc=crtc->crtc_id; s->plane=plane; s->width=crtc->width; s->height=crtc->height; *w=s->width; *h=s->height; }
}
if (crtc) drmModeFreeCrtc(crtc);
if (s) break;
}
drmModeFreeResources(res);
if (!s) close(fd);
return s;
}
int uu_kms_read(struct uu_kms *s, uint8_t *out, size_t length) {
drmModeCrtc *crtc=drmModeGetCrtc(s->fd,s->crtc);
if (!crtc) return -1;
drmModePlane *plane=drmModeGetPlane(s->fd,s->plane);
uint32_t id=plane ? plane->fb_id : 0;
if (plane) drmModeFreePlane(plane);
int valid=crtc->mode_valid && crtc->width==s->width && crtc->height==s->height;
drmModeFreeCrtc(crtc);
if (!valid || !id) return -2; /* mode changed: caller must reopen */
drmModeFB2 *fb=drmModeGetFB2(s->fd,id);
if (!fb) return -3;
int result=-4;
if (!fb->handles[0]) { result=-5; goto done; } /* DRM master or CAP_SYS_ADMIN needed */
if (fb->width!=s->width || fb->height!=s->height ||
(fb->pixel_format!=DRM_FORMAT_XRGB8888 && fb->pixel_format!=DRM_FORMAT_ARGB8888) ||
((fb->flags & DRM_MODE_FB_MODIFIERS) && fb->modifier!=DRM_FORMAT_MOD_LINEAR) ||
fb->handles[1] || fb->pitches[0] < s->width*4 || length!=(size_t)s->width*s->height*4) goto done;
struct drm_mode_map_dumb map={.handle=fb->handles[0]};
if (drmIoctl(s->fd,DRM_IOCTL_MODE_MAP_DUMB,&map)<0) { result=-6; goto done; }
size_t bytes=(size_t)fb->pitches[0]*fb->height+fb->offsets[0];
void *data=mmap(NULL,bytes,PROT_READ,MAP_SHARED,s->fd,map.offset);
if (data==MAP_FAILED) { result=-7; goto done; }
for (uint32_t y=0;y<s->height;y++) {
uint8_t *row=out+(size_t)y*s->width*4;
memcpy(row,(uint8_t*)data+fb->offsets[0]+(size_t)y*fb->pitches[0],s->width*4);
for (uint32_t x=0;x<s->width;x++) row[x*4+3]=255;
}
munmap(data,bytes);
result=0;
done:
close_handles(s->fd,fb);
drmModeFreeFB2(fb);
return result;
}
+33
View File
@@ -0,0 +1,33 @@
# Virtual user-bus desktop acceptance
Install Xvfb, xauth, xdpyinfo, Xfce and dbus-user-session (Debian) or dbus-broker (Arch).
The default is `UU_VIRTUAL_BUS=user`; use it on a dedicated headless account with no real
GUI session. Its D-Bus and systemd activation environment is shared by all that user's apps.
`private` isolates ordinary D-Bus activation but cannot redirect systemd-activated programs.
1. Install the script in `~/.local/bin`, the unit in `~/.config/systemd/user`, and
`virtual-display.conf` in the host service's `.service.d/` directory.
2. `sudo loginctl enable-linger "$USER"`; set `UU_VIRTUAL_SESSION=startxfce4` in the desktop
unit. `systemctl --user daemon-reload`; enable both units. A different display needs a
matching `DISPLAY` and `XAUTHORITY=%t/uu-virtual-N.Xauthority` in the host drop-in.
3. Reboot a disposable VM with graphical login disabled. Before logging into a desktop,
check both user units are active, Xvfb is `:99`, no real GUI login session exists, and
host availability is `ready`. The desktop unit uses notify readiness after authenticated
X11 access succeeds, so the host does not race the X socket.
4. `DISPLAY=:99 XAUTHORITY="$XDG_RUNTIME_DIR/uu-virtual-99.Xauthority" xdpyinfo` must work.
`gnome-terminal` launched over the user bus must open on `:99`. Check its window via
`xdotool search --class gnome-terminal-server`; do not log terminal text.
5. Restart/stop the desktop unit: its Xvfb, desktop children and authority file must be
removed. A stale pre-existing display is rejected. The TCP X11 listener is disabled.
## Official Mac client checklist (human acceptance)
- [ ] Select the isolated VM device; confirm its identity before connecting.
- [ ] After VM boot, connect without a physical/graphical login; Xfce is visible.
- [ ] Open GNOME Terminal through its application launcher; it appears on the same `:99` desktop.
- [ ] Move/click/drag, scroll, type a synthetic test marker and verify key releases (no repeats).
- [ ] Verify both clipboard directions using synthetic text; keep the remote window focused.
- [ ] Toggle 静音运行 during playback: remote sound remains audible; VM physical output is silent.
- [ ] Disconnect/reconnect; confirm desktop and applications remain usable.
- [ ] End the last session: audio default/modules restore; auto-lock follows the saved setting.
- [ ] Repeat after reboot with no GUI login. Do not enter production credentials in test fixtures.
+69 -20
View File
@@ -1,5 +1,5 @@
#!/bin/sh
# Virtual desktop for a monitorless Linux host: Xvfb + a desktop session on its own D-Bus session bus.
# Virtual desktop for a monitorless Linux host: Xvfb + a desktop session on the user bus (or an isolated private bus).
# uuremote-server then serves it with DISPLAY=$UU_VIRTUAL_DISPLAY (see README in this folder).
# UU_VIRTUAL_DISPLAY X display number (default :99)
# UU_VIRTUAL_SIZE screen size (default 1920x1080)
@@ -18,23 +18,72 @@ if [ -z "$session" ]; then
fi
[ -n "$session" ] || { echo "no desktop session found; set UU_VIRTUAL_SESSION" >&2; exit 1; }
command -v Xvfb >/dev/null || { echo "Xvfb not installed (apt install xvfb)" >&2; exit 1; }
command -v dbus-run-session >/dev/null || { echo "dbus-run-session not installed (apt install dbus)" >&2; exit 1; }
Xvfb "$display" -screen 0 "${size}x24" -nolisten tcp -noreset &
xvfb=$!
trap 'kill "$xvfb" 2>/dev/null' EXIT INT TERM
socket="/tmp/.X11-unix/X${display#:}"
for _ in $(seq 50); do [ -S "$socket" ] && break; sleep 0.1; done
[ -S "$socket" ] || { echo "Xvfb did not start on $display" >&2; exit 1; }
export DISPLAY="$display"
unset WAYLAND_DISPLAY XAUTHORITY
export XDG_SESSION_TYPE=x11
if [ "${UU_VIRTUAL_BUS:-user}" = user ]; then
# Activated services inherit the user manager's environment, not this process's: export DISPLAY there.
dbus-update-activation-environment --systemd DISPLAY XDG_SESSION_TYPE
exec sh -c "$session"
else
# Private bus: independent of the user manager; activation through systemd may still escape to the user bus.
dbus-run-session -- sh -c "$session"
bus="${UU_VIRTUAL_BUS:-user}"
case "$bus" in user|private) ;; *) echo "UU_VIRTUAL_BUS must be user or private" >&2; exit 1 ;; esac
command -v xauth >/dev/null || { echo "xauth not installed" >&2; exit 1; }
command -v xdpyinfo >/dev/null || { echo "xdpyinfo not installed" >&2; exit 1; }
case "$display" in :[0-9]*) ;; *) echo "UU_VIRTUAL_DISPLAY must be a local display number" >&2; exit 1 ;; esac
number="${display#:}"
case "$number" in *[!0-9]*|'') echo "invalid display number" >&2; exit 1 ;; esac
socket="/tmp/.X11-unix/X$number"
# A live server owns the display; a dead one's leftovers (killed Xvfb) are cleaned up by Xvfb itself.
lock="/tmp/.X$number-lock"
if [ -e "$socket" ] || [ -e "$lock" ]; then
owner=$(tr -d ' \n' < "$lock" 2>/dev/null || :)
case "$owner" in ''|*[!0-9]*) owner= ;; esac
if [ -z "$owner" ] || kill -0 "$owner" 2>/dev/null; then echo "display already in use" >&2; exit 1; fi
echo "removing stale display $display left by dead process $owner" >&2
rm -f "$lock" "$socket" 2>/dev/null || { echo "cannot remove stale display files" >&2; exit 1; }
fi
umask 077
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
authority="$XDG_RUNTIME_DIR/uu-virtual-$number.Xauthority"
# The display socket was checked above; replace a stale authority after a service crash.
[ ! -L "$authority" ] || { echo "refusing a symlink authority" >&2; exit 1; }
temporary=$(mktemp "$XDG_RUNTIME_DIR/uu-virtual-xauth.XXXXXX")
xvfb=
child=
cleanup() {
trap - EXIT INT TERM
[ -z "$child" ] || kill "$child" 2>/dev/null || :
[ -z "$xvfb" ] || kill "$xvfb" 2>/dev/null || :
[ -z "$xvfb" ] || wait "$xvfb" 2>/dev/null || :
rm -f "$authority" "$temporary"
}
trap cleanup EXIT
trap 'exit 143' TERM
trap 'exit 130' INT
cookie=$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')
xauth -f "$temporary" add "$display" MIT-MAGIC-COOKIE-1 "$cookie"
unset cookie
mv "$temporary" "$authority"
export XAUTHORITY="$authority"
Xvfb "$display" -screen 0 "${size}x24" -auth "$authority" -nolisten tcp -noreset &
xvfb=$!
export DISPLAY="$display"
unset WAYLAND_DISPLAY SESSION_MANAGER
export XDG_SESSION_TYPE=x11
ready=false
for _ in $(seq 50); do
kill -0 "$xvfb" 2>/dev/null || break
if xdpyinfo >/dev/null 2>&1; then ready=true; break; fi
sleep 0.1
done
[ "$ready" = true ] || { echo "Xvfb did not become ready" >&2; exit 1; }
if [ "$bus" = user ]; then
# Linger creates the user manager and its bus without a graphical login.
: "${XDG_RUNTIME_DIR:=/run/user/$(id -u)}"
export XDG_RUNTIME_DIR
export DBUS_SESSION_BUS_ADDRESS="unix:path=$XDG_RUNTIME_DIR/bus"
[ -S "$XDG_RUNTIME_DIR/bus" ] || { echo "user bus unavailable; enable loginctl enable-linger" >&2; exit 1; }
systemctl --user unset-environment WAYLAND_DISPLAY
dbus-update-activation-environment --systemd DISPLAY XAUTHORITY XDG_SESSION_TYPE
# Keep the supervising shell alive: exec would discard the Xvfb cleanup trap.
sh -c "$session" &
else
dbus-run-session -- sh -c "$session" &
fi
child=$!
if [ -n "${NOTIFY_SOCKET:-}" ]; then systemd-notify --ready; fi
wait "$child"
@@ -3,6 +3,11 @@ Description=Virtual X desktop for uu-remote-linux (Xvfb + session bus)
After=dbus.socket
[Service]
Type=notify
NotifyAccess=all
TimeoutStartSec=30
TimeoutStopSec=10
KillMode=control-group
# Environment=UU_VIRTUAL_SESSION=startxfce4
# Environment=UU_VIRTUAL_SIZE=1920x1080
# Environment=UU_VIRTUAL_BUS=private
+2 -1
View File
@@ -7,4 +7,5 @@ After=uu-virtual-desktop.service
[Service]
Environment=DISPLAY=:99
Environment=UU_DESKTOP_BACKEND=x11
UnsetEnvironment=XAUTHORITY WAYLAND_DISPLAY
Environment=XAUTHORITY=%t/uu-virtual-99.Xauthority
UnsetEnvironment=WAYLAND_DISPLAY
+31
View File
@@ -0,0 +1,31 @@
VAAPI 硬件编码、虚拟声卡、无人登录的虚拟桌面和 Arch/Omarchy 支持。/ VAAPI encoding, virtual audio sink, linger virtual desktop, Arch/Omarchy.
### 新增 / Added
- **VAAPI 硬件编码(Intel/AMD)**:`UU_VIDEO_ENCODER=vaapi`(`UU_VAAPI_DEVICE` 默认 `/dev/dri/renderD128`),需要显式开启,不会自动选择;
初始化失败时自动回退 CPU 编码。✅ Intel UHD 770(iHD 驱动)实机验收:用官方 Mac 控制端测试了各档画质、30/60 帧、缩放和重连,
全程硬件编码,CPU 占用比 libx264 低约 40%。⚠️ AMD 显卡、`--features inproc-av` 管线的 VAAPI 尚未实机验证。
Opt-in VAAPI with CPU fallback. Accepted on Intel UHD 770; AMD and the inproc-av path are not hardware-verified.
- **虚拟声卡(静音运行)**:开启「静音运行」时创建虚拟输出 `uu-remote-sink`,把声音转到它上面采集,本机扬声器静音、控制端照常有声;
结束后恢复原默认输出;异常退出后下次启动自动清理。有 `pactl` 时用它,没有时(默认的 Ubuntu 桌面)用 PipeWire 自带的 `pw-cli`/`wpctl`。
✅ Ubuntu 24.04(PipeWire 1.0.5)上用官方 Mac 控制端验收:开关、断开恢复、多工具连接,以及强杀后的恢复。
Virtual null sink while muted (pactl or PipeWire-native). Accepted on Ubuntu 24.04 with the official Mac viewer.
- **虚拟桌面改进**:默认使用 linger 用户总线,并导入 `DISPLAY`/`XAUTHORITY`,D-Bus 激活的程序(如 GNOME Terminal)也会出现在虚拟屏幕上;
独立的 X 认证 cookie、`Type=notify` 就绪通知、退出清理;Xvfb 被强杀后残留的显示器文件会自动清理。
✅ 虚拟机里验证了无人登录开机上线、GNOME Terminal 出现在 `:99`、键鼠;残留清理在实机验证。
⚠️ 无人登录场景下尚未用官方控制端完成验收(清单见 `contrib/virtual-desktop/ACCEPTANCE.md`)。
- **Arch Linux / Omarchy**:README 增加 pacman 依赖和 Omarchy(Hyprland)说明。✅ 虚拟机里 Hyprland 截屏、uinput 键鼠、剪贴板通过。
⚠️ 尚未在真实 Omarchy 上用官方控制端验收;相对鼠标位移的精确像素没有验证。
- **KMS 截屏(实验原型,默认不编译)**:`--features kms` + `UU_DESKTOP_BACKEND=kms`,需要 `cap_sys_admin`。
❌ 虚拟机里登录界面截图全黑,视觉验收未通过;**不支持**锁屏/登录界面,发布包里也不包含这个功能。说明见 `docs/validation/kms.md`。
### 修复 / Fixed
- 虚拟声卡恢复时,如果原默认声卡已经不存在(比如 USB 声卡被拔掉),不再留下虚拟输出作为默认。
- 「静音运行」相关的日志会区分虚拟输出和真实声卡的静音状态。
### 未改变 / Unchanged
- 无头模式下打开远程桌面仍会显示「正在重新连接」:没找到官方的「无桌面」错误码,不挪用其他错误码。
### 下载 / Download
`uu-remote-linux-v0.1.7-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
+3 -2
View File
@@ -81,9 +81,9 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
if closed(&pc) { return; }
tokio::time::sleep(Duration::from_millis(100)).await;
}
let source = std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into());
let mut failures = 0u32;
while !closed(&pc) {
let source = crate::virtual_audio::source();
let (tx, mut rx) = tokio::sync::mpsc::channel::<bytes::Bytes>(25);
let (stopper, job) = match start(&source, tx) {
Ok(started) => started,
@@ -101,7 +101,7 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
}
packets += 1;
},
_ = health.tick() => if closed(&pc) { break },
_ = health.tick() => if closed(&pc) || crate::virtual_audio::source() != source { break },
}
}
// Dropping the receiver and the stopper ends the encoder at its next packet.
@@ -109,6 +109,7 @@ pub async fn stream_audio(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStati
drop(stopper);
let result = job.await.unwrap_or_else(|e| Err(e.to_string()));
if closed(&pc) { break; }
if crate::virtual_audio::source() != source { failures=0; continue; }
log::warn!("Desktop audio stopped after {packets} packets: {:?}", result.err());
failures = if packets > 250 { 0 } else { failures + 1 };
if failures >= 5 { log::warn!("Desktop audio disabled for this connection"); return; }
+13 -3
View File
@@ -46,6 +46,7 @@ pub struct ScreenCapture {
pub init_error: Option<String>,
pub portal: Option<std::sync::Arc<crate::portal::PortalSession>>,
pub display: String,
kms: Option<crate::kms::Capture>,
pub width: u32,
pub height: u32,
buffer: Mutex<Vec<u8>>,
@@ -54,15 +55,21 @@ pub struct ScreenCapture {
impl ScreenCapture {
pub fn new() -> Result<Self, String> {
let backend=crate::desktop_backend::detect()?;
if backend == crate::desktop_backend::DesktopBackend::Kms {
let kms=crate::kms::Capture::open()?;
let (width,height)=(kms.width,kms.height);
return Ok(Self { backend,init_error:None,portal:None,display:"kms:linear".into(),width,height,
buffer:Mutex::new(vec![0;width as usize*height as usize*4]),kms:Some(kms) });
}
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),display:"wayland:portal".into(),width,height,
return Ok(Self { backend,init_error:None,portal:Some(session),kms:None,display:"wayland:portal".into(),width,height,
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
}
if backend == crate::desktop_backend::DesktopBackend::Wayland {
let (width,height)=crate::wayland::init()?;
return Ok(Self { backend, init_error:None, portal:None, display:"wayland:native".into(), width,height,
return Ok(Self { backend, init_error:None, portal:None, kms:None, display:"wayland:native".into(), width,height,
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
}
let ret = unsafe { x11_init(std::ptr::null()) };
@@ -90,6 +97,7 @@ impl ScreenCapture {
backend,
init_error:None,
portal:None,
kms:None,
display,
width: w as u32,
height: h as u32,
@@ -100,13 +108,14 @@ impl ScreenCapture {
/// Metadata for non-desktop tools. Never advertise a usable fake screen.
pub fn unavailable(error:String)->Self {
Self { backend:crate::desktop_backend::detect().unwrap_or(crate::desktop_backend::DesktopBackend::X11),
init_error:Some(error), portal:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
init_error:Some(error), portal:None, kms:None, display:String::new(), width:0,height:0,buffer:Mutex::new(Vec::new()) }
}
/// 截取一帧 BGRA 图像
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
if let Some(error)=&self.init_error { return Err(error.clone()); }
let mut buf = self.buffer.lock().unwrap();
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
crate::wayland::capture(&mut buf)?;
@@ -132,6 +141,7 @@ 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::Wayland {
return crate::wayland::wake();
+1 -1
View File
@@ -20,7 +20,7 @@ extern "C" {
pub fn start(display: String) -> ClipboardBridge {
if display == "wayland:native" { return start_wayland(); }
if display == "wayland:portal" {
if display == "wayland:portal" || display.starts_with("kms:") {
let (incoming,_)=watch::channel(None);let (_,outgoing)=watch::channel(None);
let (sender,ready)=tokio::sync::oneshot::channel();let _=sender.send(false);
return ClipboardBridge {incoming,outgoing,ready};
+21 -16
View File
@@ -4,9 +4,13 @@ pub fn run() -> Result<(),String> {
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
return Err("Clipboard acceptance is restricted to the isolated VM".into());
}
let session=Arc::new(crate::portal::PortalSession::open()?);
let bridge=crate::clipboard::start_portal(session.clone());
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Portal clipboard permission not granted".into()); }
let screen=crate::capture::ScreenCapture::new()?;
let input=Arc::new(crate::input::InputInjector::new());
input.bind_portal(screen.portal.clone());
let pointer=input.start_pointer_worker();
let bridge=if let Some(session)=screen.portal.clone() { crate::clipboard::start_portal(session) }
else { crate::clipboard::start(screen.display.clone()) };
if !bridge.ready.blocking_recv().unwrap_or(false) { return Err("Desktop clipboard permission not granted".into()); }
let mut child=Command::new("python3").arg("/home/uuremote/clipboard-acceptance.py")
.env("GDK_BACKEND","wayland").env("GSK_RENDERER","cairo")
.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().map_err(|_|"Start GTK clipboard fixture")?;
@@ -29,13 +33,14 @@ pub fn run() -> Result<(),String> {
bridge.incoming.send_replace(Some(text.clone()));
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
while Instant::now()<deadline { if check("read",&text)? {matched=true;break;} std::thread::sleep(Duration::from_millis(100)); }
if !matched { return Err(format!("Portal clipboard incoming {name} mismatch")); }
if !matched { return Err(format!("Desktop clipboard incoming {name} mismatch")); }
std::thread::sleep(Duration::from_millis(300));
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard echoed its own selection".into()); }
println!("Portal clipboard incoming {name}: passed; no echo");
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard echoed its own selection".into()); }
println!("Desktop clipboard incoming {name}: passed; no echo");
}
session.move_pointer(f64::from(session.width)/2.0,f64::from(session.height)/2.0,false)?;
session.button(1,true)?; session.button(1,false)?;
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
std::thread::sleep(Duration::from_millis(100));
input.mouse_button(1,true); input.mouse_button(1,false);
std::thread::sleep(Duration::from_millis(300));
let external="UU outgoing 中文🚀\nsecond line";
if !check("set",external)? { return Err("GTK clipboard ownership failed".into()); }
@@ -44,25 +49,25 @@ pub fn run() -> Result<(),String> {
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some(external) {matched=true;break;}
std::thread::sleep(Duration::from_millis(100));
}
if !matched { return Err("Portal clipboard outgoing Unicode mismatch".into()); }
println!("Portal clipboard outgoing unicode: passed");
if !matched { return Err("Desktop clipboard outgoing Unicode mismatch".into()); }
println!("Desktop clipboard outgoing unicode: passed");
bridge.incoming.send_replace(Some("x".repeat(crate::clipboard::MAX_TEXT+1)));
std::thread::sleep(Duration::from_millis(400));
if !check("read",external)? { return Err("Oversized text altered native clipboard".into()); }
println!("Portal clipboard incoming oversized rejection: passed");
println!("Desktop clipboard incoming oversized rejection: passed");
if !check("set",&"y".repeat(crate::clipboard::MAX_TEXT+1))? { return Err("GTK oversized fixture setup failed".into()); }
std::thread::sleep(Duration::from_secs(3));
if outgoing.has_changed().unwrap_or(false) { return Err("Portal clipboard published oversized native text".into()); }
println!("Portal clipboard outgoing oversized rejection: passed");
if outgoing.has_changed().unwrap_or(false) { return Err("Desktop clipboard published oversized native text".into()); }
println!("Desktop clipboard outgoing oversized rejection: passed");
if !check("set","")? { return Err("GTK empty fixture setup failed".into()); }
let deadline=Instant::now()+Duration::from_secs(6);let mut matched=false;
while Instant::now()<deadline {
if outgoing.has_changed().unwrap_or(false) && outgoing.borrow_and_update().as_deref()==Some("") {matched=true;break;}
std::thread::sleep(Duration::from_millis(100));
}
if !matched { return Err("Portal clipboard outgoing empty text mismatch".into()); }
println!("Portal clipboard outgoing empty: passed");
if !matched { return Err("Desktop clipboard outgoing empty text mismatch".into()); }
println!("Desktop clipboard outgoing empty: passed");
drop(writer); Ok(())
})();
let _=child.kill();let _=child.wait();result
let _=child.kill();let _=child.wait();pointer.abort();input.release_held();result
}
+4 -2
View File
@@ -1,6 +1,6 @@
//! Select the host desktop, not the presence of an XWayland DISPLAY socket.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DesktopBackend { X11, Wayland, Portal }
pub enum DesktopBackend { X11, Wayland, Portal, Kms }
/// `UU_DESKTOP_BACKEND=none`: headless host — no display is opened; terminal, file transfer and
/// port mapping stay available (servers without a monitor or logged-in session).
@@ -19,6 +19,7 @@ fn select(explicit: Option<&str>, session: Option<&str>, socket: Option<&str>) -
match explicit {
Some("x11") => Ok(DesktopBackend::X11),
Some("wayland") => Ok(DesktopBackend::Wayland),
Some("kms") => Ok(DesktopBackend::Kms),
Some("portal") => Ok(DesktopBackend::Portal),
Some("none") => Ok(DesktopBackend::X11), // never opened, see headless()
None | Some("auto") => {
@@ -26,7 +27,7 @@ fn select(explicit: Option<&str>, session: Option<&str>, socket: Option<&str>) -
Ok(DesktopBackend::Wayland)
} else { Ok(DesktopBackend::X11) }
},
Some(_) => Err("UU_DESKTOP_BACKEND must be auto, x11, wayland, portal or none".into()),
Some(_) => Err("UU_DESKTOP_BACKEND must be auto, x11, wayland, portal, kms or none".into()),
}
}
@@ -39,6 +40,7 @@ mod tests {
assert_eq!(select(None,None,Some("wayland-1")),Ok(DesktopBackend::Wayland));
assert_eq!(select(None,Some("x11"),None),Ok(DesktopBackend::X11));
assert_eq!(select(Some("x11"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::X11));
assert_eq!(select(Some("kms"),Some("wayland"),Some("wayland-0")),Ok(DesktopBackend::Kms));
assert!(select(Some("typo"),None,None).is_err());
}
}
+50 -7
View File
@@ -24,7 +24,7 @@ pub struct Config {
pub target_bps: i64,
pub max_bps: i64,
pub buffer_bits: i64,
pub gpu: bool,
pub encoder: crate::video::Encoder,
/// Capture on XDamage changes (XShm) instead of x11grab; `UU_CAPTURE=grab` turns it off.
pub damage: bool,
}
@@ -74,15 +74,16 @@ fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender
/// Opens the H.264 encoder for `config`. Returns the encoder, its input pixel format and output size.
fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
let (width, height) = config.output.unwrap_or(config.input);
let codec = encoder::find_by_name(if config.gpu { "h264_nvenc" } else { "libx264" })
let codec = encoder::find_by_name(config.encoder.name())
.ok_or("H.264 encoder is not available in this FFmpeg build")?;
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
context.set_width(width);
context.set_height(height);
// NVENC takes packed BGR0 directly, so the GPU path skips the CPU colour conversion to YUV420P.
let pixel = if config.gpu { Pixel::BGRZ } else { Pixel::YUV420P };
context.set_format(pixel);
let pixel = match config.encoder { crate::video::Encoder::Nvenc => Pixel::BGRZ, crate::video::Encoder::Vaapi => Pixel::NV12, _ => Pixel::YUV420P };
context.set_format(if config.encoder == crate::video::Encoder::Vaapi { Pixel::VAAPI } else { pixel });
if config.encoder == crate::video::Encoder::Vaapi { attach_vaapi(&mut context, width, height)?; }
context.set_time_base(Rational::new(1, config.fps as i32));
context.set_frame_rate(Some(Rational::new(config.fps as i32, 1)));
context.set_gop(config.gop);
@@ -98,7 +99,10 @@ fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), St
let cq = config.cq.to_string();
let gop = config.gop.to_string();
let mut options = Dictionary::new();
if config.gpu {
if config.encoder == crate::video::Encoder::Vaapi {
for (key,value) in [("profile", "constrained_baseline"), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
options.set("qmin", &cq);
} else if config.encoder == crate::video::Encoder::Nvenc {
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", "baseline"), ("rc", "vbr"),
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
options.set(key, value);
@@ -116,6 +120,45 @@ fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), St
Ok((encoder, pixel, width, height))
}
// Each codec owns a reference to its hardware frame pool. Every error path releases
// the temporary device/pool refs; AVCodecContext releases its own ref on drop.
fn attach_vaapi(context: &mut encoder::video::Video, width: u32, height: u32) -> Result<(), String> {
use ff::sys::*;
let device_name = CString::new(crate::video::vaapi_device()).map_err(|_| "Invalid VAAPI device")?;
unsafe {
let mut device = std::ptr::null_mut();
let result = av_hwdevice_ctx_create(&mut device, AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI, device_name.as_ptr(), std::ptr::null_mut(), 0);
if result < 0 { return Err(format!("VAAPI device initialization failed: {}", text(ff::Error::from(result)))); }
let mut pool = av_hwframe_ctx_alloc(device);
av_buffer_unref(&mut device);
if pool.is_null() { return Err("VAAPI frame pool allocation failed".into()); }
let frames = (*pool).data as *mut AVHWFramesContext;
(*frames).format = AVPixelFormat::AV_PIX_FMT_VAAPI;
(*frames).sw_format = AVPixelFormat::AV_PIX_FMT_NV12;
(*frames).width = width as i32;
(*frames).height = height as i32;
(*frames).initial_pool_size = 8;
let result = av_hwframe_ctx_init(pool);
if result < 0 { av_buffer_unref(&mut pool); return Err(format!("VAAPI frame pool initialization failed: {}", text(ff::Error::from(result)))); }
(*context.as_mut_ptr()).hw_frames_ctx = pool;
}
Ok(())
}
fn send_video_frame(encoder: &mut encoder::Video, frame: &frame::Video, kind: crate::video::Encoder) -> Result<(), String> {
if kind != crate::video::Encoder::Vaapi { return encoder.send_frame(frame).map_err(text); }
let mut uploaded = frame::Video::empty();
unsafe {
let result = ff::sys::av_hwframe_get_buffer((*encoder.as_ptr()).hw_frames_ctx, uploaded.as_mut_ptr(), 0);
if result < 0 { return Err(text(ff::Error::from(result))); }
let result = ff::sys::av_hwframe_transfer_data(uploaded.as_mut_ptr(), frame.as_ptr(), 0);
if result < 0 { return Err(text(ff::Error::from(result))); }
let result = ff::sys::av_frame_copy_props(uploaded.as_mut_ptr(), frame.as_ptr());
if result < 0 { return Err(text(ff::Error::from(result))); }
}
encoder.send_frame(&uploaded).map_err(text)
}
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
ensure_init()?;
@@ -184,7 +227,7 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
frame.set_pts(Some(slot));
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
encoder.send_frame(frame).map_err(text)?;
send_video_frame(&mut encoder, frame, config.encoder)?;
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
}
Ok(())
@@ -244,7 +287,7 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
};
frame.set_pts(Some(slot));
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
encoder.send_frame(frame).map_err(text)?;
send_video_frame(&mut encoder, frame, config.encoder)?;
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
let now = Instant::now();
if dirty {
+3
View File
@@ -232,6 +232,7 @@ impl InputInjector {
return Self::uinput_result(uinput.move_absolute(scale(x,w),scale(y,h)));
}
}
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("motion", portal.move_pointer(f64::from(x),f64::from(y),relative)); }
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("motion", Err("Portal input session is not bound".into()));}
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::move_pointer(x,y,relative) }
@@ -242,6 +243,7 @@ impl InputInjector {
if let Some((v,h))=crate::uinput::wheel_step(button) { return if pressed { Self::uinput_result(uinput.wheel(v,h)) } else { 0 }; }
if let Some(code)=crate::uinput::button_code(button) { return Self::uinput_result(uinput.button(code,pressed)); }
}
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("button", portal.button(button,pressed)); }
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("button", Err("Portal input session is not bound".into()));}
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::button(button,pressed) }
@@ -251,6 +253,7 @@ impl InputInjector {
if let Some(uinput)=&self.uinput {
if let Some(code)=crate::uinput::keysym_to_evdev(symbol) { return Self::uinput_result(uinput.key(code,pressed)); }
}
if self.backend==crate::desktop_backend::DesktopBackend::Kms { return -1; }
if let Some(portal)=self.portal.lock().unwrap().as_ref() { return self.portal_result("keyboard", portal.key(symbol,pressed)); }
if self.backend==crate::desktop_backend::DesktopBackend::Portal {return self.portal_result("keyboard", Err("Portal input session is not bound".into()));}
if self.backend==crate::desktop_backend::DesktopBackend::Wayland { crate::wayland::key(symbol,pressed) }
+50
View File
@@ -0,0 +1,50 @@
//! Experimental linear KMS framebuffer capture. Never selected automatically.
#[cfg(all(feature="kms",target_os="linux"))]
mod imp {
use std::ffi::CString;
use std::sync::Mutex;
extern "C" {
fn uu_kms_open(device:*const libc::c_char,w:*mut u32,h:*mut u32)->*mut libc::c_void;
fn uu_kms_read(handle:*mut libc::c_void,out:*mut u8,length:usize)->i32;
fn uu_kms_close(handle:*mut libc::c_void);
}
struct Handle(*mut libc::c_void);
// The FD/CRTC handle is exclusively accessed through Capture's mutex.
unsafe impl Send for Handle {}
impl Drop for Handle { fn drop(&mut self) { unsafe { uu_kms_close(self.0) }; } }
pub struct Capture { handle:Mutex<Handle>, pub width:u32, pub height:u32 }
impl Capture {
pub fn open()->Result<Self,String> {
let device=CString::new(std::env::var("UU_KMS_DEVICE").unwrap_or_else(|_|"/dev/dri/card0".into())).map_err(|_|"Invalid KMS device")?;
let (mut width,mut height)=(0,0);
let handle=unsafe { uu_kms_open(device.as_ptr(),&mut width,&mut height) };
if handle.is_null() { return Err("KMS: cannot open an active CRTC; check card permissions and connected output".into()); }
let capture=Self { handle:Mutex::new(Handle(handle)), width,height };
// Do not advertise a desktop until we have actually read the buffer.
let mut frame=vec![0; width as usize*height as usize*4];
capture.capture(&mut frame)?;
if frame.chunks_exact(4).all(|p| p[..3]==[0,0,0]) {
log::warn!("KMS initial frame is uniformly black; GPU buffer visibility is unverified (see KMS limitations)");
}
Ok(capture)
}
pub fn capture(&self,output:&mut [u8])->Result<(),String> {
if output.len()!=self.width as usize*self.height as usize*4 { return Err("KMS: invalid frame dimensions".into()); }
let handle=self.handle.lock().unwrap();
match unsafe { uu_kms_read(handle.0,output.as_mut_ptr(),output.len()) } {
0=>Ok(()), -2=>Err("KMS mode changed; restart host to refresh desktop geometry".into()),
-5=>Err("KMS framebuffer handles unavailable; requires cap_sys_admin+ep".into()),
code=>Err(format!("KMS capture failed ({code}); prototype needs a mappable linear XRGB/ARGB dumb buffer")),
}
}
}
}
#[cfg(all(feature="kms",target_os="linux"))]
pub use imp::Capture;
#[cfg(not(all(feature="kms",target_os="linux")))]
pub struct Capture { pub width:u32, pub height:u32 }
#[cfg(not(all(feature="kms",target_os="linux")))]
impl Capture {
pub fn open()->Result<Self,String> { Err("KMS requires Linux and --features kms".into()) }
pub fn capture(&self,_:&mut [u8])->Result<(),String> { Err("KMS requires Linux and --features kms".into()) }
}
+40 -2
View File
@@ -12,6 +12,7 @@ mod login;
mod login_ui;
mod session_lock;
mod capture;
mod kms;
mod file_trans;
mod input;
mod uinput;
@@ -28,6 +29,7 @@ mod damage_capture;
mod inproc_encoder;
mod capture_setting;
mod audio;
mod virtual_audio;
mod system_actions;
mod capability;
mod protocol;
@@ -55,7 +57,7 @@ use webrtc::WebRtcSession;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
// An unknown argument must never fall through to starting a second full host server.
const SUBCOMMANDS: [&str; 9] = ["login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
const SUBCOMMANDS: [&str; 11] = ["desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
"wayland-input-probe", "clipboard-acceptance", "portal-acceptance"];
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
@@ -164,6 +166,30 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let path=std::env::args().nth(2);
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),path).await.map_err(|e|e.into());
}
if std::env::args().nth(1).as_deref() == Some("desktop-input-probe") {
if std::fs::read_to_string("/proc/sys/kernel/hostname").unwrap_or_default().trim()!="uu-hyprland-test" {
return Err("Input acceptance is restricted to the isolated VM".into());
}
let screen=ScreenCapture::new()?;
let input=Arc::new(InputInjector::new());
input.bind_portal(screen.portal.clone());
let worker=input.start_pointer_worker();
// libinput discovers newly created uinput devices asynchronously.
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
input.mouse_absolute_normalized(0.25,0.25,(screen.width,screen.height));
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
input.mouse_absolute_normalized(0.5,0.5,(screen.width,screen.height));
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
input.mouse_button(1,true); input.mouse_button(1,false);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
input.mouse_relative(32,0);
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
for key in b"UUWAYLAND" { input.windows_key_event(i32::from(*key),true); input.windows_key_event(i32::from(*key),false); }
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
worker.abort(); input.release_held();
println!("Desktop test events sent; verify the GTK acceptance report");
return Ok(());
}
if std::env::args().nth(1).as_deref() == Some("wayland-input-probe") {
return wayland::input_probe().map_err(|e|e.into());
}
@@ -196,6 +222,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if std::env::args().nth(1).as_deref() == Some("unlock-check") {
return session_lock::check_from_stdin().map_err(|e| e.into());
}
if std::env::args().nth(1).as_deref() == Some("virtual-audio-probe") {
virtual_audio::init();
if !virtual_audio::enable() { return Err("Virtual audio probe setup failed".into()); }
println!("Virtual audio probe active");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
virtual_audio::disable();
if virtual_audio::active() { return Err("Virtual audio probe restore incomplete".into()); }
println!("Virtual audio probe ended");
return Ok(());
}
if std::env::args().nth(1).as_deref() == Some("login") {
// Interactive: register this host and bind it to an account via QR scan
// (GTK window when available; `login --no-gui` keeps it terminal-only).
@@ -203,6 +239,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if login::run(gui).await.is_err() { std::process::exit(1); } // error already shown
return Ok(());
}
virtual_audio::init();
let _audio_cleanup=virtual_audio::Cleanup;
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
let mut capture = Arc::new((if desktop_backend::headless() {
@@ -221,7 +259,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let shutdown_input=input.clone();
tokio::spawn(async move {
if let Ok(mut term)=tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
term.recv().await; shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
term.recv().await; virtual_audio::disable(); shutdown_input.release_held(); terminal::shutdown_all(); std::process::exit(0);
}
});
}
+10 -5
View File
@@ -113,10 +113,14 @@ pub fn args_shape(args: &[u8]) -> String {
}
fn mute_for_session(state: &mut HostState) {
if state.restore.is_none() { state.restore = host_muted(); }
set_host_muted(true);
if state.restore.is_none() {
state.restore = host_muted();
set_host_muted(true);
}
crate::virtual_audio::enable();
}
fn restore_mute(state: &mut HostState) {
crate::virtual_audio::disable();
if let Some(previous) = state.restore.take() { set_host_muted(previous); }
}
@@ -143,20 +147,20 @@ impl PeerActions {
let on = toggle_value(value, args);
state.settings.keep_mute = on;
if on { mute_for_session(&mut state); } else { restore_mute(&mut state); }
log::info!("System action mute (action {}): on={} muted={:?}", action, on, host_muted());
log::info!("System action mute (action {}): on={} virtual_sink={} default_muted={:?}", action, on, crate::virtual_audio::active(), host_muted());
}
// Keep the host muted after the session (远程结束后,恢复被控端声音设置 unchecked).
MUTE_PERMANENT => {
let on = toggle_value(value, args);
state.settings.mute_permanent = on;
if on { state.settings.keep_mute = true; mute_for_session(&mut state); }
log::info!("System action mute_permanent: on={} muted={:?}", on, host_muted());
log::info!("System action mute_permanent: on={} virtual_sink={} default_muted={:?}", on, crate::virtual_audio::active(), host_muted());
}
MUTE_RESTORE => {
state.settings.keep_mute = false;
state.settings.mute_permanent = false;
restore_mute(&mut state);
log::info!("System action mute_restore: muted={:?}", host_muted());
log::info!("System action mute_restore: virtual_sink={} default_muted={:?}", crate::virtual_audio::active(), host_muted());
}
SET_AUTO_LOCK => {
// Observed: enabling 远程结束后自动锁屏 sends value 2 and disabling value 1.
@@ -188,6 +192,7 @@ impl PeerActions {
let mut state = state().lock().unwrap();
state.desktop_sessions = state.desktop_sessions.saturating_sub(1);
if state.desktop_sessions > 0 { return; }
crate::virtual_audio::disable();
if !state.settings.mute_permanent { restore_mute(&mut state); }
if state.settings.auto_lock {
log::info!("Auto lock after remote session: ok={}", lock_session());
+78 -15
View File
@@ -53,9 +53,30 @@ struct CompleteFrame {
const RTP_CLOCK: u64 = 90_000;
/// `UU_VIDEO_ENCODER=cpu` keeps the virtual CPU display entirely off the GPU.
pub fn gpu_encoder_requested() -> bool {
std::env::var("UU_VIDEO_ENCODER").map_or(true, |v| v != "cpu")
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Encoder { Cpu, Nvenc, Vaapi }
impl Encoder {
pub(crate) fn requested() -> Self { Self::from_setting(std::env::var("UU_VIDEO_ENCODER").ok().as_deref()) }
fn from_setting(value: Option<&str>) -> Self {
match value { Some("cpu") => Self::Cpu, Some("vaapi") => Self::Vaapi, _ => Self::Nvenc }
}
pub(crate) fn name(self) -> &'static str { match self { Self::Cpu => "libx264", Self::Nvenc => "h264_nvenc", Self::Vaapi => "h264_vaapi" } }
}
pub(crate) fn vaapi_device() -> String { std::env::var("UU_VAAPI_DEVICE").unwrap_or_else(|_| "/dev/dri/renderD128".into()) }
/// NV12 conversion must precede upload; scaling runs on software frames, before hwupload.
fn vaapi_filter(output: Option<(u32, u32)>) -> String {
let scale = output.map_or(String::new(), |(w,h)| format!("scale={w}:{h}:flags=bilinear,"));
format!("{scale}format=nv12,hwupload")
}
fn vaapi_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
let (maxrate, buffer) = rate_args(settings);
// QVBR is not uniformly available on Intel/AMD drivers. VBR + qmin sets a quality
// floor while retaining the viewer's bitrate cap; unsupported drivers fall back to CPU.
["-c:v", "h264_vaapi", "-profile:v", "constrained_baseline", "-g", gop, "-bf", "0",
"-rc_mode", "VBR", "-b:v", &settings.max_kbps().map_or("16M".into(), |k| format!("{}k",k*3/4)),
"-maxrate", &maxrate, "-bufsize", &buffer, "-qmin", &settings.cq().to_string(), "-async_depth", "1", "-aud", "1"]
.into_iter().map(str::to_owned).collect()
}
/// Round UP (+1ns float guard) so webrtc's floor(duration*90000) yields exactly `ticks`.
@@ -121,12 +142,13 @@ async fn read_encoder(
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
) -> Result<(), String> {
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct).
if !gpu_encoder_requested() { return read_encoder_with(size,display,portal,latest,false,sequence,settings,idr).await; }
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),true,sequence.clone(),settings,idr.clone()).await {
let encoder = Encoder::requested();
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await; }
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone()).await {
Ok(())=>Ok(()),
Err(error) if !latest.is_closed()=>{
log::warn!("NVENC stream unavailable; switching scoped encoder to CPU: {}",error);
read_encoder_with(size,display,portal,latest,false,sequence,settings,idr).await
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name(),error);
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await
},
Err(error)=>Err(error),
}
@@ -167,10 +189,16 @@ fn rate_args(settings: &VideoSettings) -> (String, String) {
}
async fn read_encoder_with(
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,gpu:bool,sequence:Arc<std::sync::atomic::AtomicU64>,
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,sequence:Arc<std::sync::atomic::AtomicU64>,
mut settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
)->Result<(),String>{
let wayland=display.starts_with("wayland:");
let kms=display.starts_with("kms:");
let wayland=kms || display.starts_with("wayland:");
let kms_capture=if kms {
let capture=tokio::task::spawn_blocking(crate::kms::Capture::open).await.map_err(|_|"KMS worker failed")??;
if (capture.width,capture.height)!=size { return Err("KMS mode changed; restart host to refresh desktop geometry".into()); }
Some(Arc::new(capture))
} else { None };
#[cfg(feature = "inproc-av")]
if !wayland && portal.is_none() && crate::inproc_encoder::selected() {
let max_kbps = settings.max_kbps().unwrap_or(30_000) as i64;
@@ -178,11 +206,11 @@ async fn read_encoder_with(
display: display.clone(), input: size, output: settings.output, fps: u32::from(settings.fps),
gop: gop_frames(settings.fps, true) as u32, cq: settings.cq() as u32,
target_bps: settings.max_kbps().map_or(16_000, |k| i64::from(k) * 3 / 4) * 1000,
max_bps: max_kbps * 1000, buffer_bits: (max_kbps / 8).clamp(100, 500) * 1000, gpu,
max_bps: max_kbps * 1000, buffer_bits: (max_kbps / 8).clamp(100, 500) * 1000, encoder,
damage: crate::inproc_encoder::damage_enabled(),
};
log::info!("Desktop encoder: display={} source={}x{} output={:?} fps={} cq={} maxrate={}k encoder={} pipeline=in-process",
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,if gpu{"h264_nvenc"}else{"libx264"});
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name());
return read_inprocess(config, latest, sequence, idr).await;
}
let size = format!("{}x{}", size.0, size.1);
@@ -194,13 +222,14 @@ async fn read_encoder_with(
let (maxrate, bufsize) = rate_args(&settings);
let cq = settings.cq().to_string();
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,if gpu{"h264_nvenc"}else{"libx264"});
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name());
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
let mut command=match crate::config::ffmpeg_root() {
Some(root) => { let mut c=Command::new(root.join("bin/ffmpeg")); c.env("LD_LIBRARY_PATH",root.join("lib/x86_64-linux-gnu")); c }
None => Command::new("ffmpeg"),
};
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
if wayland {
command.args(["-f","rawvideo","-pixel_format","bgra","-video_size",&size,
"-framerate",&fps,"-i","pipe:0","-an"]);
@@ -210,10 +239,14 @@ async fn read_encoder_with(
"-video_size",&size,"-i",&display,"-an"]);
}
// 其他分辨率: scale the captured desktop (the host display mode is left untouched).
if let Some((width, height)) = settings.output {
if encoder == Encoder::Vaapi {
command.args(["-vf", &vaapi_filter(settings.output)]);
} else if let Some((width, height)) = settings.output {
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
}
if gpu{
if encoder == Encoder::Vaapi {
command.args(vaapi_options(&settings, &gop));
} else if encoder == Encoder::Nvenc {
command.args(["-c:v","h264_nvenc","-preset","p4","-tune","ull",
"-profile:v","baseline","-pix_fmt","yuv420p","-g",&gop,"-bf","0",
"-rc","vbr","-cq",&cq,"-b:v",&target,"-maxrate",&maxrate,"-bufsize",&bufsize,
@@ -237,7 +270,23 @@ async fn read_encoder_with(
.stderr(std::process::Stdio::inherit())
.spawn().map_err(|e| format!("Start scoped encoder: {e}"))?;
// Scope capture to this encoder. Dropping either task kills its child.
let _capture=if let Some(session)=portal {
let _capture=if let Some(capture)=kms_capture {
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
Some(EncoderTask(tokio::spawn(async move {
use tokio::io::AsyncWriteExt;
let mut tick=tokio::time::interval(Duration::from_nanos(1_000_000_000/u64::from(settings.fps)));
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
loop {
tick.tick().await;
let capture=capture.clone();
let bytes=tokio::task::spawn_blocking(move || {
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
capture.capture(&mut bytes)?; Ok::<_,String>(bytes)
}).await.map_err(|_|"KMS worker failed")??;
sink.write_all(&bytes).await.map_err(|_|"KMS encoder pipe failed")?;
}
})))
} else if let Some(session)=portal {
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
Some(EncoderTask(tokio::spawn(async move {
use tokio::io::AsyncWriteExt;
@@ -403,6 +452,20 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn vaapi_is_explicit_and_upload_follows_scale() {
assert_eq!(Encoder::from_setting(None), Encoder::Nvenc);
assert_eq!(Encoder::from_setting(Some("cpu")), Encoder::Cpu);
assert_eq!(Encoder::from_setting(Some("vaapi")), Encoder::Vaapi);
assert_eq!(vaapi_filter(None), "format=nv12,hwupload");
assert_eq!(vaapi_filter(Some((1280,720))), "scale=1280:720:flags=bilinear,format=nv12,hwupload");
let options = vaapi_options(&VideoSettings::default(), "30");
for pair in [["-g","30"], ["-bf","0"], ["-async_depth","1"], ["-aud","1"]] {
assert!(options.windows(2).any(|w| w[0] == pair[0] && w[1] == pair[1]));
}
assert!(!options.iter().any(|v| v == "-r" || v == "-pix_fmt"));
}
#[tokio::test]
async fn encoder_restart_stops_old_producer_before_starting_new_chain() {
use std::sync::atomic::{AtomicBool, Ordering};
+181
View File
@@ -0,0 +1,181 @@
//! Scoped PulseAudio / PipeWire-pulse routing. Never inspect or log audio payloads.
//! `pactl` is used when present; plain PipeWire installs without it (default Ubuntu desktop)
//! use `pw-cli`/`pw-dump`/`wpctl`, where WirePlumber moves default-following streams itself.
use std::process::{Command, Stdio};
use std::sync::{Mutex, OnceLock};
use std::os::fd::AsRawFd;
const SINK: &str = "uu-remote-sink";
const MONITOR: &str = "uu-remote-sink.monitor";
#[derive(serde::Serialize, serde::Deserialize)]
struct Route { previous: String, module: Option<u32>, #[serde(default)] pipewire: bool }
struct State { route: Option<Route>, _lock: std::fs::File }
static STATE: OnceLock<Mutex<Option<State>>> = OnceLock::new();
fn path() -> std::path::PathBuf { crate::config::path().with_file_name("virtual_audio.json") }
fn pactl(args: &[&str]) -> Result<String, ()> { run("pactl", args) }
fn run(program: &str, args: &[&str]) -> Result<String, ()> {
let out = Command::new(program).args(args).env("LC_ALL","C").stdin(Stdio::null()).stderr(Stdio::null()).output().map_err(|_| ())?;
if !out.status.success() { return Err(()); }
String::from_utf8(out.stdout).map(|s|s.trim().to_owned()).map_err(|_| ())
}
fn pipewire_only() -> bool { pactl(&["info"]).is_err() && run("wpctl",&["status"]).is_ok() }
/// Node ids whose node.name is `name`; `null_sink` restricts to null-audio-sink nodes (ownership).
fn pw_nodes(dump: &str, name: &str, null_sink: bool) -> Vec<u32> {
let Ok(serde_json::Value::Array(objects)) = serde_json::from_str(dump) else { return Vec::new() };
objects.iter().filter_map(|o| {
let props = o.get("info")?.get("props")?;
if props.get("node.name")?.as_str()? != name { return None; }
if null_sink && props.get("factory.name").and_then(|v| v.as_str()) != Some("support.null-audio-sink") { return None; }
u32::try_from(o.get("id")?.as_u64()?).ok()
}).collect()
}
fn pw_node(name: &str, null_sink: bool) -> Option<u32> { pw_nodes(&run("pw-dump",&["Node"]).ok()?, name, null_sink).first().copied() }
fn default_sink(pipewire: bool) -> Result<String, ()> {
if !pipewire { return pactl(&["get-default-sink"]); }
run("wpctl",&["inspect","@DEFAULT_AUDIO_SINK@"])?.lines()
.find_map(|l| l.trim().trim_start_matches("* ").strip_prefix("node.name = ").map(|v| v.trim_matches('"').to_owned())).ok_or(())
}
fn set_default(pipewire: bool, name: &str) -> Result<(), ()> {
if !pipewire { return pactl(&["set-default-sink",name]).map(|_| ()); }
run("wpctl",&["set-default",&pw_node(name,false).ok_or(())?.to_string()]).map(|_| ())
}
fn owned_modules(list: &str) -> Vec<u32> {
list.lines().filter_map(|line| {
let mut fields = line.split('\t');
let id = fields.next()?.parse().ok()?;
if fields.next()? != "module-null-sink" { return None; }
fields.next()?.split_whitespace().any(|arg| arg == "sink_name=uu-remote-sink").then_some(id)
}).collect()
}
fn sink_id(name: &str) -> Option<String> {
pactl(&["list","short","sinks"]).ok()?.lines().find_map(|line| {
let mut f=line.split_whitespace(); let id=f.next()?; (f.next()? == name).then(|| id.to_owned())
})
}
fn move_inputs(from: &str, to: &str) {
let Some(id)=sink_id(from) else { return };
if let Ok(list)=pactl(&["list","short","sink-inputs"]) {
for line in list.lines() {
let mut f=line.split_whitespace();
if let (Some(input),Some(sink))=(f.next(),f.next()) {
if sink==id { let _=pactl(&["move-sink-input",input,to]); }
}
}
}
}
fn persist(route: &Route) -> Result<(), ()> {
use std::os::unix::fs::OpenOptionsExt;
use std::io::Write;
let temp=path().with_extension("json.new");
let mut file=std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(&temp).map_err(|_| ())?;
file.write_all(&serde_json::to_vec(route).map_err(|_| ())?).map_err(|_| ())?;
file.sync_all().map_err(|_| ())?;
std::fs::rename(temp,path()).map_err(|_| ())
}
fn recover(route: &Route) -> Result<(), ()> {
// Do not overwrite a default explicitly changed by the user during remote control.
// The previous output may be gone (unplugged USB card): still unload our sink so the server
// picks another default instead of leaving every stream on a null sink.
let pw=route.pipewire;
if default_sink(pw).is_ok_and(|s| s == SINK) && set_default(pw,&route.previous).is_err() {
log::warn!("Previous default audio output unavailable; the sound server will choose a new default");
}
if pw {
for id in pw_nodes(&run("pw-dump",&["Node"])?,SINK,true) { run("pw-cli",&["destroy",&id.to_string()])?; }
} else {
move_inputs(SINK,&route.previous);
let modules=owned_modules(&pactl(&["list","short","modules"])?);
for id in modules { pactl(&["unload-module",&id.to_string()])?; }
}
let _=std::fs::remove_file(path());
Ok(())
}
fn state() -> &'static Mutex<Option<State>> {
STATE.get_or_init(|| {
use std::os::unix::fs::OpenOptionsExt;
let init=(|| {
std::fs::create_dir_all(path().parent().ok_or(())?).map_err(|_| ())?;
let lock=std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).mode(0o600)
.open(path().with_extension("lock")).map_err(|_| ())?;
if unsafe { libc::flock(lock.as_raw_fd(),libc::LOCK_EX|libc::LOCK_NB) } != 0 { return Err(()); }
let route=if path().exists() {
let old:Route=serde_json::from_slice(&std::fs::read(path()).map_err(|_| ())?).map_err(|_| ())?;
if recover(&old).is_err() { Some(old) } else { None }
} else { None };
Ok(State { route, _lock:lock })
})();
Mutex::new(init.ok())
})
}
/// Called on startup even if mute is disabled, to recover a previous crash.
pub fn init() { let _=state(); }
pub fn enable() -> bool {
let mut guard=state().lock().unwrap();
let Some(state)=guard.as_mut() else { return false };
if state.route.is_some() { return pactl(&["get-default-sink"]).is_ok_and(|s|s==SINK); }
let result=(|| {
let pipewire=pipewire_only();
let previous=default_sink(pipewire)?;
if previous==SINK { return Err(()); }
// Refuse to claim another instance's or manually-created sink.
if if pipewire { pw_node(SINK,false).is_some() } else { sink_id(SINK).is_some() } { return Err(()); }
let mut route=Route { previous, module:None, pipewire };
persist(&route)?; // Save before loading: also covers a crash between load and ID persistence.
state.route=Some(Route { previous:route.previous.clone(), module:None, pipewire });
let id=if pipewire {
// object.linger keeps the node after pw-cli exits; recover() destroys it.
run("pw-cli",&["create-node","adapter","{ factory.name=support.null-audio-sink node.name=uu-remote-sink \
node.description=UU-Remote media.class=Audio/Sink object.linger=true audio.position=[FL FR] }"])?;
let mut node=None;
for _ in 0..20 { node=pw_node(SINK,true); if node.is_some() { break; } std::thread::sleep(std::time::Duration::from_millis(50)); }
node.ok_or(())?
} else {
pactl(&["load-module","module-null-sink","sink_name=uu-remote-sink","sink_properties=device.description=UU-Remote"])?.parse().map_err(|_| ())?
};
route.module=Some(id);
persist(&route)?;
state.route=Some(route);
set_default(pipewire,SINK)?;
if !pipewire { move_inputs(&state.route.as_ref().ok_or(())?.previous,SINK); }
Ok(())
})();
if result.is_err() {
if let Some(route)=state.route.as_ref() { if recover(route).is_ok() { state.route=None; } }
log::warn!("Virtual audio routing unavailable; using the existing mute/monitor path");
}
result.is_ok()
}
pub fn disable() {
let mut guard=state().lock().unwrap();
if let Some(state)=guard.as_mut() {
if let Some(route)=state.route.as_ref() {
if recover(route).is_ok() { state.route=None; }
else { log::warn!("Virtual audio restore incomplete; recovery record retained"); }
}
}
}
pub fn active() -> bool { state().lock().unwrap().as_ref().is_some_and(|s|s.route.is_some()) }
pub fn source() -> String {
if state().lock().unwrap().as_ref().is_some_and(|s|s.route.as_ref().is_some_and(|r|r.module.is_some())) { MONITOR.into() }
else { std::env::var("UU_AUDIO_SOURCE").unwrap_or_else(|_| "@DEFAULT_MONITOR@".into()) }
}
#[cfg(test)]
mod tests {
use super::*;
#[test] fn recovery_only_claims_exact_null_sink() {
let modules="1\tmodule-null-sink\tsink_name=uu-remote-sink\t0\n2\tmodule-null-sink\tsink_name=uu-remote-sink-other\t0\n3\tmodule-other\tsink_name=uu-remote-sink\t0\n";
assert_eq!(owned_modules(modules),vec![1]);
}
#[test] fn pipewire_recovery_only_claims_owned_null_sink() {
let dump=r#"[{"id":5,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"support.null-audio-sink"}}},
{"id":6,"info":{"props":{"node.name":"uu-remote-sink","factory.name":"api.alsa.pcm.sink"}}},
{"id":7,"info":{"props":{"node.name":"other","factory.name":"support.null-audio-sink"}}},{"id":8}]"#;
assert_eq!(pw_nodes(dump,"uu-remote-sink",true),vec![5]);
assert_eq!(pw_nodes(dump,"uu-remote-sink",false),vec![5,6]);
let old:Route=serde_json::from_str(r#"{"previous":"a","module":3}"#).unwrap();
assert!(!old.pipewire);
}
}
/// Also restore on ordinary returns from the host; SIGKILL is recovered next startup.
pub struct Cleanup;
impl Drop for Cleanup { fn drop(&mut self) { disable(); } }