mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Compare commits
No files matched your search
@@ -110,6 +110,7 @@ viewer does not reconnect them automatically.
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | disable Ctrl+V → Ctrl+Shift+V in focused X11 terminals (Mac Cmd and Ctrl both arrive as VK_LCONTROL) |
|
||||
| `contrib/virtual-desktop/` | Xvfb + session (`UU_VIRTUAL_SESSION`, `UU_VIRTUAL_BUS=user|private`) for monitorless hosts; serve it with `DISPLAY=:99` |
|
||||
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: explicit Intel/AMD; `nvenc`: force; unset: NVENC→CPU only if `/proc/driver/nvidia/version` or `/dev/nvidia0` exists, else CPU (#10) |
|
||||
| `UU_VIDEO_CODEC=h264` | force H.264 even when the viewer negotiated H.265 (default: H.265 when both sides support it) |
|
||||
| `UU_FFMPEG_ROOT` | FFmpeg prefix (`bin/ffmpeg`, libs in `lib/x86_64-linux-gnu`) |
|
||||
| `UU_AUDIO=0`, `UU_AUDIO_SOURCE` | disable sound / choose the Pulse source (default `@DEFAULT_MONITOR@`) |
|
||||
| `UU_LOGIN_WINDOW=0` | don't auto-open the login window |
|
||||
@@ -126,7 +127,7 @@ every 60 s.
|
||||
| `src/signaling.rs` | Engine.IO v4 / Socket.IO client; `be-controlled` binary attachment = ConnectOptions (field 1 capture_type) |
|
||||
| `src/auth.rs`, `src/config.rs`, `src/login.rs`, `src/login_ui.rs` | signed REST client, config, device init + QR login, GTK window |
|
||||
| `src/webrtc.rs` | peer connection, data channels, CONTROL dispatch, initialization messages |
|
||||
| `src/video.rs`, `src/capture_setting.rs` | FFmpeg H.264 pipeline; viewer quality/fps/resolution settings |
|
||||
| `src/video.rs`, `src/capture_setting.rs`, `src/video_track.rs`, `src/hevc.rs` | FFmpeg H.264/H.265 pipeline, one video track for both codecs (RFC 7798 HEVC payloader); viewer quality/fps/resolution/chroma settings |
|
||||
| `src/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) |
|
||||
@@ -143,7 +144,7 @@ every 60 s.
|
||||
| `src/assist.rs`, `c_engine/assist_window.c` | remote assistance (guest + account) and the unified GTK main window (assist + account/QR card) |
|
||||
|
||||
Diagnostic subcommands: `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected),
|
||||
`desktop-probe [new.png]`, `desktop-video-probe`, `wayland-input-probe`, `clipboard-acceptance`,
|
||||
`desktop-probe [new.png]`, `desktop-video-probe` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto), `wayland-input-probe`, `clipboard-acceptance`,
|
||||
`portal-acceptance`, `help`. Unknown arguments exit with code 2. Builds up to v0.1.0 did **not** reject them and
|
||||
started a second full host server instead, so check that a subcommand exists in the binary you run.
|
||||
|
||||
@@ -164,7 +165,17 @@ started a second full host server instead, so check that a subcommand exists in
|
||||
- CaptureSettingRequest: field 1 is an **fps option index** (1=30, 2=60, 3=90); the real frame rate is
|
||||
field 18 `fpsCount`. Field 2 quality: 1 Fast, 2 General (高清 2M), 3 HD (超清 8M), 4 Bluray (原画 20M),
|
||||
5 Auto, 6 Custom. Resolution: 16/17 pixel size or 5/6, 10 resolution_type (3 = follow remote).
|
||||
Reply `RpcResponse{2 CaptureSettingResponse{}}` (no errors = applied) and send Message 25 CaptureSettingSync.
|
||||
Field 7 `chorma_format` (ChromaFormat 1 = 4:2:0, 3 = 4:4:4) is sent when 真彩 is toggled; ff_capture_setting versions
|
||||
are cumulative (3 CHROMA_SAMPLING, 4 CUSTOM_BITRATE, 5 DPI_SCALE, 6 HDR). 4:4:4 = NVENC `high444p` / x264 `high444`
|
||||
with `yuv444p`; VAAPI and inproc stay 4:2:0.
|
||||
Reply `RpcResponse{2 CaptureSettingResponse{}}` (no errors = applied) and send Message 25 CaptureSettingSync
|
||||
(field 3 = the chroma actually encoded).
|
||||
- ConnectOptions field 4 `decoder_cap_list` = DecoderCap {1 fps, 2 codec_type (1 H264, 2 H265), 3 width, 4 height,
|
||||
5 chroma_format}. The Mac viewer lists H.264 and H.265, each 4:2:0 and 4:4:4, up to 3840x2160@120.
|
||||
- Codec choice is the host's (official negotiateCodec): the Mac viewer offers H265 before H264 in SDP and
|
||||
CaptureSettingRequest field 13 only reports its current codec. The host sends CONTROL Message 18
|
||||
CodecNegotiation {1 EncoderCap {1 capture_mode=1, 2 fps, 3 codec_type, 4 width, 5 height}} at CONTROL open and
|
||||
selects H.265 when both m-line codecs are bound (`UU_VIDEO_CODEC=h264` forces H.264).
|
||||
- SystemMetrics (Message 13): GeneralMetric `2 system_sound_support` is an enum, **2 = supported** (1 = not
|
||||
supported); SettingMetric 1 keep_mute, 2 mute_permanent, 3 auto_lock, 5 file_transfer_save_location,
|
||||
8/9/10 permission states (2 = allow).
|
||||
|
||||
Generated
+71
-1
@@ -382,6 +382,22 @@ version = "0.9.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6"
|
||||
dependencies = [
|
||||
"core-foundation-sys",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation-sys"
|
||||
version = "0.8.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||
|
||||
[[package]]
|
||||
name = "cpufeatures"
|
||||
version = "0.2.17"
|
||||
@@ -969,6 +985,7 @@ dependencies = [
|
||||
"hyper",
|
||||
"hyper-util",
|
||||
"rustls 0.23.45",
|
||||
"rustls-native-certs",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tower-service",
|
||||
@@ -1417,6 +1434,12 @@ version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
|
||||
|
||||
[[package]]
|
||||
name = "openssl-probe"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
|
||||
|
||||
[[package]]
|
||||
name = "p256"
|
||||
version = "0.13.2"
|
||||
@@ -1797,6 +1820,7 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
"quinn",
|
||||
"rustls 0.23.45",
|
||||
"rustls-native-certs",
|
||||
"rustls-pki-types",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -1946,6 +1970,18 @@ dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-native-certs"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dab5152771c58876a2146916e53e35057e1a4dfa2b9df0f0305b07f611fdea4d"
|
||||
dependencies = [
|
||||
"openssl-probe",
|
||||
"rustls-pki-types",
|
||||
"schannel",
|
||||
"security-framework",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-pki-types"
|
||||
version = "1.15.1"
|
||||
@@ -1989,6 +2025,15 @@ version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
|
||||
|
||||
[[package]]
|
||||
name = "schannel"
|
||||
version = "0.1.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
@@ -2031,6 +2076,29 @@ dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "security-framework"
|
||||
version = "3.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"core-foundation",
|
||||
"core-foundation-sys",
|
||||
"libc",
|
||||
"security-framework-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "security-framework-sys"
|
||||
version = "2.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3"
|
||||
dependencies = [
|
||||
"core-foundation-sys",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "1.0.28"
|
||||
@@ -2649,8 +2717,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.12"
|
||||
version = "0.1.24"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
"cc",
|
||||
@@ -2664,6 +2733,7 @@ dependencies = [
|
||||
"log",
|
||||
"nix 0.29.0",
|
||||
"qrcode",
|
||||
"rand 0.8.8",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
||||
+4
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.12"
|
||||
version = "0.1.24"
|
||||
edition = "2021"
|
||||
authors = ["uu-remote-linux contributors"]
|
||||
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
||||
@@ -29,13 +29,15 @@ tokio-tungstenite = { version = "0.23", features = ["rustls-tls-webpki-roots"] }
|
||||
futures-util = "0.3"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls", "rustls-tls-native-roots"] }
|
||||
hmac = "0.12"
|
||||
sha2 = "0.10"
|
||||
hex = "0.4"
|
||||
base64 = "0.22"
|
||||
flate2 = "1.0"
|
||||
bytes = "1"
|
||||
async-trait = "0.1"
|
||||
rand = "0.8"
|
||||
webrtc = "0.10"
|
||||
libc = "0.2"
|
||||
nix = { version = "0.29", features = ["term", "process", "fs", "ioctl", "signal"] }
|
||||
|
||||
+24
-3
@@ -75,6 +75,8 @@ Wayland limits: single screen, fixed resolution. The native Hyprland backend now
|
||||
dynamic resolution yet; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
|
||||
permission; the permission is remembered once granted.
|
||||
|
||||
The native Wayland backend needs `wlr-output-power-management` from the compositor to wake a turned-off screen; without it only wake is unavailable — capture and encoding are unaffected.
|
||||
|
||||
Native Wayland experiments: `UU_WAYLAND_PIPELINE=0` disables pipelining for comparison.
|
||||
`UU_WAYLAND_DMABUF=1` explicitly tries dma-buf → GBM CPU mapping (off by default;
|
||||
requires runtime libgbm; `UU_WAYLAND_DMABUF_DEVICE` overrides `/dev/dri/renderD128`).
|
||||
@@ -88,13 +90,13 @@ mapping and hardware acceleration remain unverified. This is not zero-copy VAAPI
|
||||
| Feature | Status |
|
||||
|---|---|
|
||||
| Switching between multiple monitors | In progress |
|
||||
| True color (4:4:4) | Not implemented; not declared in the handshake, so the client does not enable it |
|
||||
| True color (4:4:4) | Supported (v0.1.15): the default FFmpeg pipeline with NVENC / libx264 encodes H.264 High 4:4:4; VAAPI and `inproc-av` stay 4:2:0 and report that to the client |
|
||||
| HDR | Not implemented |
|
||||
| Super screen / virtual display | Not implemented |
|
||||
| Privacy screen on the host | Not implemented |
|
||||
| Microphone (viewer audio played on the host) | Not implemented (needs a virtual sound card) |
|
||||
| Plugins such as the WoW queue notifier | Not supported; the client is told that no plugins exist |
|
||||
| Access before login (greeter / no user logged in) | The greeter cannot be controlled; without a logged-in user use headless mode or a virtual desktop (see below) |
|
||||
| Linux login screen | The system login screen (before anyone logs in to the Linux desktop) cannot be controlled yet; use headless mode or a virtual desktop (see below). This is about the Linux login: **without a UU account** the host can now be used through remote assistance |
|
||||
| Relay connections on restrictive networks | Uses the ICE servers provided by the UU cloud; complex network setups are not thoroughly tested |
|
||||
| Acting as a viewer (controlling other machines from Linux) | Out of scope |
|
||||
|
||||
@@ -191,6 +193,9 @@ connect from 开始协助 (remote assistance) in UU Remote.
|
||||
- **Signed in**: the host is in your device list and remote assistance keeps working (same device ID). A background
|
||||
service keeps the window hidden; open it with `uuremote-server show` or the "UU Remote Linux Host" launcher.
|
||||
Closing the window only hides it.
|
||||
- **Tray icon**: a background host shows a tray icon with Open main window, the allow switch and Quit. GNOME needs
|
||||
the AppIndicator extension (bundled on Ubuntu); Xfce / Cinnamon / KDE work as is. Quit stops `uuremote-native.service`;
|
||||
the launcher starts it again.
|
||||
- **Settings** (as in the official client): allow switch (off by default); verification by temporary code, custom code
|
||||
(8–16 letters and digits) or either; temporary code rotation after each session / hourly / 12 h / 24 h / never;
|
||||
copy buttons for the ID and the code. Stored in the 0600 config.
|
||||
@@ -257,6 +262,8 @@ Common environment variables:
|
||||
Host settings such as "lock after remote session" and "mute host" are stored in
|
||||
`~/.config/uuremote/host_settings.json`.
|
||||
|
||||
With external FFmpeg CPU encoding, the bitrate presets (Original / Ultra / HD / custom) use average bitrate (ABR) to avoid gray keyframes at 4K and low bitrates; Auto keeps constant rate factor (CRF).
|
||||
|
||||
## Let an AI agent install or develop it
|
||||
|
||||
If you use Claude Code, Codex, Cursor or a similar coding agent, have it read [AGENTS.md](AGENTS.md) first.
|
||||
@@ -308,6 +315,16 @@ Released under the [GPL-3.0](LICENSE): you may use, modify and redistribute it,
|
||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc), FFmpeg, GTK and the other open-source projects this builds on.
|
||||
- Most of the code was written with the help of Claude and Codex.
|
||||
|
||||
### H.265 / HEVC
|
||||
|
||||
When both sides support H.265 the host uses it (as the official host does: the host picks the codec from both
|
||||
capability lists; the official Mac viewer lists H265 first in its SDP). The first desktop connection probes a
|
||||
synthetic frame and announces encoder support with CodecNegotiation. `hevc_nvenc`, `libx265` and explicit
|
||||
`UU_VIDEO_ENCODER=vaapi` (`hevc_vaapi`); hardware failure falls back to software HEVC, then to H.264.
|
||||
`UU_VIDEO_CODEC=h264` forces H.264. 8-bit; true color 4:4:4 uses hevc_nvenc `rext` / libx265 `main444-8`.
|
||||
Verified with the official Mac viewer on an RTX 3090 (hevc_nvenc, including 4:4:4); libx265, hevc_vaapi and
|
||||
other viewers are not hardware-verified.
|
||||
|
||||
### VAAPI (opt-in / 显式启用)
|
||||
|
||||
`UU_VIDEO_ENCODER=vaapi` selects Intel/AMD H.264 VAAPI; `UU_VAAPI_DEVICE` defaults to
|
||||
@@ -351,10 +368,14 @@ cargo build --release --features wayland
|
||||
Omarchy uses Hyprland: run in its user session, select `UU_DESKTOP_BACKEND=wayland`,
|
||||
and retain `WAYLAND_DISPLAY` / `XDG_RUNTIME_DIR`. This requires wlr-screencopy and the
|
||||
virtual input protocols supported by Hyprland; other compositors may need `--features wayland,portal`.
|
||||
For uinput, load `uinput` and apply the uaccess rule above; seat ACLs require an active login.
|
||||
For uinput, load `uinput` (at boot: `echo uinput | sudo tee /etc/modules-load.d/uinput.conf`) and apply the uaccess rule above; seat ACLs require an active login.
|
||||
For unattended accounts, explicitly restrict `/dev/uinput` access to a dedicated group.
|
||||
This permission can inject into the whole seat, including lock/greeter screens.
|
||||
`UU_INPUT_BACKEND=uinput` requests kernel devices; verify startup actually reports uinput.
|
||||
In Hyprland / uwsm sessions `default.target` may start before the compositor exports `WAYLAND_DISPLAY`;
|
||||
tie the unit to the graphical session instead (community-verified autostart on Omarchy):
|
||||
`After=` and `PartOf=graphical-session.target`, `ConditionEnvironment=WAYLAND_DISPLAY`,
|
||||
`Environment=UU_DESKTOP_BACKEND=wayland` (no `DISPLAY`), and `WantedBy=graphical-session.target`.
|
||||
Optional VAAPI: `intel-media-driver` (modern Intel) or `libva-mesa-driver` (AMD), `libva-utils`
|
||||
for `vainfo`. Optional KMS: `libdrm`. Virtual Xfce: `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`.
|
||||
|
||||
@@ -66,6 +66,8 @@ Portal 仅作用于本服务获授权的会话。文件传输、终端等独立
|
||||
|
||||
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
|
||||
|
||||
原生 Wayland 后端需要合成器提供 `wlr-output-power-management` 才能唤醒已关闭的屏幕;缺少该协议不影响采集和编码。
|
||||
|
||||
原生 Wayland 实验选项:`UU_WAYLAND_PIPELINE=0` 可关闭流水线做对照。`UU_WAYLAND_DMABUF=1` 显式尝试 dma-buf→GBM CPU 映射(默认关闭,需要运行时 libgbm;可用 `UU_WAYLAND_DMABUF_DEVICE` 指定 render node,默认 `/dev/dri/renderD128`)。仅支持显式线性修饰符、单平面 8-bit RGB,失败回退 SHM;virgl VM 中只验证了回退,成功导入/映射和硬件加速均未验证。这不是零拷贝 VAAPI 路径;选择 `UU_VIDEO_ENCODER=vaapi` 时仍由 FFmpeg 上传 CPU 帧。
|
||||
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
|
||||
|
||||
@@ -74,13 +76,13 @@ Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端
|
||||
| 功能 | 状态 |
|
||||
|---|---|
|
||||
| 多显示器切换 | 开发中 |
|
||||
| 真彩(4:4:4) | 未实现,握手时不声明,客户端不会启用 |
|
||||
| 真彩(4:4:4) | 支持(v0.1.15):NVENC / libx264 的默认 FFmpeg 管线输出 H.264 High 4:4:4;VAAPI 和 `inproc-av` 管线仍为 4:2:0,并如实回报给客户端 |
|
||||
| HDR | 未实现 |
|
||||
| 超级屏 / 虚拟显示器 | 未实现 |
|
||||
| 被控端防窥模式 | 未实现 |
|
||||
| 麦克风(控制端声音传到被控端) | 未实现(需要虚拟声卡) |
|
||||
| 魔兽世界排队提醒等插件 | 不支持,会明确告诉客户端「无插件」 |
|
||||
| 登录前(登录界面 / 无人登录)访问 | 不支持控制登录界面;没有人登录时可以用无头模式或虚拟桌面(见下文) |
|
||||
| Linux 系统登录界面 | 还不支持控制开机后的系统登录界面(还没有人登录 Linux 桌面时);这种情况可以用无头模式或虚拟桌面(见下文)。注意:这里说的是 Linux 系统登录,**不登录 UU 账号**现在可以通过「远程协助」使用 |
|
||||
| 跨运营商 / 受限网络的中继连接 | 已接入官方下发的 ICE 服务器,但复杂网络环境未充分测试 |
|
||||
| 作为控制端(在 Linux 上控制别的电脑) | 不在本项目范围 |
|
||||
|
||||
@@ -171,6 +173,7 @@ X11 默认继续用 XTest,因为它能正确处理网络延迟下按住不放
|
||||
|
||||
- **未登录**:启动后直接显示主窗口,用访客身份提供远程协助,同时显示扫码登录二维码。扫码登录后自动切换到账号模式。
|
||||
- **已登录**:本机出现在你的设备列表里,可以直接连;远程协助也照常可用(同一设备 ID)。服务在后台运行时窗口默认隐藏,用 `uuremote-server show` 或应用菜单里的「UU 远程 Linux 主机」打开。关闭窗口只是隐藏,不会停止服务。
|
||||
- **托盘图标**:后台运行时顶栏/任务栏有托盘图标,菜单里可以「打开主窗口」、切换「允许他人远程协助」、「退出」。GNOME 需要 AppIndicator 扩展(Ubuntu 自带),Xfce / Cinnamon / KDE 直接可用。从托盘退出会停止 `uuremote-native.service`,之后从应用菜单打开会重新启动服务。
|
||||
- **设置**(与官方一致):「允许他人远程协助」开关(默认关闭);验证方式:仅临时验证码 / 仅自定义验证码(8–16 位,需同时包含字母和数字) / 两种都可以;临时验证码自动刷新:每次远控结束 / 每小时 / 每 12 小时 / 每 24 小时 / 不刷新;ID 和验证码旁各有「复制」按钮。设置保存在本机配置文件(0600)。
|
||||
- **只在本机窗口显示**:验证码不会写进日志。没有图形界面时,单独运行 `uuremote-server assist` 会在终端显示(`allow` / `deny` / `reset` / `mode …` / `refresh …` / `off`)。
|
||||
- **安全**:拿到 ID 和验证码的人可以完全控制这台电脑(桌面、文件、终端)。只发给信任的人,用完关掉开关或刷新验证码。
|
||||
@@ -230,6 +233,8 @@ systemctl --user restart uuremote-native.service
|
||||
|
||||
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。
|
||||
|
||||
选择码率档(原画 / 超清 / 高清 / 自定义)时,外部 FFmpeg 的 CPU 编码以平均码率(ABR)为目标,避免 4K 低码率下关键帧发灰;「自动」档仍使用恒定质量(CRF)。
|
||||
|
||||
## 让 AI 帮你安装或开发
|
||||
|
||||
如果你用 Claude Code、Codex、Cursor 等 AI 编程工具,让它先阅读 [AGENTS.md](AGENTS.md)。
|
||||
@@ -274,6 +279,14 @@ flowchart LR
|
||||
- [webrtc-rs](https://github.com/webrtc-rs/webrtc)、FFmpeg、GTK 等开源项目。
|
||||
- 本项目的大部分代码在 Claude 和 Codex 的协助下完成。
|
||||
|
||||
### H.265 / HEVC
|
||||
|
||||
控制端和主机都支持 H.265 时,主机自动使用 H.265(与官方主机一致,由主机按双方能力选择;官方 Mac 客户端在 SDP
|
||||
中优先列出 H.265)。首次桌面连接会用合成帧探测实际编码能力,并通过 CodecNegotiation 告知客户端。支持 `hevc_nvenc`、
|
||||
`libx265`、显式 `UU_VIDEO_ENCODER=vaapi` 的 `hevc_vaapi`;硬件失败先回落软件 HEVC,再回落 H.264。
|
||||
`UU_VIDEO_CODEC=h264` 强制使用 H.264。8-bit;真彩 4:4:4 使用 hevc_nvenc `rext` / libx265 `main444-8`。
|
||||
已用官方 Mac 客户端在 RTX 3090(hevc_nvenc,含 4:4:4)实机验收;libx265、hevc_vaapi 及其他客户端尚未实机验证。
|
||||
|
||||
### VAAPI 硬件编码(显式启用)
|
||||
|
||||
设置 `UU_VIDEO_ENCODER=vaapi` 使用 Intel/AMD H.264 VAAPI;`UU_VAAPI_DEVICE` 默认
|
||||
@@ -310,10 +323,27 @@ cargo build --release --features wayland
|
||||
|
||||
Omarchy 使用 Hyprland:在其用户会话中运行,设置 `UU_DESKTOP_BACKEND=wayland`,保留
|
||||
`WAYLAND_DISPLAY` 和 `XDG_RUNTIME_DIR`。其他合成器可能需要 `--features wayland,portal`。
|
||||
uinput 需加载内核模块并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
|
||||
uinput 需加载内核模块(开机自动加载:`echo uinput | sudo tee /etc/modules-load.d/uinput.conf`)并应用上文 uaccess 规则;无人登录账号需要专用组的受限授权。
|
||||
该权限能向整个 seat(包括锁屏/登录界面)注入输入。设置 `UU_INPUT_BACKEND=uinput` 后,
|
||||
仍须确认启动日志实际选择了 uinput。
|
||||
|
||||
Hyprland / uwsm 等会话中,`default.target` 可能早于合成器导出 `WAYLAND_DISPLAY`。systemd 单元可改为随图形会话启动
|
||||
(社区在 Omarchy 上验证过开机自启):
|
||||
|
||||
```ini
|
||||
[Unit]
|
||||
After=graphical-session.target
|
||||
PartOf=graphical-session.target
|
||||
ConditionEnvironment=WAYLAND_DISPLAY
|
||||
|
||||
[Service]
|
||||
Environment=UU_DESKTOP_BACKEND=wayland
|
||||
# 其余同上文 X11 示例,去掉 DISPLAY
|
||||
|
||||
[Install]
|
||||
WantedBy=graphical-session.target
|
||||
```
|
||||
|
||||
VAAPI 可选包:Intel 新显卡用 `intel-media-driver`,AMD 用 `libva-mesa-driver`,`libva-utils` 提供
|
||||
`vainfo`。KMS 依赖 `libdrm`。虚拟 Xfce 依赖 `xorg-server-xvfb xorg-xauth xorg-xdpyinfo
|
||||
xfce4-session xfce4-panel xfdesktop xfwm4 gnome-terminal`。
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <stdatomic.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
static const char *CSS =
|
||||
"window { background-color: #f5f6f8; }\n"
|
||||
@@ -27,7 +28,7 @@ static const char *CSS =
|
||||
".status.ok { color: #16a34a; }\n"
|
||||
".status.error { color: #dc2626; }\n";
|
||||
|
||||
enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8 };
|
||||
enum { CMD_NONE = 0, CMD_ALLOW = 1, CMD_DENY = 2, CMD_RESET = 3, CMD_STOP = 4, CMD_VERIFY = 5, CMD_REFRESH = 6, CMD_CUSTOM = 7, CMD_LOGOUT = 8, CMD_QUIT = 9 };
|
||||
enum { VERIFY_TEMPORARY = 0, VERIFY_CUSTOM = 1, VERIFY_BOTH = 2 };
|
||||
|
||||
typedef struct { char *id; char *code; int allowed, verify, refresh, has_custom; } State;
|
||||
@@ -47,6 +48,9 @@ static GtkWidget *refresh_items[5], *footer_label;
|
||||
// Account card: QR login before sign-in, user info (+ logout) after.
|
||||
static GtkWidget *assist_card, *account_card, *account_stack, *qr_stack, *qr_image, *login_status, *user_label;
|
||||
static int show_assist = 1, show_account;
|
||||
// Tray (host service only): AppIndicator via dlopen (no build dependency), else GtkStatusIcon.
|
||||
static GtkWidget *tray_menu, *tray_allow;
|
||||
static int suppress_tray;
|
||||
static int cur_verify, cur_refresh, cur_has_custom;
|
||||
static char current_id[64], current_code[64];
|
||||
static int code_visible = 1, suppress_switch, start_hidden, close_hides;
|
||||
@@ -274,6 +278,68 @@ static GtkWidget *build_account_card(void) {
|
||||
return card;
|
||||
}
|
||||
|
||||
static void present_window(void) { if (window) { gtk_widget_show(window); gtk_window_present(GTK_WINDOW(window)); } }
|
||||
static void on_tray_open(GtkMenuItem *i, gpointer d) { (void)i; (void)d; present_window(); }
|
||||
static void on_tray_allow(GtkCheckMenuItem *i, gpointer d) {
|
||||
(void)d;
|
||||
if (!suppress_tray) post(gtk_check_menu_item_get_active(i) ? CMD_ALLOW : CMD_DENY);
|
||||
}
|
||||
static void on_tray_quit(GtkMenuItem *i, gpointer d) {
|
||||
(void)i; (void)d;
|
||||
GtkWidget *dlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
|
||||
"退出后本机将下线:设备列表和远程协助都无法连接,直到再次启动。确定退出吗?");
|
||||
gtk_window_set_title(GTK_WINDOW(dlg), "UU 远程 Linux 主机");
|
||||
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) post(CMD_QUIT);
|
||||
gtk_widget_destroy(dlg);
|
||||
}
|
||||
static void on_status_activate(GtkStatusIcon *i, gpointer d) { (void)i; (void)d; present_window(); }
|
||||
static void on_status_menu(GtkStatusIcon *i, guint button, guint time, gpointer d) {
|
||||
(void)i; (void)button; (void)time; (void)d;
|
||||
gtk_menu_popup_at_pointer(GTK_MENU(tray_menu), NULL);
|
||||
}
|
||||
static void build_tray(void) {
|
||||
tray_menu = gtk_menu_new();
|
||||
GtkWidget *open = gtk_menu_item_new_with_label("打开主窗口");
|
||||
g_signal_connect(open, "activate", G_CALLBACK(on_tray_open), NULL);
|
||||
tray_allow = gtk_check_menu_item_new_with_label("允许他人远程协助");
|
||||
g_signal_connect(tray_allow, "toggled", G_CALLBACK(on_tray_allow), NULL);
|
||||
GtkWidget *quit = gtk_menu_item_new_with_label("退出");
|
||||
g_signal_connect(quit, "activate", G_CALLBACK(on_tray_quit), NULL);
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), open);
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), tray_allow);
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), gtk_separator_menu_item_new());
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(tray_menu), quit);
|
||||
gtk_widget_show_all(tray_menu);
|
||||
const char *icon = gtk_icon_theme_has_icon(gtk_icon_theme_get_default(), "uu-remote-linux") ? "uu-remote-linux" : "computer";
|
||||
|
||||
// StatusNotifierItem (GNOME AppIndicator extension, KDE): libayatana-appindicator3 / libappindicator3.
|
||||
const char *libs[] = { "libayatana-appindicator3.so.1", "libappindicator3.so.1" };
|
||||
for (int i = 0; i < 2; i++) {
|
||||
void *h = dlopen(libs[i], RTLD_LAZY | RTLD_LOCAL);
|
||||
if (!h) continue;
|
||||
void *(*ind_new)(const char *, const char *, int) = (void *(*)(const char *, const char *, int))dlsym(h, "app_indicator_new");
|
||||
void (*set_status)(void *, int) = (void (*)(void *, int))dlsym(h, "app_indicator_set_status");
|
||||
void (*set_menu)(void *, GtkMenu *) = (void (*)(void *, GtkMenu *))dlsym(h, "app_indicator_set_menu");
|
||||
void (*set_title)(void *, const char *) = (void (*)(void *, const char *))dlsym(h, "app_indicator_set_title");
|
||||
if (ind_new && set_status && set_menu) {
|
||||
void *ind = ind_new("uu-remote-linux", icon, 0 /* APPLICATION_STATUS */);
|
||||
if (set_title) set_title(ind, "UU 远程 Linux 主机");
|
||||
set_menu(ind, GTK_MENU(tray_menu));
|
||||
set_status(ind, 1 /* ACTIVE */);
|
||||
return; // keep the library loaded for the indicator's lifetime
|
||||
}
|
||||
dlclose(h);
|
||||
}
|
||||
// XEmbed system tray (Xfce, Cinnamon, MATE...).
|
||||
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
||||
GtkStatusIcon *status = gtk_status_icon_new_from_icon_name(icon);
|
||||
gtk_status_icon_set_tooltip_text(status, "UU 远程 Linux 主机");
|
||||
gtk_status_icon_set_title(status, "UU 远程 Linux 主机");
|
||||
g_signal_connect(status, "activate", G_CALLBACK(on_status_activate), NULL);
|
||||
g_signal_connect(status, "popup-menu", G_CALLBACK(on_status_menu), NULL);
|
||||
G_GNUC_END_IGNORE_DEPRECATIONS
|
||||
}
|
||||
|
||||
static void build_window(void) {
|
||||
GtkCssProvider *css = gtk_css_provider_new();
|
||||
gtk_css_provider_load_from_data(css, CSS, -1, NULL);
|
||||
@@ -398,6 +464,7 @@ static void build_window(void) {
|
||||
gtk_box_pack_end(GTK_BOX(root), footer_label, FALSE, FALSE, 0);
|
||||
if (close_hides) gtk_label_set_text(GTK_LABEL(footer_label), "开源 Linux 被控端 · 非网易官方客户端 · 关闭窗口后远程协助仍在后台运行");
|
||||
render_code();
|
||||
if (close_hides) build_tray();
|
||||
gtk_widget_show_all(gtk_bin_get_child(GTK_BIN(window)));
|
||||
gtk_widget_set_visible(assist_card, show_assist);
|
||||
gtk_widget_set_visible(hint_label, show_assist);
|
||||
@@ -465,6 +532,7 @@ static gboolean apply_state(gpointer data) {
|
||||
suppress_switch = 1;
|
||||
gtk_switch_set_active(GTK_SWITCH(allow_switch), s->allowed);
|
||||
suppress_switch = 0;
|
||||
if (tray_allow) { suppress_tray = 1; gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(tray_allow), s->allowed); suppress_tray = 0; }
|
||||
cur_verify = s->verify; cur_refresh = s->refresh; cur_has_custom = s->has_custom;
|
||||
suppress_combo = 1; gtk_combo_box_set_active(GTK_COMBO_BOX(verify_combo), s->verify); suppress_combo = 0;
|
||||
if (s->refresh >= 0 && s->refresh < 5) {
|
||||
|
||||
@@ -51,6 +51,7 @@ struct uu_portal {
|
||||
uint8_t *frame;
|
||||
size_t frame_length;
|
||||
uint64_t sequence;
|
||||
uint64_t revision;
|
||||
};
|
||||
#include "portal_clipboard.inc"
|
||||
|
||||
@@ -182,6 +183,7 @@ static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
|
||||
memset(p->frame, 0, p->frame_length);
|
||||
for (size_t pixel = 0; pixel < p->frame_length / 4; pixel++) p->frame[pixel * 4 + 3] = 255;
|
||||
p->sequence++;
|
||||
p->revision++;
|
||||
return;
|
||||
}
|
||||
struct spa_meta_region *crop = spa_buffer_find_meta_data(spa, SPA_META_VideoCrop, sizeof(*crop));
|
||||
@@ -203,6 +205,7 @@ static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
|
||||
}
|
||||
}
|
||||
p->sequence++;
|
||||
p->revision++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -418,14 +421,22 @@ void uu_portal_reset_frame(struct uu_portal *p) {
|
||||
if (!p) return;
|
||||
pthread_mutex_lock(&p->lock);
|
||||
p->sequence = 0;
|
||||
p->revision++;
|
||||
if (p->frame) memset(p->frame, 0, p->frame_length);
|
||||
pthread_mutex_unlock(&p->lock);
|
||||
}
|
||||
int uu_portal_capture(struct uu_portal *p, uint8_t *frame, size_t length) {
|
||||
return uu_portal_capture_since(p, frame, length, NULL);
|
||||
}
|
||||
int uu_portal_capture_since(struct uu_portal *p, uint8_t *frame, size_t length, uint64_t *revision) {
|
||||
if (!p || !frame) return 11;
|
||||
pthread_mutex_lock(&p->lock);
|
||||
int result = atomic_load(&p->closed) ? 9 : p->error ? p->error : !p->sequence ? 10 : length != p->frame_length ? 11 : 0;
|
||||
if (!result) memcpy(frame, p->frame, length);
|
||||
if (!result) {
|
||||
if (revision && *revision == p->revision) result = 12;
|
||||
else memcpy(frame, p->frame, length);
|
||||
if (revision) *revision = p->revision;
|
||||
}
|
||||
pthread_mutex_unlock(&p->lock); return result;
|
||||
}
|
||||
int uu_portal_alive(struct uu_portal *p) {
|
||||
|
||||
@@ -16,6 +16,7 @@ int uu_portal_clipboard_poll(struct uu_portal *portal, uint8_t *text, size_t cap
|
||||
int uu_portal_alive(struct uu_portal *portal);
|
||||
void uu_portal_reset_frame(struct uu_portal *portal);
|
||||
int uu_portal_capture(struct uu_portal *portal, uint8_t *bgra, size_t length);
|
||||
int uu_portal_capture_since(struct uu_portal *portal, uint8_t *bgra, size_t length, uint64_t *revision);
|
||||
int uu_portal_move(struct uu_portal *portal, double x, double y, int relative);
|
||||
int uu_portal_button(struct uu_portal *portal, int button, int pressed);
|
||||
int uu_portal_key(struct uu_portal *portal, uint32_t keysym, int pressed);
|
||||
|
||||
+94
-32
@@ -21,6 +21,7 @@
|
||||
|
||||
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static struct wl_display *display;
|
||||
static struct wl_registry *registry;
|
||||
static struct wl_shm *shm;
|
||||
static struct wl_seat *seat;
|
||||
static struct zwlr_screencopy_manager_v1 *copy_manager;
|
||||
@@ -65,18 +66,22 @@ static int initialized;
|
||||
/* Independent slots let the compositor fill the next frame during delivery. */
|
||||
struct frame {
|
||||
struct zwlr_screencopy_frame_v1 *handle;
|
||||
struct wl_buffer *buffer;
|
||||
void *pixels;
|
||||
size_t length;
|
||||
struct wl_buffer *buffer,*shm_buffer;
|
||||
struct wl_shm_pool *shm_pool;
|
||||
void *pixels,*shm_pixels;
|
||||
size_t shm_length;
|
||||
int shm_fd,shm_busy;
|
||||
uint32_t shm_width,shm_height,shm_stride,shm_format;
|
||||
uint32_t width,height,stride,format,flags;
|
||||
int done,failed,submitted;
|
||||
int done,failed,submitted,released;
|
||||
struct zwp_linux_buffer_params_v1 *params;
|
||||
struct gbm_bo *bo;
|
||||
int bo_fd,dmabuf_used;
|
||||
void *map_data;
|
||||
uint32_t dma_format,dma_width,dma_height;
|
||||
uint32_t bo_format,bo_width,bo_height;
|
||||
};
|
||||
static struct frame frames[2];
|
||||
static struct frame frames[2]={{.shm_fd=-1,.bo_fd=-1},{.shm_fd=-1,.bo_fd=-1}};
|
||||
static unsigned pending;
|
||||
|
||||
static uint32_t timestamp(void) {
|
||||
@@ -177,15 +182,37 @@ static void dma_init(void) {
|
||||
gbm.fd=open(device && *device ? device:"/dev/dri/renderD128",O_RDWR|O_CLOEXEC);
|
||||
if(gbm.fd<0 || !(gbm.device=gbm.create_device(gbm.fd))) dma_disable("render-device");
|
||||
}
|
||||
static void shm_clear(struct frame *frame) {
|
||||
if(frame->shm_buffer) wl_buffer_destroy(frame->shm_buffer);
|
||||
if(frame->shm_pool) wl_shm_pool_destroy(frame->shm_pool);
|
||||
if(frame->shm_pixels) munmap(frame->shm_pixels,frame->shm_length);
|
||||
if(frame->shm_fd>=0) close(frame->shm_fd);
|
||||
frame->shm_buffer=NULL; frame->shm_pool=NULL; frame->shm_pixels=NULL;
|
||||
frame->shm_length=0; frame->shm_fd=-1; frame->shm_busy=0;
|
||||
frame->shm_width=frame->shm_height=frame->shm_stride=frame->shm_format=0;
|
||||
}
|
||||
static void dma_clear(struct frame *frame) {
|
||||
if(frame->buffer) wl_buffer_destroy(frame->buffer);
|
||||
if(frame->dmabuf_used && frame->pixels) {
|
||||
gbm.bo_unmap(frame->bo,frame->map_data); frame->pixels=NULL; frame->map_data=NULL;
|
||||
}
|
||||
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
|
||||
frame->buffer=NULL; frame->bo=NULL; frame->bo_fd=-1; frame->dmabuf_used=frame->released=0;
|
||||
frame->bo_format=frame->bo_width=frame->bo_height=0;
|
||||
}
|
||||
static void frame_clear(struct frame *frame) {
|
||||
if(frame->handle) zwlr_screencopy_frame_v1_destroy(frame->handle);
|
||||
if(frame->params) zwp_linux_buffer_params_v1_destroy(frame->params);
|
||||
if(frame->buffer) wl_buffer_destroy(frame->buffer);
|
||||
if(frame->dmabuf_used && frame->pixels) gbm.bo_unmap(frame->bo,frame->map_data);
|
||||
else if(frame->pixels) munmap(frame->pixels,frame->length);
|
||||
if(frame->bo) { if(frame->bo_fd>=0)close(frame->bo_fd); gbm.bo_destroy(frame->bo); }
|
||||
memset(frame,0,sizeof(*frame));
|
||||
if(frame->shm_busy) shm_clear(frame);
|
||||
frame->handle=NULL; frame->params=NULL;
|
||||
frame->pixels=NULL; frame->map_data=NULL; frame->dmabuf_used=0;
|
||||
frame->width=frame->height=frame->stride=frame->format=frame->flags=0;
|
||||
frame->done=frame->failed=frame->submitted=0;
|
||||
frame->dma_format=frame->dma_width=frame->dma_height=0;
|
||||
}
|
||||
static void buffer_release(void*d,struct wl_buffer*b) { ((struct frame*)d)->released=1; }
|
||||
static const struct wl_buffer_listener buffer_listener={buffer_release};
|
||||
static void frame_fail(struct frame *frame) { frame->failed=frame->done=frame->submitted=1; }
|
||||
static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h,uint32_t s) {
|
||||
struct frame *frame=d;
|
||||
@@ -194,22 +221,32 @@ static void copy_buffer(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,ui
|
||||
(fmt!=WL_SHM_FORMAT_XRGB8888 && fmt!=WL_SHM_FORMAT_ARGB8888 &&
|
||||
fmt!=WL_SHM_FORMAT_XBGR8888 && fmt!=WL_SHM_FORMAT_ABGR8888)) { frame_fail(frame); return; }
|
||||
size_t length=(size_t)s*h;
|
||||
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
|
||||
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
|
||||
frame->pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
|
||||
if (frame->pixels==MAP_FAILED) { frame->pixels=NULL; close(fd); frame_fail(frame); return; }
|
||||
frame->length=length;
|
||||
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
|
||||
frame->buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
|
||||
wl_shm_pool_destroy(pool); close(fd);
|
||||
if (frame->shm_busy || !frame->shm_buffer || !frame->shm_pool || !frame->shm_pixels || frame->shm_fd<0 ||
|
||||
frame->shm_length!=length || frame->shm_width!=w || frame->shm_height!=h ||
|
||||
frame->shm_stride!=s || frame->shm_format!=fmt) {
|
||||
shm_clear(frame);
|
||||
int fd=memfd_create("uuremote-wayland-frame",MFD_CLOEXEC);
|
||||
if (fd<0 || ftruncate(fd,(off_t)length)!=0) { if(fd>=0)close(fd); frame_fail(frame); return; }
|
||||
void *pixels=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
|
||||
if (pixels==MAP_FAILED) { close(fd); frame_fail(frame); return; }
|
||||
struct wl_shm_pool *pool=wl_shm_create_pool(shm,fd,(int32_t)length);
|
||||
if (!pool) { munmap(pixels,length); close(fd); frame_fail(frame); return; }
|
||||
struct wl_buffer *buffer=wl_shm_pool_create_buffer(pool,0,w,h,s,fmt);
|
||||
if (!buffer) { wl_shm_pool_destroy(pool); munmap(pixels,length); close(fd); frame_fail(frame); return; }
|
||||
frame->shm_fd=fd; frame->shm_pixels=pixels; frame->shm_length=length;
|
||||
frame->shm_pool=pool; frame->shm_buffer=buffer;
|
||||
frame->shm_width=w; frame->shm_height=h; frame->shm_stride=s; frame->shm_format=fmt;
|
||||
}
|
||||
frame->pixels=frame->shm_pixels;
|
||||
/* copy is valid immediately for SHM, including frame versions <3. */
|
||||
if (zwlr_screencopy_frame_v1_get_version(f)<3) {
|
||||
zwlr_screencopy_frame_v1_copy(f,frame->buffer); frame->submitted=1;
|
||||
zwlr_screencopy_frame_v1_copy(f,frame->shm_buffer); frame->shm_busy=frame->submitted=1;
|
||||
}
|
||||
}
|
||||
static void copy_flags(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t value) { ((struct frame*)d)->flags=value; }
|
||||
static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint32_t lo,uint32_t ns) {
|
||||
struct frame *frame=d;
|
||||
frame->shm_busy=0;
|
||||
if(frame->dmabuf_used) {
|
||||
/* GBM READ mapping synchronizes readback, possibly via a staging buffer. */
|
||||
uint32_t stride=0;
|
||||
@@ -223,7 +260,7 @@ static void copy_ready(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t hi,uint
|
||||
}
|
||||
frame->done=1;
|
||||
}
|
||||
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { frame_fail(d); }
|
||||
static void copy_failed(void*d,struct zwlr_screencopy_frame_v1*f) { ((struct frame*)d)->shm_busy=0; frame_fail(d); }
|
||||
static void copy_damage(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t x,uint32_t y,uint32_t w,uint32_t h) {}
|
||||
static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,uint32_t w,uint32_t h) {
|
||||
struct frame *frame=d;
|
||||
@@ -232,30 +269,40 @@ static void copy_dmabuf(void*d,struct zwlr_screencopy_frame_v1*f,uint32_t fmt,ui
|
||||
}
|
||||
}
|
||||
static void submit_shm(struct frame *frame) {
|
||||
if(frame->buffer && !frame->failed) {
|
||||
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); frame->submitted=1;
|
||||
if(frame->shm_buffer && !frame->shm_busy && !frame->failed) {
|
||||
zwlr_screencopy_frame_v1_copy(frame->handle,frame->shm_buffer); frame->shm_busy=frame->submitted=1;
|
||||
} else frame_fail(frame);
|
||||
}
|
||||
static void dma_created(void*d,struct zwp_linux_buffer_params_v1*params,struct wl_buffer*buffer) {
|
||||
struct frame *frame=d;
|
||||
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
|
||||
wl_buffer_destroy(frame->buffer);
|
||||
munmap(frame->pixels,frame->length); frame->pixels=NULL; frame->length=0;
|
||||
frame->pixels=NULL;
|
||||
frame->buffer=buffer; frame->dmabuf_used=1;
|
||||
frame->width=frame->dma_width; frame->height=frame->dma_height;
|
||||
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
|
||||
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
|
||||
wl_buffer_add_listener(buffer,&buffer_listener,frame);
|
||||
frame->released=0;
|
||||
zwlr_screencopy_frame_v1_copy(frame->handle,buffer); frame->submitted=1;
|
||||
}
|
||||
static void dma_failed(void*d,struct zwp_linux_buffer_params_v1*params) {
|
||||
struct frame *frame=d;
|
||||
zwp_linux_buffer_params_v1_destroy(params); frame->params=NULL;
|
||||
dma_disable("import"); submit_shm(frame);
|
||||
dma_disable("import"); dma_clear(frame); submit_shm(frame);
|
||||
}
|
||||
static const struct zwp_linux_buffer_params_v1_listener dma_params_listener={dma_created,dma_failed};
|
||||
static int dma_start(struct frame *frame) {
|
||||
if(!dmabuf_requested || dmabuf_disabled || !gbm.device || frame->failed || !frame->buffer ||
|
||||
if(!dmabuf_requested || dmabuf_disabled || !gbm.device || frame->failed || !frame->shm_buffer ||
|
||||
!(dmabuf_linear_formats&dma_format_bit(frame->dma_format))) return 0;
|
||||
if(frame->buffer && frame->bo && frame->released && frame->bo_width==frame->dma_width &&
|
||||
frame->bo_height==frame->dma_height && frame->bo_format==frame->dma_format) {
|
||||
frame->pixels=NULL; frame->dmabuf_used=1; frame->released=0;
|
||||
frame->width=frame->dma_width; frame->height=frame->dma_height;
|
||||
frame->format=frame->dma_format==0x34325258 ? WL_SHM_FORMAT_XRGB8888:
|
||||
frame->dma_format==0x34325241 ? WL_SHM_FORMAT_ARGB8888:frame->dma_format;
|
||||
zwlr_screencopy_frame_v1_copy(frame->handle,frame->buffer); frame->submitted=1; return 1;
|
||||
}
|
||||
dma_clear(frame);
|
||||
/* Single-plane linear RGB only; unsupported imports are asynchronous failures,
|
||||
never create_immed protocol errors. No privilege or device probing changes. */
|
||||
frame->bo=gbm.bo_create(gbm.device,frame->dma_width,frame->dma_height,frame->dma_format,(1u<<2)|(1u<<4));
|
||||
@@ -264,7 +311,8 @@ static int dma_start(struct frame *frame) {
|
||||
uint32_t stride=gbm.bo_get_stride(frame->bo);
|
||||
if(gbm.bo_get_plane_count(frame->bo)!=1 || gbm.bo_get_modifier(frame->bo)!=0 ||
|
||||
stride<frame->dma_width*4 || stride>131072 || (uint64_t)stride*frame->dma_height>256*1024*1024 ||
|
||||
(frame->bo_fd=gbm.bo_get_fd(frame->bo))<0) {dma_disable("layout-or-export"); return 0;}
|
||||
(frame->bo_fd=gbm.bo_get_fd(frame->bo))<0) {dma_disable("layout-or-export"); dma_clear(frame); return 0;}
|
||||
frame->bo_format=frame->dma_format; frame->bo_width=frame->dma_width; frame->bo_height=frame->dma_height;
|
||||
frame->params=zwp_linux_dmabuf_v1_create_params(dmabuf);
|
||||
zwp_linux_buffer_params_v1_add_listener(frame->params,&dma_params_listener,frame);
|
||||
zwp_linux_buffer_params_v1_add(frame->params,frame->bo_fd,0,gbm.bo_get_offset(frame->bo,0),stride,0,0);
|
||||
@@ -285,14 +333,25 @@ static void reset(void) {
|
||||
if (key_state) xkb_state_unref(key_state);
|
||||
if (keymap) xkb_keymap_unref(keymap);
|
||||
if (xkb) xkb_context_unref(xkb);
|
||||
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
|
||||
for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); shm_clear(&frames[i]); }
|
||||
if(gbm.device) gbm.device_destroy(gbm.device);
|
||||
if(gbm.fd>=0) close(gbm.fd);
|
||||
if(gbm.library) dlclose(gbm.library);
|
||||
memset(&gbm,0,sizeof(gbm)); gbm.fd=-1;
|
||||
/* Disconnect drops every proxy; failure never leaves an initialized backend. */
|
||||
if(keyboard) zwp_virtual_keyboard_v1_destroy(keyboard);
|
||||
if(pointer) zwlr_virtual_pointer_v1_destroy(pointer);
|
||||
if(power) zwlr_output_power_v1_destroy(power);
|
||||
if(keyboard_manager) zwp_virtual_keyboard_manager_v1_destroy(keyboard_manager);
|
||||
if(pointer_manager) zwlr_virtual_pointer_manager_v1_destroy(pointer_manager);
|
||||
if(power_manager) zwlr_output_power_manager_v1_destroy(power_manager);
|
||||
if(copy_manager) zwlr_screencopy_manager_v1_destroy(copy_manager);
|
||||
if(dmabuf) zwp_linux_dmabuf_v1_destroy(dmabuf);
|
||||
for(unsigned i=0;i<output_count;i++) if(outputs[i].handle) wl_output_destroy(outputs[i].handle);
|
||||
if(seat) wl_seat_destroy(seat);
|
||||
if(shm) wl_shm_destroy(shm);
|
||||
if(registry) wl_registry_destroy(registry);
|
||||
if (display) wl_display_disconnect(display);
|
||||
display=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
|
||||
display=NULL; registry=NULL; shm=NULL; seat=NULL; copy_manager=NULL; pointer_manager=NULL; keyboard_manager=NULL;
|
||||
pointer=NULL; keyboard=NULL; xkb=NULL; keymap=NULL; key_state=NULL;
|
||||
power_manager=NULL; power=NULL; power_failed=0;
|
||||
dmabuf=NULL; dmabuf_requested=dmabuf_disabled=dmabuf_reported=0; dmabuf_linear_formats=0;
|
||||
@@ -305,7 +364,7 @@ int uu_wl_init(int32_t *w,int32_t *h) {
|
||||
int result=-1;
|
||||
const char *dma=getenv("UU_WAYLAND_DMABUF"); dmabuf_requested=dma && !strcmp(dma,"1");
|
||||
display=wl_display_connect(NULL); if (!display) goto end;
|
||||
struct wl_registry *registry=wl_display_get_registry(display);
|
||||
registry=wl_display_get_registry(display);
|
||||
wl_registry_add_listener(registry,®istry_listener,NULL);
|
||||
if(sync_display()!=0 || sync_display()!=0) goto end;
|
||||
dma_init();
|
||||
@@ -347,6 +406,8 @@ end:
|
||||
}
|
||||
int uu_wl_wake(void) {
|
||||
pthread_mutex_lock(&lock); int result=-1;
|
||||
/* Output power management is optional; missing it must not block screencopy. */
|
||||
if(initialized && !power) result=0;
|
||||
if(initialized && power && !power_failed) {
|
||||
zwlr_output_power_v1_set_mode(power,ZWLR_OUTPUT_POWER_V1_MODE_ON);
|
||||
if(sync_display()==0 && !power_failed) result=0;
|
||||
@@ -364,7 +425,8 @@ retry:;
|
||||
if(frame->failed && frame->dmabuf_used && !retried) {
|
||||
/* Mapping/copy failure retries once on SHM; connection errors do not. */
|
||||
dma_disable("copy"); retried=1;
|
||||
for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
|
||||
/* dma-buf stays disabled, so its cached buffers are never reused. */
|
||||
for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); }
|
||||
pending=0; goto retry;
|
||||
}
|
||||
if(frame->failed || !frame->pixels || frame->width!=width || frame->height!=height) goto end;
|
||||
@@ -389,7 +451,7 @@ retry:;
|
||||
}
|
||||
frame_clear(frame); pending=1-pending;
|
||||
end:
|
||||
if(result!=0) for(unsigned i=0;i<2;i++) frame_clear(&frames[i]);
|
||||
if(result!=0) for(unsigned i=0;i<2;i++) { frame_clear(&frames[i]); dma_clear(&frames[i]); }
|
||||
pthread_mutex_unlock(&lock); return result;
|
||||
}
|
||||
int uu_wl_move(int32_t x,int32_t y,int relative) {
|
||||
|
||||
+70
-45
@@ -16,6 +16,7 @@
|
||||
#include <X11/extensions/Xfixes.h>
|
||||
#include <stdint.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
|
||||
static Display *g_display = NULL;
|
||||
static Window g_root = 0;
|
||||
@@ -27,6 +28,8 @@ static int g_depth = 0;
|
||||
static XShmSegmentInfo g_shminfo;
|
||||
static XImage *g_shm_image = NULL;
|
||||
static int g_use_shm = 0;
|
||||
/* Guards the capture geometry and SHM image against a resize between connections. */
|
||||
static pthread_mutex_t g_capture_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static unsigned char g_injected_down[32]; // keycodes currently held by the remote viewer
|
||||
static int g_injected_count = 0;
|
||||
static int g_restore_repeat = 0;
|
||||
@@ -52,6 +55,34 @@ static void track_injected_key(int keycode, int is_press) {
|
||||
}
|
||||
}
|
||||
|
||||
static void shm_open_image(void) {
|
||||
g_use_shm = 0;
|
||||
if (!XShmQueryExtension(g_display)) return;
|
||||
g_shm_image = XShmCreateImage(g_display, DefaultVisual(g_display, g_screen),
|
||||
g_depth, ZPixmap, NULL, &g_shminfo, g_width, g_height);
|
||||
if (!g_shm_image) return;
|
||||
g_shminfo.shmid = shmget(IPC_PRIVATE, g_shm_image->bytes_per_line * g_shm_image->height, IPC_CREAT | 0777);
|
||||
if (g_shminfo.shmid == -1) return;
|
||||
g_shminfo.shmaddr = g_shm_image->data = (char *)shmat(g_shminfo.shmid, 0, 0);
|
||||
g_shminfo.readOnly = False;
|
||||
if (g_shminfo.shmaddr != (char *)-1 && XShmAttach(g_display, &g_shminfo)) {
|
||||
XSync(g_display, False);
|
||||
g_use_shm = 1;
|
||||
}
|
||||
shmctl(g_shminfo.shmid, IPC_RMID, 0); // 标记删除,进程退出时自动回收;失败时也不遗留段
|
||||
}
|
||||
static void shm_close_image(void) {
|
||||
if (g_use_shm) XShmDetach(g_display, &g_shminfo);
|
||||
if (g_shm_image) {
|
||||
g_shm_image->data = NULL; /* shm memory is released by shmdt, not XDestroyImage */
|
||||
XDestroyImage(g_shm_image);
|
||||
g_shm_image = NULL;
|
||||
}
|
||||
if (g_shminfo.shmaddr && g_shminfo.shmaddr != (char *)-1) shmdt(g_shminfo.shmaddr);
|
||||
memset(&g_shminfo, 0, sizeof(g_shminfo));
|
||||
g_use_shm = 0;
|
||||
}
|
||||
|
||||
int x11_init(const char *display_name) {
|
||||
if (g_display) return 0;
|
||||
|
||||
@@ -77,59 +108,60 @@ int x11_init(const char *display_name) {
|
||||
g_height = DisplayHeight(g_display, g_screen);
|
||||
g_depth = DefaultDepth(g_display, g_screen);
|
||||
|
||||
// 尝试初始化 XShm
|
||||
if (XShmQueryExtension(g_display)) {
|
||||
g_shm_image = XShmCreateImage(g_display, DefaultVisual(g_display, g_screen),
|
||||
g_depth, ZPixmap, NULL, &g_shminfo, g_width, g_height);
|
||||
if (g_shm_image) {
|
||||
g_shminfo.shmid = shmget(IPC_PRIVATE, g_shm_image->bytes_per_line * g_shm_image->height, IPC_CREAT | 0777);
|
||||
if (g_shminfo.shmid != -1) {
|
||||
g_shminfo.shmaddr = g_shm_image->data = (char *)shmat(g_shminfo.shmid, 0, 0);
|
||||
g_shminfo.readOnly = False;
|
||||
if (g_shminfo.shmaddr != (char *)-1) {
|
||||
if (XShmAttach(g_display, &g_shminfo)) {
|
||||
XSync(g_display, False);
|
||||
shmctl(g_shminfo.shmid, IPC_RMID, 0); // 标记删除,进程退出时自动回收
|
||||
g_use_shm = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shm_open_image();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int x11_get_dimensions(int *out_w, int *out_h) {
|
||||
if (!g_display) return -1;
|
||||
pthread_mutex_lock(&g_capture_lock);
|
||||
*out_w = g_width;
|
||||
*out_h = g_height;
|
||||
pthread_mutex_unlock(&g_capture_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The startup size goes stale when RandR/xrdp resizes the root window. Query the
|
||||
* server and rebuild the SHM image on change. Returns 1 if changed, 0 if not, <0 on error. */
|
||||
int x11_refresh_geometry(void) {
|
||||
if (!g_display) return -1;
|
||||
Window root; int x, y; unsigned int w, h, border, depth;
|
||||
if (!XGetGeometry(g_display, g_root, &root, &x, &y, &w, &h, &border, &depth) || !w || !h) return -2;
|
||||
pthread_mutex_lock(&g_capture_lock);
|
||||
int changed = (int)w != g_width || (int)h != g_height;
|
||||
if (changed) {
|
||||
shm_close_image();
|
||||
g_width = (int)w; g_height = (int)h;
|
||||
shm_open_image();
|
||||
}
|
||||
pthread_mutex_unlock(&g_capture_lock);
|
||||
return changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取一帧画面(RGBA/BGRA 格式)
|
||||
* @param out_buffer 输出缓冲区,大小必须至少为 width * height * 4
|
||||
*/
|
||||
/* *out_w/*out_h carry the caller's buffer geometry in; a mismatch (desktop resized
|
||||
* since that buffer was sized) returns -4 instead of overflowing it. */
|
||||
int x11_capture_frame(unsigned char *out_buffer, int *out_w, int *out_h) {
|
||||
if (!g_display) return -1;
|
||||
pthread_mutex_lock(&g_capture_lock);
|
||||
int result = 0;
|
||||
if (*out_w != g_width || *out_h != g_height) result = -4;
|
||||
else if (g_use_shm && g_shm_image) {
|
||||
if (!XShmGetImage(g_display, g_root, g_shm_image, 0, 0, AllPlanes)) result = -2;
|
||||
else memcpy(out_buffer, g_shm_image->data, (size_t)g_width * g_height * 4);
|
||||
} else {
|
||||
// Fallback: XGetImage
|
||||
XImage *img = XGetImage(g_display, g_root, 0, 0, g_width, g_height, AllPlanes, ZPixmap);
|
||||
if (!img) result = -3;
|
||||
else { memcpy(out_buffer, img->data, (size_t)g_width * g_height * 4); XDestroyImage(img); }
|
||||
}
|
||||
*out_w = g_width;
|
||||
*out_h = g_height;
|
||||
|
||||
if (g_use_shm && g_shm_image) {
|
||||
if (!XShmGetImage(g_display, g_root, g_shm_image, 0, 0, AllPlanes)) {
|
||||
return -2;
|
||||
}
|
||||
memcpy(out_buffer, g_shm_image->data, g_width * g_height * 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Fallback: XGetImage
|
||||
XImage *img = XGetImage(g_display, g_root, 0, 0, g_width, g_height, AllPlanes, ZPixmap);
|
||||
if (!img) return -3;
|
||||
memcpy(out_buffer, img->data, g_width * g_height * 4);
|
||||
XDestroyImage(img);
|
||||
return 0;
|
||||
pthread_mutex_unlock(&g_capture_lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
// ============ 原生输入注入 ============
|
||||
@@ -261,16 +293,9 @@ int x11_terminal_shortcut_state(void) {
|
||||
|
||||
void x11_close() {
|
||||
if (!g_display) return;
|
||||
if (g_use_shm) {
|
||||
XShmDetach(g_display, &g_shminfo);
|
||||
if (g_shm_image) {
|
||||
XDestroyImage(g_shm_image);
|
||||
g_shm_image = NULL;
|
||||
}
|
||||
if (g_shminfo.shmaddr && g_shminfo.shmaddr != (char *)-1) {
|
||||
shmdt(g_shminfo.shmaddr);
|
||||
}
|
||||
}
|
||||
pthread_mutex_lock(&g_capture_lock);
|
||||
shm_close_image();
|
||||
pthread_mutex_unlock(&g_capture_lock);
|
||||
XCloseDisplay(g_display);
|
||||
g_display = NULL;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
托盘图标。/ Tray icon.
|
||||
|
||||
### 新增 / Added
|
||||
- **托盘图标**:后台运行时,顶栏/任务栏会显示托盘图标,菜单里有「打开主窗口」、「允许他人远程协助」(与主窗口开关同步)和「退出」。
|
||||
- GNOME 通过 AppIndicator 显示(Ubuntu 自带扩展;运行时加载 libayatana-appindicator,无需额外编译依赖);Xfce / Cinnamon / KDE 等使用系统托盘。
|
||||
- 「退出」会停止 `uuremote-native.service`(不会被自动重启);之后从应用菜单「UU 远程 Linux 主机」打开,会重新启动服务并显示主窗口。
|
||||
✅ Ubuntu 24.04 GNOME(X11)实机验收:图标、菜单、开关同步、退出、从应用菜单重新启动。⚠️ Xfce 等系统托盘路径尚未实机验收。
|
||||
Tray icon with open / allow / quit; Quit stops the user service and the launcher restarts it. Verified on Ubuntu GNOME.
|
||||
|
||||
### 改进 / Improved
|
||||
- `uuremote-server show`:主机没在运行时,优先启动已安装的 `uuremote-native.service`,而不是另起一个不受管理的进程。
|
||||
- 修复启动时的一个时序问题:先注册窗口唤起信号再写入 PID 文件,避免过早的 `show` 意外结束主机进程。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.13-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,13 @@
|
||||
修复手机切回终端时黑屏。/ Fixes a black terminal after returning to it on mobile.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **终端历史输出较多时,重连后黑屏**(#14,感谢 @cnetst):手机客户端切到后台再回到同一个终端时,如果之前积累的输出较多,
|
||||
会整屏黑屏,连新建终端也没有输出,只能完全退出 App 重连。原因是重连时把最多 256 KB 的历史输出作为一条消息发送,
|
||||
超过了 WebRTC 数据通道单条消息 64 KB 的上限,发送失败后整个终端连接卡住。现在历史输出按 16 KB 分段发送,顺序不变。
|
||||
✅ 已用官方手机客户端在 3090 实机验收。
|
||||
Terminal scrollback is replayed in 16 KiB chunks instead of one message over the 64 KiB SCTP limit. Verified on mobile.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.14-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,20 @@
|
||||
支持真彩(4:4:4)。/ True color (4:4:4) support.
|
||||
|
||||
### 新功能 / Added
|
||||
- **真彩(4:4:4)**:在客户端画面菜单打开「真彩」后,主机改用 H.264 High 4:4:4 编码,彩色细字(红字、链接、
|
||||
代码高亮)边缘不再发虚;关闭后切回 4:2:0。适用于默认的 FFmpeg 管线(NVENC 或 libx264);VAAPI 和
|
||||
`--features inproc-av` 管线仍为 4:2:0,并如实告知客户端。
|
||||
✅ 已用官方 Mac 客户端在 RTX 3090(NVENC)上实机验证:开关来回切换正常、画面正常解码。
|
||||
When 真彩 is enabled the host encodes H.264 High 4:4:4 (default FFmpeg pipeline with NVENC or libx264); VAAPI and
|
||||
inproc-av stay 4:2:0 and report it. Verified with the official Mac viewer on NVENC (toggling, decoding).
|
||||
- 日志记录客户端声明的解码能力(编码类型、色度、最大分辨率、帧率,仅数字),便于排查画质问题。
|
||||
The log now shows the viewer's declared decoder capabilities (numbers only).
|
||||
|
||||
### 说明 / Notes
|
||||
- 4:4:4 码率会略高于 4:2:0;在缩放到较低分辨率时差别不明显,建议配合「跟随远端」分辨率使用。
|
||||
4:4:4 costs some bitrate; the difference is hardly visible when the stream is scaled down.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.15-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,22 @@
|
||||
支持 H.265(HEVC)。/ H.265 (HEVC) support.
|
||||
|
||||
### 新功能 / Added
|
||||
- **H.265**:控制端和主机都支持时自动使用 H.265,同等画质下码率更低。与官方主机一致,由主机按双方能力选择编码
|
||||
(官方 Mac 客户端没有手动切换编码的菜单)。连接时主机会告知客户端自己能编哪些格式(CodecNegotiation)。
|
||||
支持 `hevc_nvenc`、`libx265` 和显式 VAAPI 的 `hevc_vaapi`;硬件失败先回落软件 HEVC,再回落 H.264。
|
||||
需要时用 `UU_VIDEO_CODEC=h264` 强制使用 H.264。
|
||||
- 真彩(4:4:4)同样适用于 H.265(hevc_nvenc `rext` / libx265 `main444-8`)。
|
||||
✅ 已用官方 Mac 客户端在 RTX 3090(hevc_nvenc,含 4:4:4)实机验收:画面正常、重连正常、无丢帧。
|
||||
⚠️ libx265、hevc_vaapi、Windows/手机客户端尚未实机验证;如有问题请设置 `UU_VIDEO_CODEC=h264` 并提 issue。
|
||||
The host now uses H.265 when both sides support it (the host picks the codec, like the official host), announces
|
||||
its encoders with CodecNegotiation, and falls back to software HEVC, then H.264. `UU_VIDEO_CODEC=h264` forces H.264.
|
||||
Verified with the official Mac viewer on NVENC including 4:4:4; other encoders and viewers are unverified.
|
||||
|
||||
### 修复 / Fixed
|
||||
- 日志新增客户端 offer 中的编码列表与最终协商结果(仅编码名),便于排查画面问题。
|
||||
The log shows offered and negotiated video codecs (names only).
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.16-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,24 @@
|
||||
修复终端接回错位、低码率 CPU 编码灰屏、niri 无法建流。/ Terminal reattach, low-bitrate CPU encoding and niri fixes.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **终端接回后全屏程序画面错位**(#17):接回已有会话时,主机先把终端行数临时减一、稍后恢复,触发 tmux、vim、
|
||||
Claude Code 等全屏程序整屏重画。
|
||||
⚠️ 已通过单元测试;尚未用 Android 客户端实机验证,如仍错位请在 #17 反馈。
|
||||
Reattaching a terminal session now nudges the PTY size so full-screen programs repaint. Unit-tested; not yet
|
||||
verified with the Android viewer.
|
||||
- **低码率档周期性灰屏**(#15,感谢 @mengdehong):外部 FFmpeg 的 CPU 编码(libx264,以及同样处理的 libx265)在
|
||||
码率档(原画 / 超清 / 高清 / 自定义)改用平均码率(ABR),避免 4K 低码率下关键帧耗尽缓冲而发灰;「自动」档仍用 CRF。
|
||||
libx264 修复由贡献者在 Android 客户端实测;libx265 只验证了参数可用。
|
||||
CPU encoding uses ABR for bitrate presets (CRF for Auto), fixing gray keyframes at 4K and low bitrates.
|
||||
- **niri 等合成器无法建流**(#15):原生 Wayland 后端在合成器没有 `wlr-output-power-management` 时跳过唤醒屏幕,
|
||||
不再中断采集。Capture continues when the compositor lacks output power management.
|
||||
|
||||
### 文档 / Docs
|
||||
- Wayland 会话的 systemd 单元写法、uinput 模块开机加载(#16,感谢 @meotao 的实机测量与建议);未编入 `wayland`
|
||||
特性时的报错改为明确提示重新编译。
|
||||
Wayland systemd unit, uinput autoload, and a clearer error when the binary lacks the `wayland` feature.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.17-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,24 @@
|
||||
修复分辨率变化后无法出画面;Wayland 采集提速;终端接回修复。/ Resolution change fix, faster Wayland capture, terminal fixes.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **服务运行中桌面分辨率变化后,新连接一直停在「正在传输画面」**(#18,感谢 @liangjiahui01):每次建立桌面连接前重新读取
|
||||
X11 桌面尺寸,变化时重建截屏;截屏尺寸不符时报明确错误,不再误报「HEVC unavailable」。
|
||||
已在隔离 Xvfb 上用 `xrandr` 改尺寸验证;xrdp 场景未实测。连接期间改分辨率暂不支持(需重连)。
|
||||
X11 now re-reads the desktop size before each connection, so a RandR/xrdp resize no longer stalls new connections.
|
||||
- **终端接回**(#17 后续):修复延迟恢复尺寸可能覆盖新尺寸的竞态,以及接回时同一段输出既在回放里又被实时重发的问题。
|
||||
Fixed a size-restore race and duplicated output on terminal reattach.
|
||||
|
||||
### 改进 / Improved
|
||||
- **原生 Wayland 采集**(#16,感谢 @meotao 的实机测量):采集进程直接写入 FFmpeg,不再经主进程中转 4K 帧;采集与写管道
|
||||
双线程交替,管道缓冲扩大;SHM 截屏缓冲复用,不再每帧分配。
|
||||
⚠️ 只做了编译和单元测试,没有在真机上测到提速效果;请求高帧率时的实际帧率欢迎在 #16 反馈。
|
||||
Native Wayland: capture pipes straight into FFmpeg, overlaps capture with writes and reuses SHM buffers. Not yet
|
||||
measured on real hardware.
|
||||
- 视频统计日志新增本窗口的 `window_s`、`window_sent`、`window_skipped`(原 `sent`/`skipped` 仍为累计值)。
|
||||
The latest-frame stats line adds per-window counts.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.18-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
Wayland 后端需自行用 `--features wayland` 编译。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,14 @@
|
||||
信令断线检测:主机休眠恢复或网络悄悄断开后自动重新上线。/ Detect dead signaling connections and reconnect.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **主机显示在线但控制端看不到、只能手动重启服务**(#19,感谢 @Keinns):之前只有收到关闭帧或读错误才判定信令断开;
|
||||
系统休眠恢复、NAT 超时等造成的半开连接会让主机一直以为自己在线。现在按 Engine.IO 协议,超过服务器下发的
|
||||
`pingInterval + pingTimeout`(目前约 33 秒)没收到心跳就判定断线,走已有的自动重连。日志:`Signaling heartbeat lost`。
|
||||
已用模拟的「不发心跳」信令服务器单元测试验证;真实休眠恢复场景未实测。
|
||||
Signaling now treats a connection without pings for pingInterval + pingTimeout as dead and reconnects.
|
||||
Unit-tested against a silent mock server; real suspend/resume not yet measured.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.19-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,16 @@
|
||||
登录诊断与系统证书支持。/ Login diagnostics and system certificate support.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **企业代理 / 自定义 CA 环境下登录失败**(#21,感谢 @liuji10000qian-svg):除内置根证书外,现在也信任系统证书库,
|
||||
并遵循 `SSL_CERT_FILE` / `SSL_CERT_DIR`。会做 TLS 拦截的代理(使用自签 CA)此前会导致设备注册请求失败。
|
||||
Trust the system certificate store and honor `SSL_CERT_FILE` / `SSL_CERT_DIR` in addition to the bundled roots.
|
||||
- **网络请求失败时只显示「error sending request」**:错误信息现在包含底层原因链(证书、代理、DNS、超时等),
|
||||
并去掉 URL 中的用户名密码(如代理凭据)和查询参数。
|
||||
Transport errors now include their cause chain, with URL credentials and query strings removed.
|
||||
|
||||
⚠️ 自定义 CA / 代理环境未实测,请在 #21 反馈新版本的错误信息。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.20-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,17 @@
|
||||
视频管线减少重复分配和过量编码。/ Fewer allocations and less wasted encoding in the video pipeline.
|
||||
|
||||
### 改进 / Improved(#20,感谢 @mengdehong)
|
||||
- 采集到发送之间最多 4 帧在途:网络发不出去时采集端自动放慢,不再白白编码无法送出的帧。
|
||||
贡献者实验(发送受阻场景):编码 CPU 耗时降低约 88%。
|
||||
- 原生 Wayland 可选的 dma-buf 路径、Portal / KMS 路径复用采集缓冲;Portal 同一帧不重复复制。
|
||||
- X11 进程内编码(`inproc-av`,需自行编译)缓存静态画面的采集与转色结果:静态画面每帧 CPU 降低约 40%。
|
||||
- 原生 Wayland 关闭时完整释放协议对象。
|
||||
|
||||
⚠️ 以上性能数据来自贡献者在隔离 headless Sway / Xvfb 中的合成画面实验,未在维护者的真机上复测。
|
||||
预编译的 X11 包默认走外部 FFmpeg 管线,受影响的主要是 Wayland / Portal / KMS / `inproc-av` 自编译用户。
|
||||
Performance figures come from the contributor's headless Sway/Xvfb experiments; not re-measured on maintainer hardware.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.21-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,15 @@
|
||||
v0.1.21 视频管线改动的两处修正。/ Two follow-up fixes to the v0.1.21 video pipeline changes.
|
||||
|
||||
### 修复 / Fixed
|
||||
- 原生 Wayland 的 dma-buf 映射失败、回退到 SHM 时,已缓存的 dma-buf 缓冲没有释放(最多两个槽位,到会话结束才回收)。
|
||||
Release cached dma-buf buffers when capture falls back to SHM.
|
||||
- 原生 Wayland 在发送受阻(背压)解除后的第一帧可能是受阻前预取的旧画面;现在等待超过两个帧间隔时会重新采集。
|
||||
After a backpressure stall the first native Wayland frame is captured fresh instead of reusing the stale prefetch.
|
||||
|
||||
两处均由代码审查发现,只做了编译和单元测试,未在真机复现。
|
||||
Both found in review; compile- and unit-tested only.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.22-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,14 @@
|
||||
测量工具:`desktop-video-probe` 可选码率档。/ Probe bitrate presets.
|
||||
|
||||
### 新增 / Added
|
||||
- `desktop-video-probe` 支持 `UU_PROBE_QUALITY=1..5`,对应控制端画质档(1 极速 1M、2 流畅 2M、3 高清 8M、4 蓝光 20M、5 自动)。
|
||||
码率档走平均码率(ABR)路径,便于不用控制端复测 #15 的灰屏修复(#9 的建议)。
|
||||
`desktop-video-probe` accepts `UU_PROBE_QUALITY=1..5` (viewer bitrate presets; 5 = Auto/CRF).
|
||||
|
||||
### 其他 / Other
|
||||
- 修复一个在多网卡机器上偶发超时的编码协商单元测试。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.23-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,17 @@
|
||||
支持 Ubuntu 22.04 自带的 FFmpeg 4.4。/ Works with Ubuntu 22.04's FFmpeg 4.4.
|
||||
|
||||
### 改进 / Improved
|
||||
- **自动适配 FFmpeg 版本**(#22,感谢 @Guancheng-Li):启动编码前检测所用 FFmpeg 支持的参数。FFmpeg 5 及以上继续使用
|
||||
`-fps_mode vfr -enc_time_base 1/<fps>`;FFmpeg 4.x(如 Ubuntu 22.04)改用 `-vsync 0`。检测失败时保持新版参数。
|
||||
FFmpeg 4.x 下采集卡顿后的补帧会全部编码(新版会丢弃),属于已知降级。贡献者已在 FFmpeg 4.4.2 上验证。
|
||||
The host probes the FFmpeg it uses and falls back to `-vsync 0` only on FFmpeg 4.x; probe failures keep the modern arguments.
|
||||
|
||||
### 其他 / Other
|
||||
- 新增几条只记结构的诊断日志(收到的 SOAC offer 结构、SDP 段落属性名、ice_id 是否一致),用于排查与官方控制端的互通,
|
||||
不记录 SDP 内容或任何 ID。
|
||||
Structure-only diagnostics for incoming offers (no SDP contents or ids).
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.24-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
+3
-2
@@ -1,4 +1,4 @@
|
||||
//! Opt-in guest assistance. Protocol sources: docs/validation/remote-assist-evidence.md.
|
||||
//! Opt-in guest assistance. Protocol sources: AGENTS.md "Remote assistance".
|
||||
//! Secret values never implement Display/Debug or enter diagnostics.
|
||||
use crate::auth::{Credentials, RoomData, UUAuthClient};
|
||||
use serde_json::{json, Value};
|
||||
@@ -68,7 +68,7 @@ fn valid_custom(code: &str) -> bool {
|
||||
&& code.bytes().any(|b| b.is_ascii_alphabetic()) && code.bytes().any(|b| b.is_ascii_digit())
|
||||
}
|
||||
|
||||
pub enum Command { Allow, Deny, Reset, Stop, Verify(Verify), Refresh(Refresh), Custom(String), Logout }
|
||||
pub enum Command { Allow, Deny, Reset, Stop, Verify(Verify), Refresh(Refresh), Custom(String), Logout, Quit }
|
||||
|
||||
#[cfg(uu_gtk)]
|
||||
extern "C" {
|
||||
@@ -156,6 +156,7 @@ impl LocalUi {
|
||||
6 => if let Some(r) = Refresh::from_index(index) { return Command::Refresh(r) },
|
||||
7 => return Command::Custom(text),
|
||||
8 => return Command::Logout,
|
||||
9 => return Command::Quit,
|
||||
_ => tokio::time::sleep(std::time::Duration::from_millis(150)).await,
|
||||
}
|
||||
},
|
||||
|
||||
+38
-2
@@ -91,6 +91,36 @@ pub struct UUAuthClient {
|
||||
guest_id: Option<String>,
|
||||
}
|
||||
|
||||
/// Transport failures name only the outer request; the cause (certificate, proxy, DNS,
|
||||
/// timeout) lives in the source chain. URL user-info (proxy credentials) and query
|
||||
/// strings are removed before the chain is shown.
|
||||
pub fn transport_error(error: &(dyn std::error::Error + 'static)) -> String {
|
||||
let mut parts = vec![error.to_string()];
|
||||
let mut source = error.source();
|
||||
while let Some(cause) = source {
|
||||
let text = cause.to_string();
|
||||
if parts.last() != Some(&text) { parts.push(text); }
|
||||
source = cause.source();
|
||||
if parts.len() >= 8 { break; }
|
||||
}
|
||||
redact_urls(&parts.join(": "))
|
||||
}
|
||||
fn redact_urls(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
let mut rest = text;
|
||||
while let Some(i) = rest.find("://") {
|
||||
let (head, tail) = rest.split_at(i + 3);
|
||||
out.push_str(head);
|
||||
let end = tail.find(|c: char| c.is_whitespace() || c == ')' || c == '"' || c == '\'').unwrap_or(tail.len());
|
||||
let url = &tail[..end];
|
||||
let url = url.rsplit_once('@').map_or(url, |(_, host)| host);
|
||||
out.push_str(url.split(['?', '#']).next().unwrap_or(""));
|
||||
rest = &tail[end..];
|
||||
}
|
||||
out.push_str(rest);
|
||||
out
|
||||
}
|
||||
|
||||
impl UUAuthClient {
|
||||
pub fn new(creds: Credentials) -> Self {
|
||||
let http = reqwest::Client::builder()
|
||||
@@ -200,7 +230,7 @@ impl UUAuthClient {
|
||||
let body_str = serde_json::to_string(body).map_err(|e| e.to_string())?;
|
||||
let resp = self.http.post(format!("{}{}", BASE_URL, path))
|
||||
.headers(self.build_headers("POST", path, &body_str)).body(body_str)
|
||||
.send().await.map_err(|e| format!("HTTP post {path} failed: {e}"))?;
|
||||
.send().await.map_err(|e| format!("HTTP post {path} failed: {}", transport_error(&e)))?;
|
||||
let status = resp.status().as_u16();
|
||||
let value = resp.json().await.unwrap_or(serde_json::Value::Null);
|
||||
Ok((status, value))
|
||||
@@ -219,7 +249,7 @@ impl UUAuthClient {
|
||||
}
|
||||
async fn assist_send(&self, request: reqwest::Request) -> Result<(u16, serde_json::Value), String> {
|
||||
let response = self.http.execute(request)
|
||||
.await.map_err(|_| "assist request transport failed")?;
|
||||
.await.map_err(|e| format!("assist request transport failed: {}", transport_error(&e)))?;
|
||||
let status = response.status().as_u16();
|
||||
let bytes = response.bytes().await.map_err(|_| "assist response read failed")?;
|
||||
if bytes.len() > 65536 { return Err("assist response too large".into()); }
|
||||
@@ -310,4 +340,10 @@ mod guest_tests {
|
||||
let mut without_guest = params; without_guest.remove("x-param-guest-id");
|
||||
assert_ne!(headers["x-param-sign"], client.sign_request("POST", "/api/v1/guest/room/create", &without_guest, ""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transport_error_redacts_proxy_credentials_and_queries() {
|
||||
let text = super::redact_urls("proxy http://user:[email protected]:3128 failed for (https://api.example/p?token=abc)");
|
||||
assert_eq!(text, "proxy http://proxy.example:3128 failed for (https://api.example/p)");
|
||||
}
|
||||
}
|
||||
+18
-6
@@ -27,17 +27,20 @@ pub fn h264_device_capability(
|
||||
fps: i32,
|
||||
codec_impl: i32,
|
||||
) -> Value {
|
||||
device_capability(client_id, ice_id, display_id, fps, codec_impl, false)
|
||||
}
|
||||
|
||||
pub fn device_capability(client_id: &str, ice_id: &str, display_id: i32, fps: i32, codec_impl: i32, hevc: bool) -> Value {
|
||||
let mut codecs = vec![json!({"video_codec": VIDEO_CODEC_H264, "chroma_sampling": 1,
|
||||
"codec_impl": codec_impl, "bit_depth": 8})];
|
||||
// Official VideoCodecCapability::SerializeJson uses the same fields for CodecType H265=2.
|
||||
if hevc { codecs.push(json!({"video_codec": 2, "chroma_sampling": 1, "codec_impl": codec_impl, "bit_depth": 8})); }
|
||||
json!({
|
||||
"client_id": client_id,
|
||||
"type": "device_capability",
|
||||
"data": {
|
||||
"display_info": [{"id": display_id, "fps": fps, "type": 0, "hdr": 0}],
|
||||
"video_codec_capability": [{
|
||||
"video_codec": VIDEO_CODEC_H264,
|
||||
"chroma_sampling": 1,
|
||||
"codec_impl": codec_impl,
|
||||
"bit_depth": 8
|
||||
}],
|
||||
"video_codec_capability": codecs,
|
||||
"ice_id": ice_id
|
||||
}
|
||||
})
|
||||
@@ -47,6 +50,15 @@ pub fn h264_device_capability(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn hevc_capability_uses_official_codec_enum_and_420_only() {
|
||||
let payload = device_capability("client-example", "ice-example", 0, 60, 5, true);
|
||||
assert_eq!(payload["data"]["video_codec_capability"], json!([
|
||||
{"video_codec":1,"chroma_sampling":1,"codec_impl":5,"bit_depth":8},
|
||||
{"video_codec":2,"chroma_sampling":1,"codec_impl":5,"bit_depth":8}]));
|
||||
assert_eq!(device_capability("c", "i", 0, 60, 5, false)["data"]["video_codec_capability"].as_array().unwrap().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ice_finished_keeps_string_identifiers_and_exact_fields() {
|
||||
let payload = ice_finished("ice-example", "control-example");
|
||||
|
||||
+19
-5
@@ -6,6 +6,7 @@ use std::sync::Mutex;
|
||||
extern "C" {
|
||||
fn x11_init(display_name: *const libc::c_char) -> libc::c_int;
|
||||
fn x11_get_dimensions(out_w: *mut libc::c_int, out_h: *mut libc::c_int) -> libc::c_int;
|
||||
fn x11_refresh_geometry() -> libc::c_int;
|
||||
fn x11_capture_frame(
|
||||
out_buffer: *mut libc::c_uchar,
|
||||
out_w: *mut libc::c_int,
|
||||
@@ -37,10 +38,22 @@ unsafe fn x11_capture_frame(
|
||||
}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
unsafe fn x11_close() {}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
unsafe fn x11_refresh_geometry() -> libc::c_int { 0 }
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
unsafe fn x11_display_name() -> *const libc::c_char { b":0\0".as_ptr().cast() }
|
||||
|
||||
/// The X11 root can be resized while the host runs (RandR, xrdp reconnects); the
|
||||
/// geometry read at startup then makes x11grab reject every new connection.
|
||||
pub fn x11_resized() -> bool {
|
||||
match unsafe { x11_refresh_geometry() } {
|
||||
1 => true,
|
||||
0 => false,
|
||||
code => { log::warn!("X11 geometry query failed with code {code}"); false }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ScreenCapture {
|
||||
pub backend: crate::desktop_backend::DesktopBackend,
|
||||
pub init_error: Option<String>,
|
||||
@@ -59,18 +72,18 @@ impl ScreenCapture {
|
||||
let kms=crate::kms::Capture::open()?;
|
||||
let (width,height)=(kms.width,kms.height);
|
||||
return Ok(Self { backend,init_error:None,portal:None,display:"kms:linear".into(),width,height,
|
||||
buffer:Mutex::new(vec![0;width as usize*height as usize*4]),kms:Some(kms) });
|
||||
buffer:Mutex::new(Vec::new()),kms:Some(kms) });
|
||||
}
|
||||
if backend == crate::desktop_backend::DesktopBackend::Portal {
|
||||
let session=std::sync::Arc::new(crate::portal::PortalSession::open()?);
|
||||
let (width,height)=(session.width,session.height);
|
||||
return Ok(Self { backend,init_error:None,portal:Some(session),kms:None,display:"wayland:portal".into(),width,height,
|
||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||
buffer:Mutex::new(Vec::new()) });
|
||||
}
|
||||
if backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
let (width,height)=crate::wayland::init()?;
|
||||
return Ok(Self { backend, init_error:None, portal:None, kms:None, display:"wayland:native".into(), width,height,
|
||||
buffer:Mutex::new(vec![0;(width as usize)*(height as usize)*4]) });
|
||||
buffer:Mutex::new(Vec::new()) });
|
||||
}
|
||||
let ret = unsafe { x11_init(std::ptr::null()) };
|
||||
if ret != 0 {
|
||||
@@ -85,8 +98,7 @@ impl ScreenCapture {
|
||||
return Err(format!("Invalid screen dimensions: {}x{}", w, h));
|
||||
}
|
||||
|
||||
let buf_size = (w * h * 4) as usize;
|
||||
let buffer = Mutex::new(vec![0u8; buf_size]);
|
||||
let buffer = Mutex::new(Vec::new());
|
||||
|
||||
log::info!("ScreenCapture initialized: {}x{}", w, h);
|
||||
let display_ptr = unsafe { x11_display_name() };
|
||||
@@ -115,6 +127,7 @@ impl ScreenCapture {
|
||||
pub fn capture_frame(&self) -> Result<Vec<u8>, String> {
|
||||
if let Some(error)=&self.init_error { return Err(error.clone()); }
|
||||
let mut buf = self.buffer.lock().unwrap();
|
||||
buf.resize(self.width as usize * self.height as usize * 4, 0);
|
||||
if let Some(kms)=&self.kms { kms.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||
if let Some(session)=&self.portal { session.capture(&mut buf)?; return Ok(buf.clone()); }
|
||||
if self.backend == crate::desktop_backend::DesktopBackend::Wayland {
|
||||
@@ -126,6 +139,7 @@ impl ScreenCapture {
|
||||
|
||||
let ret = unsafe { x11_capture_frame(buf.as_mut_ptr(), &mut w, &mut h) };
|
||||
if ret != 0 {
|
||||
if ret == -4 { return Err(format!("desktop resized to {w}x{h}; reconnect to capture the new size")); }
|
||||
return Err(format!("x11_capture_frame failed with code {}", ret));
|
||||
}
|
||||
|
||||
|
||||
+111
-5
@@ -19,24 +19,40 @@ pub const QUALITY_AUTO: u64 = 5;
|
||||
pub const QUALITY_CUSTOM: u64 = 6;
|
||||
|
||||
/// FeatureFlag.ff_capture_setting we declare: fps, preset quality and custom bitrate.
|
||||
/// (Chroma 4:4:4, DPI scaling and HDR are higher versions and are not implemented.)
|
||||
/// Versions are cumulative (official FF_CaptureSetting enum: 3 CHROMA_SAMPLING, 4 CUSTOM_BITRATE,
|
||||
/// 5 DPI_SCALE, 6 HDR), so 4 also tells the viewer we accept a chroma format (真彩).
|
||||
pub const FEATURE_VERSION: u64 = 4;
|
||||
|
||||
/// Official GvPb_ChromaFormat values (CaptureSettingRequest field 7, CaptureSettingSync field 3).
|
||||
pub const CHROMA_420: u64 = 1;
|
||||
pub const CHROMA_444: u64 = 3;
|
||||
|
||||
const MIN_FPS: u32 = 5;
|
||||
const MAX_FPS: u32 = 144;
|
||||
|
||||
/// Official GvPb_CodecType, CaptureSettingRequest.codec_type (field 13).
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum VideoCodec { #[default] H264, H265 }
|
||||
impl VideoCodec {
|
||||
pub fn wire(self) -> u64 { match self { Self::H264 => 1, Self::H265 => 2 } }
|
||||
pub fn mime(self) -> &'static str { match self { Self::H264 => "video/H264", Self::H265 => "video/H265" } }
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct VideoSettings {
|
||||
pub fps: u32,
|
||||
pub codec: VideoCodec,
|
||||
pub quality: u64,
|
||||
/// Viewer max_custom_bitrate (only used for QUALITY_CUSTOM): Mbit/s, or kbit/s above 1000.
|
||||
pub custom_mbps: u32,
|
||||
/// Scaled stream size (其他分辨率); None streams the native desktop size.
|
||||
pub output: Option<(u32, u32)>,
|
||||
/// CHROMA_420 or CHROMA_444 (真彩); 4:4:4 only when the encoder pipeline supports it.
|
||||
pub chroma: u64,
|
||||
}
|
||||
|
||||
impl Default for VideoSettings {
|
||||
fn default() -> Self { Self { fps: 60, quality: QUALITY_AUTO, custom_mbps: 0, output: None } }
|
||||
fn default() -> Self { Self { codec: VideoCodec::H264, fps: 60, quality: QUALITY_AUTO, custom_mbps: 0, output: None, chroma: CHROMA_420 } }
|
||||
}
|
||||
|
||||
/// Selectable smaller stream sizes with the desktop's aspect ratio (even dimensions).
|
||||
@@ -95,6 +111,21 @@ fn fps_from_option(value: u64) -> Option<u64> {
|
||||
/// Applies a capture-setting RPC to `settings` and returns the RpcResponse payload (Message 22),
|
||||
/// or None when the RPC is not a capture/fps/quality setting.
|
||||
pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32)) -> Option<Vec<u8>> {
|
||||
handle_rpc_with_codecs(rpc, settings, native, true, false)
|
||||
}
|
||||
|
||||
/// Initial codec once the m-line is negotiated: H.265 when both sides support it (the Mac viewer
|
||||
/// lists H265 first in its offer), unless UU_VIDEO_CODEC=h264.
|
||||
pub fn preferred_codec(h264: bool, h265: bool) -> VideoCodec {
|
||||
let forced_h264 = std::env::var("UU_VIDEO_CODEC").is_ok_and(|v| v.eq_ignore_ascii_case("h264"));
|
||||
if h265 && !(forced_h264 && h264) { VideoCodec::H265 } else { VideoCodec::H264 }
|
||||
}
|
||||
|
||||
pub fn handle_rpc_with_codecs(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool) -> Option<Vec<u8>> {
|
||||
apply_rpc(rpc, settings, native, h264, h265, crate::video::full_chroma_available())
|
||||
}
|
||||
|
||||
fn apply_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool, full_chroma: bool) -> Option<Vec<u8>> {
|
||||
let header = message(rpc, 1).unwrap_or(&[]);
|
||||
let mut next = *settings.borrow();
|
||||
if let Some(request) = message(rpc, 2) {
|
||||
@@ -102,10 +133,21 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
|
||||
// Numbers only: fps, quality, bitrate, resolution and flags carry no user content.
|
||||
log::info!("Capture setting request: {:?}", fields.iter().filter_map(|f| match f.value {
|
||||
WireValue::Varint(v) => Some((f.number, v)), _ => None }).collect::<Vec<_>>());
|
||||
// codec_type 13 (1 H264, 2 H265): like the official host (negotiateCodec), the host picks the
|
||||
// codec from both capability lists; the Mac viewer sends its current codec (1 while receiving
|
||||
// H.264), so only an explicit H.265 request switches, never one absent from the m-line.
|
||||
if number(&fields, 13) == Some(2) && h265 { next.codec = VideoCodec::H265; }
|
||||
// Field 1 is the viewer's fps option index (1 = 30, 2 = 60, ...); 18 fpsCount is the frame rate.
|
||||
if let Some(fps) = number(&fields, 18).filter(|v| *v > 0).or_else(|| number(&fields, 1).and_then(fps_from_option)) { next.fps = fps as u32; }
|
||||
if let Some(quality) = number(&fields, 2).filter(|v| *v > 0) { next.quality = quality; }
|
||||
if let Some(bitrate) = number(&fields, 8).filter(|v| *v > 0) { next.custom_mbps = bitrate as u32; }
|
||||
// Field 7 chorma_format (sic): 3 = 4:4:4 when 真彩 is on, 1 = 4:2:0. The Sync reports what is encoded.
|
||||
match number(&fields, 7) {
|
||||
Some(CHROMA_444) if full_chroma => next.chroma = CHROMA_444,
|
||||
Some(CHROMA_444) => { log::info!("Chroma 4:4:4 requested; this encoder pipeline only supports 4:2:0"); next.chroma = CHROMA_420; }
|
||||
Some(CHROMA_420) => next.chroma = CHROMA_420,
|
||||
_ => {}
|
||||
}
|
||||
// ChooseResolutionType 3 = follow remote (native); otherwise pixel size, else logical size.
|
||||
if number(&fields, 10) == Some(3) { next.output = None; }
|
||||
else if let Some((w, h)) = number(&fields, 16).zip(number(&fields, 17)).or_else(|| number(&fields, 5).zip(number(&fields, 6))) {
|
||||
@@ -129,7 +171,7 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
|
||||
}
|
||||
next.fps = next.fps.clamp(MIN_FPS, MAX_FPS);
|
||||
settings.send_if_modified(|current| if *current != next {
|
||||
log::info!("Desktop video settings: fps={} quality={} max_kbps={:?} output={:?}", next.fps, next.quality, next.max_kbps(), next.output);
|
||||
log::info!("Desktop video settings: fps={} quality={} max_kbps={:?} output={:?} chroma={}", next.fps, next.quality, next.max_kbps(), next.output, next.chroma);
|
||||
*current = next; true
|
||||
} else { false });
|
||||
let mut out = Vec::new();
|
||||
@@ -156,7 +198,7 @@ pub fn sync_payload(settings: &VideoSettings, native: (u32, u32)) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
put_varint(1, u64::from(settings.fps), &mut out);
|
||||
put_varint(2, settings.quality, &mut out);
|
||||
put_varint(3, 1, &mut out); // ChromaFormat_420
|
||||
put_varint(3, settings.chroma, &mut out);
|
||||
if settings.custom_mbps > 0 { put_varint(4, u64::from(settings.custom_mbps), &mut out); }
|
||||
put_varint(5, u64::from(size.0), &mut out);
|
||||
put_varint(6, u64::from(size.1), &mut out);
|
||||
@@ -165,6 +207,23 @@ pub fn sync_payload(settings: &VideoSettings, native: (u32, u32)) -> Vec<u8> {
|
||||
out
|
||||
}
|
||||
|
||||
/// Message 18 CodecNegotiation {1 encoder_cap_list: EncoderCap {1 capture_mode (1 DESKTOP), 2 fps,
|
||||
/// 3 codec_type, 4 width, 5 height}} (official Windows host descriptor). The viewer's own list is
|
||||
/// ConnectOptions field 4 decoder_cap_list; it only selects a codec the host announces here.
|
||||
pub fn codec_negotiation(size: (u32, u32), fps: u32, hevc: bool) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
for codec in [Some(VideoCodec::H264), hevc.then_some(VideoCodec::H265)].into_iter().flatten() {
|
||||
let mut cap = Vec::new();
|
||||
put_varint(1, 1, &mut cap);
|
||||
put_varint(2, u64::from(fps), &mut cap);
|
||||
put_varint(3, codec.wire(), &mut cap);
|
||||
put_varint(4, u64::from(size.0), &mut cap);
|
||||
put_varint(5, u64::from(size.1), &mut cap);
|
||||
put_bytes(1, &cap, &mut out);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -187,7 +246,7 @@ mod tests {
|
||||
put_varint(18, 30, &mut body);
|
||||
let request = rpc(77, 2, &body);
|
||||
let response = handle_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080)).unwrap();
|
||||
assert_eq!(*rx.borrow(), VideoSettings { fps: 30, quality: QUALITY_GENERAL, custom_mbps: 0, output: None });
|
||||
assert_eq!(*rx.borrow(), VideoSettings { codec: VideoCodec::H264, fps: 30, quality: QUALITY_GENERAL, custom_mbps: 0, output: None, chroma: CHROMA_420 });
|
||||
assert_eq!(rx.borrow().max_kbps(), Some(2_000));
|
||||
let fields = decode_wire(&response).unwrap();
|
||||
assert_eq!(message(&fields, 1), Some(&[8u8, 77][..]));
|
||||
@@ -208,6 +267,29 @@ mod tests {
|
||||
assert!(handle_rpc(&decode_wire(&rpc(3, 9, &[])).unwrap(), &tx, (1920, 1080)).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn true_color_request_selects_444_only_when_the_encoder_supports_it() {
|
||||
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||
// Observed Mac viewer request with 真彩 on: field 7 = 3.
|
||||
let request = rpc(5, 2, &[8, 2, 16, 4, 56, 3, 144, 1, 60]);
|
||||
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, false).unwrap();
|
||||
assert_eq!(rx.borrow().chroma, CHROMA_420);
|
||||
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
|
||||
assert_eq!(rx.borrow().chroma, CHROMA_444);
|
||||
assert_eq!(number(&decode_wire(&sync_payload(&rx.borrow(), (1920, 1080))).unwrap(), 3), Some(CHROMA_444));
|
||||
apply_rpc(&decode_wire(&rpc(6, 2, &[56, 1])).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
|
||||
assert_eq!(rx.borrow().chroma, CHROMA_420);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codec_negotiation_lists_h264_then_optional_h265() {
|
||||
assert_eq!(codec_negotiation((1920, 1080), 60, false), [10, 12, 8, 1, 16, 60, 24, 1, 32, 128, 15, 40, 184, 8]);
|
||||
let both = codec_negotiation((1920, 1080), 60, true);
|
||||
let caps: Vec<_> = decode_wire(&both).unwrap().iter().filter_map(|f| match f.value { WireValue::Bytes(b) => Some(b), _ => None })
|
||||
.map(|b| number(&decode_wire(b).unwrap(), 3)).collect();
|
||||
assert_eq!(caps, [Some(1), Some(2)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolution_requests_scale_below_native_only() {
|
||||
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||
@@ -235,3 +317,27 @@ mod tests {
|
||||
assert_eq!(number(&fields, 10), Some(1080));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod codec_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn codec_is_opt_in_and_requires_negotiated_support() {
|
||||
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||
let rpc = |value| decode_wire(if value == 2 { &[18, 2, 104, 2] } else { &[18, 2, 104, 1] }).unwrap();
|
||||
assert_eq!(rx.borrow().codec, VideoCodec::H264);
|
||||
handle_rpc_with_codecs(&rpc(2), &tx, (1920,1080), true, false).unwrap();
|
||||
assert_eq!(rx.borrow().codec, VideoCodec::H264);
|
||||
handle_rpc_with_codecs(&rpc(2), &tx, (1920,1080), true, true).unwrap();
|
||||
assert_eq!(rx.borrow().codec, VideoCodec::H265);
|
||||
handle_rpc_with_codecs(&rpc(1), &tx, (1920,1080), false, true).unwrap();
|
||||
assert_eq!(rx.borrow().codec, VideoCodec::H265); // unnegotiated H264 is never sent
|
||||
// An unrelated FPS RPC must preserve the explicit codec selection.
|
||||
handle_rpc_with_codecs(&decode_wire(&[26,2,8,1]).unwrap(), &tx, (1920,1080), true, true).unwrap();
|
||||
assert_eq!(rx.borrow().codec, VideoCodec::H265);
|
||||
// The viewer reports its current codec (1) in every request; that must not undo H.265.
|
||||
handle_rpc_with_codecs(&rpc(1), &tx, (1920,1080), true, true).unwrap();
|
||||
assert_eq!(rx.borrow().codec, VideoCodec::H265);
|
||||
assert_eq!(preferred_codec(true, false), VideoCodec::H264);
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
//! RFC 7798 §4.4.1 / §4.4.3, single-layer non-interleaved HEVC (no DONL).
|
||||
//! Parameter sets travel in-band before each IDR; no aggregation is required.
|
||||
use bytes::Bytes;
|
||||
|
||||
pub fn payload(mtu: usize, annex_b: &[u8]) -> Result<Vec<Bytes>, String> {
|
||||
if mtu < 4 { return Err("HEVC payload MTU too small".into()); }
|
||||
let mut starts = Vec::new();
|
||||
let mut i = 0;
|
||||
while i + 3 <= annex_b.len() {
|
||||
let prefix = if annex_b[i..].starts_with(&[0, 0, 0, 1]) { 4 }
|
||||
else if annex_b[i..].starts_with(&[0, 0, 1]) { 3 } else { i += 1; continue; };
|
||||
starts.push((i, i + prefix)); i += prefix;
|
||||
}
|
||||
if starts.is_empty() { return Err("HEVC access unit has no Annex-B start code".into()); }
|
||||
let mut out = Vec::new();
|
||||
for (index, &(_, start)) in starts.iter().enumerate() {
|
||||
let mut end = starts.get(index + 1).map_or(annex_b.len(), |s| s.0);
|
||||
while end > start && annex_b[end - 1] == 0 { end -= 1; } // trailing_zero_8bits
|
||||
let nal = &annex_b[start..end];
|
||||
if nal.len() < 2 || nal[0] & 0x80 != 0 || nal[1] & 7 == 0 {
|
||||
return Err("Invalid HEVC NAL header".into());
|
||||
}
|
||||
let kind = (nal[0] >> 1) & 63;
|
||||
if kind >= 48 { return Err("HEVC Annex-B contains RTP packet type".into()); }
|
||||
if nal.len() <= mtu { out.push(Bytes::copy_from_slice(nal)); continue; }
|
||||
let chunks = nal[2..].chunks(mtu - 3);
|
||||
let last = chunks.len() - 1;
|
||||
for (n, chunk) in chunks.enumerate() {
|
||||
let mut fu = Vec::with_capacity(3 + chunk.len());
|
||||
// Keep F, LayerId and TID, replacing only the NAL type with FU=49.
|
||||
fu.extend_from_slice(&[(nal[0] & 0x81) | (49 << 1), nal[1],
|
||||
kind | if n == 0 { 0x80 } else { 0 } | if n == last { 0x40 } else { 0 }]);
|
||||
fu.extend_from_slice(chunk); out.push(fu.into());
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn parameter_sets_and_small_nals_remain_in_order_without_start_codes() {
|
||||
assert_eq!(payload(1200, &[0,0,0,1,64,1,9,0,0,1,66,1,8,0,0,1,38,1,7]).unwrap(),
|
||||
vec![Bytes::from_static(&[64,1,9]), Bytes::from_static(&[66,1,8]), Bytes::from_static(&[38,1,7])]);
|
||||
}
|
||||
#[test]
|
||||
fn fu_round_trip_preserves_header_payload_and_mtu_boundaries() {
|
||||
for mtu in [4, 5, 7, 1190] {
|
||||
let header = [0x27, 0x2b]; // IDR type19, nonzero layer and temporal id
|
||||
let data: Vec<u8> = (0..2501).map(|i| (i % 251 + 1) as u8).collect();
|
||||
let mut unit = vec![0,0,0,1]; unit.extend(header); unit.extend(&data);
|
||||
let packets = payload(mtu, &unit).unwrap();
|
||||
assert!(packets.len() > 1);
|
||||
let mut rebuilt = vec![(packets[0][0] & 0x81) | ((packets[0][2] & 63) << 1), packets[0][1]];
|
||||
for (i, p) in packets.iter().enumerate() {
|
||||
assert!(p.len() <= mtu); assert_eq!((p[0] >> 1) & 63, 49);
|
||||
assert_eq!(p[2] & 0x80 != 0, i == 0);
|
||||
assert_eq!(p[2] & 0x40 != 0, i + 1 == packets.len());
|
||||
rebuilt.extend_from_slice(&p[3..]);
|
||||
}
|
||||
assert_eq!(&rebuilt[..2], &header); assert_eq!(&rebuilt[2..], &data);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn rejects_invalid_headers_and_too_small_mtu() {
|
||||
for bytes in [&[0,0,1,64][..], &[0,0,1,64,0][..], &[0,0,1,128,1][..], &[64,1][..]] {
|
||||
assert!(payload(1200, bytes).is_err());
|
||||
}
|
||||
assert!(payload(3, &[0,0,1,64,1]).is_err());
|
||||
}
|
||||
}
|
||||
+77
-26
@@ -1,4 +1,4 @@
|
||||
//! In-process X11 capture and H.264 encoding through libavdevice/libavcodec.
|
||||
//! In-process X11 capture and H.264/HEVC encoding through libavdevice/libavcodec.
|
||||
//! It replaces the `ffmpeg` child process with the same codec options, so the viewer
|
||||
//! still receives the same Annex-B access units (AUD, SPS/PPS, IDR, P).
|
||||
use std::ffi::CString;
|
||||
@@ -11,6 +11,8 @@ use ff::ChannelLayout;
|
||||
use ff::util::picture;
|
||||
use ff::{codec, encoder, frame, software::scaling, Dictionary, Packet, Rational};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::{Semaphore, OwnedSemaphorePermit};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use crate::damage_capture::DamageCapture;
|
||||
|
||||
@@ -25,6 +27,7 @@ pub struct Config {
|
||||
pub max_bps: i64,
|
||||
pub buffer_bits: i64,
|
||||
pub encoder: crate::video::Encoder,
|
||||
pub codec: crate::capture_setting::VideoCodec,
|
||||
/// Capture on XDamage changes (XShm) instead of x11grab; `UU_CAPTURE=grab` turns it off.
|
||||
pub damage: bool,
|
||||
}
|
||||
@@ -57,13 +60,38 @@ fn ensure_init() -> Result<(), String> {
|
||||
result
|
||||
}
|
||||
|
||||
/// Sends one access unit per encoder packet. Returns `Ok(false)` when the receiver is gone.
|
||||
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<Vec<u8>>) -> Result<bool, String> {
|
||||
pub struct EncodedUnit { pub data: Vec<u8>, pub permit: OwnedSemaphorePermit, pub completed_at: Instant }
|
||||
|
||||
fn acquire(budget: &Arc<Semaphore>, units: &mpsc::Sender<EncodedUnit>) -> Option<OwnedSemaphorePermit> {
|
||||
tokio::runtime::Handle::current().block_on(async {
|
||||
tokio::select! {
|
||||
permit = budget.clone().acquire_owned() => permit.ok(),
|
||||
_ = units.closed() => None,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod budget_tests {
|
||||
use super::*;
|
||||
#[tokio::test]
|
||||
async fn closing_encoder_output_wakes_a_blocked_capture() {
|
||||
let budget=Arc::new(Semaphore::new(0));
|
||||
let (tx,rx)=mpsc::channel(1);
|
||||
let worker=tokio::task::spawn_blocking(move || acquire(&budget,&tx));
|
||||
tokio::task::yield_now().await;
|
||||
drop(rx);
|
||||
assert!(tokio::time::timeout(Duration::from_secs(1),worker).await.unwrap().unwrap().is_none());
|
||||
}
|
||||
}
|
||||
|
||||
fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender<EncodedUnit>, permits: &mut VecDeque<OwnedSemaphorePermit>) -> Result<bool, String> {
|
||||
loop {
|
||||
match encoder.receive_packet(packet) {
|
||||
Ok(()) => {
|
||||
let data = packet.data().unwrap_or_default().to_vec();
|
||||
if units.blocking_send(data).is_err() { return Ok(false); }
|
||||
let permit = permits.pop_front().ok_or("Encoder returned an unbudgeted access unit")?;
|
||||
if units.blocking_send(EncodedUnit {data, permit, completed_at: Instant::now()}).is_err() { return Ok(false); }
|
||||
}
|
||||
Err(ff::Error::Other { errno }) if errno == libc::EAGAIN => return Ok(true),
|
||||
Err(error) => return Err(text(error)),
|
||||
@@ -71,11 +99,11 @@ fn drain(encoder: &mut encoder::Video, packet: &mut Packet, units: &mpsc::Sender
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the H.264 encoder for `config`. Returns the encoder, its input pixel format and output size.
|
||||
/// Opens the selected video encoder for `config`. Returns the encoder, its input pixel format and output size.
|
||||
fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), String> {
|
||||
let (width, height) = config.output.unwrap_or(config.input);
|
||||
let codec = encoder::find_by_name(config.encoder.name())
|
||||
.ok_or("H.264 encoder is not available in this FFmpeg build")?;
|
||||
let codec = encoder::find_by_name(config.encoder.name_for(config.codec))
|
||||
.ok_or_else(|| format!("{} is not available in this FFmpeg build", config.encoder.name_for(config.codec)))?;
|
||||
// Allocate for this codec so FFmpeg fills its defaults; libx264 rejects a context allocated without one.
|
||||
let mut context = unsafe { codec::context::Context::wrap(ff::sys::avcodec_alloc_context3(codec.as_ptr()), None) }.encoder().video().map_err(text)?;
|
||||
context.set_width(width);
|
||||
@@ -99,15 +127,22 @@ fn open_encoder(config: &Config) -> Result<(encoder::Video, Pixel, u32, u32), St
|
||||
let cq = config.cq.to_string();
|
||||
let gop = config.gop.to_string();
|
||||
let mut options = Dictionary::new();
|
||||
let hevc = config.codec == crate::capture_setting::VideoCodec::H265;
|
||||
if config.encoder == crate::video::Encoder::Vaapi {
|
||||
for (key,value) in [("profile", "constrained_baseline"), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
|
||||
for (key,value) in [("profile", if hevc { "main" } else { "constrained_baseline" }), ("rc_mode", "VBR"), ("async_depth", "1"), ("aud", "1")] { options.set(key,value); }
|
||||
options.set("qmin", &cq);
|
||||
} else if config.encoder == crate::video::Encoder::Nvenc {
|
||||
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", "baseline"), ("rc", "vbr"),
|
||||
for (key, value) in [("preset", "p4"), ("tune", "ull"), ("profile", if hevc { "main" } else { "baseline" }), ("rc", "vbr"),
|
||||
("rc-lookahead", "0"), ("zerolatency", "1"), ("delay", "0"), ("aud", "1"), ("forced-idr", "1")] {
|
||||
options.set(key, value);
|
||||
}
|
||||
options.set("cq", &cq);
|
||||
} else if hevc {
|
||||
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "main"), ("threads", "4"), ("forced-idr", "1")] {
|
||||
options.set(key, value);
|
||||
}
|
||||
options.set("crf", &cq);
|
||||
options.set("x265-params", &crate::video::x265_params(&gop));
|
||||
} else {
|
||||
let x264 = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
|
||||
for (key, value) in [("preset", "superfast"), ("tune", "zerolatency"), ("profile", "baseline"), ("threads", "4")] {
|
||||
@@ -160,11 +195,11 @@ fn send_video_frame(encoder: &mut encoder::Video, frame: &frame::Video, kind: cr
|
||||
}
|
||||
|
||||
/// Runs until `stop` is set or the receiver is dropped. A set `idr` flag forces the next frame to be an IDR.
|
||||
pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
pub fn run(config: Config, units: mpsc::Sender<EncodedUnit>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>) -> Result<(), String> {
|
||||
ensure_init()?;
|
||||
if config.damage {
|
||||
match DamageCapture::open(&config.display) {
|
||||
Ok(capture) => return run_damage(config, capture, units, stop, idr),
|
||||
Ok(capture) => return run_damage(config, capture, units, stop, idr, budget),
|
||||
Err(error) => log::warn!("Damage-driven capture unavailable, using x11grab: {error}"),
|
||||
}
|
||||
}
|
||||
@@ -187,14 +222,18 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
|
||||
|
||||
let mut scaler: Option<(scaling::Context, (Pixel, u32, u32))> = None;
|
||||
let mut decoded = frame::Video::empty();
|
||||
let mut converted = frame::Video::new(pixel, width, height);
|
||||
let mut converted = frame::Video::empty();
|
||||
let mut packet = Packet::empty();
|
||||
let started = Instant::now();
|
||||
let frame_interval = Duration::from_nanos(1_000_000_000 / u64::from(config.fps));
|
||||
let mut last_at: Option<Duration> = None;
|
||||
let mut last_slot: i64 = -1;
|
||||
for (stream, input_packet) in input.packets() {
|
||||
let mut permits = VecDeque::with_capacity(4);
|
||||
let mut packets = input.packets();
|
||||
loop {
|
||||
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||
let Some(permit) = acquire(&budget, &units) else { return Ok(()); };
|
||||
let Some((stream, input_packet)) = packets.next() else { return Ok(()); };
|
||||
if stream.index() != index { continue; }
|
||||
decoder.send_packet(&input_packet).map_err(text)?;
|
||||
match decoder.receive_frame(&mut decoded) {
|
||||
@@ -228,20 +267,19 @@ pub fn run(config: Config, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>,
|
||||
frame.set_pts(Some(slot));
|
||||
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
||||
permits.push_back(permit);
|
||||
if !drain(&mut encoder, &mut packet, &units, &mut permits)? { return Ok(()); }
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Damage-driven loop. Damage only marks the picture dirty; a dirty picture is captured and encoded on
|
||||
/// the next fixed frame tick, so the stream keeps a steady cadence while the screen moves. The viewer's
|
||||
/// jitter buffer grows with irregular arrivals, and irregular sends were what raised its latency.
|
||||
/// An idle screen sends a keepalive about once a second so the viewer still sees traffic.
|
||||
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Vec<u8>>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<EncodedUnit>, stop: Arc<AtomicBool>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>) -> Result<(), String> {
|
||||
let (mut encoder, pixel, width, height) = open_encoder(&config)?;
|
||||
let (cap_width, cap_height) = capture.size();
|
||||
let mut captured = frame::Video::new(Pixel::BGRZ, cap_width, cap_height);
|
||||
let mut converted = frame::Video::new(pixel, width, height);
|
||||
let mut converted = frame::Video::empty();
|
||||
let mut scaler = None;
|
||||
let mut packet = Packet::empty();
|
||||
let started = Instant::now();
|
||||
@@ -253,6 +291,9 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
|
||||
let mut next_tick = Instant::now();
|
||||
let mut last_send = Instant::now();
|
||||
let mut last_slot: i64 = -1;
|
||||
let mut permits = VecDeque::with_capacity(4);
|
||||
let locked = crate::session_lock::watch_locked();
|
||||
let mut last_locked = *locked.borrow();
|
||||
loop {
|
||||
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||
let deadline = if dirty { next_tick } else { last_send + keepalive };
|
||||
@@ -263,13 +304,20 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
|
||||
let now = Instant::now();
|
||||
if dirty && now < next_tick { continue; }
|
||||
if !dirty && now < last_send + keepalive { continue; }
|
||||
let Some(permit) = acquire(&budget, &units) else { return Ok(()); };
|
||||
if stop.load(Ordering::Relaxed) { return Ok(()); }
|
||||
dirty |= capture.wait(Duration::ZERO)?;
|
||||
let state = *locked.borrow();
|
||||
if state != last_locked { dirty = true; last_locked = state; }
|
||||
|
||||
let source = capture.capture()?;
|
||||
let bytes_per_line = cap_width as usize * 4;
|
||||
let stride = captured.stride(0);
|
||||
let row = bytes_per_line.min(stride);
|
||||
for (y, src) in source.chunks_exact(bytes_per_line).enumerate() {
|
||||
captured.data_mut(0)[y * stride..y * stride + row].copy_from_slice(&src[..row]);
|
||||
if dirty {
|
||||
let source = capture.capture()?;
|
||||
let bytes_per_line = cap_width as usize * 4;
|
||||
let stride = captured.stride(0);
|
||||
let row = bytes_per_line.min(stride);
|
||||
for (y, src) in source.chunks_exact(bytes_per_line).enumerate() {
|
||||
captured.data_mut(0)[y * stride..y * stride + row].copy_from_slice(&src[..row]);
|
||||
}
|
||||
}
|
||||
|
||||
let slot = ((started.elapsed().as_micros() * u128::from(config.fps) + 500_000) / 1_000_000) as i64;
|
||||
@@ -282,13 +330,16 @@ fn run_damage(config: Config, mut capture: DamageCapture, units: mpsc::Sender<Ve
|
||||
if scaler.is_none() {
|
||||
scaler = Some(scaling::Context::get(Pixel::BGRZ, cap_width, cap_height, pixel, width, height, scaling::Flags::BILINEAR).map_err(text)?);
|
||||
}
|
||||
if let Some(context) = scaler.as_mut() { context.run(&captured, &mut converted).map_err(text)?; }
|
||||
if dirty {
|
||||
if let Some(context) = scaler.as_mut() { context.run(&captured, &mut converted).map_err(text)?; }
|
||||
}
|
||||
&mut converted
|
||||
};
|
||||
frame.set_pts(Some(slot));
|
||||
frame.set_kind(if idr.swap(false, Ordering::AcqRel) { picture::Type::I } else { picture::Type::None });
|
||||
send_video_frame(&mut encoder, frame, config.encoder)?;
|
||||
if !drain(&mut encoder, &mut packet, &units)? { return Ok(()); }
|
||||
permits.push_back(permit);
|
||||
if !drain(&mut encoder, &mut packet, &units, &mut permits)? { return Ok(()); }
|
||||
let now = Instant::now();
|
||||
if dirty {
|
||||
// Keep the tick phase; after a long idle, restart the phase from now instead of bursting to catch up.
|
||||
|
||||
+41
-5
@@ -24,6 +24,8 @@ mod signaling;
|
||||
mod terminal;
|
||||
mod webrtc;
|
||||
mod video;
|
||||
mod video_track;
|
||||
mod hevc;
|
||||
#[cfg(feature = "inproc-av")]
|
||||
mod damage_capture;
|
||||
#[cfg(feature = "inproc-av")]
|
||||
@@ -63,14 +65,19 @@ fn restart_host() -> ! {
|
||||
eprintln!("restart failed: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
/// systemd user unit running this process (from /proc/self/cgroup), e.g. "uuremote-native.service".
|
||||
fn systemd_unit() -> Option<String> {
|
||||
std::env::var_os("INVOCATION_ID")?;
|
||||
let cgroup = std::fs::read_to_string("/proc/self/cgroup").ok()?;
|
||||
cgroup.lines().find_map(|l| l.rsplit('/').next().filter(|u| u.ends_with(".service")).map(str::to_owned))
|
||||
}
|
||||
fn display_present() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() }
|
||||
/// SIGUSR1 (`uuremote-server show`) opens the host window.
|
||||
fn present_on_usr1(ui: std::sync::Arc<assist::LocalUi>) {
|
||||
// Install the handler before publishing the pid: SIGUSR1's default action terminates the process.
|
||||
let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) else { return };
|
||||
write_host_pid();
|
||||
tokio::spawn(async move {
|
||||
let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) else { return };
|
||||
while usr1.recv().await.is_some() { ui.present(); }
|
||||
});
|
||||
tokio::spawn(async move { while usr1.recv().await.is_some() { ui.present(); } });
|
||||
}
|
||||
|
||||
fn host_pid_path() -> std::path::PathBuf { config::path().with_file_name("host.pid") }
|
||||
@@ -94,6 +101,24 @@ fn show_running_host() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if let Some(pid) = running {
|
||||
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
|
||||
}
|
||||
// Stopped from the tray but installed as a user service: start the service (not a second,
|
||||
// unmanaged host), wait for it to publish its pid, then ask it to show the window.
|
||||
let unit = std::env::var("UU_SERVICE_UNIT").unwrap_or_else(|_| "uuremote-native.service".into());
|
||||
let installed = std::process::Command::new("systemctl").args(["--user", "cat", &unit])
|
||||
.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).status().is_ok_and(|s| s.success());
|
||||
if installed && std::process::Command::new("systemctl").args(["--user", "start", &unit]).status().is_ok_and(|s| s.success()) {
|
||||
let before = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok();
|
||||
for _ in 0..60 {
|
||||
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||
let fresh = std::fs::metadata(host_pid_path()).and_then(|m| m.modified()).ok().is_some_and(|t| Some(t) != before);
|
||||
let pid = std::fs::read_to_string(host_pid_path()).ok().and_then(|s| s.trim().parse::<i32>().ok());
|
||||
if let (true, Some(pid)) = (fresh, pid) {
|
||||
std::thread::sleep(std::time::Duration::from_millis(500)); // signal handler installed right after the pid
|
||||
if unsafe { libc::kill(pid, libc::SIGUSR1) } == 0 { return Ok(()); }
|
||||
}
|
||||
}
|
||||
return Err("the UU remote host service started but did not open its window".into());
|
||||
}
|
||||
use std::os::unix::process::CommandExt;
|
||||
Err(Box::new(std::process::Command::new("/proc/self/exe").env("UU_SHOW_WINDOW", "1").exec()))
|
||||
}
|
||||
@@ -524,6 +549,14 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
SignalingEvent::AssistCommand(assist::Command::Verify(v)) => { if let Some(s) = assist.as_mut() { s.set_verify(v)?; } }
|
||||
SignalingEvent::AssistCommand(assist::Command::Refresh(r)) => { if let Some(s) = assist.as_mut() { s.set_refresh(r)?; } }
|
||||
SignalingEvent::AssistCommand(assist::Command::Custom(c)) => { if let Some(s) = assist.as_mut() { s.set_custom(c)?; } }
|
||||
SignalingEvent::AssistCommand(assist::Command::Quit) => {
|
||||
// Tray 退出: under systemd (Restart=always) stop the unit itself, else just exit.
|
||||
log::info!("Quit requested from the tray");
|
||||
if let Some(unit) = systemd_unit() {
|
||||
let _ = std::process::Command::new("systemctl").args(["--user", "stop", "--no-block", &unit]).status();
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
SignalingEvent::AssistCommand(assist::Command::Logout) => {
|
||||
if let Some(s) = assist.as_mut().filter(|s| s.is_account()) {
|
||||
s.logout().await;
|
||||
@@ -629,7 +662,10 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
log::info!("Desktop connection ignored: headless mode (terminal, file transfer and port mapping remain available)");
|
||||
continue;
|
||||
}
|
||||
let session_capture=if desktop && (capture.init_error.is_some() || stale_portal) {
|
||||
let resized=desktop && capture.init_error.is_none() && capture.backend==desktop_backend::DesktopBackend::X11
|
||||
&& tokio::task::spawn_blocking(crate::capture::x11_resized).await.unwrap_or(false);
|
||||
if resized { log::info!("Desktop geometry changed since startup; reinitializing capture"); }
|
||||
let session_capture=if desktop && (capture.init_error.is_some() || stale_portal || resized) {
|
||||
if stale_portal {
|
||||
input.release_held();input.bind_portal(None);
|
||||
capture=Arc::new(ScreenCapture::unavailable("Portal authorization expired".into()));
|
||||
|
||||
+25
-8
@@ -1,6 +1,8 @@
|
||||
//! GNOME/KDE portal session. Capture and input must share this Arc; do not
|
||||
//! recreate it in an encoder subprocess (that would request consent again).
|
||||
use std::sync::Arc;
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct FrameVersion(usize, u64);
|
||||
#[cfg(unix)]
|
||||
#[path = "portal_permissions.rs"]
|
||||
mod permissions;
|
||||
@@ -17,7 +19,7 @@ mod native {
|
||||
fn uu_portal_close(portal: *mut c_void);
|
||||
fn uu_portal_alive(portal: *mut c_void) -> i32;
|
||||
fn uu_portal_reset_frame(portal: *mut c_void);
|
||||
fn uu_portal_capture(portal: *mut c_void, bgra: *mut u8, length: usize) -> i32;
|
||||
fn uu_portal_capture_since(portal: *mut c_void, bgra: *mut u8, length: usize, revision: *mut u64) -> i32;
|
||||
fn uu_portal_move(portal: *mut c_void, x: f64, y: f64, relative: i32) -> i32;
|
||||
fn uu_portal_button(portal: *mut c_void, button: i32, pressed: i32) -> i32;
|
||||
fn uu_portal_key(portal: *mut c_void, keysym: u32, pressed: i32) -> i32;
|
||||
@@ -26,6 +28,10 @@ mod native {
|
||||
// The engine owns callback threads; public calls are serialized by Mutex.
|
||||
unsafe impl Send for Handle {}
|
||||
impl Drop for Handle { fn drop(&mut self) { unsafe { uu_portal_close(self.0.as_ptr()) } } }
|
||||
fn blank_changed(frame: &mut [u8], version: &mut Option<super::FrameVersion>) -> bool {
|
||||
if *version == Some(super::FrameVersion(0, 0)) { return false; }
|
||||
super::blank(frame); *version = Some(super::FrameVersion(0, 0)); true
|
||||
}
|
||||
pub struct Session { handle: Mutex<Handle>, locked: tokio::sync::watch::Receiver<Option<bool>>, was_locked: std::sync::atomic::AtomicBool, retry_at: Mutex<Option<std::time::Instant>>, pub width: u32, pub height: u32 }
|
||||
impl Session {
|
||||
pub fn is_alive(&self) -> bool {
|
||||
@@ -56,6 +62,10 @@ mod native {
|
||||
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), retry_at: Mutex::new(None), width, height })
|
||||
}
|
||||
pub fn capture(&self, frame: &mut [u8]) -> Result<(), String> {
|
||||
self.capture_changed(frame, &mut None).map(|_| ())
|
||||
}
|
||||
pub fn capture_changed(&self, frame: &mut [u8], version: &mut Option<super::FrameVersion>) -> Result<bool, String> {
|
||||
if frame.len() != self.width as usize * self.height as usize * 4 { return Err(super::error(11)); }
|
||||
let mut handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||
let state = *self.locked.borrow();
|
||||
if state != Some(false) {
|
||||
@@ -64,30 +74,30 @@ mod native {
|
||||
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
||||
}
|
||||
super::blank(frame); return Ok(());
|
||||
return Ok(blank_changed(frame, version));
|
||||
}
|
||||
if self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel) {
|
||||
// First unlocked capture after a real lock: drop frames captured while locked.
|
||||
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
||||
super::blank(frame); return Ok(());
|
||||
return Ok(blank_changed(frame, version));
|
||||
}
|
||||
if unsafe { uu_portal_alive(handle.0.as_ptr()) } == 0 {
|
||||
// GNOME can close the stream before the periodic lock watcher updates.
|
||||
if crate::session_lock::locked_now() != Some(false) {
|
||||
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||
super::blank(frame); return Ok(());
|
||||
return Ok(blank_changed(frame, version));
|
||||
}
|
||||
// logind's LockedHint can lag compositor stream closure. Give it a
|
||||
// full watcher interval before attempting another permission session.
|
||||
let mut retry = self.retry_at.lock().map_err(|_| "Portal recovery lock poisoned")?;
|
||||
let deadline = retry.get_or_insert_with(|| std::time::Instant::now() + std::time::Duration::from_secs(1));
|
||||
if std::time::Instant::now() < *deadline { super::blank(frame); return Ok(()); }
|
||||
if std::time::Instant::now() < *deadline { return Ok(blank_changed(frame, version)); }
|
||||
let replacement = match Self::open() {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
log::warn!("Portal capture recovery deferred: {error}");
|
||||
*retry = Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
|
||||
super::blank(frame); return Ok(());
|
||||
return Ok(blank_changed(frame, version));
|
||||
}
|
||||
};
|
||||
*retry = None;
|
||||
@@ -97,8 +107,14 @@ mod native {
|
||||
*handle = replacement.handle.into_inner().map_err(|_| "Portal session lock poisoned")?;
|
||||
log::info!("Portal capture session restored after closure");
|
||||
}
|
||||
let code = unsafe { uu_portal_capture(handle.0.as_ptr(), frame.as_mut_ptr(), frame.len()) };
|
||||
if code == 10 || code == 9 { super::blank(frame); Ok(()) } else { super::check(code) }
|
||||
let identity = handle.0.as_ptr() as usize;
|
||||
let mut revision = version.filter(|v| v.0 == identity).map_or(0, |v| v.1);
|
||||
let code = unsafe { uu_portal_capture_since(handle.0.as_ptr(), frame.as_mut_ptr(), frame.len(), &mut revision) };
|
||||
match code {
|
||||
0 | 12 => { *version = Some(super::FrameVersion(identity, revision)); Ok(code == 0) }
|
||||
9 | 10 => Ok(blank_changed(frame, version)),
|
||||
_ => Err(super::error(code)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clipboard_supported(&self) -> bool {
|
||||
@@ -166,6 +182,7 @@ impl PortalSession {
|
||||
pub fn is_alive(&self) -> bool { false }
|
||||
pub fn open() -> Result<Self, String> { Err("Portal backend requires Linux and --features portal".into()) }
|
||||
pub fn capture(&self, _: &mut [u8]) -> Result<(), String> { Err(error(2)) }
|
||||
pub fn capture_changed(&self, _: &mut [u8], _: &mut Option<FrameVersion>) -> Result<bool, String> { Err(error(2)) }
|
||||
pub fn move_pointer(&self, _: f64, _: f64, _: bool) -> Result<(), String> { Err(error(2)) }
|
||||
pub fn button(&self, _: i32, _: bool) -> Result<(), String> { Err(error(2)) }
|
||||
pub fn key(&self, _: u64, _: bool) -> Result<(), String> { Err(error(2)) }
|
||||
|
||||
@@ -75,6 +75,66 @@ pub fn decode_wire(input: &[u8]) -> Result<Vec<WireField<'_>>, WireError> {
|
||||
Ok(fields)
|
||||
}
|
||||
|
||||
/// Structure-only rendering for diagnostics: field numbers, wire types, lengths and small
|
||||
/// varints (enums, sizes, fps). String and byte contents are never rendered.
|
||||
pub fn wire_shape(data: &[u8], depth: u8) -> String {
|
||||
let Ok(fields) = decode_wire(data) else { return format!("bytes({})", data.len()) };
|
||||
let parts: Vec<String> = fields.iter().map(|f| match f.value {
|
||||
WireValue::Varint(u64::MAX) => format!("{}=-1", f.number),
|
||||
WireValue::Varint(v) if v <= 0xFFFF => format!("{}={v}", f.number),
|
||||
WireValue::Varint(_) => format!("{}=big", f.number),
|
||||
WireValue::Fixed32(_) => format!("{}:f32", f.number),
|
||||
WireValue::Fixed64(_) => format!("{}:f64", f.number),
|
||||
WireValue::Bytes(b) => format!("{}:{}", f.number, bytes_shape(b, depth)),
|
||||
}).collect();
|
||||
format!("{{{}}}", parts.join(","))
|
||||
}
|
||||
fn bytes_shape(b: &[u8], depth: u8) -> String {
|
||||
let printable = !b.is_empty() && std::str::from_utf8(b).is_ok_and(|t| t.chars().all(|c| !c.is_control()));
|
||||
if printable { return format!("str({})", b.len()); }
|
||||
if depth > 0 && !b.is_empty() && decode_wire(b).is_ok_and(|f| !f.is_empty() && f.iter().all(|x| (1..=128).contains(&x.number))) {
|
||||
return wire_shape(b, depth - 1);
|
||||
}
|
||||
format!("bytes({})", b.len())
|
||||
}
|
||||
|
||||
/// JSON counterpart: keys and value types; strings as lengths (or nested JSON/base64-protobuf
|
||||
/// shape), numbers only when small. Values that could identify a user are never rendered.
|
||||
pub fn json_shape(value: &serde_json::Value, depth: u8) -> String {
|
||||
use base64::Engine;
|
||||
use serde_json::Value;
|
||||
match value {
|
||||
Value::Null => "null".into(),
|
||||
Value::Bool(b) => b.to_string(),
|
||||
Value::Number(n) => match n.as_i64() { Some(v) if v.abs() <= 0xFFFF => v.to_string(), _ => "num".into() },
|
||||
Value::String(t) => {
|
||||
if depth > 0 {
|
||||
if let Ok(inner @ (Value::Object(_) | Value::Array(_))) = serde_json::from_str::<Value>(t) {
|
||||
return format!("json{}", json_shape(&inner, depth - 1));
|
||||
}
|
||||
if t.len() >= 8 && t.len() % 4 == 0 {
|
||||
if let Ok(raw) = base64::engine::general_purpose::STANDARD.decode(t) {
|
||||
if decode_wire(&raw).is_ok_and(|f| !f.is_empty()) { return format!("b64{}", wire_shape(&raw, depth - 1)); }
|
||||
}
|
||||
}
|
||||
}
|
||||
format!("str({})", t.len())
|
||||
}
|
||||
Value::Array(items) => match items.first() {
|
||||
Some(first) if depth > 0 => format!("[{}x{}]", items.len(), json_shape(first, depth - 1)),
|
||||
_ => format!("[{}]", items.len()),
|
||||
},
|
||||
Value::Object(map) if depth > 0 => {
|
||||
let parts: Vec<String> = map.iter().take(48).map(|(k, v)| {
|
||||
let key: String = k.chars().filter(|c| c.is_ascii_alphanumeric() || *c == '_').take(40).collect();
|
||||
format!("{key}:{}", json_shape(v, depth - 1))
|
||||
}).collect();
|
||||
format!("{{{}}}", parts.join(","))
|
||||
}
|
||||
Value::Object(map) => format!("{{{} keys}}", map.len()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -105,4 +165,20 @@ mod tests {
|
||||
assert_eq!(decode_wire(&[11]), Err(WireError::UnsupportedWireType(3)));
|
||||
assert_eq!(decode_wire(&[255, 255, 255, 255, 31]), Err(WireError::InvalidField));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shapes_never_render_contents() {
|
||||
// {1:1, 2:-1, 4:{1:60,2:2}, 9:"device-secret", 12:big}
|
||||
let mut data = vec![0x08, 0x01, 0x10];
|
||||
data.extend([0xff; 9]); data.push(0x01);
|
||||
data.extend([0x22, 0x04, 0x08, 0x3c, 0x10, 0x02]);
|
||||
data.extend([0x4a, 0x0d]); data.extend(b"device-secret");
|
||||
data.extend([0x60, 0x80, 0x80, 0x80, 0x80, 0x10]);
|
||||
let shape = super::wire_shape(&data, 3);
|
||||
assert_eq!(shape, "{1=1,2=-1,4:{1=60,2=2},9:str(13),12=big}");
|
||||
let header = serde_json::json!({"streamer_data": "{\"token\":\"secret-value\",\"n\":3}", "city": "Somewhere", "id": 123456789});
|
||||
let shape = super::json_shape(&header, 4);
|
||||
assert!(!shape.contains("secret") && !shape.contains("Somewhere") && !shape.contains("123456789"), "{shape}");
|
||||
assert!(shape.contains("streamer_data:json{n:3,token:str(12)}"), "{shape}");
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,30 @@ mod refresh_tests {
|
||||
// Retain it until after the server sees closure, just like PC callbacks.
|
||||
drop(retained);
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn silent_half_open_socket_is_reported_disconnected() {
|
||||
use futures_util::{SinkExt,StreamExt};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
let listener=tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address=listener.local_addr().unwrap();
|
||||
let (hold_tx,hold_rx)=tokio::sync::oneshot::channel::<()>();
|
||||
tokio::spawn(async move {
|
||||
let (tcp,_)=listener.accept().await.unwrap();
|
||||
let mut socket=tokio_tungstenite::accept_async(tcp).await.unwrap();
|
||||
// Shortest accepted window (clamped to 5 s); the server then never pings.
|
||||
socket.send(Message::Text(r#"0{"pingInterval":1000,"pingTimeout":1000}"#.into())).await.unwrap();
|
||||
let _=socket.next().await;
|
||||
socket.send(Message::Text("40{}".into())).await.unwrap();
|
||||
let _=hold_rx.await; // keep the TCP connection open but silent
|
||||
});
|
||||
let (sender,mut events)=tokio::sync::mpsc::channel(8);
|
||||
let _client=super::SignalingClient::connect(&format!("ws://{address}"),"synthetic-only",sender).await.unwrap();
|
||||
let disconnected=tokio::time::timeout(std::time::Duration::from_secs(8),async {
|
||||
loop { if matches!(events.recv().await,Some(super::SignalingEvent::Disconnected)|None) { return; } }
|
||||
}).await;
|
||||
assert!(disconnected.is_ok(),"a socket without pings must be reported dead");
|
||||
drop(hold_tx);
|
||||
}
|
||||
}
|
||||
|
||||
impl SignalingClient {
|
||||
@@ -156,9 +180,21 @@ impl SignalingClient {
|
||||
let mut accepted_refresh=0;
|
||||
// Socket.IO binary event awaiting attachments: (event name, header JSON).
|
||||
let mut pending_binary: Option<(String, Value)> = None;
|
||||
// Relation check only: the be-controlled ice_id versus the next offer's data.ice_id.
|
||||
let mut last_connect_ice: Option<String> = None;
|
||||
// Engine.IO 4: a client treats the connection as dead when no ping arrives within
|
||||
// pingInterval + pingTimeout. A half-open TCP socket (suspend, NAT drop) otherwise
|
||||
// blocks the read forever and the host silently stays offline.
|
||||
let mut ping_window=std::time::Duration::from_secs(25+20);
|
||||
let mut ping_deadline=tokio::time::Instant::now()+ping_window;
|
||||
loop {
|
||||
let msg_res=tokio::select! {
|
||||
_=event_sender.closed()=>break,
|
||||
_=tokio::time::sleep_until(ping_deadline) => {
|
||||
log::warn!("Signaling heartbeat lost: generation={} age_s={} pings={} last_ping_gap_s={}",generation,started.elapsed().as_secs(),pings,last_ping.elapsed().as_secs());
|
||||
let _ = event_sender.send(SignalingEvent::Disconnected).await;
|
||||
break;
|
||||
},
|
||||
// Official ControlledSession RefreshSignalTokenLoop: first
|
||||
// 30s, success every ~300s, failure retry after 30s. Native
|
||||
// libstreamer 0x885774 / 0x885e58; emit has NO payload args.
|
||||
@@ -185,12 +221,17 @@ impl SignalingClient {
|
||||
if txt.starts_with('0') {
|
||||
if let Ok(open)=serde_json::from_str::<Value>(&txt[1..]) {
|
||||
log::info!("Signaling heartbeat config: generation={} ping_interval_ms={:?} ping_timeout_ms={:?}",generation,open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64));
|
||||
if let (Some(interval),Some(timeout))=(open.get("pingInterval").and_then(Value::as_u64),open.get("pingTimeout").and_then(Value::as_u64)) {
|
||||
ping_window=std::time::Duration::from_millis(interval.saturating_add(timeout).clamp(5_000,300_000));
|
||||
ping_deadline=tokio::time::Instant::now()+ping_window;
|
||||
}
|
||||
}
|
||||
// Open packet -> 发送 Socket.IO connect
|
||||
let _ = tx_out_clone.send("40".into()).await;
|
||||
} else if txt == "2" {
|
||||
// Ping -> 回复 Pong
|
||||
let gap=last_ping.elapsed().as_secs();last_ping=std::time::Instant::now();pings+=1;
|
||||
ping_deadline=tokio::time::Instant::now()+ping_window;
|
||||
if tx_out_clone.send("3".into()).await.is_ok() { pong_queued+=1; }
|
||||
if pings==1 || pings%12==0 {
|
||||
log::info!("Signaling heartbeat progress: generation={} age_s={} pings={} pong_queued={} gap_s={}",generation,started.elapsed().as_secs(),pings,pong_queued,gap);
|
||||
@@ -255,6 +296,13 @@ impl SignalingClient {
|
||||
.await;
|
||||
}
|
||||
"soac" => {
|
||||
// Offers only: structure (keys, types, lengths), never SDP text or ids.
|
||||
let kind = data.get("data").unwrap_or(data).get("type").and_then(Value::as_str);
|
||||
if kind == Some("offer") {
|
||||
log::info!("SOAC offer shape: {}", crate::protocol::json_shape(data, 3));
|
||||
let offer_ice = data.get("data").unwrap_or(data).get("ice_id").and_then(Value::as_str);
|
||||
log::info!("SOAC offer ice_id equals be-controlled ice_id: {:?}", last_connect_ice.as_deref().zip(offer_ice).map(|(a, b)| a == b));
|
||||
}
|
||||
let (target_val, client_id_opt) = if let Some(inner) = data.get("data") {
|
||||
(inner.clone(), data.get("client_id").and_then(|v| v.as_str()).map(|s| s.to_string()))
|
||||
} else {
|
||||
@@ -317,6 +365,24 @@ impl SignalingClient {
|
||||
let ice_id = ["ice_id", "iceId"].iter().find_map(|k| header.get(*k).or_else(|| header.get("data").and_then(|d| d.get(*k))))
|
||||
.and_then(|v| v.as_str()).map(str::to_owned);
|
||||
log::info!("be-controlled connect request: capture_type={} header_keys={:?} ice_id_present={}", capture_type, keys, ice_id.is_some());
|
||||
last_connect_ice = ice_id.clone();
|
||||
// Structure only (numbers, types, lengths): documents what the viewer and cloud send.
|
||||
let header_shape: String = crate::protocol::json_shape(&header, 7).chars().take(8000).collect();
|
||||
log::info!("Connect request shape: app_data={} header={}", crate::protocol::wire_shape(data, 3), header_shape);
|
||||
// Relation only, never the ids themselves.
|
||||
let control_id = header.get("streamer_data").and_then(Value::as_str)
|
||||
.and_then(|t| serde_json::from_str::<Value>(t).ok())
|
||||
.and_then(|v| v.get("control_id").and_then(Value::as_str).map(str::to_owned));
|
||||
let app_control_id = header.get("app_control_id").and_then(Value::as_str);
|
||||
log::info!("Connect request ids: streamer_control_id_equals_app_control_id={:?}",
|
||||
control_id.as_deref().zip(app_control_id).map(|(a, b)| a == b));
|
||||
// ConnectOptions 4 decoder_cap_list: DecoderCap {1 fps, 2 codec_type (1 H264, 2 H265),
|
||||
// 3 width, 4 height, 5 chroma_format (1 4:2:0, 3 4:4:4)}. Numbers only.
|
||||
let caps: Vec<Vec<(u32, u64)>> = fields.iter().flatten().filter(|f| f.number == 4).filter_map(|f| match f.value {
|
||||
crate::protocol::WireValue::Bytes(b) => crate::protocol::decode_wire(b).ok().map(|cap| cap.iter().filter_map(|x| match x.value {
|
||||
crate::protocol::WireValue::Varint(v) => Some((x.number, v)), _ => None }).collect()),
|
||||
_ => None }).collect();
|
||||
if !caps.is_empty() { log::info!("Viewer decoder caps (fps,codec,w,h,chroma fields): {:?}", caps); }
|
||||
// Viewer platform (website enum: 1 windows, 2 android, 3 ios, 4 macos, 5 tv).
|
||||
let platform = header.get("controller_platform").or_else(|| header.get("data").and_then(|d| d.get("controller_platform")));
|
||||
let controller_mac = platform.is_some_and(|v| v.as_i64() == Some(4)
|
||||
|
||||
+102
-12
@@ -34,6 +34,7 @@ const MAGIC: &[u8; 4] = b"TERM";
|
||||
const HEADER: usize = 16;
|
||||
const MAX_PAYLOAD: usize = 0x10_0000;
|
||||
const SCROLLBACK: usize = 256 * 1024;
|
||||
const OUTPUT_CHUNK: usize = 16 * 1024;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Frame { pub kind: u8, pub flags: [u8; 2], pub session: u32, pub payload: Vec<u8> }
|
||||
@@ -70,9 +71,24 @@ struct Session {
|
||||
pid: i32,
|
||||
sink: Mutex<Option<UnboundedSender<Vec<u8>>>>,
|
||||
scrollback: Mutex<VecDeque<u8>>,
|
||||
size: Mutex<(u16, u16)>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// Lock order sink → scrollback matches OPEN, so a reattach snapshot sees each
|
||||
/// chunk either in the replay or live, never both.
|
||||
fn record_output(&self, data: &[u8]) {
|
||||
let mut sink = self.sink.lock().unwrap();
|
||||
{
|
||||
let mut sb = self.scrollback.lock().unwrap();
|
||||
sb.extend(data);
|
||||
let excess = sb.len().saturating_sub(SCROLLBACK);
|
||||
sb.drain(..excess);
|
||||
}
|
||||
if let Some(tx) = sink.as_ref() {
|
||||
if tx.send(encode(STDOUT, self.id, data)).is_err() { *sink = None; }
|
||||
}
|
||||
}
|
||||
fn emit(&self, kind: u8, payload: &[u8]) -> bool {
|
||||
let mut sink = self.sink.lock().unwrap();
|
||||
if let Some(tx) = sink.as_ref() {
|
||||
@@ -81,10 +97,31 @@ impl Session {
|
||||
}
|
||||
false
|
||||
}
|
||||
// The size lock is held across the ioctl so a delayed restore cannot reapply a stale size.
|
||||
fn resize(&self, cols: u16, rows: u16) {
|
||||
let mut size = self.size.lock().unwrap();
|
||||
*size = (cols, rows);
|
||||
self.set_winsize(cols, rows);
|
||||
}
|
||||
fn set_winsize(&self, cols: u16, rows: u16) {
|
||||
let win = libc::winsize { ws_row: rows, ws_col: cols, ws_xpixel: 0, ws_ypixel: 0 };
|
||||
unsafe { libc::ioctl(self.master.as_raw_fd(), libc::TIOCSWINSZ as _, &win); }
|
||||
}
|
||||
/// Full-screen programs repaint only on SIGWINCH, which an unchanged size never raises:
|
||||
/// shrink by one row, then restore the latest requested size after a pause.
|
||||
fn force_redraw(self: &Arc<Self>) {
|
||||
{
|
||||
let size = self.size.lock().unwrap();
|
||||
if size.1 <= 1 { return; }
|
||||
self.set_winsize(size.0, size.1 - 1);
|
||||
}
|
||||
let s = self.clone();
|
||||
std::thread::spawn(move || {
|
||||
std::thread::sleep(std::time::Duration::from_millis(150));
|
||||
let size = s.size.lock().unwrap();
|
||||
s.set_winsize(size.0, size.1);
|
||||
});
|
||||
}
|
||||
fn write(&self, data: &[u8]) {
|
||||
let mut off = 0;
|
||||
while off < data.len() {
|
||||
@@ -136,23 +173,15 @@ fn spawn(requested_id: u32, cols: u16, rows: u16, shell: Option<&str>) -> Result
|
||||
}
|
||||
};
|
||||
let session = Arc::new(Session { id, shell: shell.clone(), created_ms: now_ms(), pid: child.id() as i32,
|
||||
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()) });
|
||||
master: pty.master, sink: Mutex::new(None), scrollback: Mutex::new(VecDeque::new()), size: Mutex::new((cols, rows)) });
|
||||
let mut reader = std::fs::File::from(session.master.try_clone().map_err(|e| format!("pty dup failed: {e}"))?);
|
||||
let worker = session.clone();
|
||||
std::thread::Builder::new().name(format!("uu-term-{id}")).spawn(move || {
|
||||
let mut buf = vec![0u8; 16 * 1024];
|
||||
let mut buf = vec![0u8; OUTPUT_CHUNK];
|
||||
loop {
|
||||
match reader.read(&mut buf) {
|
||||
Ok(0) | Err(_) => break,
|
||||
Ok(n) => {
|
||||
{
|
||||
let mut sb = worker.scrollback.lock().unwrap();
|
||||
sb.extend(&buf[..n]);
|
||||
let excess = sb.len().saturating_sub(SCROLLBACK);
|
||||
sb.drain(..excess);
|
||||
}
|
||||
worker.emit(STDOUT, &buf[..n]);
|
||||
}
|
||||
Ok(n) => worker.record_output(&buf[..n]),
|
||||
}
|
||||
}
|
||||
let code = child.wait().ok().and_then(|s| s.code()).unwrap_or(-1);
|
||||
@@ -211,6 +240,7 @@ impl TerminalConn {
|
||||
OPEN => {
|
||||
let v = json(&frame.payload);
|
||||
let (cols, rows) = dims(&v);
|
||||
let reattach = existing.is_some();
|
||||
let session = match existing {
|
||||
Some(s) => { s.resize(cols, rows); Ok(s) }
|
||||
None => spawn(frame.session, cols, rows, v.get("shell").and_then(|s| s.as_str())),
|
||||
@@ -222,7 +252,11 @@ impl TerminalConn {
|
||||
self.send(OPEN_RESP, s.id, &reply(0, "ok"));
|
||||
*sink = Some(self.out.clone());
|
||||
let replay: Vec<u8> = s.scrollback.lock().unwrap().iter().copied().collect();
|
||||
if !replay.is_empty() { self.send(STDOUT, s.id, &replay); }
|
||||
// A full scrollback exceeds SCTP's 64 KiB outbound message limit.
|
||||
for chunk in replay.chunks(OUTPUT_CHUNK) { self.send(STDOUT, s.id, chunk); }
|
||||
drop(sink);
|
||||
// The replay tail starts mid-redraw for full-screen programs.
|
||||
if reattach { s.force_redraw(); }
|
||||
}
|
||||
Err(e) => { log::warn!("Terminal open failed: {}", e); self.send(OPEN_RESP, frame.session, &reply(1, &e)); }
|
||||
}
|
||||
@@ -289,6 +323,62 @@ mod tests {
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn force_redraw_shrinks_then_restores_latest_size() {
|
||||
let pty = nix::pty::openpty(None, None).unwrap();
|
||||
let session = Arc::new(Session { id: u32::MAX - 2, shell: "/bin/sh".into(), created_ms: now_ms(),
|
||||
master: pty.master, pid: 0, sink: Mutex::new(None),
|
||||
scrollback: Mutex::new(VecDeque::new()), size: Mutex::new((80, 24)) });
|
||||
let rows = |s: &Session| {
|
||||
let mut win = libc::winsize { ws_row: 0, ws_col: 0, ws_xpixel: 0, ws_ypixel: 0 };
|
||||
unsafe { libc::ioctl(s.master.as_raw_fd(), libc::TIOCGWINSZ as _, &mut win); }
|
||||
(win.ws_col, win.ws_row)
|
||||
};
|
||||
session.resize(100, 30);
|
||||
session.force_redraw();
|
||||
assert_eq!(rows(&session), (100, 29));
|
||||
session.resize(120, 40);
|
||||
std::thread::sleep(std::time::Duration::from_millis(400));
|
||||
assert_eq!(rows(&session), (120, 40), "restore must use the latest requested size");
|
||||
}
|
||||
#[test]
|
||||
fn reconnect_replays_large_history_in_bounded_frames() {
|
||||
let pty = nix::pty::openpty(None, None).unwrap();
|
||||
let id = u32::MAX - 1;
|
||||
let history: Vec<u8> = (0..SCROLLBACK).map(|n| (n % 251) as u8).collect();
|
||||
let session = Arc::new(Session { id, shell: "/bin/sh".into(), created_ms: now_ms(),
|
||||
master: pty.master, pid: 0, sink: Mutex::new(None),
|
||||
scrollback: Mutex::new(history.iter().copied().collect()), size: Mutex::new((80, 24)) });
|
||||
registry().lock().unwrap().insert(id, session.clone());
|
||||
struct Cleanup(u32);
|
||||
impl Drop for Cleanup { fn drop(&mut self) { registry().lock().unwrap().remove(&self.0); } }
|
||||
let _cleanup = Cleanup(id);
|
||||
|
||||
for _ in 0..2 {
|
||||
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
let conn = TerminalConn::new(tx);
|
||||
conn.handle(&encode(OPEN, id, br#"{"cols":80,"rows":24}"#));
|
||||
let opened = decode(&rx.try_recv().unwrap()).unwrap();
|
||||
assert_eq!(opened.kind, OPEN_RESP);
|
||||
assert_eq!(opened.session, id);
|
||||
let mut replay = Vec::new();
|
||||
while replay.len() < history.len() {
|
||||
let packet = rx.try_recv().unwrap();
|
||||
// SCTP's default outbound limit includes the TERM header.
|
||||
assert!(packet.len() <= 65536, "terminal replay exceeds the SCTP message limit");
|
||||
let frame = decode(&packet).unwrap();
|
||||
assert_eq!(frame.kind, STDOUT);
|
||||
assert_eq!(frame.session, id);
|
||||
replay.extend(frame.payload);
|
||||
}
|
||||
assert!(replay == history, "terminal replay must preserve every byte in order");
|
||||
assert!(session.emit(STDOUT, b"live output"));
|
||||
assert_eq!(decode(&rx.try_recv().unwrap()).unwrap().payload, b"live output");
|
||||
conn.handle(&encode(LIST_REQ, 7, &[]));
|
||||
assert_eq!(decode(&rx.try_recv().unwrap()).unwrap().kind, LIST_RESP);
|
||||
conn.handle(&encode(DETACH, id, &[]));
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn open_spawns_shell_and_streams_output() {
|
||||
for requested_id in [0, u32::MAX] {
|
||||
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
|
||||
+528
-95
@@ -1,14 +1,16 @@
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::process::Command;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::{mpsc, Semaphore, OwnedSemaphorePermit};
|
||||
use webrtc::media::Sample;
|
||||
use webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState;
|
||||
use webrtc::peer_connection::RTCPeerConnection;
|
||||
use webrtc::track::track_local::track_local_static_sample::TrackLocalStaticSample;
|
||||
use crate::capture_setting::VideoSettings;
|
||||
use crate::capture_setting::{VideoCodec, VideoSettings};
|
||||
|
||||
/// Default frame rate; the viewer's 帧率 setting overrides it per connection.
|
||||
pub const FPS: u64 = 60;
|
||||
@@ -28,27 +30,46 @@ fn gop_frames(fps: u32, in_process: bool) -> u64 {
|
||||
}
|
||||
const MAX_ACCESS_UNIT: usize = 8 * 1024 * 1024;
|
||||
|
||||
fn nal_offsets(bytes: &[u8], kind: u8) -> Vec<usize> {
|
||||
fn nal_offsets_for(bytes: &[u8], kind: u8, codec: VideoCodec) -> Vec<usize> {
|
||||
let mut result = Vec::new();
|
||||
let mut i = 0;
|
||||
while i + 3 < bytes.len() {
|
||||
let prefix = if bytes[i..].starts_with(&[0, 0, 0, 1]) { 4 }
|
||||
else if bytes[i..].starts_with(&[0, 0, 1]) { 3 } else { i += 1; continue };
|
||||
if i + prefix < bytes.len() && bytes[i + prefix] & 31 == kind { result.push(i); }
|
||||
let header = i + prefix;
|
||||
let matches = match codec {
|
||||
VideoCodec::H264 => header < bytes.len() && bytes[header] & 31 == kind,
|
||||
// HEVC has a two-byte header: forbidden_zero_bit=0, temporal_id_plus1 != 0.
|
||||
VideoCodec::H265 => header + 1 < bytes.len() && bytes[header] & 128 == 0
|
||||
&& bytes[header + 1] & 7 != 0 && (bytes[header] >> 1) & 63 == kind,
|
||||
};
|
||||
if matches { result.push(i); }
|
||||
i += prefix;
|
||||
}
|
||||
result
|
||||
}
|
||||
fn aud_offsets(bytes: &[u8]) -> Vec<usize> { nal_offsets(bytes, 9) }
|
||||
fn is_independent_frame(bytes: &[u8]) -> bool {
|
||||
!nal_offsets(bytes, 5).is_empty() && nal_offsets(bytes, 1).is_empty()
|
||||
fn aud_offsets_for(bytes: &[u8], codec: VideoCodec) -> Vec<usize> {
|
||||
nal_offsets_for(bytes, if codec == VideoCodec::H265 { 35 } else { 9 }, codec)
|
||||
}
|
||||
fn is_independent_frame_for(bytes: &[u8], codec: VideoCodec) -> bool {
|
||||
match codec {
|
||||
VideoCodec::H264 => !nal_offsets_for(bytes, 5, codec).is_empty() && nal_offsets_for(bytes, 1, codec).is_empty(),
|
||||
// BLA/IDR/CRA are IRAP (RFC 7798 §4.1); no leading pictures with our no-B-frame encoder.
|
||||
VideoCodec::H265 => (16..=21).any(|kind| !nal_offsets_for(bytes, kind, codec).is_empty())
|
||||
&& !(0..16).any(|kind| !nal_offsets_for(bytes, kind, codec).is_empty()),
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
fn aud_offsets(bytes: &[u8]) -> Vec<usize> { aud_offsets_for(bytes, VideoCodec::H264) }
|
||||
fn is_independent_frame(bytes: &[u8]) -> bool { is_independent_frame_for(bytes, VideoCodec::H264) }
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CompleteFrame {
|
||||
codec: VideoCodec,
|
||||
sequence: u64,
|
||||
completed_at: Instant,
|
||||
data: Vec<u8>,
|
||||
_permit: Option<OwnedSemaphorePermit>,
|
||||
}
|
||||
|
||||
const RTP_CLOCK: u64 = 90_000;
|
||||
@@ -63,11 +84,35 @@ impl Encoder {
|
||||
match value { Some("cpu") => Self::Cpu, Some("vaapi") => Self::Vaapi, Some("nvenc") => Self::Nvenc,
|
||||
_ => if nvidia { Self::Nvenc } else { Self::Cpu } }
|
||||
}
|
||||
pub(crate) fn name(self) -> &'static str { match self { Self::Cpu => "libx264", Self::Nvenc => "h264_nvenc", Self::Vaapi => "h264_vaapi" } }
|
||||
pub(crate) fn name_for(self, codec: VideoCodec) -> &'static str {
|
||||
match (self, codec) {
|
||||
(Self::Cpu, VideoCodec::H264) => "libx264", (Self::Cpu, VideoCodec::H265) => "libx265",
|
||||
(Self::Nvenc, VideoCodec::H264) => "h264_nvenc", (Self::Nvenc, VideoCodec::H265) => "hevc_nvenc",
|
||||
(Self::Vaapi, VideoCodec::H264) => "h264_vaapi", (Self::Vaapi, VideoCodec::H265) => "hevc_vaapi",
|
||||
}
|
||||
}
|
||||
}
|
||||
fn nvidia_present() -> bool {
|
||||
std::path::Path::new("/proc/driver/nvidia/version").exists() || std::path::Path::new("/dev/nvidia0").exists()
|
||||
}
|
||||
/// 真彩 (4:4:4) needs the FFmpeg process pipeline with NVENC or libx264 (h264_vaapi and the
|
||||
/// in-process encoder are 4:2:0 only).
|
||||
pub fn full_chroma_available() -> bool {
|
||||
#[cfg(feature = "inproc-av")]
|
||||
if crate::inproc_encoder::selected() { return false; }
|
||||
Encoder::requested() != Encoder::Vaapi
|
||||
}
|
||||
|
||||
/// (-profile:v, -pix_fmt) for NVENC / libx264 / libx265: 4:4:4 (真彩) needs a 4:4:4 profile.
|
||||
fn chroma_args(settings: &VideoSettings, encoder: Encoder) -> (&'static str, &'static str) {
|
||||
if settings.chroma != crate::capture_setting::CHROMA_444 { return (profile_for(settings.codec, false), "yuv420p"); }
|
||||
match (settings.codec, encoder) {
|
||||
(VideoCodec::H264, Encoder::Nvenc) => ("high444p", "yuv444p"),
|
||||
(VideoCodec::H264, _) => ("high444", "yuv444p"),
|
||||
(VideoCodec::H265, Encoder::Nvenc) => ("rext", "yuv444p"),
|
||||
(VideoCodec::H265, _) => ("main444-8", "yuv444p"),
|
||||
}
|
||||
}
|
||||
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.
|
||||
@@ -79,12 +124,187 @@ fn vaapi_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
|
||||
let (maxrate, buffer) = rate_args(settings, Encoder::Vaapi);
|
||||
// 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",
|
||||
["-c:v", Encoder::Vaapi.name_for(settings.codec), "-profile:v", profile_for(settings.codec, true), "-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()
|
||||
}
|
||||
|
||||
fn profile_for(codec: VideoCodec, vaapi: bool) -> &'static str {
|
||||
match codec { VideoCodec::H265 => "main", VideoCodec::H264 => if vaapi { "constrained_baseline" } else { "baseline" } }
|
||||
}
|
||||
pub(crate) fn x265_params(gop: &str) -> String {
|
||||
// Explicit no-B/no-lookahead avoids delayed frames; repeat parameter sets at each IRAP.
|
||||
format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}:open-gop=0:bframes=0:rc-lookahead=0:frame-threads=1:pools=4")
|
||||
}
|
||||
fn cpu_hevc_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
|
||||
let (maxrate, buffer) = rate_args(settings, Encoder::Cpu);
|
||||
let (profile, pix_fmt) = chroma_args(settings, Encoder::Cpu);
|
||||
let mut options = ["-c:v", "libx265", "-preset", "superfast", "-tune", "zerolatency", "-profile:v", profile,
|
||||
"-pix_fmt", pix_fmt, "-g", gop, "-bf", "0", "-threads", "4",
|
||||
"-maxrate", &maxrate, "-bufsize", &buffer, "-x265-params", &x265_params(gop)]
|
||||
.into_iter().map(str::to_owned).collect::<Vec<_>>();
|
||||
// Same as libx264: capped CRF starves repeated keyframes, so bitrate presets use ABR.
|
||||
match settings.max_kbps() {
|
||||
Some(k) => options.extend(["-b:v".into(), format!("{}k", k * 3 / 4)]),
|
||||
None => options.extend(["-crf".into(), settings.cq().to_string()]),
|
||||
}
|
||||
options
|
||||
}
|
||||
|
||||
fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str) -> Vec<String> {
|
||||
let (maxrate, bufsize) = rate_args(settings, encoder);
|
||||
let cq = settings.cq().to_string();
|
||||
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
|
||||
if encoder == Encoder::Vaapi { return vaapi_options(settings, gop); }
|
||||
let (profile, pix_fmt) = chroma_args(settings, encoder);
|
||||
if encoder == Encoder::Nvenc {
|
||||
return ["-c:v", encoder.name_for(settings.codec), "-preset", "p4", "-tune", "ull",
|
||||
"-profile:v", profile, "-pix_fmt", pix_fmt, "-g", gop, "-bf", "0",
|
||||
"-rc", "vbr", "-cq", &cq, "-b:v", &target, "-maxrate", &maxrate, "-bufsize", &bufsize,
|
||||
"-rc-lookahead", "0", "-zerolatency", "1", "-delay", "0", "-aud", "1"]
|
||||
.into_iter().map(str::to_owned).collect();
|
||||
}
|
||||
if settings.codec == VideoCodec::H265 { return cpu_hevc_options(settings, gop); }
|
||||
let params = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
|
||||
let mut options = vec!["-c:v", "libx264", "-preset", "superfast", "-tune", "zerolatency",
|
||||
"-profile:v", profile, "-pix_fmt", pix_fmt, "-g", gop, "-threads", "4",
|
||||
"-maxrate", &maxrate, "-bufsize", &bufsize, "-x264-params", ¶ms];
|
||||
// Capped CRF starves repeated 4K keyframes; use ABR for bitrate presets.
|
||||
if settings.max_kbps().is_some() { options.extend(["-b:v", &target]); }
|
||||
else { options.extend(["-crf", &cq]); }
|
||||
options.into_iter().map(str::to_owned).collect()
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
struct FfmpegSpec {
|
||||
program: PathBuf,
|
||||
ld_library_path: Option<OsString>,
|
||||
path: Option<OsString>,
|
||||
}
|
||||
|
||||
impl FfmpegSpec {
|
||||
fn command(&self) -> Command {
|
||||
let mut command = Command::new(&self.program);
|
||||
if let Some(path) = &self.ld_library_path { command.env("LD_LIBRARY_PATH", path); }
|
||||
if let Some(path) = &self.path { command.env("PATH", path); }
|
||||
command
|
||||
}
|
||||
}
|
||||
|
||||
fn ffmpeg_spec() -> FfmpegSpec {
|
||||
match crate::config::ffmpeg_root() {
|
||||
Some(root) => FfmpegSpec {
|
||||
program: root.join("bin/ffmpeg"),
|
||||
ld_library_path: Some(root.join("lib/x86_64-linux-gnu").into_os_string()),
|
||||
path: None,
|
||||
},
|
||||
None => FfmpegSpec {
|
||||
program: PathBuf::from("ffmpeg"),
|
||||
ld_library_path: None,
|
||||
path: std::env::var_os("PATH"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn ffmpeg_command() -> Command { ffmpeg_spec().command() }
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum FfmpegTimingMode { Modern, Legacy }
|
||||
|
||||
fn help_has_option(help: &str, option: &str) -> bool {
|
||||
help.lines().filter_map(|line| line.split_whitespace().next()).any(|token| {
|
||||
token == option || token.starts_with(&format!("{option}:") ) || token.starts_with(&format!("{option}["))
|
||||
})
|
||||
}
|
||||
|
||||
fn timing_mode_from_help(help: &str) -> FfmpegTimingMode {
|
||||
if help_has_option(help, "-fps_mode") && help_has_option(help, "-enc_time_base") {
|
||||
FfmpegTimingMode::Modern
|
||||
} else {
|
||||
FfmpegTimingMode::Legacy
|
||||
}
|
||||
}
|
||||
|
||||
fn timing_args(mode: FfmpegTimingMode, fps: u32) -> Vec<String> {
|
||||
match mode {
|
||||
FfmpegTimingMode::Modern => vec!["-fps_mode".into(), "vfr".into(), "-enc_time_base".into(), format!("1/{fps}")],
|
||||
FfmpegTimingMode::Legacy => vec!["-vsync".into(), "0".into()],
|
||||
}
|
||||
}
|
||||
|
||||
/// Legacy is chosen only from a completed help dump that visibly lacks the
|
||||
/// modern options. A failed probe (spawn error, non-zero exit, timeout, or no
|
||||
/// parseable help text) must keep the modern args: `-vsync` is deprecated
|
||||
/// since FFmpeg 5.1 and may not be accepted by newer builds, so falling back
|
||||
/// to it on a failed probe would kill the encoder there — worse than guessing.
|
||||
fn timing_mode_from_probe(success: bool, stdout: &[u8], stderr: &[u8]) -> FfmpegTimingMode {
|
||||
let mut help = Vec::with_capacity(stdout.len() + stderr.len());
|
||||
help.extend_from_slice(stdout);
|
||||
help.extend_from_slice(stderr);
|
||||
if !success || help.is_empty() { return FfmpegTimingMode::Modern; }
|
||||
timing_mode_from_help(&String::from_utf8_lossy(&help))
|
||||
}
|
||||
|
||||
async fn probe_timing_mode(spec: &FfmpegSpec) -> FfmpegTimingMode {
|
||||
let mut command = spec.command();
|
||||
command.args(["-hide_banner", "-nostdin", "-h", "full"])
|
||||
.kill_on_drop(true)
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped());
|
||||
match tokio::time::timeout(Duration::from_secs(3), command.output()).await {
|
||||
Ok(Ok(output)) => timing_mode_from_probe(output.status.success(), &output.stdout, &output.stderr),
|
||||
_ => timing_mode_from_probe(false, &[], &[]),
|
||||
}
|
||||
}
|
||||
|
||||
fn timing_cache() -> &'static Mutex<HashMap<FfmpegSpec, Arc<tokio::sync::OnceCell<FfmpegTimingMode>>>> {
|
||||
static CACHE: OnceLock<Mutex<HashMap<FfmpegSpec, Arc<tokio::sync::OnceCell<FfmpegTimingMode>>>>> = OnceLock::new();
|
||||
CACHE.get_or_init(|| Mutex::new(HashMap::new()))
|
||||
}
|
||||
|
||||
fn timing_cell(
|
||||
cache: &Mutex<HashMap<FfmpegSpec, Arc<tokio::sync::OnceCell<FfmpegTimingMode>>>>,
|
||||
spec: &FfmpegSpec,
|
||||
) -> Arc<tokio::sync::OnceCell<FfmpegTimingMode>> {
|
||||
let mut cache = cache.lock().expect("FFmpeg timing cache poisoned");
|
||||
cache.entry(spec.clone()).or_insert_with(|| Arc::new(tokio::sync::OnceCell::const_new())).clone()
|
||||
}
|
||||
|
||||
async fn ffmpeg_timing_mode(spec: &FfmpegSpec) -> FfmpegTimingMode {
|
||||
let cell = timing_cell(timing_cache(), spec);
|
||||
*cell.get_or_init(|| probe_timing_mode(spec)).await
|
||||
}
|
||||
|
||||
/// A compiled-in encoder name does not prove that its driver can initialize.
|
||||
/// Probe one synthetic 4:2:0 frame once per process before advertising HEVC.
|
||||
pub async fn hevc_available() -> bool {
|
||||
static AVAILABLE: tokio::sync::OnceCell<bool> = tokio::sync::OnceCell::const_new();
|
||||
*AVAILABLE.get_or_init(|| async {
|
||||
let requested = Encoder::requested();
|
||||
let mut candidates = vec![requested];
|
||||
if requested != Encoder::Cpu { candidates.push(Encoder::Cpu); }
|
||||
for encoder in candidates {
|
||||
let settings = VideoSettings { codec: VideoCodec::H265, ..Default::default() };
|
||||
let mut command = ffmpeg_command();
|
||||
command.args(["-hide_banner", "-loglevel", "error", "-nostdin"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
command.args(["-f", "lavfi", "-i", "color=black:size=256x256:rate=1", "-frames:v", "1", "-an"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vf", &vaapi_filter(None)]); }
|
||||
command.args(encoder_options(&settings, encoder, "30"));
|
||||
command.args(["-f", "hevc", "pipe:1"]).kill_on_drop(true).stderr(std::process::Stdio::null());
|
||||
if let Ok(Ok(output)) = tokio::time::timeout(Duration::from_secs(5), command.output()).await {
|
||||
if output.status.success() && is_independent_frame_for(&output.stdout, VideoCodec::H265) {
|
||||
log::info!("HEVC encoder probe: {} ok", encoder.name_for(VideoCodec::H265));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
log::info!("HEVC encoder probe: unavailable");
|
||||
false
|
||||
}).await
|
||||
}
|
||||
|
||||
/// Round UP (+1ns float guard) so webrtc's floor(duration*90000) yields exactly `ticks`.
|
||||
fn ticks_to_duration(ticks: u64) -> Duration {
|
||||
Duration::from_nanos((ticks * 1_000_000_000 + RTP_CLOCK - 1) / RTP_CLOCK + 1)
|
||||
@@ -115,7 +335,7 @@ struct DependencyGate {needs_idr:bool}
|
||||
impl Default for DependencyGate {fn default()->Self{Self{needs_idr:true}}}
|
||||
impl DependencyGate{
|
||||
fn submit(&mut self,tx:&mpsc::Sender<Arc<CompleteFrame>>,frame:Arc<CompleteFrame>){
|
||||
if self.needs_idr&&!is_independent_frame(&frame.data){return;}
|
||||
if self.needs_idr&&!is_independent_frame_for(&frame.data, frame.codec){return;}
|
||||
match tx.try_send(frame){
|
||||
Ok(())=>self.needs_idr=false,
|
||||
Err(mpsc::error::TrySendError::Full(_))=>self.needs_idr=true,
|
||||
@@ -145,16 +365,16 @@ impl Drop for StopOnDrop { fn drop(&mut self) { self.0.store(true, std::sync::at
|
||||
async fn read_encoder(
|
||||
size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||
latest: mpsc::Sender<Arc<CompleteFrame>>,
|
||||
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
|
||||
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
|
||||
) -> Result<(), String> {
|
||||
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct).
|
||||
let encoder = Encoder::requested();
|
||||
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone()).await {
|
||||
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr,budget).await; }
|
||||
match read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,sequence.clone(),settings,idr.clone(),budget.clone()).await {
|
||||
Ok(())=>Ok(()),
|
||||
Err(error) if !latest.is_closed()=>{
|
||||
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name(),error);
|
||||
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr).await
|
||||
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name_for(settings.codec),error);
|
||||
read_encoder_with(size,display,portal,latest,Encoder::Cpu,sequence,settings,idr,budget).await
|
||||
},
|
||||
Err(error)=>Err(error),
|
||||
}
|
||||
@@ -164,20 +384,21 @@ async fn read_encoder(
|
||||
#[cfg(feature = "inproc-av")]
|
||||
async fn read_inprocess(
|
||||
config: crate::inproc_encoder::Config, latest: mpsc::Sender<Arc<CompleteFrame>>,
|
||||
sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>,
|
||||
sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
|
||||
) -> Result<(), String> {
|
||||
let (units_tx, mut units) = mpsc::channel::<Vec<u8>>(4);
|
||||
let codec = config.codec;
|
||||
let (units_tx, mut units) = mpsc::channel::<crate::inproc_encoder::EncodedUnit>(4);
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
// If this future is dropped (reader task aborted), the blocking encoder stops at its next packet.
|
||||
let _stop_on_drop = StopOnDrop(stop.clone());
|
||||
let job = tokio::task::spawn_blocking({
|
||||
let stop = stop.clone();
|
||||
move || crate::inproc_encoder::run(config, units_tx, stop, idr)
|
||||
move || crate::inproc_encoder::run(config, units_tx, stop, idr, budget)
|
||||
});
|
||||
let mut gate = DependencyGate::default();
|
||||
while let Some(data) = units.recv().await {
|
||||
while let Some(unit) = units.recv().await {
|
||||
let sequence = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
|
||||
gate.submit(&latest, Arc::new(CompleteFrame { sequence, completed_at: Instant::now(), data }));
|
||||
gate.submit(&latest, Arc::new(CompleteFrame { codec, sequence, completed_at: unit.completed_at, data: unit.data, _permit: Some(unit.permit) }));
|
||||
if latest.is_closed() { break; }
|
||||
}
|
||||
// Dropping the receiver also unblocks the encoder if it is waiting to send.
|
||||
@@ -195,17 +416,24 @@ fn buffer_kbits(max_kbps: u32, encoder: Encoder) -> u32 {
|
||||
if encoder == Encoder::Cpu { max_kbps.clamp(500, 8000) } else { (max_kbps / 8).clamp(100, 500) }
|
||||
}
|
||||
|
||||
fn buffer_for(settings: &VideoSettings, encoder: Encoder) -> u32 {
|
||||
if settings.codec == VideoCodec::H265 && encoder == Encoder::Cpu {
|
||||
settings.max_kbps().unwrap_or(30_000) // one second of HEVC bitrate, including high presets
|
||||
} else { buffer_kbits(settings.max_kbps().unwrap_or(30_000), encoder) }
|
||||
}
|
||||
|
||||
fn rate_args(settings: &VideoSettings, encoder: Encoder) -> (String, String) {
|
||||
let max_kbps = settings.max_kbps().unwrap_or(30_000);
|
||||
(format!("{max_kbps}k"), format!("{}k", buffer_kbits(max_kbps, encoder)))
|
||||
(format!("{max_kbps}k"), format!("{}k", buffer_for(settings, encoder)))
|
||||
}
|
||||
|
||||
async fn read_encoder_with(
|
||||
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,sequence:Arc<std::sync::atomic::AtomicU64>,
|
||||
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>,
|
||||
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
|
||||
)->Result<(),String>{
|
||||
let kms=display.starts_with("kms:");
|
||||
let wayland=kms || display.starts_with("wayland:");
|
||||
let (permits_tx,mut permits_rx)=mpsc::channel::<OwnedSemaphorePermit>(4);
|
||||
let kms_capture=if kms {
|
||||
let capture=tokio::task::spawn_blocking(crate::kms::Capture::open).await.map_err(|_|"KMS worker failed")??;
|
||||
if (capture.width,capture.height)!=size { return Err("KMS mode changed; restart host to refresh desktop geometry".into()); }
|
||||
@@ -218,26 +446,23 @@ 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: i64::from(buffer_kbits(max_kbps as u32, encoder)) * 1000, encoder,
|
||||
max_bps: max_kbps * 1000, buffer_bits: i64::from(buffer_for(&settings, encoder)) * 1000, encoder, codec: settings.codec,
|
||||
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,encoder.name());
|
||||
return read_inprocess(config, latest, sequence, idr).await;
|
||||
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name_for(settings.codec));
|
||||
return read_inprocess(config, latest, sequence, idr, budget).await;
|
||||
}
|
||||
let size = format!("{}x{}", size.0, size.1);
|
||||
let fps = settings.fps.to_string();
|
||||
// Keep the IDR interval at 2 s whatever the frame rate.
|
||||
let gop = gop_frames(settings.fps, false).to_string();
|
||||
let (maxrate, bufsize) = rate_args(&settings, encoder);
|
||||
let (maxrate, _) = rate_args(&settings, encoder);
|
||||
let cq = settings.cq().to_string();
|
||||
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name());
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} pix_fmt={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name_for(settings.codec),if encoder == Encoder::Vaapi { "nv12" } else { chroma_args(&settings, encoder).1 });
|
||||
// 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"),
|
||||
};
|
||||
let ffmpeg = ffmpeg_spec();
|
||||
let mut command = ffmpeg.command();
|
||||
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
if wayland {
|
||||
@@ -254,27 +479,35 @@ async fn read_encoder_with(
|
||||
} else if let Some((width, height)) = settings.output {
|
||||
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
|
||||
}
|
||||
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,
|
||||
"-rc-lookahead","0","-zerolatency","1","-delay","0","-aud","1"]);
|
||||
}else{
|
||||
let x264 = format!("aud=1:repeat-headers=1:scenecut=0:keyint={gop}:min-keyint={gop}");
|
||||
command.args([
|
||||
"-c:v", "libx264", "-preset", "superfast", "-tune", "zerolatency",
|
||||
"-profile:v", "baseline", "-pix_fmt", "yuv420p", "-g", &gop, "-threads", "4",
|
||||
"-crf", &cq, "-maxrate", &maxrate, "-bufsize", &bufsize,
|
||||
"-x264-params", &x264]);
|
||||
}
|
||||
command.args(encoder_options(&settings, encoder, &gop));
|
||||
// x11grab paces against an absolute schedule; after a capture stall it grabs
|
||||
// back-to-back to "catch up" (measured 69 frames in 200ms). vfr on a 1/FPS
|
||||
// timebase drops those same-slot frames instead of encoding a burst.
|
||||
let time_base = format!("1/{fps}");
|
||||
if wayland { command.stdin(std::process::Stdio::piped()); }
|
||||
let mut child=command.args(["-fps_mode","vfr","-enc_time_base",&time_base,"-f","h264","pipe:1"])
|
||||
// timebase drops those same-slot frames instead of encoding a burst. The
|
||||
// legacy -vsync 0 path (FFmpeg 4.x) passes catch-up frames straight through
|
||||
// and encodes the whole burst; a known degradation on old FFmpeg.
|
||||
// Native Wayland: the capture process writes straight into the encoder's stdin;
|
||||
// relaying 33 MB 4K frames through this process cost a full extra copy per frame.
|
||||
let mut native_capture=None;
|
||||
if wayland && kms_capture.is_none() && portal.is_none() {
|
||||
// Linux keeps the running executable inode accessible after a scoped
|
||||
// development/release replacement; current_exe may name a deleted path.
|
||||
#[cfg(target_os="linux")]
|
||||
let executable=std::path::PathBuf::from("/proc/self/exe");
|
||||
#[cfg(not(target_os="linux"))]
|
||||
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
|
||||
let mut capture=wayland_capture_command(&executable,settings.fps)
|
||||
.env("UU_WAYLAND_DEMAND","1")
|
||||
.kill_on_drop(true).stdin(std::process::Stdio::piped()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
|
||||
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
|
||||
let source:std::process::Stdio=capture.stdout.take().ok_or("Capture stdout missing")?
|
||||
.try_into().map_err(|e|format!("Capture pipe: {e}"))?;
|
||||
command.stdin(source);
|
||||
native_capture=Some(capture);
|
||||
} else if wayland { command.stdin(std::process::Stdio::piped()); }
|
||||
let timing = timing_args(ffmpeg_timing_mode(&ffmpeg).await, settings.fps);
|
||||
let mut output_args = timing;
|
||||
output_args.extend(["-f".into(), if settings.codec == VideoCodec::H265 { "hevc".into() } else { "h264".into() }, "pipe:1".into()]);
|
||||
let mut child=command.args(output_args)
|
||||
.kill_on_drop(true)
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::inherit())
|
||||
@@ -286,11 +519,13 @@ async fn read_encoder_with(
|
||||
use tokio::io::AsyncWriteExt;
|
||||
let mut tick=tokio::time::interval(Duration::from_nanos(1_000_000_000/u64::from(settings.fps)));
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
|
||||
loop {
|
||||
tick.tick().await;
|
||||
let permit=budget.clone().acquire_owned().await.map_err(|_|"Encoder stopped")?;
|
||||
permits_tx.send(permit).await.map_err(|_|"Encoder stopped")?;
|
||||
let capture=capture.clone();
|
||||
let bytes=tokio::task::spawn_blocking(move || {
|
||||
let mut bytes=vec![0;capture.width as usize*capture.height as usize*4];
|
||||
bytes=tokio::task::spawn_blocking(move || {
|
||||
capture.capture(&mut bytes)?; Ok::<_,String>(bytes)
|
||||
}).await.map_err(|_|"KMS worker failed")??;
|
||||
sink.write_all(&bytes).await.map_err(|_|"KMS encoder pipe failed")?;
|
||||
@@ -303,33 +538,42 @@ async fn read_encoder_with(
|
||||
let interval=Duration::from_nanos(1_000_000_000/u64::from(settings.fps));
|
||||
let mut tick=tokio::time::interval(interval);
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
let mut frame=vec![0;(session.width as usize)*(session.height as usize)*4];
|
||||
let mut version=None;
|
||||
loop {
|
||||
tick.tick().await;
|
||||
let permit=budget.clone().acquire_owned().await.map_err(|_|"Encoder stopped")?;
|
||||
permits_tx.send(permit).await.map_err(|_|"Encoder stopped")?;
|
||||
let session=session.clone();
|
||||
let frame=tokio::task::spawn_blocking(move || {
|
||||
let mut frame=vec![0;(session.width as usize)*(session.height as usize)*4];
|
||||
session.capture(&mut frame)?;
|
||||
Ok::<_,String>(frame)
|
||||
(frame,version)=tokio::task::spawn_blocking(move || {
|
||||
session.capture_changed(&mut frame,&mut version)?;
|
||||
Ok::<_,String>((frame,version))
|
||||
}).await.map_err(|e|format!("Portal capture worker: {e}"))??;
|
||||
sink.write_all(&frame).await.map_err(|e|format!("Portal encoder pipe: {e}"))?;
|
||||
}
|
||||
})))
|
||||
} else if wayland {
|
||||
// Linux keeps the running executable inode accessible after a scoped
|
||||
// development/release replacement; current_exe may name a deleted path.
|
||||
#[cfg(target_os="linux")]
|
||||
let executable=std::path::PathBuf::from("/proc/self/exe");
|
||||
#[cfg(not(target_os="linux"))]
|
||||
let executable=std::env::current_exe().map_err(|e|format!("Capture executable: {e}"))?;
|
||||
let mut capture=wayland_capture_command(&executable,settings.fps)
|
||||
.kill_on_drop(true).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::inherit())
|
||||
.spawn().map_err(|e|format!("Start Wayland capture: {e}"))?;
|
||||
let mut source=capture.stdout.take().ok_or("Capture stdout missing")?;
|
||||
let mut sink=child.stdin.take().ok_or("Encoder stdin missing")?;
|
||||
} else if let Some(mut capture)=native_capture {
|
||||
let mut demand=capture.stdin.take().ok_or("Capture stdin missing")?;
|
||||
Some(EncoderTask(tokio::spawn(async move {
|
||||
let result=tokio::io::copy(&mut source,&mut sink).await;
|
||||
drop(capture);
|
||||
result.map(|_|()).map_err(|e|format!("Wayland frame pipe: {e}"))
|
||||
use tokio::io::AsyncWriteExt;
|
||||
tokio::select! {
|
||||
result=async {
|
||||
let mut tick=tokio::time::interval(Duration::from_nanos(1_000_000_000/u64::from(settings.fps)));
|
||||
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
loop {
|
||||
let permit=budget.clone().acquire_owned().await.map_err(|_|"Encoder stopped")?;
|
||||
tick.tick().await;
|
||||
permits_tx.send(permit).await.map_err(|_|"Encoder stopped")?;
|
||||
demand.write_all(&[1]).await.map_err(|_|"Capture demand pipe closed")?;
|
||||
}
|
||||
} => result,
|
||||
status=capture.wait() => {
|
||||
let status=status.map_err(|e|format!("Wayland capture: {e}"))?;
|
||||
// Nothing awaits this task; log so the cause survives the generic encoder EOF.
|
||||
log::warn!("Wayland capture exited: {status}");
|
||||
Err(format!("Wayland capture exited: {status}"))
|
||||
},
|
||||
}
|
||||
})))
|
||||
} else { None };
|
||||
let mut stdout = child.stdout.take().ok_or("Encoder stdout missing")?;
|
||||
@@ -342,14 +586,15 @@ async fn read_encoder_with(
|
||||
pending.extend_from_slice(&chunk[..count]);
|
||||
// A chunk can include the next AUD, so split before enforcing the bound.
|
||||
loop {
|
||||
let aud = aud_offsets(&pending);
|
||||
let aud = aud_offsets_for(&pending, settings.codec);
|
||||
if aud.len() < 2 { break; }
|
||||
let next = aud[1];
|
||||
if next > MAX_ACCESS_UNIT { return Err("Encoder access unit exceeds limit".into()); }
|
||||
let frame: Vec<u8> = pending.drain(..next).collect();
|
||||
let sequence = sequence.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
|
||||
let permit=if wayland {Some(permits_rx.recv().await.ok_or("Capture producer stopped")?)} else {None};
|
||||
gate.submit(&latest, Arc::new(CompleteFrame {
|
||||
sequence, completed_at: Instant::now(), data: frame,
|
||||
codec: settings.codec, sequence, completed_at: Instant::now(), data: frame, _permit: permit,
|
||||
}));
|
||||
if latest.is_closed() { return Ok(()); }
|
||||
}
|
||||
@@ -378,8 +623,14 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
|
||||
let (tx,mut rx)=mpsc::channel(4);
|
||||
let sequence=Arc::new(std::sync::atomic::AtomicU64::new(0));
|
||||
let fps=crate::wayland::capture_fps(std::env::var("UU_PROBE_FPS").ok().as_deref())?;
|
||||
let settings=VideoSettings {fps,..VideoSettings::default()};
|
||||
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)))));
|
||||
// UU_PROBE_QUALITY selects a viewer preset (1 fast 1M, 2 general 2M, 3 HD 8M, 4 bluray 20M,
|
||||
// 5 auto); bitrate presets exercise the capped/ABR encoder path instead of CRF.
|
||||
let quality=match std::env::var("UU_PROBE_QUALITY").ok().as_deref() {
|
||||
None=>crate::capture_setting::QUALITY_AUTO,
|
||||
Some(v)=>v.parse::<u64>().ok().filter(|q|(1..=5).contains(q)).ok_or("UU_PROBE_QUALITY must be 1-5")?,
|
||||
};
|
||||
let settings=VideoSettings {fps,quality,..VideoSettings::default()};
|
||||
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)),Arc::new(Semaphore::new(4)))));
|
||||
let mut count=0;
|
||||
let mut bytes=0;
|
||||
// UU_PROBE_FRAMES lengthens the sample for baseline measurements; default is 5 frames.
|
||||
@@ -397,8 +648,8 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalStaticSample>, size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||
mut settings: tokio::sync::watch::Receiver<VideoSettings>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_track::VideoTrack>, size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||
mut settings: tokio::sync::watch::Receiver<VideoSettings>, applied: Arc<tokio::sync::watch::Sender<VideoSettings>>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
loop {
|
||||
if transport_closed(&pc) { return Ok(()); }
|
||||
if pc.connection_state() == RTCPeerConnectionState::Connected
|
||||
@@ -407,9 +658,15 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
|
||||
}
|
||||
crate::capture::keep_display_awake()?;
|
||||
let (latest, mut receiver) = mpsc::channel(4);
|
||||
let budget=Arc::new(Semaphore::new(4));
|
||||
let sequence = Arc::new(std::sync::atomic::AtomicU64::new(0));
|
||||
let current = *settings.borrow_and_update();
|
||||
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone())));
|
||||
let mut current = *settings.borrow_and_update();
|
||||
if !track.supports(current.codec) && track.supports(VideoCodec::H265) {
|
||||
current.codec = VideoCodec::H265; // H265-only remote video offer.
|
||||
applied.send_replace(current);
|
||||
settings.borrow_and_update();
|
||||
}
|
||||
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone())));
|
||||
let mut previous = None;
|
||||
let mut timeline = RtpTimeline { fps: u64::from(current.fps), ..Default::default() };
|
||||
let mut settings_open = true;
|
||||
@@ -420,21 +677,47 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
|
||||
let mut health = tokio::time::interval(Duration::from_millis(200));
|
||||
health.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
let mut stats = Instant::now();
|
||||
loop {
|
||||
// sent/skipped are cumulative; window_* and max_* cover the interval since the last stats line.
|
||||
let (mut window_frames, mut window_skipped) = (0u64, 0u64);
|
||||
let mut send_interrupted=false;
|
||||
'stream: loop {
|
||||
let next = *settings.borrow_and_update();
|
||||
if next != current || send_interrupted {
|
||||
let discard = next.codec != current.codec || send_interrupted;
|
||||
current = next;
|
||||
timeline.fps = u64::from(next.fps);
|
||||
if discard {
|
||||
reader.0.abort();
|
||||
let _ = (&mut reader.0).await;
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone()));
|
||||
} else {
|
||||
reader.restart(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone())).await;
|
||||
}
|
||||
send_interrupted=false;
|
||||
}
|
||||
tokio::select! {
|
||||
result = &mut reader.0 => return result.map_err(|e| format!("Encoder reader task: {e}"))?,
|
||||
result = &mut reader.0 => {
|
||||
let result = result.map_err(|e| format!("Encoder reader task: {e}"))?;
|
||||
if let (Err(error), VideoCodec::H265, true) = (&result, current.codec, track.supports(VideoCodec::H264)) {
|
||||
log::warn!("HEVC stream failed ({error}); falling back to negotiated H264");
|
||||
applied.send_modify(|value| value.codec = VideoCodec::H264);
|
||||
current = *settings.borrow_and_update();
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone()));
|
||||
continue;
|
||||
}
|
||||
return result;
|
||||
},
|
||||
_ = health.tick() => { if transport_closed(&pc) { return Ok(()); } },
|
||||
changed = settings.changed(), if settings_open => {
|
||||
if changed.is_err() { settings_open = false; continue; }
|
||||
// Restart the encoder with the viewer's new 帧率/画质; it begins with an IDR, and frames
|
||||
// still queued from the old encoder remain a valid chain before it.
|
||||
let next = *settings.borrow_and_update();
|
||||
timeline.fps = u64::from(next.fps);
|
||||
reader.restart(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone())).await;
|
||||
if changed.is_err() { settings_open = false; }
|
||||
continue;
|
||||
},
|
||||
frame = receiver.recv() => {
|
||||
// A closed queue means the reader ended; its JoinHandle carries the reason.
|
||||
let Some(frame) = frame else { return (&mut reader.0).await.map_err(|e| format!("Encoder reader task: {e}"))?; };
|
||||
if frame.codec != current.codec { continue; }
|
||||
let duration = timeline.duration_after(frame.completed_at);
|
||||
if let Some(last) = previous { skipped += frame.sequence.saturating_sub(last + 1); }
|
||||
max_age_ms = max_age_ms.max(frame.completed_at.elapsed().as_millis());
|
||||
@@ -443,13 +726,17 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
|
||||
data: frame.data.clone().into(), duration, ..Default::default()
|
||||
};
|
||||
let extensions = [crate::rtp_metadata::capture_index(0), crate::rtp_metadata::new_frame()];
|
||||
let send = track.write_sample_with_extensions(&sample, &extensions);
|
||||
let send = track.write_sample_with_extensions(frame.codec, &sample, &extensions);
|
||||
let send_started=Instant::now();
|
||||
tokio::pin!(send);
|
||||
loop {
|
||||
tokio::select! {
|
||||
result = &mut send => { result.map_err(|e| format!("Video send: {e}"))?; break; },
|
||||
_ = health.tick() => { if transport_closed(&pc) { return Ok(()); } },
|
||||
changed = settings.changed(), if settings_open => {
|
||||
if changed.is_err() { settings_open=false; }
|
||||
else if *settings.borrow()!=current { send_interrupted=true; continue 'stream; }
|
||||
},
|
||||
}
|
||||
}
|
||||
previous = Some(frame.sequence);
|
||||
@@ -457,7 +744,9 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<TrackLocalSta
|
||||
frames += 1;
|
||||
if stats.elapsed() >= Duration::from_secs(30) {
|
||||
crate::capture::keep_display_awake()?;
|
||||
log::info!("Desktop latest-frame stats: sent={} skipped={} latest_seq={} max_slot_age_ms={} max_send_ms={}", frames, skipped, frame.sequence, max_age_ms,max_send_ms);
|
||||
log::info!("Desktop latest-frame stats: sent={} skipped={} latest_seq={} window_s={} window_sent={} window_skipped={} max_slot_age_ms={} max_send_ms={}",
|
||||
frames, skipped, frame.sequence, stats.elapsed().as_secs(), frames - window_frames, skipped - window_skipped, max_age_ms, max_send_ms);
|
||||
(window_frames, window_skipped) = (frames, skipped);
|
||||
stats = Instant::now();
|
||||
max_age_ms = 0;
|
||||
max_send_ms = 0;
|
||||
@@ -478,6 +767,96 @@ mod tests {
|
||||
}
|
||||
}
|
||||
use super::*;
|
||||
#[tokio::test]
|
||||
async fn frame_budget_spans_queued_sending_and_restarted_encoders() {
|
||||
let budget=Arc::new(Semaphore::new(2));
|
||||
let (tx,mut rx)=mpsc::channel(4);
|
||||
let mut gate=DependencyGate::default();
|
||||
for sequence in 1..=2 {
|
||||
let permit=budget.clone().acquire_owned().await.unwrap();
|
||||
gate.submit(&tx,Arc::new(CompleteFrame {codec:VideoCodec::H264,sequence,completed_at:Instant::now(),
|
||||
data:vec![0,0,1,5,42],_permit:Some(permit)}));
|
||||
}
|
||||
let sending=rx.recv().await.unwrap();
|
||||
assert_eq!(budget.available_permits(),0);
|
||||
let restarted=tokio::spawn(budget.clone().acquire_owned());
|
||||
tokio::task::yield_now().await;
|
||||
assert!(!restarted.is_finished());
|
||||
drop(sending);
|
||||
let permit=tokio::time::timeout(Duration::from_secs(1),restarted).await.unwrap().unwrap().unwrap();
|
||||
assert_eq!(budget.available_permits(),0);
|
||||
drop(rx);
|
||||
assert_eq!(budget.available_permits(),1);
|
||||
drop(permit);
|
||||
assert_eq!(budget.available_permits(),2);
|
||||
}
|
||||
#[test]
|
||||
fn timing_help_requires_both_modern_options() {
|
||||
let modern = " -fps_mode mode video sync method\n -enc_time_base ratio set encoder timebase";
|
||||
assert_eq!(timing_mode_from_help(modern), FfmpegTimingMode::Modern);
|
||||
assert_eq!(timing_mode_from_help(" -vsync parameter\n"), FfmpegTimingMode::Legacy);
|
||||
assert_eq!(timing_mode_from_help(" -fps_mode mode\n"), FfmpegTimingMode::Legacy);
|
||||
assert_eq!(timing_mode_from_help(" -enc_time_base ratio\n"), FfmpegTimingMode::Legacy);
|
||||
assert_eq!(timing_mode_from_help(" --not-fps_mode-test\n"), FfmpegTimingMode::Legacy);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timing_args_match_ffmpeg_generation() {
|
||||
assert_eq!(timing_args(FfmpegTimingMode::Modern, 60),
|
||||
vec!["-fps_mode", "vfr", "-enc_time_base", "1/60"]);
|
||||
assert_eq!(timing_args(FfmpegTimingMode::Legacy, 30), vec!["-vsync", "0"]);
|
||||
assert!(!timing_args(FfmpegTimingMode::Modern, 60).contains(&"-r".to_owned()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timing_probe_failure_keeps_modern_args() {
|
||||
let modern = b" -fps_mode mode\n -enc_time_base ratio\n";
|
||||
// Legacy is only confirmed from a completed help dump that lacks the options.
|
||||
assert_eq!(timing_mode_from_probe(true, b"", b" -fps_mode mode\n"), FfmpegTimingMode::Legacy);
|
||||
// Any probe failure keeps the modern args (-vsync is deprecated on new FFmpeg).
|
||||
assert_eq!(timing_mode_from_probe(false, modern, &[]), FfmpegTimingMode::Modern);
|
||||
assert_eq!(timing_mode_from_probe(false, &[], &[]), FfmpegTimingMode::Modern);
|
||||
assert_eq!(timing_mode_from_probe(true, &[], &[]), FfmpegTimingMode::Modern);
|
||||
assert_eq!(timing_mode_from_probe(true, modern, &[]), FfmpegTimingMode::Modern);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn timing_cache_initializer_runs_once_for_concurrent_requests() {
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
let cell = Arc::new(tokio::sync::OnceCell::new());
|
||||
let calls = Arc::new(AtomicUsize::new(0));
|
||||
let first = cell.clone();
|
||||
let first_calls = calls.clone();
|
||||
let second = cell.clone();
|
||||
let second_calls = calls.clone();
|
||||
let (first_mode, second_mode) = tokio::join!(
|
||||
async move { *first.get_or_init(|| async {
|
||||
first_calls.fetch_add(1, Ordering::SeqCst);
|
||||
tokio::task::yield_now().await;
|
||||
FfmpegTimingMode::Legacy
|
||||
}).await },
|
||||
async move { *second.get_or_init(|| async {
|
||||
second_calls.fetch_add(1, Ordering::SeqCst);
|
||||
tokio::task::yield_now().await;
|
||||
FfmpegTimingMode::Modern
|
||||
}).await },
|
||||
);
|
||||
assert_eq!(first_mode, second_mode);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ffmpeg_specs_separate_executable_environments() {
|
||||
let path = FfmpegSpec { program: PathBuf::from("ffmpeg"), ld_library_path: None,
|
||||
path: Some(OsString::from("/one")) };
|
||||
let other_path = FfmpegSpec { program: PathBuf::from("ffmpeg"), ld_library_path: None,
|
||||
path: Some(OsString::from("/two")) };
|
||||
let bundled = FfmpegSpec { program: PathBuf::from("/opt/ffmpeg/bin/ffmpeg"),
|
||||
ld_library_path: Some(OsString::from("/opt/ffmpeg/lib/x86_64-linux-gnu")), path: None };
|
||||
assert_ne!(path, other_path);
|
||||
assert_ne!(path, bundled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn x264_buffer_fits_keyframes() {
|
||||
assert_eq!(buffer_kbits(2000, Encoder::Nvenc), 250);
|
||||
@@ -515,8 +894,8 @@ mod tests {
|
||||
let (tx, mut rx) = mpsc::channel(4);
|
||||
let old_tx = tx.clone();
|
||||
let (started_tx, started_rx) = tokio::sync::oneshot::channel();
|
||||
let frame = |sequence, idr| Arc::new(CompleteFrame {
|
||||
sequence, completed_at: Instant::now(),
|
||||
let frame = |sequence, idr| Arc::new(CompleteFrame { _permit: None,
|
||||
sequence, codec: VideoCodec::H264, completed_at: Instant::now(),
|
||||
data: vec![0, 0, 1, 9, 240, 0, 0, 1, if idr { 5 } else { 1 }, 42],
|
||||
});
|
||||
let mut reader = EncoderTask(tokio::spawn(async move {
|
||||
@@ -610,7 +989,7 @@ mod tests {
|
||||
async fn bounded_queue_never_sends_broken_prediction_chain_after_overload(){
|
||||
let(tx,mut rx)=mpsc::channel(1);
|
||||
let mut gate=DependencyGate::default();
|
||||
let frame=|sequence,idr|Arc::new(CompleteFrame{sequence,completed_at:Instant::now(),data:vec![0,0,1,9,240,0,0,1,if idr{5}else{1},42]});
|
||||
let frame=|sequence,idr|Arc::new(CompleteFrame{_permit:None,sequence,codec:VideoCodec::H264,completed_at:Instant::now(),data:vec![0,0,1,9,240,0,0,1,if idr{5}else{1},42]});
|
||||
gate.submit(&tx,frame(1,false));assert!(rx.try_recv().is_err()); // no IDR yet
|
||||
gate.submit(&tx,frame(2,true));
|
||||
gate.submit(&tx,frame(3,false)); // queue full: P3 lost
|
||||
@@ -620,6 +999,60 @@ mod tests {
|
||||
gate.submit(&tx,frame(31,true));assert_eq!(rx.recv().await.unwrap().sequence,31);
|
||||
gate.submit(&tx,frame(32,false));assert_eq!(rx.recv().await.unwrap().sequence,32);
|
||||
}
|
||||
#[tokio::test]
|
||||
#[ignore = "requires installed FFmpeg with a working HEVC encoder"]
|
||||
async fn hevc_capability_probe_initializes_real_encoder() {
|
||||
assert!(hevc_available().await);
|
||||
}
|
||||
#[test]
|
||||
fn hevc_annex_b_boundaries_and_irap_use_two_byte_headers() {
|
||||
let bytes = [0,0,0,1,70,1,80,0,0,1,64,1,42,0,0,1,38,1,42,0,0,1,70,1,80];
|
||||
assert_eq!(aud_offsets_for(&bytes, VideoCodec::H265), vec![0,19]);
|
||||
assert!(is_independent_frame_for(&bytes[..19], VideoCodec::H265));
|
||||
for kind in 16..=21 {
|
||||
assert!(is_independent_frame_for(&[0,0,1,kind << 1,1,42], VideoCodec::H265));
|
||||
}
|
||||
assert!(!is_independent_frame_for(&[0,0,1,2,1,42], VideoCodec::H265));
|
||||
assert!(!is_independent_frame_for(&[0,0,1,38], VideoCodec::H265));
|
||||
assert!(!is_independent_frame_for(&[0,0,1,38,0,42], VideoCodec::H265));
|
||||
assert!(!is_independent_frame_for(&[0,0,1,166,1,42], VideoCodec::H265));
|
||||
assert!(!is_independent_frame_for(&[0,0,1,38,1,42,0,0,1,2,1,42], VideoCodec::H265));
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn hevc_dependency_gate_recovers_only_at_irap_after_overflow() {
|
||||
let (tx, mut rx) = mpsc::channel(1);
|
||||
let mut gate = DependencyGate::default();
|
||||
let frame = |sequence, irap| Arc::new(CompleteFrame { _permit: None, codec: VideoCodec::H265,
|
||||
sequence, completed_at: Instant::now(), data: vec![0,0,1,if irap { 38 } else { 2 },1,42] });
|
||||
gate.submit(&tx, frame(1, false));
|
||||
assert!(rx.try_recv().is_err());
|
||||
gate.submit(&tx, frame(2, true));
|
||||
gate.submit(&tx, frame(3, false));
|
||||
assert_eq!(rx.recv().await.unwrap().sequence, 2);
|
||||
gate.submit(&tx, frame(4, false));
|
||||
assert!(rx.try_recv().is_err());
|
||||
gate.submit(&tx, frame(5, true));
|
||||
assert_eq!(rx.recv().await.unwrap().sequence, 5);
|
||||
}
|
||||
#[test]
|
||||
fn hevc_options_keep_420_low_latency_and_existing_rate_policy() {
|
||||
let settings = VideoSettings { codec: VideoCodec::H265, ..Default::default() };
|
||||
let cpu = cpu_hevc_options(&settings, "30");
|
||||
for pair in [["-c:v", "libx265"], ["-profile:v", "main"], ["-pix_fmt", "yuv420p"],
|
||||
["-bf", "0"], ["-bufsize", "30000k"]] {
|
||||
assert!(cpu.windows(2).any(|window| window == pair));
|
||||
}
|
||||
let params = x265_params("30");
|
||||
for option in ["aud=1", "repeat-headers=1", "bframes=0", "rc-lookahead=0", "keyint=30", "min-keyint=30"] {
|
||||
assert!(params.split(':').any(|value| value == option));
|
||||
}
|
||||
let vaapi = vaapi_options(&settings, "30");
|
||||
for pair in [["-c:v", "hevc_vaapi"], ["-profile:v", "main"], ["-bf", "0"], ["-bufsize", "500k"]] {
|
||||
assert!(vaapi.windows(2).any(|window| window == pair));
|
||||
}
|
||||
assert_eq!(Encoder::Nvenc.name_for(VideoCodec::H265), "hevc_nvenc");
|
||||
assert_eq!(Encoder::Cpu.name_for(VideoCodec::H264), "libx264");
|
||||
}
|
||||
#[test]
|
||||
fn nvenc_idr_with_parameter_sets_is_independent(){
|
||||
// AUD, SPS, PPS, IDR slice as h264_nvenc emits every GOP (verified on the 3090).
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
//! One negotiated video m-line/SSRC, with codec-specific payload types.
|
||||
//! A single RTP sequence/timestamp survives format changes (never two independent SSRC timelines).
|
||||
use std::{any::Any, sync::atomic::{AtomicBool, Ordering}};
|
||||
use async_trait::async_trait;
|
||||
use tokio::sync::Mutex;
|
||||
use webrtc::{error::{Error, Result}, media::Sample, rtp::{packet::Packet, header::Header, packetizer::Payloader}};
|
||||
use webrtc::rtp_transceiver::rtp_codec::{RTCRtpCodecCapability, RTCRtpCodecParameters, RTPCodecType};
|
||||
use webrtc::track::track_local::{TrackLocal, TrackLocalContext, track_local_static_rtp::TrackLocalStaticRTP};
|
||||
use crate::capture_setting::VideoCodec;
|
||||
|
||||
/// Official SDK GetSupportedFormats constructs H265 with an empty parameter map.
|
||||
pub fn register_hevc(media: &mut webrtc::api::media_engine::MediaEngine) -> Result<()> {
|
||||
media.register_codec(RTCRtpCodecParameters { capability: RTCRtpCodecCapability {
|
||||
mime_type: VideoCodec::H265.mime().into(), clock_rate: 90_000,
|
||||
rtcp_feedback: vec![webrtc::rtp_transceiver::RTCPFeedback { typ: "nack".into(), parameter: String::new() },
|
||||
webrtc::rtp_transceiver::RTCPFeedback { typ: "nack".into(), parameter: "pli".into() }],
|
||||
..Default::default() }, payload_type: 126, ..Default::default() }, RTPCodecType::Video)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Timeline { sequence: u16, timestamp: u32, h264: webrtc::rtp::codecs::h264::H264Payloader }
|
||||
#[derive(Debug)]
|
||||
pub struct VideoTrack {
|
||||
h264: TrackLocalStaticRTP, h265: TrackLocalStaticRTP,
|
||||
has_h264: AtomicBool, has_h265: AtomicBool, timeline: Mutex<Timeline>,
|
||||
progress: tokio::sync::watch::Sender<(u64, VideoCodec)>,
|
||||
}
|
||||
impl VideoTrack {
|
||||
pub fn new() -> Self {
|
||||
let track = |codec: VideoCodec| TrackLocalStaticRTP::new(
|
||||
RTCRtpCodecCapability { mime_type: codec.mime().into(), clock_rate: 90_000, ..Default::default() },
|
||||
"video_0".into(), "video_0".into());
|
||||
let (progress, _) = tokio::sync::watch::channel((0, VideoCodec::H264));
|
||||
Self { progress, h264: track(VideoCodec::H264), h265: track(VideoCodec::H265), has_h264: AtomicBool::new(false),
|
||||
has_h265: AtomicBool::new(false), timeline: Mutex::new(Timeline { sequence: rand::random(), timestamp: rand::random(), h264: Default::default() }) }
|
||||
}
|
||||
pub fn supports(&self, codec: VideoCodec) -> bool {
|
||||
match codec { VideoCodec::H264 => &self.has_h264, VideoCodec::H265 => &self.has_h265 }.load(Ordering::Acquire)
|
||||
}
|
||||
pub fn revision(&self) -> u64 { self.progress.borrow().0 }
|
||||
/// Format-change RPC success follows a transmitted frame, including a negotiated fallback.
|
||||
pub async fn wait_for_applied(&self, since: u64, settings: &tokio::sync::watch::Sender<crate::capture_setting::VideoSettings>) -> bool {
|
||||
let mut progress = self.progress.subscribe();
|
||||
tokio::time::timeout(std::time::Duration::from_secs(10), async {
|
||||
loop {
|
||||
let (revision, codec) = *progress.borrow_and_update();
|
||||
if revision > since && codec == settings.borrow().codec { return true; }
|
||||
if progress.changed().await.is_err() { return false; }
|
||||
}
|
||||
}).await.unwrap_or(false)
|
||||
}
|
||||
pub async fn write_sample_with_extensions(&self, codec: VideoCodec, sample: &Sample, extensions: &[webrtc::rtp::extension::HeaderExtension]) -> Result<()> {
|
||||
if !self.supports(codec) { return Err(Error::ErrUnsupportedCodec); }
|
||||
let track = match codec { VideoCodec::H264 => &self.h264, VideoCodec::H265 => &self.h265 };
|
||||
if track.all_binding_paused().await { return Ok(()); }
|
||||
let mut state = self.timeline.lock().await;
|
||||
// Leave room for the RTP header and negotiated UU extensions below the 1200 byte MTU.
|
||||
let payloads = match codec {
|
||||
VideoCodec::H264 => state.h264.payload(1160, &sample.data).map_err(Error::Rtp)?,
|
||||
VideoCodec::H265 => crate::hevc::payload(1160, &sample.data).map_err(Error::new)?,
|
||||
};
|
||||
let count = payloads.len();
|
||||
let mut complete = count > 0;
|
||||
let timestamp = state.timestamp;
|
||||
state.timestamp = state.timestamp.wrapping_add((sample.duration.as_secs_f64() * 90_000.0) as u32);
|
||||
for (index, payload) in payloads.into_iter().enumerate() {
|
||||
let packet = Packet { header: Header { version: 2, sequence_number: state.sequence, timestamp,
|
||||
marker: index + 1 == count, ..Default::default() }, payload };
|
||||
state.sequence = state.sequence.wrapping_add(1);
|
||||
complete &= track.write_rtp_with_extensions(&packet, extensions).await? > 0;
|
||||
}
|
||||
if complete { self.progress.send_modify(|value| { value.0 += 1; value.1 = codec; }); }
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
#[async_trait]
|
||||
impl TrackLocal for VideoTrack {
|
||||
async fn bind(&self, context: &TrackLocalContext) -> Result<RTCRtpCodecParameters> {
|
||||
let h264 = self.h264.bind(context).await.ok();
|
||||
let h265 = self.h265.bind(context).await.ok();
|
||||
self.has_h264.store(h264.is_some(), Ordering::Release);
|
||||
self.has_h265.store(h265.is_some(), Ordering::Release);
|
||||
log::info!("Video track negotiated: h264={} h265={}", h264.is_some(), h265.is_some());
|
||||
h264.or(h265).ok_or(Error::ErrUnsupportedCodec)
|
||||
}
|
||||
async fn unbind(&self, context: &TrackLocalContext) -> Result<()> {
|
||||
if self.has_h264.swap(false, Ordering::AcqRel) { self.h264.unbind(context).await?; }
|
||||
if self.has_h265.swap(false, Ordering::AcqRel) { self.h265.unbind(context).await?; }
|
||||
Ok(())
|
||||
}
|
||||
fn id(&self) -> &str { "video_0" }
|
||||
fn stream_id(&self) -> &str { "video_0" }
|
||||
fn kind(&self) -> RTPCodecType { RTPCodecType::Video }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use webrtc::{api::{APIBuilder, media_engine::MediaEngine}, peer_connection::configuration::RTCConfiguration};
|
||||
async fn negotiate(h264: bool, hevc: bool) {
|
||||
let mut remote_media = MediaEngine::default();
|
||||
if h264 { remote_media.register_codec(RTCRtpCodecParameters { capability: RTCRtpCodecCapability {
|
||||
mime_type: VideoCodec::H264.mime().into(), clock_rate: 90_000,
|
||||
sdp_fmtp_line: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f".into(), ..Default::default()
|
||||
}, payload_type: 102, ..Default::default() }, RTPCodecType::Video).unwrap(); }
|
||||
if hevc { register_hevc(&mut remote_media).unwrap(); }
|
||||
let mut setting = webrtc::api::setting_engine::SettingEngine::default();
|
||||
setting.set_ice_multicast_dns_mode(webrtc::ice::mdns::MulticastDnsMode::Disabled);
|
||||
// UDP over IPv4 only: pairing every host interface (VPNs, IPv6) made ICE exceed the
|
||||
// connect timeout when the whole suite runs in parallel.
|
||||
setting.set_network_types(vec![webrtc::ice::network_type::NetworkType::Udp4]);
|
||||
let remote = APIBuilder::new().with_setting_engine(setting.clone()).with_media_engine(remote_media).build().new_peer_connection(RTCConfiguration::default()).await.unwrap();
|
||||
remote.add_transceiver_from_kind(RTPCodecType::Video, None).await.unwrap();
|
||||
let mut media = MediaEngine::default(); media.register_default_codecs().unwrap(); register_hevc(&mut media).unwrap();
|
||||
let host = APIBuilder::new().with_setting_engine(setting).with_media_engine(media).build().new_peer_connection(RTCConfiguration::default()).await.unwrap();
|
||||
let (packets_tx, mut packets_rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
remote.on_track(Box::new(move |remote_track, _, _| {
|
||||
let packets_tx = packets_tx.clone();
|
||||
Box::pin(async move {
|
||||
tokio::spawn(async move {
|
||||
while let Ok((packet, _)) = remote_track.read_rtp().await {
|
||||
if packets_tx.send(packet).is_err() { break; }
|
||||
}
|
||||
});
|
||||
})
|
||||
}));
|
||||
let track = std::sync::Arc::new(VideoTrack::new());
|
||||
host.add_track(track.clone()).await.unwrap();
|
||||
let offer = remote.create_offer(None).await.unwrap();
|
||||
let mut gathered = remote.gathering_complete_promise().await;
|
||||
remote.set_local_description(offer).await.unwrap();
|
||||
gathered.recv().await;
|
||||
host.set_remote_description(remote.local_description().await.unwrap()).await.unwrap();
|
||||
let answer = host.create_answer(None).await.unwrap();
|
||||
assert_eq!(answer.sdp.contains("H265/90000"), hevc);
|
||||
assert_eq!(answer.sdp.contains("H264/90000"), h264);
|
||||
let mut gathered = host.gathering_complete_promise().await;
|
||||
host.set_local_description(answer).await.unwrap();
|
||||
gathered.recv().await;
|
||||
remote.set_remote_description(host.local_description().await.unwrap()).await.unwrap();
|
||||
tokio::time::timeout(std::time::Duration::from_secs(10), async {
|
||||
while !track.supports(VideoCodec::H264) && !track.supports(VideoCodec::H265) { tokio::time::sleep(std::time::Duration::from_millis(10)).await; }
|
||||
}).await.unwrap();
|
||||
assert_eq!(track.supports(VideoCodec::H264), h264);
|
||||
assert_eq!(track.supports(VideoCodec::H265), hevc);
|
||||
tokio::time::timeout(std::time::Duration::from_secs(30), async {
|
||||
while host.connection_state() != webrtc::peer_connection::peer_connection_state::RTCPeerConnectionState::Connected {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||||
}
|
||||
}).await.unwrap();
|
||||
let codecs = if !h264 { vec![VideoCodec::H265] } else if hevc { vec![VideoCodec::H264, VideoCodec::H265, VideoCodec::H264] } else { vec![VideoCodec::H264] };
|
||||
let mut previous: Option<Packet> = None;
|
||||
for codec in codecs {
|
||||
let data = match codec { VideoCodec::H264 => bytes::Bytes::from_static(&[0,0,1,65,1,2]),
|
||||
VideoCodec::H265 => bytes::Bytes::from_static(&[0,0,1,38,1,2]) };
|
||||
let (settings, _) = tokio::sync::watch::channel(crate::capture_setting::VideoSettings { codec, ..Default::default() });
|
||||
let applied = track.wait_for_applied(track.revision(), &settings);
|
||||
tokio::pin!(applied);
|
||||
assert!(tokio::time::timeout(std::time::Duration::from_millis(1), &mut applied).await.is_err());
|
||||
track.write_sample_with_extensions(codec, &Sample { data, duration: std::time::Duration::from_millis(20), ..Default::default() }, &[]).await.unwrap();
|
||||
assert!(applied.await);
|
||||
let packet = tokio::time::timeout(std::time::Duration::from_secs(3), packets_rx.recv()).await.unwrap().unwrap();
|
||||
assert_eq!(packet.header.payload_type, if codec == VideoCodec::H264 { 102 } else { 126 });
|
||||
assert!(packet.header.marker);
|
||||
if let Some(previous) = previous {
|
||||
assert_eq!(packet.header.ssrc, previous.header.ssrc);
|
||||
assert_eq!(packet.header.sequence_number, previous.header.sequence_number.wrapping_add(1));
|
||||
assert_eq!(packet.header.timestamp, previous.header.timestamp.wrapping_add(1800));
|
||||
}
|
||||
previous = Some(packet);
|
||||
}
|
||||
host.close().await.unwrap(); remote.close().await.unwrap();
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn negotiated_codecs_only_and_legacy_h264_offer() { negotiate(true, false).await; negotiate(true, true).await; negotiate(false, true).await; }
|
||||
}
|
||||
+36
-6
@@ -26,7 +26,7 @@ pub fn init() -> Result<(u32,u32),String> {
|
||||
return Ok((w as u32,h as u32));
|
||||
}
|
||||
#[cfg(not(all(target_os="linux", feature="wayland")))]
|
||||
Err("Wayland backend requires Linux and a build with --features wayland".into())
|
||||
Err("this build was compiled without the wayland feature; rebuild with --features wayland".into())
|
||||
}
|
||||
pub fn capture(buffer:&mut [u8]) -> Result<(),String> {
|
||||
#[cfg(all(target_os="linux", feature="wayland"))]
|
||||
@@ -60,19 +60,49 @@ pub fn key(symbol:u64,pressed:bool)->i32 {
|
||||
}
|
||||
|
||||
/// A separate process owns capture; encoder cancellation closes its pipe.
|
||||
/// Capture and the pipe write run on separate threads with two frame buffers, so a
|
||||
/// slow encoder read no longer delays the next screencopy request.
|
||||
pub fn capture_stdout() -> Result<(),String> {
|
||||
use std::io::Write;
|
||||
use std::io::{Read,Write};
|
||||
let (w,h)=init()?;
|
||||
let mut frame=vec![0u8;(w as usize)*(h as usize)*4];
|
||||
let mut stdout=std::io::stdout().lock();
|
||||
let size=(w as usize)*(h as usize)*4;
|
||||
let fps=capture_fps(std::env::var("UU_WAYLAND_FPS").ok().as_deref())?;
|
||||
let interval=std::time::Duration::from_nanos(1_000_000_000/u64::from(fps));
|
||||
// A larger pipe absorbs more of each frame per write; failure keeps the default.
|
||||
#[cfg(target_os="linux")]
|
||||
let _=unsafe { libc::fcntl(1, libc::F_SETPIPE_SZ, 1<<20) };
|
||||
let (full_tx,full_rx)=std::sync::mpsc::sync_channel::<Vec<u8>>(1);
|
||||
let (free_tx,free_rx)=std::sync::mpsc::sync_channel::<Vec<u8>>(2);
|
||||
for _ in 0..2 { free_tx.send(vec![0u8;size]).map_err(|_|"Capture buffers")?; }
|
||||
let writer=std::thread::Builder::new().name("uu-wl-write".into()).spawn(move || -> Result<(),String> {
|
||||
let mut stdout=std::io::stdout().lock();
|
||||
for frame in full_rx {
|
||||
stdout.write_all(&frame).map_err(|e|format!("Capture pipe: {e}"))?;
|
||||
stdout.flush().map_err(|e|format!("Capture pipe: {e}"))?;
|
||||
if free_tx.send(frame).is_err() { break; }
|
||||
}
|
||||
Ok(())
|
||||
}).map_err(|e|format!("Capture writer: {e}"))?;
|
||||
let demand=std::env::var("UU_WAYLAND_DEMAND").is_ok_and(|v|v=="1");
|
||||
let mut stdin=std::io::stdin().lock();
|
||||
let mut delivered=std::time::Instant::now();
|
||||
loop {
|
||||
if demand { stdin.read_exact(&mut [0]).map_err(|_|"Capture demand pipe closed")?; }
|
||||
// Time from buffer availability: a slow encoder read must not shorten the next interval.
|
||||
let Ok(mut frame)=free_rx.recv() else { break };
|
||||
let start=std::time::Instant::now();
|
||||
// The engine prefetches the next frame on delivery; after a backpressure stall that
|
||||
// prefetch is as old as the stall, so drop it and capture the current screen.
|
||||
if demand && start.duration_since(delivered)>interval*2 { capture(&mut frame)?; }
|
||||
capture(&mut frame)?;
|
||||
stdout.write_all(&frame).map_err(|e|format!("Capture pipe: {e}"))?;
|
||||
if let Some(left)=interval.checked_sub(start.elapsed()) { std::thread::sleep(left); }
|
||||
delivered=std::time::Instant::now();
|
||||
if full_tx.send(frame).is_err() { break; }
|
||||
if !demand {
|
||||
if let Some(left)=interval.checked_sub(start.elapsed()) { std::thread::sleep(left); }
|
||||
}
|
||||
}
|
||||
drop(full_tx);
|
||||
writer.join().map_err(|_|"Capture writer panicked")?
|
||||
}
|
||||
|
||||
pub(crate) fn capture_fps(value:Option<&str>)->Result<u32,String> {
|
||||
|
||||
+66
-13
@@ -53,6 +53,9 @@ impl WebRtcSession {
|
||||
m.register_default_codecs()
|
||||
.map_err(|e| format!("Register standard codecs failed: {}", e))?;
|
||||
|
||||
let hevc = desktop && crate::video::hevc_available().await;
|
||||
if hevc { crate::video_track::register_hevc(&mut m).map_err(|e| format!("Register HEVC failed: {e}"))?; }
|
||||
|
||||
m.register_header_extension(
|
||||
webrtc::rtp_transceiver::rtp_codec::RTCRtpHeaderExtensionCapability { uri: crate::rtp_metadata::CAPTURE_INDEX_URI.into() },
|
||||
webrtc::rtp_transceiver::rtp_codec::RTPCodecType::Video, None,
|
||||
@@ -162,7 +165,7 @@ impl WebRtcSession {
|
||||
tokio::spawn(async move {
|
||||
let ctx = ctx.lock().await.clone();
|
||||
if let (Some(client), Some(ice)) = (&ctx.client_id, ctx.ice_id.as_ref().and_then(|v| v.as_str())) {
|
||||
let payload = crate::capability::h264_device_capability(client, ice, 0, crate::video::FPS as i32, 5);
|
||||
let payload = crate::capability::device_capability(client, ice, 0, crate::video::FPS as i32, 5, hevc);
|
||||
if let Err(e) = sig.emit_event(crate::capability::FORWARD_SETTING_EVENT, payload).await {
|
||||
log::error!("Device capability notification failed: {}", e);
|
||||
}
|
||||
@@ -179,14 +182,7 @@ impl WebRtcSession {
|
||||
}));
|
||||
|
||||
// 注册视频轨
|
||||
let video_track = Arc::new(TrackLocalStaticSample::new(
|
||||
RTCRtpCodecCapability {
|
||||
mime_type: "video/H264".to_owned(),
|
||||
..Default::default()
|
||||
},
|
||||
"video_0".to_owned(),
|
||||
"video_0".to_owned(),
|
||||
));
|
||||
let video_track = Arc::new(crate::video_track::VideoTrack::new());
|
||||
|
||||
// Set by PLI/FIR; the desktop encoder answers with an IDR on its next frame.
|
||||
let idr_request = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
@@ -274,10 +270,18 @@ impl WebRtcSession {
|
||||
}
|
||||
let portal=capture.portal.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 {
|
||||
let state_pc = streaming_pc.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, idr_request).await {
|
||||
log::error!("Live desktop stream stopped: {}", e);
|
||||
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, stream_settings, idr_request).await {
|
||||
// A send racing the viewer's close fails with a misleading track error.
|
||||
if matches!(state_pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed) {
|
||||
log::info!("Live desktop stream ended: peer connection closed ({})", e);
|
||||
} else {
|
||||
log::error!("Live desktop stream stopped: {}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@@ -338,6 +342,7 @@ impl WebRtcSession {
|
||||
let peer_files = peer_files.clone();
|
||||
let peer_port_mapping = port_mapping.clone();
|
||||
let peer_video_settings = video_settings.clone();
|
||||
let peer_video_track = control_video_track.clone();
|
||||
let peer_actions = peer_actions.clone();
|
||||
if label == "FILE_DATA_CHANNEL" { *file_channel.lock().unwrap() = Some(d.clone()); }
|
||||
|
||||
@@ -389,9 +394,11 @@ impl WebRtcSession {
|
||||
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();
|
||||
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();
|
||||
@@ -462,9 +469,18 @@ impl WebRtcSession {
|
||||
(13, Ok(crate::initialization::envelope(next_seq(), timestamp, 13, &init_actions.metrics_payload(desktop_sound, desktop_permission)))),
|
||||
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
|
||||
&crate::system_metrics::remote_download_path(&home)))),
|
||||
(18, Ok(crate::initialization::envelope(next_seq(), timestamp, 18,
|
||||
&crate::capture_setting::codec_negotiation(screen_size, crate::video::FPS as u32, init_track.supports(crate::capture_setting::VideoCodec::H265))))),
|
||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
|
||||
];
|
||||
if desktop && screen.init_error.is_none() {
|
||||
let codec = crate::capture_setting::preferred_codec(init_track.supports(crate::capture_setting::VideoCodec::H264),
|
||||
init_track.supports(crate::capture_setting::VideoCodec::H265));
|
||||
init_video.send_if_modified(|current| if current.codec != codec {
|
||||
log::info!("Desktop codec selected: {:?}", codec); current.codec = codec; true } else { false });
|
||||
}
|
||||
for (field, message) in messages {
|
||||
if (!desktop || screen.init_error.is_some()) && field == 18 { continue; }
|
||||
if desktop && screen.init_error.is_some() && matches!(field,7|8) { continue; }
|
||||
let message = match message {
|
||||
Ok(message) => message,
|
||||
@@ -522,6 +538,7 @@ impl WebRtcSession {
|
||||
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();
|
||||
|
||||
Box::pin(async move {
|
||||
@@ -532,8 +549,17 @@ impl WebRtcSession {
|
||||
use crate::protocol::WireValue;
|
||||
let rpc = fields.iter().find_map(|f| match (f.number, &f.value) { (21, WireValue::Bytes(b)) => Some(*b), _ => None });
|
||||
let rpc_fields = rpc.and_then(|r| crate::protocol::decode_wire(r).ok());
|
||||
let output_before = video_settings.borrow().output;
|
||||
if let Some(response) = rpc_fields.as_deref().and_then(|r| crate::capture_setting::handle_rpc(r, &video_settings, input_size)) {
|
||||
let 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));
|
||||
@@ -865,6 +891,33 @@ impl WebRtcSession {
|
||||
};
|
||||
log::info!("Offer x-uuremote-quic: {}", quic_summary);
|
||||
log::info!("Offer includes UU capture-index extension: {}", sdp_str.lines().any(|l| l.starts_with("a=extmap:") && l.contains(crate::rtp_metadata::CAPTURE_INDEX_URI)));
|
||||
let mut codecs: Vec<&str> = sdp_str.lines().filter_map(|l| l.strip_prefix("a=rtpmap:")).filter_map(|l| l.split_whitespace().nth(1))
|
||||
.filter_map(|c| c.split('/').next()).collect();
|
||||
codecs.dedup();
|
||||
log::info!("Offer codecs: {:?}", codecs);
|
||||
// SDP structure only: attribute names per section and public extmap URIs; never ufrag,
|
||||
// password, fingerprint, candidate or fmtp values.
|
||||
let mut sections: Vec<String> = Vec::new();
|
||||
let mut current = String::from("session");
|
||||
let mut names: std::collections::BTreeSet<String> = Default::default();
|
||||
let mut flush = |current: &str, names: &mut std::collections::BTreeSet<String>, sections: &mut Vec<String>| {
|
||||
sections.push(format!("{current}[{}]", names.iter().cloned().collect::<Vec<_>>().join(",")));
|
||||
names.clear();
|
||||
};
|
||||
for line in sdp_str.lines() {
|
||||
if let Some(m) = line.strip_prefix("m=") {
|
||||
flush(¤t, &mut names, &mut sections);
|
||||
let mut parts = m.split_whitespace();
|
||||
let kind = parts.next().unwrap_or("?");
|
||||
current = format!("{kind}/{}pt", parts.skip(2).count());
|
||||
} else if let Some(a) = line.strip_prefix("a=") {
|
||||
let name = a.split([':', ' ']).next().unwrap_or("");
|
||||
if name == "extmap" { if let Some(uri) = a.split_whitespace().nth(1) { names.insert(format!("extmap={}", uri.rsplit('/').next().unwrap_or(uri))); } }
|
||||
else if !name.is_empty() && name.len() <= 32 { names.insert(name.to_owned()); }
|
||||
}
|
||||
}
|
||||
flush(¤t, &mut names, &mut sections);
|
||||
log::info!("Offer SDP structure: {}", sections.join(" | "));
|
||||
let offer = RTCSessionDescription::offer(sdp_str)
|
||||
.map_err(|e| format!("Invalid offer: {}", e))?;
|
||||
|
||||
|
||||
Reference in new issue
Block a user