mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
369b493e47 | ||
|
|
ce28d7e230 | ||
|
|
b0beba1a4e | ||
|
|
77a556c674 | ||
|
|
e89c91e83b | ||
|
|
f35ec3b2c7 | ||
|
|
aa4dca75d2 | ||
|
|
d1791ef861 | ||
|
|
90e802b94a | ||
|
|
6226e723b5 |
No files matched your search
@@ -145,7 +145,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` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto), `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; `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
@@ -2717,7 +2717,7 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.25"
|
||||
version = "0.1.28"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "uu-remote-linux"
|
||||
version = "0.1.25"
|
||||
version = "0.1.28"
|
||||
edition = "2021"
|
||||
authors = ["uu-remote-linux contributors"]
|
||||
description = "Unofficial Linux host for NetEase UU Remote: control a Linux desktop from the official clients"
|
||||
|
||||
+3
-1
@@ -75,7 +75,7 @@ Wayland limits: single screen, fixed resolution. The native Hyprland backend now
|
||||
dynamic resolution yet; the clipboard syncs while the viewer's remote window has focus. On the first connection the desktop asks for "Remote Desktop / Screen Sharing"
|
||||
permission; the permission is remembered once granted.
|
||||
|
||||
Scaled GNOME desktops using native Portal input use EIS on the same authorized session when `ConnectToEIS` and a screen `mapping_id` are available. Each axis is mapped using the matched region and actual frame dimensions, with no fixed resolution or scale factor. This avoids scaled-coordinate rejection by `NotifyPointerMotionAbsolute`. Portal builds require libei >= 1.1. Missing EIS support retains the legacy input path and its existing scaling limitations; other initialization failures are reported and trigger session recovery. After a successful EIS connection, all input uses EIS. Capture alone and uinput do not initialize EIS; missing optional logical dimensions do not block capture. Keyboard input uses the existing Windows VK to evdev mapping. Recovery requires reconnecting if physical or logical dimensions change. Lock recovery still needs hardware acceptance testing; multi-monitor support, live resolution changes, and rotation/reflection transforms are outside this fix's scope. Capture settings remain at protocol version 4: advertised resolutions use pixel coordinates and DPR 1; Portal logical dimensions are used only for local input mapping and recovery checks.
|
||||
Scaled GNOME desktops using native Portal input use EIS on the same authorized session when `ConnectToEIS` and a screen `mapping_id` are available. Each axis is mapped using the matched region and actual frame dimensions, with no fixed resolution or scale factor. This avoids scaled-coordinate rejection by `NotifyPointerMotionAbsolute`. Portal builds require libei >= 1.1. Missing EIS support retains the legacy input path and its existing scaling limitations; other initialization failures are reported and trigger session recovery. After a successful EIS connection, all input uses EIS. Capture alone and uinput do not initialize EIS; missing optional logical dimensions do not block capture. Keyboard input uses the existing Windows VK to evdev mapping. Recovery keeps the encoder frame dimensions fixed but accepts logical-size changes after scaling. A previously streaming Portal capture that stays paused without a negotiated format for five seconds enters session recovery; a static desktop does not trigger this timeout. Tested while connected on Ubuntu 26.04, GNOME Wayland, one 3840x2160 display: 200% -> 166.67% -> 200%, with continuously updating video and full-screen pointer control after recovery. Switching briefly freezes and goes black before recovering; five seconds is the detection threshold, not the total recovery time. Lock recovery still needs hardware acceptance testing; multi-monitor support, live resolution changes, and rotation/reflection transforms are outside this fix's scope. Capture settings remain at protocol version 4: advertised resolutions use pixel coordinates and DPR 1; Portal logical dimensions are used only for local input mapping.
|
||||
|
||||
The native Wayland backend needs `wlr-output-power-management` from the compositor to wake a turned-off screen; without it only wake is unavailable — capture and encoding are unaffected.
|
||||
|
||||
@@ -272,6 +272,8 @@ If you use Claude Code, Codex, Cursor or a similar coding agent, have it read [A
|
||||
It is written for language models: build and install steps, a code map, protocol notes and development rules,
|
||||
enough to take over installation or development.
|
||||
|
||||
For local encoding diagnostics, `desktop-video-probe [output.h264]` accepts `UU_PROBE_FPS`, `UU_PROBE_FRAMES` (default 5), and `UU_PROBE_QUALITY` (1–5); set `UU_PROBE_INPUT=clip.y4m` (also `.mp4` or `frames/%05d.png`) to bypass desktop capture and measure fixed input with the same production FFmpeg encoding options, printing dimensions and encoding statistics, and optionally `UU_PROBE_LOOP=1` to repeat until the requested frame count (unreadable or short input exits nonzero; fixed input always uses the external FFmpeg pipeline).
|
||||
|
||||
## How it works
|
||||
|
||||
```mermaid
|
||||
|
||||
@@ -66,7 +66,7 @@ GNOME Wayland 的 Portal/EIS 路径已在 Ubuntu 26.04、GNOME 50、单屏 4K/20
|
||||
|
||||
Wayland 下的限制:只支持单屏、固定分辨率;Hyprland 原生后端默认使用双缓冲流水线 shm screencopy,控制端帧率设置会同步到采集节拍和编码器,但实际帧率仍受合成器、渲染和读回速度限制(旧串行路径在 2560x1600 曾测得约 16~22 帧;流水线只在虚拟机里测量过,不代表实机保证);portal 后端暂不支持唤醒屏幕和动态分辨率;剪贴板只在控制端的远控窗口处于前台时同步。
|
||||
|
||||
GNOME 缩放桌面在使用 Portal 原生键鼠时,若支持 `ConnectToEIS` 和屏幕 `mapping_id`,通过同一授权会话的 EIS 接口注入输入,按匹配的区域与实际帧尺寸分别映射横纵坐标,没有固定分辨率或缩放倍率,可避免 `NotifyPointerMotionAbsolute` 在缩放时拒绝坐标。Portal 构建需要 libei ≥ 1.1。缺少 EIS 支持时保留旧输入路径及其既有缩放限制;其他初始化错误会明确报告并触发会话恢复。EIS 连接成功后全部输入统一使用 EIS。只采集画面或使用 uinput 时不会初始化 EIS;缺少可选的逻辑尺寸也不阻断采集。键盘沿用项目现有的 Windows VK → evdev 映射。恢复时若物理或逻辑尺寸变化,需要重新连接。此路径仍需实机验证锁屏恢复;多屏、连接中改变分辨率及旋转/镜像变换不属于本次修复范围。 捕获设置协议仍为版本 4:对客户端上报的分辨率统一使用像素坐标、DPR 为 1;Portal 逻辑尺寸仅用于本地输入映射和恢复校验。
|
||||
GNOME 缩放桌面在使用 Portal 原生键鼠时,若支持 `ConnectToEIS` 和屏幕 `mapping_id`,通过同一授权会话的 EIS 接口注入输入,按匹配的区域与实际帧尺寸分别映射横纵坐标,没有固定分辨率或缩放倍率,可避免 `NotifyPointerMotionAbsolute` 在缩放时拒绝坐标。Portal 构建需要 libei ≥ 1.1。缺少 EIS 支持时保留旧输入路径及其既有缩放限制;其他初始化错误会明确报告并触发会话恢复。EIS 连接成功后全部输入统一使用 EIS。只采集画面或使用 uinput 时不会初始化 EIS;缺少可选的逻辑尺寸也不阻断采集。键盘沿用项目现有的 Windows VK → evdev 映射。恢复时保持编码器的采集帧尺寸不变,允许缩放后的逻辑尺寸变化。已开始推流的 Portal 采集若暂停且无有效协商格式持续五秒,将进入会话恢复;静态桌面不会触发该超时。Ubuntu 26.04、GNOME Wayland 单屏 3840×2160 已实测保持连接切换 200%→166.67%→200%,恢复后画面持续更新且鼠标可控制全屏;切换期间会短暂停顿、黑屏后恢复,五秒是故障判定阈值,并非总恢复时间。此路径仍需实机验证锁屏恢复;多屏、连接中改变分辨率及旋转/镜像变换不属于本次修复范围。 捕获设置协议仍为版本 4:对客户端上报的分辨率统一使用像素坐标、DPR 为 1;Portal 逻辑尺寸仅用于本地输入映射。
|
||||
|
||||
原生 Wayland 后端需要合成器提供 `wlr-output-power-management` 才能唤醒已关闭的屏幕;缺少该协议不影响采集和编码。
|
||||
|
||||
@@ -237,6 +237,8 @@ systemctl --user restart uuremote-native.service
|
||||
|
||||
选择码率档(原画 / 超清 / 高清 / 自定义)时,外部 FFmpeg 的 CPU 编码以平均码率(ABR)为目标,避免 4K 低码率下关键帧发灰;「自动」档仍使用恒定质量(CRF)。
|
||||
|
||||
本地编码诊断 `desktop-video-probe [output.h264]` 可设置 `UU_PROBE_FPS`、`UU_PROBE_FRAMES`(默认 5)和 `UU_PROBE_QUALITY`(1–5);设置 `UU_PROBE_INPUT=clip.y4m`(也支持 `.mp4`、`frames/%05d.png`)可绕过桌面采集,以相同的生产 FFmpeg 编码参数测量固定输入并打印分辨率和编码统计,`UU_PROBE_LOOP=1` 循环到指定帧数,输入无法打开或帧数不足时以非零状态退出(固定输入始终使用外部 FFmpeg 管线)。
|
||||
|
||||
## 让 AI 帮你安装或开发
|
||||
|
||||
如果你用 Claude Code、Codex、Cursor 等 AI 编程工具,让它先阅读 [AGENTS.md](AGENTS.md)。
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#define SCREEN "org.freedesktop.portal.ScreenCast"
|
||||
#define REQUEST "org.freedesktop.portal.Request"
|
||||
#define SESSION "org.freedesktop.portal.Session"
|
||||
#define PORTAL_RENEGOTIATION_TIMEOUT (5 * G_TIME_SPAN_SECOND)
|
||||
|
||||
struct uu_portal {
|
||||
GDBusConnection *bus;
|
||||
@@ -46,6 +47,9 @@ struct uu_portal {
|
||||
pthread_cond_t changed;
|
||||
atomic_int closed;
|
||||
int error;
|
||||
enum pw_stream_state state;
|
||||
int format_valid, stream_started;
|
||||
gint64 interrupted_at;
|
||||
uint32_t node, width, height;
|
||||
int logical_width, logical_height;
|
||||
int absolute_stream_pixels;
|
||||
@@ -155,13 +159,33 @@ static int has_interface(struct uu_portal *p, const char *interface) {
|
||||
if (!r) return 0;
|
||||
g_variant_unref(r); return 1;
|
||||
}
|
||||
/* Called with the frame mutex held. Count only the continuous invalid pause,
|
||||
* not a healthy pause before format removal, and never arm during startup. */
|
||||
static void update_interruption(struct uu_portal *p) {
|
||||
if (p->stream_started && p->state == PW_STREAM_STATE_PAUSED && !p->format_valid) {
|
||||
if (!p->interrupted_at) p->interrupted_at = g_get_monotonic_time();
|
||||
} else p->interrupted_at = 0;
|
||||
}
|
||||
static void stream_state(void *data, enum pw_stream_state old, enum pw_stream_state state, const char *message) {
|
||||
(void)old; (void)message;
|
||||
struct uu_portal *p = data;
|
||||
pthread_mutex_lock(&p->lock);
|
||||
p->state = state;
|
||||
if (state == PW_STREAM_STATE_STREAMING) p->stream_started = 1;
|
||||
update_interruption(p);
|
||||
pthread_mutex_unlock(&p->lock);
|
||||
if (state == PW_STREAM_STATE_ERROR || state == PW_STREAM_STATE_UNCONNECTED) fail(data, 9);
|
||||
}
|
||||
static void stream_format(void *data, uint32_t id, const struct spa_pod *param) {
|
||||
struct uu_portal *p = data;
|
||||
if (id != SPA_PARAM_Format || !param) return;
|
||||
if (id != SPA_PARAM_Format) return;
|
||||
if (!param) {
|
||||
pthread_mutex_lock(&p->lock);
|
||||
p->format_valid = 0;
|
||||
update_interruption(p);
|
||||
pthread_mutex_unlock(&p->lock);
|
||||
return;
|
||||
}
|
||||
struct spa_video_info_raw info = {0};
|
||||
if (spa_format_video_raw_parse(param, &info) < 0 || !info.size.width || !info.size.height ||
|
||||
info.size.width > 8192 || info.size.height > 8192 ||
|
||||
@@ -176,13 +200,15 @@ static void stream_format(void *data, uint32_t id, const struct spa_pod *param)
|
||||
if (!p->frame) { p->error = 8; pthread_mutex_unlock(&p->lock); return; }
|
||||
p->width = info.size.width; p->height = info.size.height; p->format = info.format;
|
||||
p->frame_length = length;
|
||||
p->format_valid = 1;
|
||||
update_interruption(p);
|
||||
pthread_cond_broadcast(&p->changed);
|
||||
pthread_mutex_unlock(&p->lock);
|
||||
}
|
||||
/* Called with the frame mutex held. Invalid transient chunks are discarded;
|
||||
* allocation capacity is not evidence that valid pixels have been supplied. */
|
||||
static void copy_video_buffer(struct uu_portal *p, struct spa_buffer *spa) {
|
||||
if (!p->error && p->frame && spa->n_datas >= 1) {
|
||||
if (!p->error && p->format_valid && p->frame && spa->n_datas >= 1) {
|
||||
struct spa_data *d = &spa->datas[0];
|
||||
struct spa_meta_header *header = spa_buffer_find_meta_data(spa, SPA_META_Header, sizeof(*header));
|
||||
if (!d->chunk || (d->chunk->flags & SPA_CHUNK_FLAG_CORRUPTED) ||
|
||||
@@ -448,10 +474,18 @@ void uu_portal_reset_frame(struct uu_portal *p) {
|
||||
int uu_portal_capture(struct uu_portal *p, uint8_t *frame, size_t length) {
|
||||
return uu_portal_capture_since(p, frame, length, NULL);
|
||||
}
|
||||
/* A static desktop can stop producing frames indefinitely. Only time out the
|
||||
* paused, format-less renegotiation observed after a live scaling change (#25).
|
||||
* Called with the frame mutex held; recovery runs on the Rust capture worker. */
|
||||
static int stream_stalled(struct uu_portal *p) {
|
||||
return p->state == PW_STREAM_STATE_PAUSED && !p->format_valid && p->interrupted_at &&
|
||||
g_get_monotonic_time() - p->interrupted_at >= PORTAL_RENEGOTIATION_TIMEOUT;
|
||||
}
|
||||
int uu_portal_capture_since(struct uu_portal *p, uint8_t *frame, size_t length, uint64_t *revision) {
|
||||
if (!p || !frame) return 11;
|
||||
pthread_mutex_lock(&p->lock);
|
||||
int result = atomic_load(&p->closed) ? 9 : p->error ? p->error : !p->sequence ? 10 : length != p->frame_length ? 11 : 0;
|
||||
int result = atomic_load(&p->closed) ? 9 : p->error ? p->error : stream_stalled(p) ? 9 :
|
||||
!p->sequence ? 10 : length != p->frame_length ? 11 : 0;
|
||||
if (!result) {
|
||||
if (revision && *revision == p->revision) result = 12;
|
||||
else memcpy(frame, p->frame, length);
|
||||
@@ -466,6 +500,14 @@ int uu_portal_alive(struct uu_portal *p) {
|
||||
pthread_mutex_unlock(&p->lock);
|
||||
return alive;
|
||||
}
|
||||
/* Video renegotiation must not block input releases or clipboard traffic. */
|
||||
int uu_portal_capture_alive(struct uu_portal *p) {
|
||||
if (!uu_portal_alive(p)) return 0;
|
||||
pthread_mutex_lock(&p->lock);
|
||||
int alive = !stream_stalled(p);
|
||||
pthread_mutex_unlock(&p->lock);
|
||||
return alive;
|
||||
}
|
||||
void uu_portal_input_dispatch(struct uu_portal *p) {
|
||||
if (p->ei) portal_eis_dispatch(p);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ int uu_portal_clipboard_ready(struct uu_portal *portal);
|
||||
int uu_portal_clipboard_set(struct uu_portal *portal, const uint8_t *text, size_t length);
|
||||
int uu_portal_clipboard_poll(struct uu_portal *portal, uint8_t *text, size_t capacity);
|
||||
int uu_portal_alive(struct uu_portal *portal);
|
||||
int uu_portal_capture_alive(struct uu_portal *portal);
|
||||
void uu_portal_input_dispatch(struct uu_portal *portal);
|
||||
void uu_portal_logical_size(struct uu_portal *portal, uint32_t *width, uint32_t *height);
|
||||
void uu_portal_reset_frame(struct uu_portal *portal);
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
大目录文件传输修复、上报实际刷新率、NVENC 省掉 CPU 颜色转换。/ Large-folder file transfer fix, real refresh rate, NVENC without CPU color conversion.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **文件传输打开文件很多的目录时一直转圈**:目录回复同时带了未压缩和压缩两份列表,几千个文件时超过数据通道单条消息上限,
|
||||
回复被丢弃。现在压缩列表始终发送,未压缩副本只在放得下时附带;同时把 SCTP 发送上限从 64 KB 提到 256 KB
|
||||
(与官方控制端在 SDP 中声明的一致)。已在 5600 个文件的目录上实测。
|
||||
Large directory listings no longer exceed the data-channel message limit (verified with 5600 entries).
|
||||
|
||||
### 改进 / Improved
|
||||
- **上报显示器实际刷新率**(#16):`display_info`、能力通知和编解码协商原先固定报 60,现在取当前显示模式
|
||||
(Hyprland 用 `hyprctl`,X11 用 `xrandr`;取不到时仍为 60,`UU_DISPLAY_FPS` 可手动指定)。
|
||||
The host reports the monitor's refresh rate instead of a fixed 60 (`UU_DISPLAY_FPS` overrides).
|
||||
- **NVENC 直接接收 BGRA/BGR0**(#16):4:2:0 且不缩放时,颜色转换交给 GPU,省掉 FFmpeg 子进程里的 CPU 转换;
|
||||
色彩标记与原来一致(BT.601 有限范围)。`UU_NVENC_RGB=0` 可恢复原路径。
|
||||
⚠️ 已在 X11 + RTX 3090 上实测画面与颜色;Hyprland 4K 高帧率下的 CPU/帧率收益尚未实测。
|
||||
NVENC converts RGB on the GPU; verified on X11, the Hyprland 4K gain is not yet measured.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.26-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,16 @@
|
||||
GNOME Wayland 连接中切换缩放后的停帧修复。/ Recover from live scaling changes on GNOME Wayland.
|
||||
|
||||
### 修复 / Fixed
|
||||
- **GNOME Wayland 连接中切换显示缩放后画面持续冻结**(#25、#26,感谢 @wanglz111):已推流的 Portal 流若「暂停且没有有效格式」
|
||||
持续 5 秒,复用现有会话恢复;恢复时允许逻辑尺寸变化,仍校验采集帧尺寸。启动阶段和静态画面不会触发这个超时,
|
||||
等待授权与 D-Bus 的过程不再占用输入锁。
|
||||
⚠️ 贡献者实测:Ubuntu 26.04 + GNOME Wayland、单屏 3840×2160,连接中 200%→166.67%→200% 恢复后画面和全屏鼠标正常;
|
||||
**切换时会短暂停顿、黑屏后恢复,并非无缝;5 秒是判定阈值,不是总恢复时间**。物理分辨率变化、多屏、锁屏恢复未实机验证。
|
||||
Portal recovers after a live scale change (brief freeze/black, not seamless); physical resolution changes remain unsupported.
|
||||
- Portal 失效检查出错时只记日志,不再让主机主循环退出。/ A failed Portal staleness check no longer ends the host loop.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.27-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
GNOME/KDE Wayland 的 Portal 后端需自行用 `--features wayland,portal` 构建(需要 libei-dev ≥ 1.1)。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -0,0 +1,16 @@
|
||||
编码探针支持固定输入,测试更稳定。/ Fixed-input encoder probe and deterministic WebRTC tests.
|
||||
|
||||
### 新增 / Added
|
||||
- **`desktop-video-probe` 支持固定输入**(#9,感谢 @hiclaw 的建议):设置 `UU_PROBE_INPUT=<视频或图片序列>`(如 `clip.y4m`、
|
||||
`clip.mp4`、`frames/%05d.png`)时不采集桌面,而是用与生产会话相同的 FFmpeg 编码参数(编码器、码率档、GOP、色度、缩放)
|
||||
编码该输入,消除桌面内容对测量的影响;`UU_PROBE_LOOP=1` 循环输入直到 `UU_PROBE_FRAMES`。输入打不开或帧数不足时非零退出。
|
||||
正常会话路径不变。已用 Y4M、MP4、PNG 序列验证,重复运行输出逐字节一致。⚠️ NVENC/VAAPI 下的固定输入未实机验证。
|
||||
`desktop-video-probe` can encode a fixed input (`UU_PROBE_INPUT`) with the production encoder arguments.
|
||||
|
||||
### 内部 / Internal
|
||||
- 本地 WebRTC 单测改用虚拟网络,修复全量并行测试时偶发的 30 秒超时。/ Local WebRTC tests run on a virtual network.
|
||||
|
||||
### 下载 / Download
|
||||
`uu-remote-linux-v0.1.28-x86_64-linux.tar.gz`:X11 版可执行文件(x86_64,glibc ≥ 2.35),附带 `contrib/`。
|
||||
|
||||
**非官方项目,与网易无关。** / **Unofficial, not affiliated with NetEase.**
|
||||
@@ -188,7 +188,7 @@ pub fn screen_sources(seq: u64, timestamp: u64, native: (u32, u32), output: Opti
|
||||
// DPI_SCALE starts at version 5; the observed v4 request uses fields 5/6 as pixels.
|
||||
// Keep every advertised rectangle in that space; Portal/EIS maps input independently.
|
||||
let resolution = WinRect { left: 0, top: 0, width: w, height: h, pixel_width: w, pixel_height: h };
|
||||
let screens = [Screen { id: 0, fps: crate::video::FPS as i32, resolution, primary: true, dpr: 1.0,
|
||||
let screens = [Screen { id: 0, fps: crate::video::display_fps() as i32, resolution, primary: true, dpr: 1.0,
|
||||
display_name: name, video_track_index: 0, screen_type: 0, builtin_screen_type: 0,
|
||||
scaled: &scaled, current: output.map(|(w, h)| (w as i32, h as i32)) }];
|
||||
crate::initialization::encode_screen_sources(seq, timestamp, &screens, 0)
|
||||
|
||||
+6
-2
@@ -392,8 +392,12 @@ impl FileSession {
|
||||
let mut listing = Enc::default();
|
||||
for e in &entries { listing = listing.bytes(1, &e.0); }
|
||||
let compressed = zlib(&listing.0);
|
||||
log::info!("File transfer read_dir: request_id={} root={} entries={} dir_data_bytes={}", rid, path.is_empty(), entries.len(), compressed.len());
|
||||
for e in entries { body = body.msg(3, e); }
|
||||
// Both copies in one message: a 5600-entry folder is ~73 KB compressed but several hundred KB
|
||||
// inline, beyond the 256 KB SCTP message limit, and the reply was dropped. Keep the inline copy
|
||||
// only when the message still fits; current viewers read dir_data.
|
||||
let inline = listing.0.len() + compressed.len() < 200_000;
|
||||
log::info!("File transfer read_dir: request_id={} root={} entries={} dir_data_bytes={} inline={}", rid, path.is_empty(), entries.len(), compressed.len(), inline);
|
||||
if inline { for e in entries { body = body.msg(3, e); } }
|
||||
body = body.uint(4, FT_SUCCEEDED).bytes(5, &compressed);
|
||||
}
|
||||
Err(code) => { log::info!("File transfer read_dir failed: request_id={} error={}", rid, code); body = body.uint(4, code); }
|
||||
|
||||
+15
-2
@@ -25,6 +25,8 @@ mod terminal;
|
||||
mod webrtc;
|
||||
mod video;
|
||||
mod video_track;
|
||||
#[cfg(test)]
|
||||
mod test_network;
|
||||
mod hevc;
|
||||
#[cfg(feature = "inproc-av")]
|
||||
mod damage_capture;
|
||||
@@ -318,6 +320,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
return Ok(());
|
||||
}
|
||||
if std::env::args().nth(1).as_deref() == Some("desktop-video-probe") {
|
||||
if let Some(input)=std::env::var_os("UU_PROBE_INPUT") {
|
||||
return video::probe_input(input,std::env::args().nth(2)).await.map_err(|e|e.into());
|
||||
}
|
||||
let screen=ScreenCapture::new()?;
|
||||
capture::keep_display_awake()?;
|
||||
return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),std::env::args().nth(2)).await.map_err(|e|e.into());
|
||||
@@ -656,8 +661,16 @@ async fn serve(mut capture: Arc<ScreenCapture>, input: Arc<InputInjector>, shutd
|
||||
let stale: Vec<String> = sessions.iter().filter(|(_, s)| s.desktop).map(|(k, _)| k.clone()).collect();
|
||||
for k in stale { if let Some(old) = sessions.remove(&k) { let _ = old.pc.close().await; } }
|
||||
}
|
||||
let stale_portal=capture.portal.as_ref().is_some_and(|session|!session.is_alive())
|
||||
&& crate::session_lock::locked_now() == Some(false);
|
||||
let stale_portal=if desktop {
|
||||
if let Some(session)=capture.portal.clone() {
|
||||
// A failed check must not end the host loop; treat it as not stale.
|
||||
match tokio::task::spawn_blocking(move || session.retire_if_closed()).await {
|
||||
Ok(Ok(stale)) => stale,
|
||||
Ok(Err(error)) => { log::warn!("Portal staleness check failed: {error}"); false }
|
||||
Err(_) => { log::warn!("Portal staleness check panicked"); false }
|
||||
}
|
||||
} else { false }
|
||||
} else { false };
|
||||
if desktop && desktop_backend::headless() {
|
||||
log::info!("Desktop connection ignored: headless mode (terminal, file transfer and port mapping remain available)");
|
||||
continue;
|
||||
|
||||
@@ -154,22 +154,21 @@ mod tests {
|
||||
assert_eq!(rates.sample(now + std::time::Duration::from_secs(3), 0, 0), None);
|
||||
}
|
||||
|
||||
/// Exercises actual ICE nomination and SCTP delivery between local peers, without
|
||||
/// Exercises actual ICE nomination and SCTP delivery between virtual peers, without
|
||||
/// login, public STUN, external endpoints or production configuration.
|
||||
#[tokio::test]
|
||||
#[ignore = "opens local UDP sockets; run explicitly for network acceptance"]
|
||||
async fn local_selected_pair_and_payload() {
|
||||
use std::sync::Arc;
|
||||
use webrtc::api::{APIBuilder, setting_engine::SettingEngine};
|
||||
use webrtc::api::APIBuilder;
|
||||
use webrtc::peer_connection::configuration::RTCConfiguration;
|
||||
let mut settings = SettingEngine::default();
|
||||
// This crate excludes loopback addresses during ICE gathering. Both
|
||||
// peers still run in this process and use only gathered local addresses.
|
||||
settings.set_ice_multicast_dns_mode(webrtc::ice::mdns::MulticastDnsMode::Disabled);
|
||||
let (left_setting, right_setting, router) = crate::test_network::virtual_network().await;
|
||||
let mut media = webrtc::api::media_engine::MediaEngine::default();
|
||||
media.register_default_codecs().unwrap();
|
||||
let api = APIBuilder::new().with_media_engine(media).with_setting_engine(settings).build();
|
||||
let api = APIBuilder::new().with_media_engine(media).with_setting_engine(left_setting).build();
|
||||
let left = Arc::new(api.new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||
let mut media = webrtc::api::media_engine::MediaEngine::default();
|
||||
media.register_default_codecs().unwrap();
|
||||
let api = APIBuilder::new().with_media_engine(media).with_setting_engine(right_setting).build();
|
||||
let right = Arc::new(api.new_peer_connection(RTCConfiguration::default()).await.unwrap());
|
||||
for (label, peer) in [("left", &left), ("right", &right)] {
|
||||
peer.on_peer_connection_state_change(Box::new(move |state| {
|
||||
@@ -222,6 +221,7 @@ mod tests {
|
||||
let outcome = tokio::time::timeout(std::time::Duration::from_secs(15), acceptance).await;
|
||||
left.close().await.unwrap();
|
||||
right.close().await.unwrap();
|
||||
router.lock().await.stop().await.unwrap();
|
||||
outcome.expect("isolated local ICE/SCTP did not connect within 15 seconds");
|
||||
}
|
||||
}
|
||||
+347
-56
@@ -9,7 +9,7 @@ mod permissions;
|
||||
|
||||
#[cfg(all(target_os = "linux", feature = "portal"))]
|
||||
mod native {
|
||||
use std::{ffi::c_void, ptr::NonNull, sync::Mutex};
|
||||
use std::{ffi::c_void, ptr::NonNull, sync::{Mutex, MutexGuard, TryLockError}};
|
||||
extern "C" {
|
||||
fn uu_portal_open_persist(restore: *const libc::c_char, save: extern "C" fn(*const libc::c_char, *mut c_void), data: *mut c_void, error: *mut i32, width: *mut u32, height: *mut u32) -> *mut c_void;
|
||||
fn uu_portal_clipboard_supported(portal: *mut c_void) -> i32;
|
||||
@@ -18,8 +18,8 @@ mod native {
|
||||
fn uu_portal_clipboard_poll(portal: *mut c_void, text: *mut u8, capacity: usize) -> i32;
|
||||
fn uu_portal_close(portal: *mut c_void);
|
||||
fn uu_portal_alive(portal: *mut c_void) -> i32;
|
||||
fn uu_portal_capture_alive(portal: *mut c_void) -> i32;
|
||||
fn uu_portal_input_dispatch(portal: *mut c_void);
|
||||
fn uu_portal_logical_size(portal: *mut c_void, width: *mut u32, height: *mut u32);
|
||||
fn uu_portal_reset_frame(portal: *mut c_void);
|
||||
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;
|
||||
@@ -34,12 +34,57 @@ mod native {
|
||||
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, logical_size: (u32, u32) }
|
||||
// Portal preparation and native teardown can wait for consent or D-Bus.
|
||||
// Only the swap may hold the mutex shared with input and clipboard.
|
||||
fn replace_handle<T>(current: &Mutex<T>, locked: MutexGuard<'_, T>, prepare: impl FnOnce() -> Result<Option<T>, String>) -> Result<bool, String> {
|
||||
drop(locked);
|
||||
let Some(replacement) = prepare()? else { return Ok(false); };
|
||||
let old = {
|
||||
let mut handle = current.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||
std::mem::replace(&mut *handle, replacement)
|
||||
};
|
||||
drop(old);
|
||||
Ok(true)
|
||||
}
|
||||
#[derive(Default)]
|
||||
struct Recovery { retry_at: Option<std::time::Instant>, retired: bool }
|
||||
impl Recovery {
|
||||
fn ready(&mut self, now: std::time::Instant, unlocked: impl FnOnce() -> bool) -> bool {
|
||||
if self.retired { return false; }
|
||||
let deadline = self.retry_at.get_or_insert(now + std::time::Duration::from_secs(1));
|
||||
if now < *deadline { return false; }
|
||||
*deadline = now + std::time::Duration::from_secs(1);
|
||||
unlocked()
|
||||
}
|
||||
}
|
||||
fn retire_if_closed<T>(current: &Mutex<T>, recovery: &Mutex<Recovery>, alive: impl FnOnce(&T) -> bool, unlocked: impl FnOnce() -> bool) -> Result<bool, String> {
|
||||
// Wait outside the input mutex. Inspect the published replacement if recovery
|
||||
// won; otherwise prevent the old encoder from opening after reconnect takes over.
|
||||
let mut recovery = recovery.lock().map_err(|_| "Portal recovery lock poisoned")?;
|
||||
let alive = {
|
||||
let handle = current.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||
alive(&handle)
|
||||
};
|
||||
if alive || !unlocked() { return Ok(false); }
|
||||
recovery.retired = true;
|
||||
Ok(true)
|
||||
}
|
||||
pub struct Session { handle: Mutex<Handle>, locked: tokio::sync::watch::Receiver<Option<bool>>, was_locked: std::sync::atomic::AtomicBool, recovery: Mutex<Recovery>, pub width: u32, pub height: u32 }
|
||||
impl Session {
|
||||
pub fn logical_size(&self) -> (u32, u32) { self.logical_size }
|
||||
fn take_lock_observation(&self) -> Result<bool, String> {
|
||||
// A recovery in progress must see the lock even if another capture
|
||||
// observes unlock and would otherwise reset only the old handle.
|
||||
let _recovery = match self.recovery.try_lock() {
|
||||
Ok(recovery) => recovery,
|
||||
Err(TryLockError::WouldBlock) => return Ok(false),
|
||||
Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()),
|
||||
};
|
||||
Ok(self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel))
|
||||
}
|
||||
fn check_geometry(&self, replacement: &Self) -> Result<(), String> {
|
||||
if (self.width, self.height) != (replacement.width, replacement.height)
|
||||
|| self.logical_size != replacement.logical_size {
|
||||
// Encoder buffers use frame pixels. Logical geometry and EIS regions
|
||||
// belong to the replacement handle and may change with scaling (#25).
|
||||
if (self.width, self.height) != (replacement.width, replacement.height) {
|
||||
Err("Portal dimensions changed; reconnect required".into())
|
||||
} else {
|
||||
Ok(())
|
||||
@@ -48,6 +93,10 @@ mod native {
|
||||
pub fn is_alive(&self) -> bool {
|
||||
self.handle.lock().is_ok_and(|handle| unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 })
|
||||
}
|
||||
pub fn retire_if_closed(&self) -> Result<bool, String> {
|
||||
retire_if_closed(&self.handle, &self.recovery, |handle| unsafe { uu_portal_alive(handle.0.as_ptr()) != 0 },
|
||||
|| crate::session_lock::locked_now() == Some(false))
|
||||
}
|
||||
pub fn open() -> Result<Self, String> {
|
||||
let (mut error, mut width, mut height) = (0, 0, 0);
|
||||
let directory = super::permissions::directory();
|
||||
@@ -67,19 +116,17 @@ mod native {
|
||||
}
|
||||
let pointer = unsafe { uu_portal_open_persist(token.as_ref().map_or(std::ptr::null(), |v| v.as_ptr()), save_token, &mut cache as *mut _ as *mut c_void, &mut error, &mut width, &mut height) };
|
||||
let handle = NonNull::new(pointer).ok_or_else(|| super::error(error))?;
|
||||
let (mut logical_width, mut logical_height) = (0, 0);
|
||||
unsafe { uu_portal_logical_size(handle.as_ptr(), &mut logical_width, &mut logical_height) };
|
||||
// A freshly opened session holds no lock-screen frame: only an observed lock (or a
|
||||
// closed stream) must discard the cached frame. Starting "locked" cleared the first
|
||||
// valid frame, and a static desktop sends no new one, so the viewer stayed black (#2).
|
||||
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), retry_at: Mutex::new(None), width, height, logical_size: (logical_width, logical_height) })
|
||||
Ok(Self { handle: Mutex::new(Handle(handle)), locked: crate::session_lock::watch_locked(), was_locked: std::sync::atomic::AtomicBool::new(false), recovery: Mutex::new(Recovery::default()), width, height })
|
||||
}
|
||||
pub fn capture(&self, frame: &mut [u8]) -> Result<(), String> {
|
||||
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 handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||
// Drain EIS even on a static desktop so pause/disconnect and sync
|
||||
// events are processed without waiting for remote input.
|
||||
unsafe { uu_portal_input_dispatch(handle.0.as_ptr()) };
|
||||
@@ -92,34 +139,60 @@ mod native {
|
||||
}
|
||||
return Ok(blank_changed(frame, version));
|
||||
}
|
||||
if self.was_locked.swap(false, std::sync::atomic::Ordering::AcqRel) {
|
||||
if self.was_locked.load(std::sync::atomic::Ordering::Acquire) {
|
||||
// First unlocked capture after a real lock: drop frames captured while locked.
|
||||
unsafe { uu_portal_reset_frame(handle.0.as_ptr()) };
|
||||
if self.take_lock_observation()? { unsafe { uu_portal_reset_frame(handle.0.as_ptr()) }; }
|
||||
return Ok(blank_changed(frame, version));
|
||||
}
|
||||
if unsafe { uu_portal_alive(handle.0.as_ptr()) } == 0 {
|
||||
// GNOME can close the stream before the periodic lock watcher updates.
|
||||
if crate::session_lock::locked_now() != Some(false) {
|
||||
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||
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 { 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));
|
||||
return Ok(blank_changed(frame, version));
|
||||
}
|
||||
if unsafe { uu_portal_capture_alive(handle.0.as_ptr()) } == 0 {
|
||||
// Serialize recovery without making another capture worker hold
|
||||
// the input mutex while waiting for this worker's Portal calls.
|
||||
let mut recovery = match self.recovery.try_lock() {
|
||||
Ok(retry) => retry,
|
||||
Err(TryLockError::WouldBlock) => return Ok(blank_changed(frame, version)),
|
||||
Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()),
|
||||
};
|
||||
*retry = None;
|
||||
self.check_geometry(&replacement)?;
|
||||
*handle = replacement.handle.into_inner().map_err(|_| "Portal session lock poisoned")?;
|
||||
log::info!("Portal capture session restored after closure");
|
||||
if replace_handle(&self.handle, handle, || {
|
||||
{
|
||||
let handle = self.handle.lock().map_err(|_| "Portal session lock poisoned")?;
|
||||
if unsafe { uu_portal_capture_alive(handle.0.as_ptr()) } != 0 {
|
||||
recovery.retry_at = None;
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
// logind's LockedHint can lag compositor stream closure.
|
||||
if !recovery.ready(std::time::Instant::now(), || {
|
||||
if crate::session_lock::locked_now() == Some(false) { return true; }
|
||||
self.was_locked.store(true, std::sync::atomic::Ordering::Release);
|
||||
false
|
||||
}) { return Ok(None); }
|
||||
let replacement = match Self::open() {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
log::warn!("Portal capture recovery deferred: {error}");
|
||||
recovery.retry_at = Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
|
||||
return Ok(None);
|
||||
}
|
||||
};
|
||||
recovery.retry_at = None;
|
||||
self.check_geometry(&replacement)?;
|
||||
// Consent can outlive the unlocked state checked above.
|
||||
if *self.locked.borrow() != Some(false) || self.was_locked.load(std::sync::atomic::Ordering::Acquire) {
|
||||
return Ok(None);
|
||||
}
|
||||
replacement.handle.into_inner().map(Some).map_err(|_| "Portal session lock poisoned".into())
|
||||
})? {
|
||||
*version = None;
|
||||
log::info!("Portal capture session restored after stream loss");
|
||||
}
|
||||
// Recheck lock state on the next capture before showing the new session.
|
||||
return Ok(blank_changed(frame, version));
|
||||
} else {
|
||||
match self.recovery.try_lock() {
|
||||
Ok(mut recovery) => recovery.retry_at = None,
|
||||
Err(TryLockError::WouldBlock) => (),
|
||||
Err(TryLockError::Poisoned(_)) => return Err("Portal recovery lock poisoned".into()),
|
||||
}
|
||||
}
|
||||
let identity = handle.0.as_ptr() as usize;
|
||||
let mut revision = version.filter(|v| v.0 == identity).map_or(0, |v| v.1);
|
||||
@@ -172,20 +245,19 @@ mod native {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::{mem::ManuallyDrop, sync::Arc};
|
||||
|
||||
struct Fixture(ManuallyDrop<Session>);
|
||||
impl Fixture {
|
||||
fn new(physical: (u32, u32), logical_size: (u32, u32)) -> Self {
|
||||
fn new(physical: (u32, u32)) -> Self {
|
||||
let (_, locked) = tokio::sync::watch::channel(None);
|
||||
Self(ManuallyDrop::new(Session {
|
||||
handle: Mutex::new(Handle(NonNull::dangling())),
|
||||
locked,
|
||||
was_locked: std::sync::atomic::AtomicBool::new(false),
|
||||
retry_at: Mutex::new(None),
|
||||
recovery: Mutex::new(Recovery::default()),
|
||||
width: physical.0,
|
||||
height: physical.1,
|
||||
logical_size,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -193,35 +265,254 @@ mod native {
|
||||
}
|
||||
impl Drop for Fixture {
|
||||
fn drop(&mut self) {
|
||||
let Session { handle, locked, was_locked, retry_at, .. } =
|
||||
let Session { handle, locked, was_locked, recovery, .. } =
|
||||
unsafe { ManuallyDrop::take(&mut self.0) };
|
||||
let handle = handle.into_inner().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
std::mem::forget(handle);
|
||||
drop((locked, was_locked, retry_at));
|
||||
drop((locked, was_locked, recovery));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logical_size_does_not_depend_on_handle_mutex() {
|
||||
let session = Fixture::new((1920, 1080), (1280, 720));
|
||||
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
let _handle = session.session().handle.lock().unwrap();
|
||||
panic!("poison test handle mutex");
|
||||
}));
|
||||
fn recovery_requires_only_matching_frame_dimensions() {
|
||||
let original = Fixture::new((3840, 2160));
|
||||
let same = Fixture::new((3840, 2160));
|
||||
let width_changed = Fixture::new((2560, 2160));
|
||||
let height_changed = Fixture::new((3840, 1440));
|
||||
|
||||
assert_eq!(session.session().logical_size(), (1280, 720));
|
||||
assert!(original.session().check_geometry(same.session()).is_ok());
|
||||
assert!(original.session().check_geometry(width_changed.session()).is_err());
|
||||
assert!(original.session().check_geometry(height_changed.session()).is_err());
|
||||
}
|
||||
|
||||
struct TestHandle {
|
||||
generation: u8,
|
||||
key_held: bool,
|
||||
button_held: bool,
|
||||
retirement: Option<(std::sync::mpsc::Sender<()>, std::sync::mpsc::Receiver<()>)>,
|
||||
}
|
||||
impl Drop for TestHandle {
|
||||
fn drop(&mut self) {
|
||||
if let Some((entered, resume)) = self.retirement.take() {
|
||||
let _ = entered.send(());
|
||||
let _ = resume.recv_timeout(std::time::Duration::from_secs(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
fn release_input(current: Arc<Mutex<TestHandle>>) -> (std::thread::JoinHandle<()>, std::sync::mpsc::Receiver<(u8, bool, bool)>) {
|
||||
let (released, received) = std::sync::mpsc::channel();
|
||||
let worker = std::thread::spawn(move || {
|
||||
let mut handle = current.lock().unwrap();
|
||||
handle.key_held = false;
|
||||
handle.button_held = false;
|
||||
released.send((handle.generation, handle.key_held, handle.button_held)).unwrap();
|
||||
});
|
||||
(worker, received)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recovery_rejects_physical_or_logical_geometry_changes() {
|
||||
let original = Fixture::new((1920, 1080), (1280, 720));
|
||||
let same = Fixture::new((1920, 1080), (1280, 720));
|
||||
let physical_changed = Fixture::new((2560, 1440), (1280, 720));
|
||||
let logical_changed = Fixture::new((1920, 1080), (1600, 900));
|
||||
fn recovery_does_not_block_input_releases_during_open_or_close() {
|
||||
use std::{sync::mpsc::channel, time::Duration};
|
||||
let timeout = Duration::from_secs(3);
|
||||
let (opening, opened) = channel();
|
||||
let (resume_open, wait_open) = channel();
|
||||
let (closing, closed) = channel();
|
||||
let (resume_close, wait_close) = channel();
|
||||
let current = Arc::new(Mutex::new(TestHandle {
|
||||
generation: 1, key_held: true, button_held: true, retirement: Some((closing, wait_close)),
|
||||
}));
|
||||
let retry = Arc::new(Mutex::new(None::<std::time::Instant>));
|
||||
let recovery = {
|
||||
let current = current.clone();
|
||||
let retry = retry.clone();
|
||||
std::thread::spawn(move || {
|
||||
let handle = current.lock().unwrap();
|
||||
let _owner = retry.try_lock().unwrap();
|
||||
replace_handle(¤t, handle, || {
|
||||
opening.send(()).unwrap();
|
||||
wait_open.recv_timeout(Duration::from_secs(10)).unwrap();
|
||||
Ok(Some(TestHandle {
|
||||
generation: 2, key_held: true, button_held: true, retirement: None,
|
||||
}))
|
||||
})
|
||||
})
|
||||
};
|
||||
let opening_result = opened.recv_timeout(timeout);
|
||||
let (first_worker, first_release) = release_input(current.clone());
|
||||
let first_result = first_release.recv_timeout(timeout);
|
||||
// A concurrent capture must not wait on recovery while retaining input's mutex.
|
||||
let (contended, contention) = channel();
|
||||
let contender = {
|
||||
let current = current.clone();
|
||||
let retry = retry.clone();
|
||||
std::thread::spawn(move || {
|
||||
let _handle = current.lock().unwrap();
|
||||
contended.send(matches!(retry.try_lock(), Err(TryLockError::WouldBlock))).unwrap();
|
||||
})
|
||||
};
|
||||
let contention_result = contention.recv_timeout(timeout);
|
||||
let _ = resume_open.send(());
|
||||
let closing_result = closed.recv_timeout(timeout);
|
||||
let (second_worker, second_release) = release_input(current.clone());
|
||||
let second_result = second_release.recv_timeout(timeout);
|
||||
// Always release blocked work before asserting, including on regression.
|
||||
let _ = resume_close.send(());
|
||||
let recovered = recovery.join().unwrap();
|
||||
first_worker.join().unwrap();
|
||||
second_worker.join().unwrap();
|
||||
contender.join().unwrap();
|
||||
assert!(opening_result.is_ok() && closing_result.is_ok());
|
||||
assert_eq!(first_result.unwrap(), (1, false, false));
|
||||
assert_eq!(second_result.unwrap(), (2, false, false));
|
||||
assert!(contention_result.unwrap());
|
||||
assert!(recovered.unwrap());
|
||||
}
|
||||
|
||||
assert!(original.session().check_geometry(same.session()).is_ok());
|
||||
assert!(original.session().check_geometry(physical_changed.session()).is_err());
|
||||
assert!(original.session().check_geometry(logical_changed.session()).is_err());
|
||||
#[test]
|
||||
fn failed_or_deferred_recovery_keeps_the_current_handle_unlocked() {
|
||||
let current = Mutex::new(1);
|
||||
for failure in [false, true] {
|
||||
let handle = current.lock().unwrap();
|
||||
let result = replace_handle(¤t, handle, || {
|
||||
assert_eq!(*current.try_lock().unwrap(), 1);
|
||||
if failure { Err("open failed".into()) } else { Ok(None) }
|
||||
});
|
||||
if failure { assert_eq!(result.unwrap_err(), "open failed"); }
|
||||
else { assert!(!result.unwrap()); }
|
||||
assert_eq!(*current.try_lock().unwrap(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reconnect_waits_for_recovery_and_rechecks_the_published_handle() {
|
||||
use std::{sync::mpsc::channel, time::Duration};
|
||||
for succeeds in [false, true] {
|
||||
let current = Arc::new(Mutex::new(false));
|
||||
let recovery = Arc::new(Mutex::new(Recovery::default()));
|
||||
let (opening, opened) = channel();
|
||||
let (resume, wait) = channel();
|
||||
let (checked, observed) = channel();
|
||||
let (starting, started) = channel();
|
||||
let worker = {
|
||||
let current = current.clone();
|
||||
let recovery = recovery.clone();
|
||||
std::thread::spawn(move || {
|
||||
let handle = current.lock().unwrap();
|
||||
let _owner = recovery.lock().unwrap();
|
||||
replace_handle(¤t, handle, || {
|
||||
opening.send(()).unwrap();
|
||||
wait.recv_timeout(Duration::from_secs(10)).unwrap();
|
||||
if succeeds { Ok(Some(true)) } else { Ok(None) }
|
||||
}).unwrap()
|
||||
})
|
||||
};
|
||||
let ready = opened.recv_timeout(Duration::from_secs(3));
|
||||
let reconnect = {
|
||||
let current = current.clone();
|
||||
let recovery = recovery.clone();
|
||||
std::thread::spawn(move || {
|
||||
starting.send(()).unwrap();
|
||||
retire_if_closed(¤t, &recovery, |alive| {
|
||||
checked.send(*alive).unwrap();
|
||||
*alive
|
||||
}, || {
|
||||
assert!(current.try_lock().is_ok());
|
||||
true
|
||||
}).unwrap()
|
||||
})
|
||||
};
|
||||
let started = started.recv_timeout(Duration::from_secs(3));
|
||||
// A reconnect waiting on recovery must leave input's mutex available.
|
||||
let input_available = current.try_lock().is_ok();
|
||||
let premature_check = observed.recv_timeout(Duration::from_millis(100));
|
||||
let _ = resume.send(());
|
||||
let recovered = worker.join().unwrap();
|
||||
let stale = reconnect.join().unwrap();
|
||||
assert!(ready.is_ok() && started.is_ok() && input_available);
|
||||
assert!(matches!(premature_check, Err(std::sync::mpsc::RecvTimeoutError::Timeout)));
|
||||
assert_eq!(observed.recv_timeout(Duration::from_secs(3)).unwrap(), succeeds);
|
||||
assert_eq!(recovered, succeeds);
|
||||
assert_eq!(stale, !succeeds);
|
||||
assert_eq!(recovery.lock().unwrap().retired, !succeeds);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reconnect_retirement_prevents_a_late_encoder_recovery() {
|
||||
let current = Mutex::new(false);
|
||||
let recovery = Mutex::new(Recovery::default());
|
||||
// Locked or unknown sessions must not be retired by a reconnect.
|
||||
assert!(!retire_if_closed(¤t, &recovery, |alive| *alive, || false).unwrap());
|
||||
assert!(!recovery.lock().unwrap().retired);
|
||||
assert!(retire_if_closed(¤t, &recovery, |alive| *alive, || true).unwrap());
|
||||
let mut recovery = recovery.lock().unwrap();
|
||||
recovery.retry_at = Some(std::time::Instant::now());
|
||||
assert!(!recovery.ready(std::time::Instant::now(), || panic!("retired encoder probed logind")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recovery_backoff_skips_lock_probes_until_the_deadline() {
|
||||
use std::{cell::Cell, time::{Duration, Instant}};
|
||||
let now = Instant::now();
|
||||
let probes = Cell::new(0);
|
||||
let unlocked = || { probes.set(probes.get() + 1); true };
|
||||
let mut recovery = Recovery::default();
|
||||
// Initial one-second grace period, polled at up to 144 fps.
|
||||
for millis in 0..1000 {
|
||||
assert!(!recovery.ready(now + Duration::from_millis(millis), unlocked));
|
||||
}
|
||||
assert_eq!(probes.get(), 0);
|
||||
assert!(recovery.ready(now + Duration::from_secs(1), unlocked));
|
||||
assert_eq!(probes.get(), 1);
|
||||
// A failed open schedules the existing five-second retry delay.
|
||||
recovery.retry_at = Some(now + Duration::from_secs(6));
|
||||
for millis in 1000..6000 {
|
||||
assert!(!recovery.ready(now + Duration::from_millis(millis), unlocked));
|
||||
}
|
||||
assert_eq!(probes.get(), 1);
|
||||
// A locked/unknown probe denies opening and is itself throttled.
|
||||
assert!(!recovery.ready(now + Duration::from_secs(6), || { probes.set(2); false }));
|
||||
assert!(!recovery.ready(now + Duration::from_millis(6999), || panic!("early lock probe")));
|
||||
assert!(recovery.ready(now + Duration::from_secs(7), unlocked));
|
||||
assert_eq!(probes.get(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_observation_survives_unlock_while_recovery_is_opening() {
|
||||
use std::{sync::{atomic::Ordering, mpsc::channel}, time::Duration};
|
||||
let mut fixture = Fixture::new((2, 2));
|
||||
let (locked, watch) = tokio::sync::watch::channel(Some(false));
|
||||
fixture.0.locked = watch;
|
||||
let session = fixture.session();
|
||||
std::thread::scope(|threads| {
|
||||
let (opening, opened) = channel();
|
||||
let (resume, wait) = channel();
|
||||
let recovery = threads.spawn(move || {
|
||||
let _owner = session.recovery.lock().unwrap();
|
||||
opening.send(()).unwrap();
|
||||
wait.recv_timeout(Duration::from_secs(10)).unwrap();
|
||||
});
|
||||
let ready = opened.recv_timeout(Duration::from_secs(3));
|
||||
locked.send_replace(Some(true));
|
||||
session.was_locked.store(true, Ordering::Release);
|
||||
locked.send_replace(Some(false));
|
||||
let (reset, result) = channel();
|
||||
let capture = threads.spawn(move || {
|
||||
let _handle = session.handle.lock().unwrap();
|
||||
assert_eq!(*session.locked.borrow(), Some(false));
|
||||
reset.send(session.take_lock_observation()).unwrap();
|
||||
});
|
||||
let consumed = result.recv_timeout(Duration::from_secs(3));
|
||||
let still_pending = session.was_locked.load(Ordering::Acquire);
|
||||
let _ = resume.send(());
|
||||
recovery.join().unwrap();
|
||||
capture.join().unwrap();
|
||||
assert!(ready.is_ok());
|
||||
assert!(!consumed.unwrap().unwrap());
|
||||
assert!(still_pending);
|
||||
});
|
||||
assert!(session.take_lock_observation().unwrap());
|
||||
assert!(!session.was_locked.load(std::sync::atomic::Ordering::Acquire));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,12 +537,12 @@ pub use native::Session as PortalSession;
|
||||
pub struct PortalSession { pub width: u32, pub height: u32 }
|
||||
#[cfg(not(all(target_os = "linux", feature = "portal")))]
|
||||
impl PortalSession {
|
||||
pub fn logical_size(&self) -> (u32, u32) { (self.width, self.height) }
|
||||
pub fn clipboard_supported(&self) -> bool { false }
|
||||
pub fn clipboard_ready(&self) -> bool { false }
|
||||
pub fn clipboard_set(&self, _: &str) -> Result<(), String> { Err(error(2)) }
|
||||
pub fn clipboard_poll(&self) -> Option<String> { None }
|
||||
pub fn is_alive(&self) -> bool { false }
|
||||
pub fn retire_if_closed(&self) -> Result<bool, String> { Ok(false) }
|
||||
pub fn open() -> Result<Self, String> { Err("Portal backend requires Linux and --features portal".into()) }
|
||||
pub fn capture(&self, _: &mut [u8]) -> Result<(), String> { Err(error(2)) }
|
||||
pub fn capture_changed(&self, _: &mut [u8], _: &mut Option<FrameVersion>) -> Result<bool, String> { Err(error(2)) }
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use webrtc::{api::setting_engine::SettingEngine, ice::{mdns::MulticastDnsMode, network_type::NetworkType},
|
||||
util::vnet::{net::{Net, NetConfig}, router::{Router, RouterConfig}}};
|
||||
|
||||
pub(crate) async fn virtual_network() -> (SettingEngine, SettingEngine, Arc<Mutex<Router>>) {
|
||||
// Each pair owns its router and address space; no host interfaces or sockets are used.
|
||||
let router = Arc::new(Mutex::new(Router::new(RouterConfig {
|
||||
cidr: "192.0.2.0/24".into(), ..Default::default()
|
||||
}).unwrap()));
|
||||
let mut settings = Vec::new();
|
||||
for ip in ["192.0.2.1", "192.0.2.2"] {
|
||||
let net = Arc::new(Net::new(Some(NetConfig { static_ips: vec![ip.into()], ..Default::default() })));
|
||||
let nic = net.get_nic().unwrap();
|
||||
router.lock().await.add_net(nic.clone()).await.unwrap();
|
||||
nic.lock().await.set_router(router.clone()).await.unwrap();
|
||||
let mut setting = SettingEngine::default();
|
||||
setting.set_vnet(Some(net));
|
||||
setting.set_network_types(vec![NetworkType::Udp4]);
|
||||
setting.set_ice_multicast_dns_mode(MulticastDnsMode::Disabled);
|
||||
settings.push(setting);
|
||||
}
|
||||
router.lock().await.start().await.unwrap();
|
||||
let right = settings.pop().unwrap();
|
||||
(settings.pop().unwrap(), right, router)
|
||||
}
|
||||
+215
-18
@@ -14,6 +14,60 @@ use crate::capture_setting::{VideoCodec, VideoSettings};
|
||||
|
||||
/// Default frame rate; the viewer's 帧率 setting overrides it per connection.
|
||||
pub const FPS: u64 = 60;
|
||||
|
||||
/// Refresh rate reported to the viewer (display_info / capability / codec negotiation), like the
|
||||
/// official hosts report the monitor's mode (official Mac capability: 120). `UU_DISPLAY_FPS` overrides;
|
||||
/// Hyprland from `hyprctl -j monitors`, X11 from the current `xrandr` mode, otherwise 60.
|
||||
pub fn display_fps() -> u32 {
|
||||
static CACHE: std::sync::Mutex<Option<(std::time::Instant, u32)>> = std::sync::Mutex::new(None);
|
||||
let mut cache = CACHE.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some((at, fps)) = *cache { if at.elapsed() < Duration::from_secs(30) { return fps; } }
|
||||
let fps = std::env::var("UU_DISPLAY_FPS").ok().and_then(|v| v.trim().parse::<f64>().ok())
|
||||
.or_else(detect_refresh).map_or(FPS as u32, |hz| hz.round().clamp(24.0, 360.0) as u32);
|
||||
*cache = Some((std::time::Instant::now(), fps));
|
||||
fps
|
||||
}
|
||||
fn detect_refresh() -> Option<f64> {
|
||||
if cfg!(test) { return None; }
|
||||
// Bounded: a display tool must never stall connection setup (e.g. an X server started on demand).
|
||||
let run = |program: &str, args: &[&str]| -> Option<String> {
|
||||
use std::io::Read;
|
||||
let mut child = std::process::Command::new(program).args(args).stdin(std::process::Stdio::null())
|
||||
.stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null()).spawn().ok()?;
|
||||
let deadline = std::time::Instant::now() + Duration::from_secs(1);
|
||||
let status = loop {
|
||||
if let Some(status) = child.try_wait().ok()? { break status; }
|
||||
if std::time::Instant::now() >= deadline { let _ = child.kill(); let _ = child.wait(); return None; }
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
};
|
||||
let mut text = String::new();
|
||||
child.stdout.take()?.read_to_string(&mut text).ok()?;
|
||||
status.success().then_some(text)
|
||||
};
|
||||
if std::env::var_os("HYPRLAND_INSTANCE_SIGNATURE").is_some() {
|
||||
if let Some(hz) = run("hyprctl", &["-j", "monitors"]).as_deref().and_then(hyprland_refresh) { return Some(hz); }
|
||||
}
|
||||
if std::env::var_os("DISPLAY").is_some() { return run("xrandr", &["--current"]).as_deref().and_then(xrandr_refresh); }
|
||||
None
|
||||
}
|
||||
/// The focused monitor (else the first); the portal/Hyprland capture serves one output.
|
||||
fn hyprland_refresh(json: &str) -> Option<f64> {
|
||||
let monitors: Vec<serde_json::Value> = serde_json::from_str(json).ok()?;
|
||||
let monitor = monitors.iter().find(|m| m["focused"] == true).or(monitors.first())?;
|
||||
monitor["refreshRate"].as_f64().filter(|hz| *hz > 0.0)
|
||||
}
|
||||
/// The current mode (`60.00*+`) of the output marked `primary`, else of the first connected output.
|
||||
fn xrandr_refresh(text: &str) -> Option<f64> {
|
||||
let current = |lines: &mut dyn Iterator<Item = &str>| lines.take_while(|l| l.starts_with(' '))
|
||||
.flat_map(str::split_whitespace).find(|t| t.contains('*'))
|
||||
.and_then(|t| t.trim_end_matches(['*', '+']).parse::<f64>().ok());
|
||||
let mut lines = text.lines();
|
||||
while let Some(line) = lines.next() {
|
||||
if line.contains(" connected primary") { return current(&mut lines); }
|
||||
}
|
||||
let mut lines = text.lines().skip_while(|l| !l.contains(" connected")).skip(1);
|
||||
current(&mut lines)
|
||||
}
|
||||
/// Keyframe interval. All-intra 60fps measured 30 Mbit/s on the real desktop (saturating the
|
||||
/// viewer's Wi-Fi: 3-6 fps received). On a static desktop with NVENC CQ18 a 0.5s GOP measured
|
||||
/// 7.7 Mbit/s and a 2s GOP 1.9 Mbit/s. A broken prediction chain freezes until the next IDR or
|
||||
@@ -152,7 +206,15 @@ fn cpu_hevc_options(settings: &VideoSettings, gop: &str) -> Vec<String> {
|
||||
options
|
||||
}
|
||||
|
||||
fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str) -> Vec<String> {
|
||||
/// NVENC converts packed RGB on the GPU (BT.601 limited range, tagged bt470bg like the swscale default;
|
||||
/// compared on the 3090), so 4:2:0 without scaling skips the CPU BGRA→YUV pass (#16: ~2 cores at 4K).
|
||||
/// `input` is the source pixel format; `UU_NVENC_RGB=0` restores CPU conversion.
|
||||
fn nvenc_rgb_input(settings: &VideoSettings, encoder: Encoder, input: &'static str) -> Option<&'static str> {
|
||||
(encoder == Encoder::Nvenc && settings.chroma != crate::capture_setting::CHROMA_444 && settings.output.is_none()
|
||||
&& std::env::var("UU_NVENC_RGB").as_deref() != Ok("0")).then_some(input)
|
||||
}
|
||||
|
||||
fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str, rgb: Option<&'static str>) -> Vec<String> {
|
||||
let (maxrate, bufsize) = rate_args(settings, encoder);
|
||||
let cq = settings.cq().to_string();
|
||||
let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));
|
||||
@@ -160,7 +222,7 @@ fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str) -> Vec
|
||||
let (profile, pix_fmt) = chroma_args(settings, encoder);
|
||||
if encoder == Encoder::Nvenc {
|
||||
return ["-c:v", encoder.name_for(settings.codec), "-preset", "p4", "-tune", "ull",
|
||||
"-profile:v", profile, "-pix_fmt", pix_fmt, "-g", gop, "-bf", "0",
|
||||
"-profile:v", profile, "-pix_fmt", rgb.unwrap_or(pix_fmt), "-g", gop, "-bf", "0",
|
||||
"-rc", "vbr", "-cq", &cq, "-b:v", &target, "-maxrate", &maxrate, "-bufsize", &bufsize,
|
||||
"-rc-lookahead", "0", "-zerolatency", "1", "-delay", "0", "-aud", "1"]
|
||||
.into_iter().map(str::to_owned).collect();
|
||||
@@ -176,6 +238,11 @@ fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str) -> Vec
|
||||
options.into_iter().map(str::to_owned).collect()
|
||||
}
|
||||
|
||||
fn scale_options(settings: &VideoSettings, encoder: Encoder) -> Vec<String> {
|
||||
if encoder == Encoder::Vaapi { return vec!["-vf".into(), vaapi_filter(settings.output)]; }
|
||||
settings.output.map_or_else(Vec::new, |(w,h)| vec!["-vf".into(), format!("scale={w}:{h}:flags=bilinear")])
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
struct FfmpegSpec {
|
||||
program: PathBuf,
|
||||
@@ -291,7 +358,7 @@ pub async fn hevc_available() -> bool {
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
command.args(["-f", "lavfi", "-i", "color=black:size=256x256:rate=1", "-frames:v", "1", "-an"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vf", &vaapi_filter(None)]); }
|
||||
command.args(encoder_options(&settings, encoder, "30"));
|
||||
command.args(encoder_options(&settings, encoder, "30", None));
|
||||
command.args(["-f", "hevc", "pipe:1"]).kill_on_drop(true).stderr(std::process::Stdio::null());
|
||||
if let Ok(Ok(output)) = tokio::time::timeout(Duration::from_secs(5), command.output()).await {
|
||||
if output.status.success() && is_independent_frame_for(&output.stdout, VideoCodec::H265) {
|
||||
@@ -459,7 +526,9 @@ async fn read_encoder_with(
|
||||
let gop = gop_frames(settings.fps, false).to_string();
|
||||
let (maxrate, _) = rate_args(&settings, encoder);
|
||||
let cq = settings.cq().to_string();
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} pix_fmt={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name_for(settings.codec),if encoder == Encoder::Vaapi { "nv12" } else { chroma_args(&settings, encoder).1 });
|
||||
// x11grab delivers bgr0; the Wayland/portal/KMS pipe is bgra.
|
||||
let rgb = nvenc_rgb_input(&settings, encoder, if wayland { "bgra" } else { "bgr0" });
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} pix_fmt={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name_for(settings.codec),if encoder == Encoder::Vaapi { "nv12" } else { rgb.unwrap_or(chroma_args(&settings, encoder).1) });
|
||||
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
|
||||
let ffmpeg = ffmpeg_spec();
|
||||
let mut command = ffmpeg.command();
|
||||
@@ -474,12 +543,8 @@ async fn read_encoder_with(
|
||||
"-video_size",&size,"-i",&display,"-an"]);
|
||||
}
|
||||
// 其他分辨率: scale the captured desktop (the host display mode is left untouched).
|
||||
if encoder == Encoder::Vaapi {
|
||||
command.args(["-vf", &vaapi_filter(settings.output)]);
|
||||
} else if let Some((width, height)) = settings.output {
|
||||
command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]);
|
||||
}
|
||||
command.args(encoder_options(&settings, encoder, &gop));
|
||||
command.args(scale_options(&settings, encoder));
|
||||
command.args(encoder_options(&settings, encoder, &gop, rgb));
|
||||
// x11grab paces against an absolute schedule; after a capture stall it grabs
|
||||
// back-to-back to "catch up" (measured 69 frames in 200ms). vfr on a 1/FPS
|
||||
// timebase drops those same-slot frames instead of encoding a burst. The
|
||||
@@ -613,6 +678,97 @@ fn transport_closed(pc: &RTCPeerConnection) -> bool {
|
||||
|| pc.sctp().state() == webrtc::sctp_transport::sctp_transport_state::RTCSctpTransportState::Closed
|
||||
}
|
||||
|
||||
fn probe_settings() -> Result<VideoSettings,String> {
|
||||
let fps=crate::wayland::capture_fps(std::env::var("UU_PROBE_FPS").ok().as_deref())?;
|
||||
// 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")?,
|
||||
};
|
||||
Ok(VideoSettings {fps,quality,..VideoSettings::default()})
|
||||
}
|
||||
|
||||
fn probe_input_command(spec:&FfmpegSpec,input:&std::ffi::OsStr,loop_input:bool,settings:&VideoSettings,
|
||||
encoder:Encoder,wanted:u64,timing:FfmpegTimingMode)->Command {
|
||||
let mut command=spec.command();
|
||||
command.args(["-hide_banner","-loglevel","error","-nostdin"]);
|
||||
if encoder==Encoder::Vaapi { command.args(["-vaapi_device",&vaapi_device()]); }
|
||||
if loop_input { command.args(["-stream_loop","-1"]); }
|
||||
// Override source timestamps so clips and image sequences use the same nominal probe rate.
|
||||
command.args(["-r",&settings.fps.to_string(),"-i"]).arg(input).args(["-map","0:v:0","-an"]);
|
||||
command.args(scale_options(settings,encoder));
|
||||
command.args(encoder_options(settings,encoder,&gop_frames(settings.fps,false).to_string(),None));
|
||||
command.args(timing_args(timing,settings.fps));
|
||||
command.args(["-frames:v",&wanted.to_string(),"-f","h264","pipe:1"]);
|
||||
command
|
||||
}
|
||||
|
||||
async fn probe_input_size(spec:&FfmpegSpec,input:&std::ffi::OsStr)->Result<(u32,u32),String> {
|
||||
// Read only the PNG header; never log decoded pixels, input paths or FFmpeg diagnostics.
|
||||
let mut child=spec.command().args(["-hide_banner","-loglevel","error","-nostdin","-i"]).arg(input)
|
||||
.args(["-map","0:v:0","-frames:v","1","-c:v","png","-f","image2pipe","pipe:1"])
|
||||
.stdin(std::process::Stdio::null()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null())
|
||||
.kill_on_drop(true).spawn().map_err(|e|format!("Start fixed-input probe: {e}"))?;
|
||||
let mut header=[0u8;24];
|
||||
let result=tokio::time::timeout(Duration::from_secs(15),child.stdout.as_mut().unwrap().read_exact(&mut header)).await;
|
||||
let _=child.kill().await;
|
||||
if !matches!(result,Ok(Ok(_))) || &header[..8]!=b"\x89PNG\r\n\x1a\n" {
|
||||
return Err("Cannot open/decode UU_PROBE_INPUT video input".into());
|
||||
}
|
||||
Ok((u32::from_be_bytes(header[16..20].try_into().unwrap()),u32::from_be_bytes(header[20..24].try_into().unwrap())))
|
||||
}
|
||||
|
||||
/// Fixed input bypasses desktop initialization and always uses the FFmpeg process encoder.
|
||||
pub async fn probe_input(input:OsString,path:Option<String>)->Result<(),String> {
|
||||
use std::io::Write;
|
||||
let settings=probe_settings()?;
|
||||
let wanted:u64=std::env::var("UU_PROBE_FRAMES").ok().and_then(|v|v.parse().ok()).unwrap_or(5);
|
||||
if wanted==0 { return Err("Fixed-input UU_PROBE_FRAMES must be positive".into()); }
|
||||
let spec=ffmpeg_spec();
|
||||
let size=probe_input_size(&spec,&input).await?;
|
||||
let encoder=Encoder::requested();
|
||||
let (maxrate,_)=rate_args(&settings,encoder);
|
||||
let output=settings.output.unwrap_or(size);
|
||||
println!("Fixed input probe: source={}x{} output={}x{} fps={} cq={} maxrate={} encoder={}",
|
||||
size.0,size.1,output.0,output.1,settings.fps,settings.cq(),maxrate,encoder.name_for(settings.codec));
|
||||
let mut file=match path {
|
||||
Some(path)=>Some(std::fs::OpenOptions::new().write(true).create_new(true).open(path).map_err(|e|format!("Video probe file: {e}"))?),
|
||||
None=>None,
|
||||
};
|
||||
let mut command=probe_input_command(&spec,&input,std::env::var("UU_PROBE_LOOP").as_deref()==Ok("1"),
|
||||
&settings,encoder,wanted,ffmpeg_timing_mode(&spec).await);
|
||||
let mut child=command.stdin(std::process::Stdio::null()).stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::null()).kill_on_drop(true).spawn().map_err(|e|format!("Start fixed-input encoder: {e}"))?;
|
||||
let mut stdout=child.stdout.take().ok_or("Encoder stdout missing")?;
|
||||
let (mut count,mut bytes)=(0u64,0usize);
|
||||
tokio::time::timeout(Duration::from_secs(15+wanted/30),async {
|
||||
let mut pending=Vec::new();
|
||||
let mut chunk=[0u8;16384];
|
||||
loop {
|
||||
let n=stdout.read(&mut chunk).await.map_err(|e|format!("Encoder read: {e}"))?;
|
||||
pending.extend_from_slice(&chunk[..n]);
|
||||
loop {
|
||||
let aud=aud_offsets_for(&pending,settings.codec);
|
||||
let end=if aud.len()>1 { aud[1] } else if n==0 && !pending.is_empty() { pending.len() } else { break; };
|
||||
if end>MAX_ACCESS_UNIT { return Err("Encoder access unit exceeds limit".into()); }
|
||||
if count==0 && !is_independent_frame(&pending[..end]) { return Err("Video probe did not begin with IDR".into()); }
|
||||
if let Some(file)=file.as_mut() { file.write_all(&pending[..end]).map_err(|e|e.to_string())?; }
|
||||
bytes+=end; count+=1;
|
||||
pending.drain(..end);
|
||||
}
|
||||
if pending.len()>MAX_ACCESS_UNIT { return Err("Encoder access unit exceeds limit".into()); }
|
||||
if n==0 { break; }
|
||||
}
|
||||
let status=child.wait().await.map_err(|e|format!("Fixed-input encoder: {e}"))?;
|
||||
if !status.success() { return Err(format!("Fixed-input encoder failed ({status}); check input and encoder availability")); }
|
||||
if count!=wanted { return Err(format!("Fixed input ended after {count} frames; requested {wanted} (use UU_PROBE_LOOP=1 to repeat)")); }
|
||||
Ok::<(),String>(())
|
||||
}).await.map_err(|_|"Video probe timed out".to_owned())??;
|
||||
println!("Encoded capture probe: frames={count} h264_bytes={bytes} first_frame_idr=true");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Exercise the real capture/encoder chain without UU auth or a network peer.
|
||||
pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,path:Option<String>)->Result<(),String> {
|
||||
use std::io::Write;
|
||||
@@ -622,14 +778,7 @@ 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())?;
|
||||
// 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 settings=probe_settings()?;
|
||||
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;
|
||||
@@ -758,6 +907,54 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn fixed_input_arguments_share_production_encoding_options() {
|
||||
let spec=FfmpegSpec {program:"ffmpeg".into(),ld_library_path:None,path:None};
|
||||
for encoder in [Encoder::Cpu,Encoder::Nvenc,Encoder::Vaapi] {
|
||||
for quality in 1..=5 {
|
||||
for chroma in [crate::capture_setting::CHROMA_420,crate::capture_setting::CHROMA_444] {
|
||||
let settings=VideoSettings {fps:30,quality,chroma,output:Some((640,360)),..Default::default()};
|
||||
let command=probe_input_command(&spec,std::ffi::OsStr::new("frames/%05d.png"),false,
|
||||
&settings,encoder,17,FfmpegTimingMode::Modern);
|
||||
let args:Vec<_>=command.as_std().get_args().map(|a|a.to_str().unwrap().to_owned()).collect();
|
||||
let expected=encoder_options(&settings,encoder,&gop_frames(30,false).to_string(),None);
|
||||
assert!(args.windows(expected.len()).any(|w|w==expected));
|
||||
let scale=scale_options(&settings,encoder);
|
||||
assert!(args.windows(scale.len()).any(|w|w==scale));
|
||||
assert!(args.windows(4).any(|w|w==["-r","30","-i","frames/%05d.png"]));
|
||||
assert!(args.windows(2).any(|w|w==["-frames:v","17"]));
|
||||
assert!(args.windows(4).any(|w|w==["-fps_mode","vfr","-enc_time_base","1/30"]));
|
||||
assert!(!args.iter().any(|a|["x11grab","rawvideo","-stream_loop"].contains(&a.as_str())));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixed_input_loop_is_an_input_option_and_keeps_paths_literal() {
|
||||
let spec=FfmpegSpec {program:"ffmpeg".into(),ld_library_path:None,path:None};
|
||||
for input in ["clip.y4m","a clip.mp4","frames/%05d.png"] {
|
||||
let command=probe_input_command(&spec,std::ffi::OsStr::new(input),true,&VideoSettings::default(),
|
||||
Encoder::Cpu,5,FfmpegTimingMode::Legacy);
|
||||
let args:Vec<_>=command.as_std().get_args().map(|a|a.to_str().unwrap()).collect();
|
||||
assert!(args.windows(2).any(|w|w==["-i",input]));
|
||||
assert!(args.windows(2).any(|w|w==["-stream_loop","-1"]));
|
||||
assert!(args.iter().position(|a|*a=="-stream_loop")<args.iter().position(|a|*a=="-i"));
|
||||
assert!(args.windows(2).any(|w|w==["-vsync","0"]));
|
||||
assert!(!args.contains(&"-vf"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_refresh_parsers() {
|
||||
let xrandr = "Screen 0: minimum 8 x 8, current 5760 x 2160\nHDMI-1 connected 1920x1080+3840+0 (normal) 0mm x 0mm\n 1920x1080 60.00*+ 50.00\nDP-1 connected primary 3840x2160+0+0 (normal) 600mm x 340mm\n 3840x2160 60.00 + 143.98*\n 2560x1440 120.00\n";
|
||||
assert_eq!(xrandr_refresh(xrandr), Some(143.98));
|
||||
assert_eq!(xrandr_refresh("Screen 0:\nVGA-1 disconnected\nHDMI-1 connected 1920x1080+0+0\n 1920x1080 74.97*+ 60.00\n"), Some(74.97));
|
||||
assert_eq!(xrandr_refresh("Screen 0:\nHDMI-1 connected\n 1920x1080 60.00 +\n"), None);
|
||||
assert_eq!(hyprland_refresh(r#"[{"name":"HDMI-A-1","focused":false,"refreshRate":60.0},{"name":"DP-1","focused":true,"refreshRate":143.856}]"#), Some(143.856));
|
||||
assert_eq!(hyprland_refresh("[]"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_wayland_child_uses_requested_capture_rate() {
|
||||
for fps in [5,30,60,90,144] {
|
||||
|
||||
+4
-7
@@ -105,15 +105,11 @@ mod tests {
|
||||
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();
|
||||
let (remote_setting, host_setting, router) = crate::test_network::virtual_network().await;
|
||||
let remote = APIBuilder::new().with_setting_engine(remote_setting).with_media_engine(remote_media).build().new_peer_connection(RTCConfiguration::default()).await.unwrap();
|
||||
remote.add_transceiver_from_kind(RTPCodecType::Video, None).await.unwrap();
|
||||
let 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 host = APIBuilder::new().with_setting_engine(host_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();
|
||||
@@ -171,6 +167,7 @@ mod tests {
|
||||
previous = Some(packet);
|
||||
}
|
||||
host.close().await.unwrap(); remote.close().await.unwrap();
|
||||
router.lock().await.stop().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; }
|
||||
|
||||
+2
-2
@@ -165,7 +165,7 @@ impl WebRtcSession {
|
||||
tokio::spawn(async move {
|
||||
let ctx = ctx.lock().await.clone();
|
||||
if let (Some(client), Some(ice)) = (&ctx.client_id, ctx.ice_id.as_ref().and_then(|v| v.as_str())) {
|
||||
let payload = crate::capability::device_capability(client, ice, 0, crate::video::FPS as i32, 5, hevc);
|
||||
let payload = crate::capability::device_capability(client, ice, 0, crate::video::display_fps() as i32, 5, hevc);
|
||||
if let Err(e) = sig.emit_event(crate::capability::FORWARD_SETTING_EVENT, payload).await {
|
||||
log::error!("Device capability notification failed: {}", e);
|
||||
}
|
||||
@@ -470,7 +470,7 @@ impl WebRtcSession {
|
||||
(26, Ok(crate::initialization::envelope(next_seq(), timestamp, 26,
|
||||
&crate::system_metrics::remote_download_path(&home)))),
|
||||
(18, Ok(crate::initialization::envelope(next_seq(), timestamp, 18,
|
||||
&crate::capture_setting::codec_negotiation(screen_size, crate::video::FPS as u32, init_track.supports(crate::capture_setting::VideoCodec::H265))))),
|
||||
&crate::capture_setting::codec_negotiation(screen_size, crate::video::display_fps(), init_track.supports(crate::capture_setting::VideoCodec::H265))))),
|
||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
|
||||
];
|
||||
if desktop && screen.init_error.is_none() {
|
||||
|
||||
+39
-1
@@ -380,6 +380,24 @@ static void run_geometry(const struct geometry *geometry) {
|
||||
assert(h.events[0].type == EIS_EVENT_SCROLL_DISCRETE);
|
||||
assert(h.events[0].dx == 0 && h.events[0].dy == -120);
|
||||
|
||||
uint8_t storage[256];
|
||||
struct spa_pod_builder builder = SPA_POD_BUILDER_INIT(storage, sizeof(storage));
|
||||
const struct spa_pod *format = spa_format_video_raw_build(&builder, SPA_PARAM_Format,
|
||||
&SPA_VIDEO_INFO_RAW_INIT(.format=SPA_VIDEO_FORMAT_BGRA,
|
||||
.size=SPA_RECTANGLE(geometry->width, geometry->height), .framerate=SPA_FRACTION(60,1)));
|
||||
assert(format);
|
||||
stream_format(h.portal, SPA_PARAM_Format, format);
|
||||
stream_state(h.portal, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_STREAMING, NULL);
|
||||
stream_state(h.portal, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
|
||||
stream_format(h.portal, SPA_PARAM_Format, NULL);
|
||||
/* Expire only video recovery; keep EIS and the harness wall clock live. */
|
||||
pthread_mutex_lock(&h.portal->lock);
|
||||
h.portal->interrupted_at = g_get_monotonic_time() - PORTAL_RENEGOTIATION_TIMEOUT - 1;
|
||||
pthread_mutex_unlock(&h.portal->lock);
|
||||
assert(!uu_portal_capture_alive(h.portal));
|
||||
uint8_t pixel = 99;
|
||||
assert(uu_portal_capture_since(h.portal, &pixel, sizeof(pixel), NULL) == 9 && pixel == 99);
|
||||
|
||||
clear_events(&h);
|
||||
assert(uu_portal_key(h.portal, 'a', 30, 0) == 0);
|
||||
wait_for_input(&h);
|
||||
@@ -392,6 +410,24 @@ static void run_geometry(const struct geometry *geometry) {
|
||||
assert(h.events[0].type == EIS_EVENT_BUTTON_BUTTON);
|
||||
assert(h.events[0].code == 0x110 && !h.events[0].pressed);
|
||||
|
||||
stream_format(h.portal, SPA_PARAM_Format, format);
|
||||
stream_state(h.portal, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_STREAMING, NULL);
|
||||
assert(uu_portal_capture_alive(h.portal) && !h.portal->interrupted_at);
|
||||
/* Natural video recovery keeps this EIS device, with neither input held. */
|
||||
assert(h.portal->ei_devices->data == device && h.connect_calls == 1 && h.starts == 1);
|
||||
for (int pressed = 1; pressed >= 0; pressed--) {
|
||||
clear_events(&h);
|
||||
assert(uu_portal_key(h.portal, 'a', 30, pressed) == 0);
|
||||
wait_for_input(&h);
|
||||
assert(h.events[0].type == EIS_EVENT_KEYBOARD_KEY);
|
||||
assert(h.events[0].code == 30 && h.events[0].pressed == pressed);
|
||||
clear_events(&h);
|
||||
assert(uu_portal_button(h.portal, 1, pressed) == 0);
|
||||
wait_for_input(&h);
|
||||
assert(h.events[0].type == EIS_EVENT_BUTTON_BUTTON);
|
||||
assert(h.events[0].code == 0x110 && h.events[0].pressed == pressed);
|
||||
}
|
||||
|
||||
pause_resume(&h);
|
||||
|
||||
clear_events(&h);
|
||||
@@ -436,6 +472,8 @@ int main(void) {
|
||||
{3840, 2160, 2560, 1440, 100, 200}, /* 150% */
|
||||
{3840, 2160, 2194, 1234, 0, 0}, /* 175%, rounded logical size */
|
||||
{3840, 2160, 1920, 1080, 320, 90}, /* 200% */
|
||||
{3840, 2160, 2304, 1296, 320, 90}, /* replacement at 166.67% */
|
||||
{3840, 2160, 1920, 1080, 320, 90}, /* replacement back at 200% */
|
||||
{5120, 2880, 2560, 1440, 0, 0}, /* 5K */
|
||||
{7680, 4320, 3840, 2160, 7680, 0}, /* 8K, nonzero origin */
|
||||
{3440, 1440, 2293, 960, 0, 1080}, /* ultrawide */
|
||||
@@ -443,6 +481,6 @@ int main(void) {
|
||||
{2257, 1505, 1806, 1204, 0, 0}, /* odd frame dimensions */
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) run_geometry(&cases[i]);
|
||||
puts("Portal EIS integration checks passed: 11 geometries");
|
||||
printf("Portal EIS integration checks passed: %zu geometries\n", sizeof(cases) / sizeof(cases[0]));
|
||||
return 0;
|
||||
}
|
||||
@@ -1,7 +1,190 @@
|
||||
#define _GNU_SOURCE
|
||||
/* Protocol-independent C bounds/lifecycle checks; never requests consent. */
|
||||
#include <gio/gio.h>
|
||||
#include <assert.h>
|
||||
static gint64 test_monotonic_time = 1000000;
|
||||
static gint64 portal_test_monotonic_time(void) { return test_monotonic_time; }
|
||||
#define g_get_monotonic_time portal_test_monotonic_time
|
||||
#include "../c_engine/portal_engine.c"
|
||||
#undef g_get_monotonic_time
|
||||
|
||||
static struct uu_portal *test_portal(void) {
|
||||
struct uu_portal *p = calloc(1, sizeof(*p));
|
||||
assert(p);
|
||||
pthread_mutex_init(&p->lock, NULL);
|
||||
pthread_cond_init(&p->changed, NULL);
|
||||
return p;
|
||||
}
|
||||
static void test_format(struct uu_portal *p, uint32_t width, uint32_t height) {
|
||||
uint8_t storage[256];
|
||||
struct spa_pod_builder builder = SPA_POD_BUILDER_INIT(storage, sizeof(storage));
|
||||
const struct spa_pod *format = spa_format_video_raw_build(&builder, SPA_PARAM_Format,
|
||||
&SPA_VIDEO_INFO_RAW_INIT(.format=SPA_VIDEO_FORMAT_BGRA, .size=SPA_RECTANGLE(width,height),
|
||||
.framerate=SPA_FRACTION(60,1)));
|
||||
assert(format);
|
||||
stream_format(p, SPA_PARAM_Format, format);
|
||||
}
|
||||
static void test_frame(struct uu_portal *p) {
|
||||
uint8_t pixels[16] = {1,2,3,255, 4,5,6,255, 7,8,9,255, 10,11,12,255};
|
||||
struct spa_chunk chunk = {.offset=0, .size=sizeof(pixels), .stride=8};
|
||||
struct spa_data data = {.type=SPA_DATA_MemPtr, .data=pixels, .maxsize=sizeof(pixels), .chunk=&chunk};
|
||||
struct spa_buffer buffer = {.n_datas=1, .datas=&data};
|
||||
copy_video_buffer(p, &buffer);
|
||||
}
|
||||
static void assert_capture_ok(struct uu_portal *p, uint8_t *pixels) {
|
||||
uint64_t revision = p->revision + 1;
|
||||
assert(uu_portal_capture_since(p, pixels, p->frame_length, &revision) == 0);
|
||||
}
|
||||
static void portal_renegotiation_timeout(void) {
|
||||
uint8_t pixels[16];
|
||||
struct uu_portal *p = test_portal();
|
||||
test_monotonic_time = 1000000;
|
||||
test_format(p, 2, 2);
|
||||
assert(p->format_valid);
|
||||
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
|
||||
test_frame(p);
|
||||
assert(p->sequence == 1 && uu_portal_capture_alive(p));
|
||||
assert_capture_ok(p, pixels);
|
||||
|
||||
gint64 paused_at = test_monotonic_time;
|
||||
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
|
||||
assert(!p->interrupted_at);
|
||||
stream_format(p, SPA_PARAM_Format, NULL);
|
||||
assert(!p->format_valid && p->interrupted_at == paused_at && uu_portal_capture_alive(p));
|
||||
uint64_t sequence = p->sequence;
|
||||
test_frame(p);
|
||||
assert(p->sequence == sequence); /* Buffers without a negotiated format are ignored. */
|
||||
|
||||
test_monotonic_time = paused_at + 3 * G_TIME_SPAN_SECOND;
|
||||
stream_state(p, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_PAUSED, NULL);
|
||||
stream_format(p, SPA_PARAM_Format, NULL);
|
||||
test_monotonic_time = paused_at + 4 * G_TIME_SPAN_SECOND;
|
||||
stream_format(p, SPA_PARAM_Format, NULL);
|
||||
assert(p->interrupted_at == paused_at); /* Repeated NULL formats do not restart the deadline. */
|
||||
assert(uu_portal_capture_alive(p));
|
||||
assert_capture_ok(p, pixels);
|
||||
|
||||
test_monotonic_time = paused_at + 5 * G_TIME_SPAN_SECOND - 1;
|
||||
assert(uu_portal_capture_alive(p));
|
||||
assert_capture_ok(p, pixels);
|
||||
test_monotonic_time = paused_at + 5 * G_TIME_SPAN_SECOND;
|
||||
assert(!uu_portal_capture_alive(p) && uu_portal_alive(p));
|
||||
uint64_t revision = 0;
|
||||
memset(pixels, 99, sizeof(pixels));
|
||||
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 9);
|
||||
assert(!revision && pixels[0] == 99 && pixels[15] == 99);
|
||||
/* Late renegotiation can recover naturally before the worker replaces it. */
|
||||
test_format(p, 2, 2);
|
||||
stream_state(p, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_STREAMING, NULL);
|
||||
test_frame(p);
|
||||
assert(uu_portal_capture_alive(p) && p->revision == 2);
|
||||
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 0);
|
||||
assert(revision == 2 && pixels[0] == 1);
|
||||
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 12);
|
||||
uu_portal_close(p);
|
||||
}
|
||||
static void portal_renegotiation_recovery(void) {
|
||||
uint8_t pixels[16];
|
||||
struct uu_portal *p = test_portal();
|
||||
test_monotonic_time = 2000000;
|
||||
test_format(p, 2, 2);
|
||||
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
|
||||
test_frame(p);
|
||||
gint64 paused_at = test_monotonic_time;
|
||||
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
|
||||
stream_format(p, SPA_PARAM_Format, NULL);
|
||||
|
||||
test_monotonic_time = paused_at + 2 * G_TIME_SPAN_SECOND;
|
||||
test_format(p, 2, 2);
|
||||
assert(p->format_valid && !p->interrupted_at);
|
||||
stream_state(p, PW_STREAM_STATE_PAUSED, PW_STREAM_STATE_STREAMING, NULL);
|
||||
assert(p->interrupted_at == 0 && uu_portal_capture_alive(p));
|
||||
test_monotonic_time = paused_at + 6 * G_TIME_SPAN_SECOND;
|
||||
assert(uu_portal_capture_alive(p));
|
||||
assert_capture_ok(p, pixels);
|
||||
uu_portal_close(p);
|
||||
}
|
||||
static void portal_renegotiation_event_order_and_healthy_states(void) {
|
||||
uint8_t pixels[16];
|
||||
struct uu_portal *p = test_portal();
|
||||
test_monotonic_time = 3000000;
|
||||
test_format(p, 2, 2);
|
||||
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
|
||||
test_frame(p);
|
||||
stream_format(p, SPA_PARAM_Format, NULL); /* Format can disappear before the state callback. */
|
||||
assert(!p->format_valid && !p->interrupted_at && uu_portal_capture_alive(p));
|
||||
test_monotonic_time += 20 * G_TIME_SPAN_SECOND;
|
||||
assert(uu_portal_capture_alive(p));
|
||||
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
|
||||
gint64 paused_at = test_monotonic_time;
|
||||
assert(p->interrupted_at == paused_at);
|
||||
test_monotonic_time = paused_at + 5 * G_TIME_SPAN_SECOND;
|
||||
assert(!uu_portal_capture_alive(p) && uu_portal_alive(p));
|
||||
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), NULL) == 9);
|
||||
uu_portal_close(p);
|
||||
|
||||
p = test_portal();
|
||||
test_monotonic_time = 4000000;
|
||||
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_PAUSED, NULL);
|
||||
assert(!p->interrupted_at);
|
||||
test_monotonic_time += 60 * G_TIME_SPAN_SECOND;
|
||||
assert(uu_portal_capture_alive(p)); /* A stream that never reached STREAMING has no recovery deadline. */
|
||||
assert(uu_portal_capture(p, pixels, sizeof(pixels)) == 10);
|
||||
uu_portal_close(p);
|
||||
|
||||
p = test_portal();
|
||||
test_monotonic_time = 5000000;
|
||||
test_format(p, 2, 2);
|
||||
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
|
||||
test_frame(p);
|
||||
uint64_t revision = 0;
|
||||
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 0);
|
||||
test_monotonic_time += 60 * G_TIME_SPAN_SECOND;
|
||||
assert(uu_portal_capture_alive(p)); /* A static screen can stream without producing a new frame. */
|
||||
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), &revision) == 12);
|
||||
uu_portal_close(p);
|
||||
|
||||
p = test_portal();
|
||||
test_monotonic_time = 6000000;
|
||||
test_format(p, 2, 2);
|
||||
stream_state(p, PW_STREAM_STATE_UNCONNECTED, PW_STREAM_STATE_STREAMING, NULL);
|
||||
test_frame(p);
|
||||
stream_state(p, PW_STREAM_STATE_STREAMING, PW_STREAM_STATE_PAUSED, NULL);
|
||||
test_monotonic_time += 6 * G_TIME_SPAN_SECOND;
|
||||
assert(p->format_valid && uu_portal_capture_alive(p)); /* PAUSED alone is not a format failure. */
|
||||
assert_capture_ok(p, pixels);
|
||||
assert(!p->interrupted_at);
|
||||
/* Healthy paused time must not consume a later negotiation's allowance. */
|
||||
stream_format(p, SPA_PARAM_Format, NULL);
|
||||
paused_at = test_monotonic_time;
|
||||
assert(p->interrupted_at == paused_at && uu_portal_capture_alive(p));
|
||||
test_monotonic_time += 4 * G_TIME_SPAN_SECOND;
|
||||
assert(uu_portal_capture_alive(p));
|
||||
test_format(p, 2, 2);
|
||||
assert(!p->interrupted_at && uu_portal_capture_alive(p));
|
||||
/* A second format removal while still paused gets a new full deadline. */
|
||||
test_monotonic_time += 20 * G_TIME_SPAN_SECOND;
|
||||
stream_format(p, SPA_PARAM_Format, NULL);
|
||||
paused_at = test_monotonic_time;
|
||||
assert(p->interrupted_at == paused_at && uu_portal_capture_alive(p));
|
||||
test_monotonic_time += 5 * G_TIME_SPAN_SECOND - 1;
|
||||
assert(uu_portal_capture_alive(p));
|
||||
test_monotonic_time++;
|
||||
assert(!uu_portal_capture_alive(p) && uu_portal_alive(p));
|
||||
uu_portal_close(p);
|
||||
}
|
||||
static void portal_changed_physical_format_still_fails(void) {
|
||||
struct uu_portal *p = test_portal();
|
||||
uint8_t pixels[24];
|
||||
test_monotonic_time = 7000000;
|
||||
test_format(p, 2, 2);
|
||||
assert(p->format_valid && !p->error);
|
||||
test_format(p, 3, 2);
|
||||
assert(p->error == 8 && !uu_portal_capture_alive(p));
|
||||
assert(uu_portal_capture_since(p, pixels, sizeof(pixels), NULL) == 8);
|
||||
uu_portal_close(p);
|
||||
}
|
||||
|
||||
static void clipboard_bounds(void) {
|
||||
int fds[2]; uint8_t bytes[8]={0};
|
||||
assert(pipe2(fds,O_NONBLOCK)==0);
|
||||
@@ -65,6 +248,10 @@ static void authorization_still_required(void) {
|
||||
}
|
||||
}
|
||||
int main(void) {
|
||||
portal_renegotiation_timeout();
|
||||
portal_renegotiation_recovery();
|
||||
portal_renegotiation_event_order_and_healthy_states();
|
||||
portal_changed_physical_format_still_fails();
|
||||
clipboard_bounds();
|
||||
capture_with_optional_geometry(0);
|
||||
capture_with_optional_geometry(1);
|
||||
|
||||
+4
-1
@@ -156,7 +156,10 @@ impl RTCSctpTransport {
|
||||
association = sctp::association::Association::client(sctp::association::Config {
|
||||
net_conn: Arc::clone(net_conn) as Arc<dyn Conn + Send + Sync>,
|
||||
max_receive_buffer_size: 0,
|
||||
max_message_size: 0,
|
||||
// uu-remote-linux patch: the association's send limit ignores the remote
|
||||
// a=max-message-size and defaults to 64 KB; libwebrtc peers (official viewers,
|
||||
// our answers/offers) advertise 262144. A 5600-entry directory reply is ~73 KB.
|
||||
max_message_size: 262_144,
|
||||
name: String::new(),
|
||||
}) => {
|
||||
break Arc::new(association?);
|
||||
|
||||
Reference in new issue
Block a user