mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 08:50:40 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c041160d6d | ||
|
|
69aecf0269 | ||
|
|
979b62d271 | ||
|
|
6a3da14961 |
No files matched your search
@@ -32,8 +32,8 @@ body:
|
||||
attributes:
|
||||
label: 诊断信息 / Diagnostics
|
||||
description: |
|
||||
先复现一次问题,然后在被控的 Linux 上运行下面的命令,把输出粘贴到这里(IP 会被自动抹掉,不包含密码、令牌或剪贴板内容)。
|
||||
Reproduce the problem, then run this on the Linux host and paste the output (IPs are masked; no passwords, tokens or clipboard data).
|
||||
先复现一次问题,然后在被控 Linux 的**图形桌面里打开的终端**中运行下面的命令(SSH 或 tty 里缺少桌面环境变量,doctor 结果不准),把输出粘贴到这里(IPv4/IPv6/MAC 地址会被自动抹掉,不包含密码、令牌或剪贴板内容)。
|
||||
Reproduce the problem, then run this in a terminal **inside the Linux graphical session** (SSH/tty lacks the desktop environment, so doctor would be misleading) and paste the output (IPv4/IPv6/MAC addresses are masked; no passwords, tokens or clipboard data).
|
||||
|
||||
```sh
|
||||
{
|
||||
@@ -45,8 +45,9 @@ body:
|
||||
echo "== 显卡"; lspci 2>/dev/null | grep -iE 'vga|3d'
|
||||
echo "== FFmpeg"; ffmpeg -hide_banner -encoders 2>/dev/null | grep -E 'h264_nvenc|libx264'
|
||||
echo "== 日志"; journalctl --user -u uuremote-native.service --since "-30min" --no-pager -o cat \
|
||||
| grep -E 'Starting|backend|Selected capture|Desktop encoder|Capture setting request|Desktop video settings|Offer for connection|WARN|ERROR' | tail -n 80
|
||||
} 2>&1 | sed -E 's/[0-9]{1,3}(\.[0-9]{1,3}){3}/x.x.x.x/g'
|
||||
| grep -E 'Starting|backend|Selected capture|Desktop encoder|Capture setting request|Desktop video settings|Offer for connection|PeerConnection State|Remote controller disconnected|ICE path|Port mapping|WARN|ERROR' \
|
||||
| grep -vE 'webrtc_ice|webrtc_dtls|webrtc::mux' | tail -n 120
|
||||
} 2>&1 | sed -E 's/[0-9]{1,3}(\.[0-9]{1,3}){3}/x.x.x.x/g; s/([0-9a-fA-F]{1,4}:){4,7}[0-9a-fA-F]{1,4}|[0-9a-fA-F]{0,4}::[0-9a-fA-F:]*[0-9a-fA-F]/x:x::x/g'
|
||||
```
|
||||
render: text
|
||||
validations:
|
||||
|
||||
@@ -117,8 +117,9 @@ viewer does not reconnect them automatically.
|
||||
| Item | Meaning |
|
||||
|---|---|
|
||||
| `~/.config/uuremote/config.json` | user_id, device_id, client_id, system_id, machine_guid, token, csrf; optional `sign_key`, `ffmpeg_root`. `$UU_CONFIG` overrides the path |
|
||||
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent; optional `video_encoder` (`auto`/`cpu`/`vaapi`/`nvenc`) and `video_codec` (`auto`/`h264`/`h265`), written by `set-encoder` / `set-codec` (0600, merge write). Non-`auto` beats `UU_VIDEO_ENCODER` / `UU_VIDEO_CODEC`. Applied on the next desktop stream |
|
||||
| `~/.config/uuremote/host_settings.json` | persisted 安全 settings: auto_lock, keep_mute, mute_permanent; optional `video_encoder` (`auto`/`cpu`/`vaapi`/`nvenc`) and `video_codec` (`auto`/`h264`/`h265`), written by `set-encoder` / `set-codec` (0600, merge write). Non-`auto` beats `UU_VIDEO_ENCODER` / `UU_VIDEO_CODEC`. `set-encoder` and `set-codec` both send `SIGUSR2` to the pid in `host.pid` (same binary). Live desktop streams restart the encoder (`EncoderTask::restart`, sequence kept, first frame IDR) without dropping the peer. Codec preference is logged and applies on the next desktop stream. `SIGUSR2` does not re-probe. Hand edits: `kill -USR2 $(cat ~/.config/uuremote/host.pid)` |
|
||||
| `UU_DESKTOP_BACKEND` | `auto` (default) / `x11` / `wayland` / `portal` / `kms` (experimental feature) / `none` (headless: no display; desktop offers ignored; tool peers get a nominal 1920x1080 screen + Allow permissions, which the port-mapping viewer requires) |
|
||||
| `UU_X11_MULTISCREEN=1`, `UU_X11_OUTPUT` | X11: advertise each RandR monitor (`xrandr --listmonitors`, read once at start; primary or `UU_X11_OUTPUT` is id 0) through the same screen-switch path as `UU_WAYLAND_MULTISCREEN` (`wayland::outputs`/`select`). x11grab reads `display+x,y` at the monitor size, XTest/uinput absolute input adds the monitor offset, the in-process engine and `desktop_mode` resize are skipped (分辨率 scales). Needs two or more monitors |
|
||||
| `UU_DESKTOP_RESIZE=0` | X11: the viewer's 分辨率 choice scales the encoded stream instead of changing the display mode. Default: `src/desktop_mode.rs` switches the primary output via xrandr (adds a CVT-RB mode when missing, re-applies once if mutter resets it), restores the original mode for resolution_type 3 and 2 s after the last desktop stream ends |
|
||||
| `UU_NVENC_RGB=0` / `=1` | NVENC 4:2:0 without scaling takes packed BGRA/BGR0 directly (no CPU swscale). Default: on, except when a connected DRM connector belongs to an nvidia-bound card (desktop composited on the encoding GPU; #16 measured 82.6 vs 98 fps at 4K). `0` forces CPU conversion, `1` forces the direct path |
|
||||
| `UU_INPUT_BACKEND` | `auto` (uinput on Wayland/portal when `/dev/uinput` is accessible, udev `uaccess` rule in README) / `uinput` / `native`. X11 keeps XTest (held-key autorepeat handling) |
|
||||
@@ -168,7 +169,7 @@ for a new QR login.
|
||||
| `src/viewer_main.rs`, `src/viewer/` | experimental viewer (`--features native-viewer,viewer-sdl`): cloud join, WebRTC offer side, SDL desktop window (ffmpeg CLI decode), files/terminal/portmap/power/assist tools, device removal (`POST /api/v1/device/{id}/unbind`, verified live; shared devices use `/api/v1/device/share/{id}/unbind`, strings only), GTK launcher |
|
||||
| `c_engine/viewer_window.c`, `c_engine/viewer_tools.c` | viewer GTK launcher and tool windows (files, port mapping, terminal list) |
|
||||
|
||||
Diagnostic subcommands: `doctor` (paste-safe report: session, selected backend, engine plugins, uinput, encoder and codec with source, sign-in/service state; booleans and names only), `set-encoder auto|cpu|vaapi|nvenc`, `set-codec auto|h264|h265` (next desktop stream; illegal values exit 2), `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected),
|
||||
Diagnostic subcommands: `doctor` (paste-safe report: session, selected backend, engine plugins, uinput, encoder and codec with source, sign-in/service state; booleans and names only), `set-encoder auto|cpu|vaapi|nvenc` and `set-codec auto|h264|h265` (both notify a running host with SIGUSR2; live encoder switch, negotiated codec unchanged, codec preference on the next desktop stream; illegal values exit 2), `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected),
|
||||
`desktop-probe [new.png]`, `desktop-video-probe` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto; `UU_PROBE_CHROMA=444` 真彩; `UU_PROBE_INPUT` fixed video/image sequence, `UU_PROBE_LOOP=1` repeats), `wayland-input-probe`, `clipboard-acceptance`,
|
||||
`portal-acceptance`, `help`. Unknown arguments exit with code 2. Builds up to v0.1.0 did **not** reject them and
|
||||
started a second full host server instead, so check that a subcommand exists in the binary you run.
|
||||
|
||||
Generated
+1
-1
@@ -2741,7 +2741,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.2.28"
|
||||
version = "0.2.29"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.2.28"
|
||||
version = "0.2.29"
|
||||
edition = "2021"
|
||||
default-run = "uuremote-server"
|
||||
authors = ["uu-remote-linux contributors"]
|
||||
|
||||
+13
-3
@@ -104,19 +104,21 @@ failure falls back to SHM. The virgl VM verified fallback only: successful impor
|
||||
mapping and hardware acceleration remain unverified. This is not zero-copy VAAPI;
|
||||
`UU_VIDEO_ENCODER=vaapi` still uploads CPU frames through FFmpeg.
|
||||
|
||||
### Hyprland monitor switching (experimental)
|
||||
### Monitor switching (experimental)
|
||||
|
||||
Native Wayland absolute pointer motion now targets the captured output by default, fixing multi-monitor click offsets. uinput may still handle keyboard, buttons and relative motion.
|
||||
|
||||
Set `UU_WAYLAND_MULTISCREEN=1` to select physical monitors from the phone client. `UU_WAYLAND_OUTPUT` chooses the initial output. Capture and absolute pointer targeting switch together across different resolutions, scales and desktop positions.
|
||||
|
||||
X11 sessions send all monitors as one combined picture by default. With `UU_X11_MULTISCREEN=1` the host lists the monitors from `xrandr --listmonitors`; the viewer selects one, only that region is captured, and absolute pointer positions are offset to that monitor. The primary monitor is the default; `UU_X11_OUTPUT` picks another (e.g. `HDMI-1`). The layout is read when the host starts; restart the service after changing it. In this mode the viewer's resolution setting scales the stream instead of changing the display mode. Experimental: unit tests and Xvfb virtual monitors only, not yet accepted with the official clients.
|
||||
|
||||
Switching reuses one video track and pauses input until the new source sends a frame. Capture/encoder failure or a first-frame timeout requests restoration of the previous source. The inventory uses `hyprctl` and supports unrotated outputs; restart the host after monitor hotplug or layout changes. Phone-side switching and clicks were tested on Omarchy / Hyprland with 2560×1440 at 125% and 1920×1080 at 133.33%.
|
||||
|
||||
## Not implemented / not supported
|
||||
|
||||
| Feature | Status |
|
||||
|---|---|
|
||||
| Switching between multiple monitors | Experimental on native Hyprland Wayland; see above |
|
||||
| Switching between multiple monitors | Experimental on native Hyprland Wayland and on X11 (`UU_X11_MULTISCREEN=1`); see above |
|
||||
| True color (4:4:4) | Supported (v0.1.15): the default FFmpeg pipeline with NVENC / libx264 encodes H.264 High 4:4:4; `inproc-av` native NVENC encodes H.264 High 4:4:4 / HEVC FRExt; VAAPI stays 4:2:0 and reports that to the client |
|
||||
| HDR | Not implemented |
|
||||
| Super screen / virtual display | Not implemented |
|
||||
@@ -339,6 +341,8 @@ Common environment variables:
|
||||
| `UU_NVENC_RGB=0` / `=1` | NVENC encodes BGRA directly (saves CPU). Off by default when a monitor is connected to the NVIDIA card (slower when the desktop is composited on the same GPU); `0` forces off, `1` forces on |
|
||||
| `UU_WAYLAND_MULTISCREEN=1` | Enable Hyprland physical monitor switching (off by default) |
|
||||
| `UU_WAYLAND_OUTPUT` | Initial native Wayland output name, e.g. `HDMI-A-1` |
|
||||
| `UU_X11_MULTISCREEN=1` | Select and capture X11 monitors separately (experimental, off by default, needs `xrandr`) |
|
||||
| `UU_X11_OUTPUT` | Initial X11 monitor name (`xrandr --listmonitors`); default: the primary |
|
||||
| `UU_WAYLAND_NO_VIRTUAL_KEYBOARD=1` | Native Wayland skips the virtual keyboard (e.g. Denial reserves it for its input method); keys go through uinput. Without wlr-virtual-pointer the pointer also uses uinput (#72) |
|
||||
| `UU_INPUT_BACKEND` | `auto` (default: uinput on Wayland when accessible) / `uinput` / `native` |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | Disable the Ctrl+V paste rewrite in terminal windows |
|
||||
@@ -365,7 +369,13 @@ stays cached until the process exits, except when NVIDIA presence (`/proc/driver
|
||||
or `/dev/nvidia0`) no longer matches the report: the next desktop connection then starts one
|
||||
background re-probe (`warm_auto_probe`) and still uses the cached encoder until the new result
|
||||
is stored. An NVENC failure is kept for 10 minutes, and the next desktop connection after that
|
||||
starts the same background re-probe while still using the cached encoder. They take effect the next time a desktop connection starts. Explicit `vaapi` with 真彩 stays on VAAPI,
|
||||
starts the same background re-probe while still using the cached encoder. `set-encoder` and
|
||||
`set-codec` both send `SIGUSR2` to the host in `host.pid` when that process is this program.
|
||||
An encoder change switches each live desktop stream without disconnecting and keeps the codec
|
||||
negotiated for that session. A failed start follows the existing fallback (in-process, then the
|
||||
FFmpeg process, then CPU). After a hand edit, `kill -USR2 $(cat ~/.config/uuremote/host.pid)`
|
||||
does the same. `SIGUSR2` does not re-probe. A codec change takes effect the next time a desktop
|
||||
connection starts; the running host only logs it. Explicit `vaapi` with 真彩 stays on VAAPI,
|
||||
encodes 4:2:0, and reports chroma 4:2:0 in CaptureSettingSync field 3. If the chosen encoder
|
||||
cannot start, the stream falls back to CPU and logs a warning. VAAPI is not an auto candidate
|
||||
until #33.
|
||||
|
||||
@@ -90,19 +90,21 @@ GNOME Portal 的控制通道不再等待授权窗口或第一帧画面:先通
|
||||
原生 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 帧。
|
||||
首次连接时,桌面会弹窗请求「远程桌面 / 屏幕共享」授权,同意后会记住授权。
|
||||
|
||||
### Hyprland 多显示器切换(实验性)
|
||||
### 多显示器切换(实验性)
|
||||
|
||||
原生 Wayland 的绝对鼠标定位默认绑定正在采集的输出,修复双屏桌面下的点击偏移。uinput 可继续用于键盘、按键和相对移动。
|
||||
|
||||
设置 `UU_WAYLAND_MULTISCREEN=1` 后,手机端可选择并切换物理显示器;`UU_WAYLAND_OUTPUT` 指定默认屏幕。画面与绝对鼠标定位同步切换,支持不同分辨率、缩放和桌面位置。
|
||||
|
||||
X11 会话默认把所有显示器拼成一张画面。设置 `UU_X11_MULTISCREEN=1` 后,主机按 `xrandr --listmonitors` 列出各个显示器,控制端可选择其中一块,只采集该区域,绝对鼠标坐标加上该屏幕在桌面中的偏移;默认屏幕为主显示器,`UU_X11_OUTPUT` 可指定其他屏幕(如 `HDMI-1`)。显示器布局在主机启动时读取,改动后需重启服务。此模式下控制端的「分辨率」改为缩放画面,不再修改显示模式。实验性:目前只有单元测试与 Xvfb 虚拟显示器实测,尚未经官方客户端实机验收。
|
||||
|
||||
切屏复用同一条视频轨道,过渡期间暂停输入,发送新屏首帧后恢复;采集或编码失败、等待首帧超时会请求恢复原屏。当前通过 `hyprctl` 枚举未旋转的显示器,显示器插拔或布局变化后需重启服务。已在 Omarchy / Hyprland 上用官方手机客户端实测 2560×1440(125%)与 1920×1080(133.33%)双屏切换及点击。
|
||||
|
||||
## 尚未实现 / 不支持
|
||||
|
||||
| 功能 | 状态 |
|
||||
|---|---|
|
||||
| 多显示器切换 | Hyprland 原生 Wayland 后端实验性支持,见上文 |
|
||||
| 多显示器切换 | Hyprland 原生 Wayland 后端与 X11(`UU_X11_MULTISCREEN=1`)实验性支持,见上文 |
|
||||
| 真彩(4:4:4) | 支持(v0.1.15):NVENC / libx264 的默认 FFmpeg 管线输出 H.264 High 4:4:4;`inproc-av` 原生 NVENC 输出 H.264 High 4:4:4 / HEVC FRExt;VAAPI 管线仍为 4:2:0,并如实回报给客户端 |
|
||||
| HDR | 未实现 |
|
||||
| 超级屏 / 虚拟显示器 | 未实现 |
|
||||
@@ -307,6 +309,8 @@ systemctl --user restart uuremote-native.service
|
||||
| `UU_NVENC_RGB=0` / `=1` | NVENC 直接编码 BGRA(省 CPU)。显示器接在 NVIDIA 卡上时默认关闭(同卡合成时直吃反而更慢),`0` 强制关,`1` 强制开 |
|
||||
| `UU_WAYLAND_MULTISCREEN=1` | 启用 Hyprland 物理显示器切换(默认关闭) |
|
||||
| `UU_WAYLAND_OUTPUT` | 原生 Wayland 默认采集输出名称,例如 `HDMI-A-1` |
|
||||
| `UU_X11_MULTISCREEN=1` | X11 下按显示器分别选择和采集(实验性,默认关闭,需要 `xrandr`) |
|
||||
| `UU_X11_OUTPUT` | X11 默认采集的显示器名称(`xrandr --listmonitors`),默认主显示器 |
|
||||
| `UU_WAYLAND_NO_VIRTUAL_KEYBOARD=1` | 原生 Wayland 不创建虚拟键盘(如 Denial 只把它给自己的输入法),键盘改走 uinput。合成器没有 wlr-virtual-pointer 时鼠标也自动改走 uinput(#72) |
|
||||
| `UU_INPUT_BACKEND` | `auto`(默认,Wayland 下可用时用 uinput)/ `uinput` / `native` |
|
||||
| `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 |
|
||||
@@ -321,7 +325,7 @@ systemctl --user restart uuremote-native.service
|
||||
| `UU_WOL_HELPER=1` | 实验性:让这台主机像官方 Windows 被控端一样充当「远程开机」辅助设备——登录后向网易上报本机局域网信息,收到云端唤醒请求时向同一局域网发送唤醒包(未经端到端验证) |
|
||||
| `UU_USE_PROXY=1` | 使用环境变量里的 HTTP/SOCKS 代理;默认忽略代理、直连网易服务器 |
|
||||
|
||||
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。同一文件里的 `video_encoder`(`auto`/`cpu`/`vaapi`/`nvenc`)和 `video_codec`(`auto`/`h264`/`h265`)用 `uuremote-server set-encoder`、`uuremote-server set-codec` 写入(非法参数退出码 2)。非 `auto` 的值优先于 `UU_VIDEO_ENCODER` / `UU_VIDEO_CODEC`;`auto` 或留空则落到环境变量,再落到 auto(有 NVIDIA 时 NVENC 优先,失败再 CPU;没有 NVIDIA 时就是 CPU,不探测;codec 仍是硬件优先 H.265、CPU 用 H.264)。探测在启动时后台进行,不挡住第一路出图。成功结果缓存到进程退出,但 NVIDIA 是否存在(`/proc/driver/nvidia/version` 或 `/dev/nvidia0`)与报告不一致时,下一次桌面连接会在后台再探测一次(`warm_auto_probe`),这一路仍用缓存的编码器,直到新结果写入。NVENC 失败只缓存 10 分钟,到期后的桌面连接同样在后台再探测一次,这一路仍用当前结果。下次建立桌面连接时生效。显式 `vaapi` 请求真彩时仍用 VAAPI、编码 4:2:0,并在 CaptureSettingSync 的 field 3 回报 4:2:0。所选编码器在本机不可用时,建流回退 CPU 并记 warning。VAAPI 进入 auto 要等 #33。
|
||||
「远程结束后自动锁屏」「静音运行」等设置保存在 `~/.config/uuremote/host_settings.json`。同一文件里的 `video_encoder`(`auto`/`cpu`/`vaapi`/`nvenc`)和 `video_codec`(`auto`/`h264`/`h265`)用 `uuremote-server set-encoder`、`uuremote-server set-codec` 写入(非法参数退出码 2)。非 `auto` 的值优先于 `UU_VIDEO_ENCODER` / `UU_VIDEO_CODEC`;`auto` 或留空则落到环境变量,再落到 auto(有 NVIDIA 时 NVENC 优先,失败再 CPU;没有 NVIDIA 时就是 CPU,不探测;codec 仍是硬件优先 H.265、CPU 用 H.264)。探测在启动时后台进行,不挡住第一路出图。成功结果缓存到进程退出,但 NVIDIA 是否存在(`/proc/driver/nvidia/version` 或 `/dev/nvidia0`)与报告不一致时,下一次桌面连接会在后台再探测一次(`warm_auto_probe`),这一路仍用缓存的编码器,直到新结果写入。NVENC 失败只缓存 10 分钟,到期后的桌面连接同样在后台再探测一次,这一路仍用当前结果。`set-encoder` 和 `set-codec` 写完后若 `host.pid` 指向正在运行的本程序,都会发 `SIGUSR2`:编码器变化时进行中的桌面流立刻换编码器(不断开,沿用本会话已协商的 codec;新编码器起不来时按进程内 → FFmpeg 进程 → CPU 回退)。手改文件后可 `kill -USR2 $(cat ~/.config/uuremote/host.pid)`。`SIGUSR2` 不重新探测。codec 偏好仍在下次建立桌面连接时生效(运行中的 host 只记日志,不重开编码)。显式 `vaapi` 请求真彩时仍用 VAAPI、编码 4:2:0,并在 CaptureSettingSync 的 field 3 回报 4:2:0。所选编码器在本机不可用时,建流回退 CPU 并记 warning。VAAPI 进入 auto 要等 #33。
|
||||
|
||||
自定义码率按客户端选择的 Mbps 生效,例如 1 Mbps 对应 1000 kbit/s 的编码上限。
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
运行中切换编码器不再断开连接;X11 双屏/多屏可以只看其中一块屏幕(实验性)。/ Switch the encoder without dropping the session; select one monitor on X11 (experimental).
|
||||
|
||||
### 新增 / Added
|
||||
- **运行中切换编码器(#85 第③步,#96,感谢 @ferstar)**:`uuremote-server set-encoder auto|cpu|vaapi|nvenc` 和 `set-codec` 写入设置后会通知正在运行的宿主(`SIGUSR2`),
|
||||
已连接的桌面在下一帧边界换用新编码器,不用断开重连;VAAPI 不支持真彩 4:4:4 时自动降为 4:2:0 并通知客户端。只通知完整路径相同的宿主程序,
|
||||
避免误杀旧版本宿主;没有通知到宿主时会打印提示(下次连接或重启服务后生效)。编码格式(H.264/H.265)偏好仍在下次连接时生效。
|
||||
- **X11 单屏选择(#98,实验性)**:设置 `UU_X11_MULTISCREEN=1` 后,主机按 `xrandr --listmonitors` 列出各显示器,控制端可选择其中一块,只采集该区域,
|
||||
鼠标绝对坐标加上该屏幕的偏移;默认主显示器,`UU_X11_OUTPUT` 可指定其他屏幕。显示器布局在启动时读取,改动后需重启服务;此模式下「分辨率」改为缩放画面。默认关闭,单屏用户不受影响。
|
||||
- `set-encoder` / `set-codec` now switch a live desktop stream's encoder via `SIGUSR2` (same binary only) without reconnecting.
|
||||
`UU_X11_MULTISCREEN=1` lets the viewer pick one X11 monitor (RandR monitors; capture and absolute input follow the selected monitor).
|
||||
|
||||
### 其他 / Other
|
||||
- Issue 模板的诊断命令:保留连接状态和端口映射日志、过滤 WebRTC 内部噪音,并抹掉 IPv6 和 MAC 地址。
|
||||
|
||||
### 验证范围 / Validation
|
||||
运行中切换编码器:单元测试与 CI,官方客户端下的切换时序尚未实机验证。X11 单屏选择:单元测试;在 Debian 13 上用两块 RandR 虚拟显示器实测,
|
||||
鼠标落点与按区域采集正确;官方客户端在真实双屏上的切换尚未实机验证,欢迎反馈。
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux_0.2.29_<amd64|arm64>.deb`,或 `uu-remote-linux-v0.2.29-<x86_64|aarch64>-linux.tar.gz`(解压后运行 `./install.sh`),或 `docker pull ghcr.io/vallierdev/uu-remote-linux:v0.2.29`(x86_64)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -56,6 +56,12 @@ pub fn x11_size() -> Option<(u32, u32)> {
|
||||
}
|
||||
|
||||
/// The root size from the last refresh, without an X round trip (pointer mapping).
|
||||
/// Display the X11 engine opened, once a capture has initialized it.
|
||||
pub fn x11_display() -> Option<String> {
|
||||
let name = unsafe { x11_display_name() };
|
||||
if name.is_null() { None } else { Some(unsafe { std::ffi::CStr::from_ptr(name) }.to_string_lossy().into_owned()) }
|
||||
}
|
||||
|
||||
pub fn x11_last_size() -> Option<(u32, u32)> {
|
||||
let (mut w, mut h): (libc::c_int, libc::c_int) = (0, 0);
|
||||
if unsafe { x11_get_dimensions(&mut w, &mut h) } != 0 || w <= 0 || h <= 0 { return None; }
|
||||
|
||||
+29
-1
@@ -334,9 +334,12 @@ impl InputInjector {
|
||||
let result=crate::wayland::move_pointer(x,y,false);
|
||||
if result!=-2 { return result; }
|
||||
}
|
||||
// X11 multi-screen: the point is inside the selected monitor; XTest and uinput span the root.
|
||||
let placement=if relative || self.backend!=crate::desktop_backend::DesktopBackend::X11 {None} else {crate::wayland::x11_placement()};
|
||||
let (x,y)=placement.map_or((x,y),|(ox,oy)|(x+ox,y+oy));
|
||||
if let Some(uinput)=&self.uinput {
|
||||
if relative { return Self::uinput_result(uinput.move_relative(x,y)); }
|
||||
let (w,h)=*self.screen.lock().unwrap();
|
||||
let (w,h)=placement.and_then(|_|crate::capture::x11_last_size()).unwrap_or(*self.screen.lock().unwrap());
|
||||
if w > 1 && h > 1 {
|
||||
let scale = |v: i32, extent: u32| (i64::from(v) * i64::from(crate::uinput::ABS_MAX) / i64::from(extent - 1)) as i32;
|
||||
return Self::uinput_result(uinput.move_absolute(scale(x,w),scale(y,h)));
|
||||
@@ -700,3 +703,28 @@ mod output_pointer_tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test,target_os="linux"))]
|
||||
mod x11_monitor_pointer_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
#[ignore="requires an X11 display with two or more RandR monitors and UU_X11_MULTISCREEN=1; moves pointer only"]
|
||||
fn absolute_pointer_lands_on_the_selected_x11_monitor() {
|
||||
let _capture=crate::capture::ScreenCapture::new().unwrap();
|
||||
let input=InputInjector::new();
|
||||
assert_eq!(input.backend,crate::desktop_backend::DesktopBackend::X11);
|
||||
let outputs=crate::wayland::outputs();assert!(crate::wayland::x11_multiscreen() && outputs.len()>=2,"needs two X11 monitors");
|
||||
for (id,output) in outputs.iter().enumerate().chain(outputs.iter().enumerate().rev()) {
|
||||
crate::wayland::select(id).unwrap();
|
||||
for (x,y) in [(0.0,0.0),(0.5,0.5),(0.999,0.999),(0.1,0.9)] {
|
||||
let size=(output.width,output.height);
|
||||
let pixels=crate::input_mapping::normalized_pointer(x,y,size.0,size.1).unwrap();
|
||||
input.mouse_absolute_normalized(x,y,size);input.flush_pointer();
|
||||
let (mut ax,mut ay,mut mask)=(0,0,0);
|
||||
assert_eq!(unsafe {x11_pointer_state(&mut ax,&mut ay,&mut mask)},0);
|
||||
assert_eq!((ax,ay),(output.x+pixels.0,output.y+pixels.1),"monitor={} normalized=({x},{y})",output.name);
|
||||
}
|
||||
}
|
||||
crate::wayland::select(0).unwrap();
|
||||
}
|
||||
}
|
||||
+108
-6
@@ -144,10 +144,46 @@ fn set_video_pref() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let selection = video_select::selection();
|
||||
let codec = match selection.codec_pref { video_select::CodecPref::Auto => "auto", video_select::CodecPref::H264 => "h264", video_select::CodecPref::H265 => "h265" };
|
||||
println!("encoder={:?} source={} codec_pref={codec} source={}", selection.encoder, video_select::encoder_source_label(selection.encoder_source), video_select::source_label(selection.codec_source));
|
||||
println!("takes effect the next time a desktop connection starts");
|
||||
// Both subcommands wake the host. Encoder changes restart live desktop streams; codec changes are logged only.
|
||||
match notify_host(libc::SIGUSR2) {
|
||||
Ok(()) => println!("notified the running host"),
|
||||
Err(_) => println!(
|
||||
"host not running, or the running host is not this binary: takes effect the next time a desktop connection starts, or after restarting the service"
|
||||
),
|
||||
}
|
||||
if which == "set-codec" {
|
||||
println!("takes effect the next time a desktop connection starts");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Signal the host recorded in host.pid only when `/proc/<pid>/exe` is exactly
|
||||
/// `canonicalize(current_exe())`. A filename match is not enough: an older host
|
||||
/// has no SIGUSR2 handler, and the default action terminates the process.
|
||||
/// A replaced binary reads back as `… (deleted)` and does not match.
|
||||
fn notify_host(signal: i32) -> Result<(), ()> {
|
||||
let pid = std::fs::read_to_string(host_pid_path())
|
||||
.ok()
|
||||
.and_then(|s| s.trim().parse::<i32>().ok())
|
||||
.filter(|p| *p > 1)
|
||||
.ok_or(())?;
|
||||
let exe = std::fs::read_link(format!("/proc/{pid}/exe")).map_err(|_| ())?;
|
||||
let own = std::fs::canonicalize(std::env::current_exe().map_err(|_| ())?).map_err(|_| ())?;
|
||||
if !same_host_binary(&exe, &own) {
|
||||
return Err(());
|
||||
}
|
||||
if unsafe { libc::kill(pid, signal) } == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
/// `/proc/<pid>/exe` must equal the canonical path of this binary. A trailing ` (deleted)` does not match.
|
||||
fn same_host_binary(proc_exe: &std::path::Path, own: &std::path::Path) -> bool {
|
||||
proc_exe == own
|
||||
}
|
||||
|
||||
/// Explicit command words for set-encoder / set-codec. Empty and whitespace are illegal.
|
||||
pub(crate) fn cli_pref_ok(which: &str, value: &str) -> bool {
|
||||
let value = value.trim();
|
||||
@@ -196,13 +232,75 @@ mod cli_exit_tests {
|
||||
assert_eq!(super::reject_cli(Some("set-encoder"), 3, super::cli_pref_ok("set-encoder", "")), Some(2));
|
||||
assert_eq!(super::reject_cli(Some("set-codec"), 3, super::cli_pref_ok("set-codec", " ")), Some(2));
|
||||
}
|
||||
|
||||
/// SIGUSR1 and SIGUSR2 are installed with no assist UI. `host.pid` is written only after both exist.
|
||||
#[test]
|
||||
fn signal_handlers_install_without_assist() {
|
||||
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
|
||||
rt.block_on(async {
|
||||
let _guard = crate::config::test_env_lock().lock().unwrap();
|
||||
let dir =
|
||||
std::env::temp_dir().join(format!("uuremote-signal-test-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::env::set_var("UU_CONFIG", dir.join("config.json"));
|
||||
assert!(super::install_host_signals(None));
|
||||
let pid = std::fs::read_to_string(super::host_pid_path()).unwrap();
|
||||
assert_eq!(pid.trim(), std::process::id().to_string());
|
||||
std::env::remove_var("UU_CONFIG");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sigusr2_requires_the_same_canonical_binary() {
|
||||
let own = std::path::Path::new("/home/user/.local/bin/uuremote-server");
|
||||
assert!(super::same_host_binary(own, own));
|
||||
assert!(!super::same_host_binary(
|
||||
std::path::Path::new("/tmp/unpack/uuremote-server"),
|
||||
own
|
||||
));
|
||||
assert!(!super::same_host_binary(
|
||||
std::path::Path::new("/home/user/.local/bin/uuremote-server (deleted)"),
|
||||
own
|
||||
));
|
||||
}
|
||||
}
|
||||
/// 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 };
|
||||
/// SIGUSR2 (set-encoder, set-codec, or `kill -USR2`) re-reads host_settings. Encoder changes switch live desktop streams; codec changes are logged for the next connection.
|
||||
/// The default action of either signal terminates the process, so both handlers are installed before `host.pid` is written.
|
||||
/// SIGUSR2 is independent of remote assistance: `UU_REMOTE_ASSIST=0` and a failed assist setup still switch live encoders.
|
||||
/// SIGUSR1 is installed first as a no-op so `show` cannot terminate a host that has no assist window yet.
|
||||
fn install_host_signals(ui: Option<std::sync::Arc<assist::LocalUi>>) -> bool {
|
||||
static USR1: std::sync::Once = std::sync::Once::new();
|
||||
static USR2: std::sync::Once = std::sync::Once::new();
|
||||
let mut usr1_ok = true;
|
||||
USR1.call_once(|| {
|
||||
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) {
|
||||
Ok(mut usr1) => { tokio::spawn(async move { while usr1.recv().await.is_some() {} }); }
|
||||
Err(_) => usr1_ok = false,
|
||||
}
|
||||
});
|
||||
if !usr1_ok { return false; }
|
||||
let mut usr2_ok = true;
|
||||
USR2.call_once(|| {
|
||||
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined2()) {
|
||||
Ok(mut usr2) => { tokio::spawn(async move { while usr2.recv().await.is_some() { crate::video_select::publish_reload(); } }); }
|
||||
Err(_) => usr2_ok = false,
|
||||
}
|
||||
});
|
||||
if !usr2_ok { return false; }
|
||||
// Assist attaches later and replaces the no-op with the window. A second registration replaces the first.
|
||||
if let Some(ui) = ui {
|
||||
if let Ok(mut usr1) = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::user_defined1()) {
|
||||
tokio::spawn(async move { while usr1.recv().await.is_some() { ui.present(); } });
|
||||
}
|
||||
}
|
||||
write_host_pid();
|
||||
tokio::spawn(async move { while usr1.recv().await.is_some() { ui.present(); } });
|
||||
true
|
||||
}
|
||||
|
||||
fn present_on_usr1(ui: std::sync::Arc<assist::LocalUi>) {
|
||||
let _ = install_host_signals(Some(ui));
|
||||
}
|
||||
|
||||
fn host_pid_path() -> std::path::PathBuf { config::path().with_file_name("host.pid") }
|
||||
@@ -210,7 +308,9 @@ fn host_pid_path() -> std::path::PathBuf { config::path().with_file_name("host.p
|
||||
fn write_host_pid() {
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::io::Write;
|
||||
if let Ok(mut f) = std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(host_pid_path()) {
|
||||
let path = host_pid_path();
|
||||
if let Some(dir) = path.parent() { let _ = std::fs::create_dir_all(dir); }
|
||||
if let Ok(mut f) = std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(path) {
|
||||
let _ = write!(f, "{}", std::process::id());
|
||||
}
|
||||
}
|
||||
@@ -521,6 +621,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
virtual_audio::init();
|
||||
let _audio_cleanup=virtual_audio::Cleanup;
|
||||
log::info!("Starting uu-remote-linux host v{}...", env!("CARGO_PKG_VERSION"));
|
||||
// Before host.pid, and before assist: a missing or failed assist session must still handle SIGUSR2.
|
||||
let _ = install_host_signals(None);
|
||||
video::warm_auto_probe();
|
||||
video::check_ffmpeg_usable();
|
||||
|
||||
|
||||
+65
-11
@@ -814,10 +814,12 @@ async fn read_encoder_with(
|
||||
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
|
||||
selection: crate::video_select::Selection,
|
||||
)->Result<(),String>{
|
||||
let selected_output=if display.starts_with("wayland:") {crate::wayland::source(settings.source_id)} else {None};
|
||||
let selected_output=crate::wayland::capture_source(&display,portal.is_some(),settings.source_id);
|
||||
let size=selected_output.map_or(size,|s|(s.width,s.height));
|
||||
let kms=display.starts_with("kms:");
|
||||
let wayland=kms || display.starts_with("wayland:");
|
||||
// X11 multi-screen: x11grab reads only the selected monitor's region of the root.
|
||||
let x11_region=selected_output.filter(|_|!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")??;
|
||||
@@ -825,7 +827,7 @@ async fn read_encoder_with(
|
||||
Some(Arc::new(capture))
|
||||
} else { None };
|
||||
#[cfg(feature = "inproc-av")]
|
||||
if allow_inproc && native_inproc(&display, portal.is_some(), encoder) {
|
||||
if allow_inproc && x11_region.is_none() && native_inproc(&display, portal.is_some(), encoder) {
|
||||
let max_kbps = settings.max_kbps().unwrap_or(30_000) as i64;
|
||||
let config = crate::inproc_encoder::Config {
|
||||
display: display.clone(), input: size, output: settings.output, fps: u32::from(settings.fps),
|
||||
@@ -859,8 +861,9 @@ async fn read_encoder_with(
|
||||
"-framerate",&fps,"-i","pipe:0","-an"]);
|
||||
} else {
|
||||
// The viewer draws its local pointer; do not bake a second into video.
|
||||
let input=x11_region.map_or_else(||display.clone(),|o|format!("{display}+{},{}",o.x,o.y));
|
||||
command.args(["-f","x11grab","-draw_mouse","0","-framerate",&fps,
|
||||
"-video_size",&size,"-i",&display,"-an"]);
|
||||
"-video_size",&size,"-i",&input,"-an"]);
|
||||
}
|
||||
// 其他分辨率: scale the captured desktop (the host display mode is left untouched).
|
||||
command.args(scale_options(&settings, encoder, Some(source_size)));
|
||||
@@ -1140,9 +1143,16 @@ fn keep_stream_display_awake() {
|
||||
if result.is_err() { log::warn!("Display wake failed; continuing desktop stream and retrying next interval"); }
|
||||
}
|
||||
|
||||
pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_track::VideoTrack>, mut size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||
/// `sync_tx` receives a Message 25 payload when a live encoder switch changes the encoded chroma.
|
||||
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>>,
|
||||
settings: tokio::sync::watch::Receiver<VideoSettings>, applied: Arc<tokio::sync::watch::Sender<VideoSettings>>, idr: Arc<AtomicBool>,
|
||||
selection: crate::video_select::Selection, sync_tx: Option<tokio::sync::mpsc::UnboundedSender<Vec<u8>>>) -> Result<(), String> {
|
||||
stream_desktop_at(pc, track, size, display, portal, settings, applied, idr, selection, sync_tx, crate::video_select::reload_generation_value()).await
|
||||
}
|
||||
|
||||
pub async fn stream_desktop_at(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_track::VideoTrack>, mut 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>,
|
||||
selection: crate::video_select::Selection) -> Result<(), String> {
|
||||
selection: crate::video_select::Selection, sync_tx: Option<tokio::sync::mpsc::UnboundedSender<Vec<u8>>>, reload_at_session: u64) -> Result<(), String> {
|
||||
let _desktop_mode = crate::desktop_mode::StreamGuard::new();
|
||||
loop {
|
||||
if transport_closed(&pc) { return Ok(()); }
|
||||
@@ -1163,7 +1173,14 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
applied.send_replace(current);
|
||||
settings.borrow_and_update();
|
||||
}
|
||||
// Reader restarts reuse this snapshot, so set-encoder waits for the next desktop connection.
|
||||
// SIGUSR2 re-reads host_settings and restarts this encoder when the effective encoder changes.
|
||||
// The negotiated codec stays; a codec-only change is logged and applied on the next connection.
|
||||
let mut live_selection = selection;
|
||||
let mut seen_codec_pref = selection.codec_pref;
|
||||
let mut reload = crate::video_select::subscribe_reload();
|
||||
// Only a SIGUSR2 since the session snapshot. A probe that finishes during setup is not a reload
|
||||
// (sessions started before the probe keep the legacy encoder for their lifetime).
|
||||
let mut reload_pending = crate::video_select::reload_since_session(reload_at_session, *reload.borrow());
|
||||
let open = {
|
||||
let display = display.clone();
|
||||
let portal = portal.clone();
|
||||
@@ -1175,7 +1192,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
read_encoder(size, display.clone(), portal.clone(), latest.clone(), settings, sequence.clone(), idr.clone(), budget.clone(), selection)
|
||||
}
|
||||
};
|
||||
let mut reader = EncoderTask(tokio::spawn(open(size, selection, current)));
|
||||
let mut reader = EncoderTask(tokio::spawn(open(size, live_selection, current)));
|
||||
let mut previous = None;
|
||||
let mut timeline = RtpTimeline { fps: u64::from(current.fps), ..Default::default() };
|
||||
let mut settings_open = true;
|
||||
@@ -1190,6 +1207,43 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
let (mut window_frames, mut window_skipped) = (0u64, 0u64);
|
||||
let mut send_interrupted=false;
|
||||
'stream: loop {
|
||||
if reload.has_changed().unwrap_or(false) || reload_pending {
|
||||
reload_pending = false;
|
||||
// Mark the watch seen. An unconsumed has_changed stays true and restarts every frame.
|
||||
let _ = reload.borrow_and_update();
|
||||
let fresh = crate::video_select::selection();
|
||||
let plan = crate::video_select::plan_switch(live_selection.encoder, seen_codec_pref, &fresh, current.chroma);
|
||||
if let Some(plan) = plan.filter(|plan| plan.encoder.is_some()) {
|
||||
let encoder = plan.encoder.unwrap();
|
||||
let chroma = if plan.chroma_downgraded { "420" } else if current.chroma == crate::capture_setting::CHROMA_444 { "444" } else { "420" };
|
||||
log::info!("Encoder switch: {} -> {} reason={} chroma={chroma}",
|
||||
format!("{:?}", live_selection.encoder).to_ascii_lowercase(),
|
||||
format!("{encoder:?}").to_ascii_lowercase(),
|
||||
crate::video_select::encoder_source_label(fresh.encoder_source));
|
||||
live_selection.encoder = encoder;
|
||||
live_selection.encoder_source = fresh.encoder_source;
|
||||
seen_codec_pref = fresh.codec_pref;
|
||||
if plan.chroma_downgraded {
|
||||
applied.send_modify(|value| value.chroma = plan.chroma);
|
||||
current.chroma = plan.chroma;
|
||||
settings.borrow_and_update();
|
||||
if let Some(sync) = sync_tx.as_ref() {
|
||||
// Wayland: x11_resized is None, and the connection size can be a previous output.
|
||||
let native = x11_resized(&display, portal_stream, size).unwrap_or_else(|| crate::wayland::sync_native(&display, current.source_id, size));
|
||||
let payload = crate::capture_setting::sync_payload(¤t, native);
|
||||
let _ = sync.send(payload);
|
||||
}
|
||||
}
|
||||
// In-process engines honor this on the first frame; the process pipeline starts on an IDR.
|
||||
idr.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
reader.restart(open(size, live_selection, current)).await;
|
||||
} else if plan.is_some() {
|
||||
log::info!("Codec preference changed; takes effect the next time a desktop connection starts");
|
||||
seen_codec_pref = fresh.codec_pref;
|
||||
live_selection.codec_pref = fresh.codec_pref;
|
||||
live_selection.codec_source = fresh.codec_source;
|
||||
}
|
||||
}
|
||||
let next = *settings.borrow_and_update();
|
||||
if next != current || send_interrupted {
|
||||
let discard = next.codec != current.codec || next.source_generation != current.source_generation || next.source_id != current.source_id || send_interrupted;
|
||||
@@ -1201,9 +1255,9 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
reader.0.abort();
|
||||
let _ = (&mut reader.0).await;
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(open(size, selection, next));
|
||||
reader.0 = tokio::spawn(open(size, live_selection, next));
|
||||
} else {
|
||||
reader.restart(open(size, selection, next)).await;
|
||||
reader.restart(open(size, live_selection, next)).await;
|
||||
}
|
||||
send_interrupted=false;
|
||||
}
|
||||
@@ -1214,7 +1268,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
log::info!("Desktop geometry changed during the stream ({}x{} -> {}x{}); restarting capture", size.0, size.1, resized.0, resized.1);
|
||||
size = resized;
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(open(size, selection, current));
|
||||
reader.0 = tokio::spawn(open(size, live_selection, current));
|
||||
continue;
|
||||
}
|
||||
if let (Err(error), VideoCodec::H265, true) = (&result, current.codec, track.supports(VideoCodec::H264)) {
|
||||
@@ -1222,7 +1276,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
applied.send_modify(|value| value.codec = VideoCodec::H264);
|
||||
current = *settings.borrow_and_update();
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(open(size, selection, current));
|
||||
reader.0 = tokio::spawn(open(size, live_selection, current));
|
||||
continue;
|
||||
}
|
||||
if result.is_err() && !crate::wayland::outputs().is_empty() && !crate::wayland::input_ready() {
|
||||
|
||||
@@ -234,11 +234,103 @@ pub fn cached_probe() -> Option<ProbeReport> {
|
||||
#[cfg(test)]
|
||||
pub(crate) static PROBE_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
/// Tests that read or bump the process-global reload generation share this lock.
|
||||
#[cfg(test)]
|
||||
pub(crate) static RELOAD_TEST_LOCK:std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn clear_probe() {
|
||||
if let Ok(mut slot) = cache().lock() { *slot = None; }
|
||||
}
|
||||
|
||||
/// Why a live desktop encoder was asked to change.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum SwitchReason {
|
||||
/// Effective encoder differs. The log line uses `Selection::encoder_source`, not this variant.
|
||||
Encoder,
|
||||
/// Codec preference changed. The negotiated codec stays until the next stream.
|
||||
CodecDeferred,
|
||||
}
|
||||
|
||||
/// What SIGUSR2 should do to one live desktop stream.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct SwitchPlan {
|
||||
/// Restart the encoder. None keeps the running encoder (codec-only or no change).
|
||||
pub encoder: Option<Encoder>,
|
||||
pub reason: SwitchReason,
|
||||
/// Chroma the restarted encoder can actually produce. VAAPI has no 4:4:4.
|
||||
pub chroma: u64,
|
||||
/// True when the request was 4:4:4 and the new encoder must report 4:2:0.
|
||||
pub chroma_downgraded: bool,
|
||||
}
|
||||
|
||||
/// Compare the selection this stream is using with a freshly read one.
|
||||
/// Codec preference is the stored preference, not the negotiated m-line codec.
|
||||
pub fn plan_switch(active_encoder: Encoder, previous_codec: CodecPref, next: &Selection, active_chroma: u64) -> Option<SwitchPlan> {
|
||||
if next.encoder != active_encoder {
|
||||
let chroma = if active_chroma == crate::capture_setting::CHROMA_444 && !crate::video::full_chroma_for(next.encoder) {
|
||||
crate::capture_setting::CHROMA_420
|
||||
} else {
|
||||
active_chroma
|
||||
};
|
||||
return Some(SwitchPlan {
|
||||
encoder: Some(next.encoder),
|
||||
reason: SwitchReason::Encoder,
|
||||
chroma,
|
||||
chroma_downgraded: chroma != active_chroma,
|
||||
});
|
||||
}
|
||||
if next.codec_pref != previous_codec {
|
||||
return Some(SwitchPlan {
|
||||
encoder: None,
|
||||
reason: SwitchReason::CodecDeferred,
|
||||
chroma: active_chroma,
|
||||
chroma_downgraded: false,
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Monotonic generation bumped by SIGUSR2. Streams watch it and re-read `selection()`.
|
||||
fn reload_generation() -> &'static std::sync::atomic::AtomicU64 {
|
||||
static GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
||||
&GEN
|
||||
}
|
||||
|
||||
pub fn reload_generation_value() -> u64 {
|
||||
reload_generation().load(std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// SIGUSR2: ask live desktop streams to re-read host_settings. Does not clear the probe cache.
|
||||
pub fn notify_reload() {
|
||||
reload_generation().fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn reload_watch() -> &'static tokio::sync::watch::Sender<u64> {
|
||||
static WATCH: std::sync::OnceLock<tokio::sync::watch::Sender<u64>> = std::sync::OnceLock::new();
|
||||
WATCH.get_or_init(|| {
|
||||
let (tx, _) = tokio::sync::watch::channel(reload_generation_value());
|
||||
tx
|
||||
})
|
||||
}
|
||||
|
||||
pub fn subscribe_reload() -> tokio::sync::watch::Receiver<u64> {
|
||||
reload_watch().subscribe()
|
||||
}
|
||||
|
||||
/// True when SIGUSR2 has bumped the generation since the session snapshot.
|
||||
/// A background auto probe that finishes during setup does not bump it.
|
||||
pub fn reload_since_session(at_session: u64, current: u64) -> bool {
|
||||
current != at_session
|
||||
}
|
||||
|
||||
/// Publish the generation so every subscribed desktop stream wakes.
|
||||
pub fn publish_reload() {
|
||||
notify_reload();
|
||||
// send() drops the value when nobody is subscribed (the gap between desktop connections).
|
||||
reload_watch().send_replace(reload_generation_value());
|
||||
}
|
||||
|
||||
/// Setting (non-auto) beats the environment, which beats the NVIDIA/CPU default.
|
||||
pub fn selection() -> Selection {
|
||||
selection_with(cached_probe().as_ref())
|
||||
@@ -486,6 +578,78 @@ mod tests {
|
||||
assert_eq!(resolve_pair(None, None, None, Some("h264"), false, Some(&va)).codec_pref, CodecPref::H264);
|
||||
}
|
||||
|
||||
fn selection_of(encoder: Encoder, codec: CodecPref) -> Selection {
|
||||
Selection { encoder, encoder_source: Source::Setting, codec_pref: codec, codec_source: Source::Setting }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_switch_restarts_encoder_and_defers_codec() {
|
||||
let nv = selection_of(Encoder::Nvenc, CodecPref::Auto);
|
||||
assert!(plan_switch(Encoder::Nvenc, CodecPref::Auto, &nv, crate::capture_setting::CHROMA_420).is_none());
|
||||
let to_cpu = plan_switch(Encoder::Nvenc, CodecPref::Auto, &selection_of(Encoder::Cpu, CodecPref::Auto), crate::capture_setting::CHROMA_444).unwrap();
|
||||
assert_eq!(to_cpu.encoder, Some(Encoder::Cpu));
|
||||
assert_eq!(to_cpu.reason, SwitchReason::Encoder);
|
||||
assert!(!to_cpu.chroma_downgraded);
|
||||
assert_eq!(to_cpu.chroma, crate::capture_setting::CHROMA_444);
|
||||
let to_vaapi = plan_switch(Encoder::Nvenc, CodecPref::H265, &selection_of(Encoder::Vaapi, CodecPref::H265), crate::capture_setting::CHROMA_444).unwrap();
|
||||
assert_eq!(to_vaapi.encoder, Some(Encoder::Vaapi));
|
||||
assert!(to_vaapi.chroma_downgraded);
|
||||
assert_eq!(to_vaapi.chroma, crate::capture_setting::CHROMA_420);
|
||||
let codec = plan_switch(Encoder::Nvenc, CodecPref::Auto, &selection_of(Encoder::Nvenc, CodecPref::H264), crate::capture_setting::CHROMA_420).unwrap();
|
||||
assert_eq!(codec.encoder, None);
|
||||
assert_eq!(codec.reason, SwitchReason::CodecDeferred);
|
||||
let both = plan_switch(Encoder::Cpu, CodecPref::Auto, &selection_of(Encoder::Nvenc, CodecPref::H264), crate::capture_setting::CHROMA_420).unwrap();
|
||||
assert_eq!(both.encoder, Some(Encoder::Nvenc));
|
||||
assert_eq!(both.reason, SwitchReason::Encoder);
|
||||
// The log reason is the source of the new selection (setting / env / auto), not a fixed word.
|
||||
for (source, label) in [(Source::Setting, "setting"), (Source::Env, "env"), (Source::Auto, "auto")] {
|
||||
let mut next = selection_of(Encoder::Vaapi, CodecPref::Auto);
|
||||
next.encoder_source = source;
|
||||
let plan = plan_switch(Encoder::Nvenc, CodecPref::Auto, &next, crate::capture_setting::CHROMA_420).unwrap();
|
||||
assert!(plan.encoder.is_some());
|
||||
assert_eq!(encoder_source_label(next.encoder_source), label);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reload_does_not_clear_the_probe_cache() {
|
||||
let _reload = RELOAD_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let _guard = PROBE_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let report = probe_report(true, &[ProbeOutcome::Ok, ProbeOutcome::Ok]);
|
||||
store_probe(report.clone());
|
||||
let before = reload_generation_value();
|
||||
notify_reload();
|
||||
assert_eq!(reload_generation_value(), before + 1);
|
||||
assert_eq!(cached_probe(), Some(report));
|
||||
assert!(reload_since_session(before, reload_generation_value()));
|
||||
assert!(!reload_since_session(reload_generation_value(), reload_generation_value()));
|
||||
clear_probe();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn probe_finish_is_not_a_reload_since_the_session_snapshot() {
|
||||
let _reload = RELOAD_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let at_session = reload_generation_value();
|
||||
// A finished background probe updates the cache only. It does not bump the generation.
|
||||
assert!(!reload_since_session(at_session, reload_generation_value()));
|
||||
notify_reload();
|
||||
assert!(reload_since_session(at_session, reload_generation_value()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mark_reload_seen_clears_has_changed() {
|
||||
let _reload = RELOAD_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
|
||||
rt.block_on(async {
|
||||
let mut watch = subscribe_reload();
|
||||
let _ = watch.borrow_and_update();
|
||||
publish_reload();
|
||||
assert!(watch.has_changed().unwrap());
|
||||
let _ = watch.borrow_and_update();
|
||||
assert!(!watch.has_changed().unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
/// Codec at CONTROL open is `preferred_codec_for` on the session snapshot.
|
||||
/// Auto + CPU uses H.264 when both codecs are negotiated; auto + NVENC keeps H.265.
|
||||
/// Forced codec ignores the encoder. 真彩 does not switch codec (true_color_keeps_codec_and_vaapi_reports_420).
|
||||
|
||||
@@ -78,7 +78,7 @@ async fn exercise(fail_wake: usize, fail_capture: bool) {
|
||||
let (settings_tx, settings_rx) = tokio::sync::watch::channel(settings);
|
||||
let selection = crate::video_select::resolve_pair(Some("cpu"), None, Some("h264"), None, false, None);
|
||||
let mut task = tokio::spawn(stream_desktop(host.clone(), track.clone(), (640, 480),
|
||||
DISPLAY.into(), None, settings_rx, Arc::new(settings_tx), Arc::new(AtomicBool::new(false)), selection));
|
||||
DISPLAY.into(), None, settings_rx, Arc::new(settings_tx), Arc::new(AtomicBool::new(false)), selection, None));
|
||||
let result = if fail_capture {
|
||||
Some((&mut task).await.unwrap())
|
||||
} else {
|
||||
|
||||
+136
-9
@@ -204,11 +204,13 @@ pub fn text_probe()->Result<(),String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Opt-in single-track monitor switching for Hyprland. The existing native engine owns
|
||||
// capture/pointer binding; no compositor layout or user credentials are modified.
|
||||
// Opt-in single-track monitor switching: Hyprland outputs (UU_WAYLAND_MULTISCREEN) or X11 RandR
|
||||
// monitors (UU_X11_MULTISCREEN). The existing engines own capture/pointer binding; X11 crops the
|
||||
// root to the selected monitor. No compositor layout or user credentials are modified.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Output { pub name:String, pub width:u32, pub height:u32, pub fps:i32 }
|
||||
static OUTPUTS: std::sync::OnceLock<Vec<Output>> = std::sync::OnceLock::new();
|
||||
pub struct Output { pub name:String, pub width:u32, pub height:u32, pub fps:i32, pub x:i32, pub y:i32 }
|
||||
struct Inventory { outputs:Vec<Output>, x11:bool }
|
||||
static OUTPUTS: std::sync::OnceLock<Inventory> = std::sync::OnceLock::new();
|
||||
#[derive(Default)]
|
||||
struct Selection { id:usize, generation:u64, presented:Option<u64>, failed:bool }
|
||||
impl Selection {
|
||||
@@ -221,19 +223,77 @@ impl Selection {
|
||||
fn ready(&self)->bool {self.presented==Some(self.generation)}
|
||||
}
|
||||
static SELECTION:std::sync::Mutex<Selection>=std::sync::Mutex::new(Selection {id:0,generation:0,presented:None,failed:false});
|
||||
pub fn outputs() -> &'static [Output] {
|
||||
fn inventory() -> &'static Inventory {
|
||||
OUTPUTS.get_or_init(|| {
|
||||
let backend=crate::desktop_backend::detect().ok();
|
||||
if backend==Some(crate::desktop_backend::DesktopBackend::X11) && !crate::desktop_backend::headless()
|
||||
&& std::env::var("UU_X11_MULTISCREEN").ok().as_deref()==Some("1") {
|
||||
let outputs=x11_outputs();
|
||||
return Inventory {x11:!outputs.is_empty(),outputs};
|
||||
}
|
||||
if !cfg!(all(target_os="linux",feature="wayland"))
|
||||
|| std::env::var("UU_WAYLAND_MULTISCREEN").ok().as_deref()!=Some("1")
|
||||
|| crate::desktop_backend::detect().ok()!=Some(crate::desktop_backend::DesktopBackend::Wayland) {return Vec::new();}
|
||||
|| backend!=Some(crate::desktop_backend::DesktopBackend::Wayland) {return Inventory {outputs:Vec::new(),x11:false};}
|
||||
let output=std::process::Command::new("hyprctl").args(["monitors","-j"]).output();
|
||||
let result=output.map_err(|_|"hyprctl unavailable").and_then(|output| {
|
||||
if !output.status.success() {return Err("hyprctl failed");}
|
||||
parse_outputs(&output.stdout,&std::env::var("UU_WAYLAND_OUTPUT").unwrap_or_default())
|
||||
});
|
||||
match result {Ok(outputs)=>outputs,Err(reason)=>{log::warn!("Wayland multi-screen disabled: {}",reason);Vec::new()}}
|
||||
let outputs=match result {Ok(outputs)=>outputs,Err(reason)=>{log::warn!("Wayland multi-screen disabled: {}",reason);Vec::new()}};
|
||||
Inventory {outputs,x11:false}
|
||||
})
|
||||
}
|
||||
pub fn outputs() -> &'static [Output] {&inventory().outputs}
|
||||
/// X11 monitors are advertised: the stream captures the selected monitor's region of the root.
|
||||
pub fn x11_multiscreen()->bool {inventory().x11}
|
||||
/// Read once, like the Hyprland inventory: a changed monitor layout needs a host restart.
|
||||
fn x11_outputs()->Vec<Output> {
|
||||
let mut command=std::process::Command::new("xrandr");
|
||||
if let Some(display)=crate::capture::x11_display().or_else(||std::env::var("DISPLAY").ok()).filter(|d|!d.is_empty()) {
|
||||
command.arg("--display").arg(display);
|
||||
}
|
||||
let result=command.arg("--listmonitors").output().map_err(|_|"xrandr unavailable").and_then(|output| {
|
||||
if !output.status.success() {return Err("xrandr failed");}
|
||||
parse_x11_monitors(&String::from_utf8_lossy(&output.stdout),&std::env::var("UU_X11_OUTPUT").unwrap_or_default(),crate::video::display_fps() as i32)
|
||||
});
|
||||
match result {
|
||||
Ok(outputs) if outputs.len()>=2=>{log::info!("X11 multi-screen: {} monitors",outputs.len());outputs},
|
||||
Ok(_)=>{log::warn!("X11 multi-screen disabled: fewer than two monitors");Vec::new()},
|
||||
Err(reason)=>{log::warn!("X11 multi-screen disabled: {}",reason);Vec::new()},
|
||||
}
|
||||
}
|
||||
/// `xrandr --listmonitors`: ` 0: +*HDMI-0 1920/527x1080/296+1920+0 HDMI-0` (`*` = primary).
|
||||
/// The preferred monitor (UU_X11_OUTPUT, else the primary) is id 0; the rest go left to right.
|
||||
fn parse_x11_monitors(text:&str,preferred:&str,fps:i32)->Result<Vec<Output>,&'static str> {
|
||||
fn geometry(text:&str)->Option<(u32,u32,i32,i32)> {
|
||||
let (width,rest)=text.split_once('x')?;
|
||||
let split=rest.find('+')?;
|
||||
let (height,offsets)=rest.split_at(split);
|
||||
let mut offsets=offsets[1..].split('+');
|
||||
let size=|v:&str|v.split('/').next()?.parse::<u32>().ok();
|
||||
let (x,y)=(offsets.next()?.parse::<i32>().ok()?,offsets.next()?.parse::<i32>().ok()?);
|
||||
if offsets.next().is_some() || x<0 || y<0 {return None;}
|
||||
Some((size(width)?,size(height)?,x,y))
|
||||
}
|
||||
let mut seen=std::collections::HashSet::new();
|
||||
let mut primary=String::new();
|
||||
let mut outputs=Vec::new();
|
||||
for line in text.lines().skip(1) {
|
||||
let mut fields=line.split_whitespace();
|
||||
let (Some(_),Some(tag),Some(rect))=(fields.next(),fields.next(),fields.next()) else {continue};
|
||||
let name=tag.trim_start_matches(['+','*']);
|
||||
let Some((width,height,x,y))=geometry(rect) else {continue};
|
||||
if name.is_empty() || name.len()>127 || !seen.insert(name.to_owned())
|
||||
|| width<2 || height<2 || width>16384 || height>16384 || u64::from(width)*u64::from(height)*4>256*1024*1024 {continue;}
|
||||
if tag.contains('*') {primary=name.to_owned();}
|
||||
outputs.push(Output {name:name.into(),width,height,fps,x,y});
|
||||
}
|
||||
let first=if preferred.is_empty() {primary} else {preferred.to_owned()};
|
||||
outputs.sort_by_key(|m| (m.name!=first,m.x,m.y));
|
||||
outputs.truncate(16);
|
||||
if outputs.is_empty() || (!preferred.is_empty() && outputs[0].name!=preferred) {return Err("No capturable preferred output");}
|
||||
Ok(outputs)
|
||||
}
|
||||
fn parse_outputs(json:&[u8],preferred:&str)->Result<Vec<Output>,&'static str> {
|
||||
let monitors=serde_json::from_slice::<Vec<serde_json::Value>>(json).map_err(|_|"Invalid monitor inventory")?;
|
||||
let mut seen=std::collections::HashSet::new();
|
||||
@@ -244,7 +304,7 @@ fn parse_outputs(json:&[u8],preferred:&str)->Result<Vec<Output>,&'static str> {
|
||||
let height=u32::try_from(m["height"].as_u64()?).ok()?;
|
||||
if name.is_empty() || name.len()>127 || name.contains('\0') || !seen.insert(name.to_owned())
|
||||
|| width<2 || height<2 || width>16384 || height>16384 || u64::from(width)*u64::from(height)*4>256*1024*1024 {return None;}
|
||||
Some(Output {name:name.into(),width,height,fps:m["refreshRate"].as_f64()?.round().clamp(1.0,240.0) as i32})
|
||||
Some(Output {name:name.into(),width,height,fps:m["refreshRate"].as_f64()?.round().clamp(1.0,240.0) as i32,x:0,y:0})
|
||||
}).collect();
|
||||
outputs.sort_by_key(|m| (m.name!=preferred,m.name.clone()));
|
||||
outputs.truncate(16);
|
||||
@@ -255,6 +315,35 @@ pub fn selection()->(usize,u64) {let s=SELECTION.lock().unwrap();(s.id,s.generat
|
||||
pub fn selected()->usize {selection().0}
|
||||
pub fn source(id:usize)->Option<&'static Output> {outputs().get(id)}
|
||||
pub fn input_size(fallback:(u32,u32))->(u32,u32) {source(selected()).map_or(fallback,|s|(s.width,s.height))}
|
||||
/// Region the encoder captures for this display: the Wayland output, or the selected X11 monitor.
|
||||
pub fn capture_source(display:&str,portal:bool,id:usize)->Option<&'static Output> {
|
||||
if display.starts_with("wayland:") || (!portal && !display.starts_with("kms:") && x11_multiscreen()) {source(id)} else {None}
|
||||
}
|
||||
/// Offset of the selected X11 monitor in the root: absolute input is relative to that monitor.
|
||||
pub fn x11_placement()->Option<(i32,i32)> {
|
||||
if !x11_multiscreen() {return None;}
|
||||
source(selected()).map(|s|(s.x,s.y))
|
||||
}
|
||||
/// Pixel size of one advertised output. Unknown ids keep `fallback` (connection size).
|
||||
pub fn source_size(id:usize,fallback:(u32,u32))->(u32,u32) {
|
||||
#[cfg(test)]
|
||||
if let Some(outputs)=*test_outputs().lock().unwrap_or_else(|e|e.into_inner()) {
|
||||
return outputs.get(id).map_or(fallback,|s|(s.width,s.height));
|
||||
}
|
||||
source(id).map_or(fallback,|s|(s.width,s.height))
|
||||
}
|
||||
#[cfg(test)]
|
||||
fn test_outputs()->&'static std::sync::Mutex<Option<&'static [Output]>> {
|
||||
static SLOT:std::sync::OnceLock<std::sync::Mutex<Option<&'static [Output]>>>=std::sync::OnceLock::new();
|
||||
SLOT.get_or_init(||std::sync::Mutex::new(None))
|
||||
}
|
||||
/// Native size written into CaptureSettingSync. Wayland uses the output the encoder is reading
|
||||
/// (`source_id`), which can differ from the size captured when the connection opened; so does an
|
||||
/// X11 monitor with UU_X11_MULTISCREEN. Otherwise X11, portal and KMS keep the connection size
|
||||
/// (X11 mode changes go through `x11_resized`).
|
||||
pub fn sync_native(display:&str,source_id:usize,connection:(u32,u32))->(u32,u32) {
|
||||
if display.starts_with("wayland:") || (!display.starts_with("kms:") && x11_multiscreen()) {source_size(source_id,connection)} else {connection}
|
||||
}
|
||||
pub fn input_ready()->bool {outputs().is_empty() || SELECTION.lock().unwrap().ready()}
|
||||
pub fn switch_failed()->bool {SELECTION.lock().unwrap().failed}
|
||||
pub fn presented(id:usize,generation:u64) {SELECTION.lock().unwrap().sent(id,generation);}
|
||||
@@ -263,7 +352,12 @@ pub fn failed(id:usize,generation:u64) {
|
||||
if (state.id,state.generation)==(id,generation) {state.failed=true;}
|
||||
}
|
||||
pub fn select(id:usize)->Result<(),String> {
|
||||
let output=source(id).ok_or("Unknown Wayland screen")?;
|
||||
let output=source(id).ok_or("Unknown screen")?;
|
||||
if x11_multiscreen() {
|
||||
SELECTION.lock().unwrap().begin(id);
|
||||
log::info!("X11 screen selected: id={} source={}x{}+{}+{}",id,output.width,output.height,output.x,output.y);
|
||||
return Ok(());
|
||||
}
|
||||
#[cfg(all(target_os="linux",feature="wayland"))]
|
||||
{
|
||||
let name=std::ffi::CString::new(output.name.as_str()).map_err(|_|"Invalid output name")?;
|
||||
@@ -353,4 +447,37 @@ mod selection_regressions {
|
||||
assert_eq!(outputs.len(),1);assert_eq!(outputs[0].name,"normal");
|
||||
assert!(parse_outputs(b"not JSON","").is_err());
|
||||
}
|
||||
#[test]
|
||||
fn x11_monitors_keep_their_root_offsets() {
|
||||
// #98: GNOME/X11, two 1920x1080 monitors side by side, primary on the right.
|
||||
let text="Monitors: 2\n 0: +*HDMI-0 1920/527x1080/296+1920+0 HDMI-0\n 1: +HDMI-1 1920/521x1080/293+0+0 HDMI-1\n";
|
||||
let outputs=parse_x11_monitors(text,"",60).unwrap();
|
||||
assert_eq!(outputs.iter().map(|m|(m.name.as_str(),m.width,m.height,m.x,m.y)).collect::<Vec<_>>(),
|
||||
vec![("HDMI-0",1920,1080,1920,0),("HDMI-1",1920,1080,0,0)]);
|
||||
let outputs=parse_x11_monitors(text,"HDMI-1",60).unwrap();
|
||||
assert_eq!((outputs[0].name.as_str(),outputs[1].name.as_str()),("HDMI-1","HDMI-0"));
|
||||
assert!(parse_x11_monitors(text,"DP-9",60).is_err());
|
||||
// xrandr --setmonitor virtual monitors (output "none"); no primary: left to right.
|
||||
let virtual_monitors="Monitors: 3\n 0: +A 1280/338x1024/270+1280+0 none\n 1: +B 1280/338x1024/270+0+0 none\n 2: C 0/0x0/0+0+0 none\n";
|
||||
let outputs=parse_x11_monitors(virtual_monitors,"",30).unwrap();
|
||||
assert_eq!(outputs.iter().map(|m|(m.name.as_str(),m.x)).collect::<Vec<_>>(),vec![("B",0),("A",1280)]);
|
||||
assert!(parse_x11_monitors("Monitors: 1\n 0: +*X 1920/1x1080/1-10+0 X\n","",60).is_err());
|
||||
assert!(parse_x11_monitors("garbage","",60).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn sync_native_follows_the_wayland_source_id() {
|
||||
assert_eq!(sync_native(":0",1,(1920,1080)),(1920,1080));
|
||||
assert_eq!(sync_native("kms:/dev/dri/card0",1,(1920,1080)),(1920,1080));
|
||||
let inventory:&'static [Output]=Box::leak(vec![
|
||||
Output {name:"eDP-1".into(),width:1920,height:1080,fps:60,x:0,y:0},
|
||||
Output {name:"HDMI-A-1".into(),width:2560,height:1440,fps:75,x:0,y:0},
|
||||
].into_boxed_slice());
|
||||
*test_outputs().lock().unwrap()=Some(inventory);
|
||||
// Connection opened on the first output; the encoder is reading the second.
|
||||
assert_eq!(sync_native("wayland:wayland-0",1,(1920,1080)),(2560,1440));
|
||||
assert_eq!(source_size(0,(800,600)),(1920,1080));
|
||||
assert_eq!(sync_native("wayland:wayland-0",9,(800,600)),(800,600));
|
||||
assert_eq!(sync_native(":0",1,(1920,1080)),(1920,1080));
|
||||
*test_outputs().lock().unwrap()=None;
|
||||
}
|
||||
}
|
||||
+66
-11
@@ -65,6 +65,8 @@ impl WebRtcSession {
|
||||
|
||||
// Encoder, codec preference and 4:4:4 stay fixed for this peer (codec is negotiated at CONTROL open).
|
||||
// Read once before the HEVC probe so a concurrent set-encoder/set-codec cannot split capability from CONTROL.
|
||||
// reload_at_session is the generation at that snapshot. A probe that finishes later is not a reload.
|
||||
let reload_at_session = crate::video_select::reload_generation_value();
|
||||
let video_selection = crate::video_select::selection();
|
||||
let hevc = desktop && crate::video::hevc_available(video_selection.encoder).await;
|
||||
if hevc { crate::video_track::register_hevc(&mut m).map_err(|e| format!("Register HEVC failed: {e}"))?; }
|
||||
@@ -305,6 +307,10 @@ impl WebRtcSession {
|
||||
let portal=capture.portal.clone();
|
||||
let stream_desktop = portal_desktop.clone();
|
||||
let stream_selection = video_selection;
|
||||
// Live encoder switch (SIGUSR2) sends CaptureSettingSync when chroma is downgraded.
|
||||
let (chroma_sync_tx, chroma_sync_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||
let chroma_sync = (desktop && !cfg!(test)).then_some(chroma_sync_tx);
|
||||
let chroma_sync_rx = Arc::new(std::sync::Mutex::new((!cfg!(test)).then_some(chroma_sync_rx)));
|
||||
// File/terminal/port-mapping connections carry no desktop video.
|
||||
let control_video_track = video_track.clone();
|
||||
let stream_settings = video_settings.clone();
|
||||
@@ -330,7 +336,7 @@ impl WebRtcSession {
|
||||
let (capture_size, capture_display, portal) = screen.as_ref().map_or(
|
||||
(capture_size, capture_display, portal),
|
||||
|screen| ((screen.width, screen.height), screen.display.clone(), screen.portal.clone()));
|
||||
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, stream_settings, idr_request, stream_selection).await {
|
||||
if let Err(e) = crate::video::stream_desktop_at(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, stream_settings, idr_request, stream_selection, chroma_sync.clone(), reload_at_session).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);
|
||||
@@ -421,6 +427,7 @@ impl WebRtcSession {
|
||||
let peer_video_track = control_video_track.clone();
|
||||
let peer_actions = peer_actions.clone();
|
||||
let peer_selection = video_selection;
|
||||
let chroma_sync_rx = chroma_sync_rx.clone();
|
||||
if label == "FILE_DATA_CHANNEL" { *file_channel.lock().unwrap() = Some(d.clone()); }
|
||||
|
||||
Box::pin(async move {
|
||||
@@ -442,17 +449,47 @@ impl WebRtcSession {
|
||||
}));
|
||||
}
|
||||
// Ordered outbound queue for protocol replies produced outside this callback.
|
||||
let (outbound, mut outbound_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||
// webrtc-rs announces the channel before handle_open (ready_state Connecting);
|
||||
// send() returns ErrClosedPipe until Open. Draining CONTROL here would drop
|
||||
// Message 25 and every later reply. Start CONTROL from the on_open below
|
||||
// (one handler; a second on_open replaces it). Receiver taken once.
|
||||
let mut control_drain: Option<Box<dyn (FnOnce() -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>) + Send + Sync>> = None;
|
||||
let (outbound, outbound_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||
let outbound_rx = Arc::new(tokio::sync::Mutex::new(Some(outbound_rx)));
|
||||
if label == "CONTROL_DATA_CHANNEL" {
|
||||
if let Some(mut chroma_sync_rx) = chroma_sync_rx.lock().unwrap().take() {
|
||||
let sync_out = outbound.clone();
|
||||
let sync_seq = reply_sequence.clone();
|
||||
tokio::spawn(async move {
|
||||
while let Some(payload) = chroma_sync_rx.recv().await {
|
||||
let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
let seq = sync_seq.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
let _ = sync_out.send(crate::initialization::envelope(seq, timestamp, 25, &payload));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
{
|
||||
let out_channel = d.clone();
|
||||
let out_label = label.clone();
|
||||
tokio::spawn(async move {
|
||||
while let Some(message) = outbound_rx.recv().await {
|
||||
if let Err(e) = out_channel.send(&bytes::Bytes::from(message)).await {
|
||||
log::warn!("{} send failed: {}", out_label, e); break;
|
||||
}
|
||||
}
|
||||
});
|
||||
let outbound_rx = outbound_rx.clone();
|
||||
let start_drain = move || {
|
||||
let out_channel = out_channel.clone();
|
||||
let out_label = out_label.clone();
|
||||
let outbound_rx = outbound_rx.clone();
|
||||
Box::pin(async move {
|
||||
let Some(mut outbound_rx) = take_outbound_once(&outbound_rx).await else { return; };
|
||||
tokio::spawn(async move {
|
||||
while let Some(message) = outbound_rx.recv().await {
|
||||
if let Err(e) = out_channel.send(&bytes::Bytes::from(message)).await {
|
||||
log::warn!("{} send failed: {}", out_label, e); break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}) as std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>
|
||||
};
|
||||
if label != "CONTROL_DATA_CHANNEL" { tokio::spawn(start_drain()); }
|
||||
else { control_drain = Some(Box::new(start_drain)); }
|
||||
}
|
||||
let (capture_tx, mut capture_rx) = tokio::sync::mpsc::channel::<Vec<u8>>(16);
|
||||
if matches!(label.as_str(),"CONTROL_DATA_CHANNEL"|"TEXT_DATA_CHANNEL") {
|
||||
@@ -569,9 +606,11 @@ impl WebRtcSession {
|
||||
}) else { continue; };
|
||||
let Ok(rpc_fields) = crate::protocol::decode_wire(rpc) else { continue; };
|
||||
// X11: 分辨率 changes the display mode itself, like the official Windows host; the stream
|
||||
// then carries the new native size instead of a scaled one (desktop_mode).
|
||||
// then carries the new native size instead of a scaled one (desktop_mode). With
|
||||
// UU_X11_MULTISCREEN the monitor layout stays as it is and the stream is scaled.
|
||||
let resized = if desktop && active_screen.init_error.is_none() && active_screen.portal.is_none()
|
||||
&& active_screen.backend == crate::desktop_backend::DesktopBackend::X11 && crate::desktop_mode::enabled() {
|
||||
&& active_screen.backend == crate::desktop_backend::DesktopBackend::X11 && crate::desktop_mode::enabled()
|
||||
&& !crate::wayland::x11_multiscreen() {
|
||||
match crate::capture_setting::requested_resolution(&rpc_fields) {
|
||||
Some(request) => {
|
||||
let display = active_screen.display.clone();
|
||||
@@ -640,6 +679,7 @@ impl WebRtcSession {
|
||||
let open_screen = screen.clone();
|
||||
let open_selection = peer_selection;
|
||||
d.on_open(Box::new(move || {
|
||||
if let Some(start_drain) = control_drain { tokio::spawn(start_drain()); }
|
||||
let init_actions = open_actions.clone();
|
||||
let init_video = open_video.clone();
|
||||
let init_track = open_track.clone();
|
||||
@@ -1422,6 +1462,11 @@ fn negotiated_bundle_media(sdp: &str) -> Option<(String, u16)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// First drain owns the outbound receiver; a later open must not start another.
|
||||
async fn take_outbound_once<T>(slot: &tokio::sync::Mutex<Option<tokio::sync::mpsc::UnboundedReceiver<T>>>) -> Option<tokio::sync::mpsc::UnboundedReceiver<T>> {
|
||||
slot.lock().await.take()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::negotiated_bundle_media;
|
||||
@@ -1444,6 +1489,16 @@ mod tests {
|
||||
let sdp = "a=group:BUNDLE data video\nm=video 9 RTP 98\na=mid:video\nm=application 9 SCTP 0\na=mid:data\n";
|
||||
assert_eq!(negotiated_bundle_media(sdp), Some(("data".into(), 1)));
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn outbound_receiver_is_taken_once() {
|
||||
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<Vec<u8>>();
|
||||
let slot = std::sync::Arc::new(tokio::sync::Mutex::new(Some(rx)));
|
||||
let first = super::take_outbound_once(&slot).await;
|
||||
let second = super::take_outbound_once(&slot).await;
|
||||
assert!(first.is_some());
|
||||
assert!(second.is_none());
|
||||
drop(tx);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user